alpaqa 1.0.0a9
Nonconvex constrained optimization
Loading...
Searching...
No Matches
ringbuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <iterator>
5
6namespace alpaqa {
7
8template <class IndexT = size_t>
10 using Index = IndexT;
15};
16/// @related CircularIndices
17/// @note Only valid for two indices in the same range.
18template <class IndexT>
20 return a.zerobased == b.zerobased;
21}
22/// @related CircularIndices
23/// @note Only valid for two indices in the same range.
24template <class IndexT>
26 return !(a == b);
27}
28
29template <class IndexT = size_t>
31 using Index = IndexT;
33
34 CircularIndexIterator() : i{0, 0}, max{0} {}
36
39
42 using difference_type = std::ptrdiff_t; // This is required but not used
43 using pointer = void;
44 using iterator_category = std::input_iterator_tag;
45
46 reference operator*() const { return i; }
48 assert(i.zerobased < max);
49 ++i.zerobased;
50 i.circular = i.circular + 1 == max ? Index{0} : i.circular + 1;
51 return *this;
52 }
54 assert(i.zerobased > 0);
55 --i.zerobased;
56 i.circular = i.circular == Index{0} ? max - 1 : i.circular - 1;
57 return *this;
58 }
60 auto r = *this;
61 ++(*this);
62 return r;
63 }
65 auto r = *this;
66 --(*this);
67 return r;
68 }
69};
70
71/// @related CircularIndexIterator
72/// @note Only valid for two indices in the same range.
73template <class IndexT>
76 assert(a.max == b.max);
77 return a.i == b.i;
78}
79/// @related CircularIndexIterator
80/// @note Only valid for two indices in the same range.
81template <class IndexT>
84 return !(a == b);
85}
86
87template <class IndexT = size_t>
90 using Index = typename ForwardIterator::Index;
92
97
99
102 using difference_type = std::ptrdiff_t; // This is required but not used
103 using pointer = void;
104 using iterator_category = std::input_iterator_tag;
105
107 auto tmp = forwardit;
108 return *(--tmp);
109 }
111 --forwardit;
112 return *this;
113 }
115 ++forwardit;
116 return *this;
117 }
119 auto r = *this;
120 ++(*this);
121 return r;
122 }
124 auto r = *this;
125 --(*this);
126 return r;
127 }
128};
129
130/// @related ReverseCircularIndexIterator
131/// @note Only valid for two indices in the same range.
132template <class IndexT>
135 return a.forwardit == b.forwardit;
136}
137/// @related ReverseCircularIndexIterator
138/// @note Only valid for two indices in the same range.
139template <class IndexT>
142 return !(a == b);
143}
144
145template <class IndexT>
147 public:
148 using Index = IndexT;
150
152 : size(size), idx1(idx1), idx2(idx2), max(max) {}
153
156
159
160 iterator begin() const { return {{Index{0}, idx1}, max}; }
161 iterator end() const { return {{size, idx2}, max}; }
162 const_iterator cbegin() const { return begin(); }
163 const_iterator cend() const { return end(); }
164
168 return const_reverse_iterator{end()};
169 }
172 }
173
174 private:
178};
179
180template <class IndexT>
182 public:
184 using Index = typename ForwardRange::Index;
186
190 : forwardrange(size, idx1, idx2, max) {}
191
194
197
198 iterator begin() const { return forwardrange.rbegin(); }
199 iterator end() const { return forwardrange.rend(); }
201 const_iterator cend() const { return forwardrange.crend(); }
202
204 reverse_iterator rend() const { return forwardrange.end(); }
207
208 private:
210};
211
212} // namespace alpaqa
iterator begin() const
Definition: ringbuffer.hpp:160
const_reverse_iterator reverse_iterator
Definition: ringbuffer.hpp:158
const_iterator cbegin() const
Definition: ringbuffer.hpp:162
reverse_iterator rbegin() const
Definition: ringbuffer.hpp:165
iterator end() const
Definition: ringbuffer.hpp:161
CircularIndices< Index > Indices
Definition: ringbuffer.hpp:149
const_reverse_iterator crbegin() const
Definition: ringbuffer.hpp:167
const_iterator cend() const
Definition: ringbuffer.hpp:163
CircularRange(Index size, Index idx1, Index idx2, Index max)
Definition: ringbuffer.hpp:151
const_iterator iterator
Definition: ringbuffer.hpp:155
reverse_iterator rend() const
Definition: ringbuffer.hpp:166
const_reverse_iterator crend() const
Definition: ringbuffer.hpp:170
CircularIndexIterator< Index > const_iterator
Definition: ringbuffer.hpp:154
ReverseCircularIndexIterator< Index > const_reverse_iterator
Definition: ringbuffer.hpp:157
ReverseCircularRange(const ForwardRange &forwardrange)
Definition: ringbuffer.hpp:187
typename ForwardRange::iterator reverse_iterator
Definition: ringbuffer.hpp:196
typename ForwardRange::const_reverse_iterator const_iterator
Definition: ringbuffer.hpp:192
ReverseCircularRange(Index size, Index idx1, Index idx2, Index max)
Definition: ringbuffer.hpp:189
const_iterator cbegin() const
Definition: ringbuffer.hpp:200
reverse_iterator rbegin() const
Definition: ringbuffer.hpp:203
typename ForwardRange::const_iterator const_reverse_iterator
Definition: ringbuffer.hpp:195
typename ForwardRange::Indices Indices
Definition: ringbuffer.hpp:185
const_reverse_iterator crbegin() const
Definition: ringbuffer.hpp:205
const_iterator cend() const
Definition: ringbuffer.hpp:201
reverse_iterator rend() const
Definition: ringbuffer.hpp:204
const_reverse_iterator crend() const
Definition: ringbuffer.hpp:206
typename ForwardRange::Index Index
Definition: ringbuffer.hpp:184
typename ForwardRange::reverse_iterator iterator
Definition: ringbuffer.hpp:193
CircularIndexIterator & operator--()
Definition: ringbuffer.hpp:53
CircularIndexIterator(Indices i, Index max)
Definition: ringbuffer.hpp:35
CircularIndexIterator & operator++()
Definition: ringbuffer.hpp:47
CircularIndexIterator operator++(int)
Definition: ringbuffer.hpp:59
CircularIndexIterator operator--(int)
Definition: ringbuffer.hpp:64
CircularIndices< Index > Indices
Definition: ringbuffer.hpp:32
bool operator!=(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:82
reference operator*() const
Definition: ringbuffer.hpp:46
bool operator==(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:74
std::input_iterator_tag iterator_category
Definition: ringbuffer.hpp:44
std::ptrdiff_t difference_type
Definition: ringbuffer.hpp:42
bool operator!=(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
Definition: ringbuffer.hpp:25
bool operator==(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
Definition: ringbuffer.hpp:19
CircularIndices(Index zerobased, Index circular)
Definition: ringbuffer.hpp:11
ReverseCircularIndexIterator operator--(int)
Definition: ringbuffer.hpp:123
ReverseCircularIndexIterator operator++(int)
Definition: ringbuffer.hpp:118
bool operator==(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:133
ReverseCircularIndexIterator(Indices i, Index max)
Definition: ringbuffer.hpp:94
typename ForwardIterator::Indices Indices
Definition: ringbuffer.hpp:91
ReverseCircularIndexIterator & operator--()
Definition: ringbuffer.hpp:114
std::input_iterator_tag iterator_category
Definition: ringbuffer.hpp:104
ReverseCircularIndexIterator & operator++()
Definition: ringbuffer.hpp:110
typename ForwardIterator::Index Index
Definition: ringbuffer.hpp:90
bool operator!=(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:140
ReverseCircularIndexIterator(ForwardIterator forwardit)
Definition: ringbuffer.hpp:95