CXXIter 0.2
Loading...
Searching...
No Matches
ContainerSources.h
1#pragma once
2
3#include <memory>
4
5#include "../Common.h"
6#include "Concepts.h"
7
8namespace CXXIter {
9
10 // ################################################################################################
11 // SOURCE (MOVE / CONSUME)
12 // ################################################################################################
13
18 template<typename TContainer>
19 requires concepts::SourceContainer<std::remove_cvref_t<TContainer>>
20 class SrcMov : public IterApi<SrcMov<TContainer>> {
21 friend struct trait::Iterator<SrcMov<TContainer>>;
22 friend struct trait::DoubleEndedIterator<SrcMov<TContainer>>;
23 friend struct trait::ExactSizeIterator<SrcMov<TContainer>>;
24 friend struct trait::ContiguousMemoryIterator<SrcMov<TContainer>>;
26 private:
27 std::unique_ptr<TContainer> container;
28 typename Src::IteratorState iter;
29 public:
30 SrcMov(TContainer&& container) : container(std::make_unique<TContainer>(std::move(container))), iter(Src::initIterator(*this->container)) {}
31 };
32 // ------------------------------------------------------------------------------------------------
34 template<typename TContainer>
35 struct trait::Iterator<SrcMov<TContainer>> {
36 using Src = trait::Source<TContainer>;
37 // CXXIter Interface
39 using Item = typename Src::Item;
40
41 static constexpr inline IterValue<Item> next(Self& self) {
42 if(!Src::hasNext(*self.container, self.iter)) [[unlikely]] { return {}; }
43 return std::move(Src::next(*self.container, self.iter));
44 }
45 static constexpr inline SizeHint sizeHint(const Self& self) { return Src::sizeHint(*self.container); }
46 static constexpr inline size_t advanceBy(Self& self, size_t n) {
47 return Src::skipN(*self.container, self.iter, n);
48 }
49 };
51 template<typename TContainer>
52 requires concepts::DoubleEndedSourceContainer<std::remove_cvref_t<TContainer>>
53 struct trait::DoubleEndedIterator<SrcMov<TContainer>> {
54 using Src = trait::Source<TContainer>;
55 using Item = typename Src::Item;
56
57 // CXXIter Interface
58 static constexpr inline IterValue<Item> nextBack(SrcMov<TContainer>& self) {
59 if(!Src::hasNext(*self.container, self.iter)) [[unlikely]] { return {}; }
60 return std::move(Src::nextBack(*self.container, self.iter));
61 }
62 };
64 template<typename TContainer>
65 struct trait::ExactSizeIterator<SrcMov<TContainer>> {
66 static constexpr inline size_t size(const SrcMov<TContainer>& self) { return self.container->size(); }
67 };
69 template<typename TContainer>
70 requires util::ContiguousMemoryContainer<TContainer>
71 struct trait::ContiguousMemoryIterator<SrcMov<TContainer>> {
72 using ItemPtr = std::add_pointer_t<std::remove_reference_t<typename SrcMov<TContainer>::Item>>;
73 static constexpr inline ItemPtr currentPtr(SrcMov<TContainer>& self) {
74 return &trait::Source<TContainer>::peekNext(*self.container, self.iter);
75 }
76 };
77
78
79
80 // ################################################################################################
81 // SOURCE (MUTABLE REFERENCE)
82 // ################################################################################################
83
90 template<typename TContainer>
91 requires concepts::SourceContainer<std::remove_cvref_t<TContainer>>
92 class SrcRef : public IterApi<SrcRef<TContainer>> {
93 friend struct trait::Iterator<SrcRef<TContainer>>;
94 friend struct trait::DoubleEndedIterator<SrcRef<TContainer>>;
95 friend struct trait::ExactSizeIterator<SrcRef<TContainer>>;
96 friend struct trait::ContiguousMemoryIterator<SrcRef<TContainer>>;
98 private:
99 TContainer& container;
100 typename Src::IteratorState iter;
101 public:
102 SrcRef(TContainer& container) : container(container), iter(Src::initIterator(this->container)) {}
103 };
104 // ------------------------------------------------------------------------------------------------
106 template<typename TContainer>
107 struct trait::Iterator<SrcRef<TContainer>> {
108 using Src = trait::Source<TContainer>;
109 // CXXIter Interface
110 using Self = SrcRef<TContainer>;
111 using Item = typename Src::ItemRef;
112
113 static constexpr inline IterValue<Item> next(Self& self) {
114 if(!Src::hasNext(self.container, self.iter)) [[unlikely]] { return {}; }
115 return Src::next(self.container, self.iter);
116 }
117 static constexpr inline SizeHint sizeHint(const Self& self) { return Src::sizeHint(self.container); }
118 static constexpr inline size_t advanceBy(Self& self, size_t n) {
119 return Src::skipN(self.container, self.iter, n);
120 }
121 };
123 template<typename TContainer>
124 requires concepts::DoubleEndedSourceContainer<std::remove_cvref_t<TContainer>>
125 struct trait::DoubleEndedIterator<SrcRef<TContainer>> {
126 using Src = trait::Source<TContainer>;
127 using Item = typename Src::ItemRef;
128
129 // CXXIter Interface
130 static constexpr inline IterValue<Item> nextBack(SrcRef<TContainer>& self) {
131 if(!Src::hasNext(self.container, self.iter)) [[unlikely]] { return {}; }
132 return Src::nextBack(self.container, self.iter);
133 }
134 };
136 template<typename TContainer>
137 struct trait::ExactSizeIterator<SrcRef<TContainer>> {
138 static constexpr inline size_t size(const SrcRef<TContainer>& self) { return self.container.size(); }
139 };
141 template<typename TContainer>
142 requires util::ContiguousMemoryContainer<TContainer>
143 struct trait::ContiguousMemoryIterator<SrcRef<TContainer>> {
144 using ItemPtr = std::add_pointer_t<std::remove_reference_t<typename SrcRef<TContainer>::Item>>;
145 static constexpr inline ItemPtr currentPtr(SrcRef<TContainer>& self) {
146 return &trait::Source<TContainer>::peekNext(self.container, self.iter);
147 }
148 };
149
150
151
152 // ################################################################################################
153 // SOURCE (CONST REFERENCE)
154 // ################################################################################################
155
161 template<typename TContainer>
162 requires concepts::SourceContainer<std::remove_cvref_t<TContainer>>
163 class SrcCRef : public IterApi<SrcCRef<TContainer>> {
164 friend struct trait::Iterator<SrcCRef<TContainer>>;
165 friend struct trait::DoubleEndedIterator<SrcCRef<TContainer>>;
166 friend struct trait::ExactSizeIterator<SrcCRef<TContainer>>;
167 friend struct trait::ContiguousMemoryIterator<SrcCRef<TContainer>>;
169 private:
170 const TContainer& container;
171 typename Src::ConstIteratorState iter;
172 public:
173 constexpr SrcCRef(const TContainer& container) : container(container), iter(Src::initIterator(this->container)) {}
174 };
175 // ------------------------------------------------------------------------------------------------
177 template<typename TContainer>
178 struct trait::Iterator<SrcCRef<TContainer>> {
179 using Src = trait::Source<TContainer>;
180 // CXXIter Interface
182 using Item = typename Src::ItemConstRef;
183
184 static constexpr inline IterValue<Item> next(Self& self) {
185 if(!Src::hasNext(self.container, self.iter)) [[unlikely]] { return {}; }
186 return Src::next(self.container, self.iter);
187 }
188 static constexpr inline SizeHint sizeHint(const Self& self) { return Src::sizeHint(self.container); }
189 static constexpr inline size_t advanceBy(Self& self, size_t n) {
190 return Src::skipN(self.container, self.iter, n);
191 }
192 };
194 template<typename TContainer>
195 requires concepts::DoubleEndedSourceContainer<std::remove_cvref_t<TContainer>>
196 struct trait::DoubleEndedIterator<SrcCRef<TContainer>> {
197 using Src = trait::Source<TContainer>;
198 using Item = typename Src::ItemConstRef;
199
200 // CXXIter Interface
201 static constexpr inline IterValue<Item> nextBack(SrcCRef<TContainer>& self) {
202 if(!Src::hasNext(self.container, self.iter)) [[unlikely]] { return {}; }
203 return Src::nextBack(self.container, self.iter);
204 }
205 };
207 template<typename TContainer>
208 struct trait::ExactSizeIterator<SrcCRef<TContainer>> {
209 static constexpr inline size_t size(const SrcCRef<TContainer>& self) { return self.container.size(); }
210 };
212 template<typename TContainer>
213 requires util::ContiguousMemoryContainer<TContainer>
214 struct trait::ContiguousMemoryIterator<SrcCRef<TContainer>> {
215 using ItemPtr = std::add_pointer_t<std::remove_reference_t<typename SrcCRef<TContainer>::Item>>;
216 static constexpr inline ItemPtr currentPtr(SrcCRef<TContainer>& self) {
217 return &trait::Source<TContainer>::peekNext(self.container, self.iter);
218 }
219 };
220
221}
Public Iterator API surface.
Definition: CXXIter.h:58
Container that is used to pass elements through CXXIter's iterator pipelines.
Definition: IterValue.h:19
CXXIter iterator source that immutably borrows the input item source, and passes immutable references...
CXXIter iterator source that takes over the input item source, and moves its items through the elemen...
CXXIter iterator source that mutably borrows the input item source, and passes mutable references to ...
CXXIter.
Definition: CXXIter.h:48
Trait, that iterators implement whose elements are stored in a contiguous block of memory.
Definition: Traits.h:78
static constexpr ItemPtr currentPtr(typename trait::Iterator< T >::Self &self)=delete
Get the pointer of the current element.
std::add_pointer_t< std::remove_reference_t< typename Iterator< T >::Item > > ItemPtr
Pointer type to an item of this iterator.
Definition: Traits.h:83
Trait that extends the Iterator trait with double-ended functionality.
Definition: Traits.h:98
static constexpr IterValue< typename Iterator< T >::Item > nextBack(Self &self)=delete
Pull the next last element from the iterator pipeline previous to this pipeline-element.
Trait, that extends iterators for which an exact length is known.
Definition: Traits.h:63
static constexpr size_t size(const typename trait::Iterator< T >::Self &self)=delete
Get the iterator's exact number of elements.
Trait, that is used for the chaining and the operation of iterator pipelines.
Definition: Traits.h:24
static constexpr IterValue< Item > next(Self &self)=delete
Pull one element from the iterator pipeline previous to this pipeline-element.
void Item
Item-Type. This is the type of elements that can be pulled from this pipeline-element.
Definition: Traits.h:32
trait::Iterator< T > Self
Self-Type. This is the type of the struct for which the trait::Iterator is being specialized.
Definition: Traits.h:28
static constexpr SizeHint sizeHint(const Self &self)=delete
Get the bounds on the remaining length of the iterator pipeline until this pipeline-element,...
static constexpr size_t advanceBy(Self &self, size_t n)=delete
Advance the iterator by n elements.
Type of the state structure stored in CXXIter's source classes, used to keep track of the iteration p...
Definition: Traits.h:150
Type of the state structure stored in CXXIter's source classes, used to keep track of the iteration p...
Definition: Traits.h:139
SourceTrait, that is used by CXXIter's standard source classes CXXIter::SrcMov, CXXIter::SrcRef and C...
Definition: Traits.h:121
static constexpr ItemRef peekNext(TContainer &container, IteratorState &iter)
Return the next item in the iteration with the given iter state on the given container,...
Definition: Traits.h:224
static constexpr IteratorState initIterator(TContainer &container)
Return an initial IteratorState instance for iteration on the given container.
Definition: Traits.h:172