CXXIter 0.2
Loading...
Searching...
No Matches
Traits.h
1#pragma once
2
3#include "IterValue.h"
4#include "SizeHint.h"
5
9namespace CXXIter::trait {
10
11 // ################################################################################################
12 // ITERATOR TRAITS
13 // ################################################################################################
14
23 template<typename T>
24 struct Iterator {
32 using Item = void;
33
39 static constexpr inline IterValue<Item> next(Self& self) = delete;
40
46 static constexpr inline SizeHint sizeHint(const Self& self) = delete;
47
56 static constexpr inline size_t advanceBy(Self& self, size_t n) = delete;
57 };
58
62 template<typename T>
69 static constexpr inline size_t size(const typename trait::Iterator<T>::Self& self) = delete;
70 };
71
77 template<typename T>
83 using ItemPtr = std::add_pointer_t<std::remove_reference_t<typename Iterator<T>::Item>>;
89 static constexpr inline ItemPtr currentPtr(typename trait::Iterator<T>::Self& self) = delete;
90 };
91
97 template<typename T>
99 using Self = typename trait::Iterator<T>::Self;
100
106 static constexpr inline IterValue<typename Iterator<T>::Item> nextBack(Self& self) = delete;
107 };
108
109
110 // ################################################################################################
111 // SOURCE TRAITS
112 // ################################################################################################
113
121 template<typename TContainer> struct Source {
125 using Item = typename TContainer::value_type;
129 using ItemRef = typename TContainer::reference;
133 using ItemConstRef = typename TContainer::const_reference;
134
141 typename TContainer::iterator left;
143 typename TContainer::iterator right;
144 };
145
152 typename TContainer::const_iterator left;
154 typename TContainer::const_iterator right;
155 };
156
163 static constexpr inline SizeHint sizeHint(const TContainer& container) { return SizeHint(container.size(), container.size()); }
164
172 static constexpr inline IteratorState initIterator(TContainer& container) { return {container.begin(), container.end()}; }
180 static constexpr inline ConstIteratorState initIterator(const TContainer& container) { return {container.begin(), container.end()}; }
181
189 static constexpr inline bool hasNext([[maybe_unused]] TContainer& container, IteratorState& iter) { return (iter.left != iter.right); }
197 static constexpr inline bool hasNext([[maybe_unused]] const TContainer& container, ConstIteratorState& iter) { return (iter.left != iter.right); }
198
206 static constexpr inline ItemRef next([[maybe_unused]] TContainer& container, IteratorState& iter) { return (*iter.left++); }
214 static constexpr inline ItemConstRef next([[maybe_unused]] const TContainer& container, ConstIteratorState& iter) { return (*iter.left++); }
215
224 static constexpr inline ItemRef peekNext([[maybe_unused]] TContainer& container, IteratorState& iter) { return (*iter.left); }
233 static constexpr inline ItemConstRef peekNext([[maybe_unused]] const TContainer& container, ConstIteratorState& iter) { return (*iter.left); }
234
243 static constexpr inline size_t skipN([[maybe_unused]] const TContainer& container, IteratorState& iter, size_t n) {
244 size_t skipN = std::min(n, static_cast<size_t>(std::distance(iter.left, iter.right)));
245 iter.left += skipN;
246 return skipN;
247 }
256 static constexpr inline size_t skipN([[maybe_unused]] const TContainer& container, ConstIteratorState& iter, size_t n) {
257 size_t skipN = std::min(n, static_cast<size_t>(std::distance(iter.left, iter.right)));
258 iter.left += skipN;
259 return skipN;
260 }
261
274 static constexpr inline ItemRef nextBack([[maybe_unused]] TContainer& container, IteratorState& iter) { return (*--iter.right); }
283 static constexpr inline ItemConstRef nextBack([[maybe_unused]] const TContainer& container, ConstIteratorState& iter) { return (*--iter.right); }
284
293 static constexpr inline size_t skipNBack([[maybe_unused]] const TContainer& container, IteratorState& iter, size_t n) {
294 size_t skipN = std::min(n, static_cast<size_t>(std::distance(iter.left, iter.right)));
295 iter.right -= skipN;
296 return skipN;
297 }
306 static constexpr inline size_t skipNBack([[maybe_unused]] const TContainer& container, ConstIteratorState& iter, size_t n) {
307 size_t skipN = std::min(n, static_cast<size_t>(std::distance(iter.left, iter.right)));
308 iter.right -= skipN;
309 return skipN;
310 }
311 //}@
312
313 };
314
315}
Container that is used to pass elements through CXXIter's iterator pipelines.
Definition: IterValue.h:19
Trait namespace. This namespaces contains all public traits that cover all of the iterator's inner wo...
Definition: Traits.h:9
Structure holding the bounds of a CXXIter iterator's estimated length.
Definition: SizeHint.h:15
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
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
TContainer::const_iterator left
Definition: Traits.h:152
TContainer::const_iterator right
Definition: Traits.h:154
Type of the state structure stored in CXXIter's source classes, used to keep track of the iteration p...
Definition: Traits.h:139
TContainer::iterator left
Definition: Traits.h:141
TContainer::iterator right
Definition: Traits.h:143
SourceTrait, that is used by CXXIter's standard source classes CXXIter::SrcMov, CXXIter::SrcRef and C...
Definition: Traits.h:121
static constexpr ItemRef next(TContainer &container, IteratorState &iter)
Return the next item in the iteration with the given iter state on the given container.
Definition: Traits.h:206
static constexpr ItemRef nextBack(TContainer &container, IteratorState &iter)
Return the next item from the back of the iteration with the given iter state on the given container.
Definition: Traits.h:274
typename TContainer::value_type Item
Type of the item TContainer holds and provides for the iterator.
Definition: Traits.h:125
static constexpr ItemConstRef next(const TContainer &container, ConstIteratorState &iter)
Return the next item in the iteration with the given iter state on the given container.
Definition: Traits.h:214
static constexpr ItemConstRef nextBack(const TContainer &container, ConstIteratorState &iter)
Return the next item from the back of the iteration with the given iter state on the given container.
Definition: Traits.h:283
static constexpr SizeHint sizeHint(const TContainer &container)
Report a size hint for a source on the given container.
Definition: Traits.h:163
static constexpr size_t skipNBack(const TContainer &container, IteratorState &iter, size_t n)
Skip the next n elements from the back of this iterator.
Definition: Traits.h:293
static constexpr size_t skipN(const TContainer &container, ConstIteratorState &iter, size_t n)
Skip the next n elements from the container.
Definition: Traits.h:256
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
static constexpr bool hasNext(TContainer &container, IteratorState &iter)
Checks whether there is a next item in the iteration with the given iter state on the given container...
Definition: Traits.h:189
static constexpr ConstIteratorState initIterator(const TContainer &container)
Return an initial IteratorState instance for iteration on the given container.
Definition: Traits.h:180
static constexpr bool hasNext(const TContainer &container, ConstIteratorState &iter)
Checks whether there is a next item in the iteration with the given iter state on the given container...
Definition: Traits.h:197
static constexpr size_t skipNBack(const TContainer &container, ConstIteratorState &iter, size_t n)
Skip the next n elements from the back of this iterator.
Definition: Traits.h:306
static constexpr size_t skipN(const TContainer &container, IteratorState &iter, size_t n)
Skip the next n elements from the container.
Definition: Traits.h:243
static constexpr ItemConstRef peekNext(const TContainer &container, ConstIteratorState &iter)
Return the next item in the iteration with the given iter state on the given container,...
Definition: Traits.h:233
typename TContainer::reference ItemRef
Type of the item TContainer holds and provides for the iterator, in referenced form.
Definition: Traits.h:129
typename TContainer::const_reference ItemConstRef
Type of the item TContainer holds and provides for the iterator, in const referenced form.
Definition: Traits.h:133