ORIGINAL
Loading...
Searching...
No Matches
randomAccessIterator.h
Go to the documentation of this file.
1#ifndef RANDOMACCESSITERATOR_H
2#define RANDOMACCESSITERATOR_H
3
4#include "container.h"
5#include "error.h"
6#include "iterator.h"
7#include <limits>
8
16namespace original {
17
38 template<typename TYPE, typename ALLOC>
39 class randomAccessIterator : public baseIterator<TYPE> {
40 protected:
41 mutable TYPE* _ptr;
43 mutable integer _pos;
44
52
58 bool equalPtr(const iterator<TYPE>* other) const override;
59
60 public:
61
67
74
79 randomAccessIterator* clone() const override;
80
85 [[nodiscard]] bool hasNext() const override;
86
91 [[nodiscard]] bool hasPrev() const override;
92
98 bool atPrev(const iterator<TYPE>* other) const override;
99
105 bool atNext(const iterator<TYPE>* other) const override;
106
110 void next() const override;
111
115 void prev() const override;
116
121 void operator+=(integer steps) const override;
122
127 void operator-=(integer steps) const override;
128
134 integer operator-(const iterator<TYPE>& other) const override;
135
140 randomAccessIterator* getNext() const override;
141
146 randomAccessIterator* getPrev() const override;
147
152 TYPE& get() override;
153
158 TYPE get() const override;
159
164 void set(const TYPE& data) override;
165
170 [[nodiscard]] bool isValid() const override;
171
176 [[nodiscard]] std::string className() const override;
177 };
178}
179
180 template<typename TYPE, typename ALLOC>
184
185 template<typename TYPE, typename ALLOC>
187 auto* other_it = dynamic_cast<const randomAccessIterator*>(other);
188 return other_it != nullptr && _ptr == other_it->_ptr;
189 }
190
191 template<typename TYPE, typename ALLOC>
195
196 template<typename TYPE, typename ALLOC>
199 if (this == &other) {
200 return *this;
201 }
202 this->_ptr = other._ptr;
203 this->_container = other._container;
204 this->_pos = other._pos;
205 return *this;
206 }
207
208 template<typename TYPE, typename ALLOC>
212
213 template<typename TYPE, typename ALLOC>
215 return this->isValid() && this->_pos <= this->_container->size() - 1;
216 }
217
218 template<typename TYPE, typename ALLOC>
220 return this->isValid() && this->_pos >= 1;
221 }
222
223 template<typename TYPE, typename ALLOC>
227
228 template<typename TYPE, typename ALLOC>
232
233 template<typename TYPE, typename ALLOC>
235 ++this->_pos;
236 ++this->_ptr;
237 }
238
239 template<typename TYPE, typename ALLOC>
241 --this->_pos;
242 --this->_ptr;
243 }
244
245 template<typename TYPE, typename ALLOC>
247 this->_pos += steps;
248 this->_ptr += steps;
249 }
250
251 template<typename TYPE, typename ALLOC>
253 this->_pos -= steps;
254 this->_ptr -= steps;
255 }
256
257 template<typename TYPE, typename ALLOC>
259 {
260 auto* other_it = dynamic_cast<const randomAccessIterator*>(&other);
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;
270 }
271
272 template<typename TYPE, typename ALLOC>
274 if (!this->isValid()) throw outOfBoundError();
275 auto it = this->clone();
276 it->next();
277 return it;
278 }
279
280 template<typename TYPE, typename ALLOC>
282 if (!this->isValid()) throw outOfBoundError();
283 auto it = this->clone();
284 it->prev();
285 return it;
286 }
287
288 template<typename TYPE, typename ALLOC>
290 if (!this->isValid()) throw outOfBoundError();
291 return *this->_ptr;
292 }
293
294 template<typename TYPE, typename ALLOC>
296 if (!this->isValid()) throw outOfBoundError();
297 if constexpr (std::is_copy_assignable_v<TYPE>) {
298 *this->_ptr = data;
299 } else if constexpr (std::is_move_assignable_v<TYPE>) {
300 *this->_ptr = std::move(const_cast<TYPE&>(data));
301 } else {
302 staticError<unSupportedMethodError, !std::is_copy_constructible_v<TYPE> && !std::is_move_constructible_v<TYPE>>::asserts();
303 }
304 }
305
306 template<typename TYPE, typename ALLOC>
308 {
309 if (!this->isValid()) throw outOfBoundError();
310 if constexpr (std::is_copy_constructible_v<TYPE>) {
311 return *this->_ptr;
312 } else if constexpr (std::is_move_constructible_v<TYPE>) {
313 return std::move(*this->_ptr);
314 } else {
315 staticError<unSupportedMethodError, !std::is_copy_constructible_v<TYPE> && !std::is_move_constructible_v<TYPE>>::asserts();
316 return TYPE{};
317 }
318 }
319
320 template<typename TYPE, typename ALLOC>
322 return this->_pos >= 0 && this->_pos < this->_container->size();
323 }
324
325 template<typename TYPE, typename ALLOC>
327 return "RandomAccessIterator";
328 }
329
330#endif //RANDOMACCESSITERATOR_H
A base class for basic iterators.
Definition iterator.h:296
Abstract base class for containers.
Definition container.h:26
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:192
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
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
Exception for unimplemented method calls.
Definition error.h:271
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
Standard namespace extensions for original::alternative.
Definition allocator.h:351