Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-iterator.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
44# ifndef AH_ITERATOR_H
45# define AH_ITERATOR_H
46
47# include <iterator>
48# include <concepts>
49# include <type_traits>
50# include <utility>
51
68 template <class SetType>
69 struct StlIterator : public SetType::Iterator
70 {
71 using T = typename SetType::Item_Type;
72 using Itor = typename SetType::Iterator;
73 using iterator_category = std::forward_iterator_tag;
74 using iterator_concept = std::forward_iterator_tag;
75
76 using difference_type = std::ptrdiff_t;
77
78 using reference = decltype(std::declval<const Itor &>().get_curr());
79
80 using pointer = std::add_pointer_t<std::remove_reference_t<reference>>;
81
82 using value_type = T;
83
85 StlIterator() noexcept = default;
86
87 using Itor::Itor;
88
90 StlIterator& operator ++ ()
91 {
92 this->next();
93 return *this;
94 }
95
98 {
99 StlIterator ret_val = *this;
100 this->next();
101 return ret_val;
102 }
103
106 bool operator == (const StlIterator & it) const
107 {
108 if constexpr (requires (const Itor & a, const Itor & b)
109 {
110 { a == b } -> std::convertible_to<bool>;
111 })
112 return static_cast<const Itor &>(*this) == static_cast<const Itor &>(it);
113
114 if (this->has_curr() and it.has_curr())
115 return this->get_pos() == it.get_pos();
116 // return this->get_curr() == it.get_curr();
117
118 if (not this->has_curr() and not it.has_curr())
119 return true;
120
121 return false;
122 }
123
125 bool operator != (const StlIterator & it) const
126 {
127 return not (*this == it);
128 }
129
130 // Note: operator* must be const to satisfy std::indirectly_readable.
131 // Constness of the iterator object does not imply constness of the element.
133 reference operator * () const { return this->get_curr(); }
134
136 pointer operator -> () const { return &this->get_curr(); }
137
139 static StlIterator begin(SetType & s)
140 {
141 return StlIterator(s);
142 }
143
145 static StlIterator end(SetType & s)
146 {
147 StlIterator it(s);
148 it.Itor::end();
149 return it;
150 }
151};
152
153
160 template <class SetType>
161 struct StlConstIterator : public SetType::Iterator
162 {
163 using T = typename SetType::Item_Type;
164 using Itor = typename SetType::Iterator;
165 using iterator_category = std::forward_iterator_tag;
166 using iterator_concept = std::forward_iterator_tag;
167
168 using difference_type = std::ptrdiff_t;
169
170 using pointer = const T*;
171
172 using reference = const T&;
173
174 using value_type = T;
175
177 StlConstIterator() noexcept = default;
178
179 using Itor::Itor;
180
182 StlConstIterator& operator ++ ()
183 {
184 this->next();
185 return *this;
186 }
187
190 {
191 StlConstIterator ret_val = *this;
192 this->next();
193 return ret_val;
194 }
195
198 bool operator == (const StlConstIterator & it) const
199 {
200 if constexpr (requires (const Itor & a, const Itor & b)
201 {
202 { a == b } -> std::convertible_to<bool>;
203 })
204 return static_cast<const Itor &>(*this) == static_cast<const Itor &>(it);
205
206 if (this->has_curr() and it.has_curr())
207 return this->get_pos() == it.get_pos();
208
209 if (not this->has_curr() and not it.has_curr())
210 return true;
211
212 return false;
213 }
214
216 bool operator != (const StlConstIterator & it) const
217 {
218 return not (*this == it);
219 }
220
222 const T & operator * () const { return this->get_curr(); }
223
225 const T * operator -> () const { return &this->get_curr(); }
226
228 static StlConstIterator cbegin(const SetType & s)
229 {
230 return StlConstIterator(s);
231 }
232
234 static StlConstIterator cend(const SetType & s)
235 {
236 StlConstIterator it(s);
237 it.Itor::end();
238 return it;
239 }
240};
241
242
250 template <class SetName>
252 {
253 SetName * me() { return static_cast<SetName*>(this); }
254
255 const SetName * const_me() const { return static_cast<const SetName*>(this); }
256
257public:
258
260
262
264 iterator begin() noexcept { return iterator::begin(*me()); }
265
267 iterator end() noexcept { return iterator::end(*me()); }
268
270 const_iterator begin() const noexcept
271 {
273 }
274
276 const_iterator end() const noexcept
277 {
279 }
280
282 const_iterator cbegin() const noexcept
283 {
285 }
286
288 const_iterator cend() const noexcept
289 {
291 }
292
293 friend const_iterator cbegin(const SetName & s) noexcept { return s.begin(); }
294
295 friend const_iterator cend(const SetName & s) noexcept { return s.end(); }
296
297 friend const_iterator begin(const SetName & s) noexcept { return s.begin(); }
298
299 friend const_iterator end(const SetName & s) noexcept { return s.end(); }
300
301 friend iterator begin(SetName & s) noexcept { return s.begin(); }
302
303 friend iterator end(SetName & s) noexcept { return s.end(); }
304};
305
306
315template <class Container> inline
318{
320 for (const auto & i : c)
321 ret.append(i);
322 return ret;
323}
324
325
326# endif // AH_ITERATOR_H
Aleph::DynList< typename Container::value_type > extract_from_stl_container(const Container &c)
Extract all items from an STL container into an Aleph DynList.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
friend const_iterator end(const SetName &s) noexcept
const_iterator begin() const noexcept
Return a const iterator to the first element.
friend const_iterator cbegin(const SetName &s) noexcept
friend const_iterator cend(const SetName &s) noexcept
SetName * me()
const_iterator cbegin() const noexcept
Return a const iterator to the first element.
const_iterator cend() const noexcept
Return a const end iterator.
friend iterator end(SetName &s) noexcept
const SetName * const_me() const
friend iterator begin(SetName &s) noexcept
iterator end() noexcept
Return an STL-compatible end iterator.
const_iterator end() const noexcept
Return a const end iterator.
friend const_iterator begin(const SetName &s) noexcept
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
STL-compatible const iterator adapter for Aleph containers.
const T & operator*() const
Dereference: return the current element.
const T & reference
StlConstIterator() noexcept=default
Default constructor creates an "end" iterator (no current element).
typename SetType::Iterator Itor
typename SetType::Item_Type T
static StlConstIterator cbegin(const SetType &s)
Create a const iterator positioned at the first element of the container.
const T * operator->() const
Member access to the current element.
std::forward_iterator_tag iterator_concept
bool operator==(const StlConstIterator &it) const
Equality compares positions when both iterators are valid; otherwise both must be in the end state.
std::ptrdiff_t difference_type
bool operator!=(const StlConstIterator &it) const
Inequality.
std::forward_iterator_tag iterator_category
const T * pointer
StlConstIterator & operator++()
Pre-increment: advance to the next element.
static StlConstIterator cend(const SetType &s)
Create a const end iterator for the container.
STL-compatible mutable iterator adapter for Aleph containers.
Definition ah-iterator.H:70
std::add_pointer_t< std::remove_reference_t< reference > > pointer
Definition ah-iterator.H:80
bool operator!=(const StlIterator &it) const
Inequality.
std::forward_iterator_tag iterator_category
Definition ah-iterator.H:73
decltype(std::declval< const Itor & >().get_curr()) reference
Definition ah-iterator.H:78
typename SetType::Iterator Itor
Definition ah-iterator.H:72
std::ptrdiff_t difference_type
Definition ah-iterator.H:76
static StlIterator begin(SetType &s)
Create an iterator positioned at the first element of the container.
static StlIterator end(SetType &s)
Create an end iterator for the container.
StlIterator() noexcept=default
Default constructor creates an "end" iterator (no current element).
pointer operator->() const
Member access to the current element.
StlIterator & operator++()
Pre-increment: advance to the next element.
Definition ah-iterator.H:90
bool operator==(const StlIterator &it) const
Equality compares positions when both iterators are valid; otherwise both must be in the end state.
std::forward_iterator_tag iterator_concept
Definition ah-iterator.H:74
reference operator*() const
Dereference: return the current element.
typename SetType::Item_Type T
Definition ah-iterator.H:71