StormByte 2.0.0
C++26 foundation of the StormByte suite
 
Loading...
Searching...
No Matches
iterable.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte.
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
44
45#include <iterator>
46#include <utility>
47
52namespace StormByte {
66 template <typename Container>
67 class Iterable {
68 protected:
69 Container m_data;
70
71 public:
72 using value_type = typename Container::value_type;
73 using size_type = typename Container::size_type;
74 using difference_type = typename Container::difference_type;
75 using reference = typename Container::reference;
76 using const_reference = typename Container::const_reference;
77 using pointer = typename Container::pointer;
78 using const_pointer = typename Container::const_pointer;
79
84 class Iterator {
85 friend class Iterable;
86 public:
87 using iterator_category = typename std::iterator_traits<typename Container::iterator>::iterator_category;
88 using value_type = typename std::iterator_traits<typename Container::iterator>::value_type;
89 using difference_type = typename std::iterator_traits<typename Container::iterator>::difference_type;
90 using pointer = typename std::iterator_traits<typename Container::iterator>::pointer;
91 using reference = typename std::iterator_traits<typename Container::iterator>::reference;
92
98
104
110
116
122
128
135
142
148
154
160
165 bool operator==(const Iterator& other) const;
166
171 bool operator!=(const Iterator& other) const;
172
173 private:
174 typename Container::iterator m_it;
175
180 Iterator(typename Container::iterator it);
181 };
182
188 friend class Iterable;
189 public:
190 using iterator_category = typename std::iterator_traits<typename Container::const_iterator>::iterator_category;
191 using value_type = typename std::iterator_traits<typename Container::const_iterator>::value_type;
192 using difference_type = typename std::iterator_traits<typename Container::const_iterator>::difference_type;
193 using pointer = typename std::iterator_traits<typename Container::const_iterator>::pointer;
194 using reference = typename std::iterator_traits<typename Container::const_iterator>::reference;
195
201
207
213
219
225
231
238
245
251
257
263
268 bool operator==(const ConstIterator& other) const;
269
274 bool operator!=(const ConstIterator& other) const;
275
276 private:
277 typename Container::const_iterator m_it;
278
283 ConstIterator(typename Container::const_iterator it);
284 };
285
288 using reverse_iterator = std::reverse_iterator<iterator>;
289 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
290
294 Iterable() = default;
295
310 template<typename C>
311 explicit Iterable(C&& data)
312 requires Type::SameAs<C, Container> && (
315 );
316
327
331 Iterable(Iterable&&) requires Type::MoveConstructible<Container> = default;
332
337
345
350 Iterable& operator=(Iterable&&) requires Type::MoveAssignable<Container> = default;
351
357
363
368
373
378
383
388
393
398
403
408
413
418
423
428
433
443
452 requires (Type::HasMappedType<Container>);
453
463 requires (Type::HasMappedType<const Container>);
464
472
488 void add(T&& value)
489 requires (
490 (Type::LvalueReference<T> && Type::CopyConstructible<value_type>) ||
491 (!Type::LvalueReference<T> && Type::MoveConstructible<value_type>)
492 );
493
502 requires Type::MoveConstructible<value_type>;
503
510
519 requires Type::HasMappedType<const Container> && std::convertible_to<M, typename Container::mapped_type>;
520
529 requires Type::HasMappedType<const Container>;
530 };
531}
532
533#include <StormByte/iterable.txx>
534
Const iterator adapter over Container::const_iterator.
Definition iterable.hxx:187
ConstIterator operator++(int)
Post-increment.
ConstIterator & operator-=(difference_type n)
Retreat in place.
ConstIterator & operator--()
Pre-decrement.
typename std::iterator_traits< typename Container::const_iterator >::iterator_category iterator_category
Matches the underlying container iterator; not assumed random-access.
Definition iterable.hxx:190
ConstIterator operator-(difference_type n) const
Iterator moved back by n.
typename std::iterator_traits< typename Container::const_iterator >::value_type value_type
Definition iterable.hxx:191
reference operator*() const
Dereference.
pointer operator->() const
Member access.
typename std::iterator_traits< typename Container::const_iterator >::pointer pointer
Definition iterable.hxx:193
ConstIterator operator+(difference_type n) const
Iterator advanced by n.
bool operator!=(const ConstIterator &other) const
Inequality.
ConstIterator & operator++()
Pre-increment.
ConstIterator operator--(int)
Post-decrement.
bool operator==(const ConstIterator &other) const
Equality.
typename std::iterator_traits< typename Container::const_iterator >::difference_type difference_type
Definition iterable.hxx:192
typename std::iterator_traits< typename Container::const_iterator >::reference reference
Definition iterable.hxx:194
difference_type operator-(const ConstIterator &other) const
Distance to other.
ConstIterator & operator+=(difference_type n)
Advance in place.
Mutable iterator adapter over Container::iterator.
Definition iterable.hxx:84
Iterator & operator++()
Pre-increment.
bool operator!=(const Iterator &other) const
Inequality.
typename std::iterator_traits< typename Container::iterator >::difference_type difference_type
Definition iterable.hxx:89
reference operator*()
Dereference.
typename std::iterator_traits< typename Container::iterator >::value_type value_type
Definition iterable.hxx:88
Iterator & operator+=(difference_type n)
Advance in place.
typename std::iterator_traits< typename Container::iterator >::pointer pointer
Definition iterable.hxx:90
Iterator operator++(int)
Post-increment.
Iterator operator--(int)
Post-decrement.
Iterator & operator-=(difference_type n)
Retreat in place.
Iterator operator+(difference_type n) const
Iterator advanced by n.
bool operator==(const Iterator &other) const
Equality.
Iterator & operator--()
Pre-decrement.
Iterator operator-(difference_type n) const
Iterator moved back by n.
typename std::iterator_traits< typename Container::iterator >::reference reference
Definition iterable.hxx:91
typename std::iterator_traits< typename Container::iterator >::iterator_category iterator_category
Matches the underlying container iterator; not assumed random-access.
Definition iterable.hxx:87
pointer operator->()
Member access.
difference_type operator-(const Iterator &other) const
Distance to other.
Wrapper that adds a uniform iteration and mutation API around a standard container.
Definition iterable.hxx:67
Iterable(C &&data)
Builds from a container.
const_reverse_iterator crbegin() const noexcept
Const reverse begin.
const_iterator cend() const noexcept
Const end.
typename Container::size_type size_type
Container::size_type.
Definition iterable.hxx:73
std::reverse_iterator< iterator > reverse_iterator
Mutable reverse adapter.
Definition iterable.hxx:288
reverse_iterator rend() noexcept
Mutable reverse end.
Iterable(Iterable &&)=default
Move constructor.
bool has_key(const K &key) const
Key lookup for associative containers.
bool has_item(const value_type &value) const
Linear search for an equal element.
iterator end() noexcept
Mutable end.
const_iterator cbegin() const noexcept
Const begin.
reverse_iterator rbegin() noexcept
Mutable reverse begin.
Iterable()=default
Empty iterable.
typename Container::value_type value_type
Container::value_type.
Definition iterable.hxx:72
bool empty() const noexcept
Whether the container is empty.
Iterable(const Iterable &other)
Copy constructor.
void add(T &&value)
Inserts value via push_back, else push_front, else associative insert.
typename Container::const_pointer const_pointer
Container::const_pointer.
Definition iterable.hxx:78
typename Container::pointer pointer
Container::pointer.
Definition iterable.hxx:77
size_type size() const noexcept
Element count.
Container m_data
Underlying container.
Definition iterable.hxx:69
typename Container::reference reference
Container::reference.
Definition iterable.hxx:75
typename Container::difference_type difference_type
Container::difference_type.
Definition iterable.hxx:74
std::reverse_iterator< const_iterator > const_reverse_iterator
Const reverse adapter.
Definition iterable.hxx:289
iterator begin() noexcept
Mutable begin.
const_reverse_iterator crend() const noexcept
Const reverse end.
typename Container::const_reference const_reference
Container::const_reference.
Definition iterable.hxx:76
Type that can actually be copy-constructed.
Definition object_semantics.hxx:188
Lvalue reference type (std::is_lvalue_reference).
Definition categories.hxx:97
Type that can be move-constructed.
Definition object_semantics.hxx:212
Same type after stripping cv and references from both sides.
Definition relations.hxx:94
Root namespace of the StormByte suite.
Octet count. Defined in byte_size.hxx. Forwarded so Type::Numeral can name it.
STL namespace.