ORIGINAL
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#ifndef ARRAY_H
2#define ARRAY_H
3#pragma once
4
5#include <initializer_list>
6#include "allocator.h"
7#include "config.h"
8#include "baseArray.h"
9#include "iterationStream.h"
11#include "error.h"
12
13namespace original {
25
40 template<typename TYPE, typename ALLOC = allocator<TYPE>>
41 class array final : public iterationStream<TYPE, array<TYPE, ALLOC>>, public baseArray<TYPE, ALLOC> {
42 u_integer size_;
43 TYPE* body;
44
51 void arrInit(u_integer size);
52
58 void arrDestruct() noexcept;
59
60 public:
61
72 class Iterator final : public randomAccessIterator<TYPE, ALLOC> {
79 explicit Iterator(TYPE* ptr, const array* container, integer pos);
80
81 public:
82 friend array;
83
89 Iterator(const Iterator& other);
90
97 Iterator& operator=(const Iterator& other);
98
104 Iterator* clone() const override;
105
112 bool atPrev(const iterator<TYPE> *other) const override;
113
120 bool atNext(const iterator<TYPE> *other) const override;
121
126 [[nodiscard]] std::string className() const override;
127 };
128
136 explicit array(u_integer size = 0, ALLOC alloc = ALLOC{});
137
144 array(const std::initializer_list<TYPE>& lst);
145
152 array(const array& other);
153
161 array& operator=(const array& other);
162
170 array(array&& other) noexcept;
171
180 array& operator=(array&& other) noexcept;
181
186 [[nodiscard]] u_integer size() const override;
187
192 TYPE& data() const;
193
200 TYPE get(integer index) const override;
201
208 TYPE& operator[](integer index) override;
209
216 void set(integer index, const TYPE& e) override;
217
223 u_integer indexOf(const TYPE& e) const override;
224
229 Iterator* begins() const override;
230
235 Iterator* ends() const override;
236
241 [[nodiscard]] std::string className() const override;
242
247 ~array() override;
248 };
249
250} // namespace original
251
252 template<typename TYPE, typename ALLOC>
253 void original::array<TYPE, ALLOC>::arrInit(const u_integer size) {
254 this->size_ = size;
255 this->body = this->allocate(this->size_);
256 for (u_integer i = 0; i < this->size(); ++i) {
257 this->construct(&this->body[i]);
258 }
259 }
260
261 template<typename TYPE, typename ALLOC>
262 void original::array<TYPE, ALLOC>::arrDestruct() noexcept
263 {
264 if (this->body){
265 for (u_integer i = 0; i < this->size_; ++i) {
266 this->destroy(&this->body[i]);
267 }
268 this->deallocate(this->body, this->size_);
269 }
270 }
271
272 template<typename TYPE, typename ALLOC>
273 original::array<TYPE, ALLOC>::Iterator::Iterator(TYPE* ptr, const array* container, integer pos)
274 : randomAccessIterator<TYPE, ALLOC>(ptr, container, pos) {}
275
276 template<typename TYPE, typename ALLOC>
277 original::array<TYPE, ALLOC>::Iterator::Iterator(const Iterator& other)
278 : randomAccessIterator<TYPE, ALLOC>(nullptr, nullptr, 0)
279 {
280 this->operator=(other);
281 }
282
283 template<typename TYPE, typename ALLOC>
284 auto original::array<TYPE, ALLOC>::Iterator::operator=(const Iterator& other) -> Iterator&
285 {
286 if (this == &other) {
287 return *this;
288 }
290 return *this;
291 }
292
293 template<typename TYPE, typename ALLOC>
295 return new Iterator(*this);
296 }
297
298 template<typename TYPE, typename ALLOC>
300 auto other_it = dynamic_cast<const Iterator*>(other);
301 return this->_ptr + 1 == other_it->_ptr;
302 }
303
304 template<typename TYPE, typename ALLOC>
306 auto other_it = dynamic_cast<const Iterator*>(other);
307 return other_it->_ptr + 1 == this->_ptr;
308 }
309
310 template<typename TYPE, typename ALLOC>
312 return "array::Iterator";
313 }
314
315 template<typename TYPE, typename ALLOC>
317 : baseArray<TYPE, ALLOC>(std::move(alloc)), size_(), body(nullptr) {
318 this->arrInit(size);
319 }
320
321 template<typename TYPE, typename ALLOC>
322 original::array<TYPE, ALLOC>::array(const std::initializer_list<TYPE>& lst)
323 : array(lst.size()) {
324 u_integer i = 0;
325 for (const auto& e : lst) {
326 this->body[i] = e;
327 i += 1;
328 }
329 }
330
331 template<typename TYPE, typename ALLOC>
333 : array(other.size()) {
334 this->operator=(other);
335 }
336
337 template<typename TYPE, typename ALLOC>
339 {
340 if (this == &other)
341 return *this;
342
343 this->arrDestruct();
344
345 this->arrInit(other.size());
346 for (u_integer i = 0; i < this->size_; i++) {
347 this->body[i] = other.body[i];
348 }
349 if constexpr (ALLOC::propagate_on_container_copy_assignment::value){
350 this->allocator = other.allocator;
351 }
352 return *this;
353 }
354
355 template<typename TYPE, typename ALLOC>
357 this->operator=(std::move(other));
358 }
359
360 template<typename TYPE, typename ALLOC>
362 if (this == &other)
363 return *this;
364
365 this->arrDestruct();
366
367 this->body = other.body;
368 this->size_ = other.size_;
369 if constexpr (ALLOC::propagate_on_container_move_assignment::value){
370 this->allocator = std::move(other.allocator);
371 }
372 other.arrInit(0);
373 return *this;
374 }
375
376 template<typename TYPE, typename ALLOC>
378 this->arrDestruct();
379 }
380
381 template<typename TYPE, typename ALLOC>
383 {
384 return this->size_;
385 }
386
387 template<typename TYPE, typename ALLOC>
389 return this->body[0];
390 }
391
392 template<typename TYPE, typename ALLOC>
394 {
395 if (this->indexOutOfBound(index)){
396 throw outOfBoundError();
397 }
398 return this->body[this->parseNegIndex(index)];
399 }
400
401 template<typename TYPE, typename ALLOC>
403 {
404 if (this->indexOutOfBound(index)){
405 throw outOfBoundError();
406 }
407 return this->body[this->parseNegIndex(index)];
408 }
409
410 template<typename TYPE, typename ALLOC>
411 auto original::array<TYPE, ALLOC>::set(integer index, const TYPE &e) -> void
412 {
413 if (this->indexOutOfBound(index)){
414 throw outOfBoundError();
415 }
416 this->body[this->parseNegIndex(index)] = e;
417 }
418
419 template<typename TYPE, typename ALLOC>
421 {
422 for (u_integer i = 0; i < this->size(); i += 1)
423 {
424 if (this->get(i) == e)
425 {
426 return i;
427 }
428 }
429 return this->size();
430 }
431
432 template<typename TYPE, typename ALLOC>
434 return new Iterator(&this->body[0], this, 0);
435 }
436
437 template<typename TYPE, typename ALLOC>
439 return new Iterator(&this->body[this->size() - 1], this, this->size() - 1);
440 }
441
442 template<typename TYPE, typename ALLOC>
444 {
445 return "array";
446 }
447
448#endif //ARRAY_H
Memory allocation interface and implementations.
Provides a base class for fixed-size serial containers.
Default memory allocator using allocators utilities.
Definition allocator.h:154
Iterator for the array class that supports random access.
Definition array.h:72
std::string className() const override
Returns the class name of this iterator.
Definition array.h:311
Iterator & operator=(const Iterator &other)
Copy assignment operator for the iterator.
Definition array.h:284
bool atNext(const iterator< TYPE > *other) const override
Checks if this iterator is positioned just after the given iterator.
Definition array.h:305
Iterator * clone() const override
Clones the iterator.
Definition array.h:294
bool atPrev(const iterator< TYPE > *other) const override
Checks if this iterator is positioned just before the given iterator.
Definition array.h:299
A fixed-size array container with random access.
Definition array.h:41
array & operator=(array &&other) noexcept
Move assignment operator.
Definition array.h:361
std::string className() const override
Returns the class name.
Definition array.h:443
void set(integer index, const TYPE &e) override
Sets the value of an element at the specified index.
Definition array.h:411
Iterator * ends() const override
Returns an iterator to the last element of the array.
Definition array.h:438
TYPE get(integer index) const override
Retrieves an element at a specified index.
Definition array.h:393
array(array &&other) noexcept
Move constructor.
Definition array.h:356
array(const std::initializer_list< TYPE > &lst)
Constructs an array from an initializer list.
Definition array.h:322
array(u_integer size=0, ALLOC alloc=ALLOC{})
Constructs an empty array.
Definition array.h:316
TYPE & data() const
Returns a reference to the first element of the array.
Definition array.h:388
~array() override
Destroys the array and releases its memory.
Definition array.h:377
array(const array &other)
Copy constructor.
Definition array.h:332
array & operator=(const array &other)
Copy assignment operator.
Definition array.h:338
u_integer indexOf(const TYPE &e) const override
Finds the index of the specified element in the array.
Definition array.h:420
TYPE & operator[](integer index) override
Access an element at a specified index for modification.
Definition array.h:402
u_integer size() const override
Returns the size of the array.
Definition array.h:382
Iterator * begins() const override
Returns an iterator to the first element of the array.
Definition array.h:433
Base class for fixed-size serial containers.
Definition baseArray.h:43
Abstract base class for containers.
Definition container.h:28
A stream class that allows iteration, comparison, and printing.
Definition iterationStream.h:33
Base iterator interface that supports common operations for iteration.
Definition iterator.h:35
Exception for container index out-of-range errors.
Definition error.h:84
randomAccessIterator(TYPE *ptr, const container< TYPE, ALLOC > *container, integer pos)
Protected constructor for derived classes.
Definition randomAccessIterator.h:181
randomAccessIterator & operator=(const randomAccessIterator &other)
Copy assignment operator.
Definition randomAccessIterator.h:197
TYPE * _ptr
Pointer to the current element.
Definition randomAccessIterator.h:41
bool indexOutOfBound(integer index) const
Checks if the provided index is out of bounds.
Definition serial.h:133
integer parseNegIndex(integer index) const
Converts negative indices into valid positive indices.
Definition serial.h:140
Platform-independent integer type definitions.
Custom exception classes and callback validation utilities.
Provides functionality for an iteration stream.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes/indexes
Definition config.h:17
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:15
Base class for random-access iterators.