guanaqo 1.0.0-alpha.24
Utilities for scientific software
Loading...
Searching...
No Matches
ringbuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup core
5/// Circular index utilities and iterators.
6
7#include <cstddef>
8#include <iterator>
9namespace guanaqo {
10
11/// @ingroup core
12template <class IndexT = size_t>
20/// @related CircularIndices
21/// @note Only valid for two indices in the same range.
22template <class IndexT>
26/// @related CircularIndices
27/// @note Only valid for two indices in the same range.
28template <class IndexT>
30 return !(a == b);
31}
32
33/// @ingroup core
34template <class IndexT = size_t>
36 using Index = IndexT;
38
39 CircularIndexIterator() : i{0, 0}, max{0} {}
41
44
47 using difference_type = std::ptrdiff_t; // This is required but not used
48 using pointer = void;
49 using iterator_category = std::input_iterator_tag;
50
51 reference operator*() const { return i; }
53 assert(i.zerobased < max);
54 ++i.zerobased;
55 i.circular = i.circular + 1 == max ? Index{0} : i.circular + 1;
56 return *this;
57 }
59 assert(i.zerobased > 0);
60 --i.zerobased;
61 i.circular = i.circular == Index{0} ? max - 1 : i.circular - 1;
62 return *this;
63 }
65 auto r = *this;
66 ++(*this);
67 return r;
68 }
70 auto r = *this;
71 --(*this);
72 return r;
73 }
74};
75
76/// @related CircularIndexIterator
77/// @note Only valid for two indices in the same range.
78template <class IndexT>
81 assert(a.max == b.max);
82 return a.i == b.i;
83}
84/// @related CircularIndexIterator
85/// @note Only valid for two indices in the same range.
86template <class IndexT>
91
92/// @ingroup core
93template <class IndexT = size_t>
96 using Index = typename ForwardIterator::Index;
98
103
105
108 using difference_type = std::ptrdiff_t; // This is required but not used
109 using pointer = void;
110 using iterator_category = std::input_iterator_tag;
111
113 auto tmp = forwardit;
114 return *(--tmp);
115 }
117 --forwardit;
118 return *this;
119 }
121 ++forwardit;
122 return *this;
123 }
125 auto r = *this;
126 ++(*this);
127 return r;
128 }
130 auto r = *this;
131 --(*this);
132 return r;
133 }
134};
135
136/// @related ReverseCircularIndexIterator
137/// @note Only valid for two indices in the same range.
138template <class IndexT>
143/// @related ReverseCircularIndexIterator
144/// @note Only valid for two indices in the same range.
145template <class IndexT>
150
151/// @ingroup core
152template <class IndexT>
186
187/// @ingroup core
188template <class IndexT>
190 public:
192 using Index = typename ForwardRange::Index;
194
198 : forwardrange(size, idx1, idx2, max) {}
199
202
205
206 iterator begin() const { return forwardrange.rbegin(); }
207 iterator end() const { return forwardrange.rend(); }
208 const_iterator cbegin() const { return forwardrange.crbegin(); }
209 const_iterator cend() const { return forwardrange.crend(); }
210
211 reverse_iterator rbegin() const { return forwardrange.begin(); }
212 reverse_iterator rend() const { return forwardrange.end(); }
213 const_reverse_iterator crbegin() const { return forwardrange.cbegin(); }
214 const_reverse_iterator crend() const { return forwardrange.cend(); }
215
216 private:
218};
219
220} // namespace guanaqo
CircularIndexIterator< Index > const_iterator
const_reverse_iterator crbegin() const
CircularRange(Index size, Index idx1, Index idx2, Index max)
const_reverse_iterator reverse_iterator
CircularIndices< Index > Indices
reverse_iterator rend() const
iterator end() const
const_reverse_iterator crend() const
reverse_iterator rbegin() const
ReverseCircularIndexIterator< Index > const_reverse_iterator
const_iterator iterator
iterator begin() const
const_iterator cend() const
const_iterator cbegin() const
typename ForwardRange::reverse_iterator iterator
const_iterator cend() const
const_reverse_iterator crend() const
const_reverse_iterator crbegin() const
typename ForwardRange::Index Index
typename ForwardRange::Indices Indices
ReverseCircularRange(Index size, Index idx1, Index idx2, Index max)
CircularRange< IndexT > ForwardRange
reverse_iterator rend() const
reverse_iterator rbegin() const
typename ForwardRange::iterator reverse_iterator
ReverseCircularRange(const ForwardRange &forwardrange)
typename ForwardRange::const_iterator const_reverse_iterator
typename ForwardRange::const_reverse_iterator const_iterator
const_iterator cbegin() const
CircularIndexIterator operator--(int)
CircularIndexIterator(Indices i, Index max)
CircularIndices< Index > Indices
CircularIndexIterator & operator--()
bool operator!=(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
CircularIndexIterator operator++(int)
bool operator==(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
std::input_iterator_tag iterator_category
CircularIndexIterator & operator++()
bool operator!=(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
bool operator==(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
CircularIndices(Index zerobased, Index circular)
ReverseCircularIndexIterator & operator++()
CircularIndexIterator< IndexT > ForwardIterator
bool operator==(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)
ReverseCircularIndexIterator & operator--()
ReverseCircularIndexIterator operator--(int)
std::input_iterator_tag iterator_category
ReverseCircularIndexIterator(ForwardIterator forwardit)
typename ForwardIterator::Index Index
typename ForwardIterator::Indices Indices
ReverseCircularIndexIterator operator++(int)
ReverseCircularIndexIterator(Indices i, Index max)
bool operator!=(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)