quala 0.0.1a1
Quasi-Newton and other accelerators
ringbuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <iterator>
6#include <vector>
7
8namespace quala {
9
10template <class T>
12 public:
13 MaxHistory(size_t memory) : buffer(std::vector<T>(memory)) {}
14
15 void add(T newt) {
16 if (full) {
17 T oldt = std::move(*it);
18 *it = std::move(newt);
19 if (*it > max_)
20 max_ = *it;
21 else if (oldt == max_)
22 max_ = *std::max_element(buffer.begin(), buffer.end());
23 ++it;
24 if (it == buffer.end())
25 it = buffer.begin();
26 } else {
27 if (it == buffer.begin() || newt > max_)
28 max_ = newt;
29 *it = std::move(newt);
30 ++it;
31 if (it == buffer.end()) {
32 it = buffer.begin();
33 full = true;
34 }
35 }
36 }
37
38 const T &max() const { return max_; }
39
40 private:
41 std::vector<T> buffer;
42 bool full = false;
43 decltype(buffer.begin()) it = buffer.begin();
44 T max_{};
45};
46
47template <class IndexT = size_t>
49 using Index = IndexT;
54};
55/// @related CircularIndices
56/// @note Only valid for two indices in the same range.
57template <class IndexT>
59 return a.zerobased == b.zerobased;
60}
61/// @related CircularIndices
62/// @note Only valid for two indices in the same range.
63template <class IndexT>
65 return !(a == b);
66}
67
68template <class IndexT = size_t>
70 using Index = IndexT;
72
73 CircularIndexIterator() : i{0, 0}, max{0} {}
75
78
81 using difference_type = std::ptrdiff_t; // This is required but not used
82 using pointer = void;
83 using iterator_category = std::input_iterator_tag;
84
85 reference operator*() const { return i; }
87 assert(i.zerobased < max);
88 ++i.zerobased;
89 i.circular = i.circular + 1 == max ? Index{0} : i.circular + 1;
90 return *this;
91 }
93 assert(i.zerobased > 0);
94 --i.zerobased;
95 i.circular = i.circular == Index{0} ? max - 1 : i.circular - 1;
96 return *this;
97 }
99 auto r = *this;
100 ++(*this);
101 return r;
102 }
104 auto r = *this;
105 --(*this);
106 return r;
107 }
108};
109
110/// @related CircularIndexIterator
111/// @note Only valid for two indices in the same range.
112template <class IndexT>
115 assert(a.max == b.max);
116 return a.i == b.i;
117}
118/// @related CircularIndexIterator
119/// @note Only valid for two indices in the same range.
120template <class IndexT>
123 return !(a == b);
124}
125
126template <class IndexT = size_t>
131
135 : forwardit(forwardit) {}
136
138
141 using difference_type = std::ptrdiff_t; // This is required but not used
142 using pointer = void;
143 using iterator_category = std::input_iterator_tag;
144
146 auto tmp = forwardit;
147 return *(--tmp);
148 }
150 --forwardit;
151 return *this;
152 }
154 ++forwardit;
155 return *this;
156 }
158 auto r = *this;
159 ++(*this);
160 return r;
161 }
163 auto r = *this;
164 --(*this);
165 return r;
166 }
167};
168
169/// @related ReverseCircularIndexIterator
170/// @note Only valid for two indices in the same range.
171template <class IndexT>
174 return a.forwardit == b.forwardit;
175}
176/// @related ReverseCircularIndexIterator
177/// @note Only valid for two indices in the same range.
178template <class IndexT>
181 return !(a == b);
182}
183
184template <class IndexT>
186 public:
187 using Index = IndexT;
189
191 : size(size), idx1(idx1), idx2(idx2), max(max) {}
192
195
198
199 iterator begin() const { return {{Index{0}, idx1}, max}; }
200 iterator end() const { return {{size, idx2}, max}; }
201 const_iterator cbegin() const { return begin(); }
202 const_iterator cend() const { return end(); }
203
207 return const_reverse_iterator{end()};
208 }
211 }
212
213 private:
217};
218
219template <class IndexT>
221 public:
223 using Index = typename ForwardRange::Index;
225
229 : forwardrange(size, idx1, idx2, max) {}
230
233
236
237 iterator begin() const { return forwardrange.rbegin(); }
238 iterator end() const { return forwardrange.rend(); }
240 const_iterator cend() const { return forwardrange.crend(); }
241
243 reverse_iterator rend() const { return forwardrange.end(); }
246
247 private:
249};
250
251} // namespace quala
iterator begin() const
Definition: ringbuffer.hpp:199
const_reverse_iterator reverse_iterator
Definition: ringbuffer.hpp:197
const_iterator cbegin() const
Definition: ringbuffer.hpp:201
reverse_iterator rbegin() const
Definition: ringbuffer.hpp:204
iterator end() const
Definition: ringbuffer.hpp:200
CircularIndices< Index > Indices
Definition: ringbuffer.hpp:188
const_reverse_iterator crbegin() const
Definition: ringbuffer.hpp:206
const_iterator cend() const
Definition: ringbuffer.hpp:202
CircularRange(Index size, Index idx1, Index idx2, Index max)
Definition: ringbuffer.hpp:190
const_iterator iterator
Definition: ringbuffer.hpp:194
reverse_iterator rend() const
Definition: ringbuffer.hpp:205
const_reverse_iterator crend() const
Definition: ringbuffer.hpp:209
CircularIndexIterator< Index > const_iterator
Definition: ringbuffer.hpp:193
ReverseCircularIndexIterator< Index > const_reverse_iterator
Definition: ringbuffer.hpp:196
MaxHistory(size_t memory)
Definition: ringbuffer.hpp:13
void add(T newt)
Definition: ringbuffer.hpp:15
std::vector< T > buffer
Definition: ringbuffer.hpp:41
decltype(buffer.begin()) it
Definition: ringbuffer.hpp:43
const T & max() const
Definition: ringbuffer.hpp:38
ReverseCircularRange(const ForwardRange &forwardrange)
Definition: ringbuffer.hpp:226
typename ForwardRange::iterator reverse_iterator
Definition: ringbuffer.hpp:235
typename ForwardRange::const_reverse_iterator const_iterator
Definition: ringbuffer.hpp:231
ReverseCircularRange(Index size, Index idx1, Index idx2, Index max)
Definition: ringbuffer.hpp:228
const_iterator cbegin() const
Definition: ringbuffer.hpp:239
reverse_iterator rbegin() const
Definition: ringbuffer.hpp:242
typename ForwardRange::const_iterator const_reverse_iterator
Definition: ringbuffer.hpp:234
typename ForwardRange::Indices Indices
Definition: ringbuffer.hpp:224
const_reverse_iterator crbegin() const
Definition: ringbuffer.hpp:244
const_iterator cend() const
Definition: ringbuffer.hpp:240
reverse_iterator rend() const
Definition: ringbuffer.hpp:243
const_reverse_iterator crend() const
Definition: ringbuffer.hpp:245
typename ForwardRange::Index Index
Definition: ringbuffer.hpp:223
typename ForwardRange::reverse_iterator iterator
Definition: ringbuffer.hpp:232
CircularIndexIterator & operator--()
Definition: ringbuffer.hpp:92
CircularIndexIterator(Indices i, Index max)
Definition: ringbuffer.hpp:74
CircularIndexIterator & operator++()
Definition: ringbuffer.hpp:86
CircularIndexIterator operator++(int)
Definition: ringbuffer.hpp:98
CircularIndexIterator operator--(int)
Definition: ringbuffer.hpp:103
CircularIndices< Index > Indices
Definition: ringbuffer.hpp:71
bool operator!=(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:121
reference operator*() const
Definition: ringbuffer.hpp:85
bool operator==(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:113
std::input_iterator_tag iterator_category
Definition: ringbuffer.hpp:83
std::ptrdiff_t difference_type
Definition: ringbuffer.hpp:81
bool operator!=(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
Definition: ringbuffer.hpp:64
bool operator==(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
Definition: ringbuffer.hpp:58
CircularIndices(Index zerobased, Index circular)
Definition: ringbuffer.hpp:50
ReverseCircularIndexIterator operator--(int)
Definition: ringbuffer.hpp:162
ReverseCircularIndexIterator operator++(int)
Definition: ringbuffer.hpp:157
bool operator==(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:172
ReverseCircularIndexIterator(Indices i, Index max)
Definition: ringbuffer.hpp:133
typename ForwardIterator::Indices Indices
Definition: ringbuffer.hpp:130
ReverseCircularIndexIterator & operator--()
Definition: ringbuffer.hpp:153
std::input_iterator_tag iterator_category
Definition: ringbuffer.hpp:143
ReverseCircularIndexIterator & operator++()
Definition: ringbuffer.hpp:149
typename ForwardIterator::Index Index
Definition: ringbuffer.hpp:129
bool operator!=(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:179
ReverseCircularIndexIterator(ForwardIterator forwardit)
Definition: ringbuffer.hpp:134