PANOC-ALM  quadratic-penalty
Nonconvex constrained optimization
ringbuffer.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstddef>
4 #include <iterator>
5 
6 namespace pa {
7 
8 template <class IndexT = size_t>
10  using Index = IndexT;
15 };
18 template <class IndexT>
20  return a.zerobased == b.zerobased;
21 }
24 template <class IndexT>
26  return !(a == b);
27 }
28 
29 template <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 
73 template <class IndexT>
76  assert(a.max == b.max);
77  return a.i == b.i;
78 }
81 template <class IndexT>
84  return !(a == b);
85 }
86 
87 template <class IndexT = size_t>
90  using Index = typename ForwardIterator::Index;
92 
96  : forwardit(forwardit) {}
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 
132 template <class IndexT>
135  return a.forwardit == b.forwardit;
136 }
139 template <class IndexT>
142  return !(a == b);
143 }
144 
145 template <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  }
171  return const_reverse_iterator{begin()};
172  }
173 
174  private:
178 };
179 
180 template <class IndexT>
182  public:
184  using Index = typename ForwardRange::Index;
185  using Indices = typename ForwardRange::Indices;
186 
189  ReverseCircularRange(Index size, Index idx1, Index idx2, Index max)
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 pa
pa::CircularRange::Indices
CircularIndices< Index > Indices
Definition: ringbuffer.hpp:149
pa::ReverseCircularRange::ReverseCircularRange
ReverseCircularRange(Index size, Index idx1, Index idx2, Index max)
Definition: ringbuffer.hpp:189
pa::ReverseCircularRange::rbegin
reverse_iterator rbegin() const
Definition: ringbuffer.hpp:203
pa::CircularRange::rend
reverse_iterator rend() const
Definition: ringbuffer.hpp:166
pa::ReverseCircularRange::cbegin
const_iterator cbegin() const
Definition: ringbuffer.hpp:200
pa::ReverseCircularIndexIterator::operator--
ReverseCircularIndexIterator operator--(int)
Definition: ringbuffer.hpp:123
pa::ReverseCircularRange::ReverseCircularRange
ReverseCircularRange(const ForwardRange &forwardrange)
Definition: ringbuffer.hpp:187
pa::CircularIndexIterator::operator++
CircularIndexIterator operator++(int)
Definition: ringbuffer.hpp:59
pa::ReverseCircularRange::begin
iterator begin() const
Definition: ringbuffer.hpp:198
pa::CircularRange::cend
const_iterator cend() const
Definition: ringbuffer.hpp:163
pa::ReverseCircularRange::crend
const_reverse_iterator crend() const
Definition: ringbuffer.hpp:206
pa::ReverseCircularRange::Indices
typename ForwardRange::Indices Indices
Definition: ringbuffer.hpp:185
pa::ReverseCircularRange::const_iterator
typename ForwardRange::const_reverse_iterator const_iterator
Definition: ringbuffer.hpp:192
pa::CircularIndexIterator
Definition: ringbuffer.hpp:30
pa::CircularIndexIterator::CircularIndexIterator
CircularIndexIterator(Indices i, Index max)
Definition: ringbuffer.hpp:35
pa::ReverseCircularRange::iterator
typename ForwardRange::reverse_iterator iterator
Definition: ringbuffer.hpp:193
pa::ReverseCircularRange::reverse_iterator
typename ForwardRange::iterator reverse_iterator
Definition: ringbuffer.hpp:196
pa::CircularRange::end
iterator end() const
Definition: ringbuffer.hpp:161
pa::CircularIndexIterator::value_type
Indices value_type
Definition: ringbuffer.hpp:40
pa::CircularRange::Index
IndexT Index
Definition: ringbuffer.hpp:148
pa::CircularIndexIterator::operator--
CircularIndexIterator & operator--()
Definition: ringbuffer.hpp:53
pa::CircularIndexIterator::operator==
bool operator==(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:74
pa::CircularIndices::operator!=
bool operator!=(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
Definition: ringbuffer.hpp:25
pa::ReverseCircularIndexIterator::ReverseCircularIndexIterator
ReverseCircularIndexIterator()
Definition: ringbuffer.hpp:93
pa::ReverseCircularIndexIterator::value_type
Indices value_type
Definition: ringbuffer.hpp:100
pa::CircularIndices::operator==
bool operator==(CircularIndices< IndexT > a, CircularIndices< IndexT > b)
Definition: ringbuffer.hpp:19
pa::CircularIndexIterator< size_t >::pointer
void pointer
Definition: ringbuffer.hpp:43
pa::CircularRange
Definition: ringbuffer.hpp:146
pa::ReverseCircularIndexIterator
Definition: ringbuffer.hpp:88
pa::ReverseCircularIndexIterator::Index
typename ForwardIterator::Index Index
Definition: ringbuffer.hpp:90
pa::CircularIndexIterator::i
Indices i
Definition: ringbuffer.hpp:37
pa
Definition: alm.hpp:10
pa::CircularIndices< Index >::Index
Index Index
Definition: ringbuffer.hpp:10
pa::CircularRange::cbegin
const_iterator cbegin() const
Definition: ringbuffer.hpp:162
pa::ReverseCircularIndexIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: ringbuffer.hpp:104
pa::CircularIndexIterator< size_t >::Index
size_t Index
Definition: ringbuffer.hpp:31
pa::ReverseCircularRange
Definition: ringbuffer.hpp:181
pa::CircularRange::iterator
const_iterator iterator
Definition: ringbuffer.hpp:155
pa::CircularIndices::CircularIndices
CircularIndices(Index zerobased, Index circular)
Definition: ringbuffer.hpp:11
pa::ReverseCircularIndexIterator::forwardit
ForwardIterator forwardit
Definition: ringbuffer.hpp:98
pa::CircularRange::max
Index max
Definition: ringbuffer.hpp:177
pa::ReverseCircularIndexIterator::ReverseCircularIndexIterator
ReverseCircularIndexIterator(Indices i, Index max)
Definition: ringbuffer.hpp:94
pa::CircularRange::begin
iterator begin() const
Definition: ringbuffer.hpp:160
pa::ReverseCircularRange::crbegin
const_reverse_iterator crbegin() const
Definition: ringbuffer.hpp:205
pa::ReverseCircularRange::Index
typename ForwardRange::Index Index
Definition: ringbuffer.hpp:184
pa::ReverseCircularIndexIterator::operator--
ReverseCircularIndexIterator & operator--()
Definition: ringbuffer.hpp:114
pa::ReverseCircularIndexIterator::ReverseCircularIndexIterator
ReverseCircularIndexIterator(ForwardIterator forwardit)
Definition: ringbuffer.hpp:95
pa::CircularIndexIterator::operator*
reference operator*() const
Definition: ringbuffer.hpp:46
pa::ReverseCircularRange::const_reverse_iterator
typename ForwardRange::const_iterator const_reverse_iterator
Definition: ringbuffer.hpp:195
pa::CircularIndexIterator< size_t >::difference_type
std::ptrdiff_t difference_type
Definition: ringbuffer.hpp:42
pa::CircularRange::reverse_iterator
const_reverse_iterator reverse_iterator
Definition: ringbuffer.hpp:158
pa::CircularRange::size
Index size
Definition: ringbuffer.hpp:175
pa::ReverseCircularIndexIterator::Indices
typename ForwardIterator::Indices Indices
Definition: ringbuffer.hpp:91
main.b
b
Definition: main.py:11
pa::CircularRange::idx1
Index idx1
Definition: ringbuffer.hpp:176
pa::CircularRange::idx2
Index idx2
Definition: ringbuffer.hpp:176
pa::CircularRange::crbegin
const_reverse_iterator crbegin() const
Definition: ringbuffer.hpp:167
pa::ReverseCircularIndexIterator::pointer
void pointer
Definition: ringbuffer.hpp:103
pa::ReverseCircularIndexIterator::operator++
ReverseCircularIndexIterator & operator++()
Definition: ringbuffer.hpp:110
pa::CircularIndexIterator::Indices
CircularIndices< Index > Indices
Definition: ringbuffer.hpp:32
pa::CircularIndexIterator::operator++
CircularIndexIterator & operator++()
Definition: ringbuffer.hpp:47
pa::ReverseCircularIndexIterator::operator==
bool operator==(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:133
pa::CircularIndexIterator::operator!=
bool operator!=(CircularIndexIterator< IndexT > a, CircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:82
pa::CircularIndexIterator< size_t >::iterator_category
std::input_iterator_tag iterator_category
Definition: ringbuffer.hpp:44
pa::CircularIndexIterator::CircularIndexIterator
CircularIndexIterator()
Definition: ringbuffer.hpp:34
pa::ReverseCircularIndexIterator::difference_type
std::ptrdiff_t difference_type
Definition: ringbuffer.hpp:102
pa::ReverseCircularRange::end
iterator end() const
Definition: ringbuffer.hpp:199
pa::ReverseCircularRange::forwardrange
ForwardRange forwardrange
Definition: ringbuffer.hpp:209
pa::ReverseCircularRange::rend
reverse_iterator rend() const
Definition: ringbuffer.hpp:204
pa::CircularIndices::circular
Index circular
Definition: ringbuffer.hpp:14
pa::ReverseCircularIndexIterator::reference
value_type reference
Definition: ringbuffer.hpp:101
pa::CircularIndexIterator::max
Index max
Definition: ringbuffer.hpp:38
pa::CircularRange::crend
const_reverse_iterator crend() const
Definition: ringbuffer.hpp:170
pa::CircularIndices::zerobased
Index zerobased
Definition: ringbuffer.hpp:13
pa::CircularRange::CircularRange
CircularRange(Index size, Index idx1, Index idx2, Index max)
Definition: ringbuffer.hpp:151
pa::ReverseCircularRange::cend
const_iterator cend() const
Definition: ringbuffer.hpp:201
pa::CircularRange::rbegin
reverse_iterator rbegin() const
Definition: ringbuffer.hpp:165
pa::CircularIndices
Definition: ringbuffer.hpp:9
pa::CircularRange::const_iterator
CircularIndexIterator< Index > const_iterator
Definition: ringbuffer.hpp:154
pa::ReverseCircularIndexIterator::operator!=
bool operator!=(ReverseCircularIndexIterator< IndexT > a, ReverseCircularIndexIterator< IndexT > b)
Definition: ringbuffer.hpp:140
pa::CircularRange::const_reverse_iterator
ReverseCircularIndexIterator< Index > const_reverse_iterator
Definition: ringbuffer.hpp:157
pa::CircularIndexIterator::operator--
CircularIndexIterator operator--(int)
Definition: ringbuffer.hpp:64
pa::ReverseCircularIndexIterator::operator*
reference operator*() const
Definition: ringbuffer.hpp:106
pa::ReverseCircularIndexIterator::operator++
ReverseCircularIndexIterator operator++(int)
Definition: ringbuffer.hpp:118