Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahIterator.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
74# ifndef AHITERATOR_H
75# define AHITERATOR_H
76
77# include <type_traits>
78
79namespace Aleph
80{
81
90 template <class Itor>
92 {
93 typedef typename Itor::value_type value_type;
94 typedef typename Itor::difference_type difference_type;
95 typedef typename Itor::pointer pointer;
96 typedef typename Itor::reference reference;
97 };
98
99# define STL_ITOR_SPEC(It_Name) \
100 public: \
101 \
102 using T = typename Set_Type::Item_Type; \
103 using Itor = typename Set_Type::Iterator; \
104 \
105 using Itor::Itor; \
106 \
107 Itor operator ++ () \
108 { \
109 this->next(); \
110 return *this; \
111 } \
112 \
113 Itor operator ++ (int) \
114 { \
115 Itor ret_val = *this; \
116 this->next(); \
117 return ret_val; \
118 } \
119 \
120 bool operator == (const Itor & it) const noexcept \
121 { \
122 if (this->has_curr() and it.has_curr()) \
123 return this->get_pos() == it.get_pos(); \
124 \
125 if (not this->has_curr() and not it.has_curr()) \
126 return true; \
127 \
128 return false; \
129 } \
130 \
131 bool operator != (const Itor & it) const noexcept \
132 { \
133 return not (*this == it); \
134 }
135
136
147 template <class Set_Type>
148 struct __iterator : public Set_Type::Iterator
149 {
151
152 // Note: operator* must be const to satisfy std::indirectly_readable concept
153 // (required by std::ranges::input_iterator). The constness of the iterator
154 // does not affect the constness of the pointed-to value.
155 T &operator *() const { return const_cast<T &>(this->get_curr()); }
156
157 T * operator ->() const
158 {
159 return &const_cast<T &>(this->get_curr());
160 }
161
162 static __iterator begin(const Set_Type & s) noexcept
163 {
164 return __iterator(s);
165 }
166
167 static __iterator end(const Set_Type & s) noexcept
168 {
169 __iterator it(s);
170 it.Itor::end();
171 return it;
172 }
173 };
174
182 template <class Set_Type>
183 struct __const_iterator : public Set_Type::Iterator
184 {
186
187 const T &operator *() const noexcept { return this->get_curr(); }
188
190 {
191 return const_cast<T *>(&this->get_curr());
192 }
193
194 static __const_iterator cbegin(const Set_Type & s) noexcept
195 {
196 return __const_iterator(s);
197 }
198
199 static __const_iterator cend(const Set_Type & s) noexcept
200 {
201 __const_iterator it(s);
202 it.Itor::end();
203 return it;
204 }
205 };
206
207
208# define STL_ALEPH_ITERATOR(Set_Name) \
209 using iterator = __iterator<Set_Name>; \
210 \
211 using const_iterator = __const_iterator<Set_Name>; \
212 \
213 iterator begin() noexcept { return iterator::begin(*this); } \
214 \
215 iterator end() noexcept { return iterator::end(*this); } \
216 \
217 const_iterator begin() const noexcept \
218 { \
219 return const_iterator::cbegin(*this); \
220 } \
221 \
222 const_iterator end() const noexcept \
223 { \
224 return const_iterator::cend(*this); \
225 } \
226 \
227 const_iterator cbegin() const noexcept \
228 { \
229 return const_iterator::cbegin(*this); \
230 } \
231 \
232 const_iterator cend() const noexcept \
233 { \
234 return const_iterator::cend(*this); \
235 } \
236 \
237 const_iterator cbegin() noexcept \
238 { \
239 return const_iterator::cbegin(*this); \
240 } \
241 \
242 const_iterator cend() noexcept \
243 { \
244 return const_iterator::cend(*this); \
245 } \
246 \
247 friend const_iterator cbegin(const Set_Name & s) noexcept \
248 { return s.begin(); } \
249 \
250 friend const_iterator cend(const Set_Name & s) noexcept \
251 { return s.end(); } \
252 \
253 friend const_iterator begin(const Set_Name & s) noexcept \
254 { return s.begin(); } \
255 \
256 friend const_iterator end(const Set_Name & s) noexcept \
257 { return s.end(); } \
258 \
259 friend iterator begin(Set_Name & s) noexcept { return s.begin(); } \
260 \
261 friend iterator end(Set_Name & s) noexcept { return s.end(); }
262
263# ifdef nada
264 inline template <class Itor>
265 typename iterator_traits<Itor>::difference_type distance(Itor it1, Itor it2)
266 {
268
269 while (it1 not_eq it2)
270 {
271 d++;
272 it1++;
273 }
274
275 return d;
276 }
277
278# endif
279}; // end namespace Aleph
280
281# endif // AHITERATOR_H
#define STL_ITOR_SPEC(It_Name)
Definition ahIterator.H:99
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:149
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL-compatible const iterator wrapper.
Definition ahIterator.H:184
static __const_iterator cend(const Set_Type &s) noexcept
Definition ahIterator.H:199
typename Set_Type::Item_Type T
Definition ahIterator.H:185
static __const_iterator cbegin(const Set_Type &s) noexcept
Definition ahIterator.H:194
T const * operator->() const noexcept
Definition ahIterator.H:189
const T & operator*() const noexcept
Definition ahIterator.H:187
STL-compatible mutable iterator wrapper.
Definition ahIterator.H:149
typename Set_Type::Item_Type T
Definition ahIterator.H:150
T & operator*() const
Definition ahIterator.H:155
T * operator->() const
Definition ahIterator.H:157
static __iterator begin(const Set_Type &s) noexcept
Definition ahIterator.H:162
static __iterator end(const Set_Type &s) noexcept
Definition ahIterator.H:167
Iterator traits for Aleph-w iterators.
Definition ahIterator.H:92
Itor::value_type value_type
Element type.
Definition ahIterator.H:93
Itor::difference_type difference_type
Distance type.
Definition ahIterator.H:94
Itor::reference reference
Reference to element.
Definition ahIterator.H:96
Itor::pointer pointer
Pointer to element.
Definition ahIterator.H:95