fennec
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
30
31
32#ifndef FENNEC_CONTAINERS_ARRAY_H
33#define FENNEC_CONTAINERS_ARRAY_H
34
35#include <fennec/lang/types.h>
36#include <fennec/lang/assert.h>
38
39namespace fennec
40{
41
63template<typename ValueT, size_t ElemV>
64struct array {
65
66// Definitions =========================================================================================================
67public:
68 using value_t = ValueT;
69
70// Public Members ======================================================================================================
71
74
77 value_t data[ElemV];
78
80
81
82
83// Properties ==========================================================================================================
84
87
91 [[nodiscard]] constexpr size_t size() const { return ElemV; }
92
96 [[nodiscard]] constexpr bool_t empty() const { return ElemV == 0; }
97
99
100
101
102// Access ==============================================================================================================
103
106
109 constexpr value_t& operator[](size_t i) {
110 assertd(i < ElemV, "Array Out of Bounds");
111 return data[i];
112 }
113
125 constexpr const value_t& operator[](size_t i) const {
126 assertd(i < ElemV, "Array Out of Bounds");
127 return data[i];
128 }
129
133 constexpr value_t& front() {
134 return data[0];
135 }
136
140 constexpr const value_t& front() const {
141 return data[0];
142 }
143
147 constexpr value_t& back() {
148 return data[ElemV - 1];
149 }
150
154 constexpr const value_t& back() const {
155 return data[ElemV - 1];
156 }
157
159
160
161
162// Comparison ==========================================================================================================
163
166
169 friend constexpr bool_t operator==(const array& lhs, const array& rhs) {
170 return array::_compare(lhs, rhs, make_index_metasequence<ElemV>{});
171 }
172
175 friend constexpr bool_t operator!=(const array& lhs, const array& rhs) {
176 return not array::_compare(lhs, rhs, make_index_metasequence<ElemV>{});
177 }
178
180
181
182// Iteration ===========================================================================================================
183
186
190 constexpr value_t* begin() {
191 return data;
192 }
193
197 constexpr value_t* end() {
198 return data + ElemV;
199 }
200
201
202
206 constexpr const value_t* begin() const {
207 return data;
208 }
209
213 constexpr const value_t* end() const {
214 return data + ElemV;
215 }
216
218
219private:
220 template<size_t...i>
221 static bool _compare(const array& lhs, const array& rhs, index_metasequence<i...>) {
222 return ((lhs[i] == rhs[i]) && ...);
223 }
224};
225
226}
227
228#endif // FENNEC_CONTAINERS_ARRAY_H
Assertions
Metasequences
Data Structure that defines a compile-time allocated array.
Definition array.h:64
constexpr value_t & back()
Access the first element.
Definition array.h:147
value_t data[ElemV]
backing c-style array handle
Definition array.h:77
constexpr const value_t & front() const
Const Access the first element.
Definition array.h:140
constexpr value_t * begin()
C++ Iterator Specification begin()
Definition array.h:190
constexpr const value_t * begin() const
Const C++ Iterator Specification begin()
Definition array.h:206
friend constexpr bool_t operator==(const array &lhs, const array &rhs)
Checks if all elements in the arrays are equal.
Definition array.h:169
constexpr value_t & operator[](size_t i)
Definition array.h:109
constexpr size_t size() const
returns the number of elements in the array
Definition array.h:91
constexpr bool_t empty() const
returns true when the array is empty
Definition array.h:96
constexpr value_t & front()
Access the first element.
Definition array.h:133
friend constexpr bool_t operator!=(const array &lhs, const array &rhs)
Checks if any element in the arrays is not equal.
Definition array.h:175
constexpr const value_t & back() const
Const Access the first element.
Definition array.h:154
constexpr const value_t & operator[](size_t i) const
access specified element
Definition array.h:125
constexpr const value_t * end() const
Const C++ Iterator Specification end()
Definition array.h:213
ValueT value_t
Alias for ValueT
Definition array.h:68
constexpr value_t * end()
C++ Iterator Specification end()
Definition array.h:197
metaprogramming integral metasequence
Definition metasequences.h:162
generate a fennec::index_metasequence
Definition metasequences.h:223
Types
bool bool_t
A conditional type.
Definition types.h:214