BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1
87#pragma once
88#include <b8/assert.h>
89#include <cstddef>
90#include <stdio.h>
91#include <stdlib.h>
92#include <trace.h>
93
94namespace ArrayOptions {
102}
103
110template <typename T, size_t Capacity>
111class b8array {
112private:
113 T* _buff = nullptr;
114 size_t _size = 0;
115
119 void alloc() {
120 _buff = new T[Capacity + 1];
121 }
122
128 void clone(const b8array& obj) {
129 if (_buff) {
130 delete[] _buff;
131 }
132 _size = obj._size;
133 alloc();
134 for (size_t nn = 0; nn < _size; ++nn) {
135 _buff[nn] = obj._buff[nn];
136 }
137 }
138
139public:
145 b8array(ArrayOptions::InitializationOption option = ArrayOptions::NoFill) {
146 alloc();
147 if (option == ArrayOptions::Fill) {
148 T t{};
149 for (size_t ii = 0; ii < Capacity; ++ii) {
150 push_back(t);
151 }
152 }
153 }
154
160 b8array(std::initializer_list<T> il) {
161 alloc();
162 size_t index = 0;
163 for (const auto& val : il) {
164 _ASSERT(index < Capacity, "Capacity exceeded by initializer list");
165 if (index < Capacity) {
166 _buff[index++] = val;
167 _size++;
168 }
169 }
170 }
171
177 b8array(const b8array& obj) {
178 clone(obj);
179 }
180
187 b8array& operator=(const b8array& obj) {
188 if (this != &obj) {
189 clone(obj);
190 }
191 return *this;
192 }
193
198 if (_buff) {
199 delete[] _buff;
200 }
201 }
202
208 bool empty() const {
209 return _size == 0;
210 }
211
217 T* begin() {
218 return _buff;
219 }
220
226 const T* cbegin() const {
227 return _buff;
228 }
229
235 const T* cend() const {
236 return _buff + _size;
237 }
238
244 T* end() {
245 return _buff + _size;
246 }
247
253 const T* begin() const {
254 return _buff;
255 }
256
262 const T* end() const {
263 return _buff + _size;
264 }
265
272 T* erase(T* it_) {
273 _ASSERT(it_ >= _buff && it_ < _buff + _size, "Invalid iterator");
274 if (it_ < _buff || it_ >= _buff + _size) {
275 return _buff + _size;
276 }
277 for (T* p = it_; p < _buff + _size - 1; ++p) {
278 *p = *(p + 1);
279 }
280 --_size;
281 return it_;
282 }
283
287 void clear() {
288 _size = 0;
289 }
290
296 size_t size() const {
297 return _size;
298 }
299
305 void push_back(const T& x_) {
306 _ASSERT(_size < Capacity, "over flow");
307 _buff[_size++] = x_;
308 }
309
316 T& at(int n_) {
317 _ASSERT(n_ >= 0, "array exception");
318 _ASSERT(n_ < static_cast<int>(size()), "array exception");
319 return _buff[n_];
320 }
321
328 const T& at(int n_) const {
329 _ASSERT(n_ >= 0, "array exception");
330 _ASSERT(n_ < static_cast<int>(size()), "array exception");
331 return _buff[n_];
332 }
339 T& operator[](int n_) {
340 _ASSERT(n_ >= 0, "array exception");
341 _ASSERT(n_ < static_cast<int>(size()), "array exception");
342 return _buff[n_];
343 }
344
351 const T& operator[](int n_) const {
352 _ASSERT(n_ >= 0, "array exception");
353 _ASSERT(n_ < static_cast<int>(size()), "array exception");
354 return _buff[n_];
355 }
356};
InitializationOption
Enum for specifying array initialization options.
Definition array.h:98
@ NoFill
Do not fill the array.
Definition array.h:100
@ Fill
Fill the array with default-constructed elements.
Definition array.h:99
A fixed-size array with custom behavior for element initialization.
Definition array.h:111
void clear()
Clear the contents of the array.
Definition array.h:287
b8array(ArrayOptions::InitializationOption option=ArrayOptions::NoFill)
Construct a new b8array.
Definition array.h:145
T * begin()
Get an iterator pointing to the first element.
Definition array.h:217
T & at(int n_)
Access element at a specific position.
Definition array.h:316
b8array(const b8array &obj)
Copy constructor for b8array.
Definition array.h:177
const T * end() const
Get a const iterator pointing to one past the last element.
Definition array.h:262
const T * begin() const
Get a const iterator pointing to the first element.
Definition array.h:253
T * end()
Get an iterator pointing to one past the last element.
Definition array.h:244
b8array & operator=(const b8array &obj)
Copy assignment operator.
Definition array.h:187
bool empty() const
Check if the array is empty.
Definition array.h:208
T * erase(T *it_)
Erase an element from the array.
Definition array.h:272
b8array(std::initializer_list< T > il)
Construct a new b8array with an initializer list.
Definition array.h:160
const T & at(int n_) const
Access element at a specific position (const version).
Definition array.h:328
const T * cbegin() const
Get a const iterator pointing to the first element.
Definition array.h:226
~b8array()
Destructor to release allocated memory.
Definition array.h:197
T & operator[](int n_)
Overload array subscript operator for non-const objects.
Definition array.h:339
size_t size() const
Get the number of elements in the array.
Definition array.h:296
const T * cend() const
Get a const iterator pointing to one past the last element.
Definition array.h:235
const T & operator[](int n_) const
Overload array subscript operator for const objects.
Definition array.h:351
void push_back(const T &x_)
Add an element to the end of the array.
Definition array.h:305
Debug tracing macros for logging and monitoring program execution.