VectorPro
High-performance, header-only dynamic array with configurable growth policy, custom allocator support, and optional modification-event notifications.
Loading...
Searching...
No Matches
Iterator.h
Go to the documentation of this file.
1
22#pragma once
23
24// clang-format off
25#include <cassert> // assert (bounds check in operator[])
26#include <compare> // std::strong_ordering / operator<=>
27#include <cstddef> // std::ptrdiff_t
28#include <iterator> // std::contiguous_iterator_tag, std::random_access_iterator_tag
29#include <type_traits> // std::remove_cv_t, std::is_same_v
30// clang-format on
31
32namespace VectorPro {
33
40template <typename T> class Iterator {
41 public:
42 // Types
43 using iterator_concept = std::contiguous_iterator_tag;
44 using iterator_category = std::random_access_iterator_tag;
45 using value_type = std::remove_cv_t<T>;
46 using element_type = T;
47 using difference_type = std::ptrdiff_t;
48 using pointer = T*;
49 using reference = T&;
50
51 private:
52 // Data
53 pointer ptr_ = nullptr;
54
55 public:
57 constexpr Iterator() noexcept = default;
58
63 constexpr explicit Iterator(pointer ptr) noexcept : ptr_(ptr) {}
64
71 template <typename U>
72 requires std::is_same_v<U, std::remove_cv_t<T>>
73 constexpr Iterator(const Iterator<U>& other) noexcept : ptr_(other.operator->()) {}
74
77 [[nodiscard]] constexpr reference operator*() const noexcept {
78 return *ptr_;
79 }
80
82 [[nodiscard]] constexpr pointer operator->() const noexcept {
83 return ptr_;
84 }
85
91 [[nodiscard]] constexpr reference operator[](difference_type n) const noexcept {
92 assert(ptr_ != nullptr);
93 return ptr_[n];
94 }
95
98 constexpr Iterator& operator++() noexcept {
99 ++ptr_;
100 return *this;
101 }
102
111 // NOLINTNEXTLINE(cert-dcl21-cpp)
112 [[nodiscard]] constexpr Iterator operator++(int) noexcept {
113 auto t = *this;
114 ++ptr_;
115 return t;
116 }
117
120 constexpr Iterator& operator--() noexcept {
121 --ptr_;
122 return *this;
123 }
124
130 // NOLINTNEXTLINE(cert-dcl21-cpp)
131 [[nodiscard]] constexpr Iterator operator--(int) noexcept {
132 auto t = *this;
133 --ptr_;
134 return t;
135 }
136
142 constexpr Iterator& operator+=(difference_type n) noexcept {
143 ptr_ += n;
144 return *this;
145 }
146
152 constexpr Iterator& operator-=(difference_type n) noexcept {
153 ptr_ -= n;
154 return *this;
155 }
156
163 [[nodiscard]] friend constexpr Iterator operator+(Iterator i, difference_type n) noexcept {
164 i += n;
165 return i;
166 }
167
169 [[nodiscard]] friend constexpr Iterator operator+(difference_type n, Iterator i) noexcept {
170 i += n;
171 return i;
172 }
173
180 [[nodiscard]] friend constexpr Iterator operator-(Iterator i, difference_type n) noexcept {
181 i -= n;
182 return i;
183 }
184
191 [[nodiscard]] friend constexpr difference_type operator-(Iterator a, Iterator b) noexcept {
192 return a.ptr_ - b.ptr_;
193 }
194
201 [[nodiscard]] friend constexpr bool operator==(const Iterator& a, const Iterator& b) noexcept {
202 return a.ptr_ == b.ptr_;
203 }
204
211 [[nodiscard]] friend constexpr auto operator<=>(const Iterator& a, const Iterator& b) noexcept {
212 return a.ptr_ <=> b.ptr_;
213 }
214};
215
216} // namespace VectorPro
A raw-pointer-backed contiguous random-access iterator.
Definition Iterator.h:40
constexpr Iterator(const Iterator< U > &other) noexcept
Converting constructor from a non-const iterator to a const iterator.
Definition Iterator.h:73
T & reference
Definition Iterator.h:49
std::ptrdiff_t difference_type
Definition Iterator.h:47
T element_type
Definition Iterator.h:46
constexpr Iterator operator++(int) noexcept
Post-increments the iterator to point to the next element.
Definition Iterator.h:112
constexpr reference operator*() const noexcept
Dereferences the iterator.
Definition Iterator.h:77
std::contiguous_iterator_tag iterator_concept
Definition Iterator.h:43
friend constexpr bool operator==(const Iterator &a, const Iterator &b) noexcept
Compares two iterators for equality.
Definition Iterator.h:201
std::random_access_iterator_tag iterator_category
Definition Iterator.h:44
friend constexpr Iterator operator-(Iterator i, difference_type n) noexcept
Returns an iterator moved back by n positions.
Definition Iterator.h:180
constexpr Iterator & operator++() noexcept
Pre-increments the iterator to point to the next element.
Definition Iterator.h:98
std::remove_cv_t< T > value_type
Definition Iterator.h:45
friend constexpr Iterator operator+(Iterator i, difference_type n) noexcept
Returns an iterator advanced by n positions.
Definition Iterator.h:163
constexpr Iterator & operator-=(difference_type n) noexcept
Moves the iterator back by n positions.
Definition Iterator.h:152
constexpr reference operator[](difference_type n) const noexcept
Accesses the element n positions away.
Definition Iterator.h:91
T * pointer
Definition Iterator.h:48
constexpr Iterator & operator+=(difference_type n) noexcept
Advances the iterator by n positions.
Definition Iterator.h:142
friend constexpr difference_type operator-(Iterator a, Iterator b) noexcept
Computes the distance between two iterators.
Definition Iterator.h:191
constexpr Iterator & operator--() noexcept
Pre-decrements the iterator to point to the previous element.
Definition Iterator.h:120
friend constexpr auto operator<=>(const Iterator &a, const Iterator &b) noexcept
Three-way compares two iterators by position.
Definition Iterator.h:211
constexpr pointer operator->() const noexcept
Returns the underlying pointer.
Definition Iterator.h:82
constexpr Iterator operator--(int) noexcept
Post-decrements the iterator to point to the previous element.
Definition Iterator.h:131
friend constexpr Iterator operator+(difference_type n, Iterator i) noexcept
Returns an iterator advanced by n positions.
Definition Iterator.h:169
constexpr Iterator() noexcept=default
Constructs a null (singular) iterator.
Definition Iterator.h:32