MetroCollect  2.3.4
CircularArray.h
Go to the documentation of this file.
1 //
2 // CircularArray.h
3 //
4 // Created on July 24th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #ifndef CIRCULAR_ARRAY_H
22 #define CIRCULAR_ARRAY_H
23 
24 #include <vector>
25 
26 
27 namespace CircularArray {
41  template<typename T>
42  class CircularArray {
43  protected:
44  std::vector<T> data_;
45  size_t capacity_;
46  size_t begin_;
47  size_t size_;
48 
49  public:
50  using ValueType = T;
51 
52 
56  CircularArray();
57 
63  CircularArray(size_t n);
64 
71  CircularArray(size_t n, const T& value);
72 
73 
79  size_t capacity() const noexcept;
80 
86  size_t size() const noexcept;
87 
88 
95  T& at(ptrdiff_t index);
96 
103  const T& at(ptrdiff_t index) const;
104 
111  T& operator[](ptrdiff_t index);
112 
119  const T& operator[](ptrdiff_t index) const;
120 
121 
128  size_t absoluteIndex(ptrdiff_t index) const noexcept;
129 
136  T& atAbsoluteIndex(size_t index);
137 
144  const T& atAbsoluteIndex(size_t index) const;
145 
153  bool absoluteIndexIsInBounds(size_t index) const noexcept;
154 
155 
161  T& front();
162 
168  const T& front() const;
169 
175  T& back();
176 
182  const T& back() const;
183 
184 
190  void moveBegin(ptrdiff_t indexes) noexcept;
191 
197  void moveEnd(ptrdiff_t indexes) noexcept;
198 
199 
203  void reset() noexcept;
204 
210  void reset(size_t capacity) noexcept;
211 
218  void reset(size_t capacity, const T& value) noexcept;
219 
225  void reset(const T& value) noexcept;
226  };
227 }
228 
229 #include "CircularArray.cc"
230 
231 #endif // CIRCULAR_ARRAY_H
size_t begin_
First index in internal storage.
Definition: CircularArray.h:46
size_t size_
Number of elements currently stored.
Definition: CircularArray.h:47
size_t capacity_
Number of elements that can be held in currently allocated storage.
Definition: CircularArray.h:45
Class to compute and store metric values variations.
std::vector< T > data_
Underlying data storage.
Definition: CircularArray.h:44