StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
iterable.txx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
42#include <iterator>
43#include <utility>
44
45namespace StormByte {
46 namespace Safe {
47 template<Type::SafeComponent Component>
48 requires requires(Component& component, const Component& readonly) {
49 typename Component::value_type;
50 { component.begin() };
51 { component.end() };
52 { readonly.begin() };
53 { readonly.end() };
54 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
55 }
56 template<bool IsConst>
57 template<bool OtherConst>
58 requires IsConst && (!OtherConst)
59 Iterable<Component>::BasicIterator<IsConst>::BasicIterator(const BasicIterator<OtherConst>& other) noexcept: m_current(other.m_current) {}
60
61 template<Type::SafeComponent Component>
62 requires requires(Component& component, const Component& readonly) {
63 typename Component::value_type;
64 { component.begin() };
65 { component.end() };
66 { readonly.begin() };
67 { readonly.end() };
68 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
69 }
70 template<bool IsConst>
71 Iterable<Component>::BasicIterator<IsConst>::BasicIterator(Inner current) noexcept: m_current(current) {}
72
73 template<Type::SafeComponent Component>
74 requires requires(Component& component, const Component& readonly) {
75 typename Component::value_type;
76 { component.begin() };
77 { component.end() };
78 { readonly.begin() };
79 { readonly.end() };
80 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
81 }
82 template<bool IsConst>
83 Iterable<Component>::BasicIterator<IsConst>::reference Iterable<Component>::BasicIterator<IsConst>::operator*() const {
84 return *m_current;
85 }
86
87 template<Type::SafeComponent Component>
88 requires requires(Component& component, const Component& readonly) {
89 typename Component::value_type;
90 { component.begin() };
91 { component.end() };
92 { readonly.begin() };
93 { readonly.end() };
94 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
95 }
96 template<bool IsConst>
97 Iterable<Component>::BasicIterator<IsConst>& Iterable<Component>::BasicIterator<IsConst>::operator++() {
98 ++m_current;
99 return *this;
100 }
101
102 template<Type::SafeComponent Component>
103 requires requires(Component& component, const Component& readonly) {
104 typename Component::value_type;
105 { component.begin() };
106 { component.end() };
107 { readonly.begin() };
108 { readonly.end() };
109 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
110 }
111 template<bool IsConst>
112 Iterable<Component>::BasicIterator<IsConst> Iterable<Component>::BasicIterator<IsConst>::operator++(int) {
113 BasicIterator previous = *this;
114 ++*this;
115 return previous;
116 }
117
118 template<Type::SafeComponent Component>
119 requires requires(Component& component, const Component& readonly) {
120 typename Component::value_type;
121 { component.begin() };
122 { component.end() };
123 { readonly.begin() };
124 { readonly.end() };
125 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
126 }
127 template<bool IsConst>
128 Iterable<Component>::BasicIterator<IsConst>& Iterable<Component>::BasicIterator<IsConst>::operator--() {
129 --m_current;
130 return *this;
131 }
132
133 template<Type::SafeComponent Component>
134 requires requires(Component& component, const Component& readonly) {
135 typename Component::value_type;
136 { component.begin() };
137 { component.end() };
138 { readonly.begin() };
139 { readonly.end() };
140 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
141 }
142 template<bool IsConst>
143 Iterable<Component>::BasicIterator<IsConst> Iterable<Component>::BasicIterator<IsConst>::operator--(int) {
144 BasicIterator previous = *this;
145 --*this;
146 return previous;
147 }
148
149 template<Type::SafeComponent Component>
150 requires requires(Component& component, const Component& readonly) {
151 typename Component::value_type;
152 { component.begin() };
153 { component.end() };
154 { readonly.begin() };
155 { readonly.end() };
156 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
157 }
158 template<bool IsConst>
159 Iterable<Component>::BasicIterator<IsConst>& Iterable<Component>::BasicIterator<IsConst>::operator+=(difference_type offset) requires random_access {
160 m_current += offset;
161 return *this;
162 }
163
164 template<Type::SafeComponent Component>
165 requires requires(Component& component, const Component& readonly) {
166 typename Component::value_type;
167 { component.begin() };
168 { component.end() };
169 { readonly.begin() };
170 { readonly.end() };
171 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
172 }
173 template<bool IsConst>
174 Iterable<Component>::BasicIterator<IsConst>& Iterable<Component>::BasicIterator<IsConst>::operator-=(difference_type offset) requires random_access {
175 m_current -= offset;
176 return *this;
177 }
178
179 template<Type::SafeComponent Component>
180 requires requires(Component& component, const Component& readonly) {
181 typename Component::value_type;
182 { component.begin() };
183 { component.end() };
184 { readonly.begin() };
185 { readonly.end() };
186 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
187 }
188 template<bool IsConst>
189 Iterable<Component>::BasicIterator<IsConst>::reference Iterable<Component>::BasicIterator<IsConst>::operator[](difference_type offset) const requires random_access {
190 return m_current[offset];
191 }
192
193 template<Type::SafeComponent Component>
194 requires requires(Component& component, const Component& readonly) {
195 typename Component::value_type;
196 { component.begin() };
197 { component.end() };
198 { readonly.begin() };
199 { readonly.end() };
200 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
201 }
202 template<bool IsConst>
203 Iterable<Component>::BasicIterator<IsConst> Iterable<Component>::BasicIterator<IsConst>::operator+(difference_type offset) const requires random_access {
204 BasicIterator advanced = *this;
205 advanced += offset;
206 return advanced;
207 }
208
209 template<Type::SafeComponent Component>
210 requires requires(Component& component, const Component& readonly) {
211 typename Component::value_type;
212 { component.begin() };
213 { component.end() };
214 { readonly.begin() };
215 { readonly.end() };
216 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
217 }
218 template<bool IsConst>
219 Iterable<Component>::BasicIterator<IsConst> Iterable<Component>::BasicIterator<IsConst>::operator-(difference_type offset) const requires random_access {
220 BasicIterator retreated = *this;
221 retreated -= offset;
222 return retreated;
223 }
224
225 template<Type::SafeComponent Component>
226 requires requires(Component& component, const Component& readonly) {
227 typename Component::value_type;
228 { component.begin() };
229 { component.end() };
230 { readonly.begin() };
231 { readonly.end() };
232 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
233 }
234 template<bool IsConst>
235 template<bool OtherConst>
236 Iterable<Component>::BasicIterator<IsConst>::difference_type Iterable<Component>::BasicIterator<IsConst>::operator-(const BasicIterator<OtherConst>& other) const requires random_access {
237 return m_current - other.m_current;
238 }
239
240 template<Type::SafeComponent Component>
241 requires requires(Component& component, const Component& readonly) {
242 typename Component::value_type;
243 { component.begin() };
244 { component.end() };
245 { readonly.begin() };
246 { readonly.end() };
247 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
248 }
249 template<bool IsConst>
250 template<bool OtherConst>
251 bool Iterable<Component>::BasicIterator<IsConst>::operator==(const BasicIterator<OtherConst>& other) const {
252 return m_current == other.m_current;
253 }
254
255 template<Type::SafeComponent Component>
256 requires requires(Component& component, const Component& readonly) {
257 typename Component::value_type;
258 { component.begin() };
259 { component.end() };
260 { readonly.begin() };
261 { readonly.end() };
262 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
263 }
264 template<bool IsConst>
265 template<bool OtherConst>
266 bool Iterable<Component>::BasicIterator<IsConst>::operator<(const BasicIterator<OtherConst>& other) const requires random_access {
267 return m_current < other.m_current;
268 }
269
270 template<Type::SafeComponent Component>
271 requires requires(Component& component, const Component& readonly) {
272 typename Component::value_type;
273 { component.begin() };
274 { component.end() };
275 { readonly.begin() };
276 { readonly.end() };
277 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
278 }
279 template<bool IsConst>
280 template<bool OtherConst>
281 bool Iterable<Component>::BasicIterator<IsConst>::operator>(const BasicIterator<OtherConst>& other) const requires random_access {
282 return other < *this;
283 }
284
285 template<Type::SafeComponent Component>
286 requires requires(Component& component, const Component& readonly) {
287 typename Component::value_type;
288 { component.begin() };
289 { component.end() };
290 { readonly.begin() };
291 { readonly.end() };
292 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
293 }
294 template<bool IsConst>
295 template<bool OtherConst>
296 bool Iterable<Component>::BasicIterator<IsConst>::operator<=(const BasicIterator<OtherConst>& other) const requires random_access {
297 return !(other < *this);
298 }
299
300 template<Type::SafeComponent Component>
301 requires requires(Component& component, const Component& readonly) {
302 typename Component::value_type;
303 { component.begin() };
304 { component.end() };
305 { readonly.begin() };
306 { readonly.end() };
307 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
308 }
309 template<bool IsConst>
310 template<bool OtherConst>
311 bool Iterable<Component>::BasicIterator<IsConst>::operator>=(const BasicIterator<OtherConst>& other) const requires random_access {
312 return !(*this < other);
313 }
314
315 template<Type::SafeComponent Component>
316 requires requires(Component& component, const Component& readonly) {
317 typename Component::value_type;
318 { component.begin() };
319 { component.end() };
320 { readonly.begin() };
321 { readonly.end() };
322 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
323 }
324 Iterable<Component>::Iterable(container_type container) noexcept: m_container(std::move(container)) {}
325
326 template<Type::SafeComponent Component>
327 requires requires(Component& component, const Component& readonly) {
328 typename Component::value_type;
329 { component.begin() };
330 { component.end() };
331 { readonly.begin() };
332 { readonly.end() };
333 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
334 }
335 Iterable<Component>::iterator Iterable<Component>::begin() noexcept {
336 return iterator(m_container.begin());
337 }
338
339 template<Type::SafeComponent Component>
340 requires requires(Component& component, const Component& readonly) {
341 typename Component::value_type;
342 { component.begin() };
343 { component.end() };
344 { readonly.begin() };
345 { readonly.end() };
346 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
347 }
348 Iterable<Component>::iterator Iterable<Component>::end() noexcept {
349 return iterator(m_container.end());
350 }
351
352 template<Type::SafeComponent Component>
353 requires requires(Component& component, const Component& readonly) {
354 typename Component::value_type;
355 { component.begin() };
356 { component.end() };
357 { readonly.begin() };
358 { readonly.end() };
359 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
360 }
361 Iterable<Component>::const_iterator Iterable<Component>::begin() const noexcept {
362 return const_iterator(m_container.begin());
363 }
364
365 template<Type::SafeComponent Component>
366 requires requires(Component& component, const Component& readonly) {
367 typename Component::value_type;
368 { component.begin() };
369 { component.end() };
370 { readonly.begin() };
371 { readonly.end() };
372 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
373 }
374 Iterable<Component>::const_iterator Iterable<Component>::end() const noexcept {
375 return const_iterator(m_container.end());
376 }
377
378 template<Type::SafeComponent Component>
379 requires requires(Component& component, const Component& readonly) {
380 typename Component::value_type;
381 { component.begin() };
382 { component.end() };
383 { readonly.begin() };
384 { readonly.end() };
385 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
386 }
387 Iterable<Component>::const_iterator Iterable<Component>::cbegin() const noexcept {
388 return begin();
389 }
390
391 template<Type::SafeComponent Component>
392 requires requires(Component& component, const Component& readonly) {
393 typename Component::value_type;
394 { component.begin() };
395 { component.end() };
396 { readonly.begin() };
397 { readonly.end() };
398 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
399 }
400 Iterable<Component>::const_iterator Iterable<Component>::cend() const noexcept {
401 return end();
402 }
403
404 template<Type::SafeComponent Component>
405 requires requires(Component& component, const Component& readonly) {
406 typename Component::value_type;
407 { component.begin() };
408 { component.end() };
409 { readonly.begin() };
410 { readonly.end() };
411 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
412 }
413 bool Iterable<Component>::empty() const noexcept {
414 return m_container.empty();
415 }
416
417 template<Type::SafeComponent Component>
418 requires requires(Component& component, const Component& readonly) {
419 typename Component::value_type;
420 { component.begin() };
421 { component.end() };
422 { readonly.begin() };
423 { readonly.end() };
424 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
425 }
426 Iterable<Component>::size_type Iterable<Component>::size() const noexcept {
427 return static_cast<size_type>(m_container.size());
428 }
429
430 template<Type::SafeComponent Component>
431 requires requires(Component& component, const Component& readonly) {
432 typename Component::value_type;
433 { component.begin() };
434 { component.end() };
435 { readonly.begin() };
436 { readonly.end() };
437 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
438 }
439 Iterable<Component>::container_type& Iterable<Component>::Container() noexcept {
440 return m_container;
441 }
442
443 template<Type::SafeComponent Component>
444 requires requires(Component& component, const Component& readonly) {
445 typename Component::value_type;
446 { component.begin() };
447 { component.end() };
448 { readonly.begin() };
449 { readonly.end() };
450 { readonly.size() } -> Type::ConvertibleTo<std::size_t>;
451 }
452 const Iterable<Component>::container_type& Iterable<Component>::Container() const noexcept {
453 return m_container;
454 }
455 }
456}