1#ifndef RANDOMACCESSITERATOR_H
2#define RANDOMACCESSITERATOR_H
38 template<
typename TYPE,
typename ALLOC>
85 [[nodiscard]]
bool hasNext()
const override;
91 [[nodiscard]]
bool hasPrev()
const override;
110 void next()
const override;
115 void prev()
const override;
152 TYPE&
get()
override;
158 TYPE
get()
const override;
164 void set(
const TYPE& data)
override;
170 [[nodiscard]]
bool isValid()
const override;
176 [[nodiscard]] std::string
className()
const override;
180 template<
typename TYPE,
typename ALLOC>
183 : _ptr{ptr}, _container{
container}, _pos(pos) {}
185 template<
typename TYPE,
typename ALLOC>
188 return other_it !=
nullptr && _ptr == other_it->
_ptr;
191 template<
typename TYPE,
typename ALLOC>
196 template<
typename TYPE,
typename ALLOC>
199 if (
this == &other) {
202 this->_ptr = other.
_ptr;
203 this->_container = other._container;
204 this->_pos = other._pos;
208 template<
typename TYPE,
typename ALLOC>
213 template<
typename TYPE,
typename ALLOC>
215 return this->isValid() && this->_pos <= this->_container->size() - 1;
218 template<
typename TYPE,
typename ALLOC>
220 return this->isValid() && this->_pos >= 1;
223 template<
typename TYPE,
typename ALLOC>
228 template<
typename TYPE,
typename ALLOC>
233 template<
typename TYPE,
typename ALLOC>
239 template<
typename TYPE,
typename ALLOC>
245 template<
typename TYPE,
typename ALLOC>
251 template<
typename TYPE,
typename ALLOC>
257 template<
typename TYPE,
typename ALLOC>
261 if (other_it ==
nullptr)
262 return this > &other ?
263 std::numeric_limits<integer>::max() :
264 std::numeric_limits<integer>::min();
265 if (this->_container != other_it->_container)
266 return this->_container > other_it->_container ?
267 std::numeric_limits<integer>::max() :
268 std::numeric_limits<integer>::min();
269 return this->_ptr - other_it->_ptr;
272 template<
typename TYPE,
typename ALLOC>
275 auto it = this->clone();
280 template<
typename TYPE,
typename ALLOC>
283 auto it = this->clone();
288 template<
typename TYPE,
typename ALLOC>
294 template<
typename TYPE,
typename ALLOC>
297 if constexpr (std::is_copy_assignable_v<TYPE>) {
299 }
else if constexpr (std::is_move_assignable_v<TYPE>) {
300 *this->_ptr = std::move(
const_cast<TYPE&
>(data));
306 template<
typename TYPE,
typename ALLOC>
310 if constexpr (std::is_copy_constructible_v<TYPE>) {
312 }
else if constexpr (std::is_move_constructible_v<TYPE>) {
313 return std::move(*this->_ptr);
320 template<
typename TYPE,
typename ALLOC>
322 return this->_pos >= 0 && this->_pos < this->_container->size();
325 template<
typename TYPE,
typename ALLOC>
327 return "RandomAccessIterator";
A base class for basic iterators.
Definition iterator.h:296
Abstract base class for containers.
Definition container.h:26
Base iterator interface that supports common operations for iteration.
Definition iterator.h:37
friend iterator< T > * operator-(const iterator< T > &it, integer steps)
Subtracts a number of steps from the iterator's current position and returns a new iterator.
Exception for container index out-of-range errors.
Definition error.h:129
Abstract base class for random-access iterators.
Definition randomAccessIterator.h:39
randomAccessIterator * getNext() const override
Gets an iterator pointing to the next element.
Definition randomAccessIterator.h:273
std::string className() const override
Gets the class name of the iterator.
Definition randomAccessIterator.h:326
bool hasNext() const override
Checks forward traversal capability.
Definition randomAccessIterator.h:214
void set(const TYPE &data) override
Sets the value of the current element.
Definition randomAccessIterator.h:295
bool equalPtr(const iterator< TYPE > *other) const override
Equality comparison implementation.
Definition randomAccessIterator.h:186
bool hasPrev() const override
Checks reverse traversal capability.
Definition randomAccessIterator.h:219
bool atNext(const iterator< TYPE > *other) const override
Checks if the current iterator is at the next position compared to another iterator.
Definition randomAccessIterator.h:229
void prev() const override
Retreats to the previous position in the container.
Definition randomAccessIterator.h:240
randomAccessIterator(TYPE *ptr, const container< TYPE, ALLOC > *container, integer pos)
Protected constructor for derived classes.
Definition randomAccessIterator.h:181
void operator+=(integer steps) const override
Moves forward by N positions.
Definition randomAccessIterator.h:246
void next() const override
Advances to the next position in the container.
Definition randomAccessIterator.h:234
randomAccessIterator * getPrev() const override
Gets an iterator pointing to the previous element.
Definition randomAccessIterator.h:281
randomAccessIterator * clone() const override
Creates a heap-allocated copy.
Definition randomAccessIterator.h:209
const container< TYPE, ALLOC > * _container
Reference to the parent container.
Definition randomAccessIterator.h:42
void operator-=(integer steps) const override
Moves backward by N positions.
Definition randomAccessIterator.h:252
randomAccessIterator & operator=(const randomAccessIterator &other)
Copy assignment operator.
Definition randomAccessIterator.h:197
bool atPrev(const iterator< TYPE > *other) const override
Checks if the current iterator is at the previous position compared to another iterator.
Definition randomAccessIterator.h:224
TYPE * _ptr
Pointer to the current element.
Definition randomAccessIterator.h:41
TYPE & get() override
Gets the current element in the container.
Definition randomAccessIterator.h:289
bool isValid() const override
Checks if the iterator is valid (points to a valid element in the container)
Definition randomAccessIterator.h:321
integer _pos
Absolute position in the container.
Definition randomAccessIterator.h:43
Abstract base class for unique element containers.
Definition set.h:44
Compile-time error triggering utility.
Definition error.h:112
Exception for unimplemented method calls.
Definition error.h:192
Abstract base class for container types.
Custom exception classes and callback validation utilities.
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:254
Defines the iterator class for traversing and manipulating container elements.
Main namespace for the project Original.
Definition algorithms.h:21