blob: 55d3f29bba64c552bdea11d8ee5f2eeb3504800d [file] [log] [blame]
Szymon Dompke3eb56862021-09-20 15:32:04 +02001#include <vector>
2
3template <class T>
4class CircularVector
5{
6 public:
7 CircularVector(size_t maxSizeIn) : maxSize(maxSizeIn)
8 {}
9
10 template <class... Args>
11 void emplace(Args&&... args)
12 {
13 if (maxSize == 0)
14 {
15 return;
16 }
17 if (vec.size() == maxSize)
18 {
19 vec.at(idx) = T(std::forward<Args>(args)...);
20 }
21 else
22 {
23 vec.emplace_back(std::forward<Args>(args)...);
24 }
25 idx = (idx + 1) == maxSize ? 0 : (idx + 1);
26 }
27
28 void clear()
29 {
30 vec.clear();
31 idx = 0;
32 }
33
34 void clearAndResize(size_t newMaxSize)
35 {
36 clear();
37 maxSize = newMaxSize;
38 }
39
40 bool isFull() const
41 {
42 return vec.size() == maxSize;
43 }
44
45 typename std::vector<T>::const_iterator begin() const
46 {
47 return vec.begin();
48 }
49
50 typename std::vector<T>::const_iterator end() const
51 {
52 return vec.end();
53 }
54
55 private:
56 size_t maxSize = 0;
57 size_t idx = 0;
58 std::vector<T> vec;
59};