|
2 | 2 |
|
3 | 3 | #include "CppImports.hpp" |
4 | 4 |
|
| 5 | +template <typename T, size_t N> |
| 6 | +class RingBuffer { |
| 7 | + std::array<T, N> buffer{}; |
5 | 8 |
|
6 | | -/** |
7 | | - * @brief a Buffer class that acts as a RingBuffer using FIFO behaviour |
8 | | - */ |
9 | | -template <typename item_type, size_t N> |
10 | | -class RingBuffer{ |
11 | | -public: |
12 | | - RingBuffer(): msize(0), mcapacity(N), mhead_index(0), mlast_index(N){} |
13 | | - |
14 | | - |
15 | | - /** |
16 | | - * @brief introduces a new item on the Ring Buffer unless it is full. Returns false when the Ring Buffer is full. |
17 | | - */ |
18 | | - bool push(item_type item){ |
19 | | - if(msize == mcapacity){ |
20 | | - return false; |
21 | | - } |
22 | | - |
23 | | - msize++; |
24 | | - mlast_index++; |
25 | | - if(mlast_index >= mcapacity){ |
26 | | - mlast_index = 0; |
27 | | - } |
28 | | - |
29 | | - buffer[mlast_index] = item; |
30 | | - return true; |
31 | | - } |
32 | | - |
33 | | - |
34 | | - bool is_empty(){return msize == 0;} /**< @brief returns true when RingBuffer is empty, false otherwise.*/ |
35 | | - bool is_full(){return msize == mcapacity;} /**< @brief returns true when RingBuffer is full, false otherwise.*/ |
36 | | - uint32_t size(){return msize;} /**< @brief returns the occupied size of the RingBuffer.*/ |
37 | | - |
38 | | - |
39 | | - |
40 | | - |
41 | | - /** |
42 | | - * @brief checks the next variable to read without removing it from the buffer. Do not use on empty buffer. |
43 | | - */ |
44 | | - item_type peek(){ |
45 | | - return buffer[mhead_index]; |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * @brief checks the next variable to read and removes it from the buffer. Do not use on empty buffer. |
50 | | - */ |
51 | | - item_type pop(){ |
52 | | - uint32_t return_index = mhead_index; |
53 | | - mhead_index++; |
54 | | - msize--; |
55 | | - if(mhead_index >= mcapacity){ |
56 | | - mhead_index = 0; |
57 | | - } |
58 | | - return buffer[return_index]; |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * @brief pop later and push new. Don't care size of buffer, needs to be initialize before |
63 | | - */ |
64 | | - void push_pop(item_type item){ |
65 | | - mhead_index = (mhead_index+1)%mcapacity; |
66 | | - mlast_index = (mlast_index+1)%mcapacity; |
67 | | - buffer[mhead_index] = item; |
68 | | - } |
69 | | - |
70 | | - item_type latest()const{ |
71 | | - return buffer[mlast_index]; |
72 | | - } |
73 | | - |
74 | | -private: |
75 | | - std::array<item_type, N> buffer; |
76 | | - uint32_t msize; |
77 | | - uint32_t mcapacity; |
78 | | - uint32_t mhead_index; |
79 | | - uint32_t mlast_index; |
| 9 | + size_t stored_items{0}; |
| 10 | + |
| 11 | + size_t front{0}; |
| 12 | + size_t back{0}; |
| 13 | + |
| 14 | + size_t move_forward(size_t origin, size_t amount) { |
| 15 | + return (origin + amount) % N; |
| 16 | + } |
| 17 | + |
| 18 | + size_t move_backward(size_t origin, size_t amount) { |
| 19 | + // N * ((amount / N) + 1) makes it so that the operation doesn't |
| 20 | + // overflow (size_t is unsigned) and with the help of modular |
| 21 | + // arithmetic, this won't change the result |
| 22 | + return ((origin + (N * ((amount / N) + 1))) - amount) % N; |
| 23 | + } |
| 24 | + |
| 25 | + public: |
| 26 | + RingBuffer() {} |
| 27 | + |
| 28 | + bool push(T item) { |
| 29 | + if (is_full()) return false; |
| 30 | + |
| 31 | + buffer[front] = item; |
| 32 | + front = move_forward(front, 1); |
| 33 | + ++stored_items; |
| 34 | + |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + bool pop() { |
| 39 | + if (is_empty()) return false; |
| 40 | + |
| 41 | + back = move_forward(back, 1); |
| 42 | + --stored_items; |
| 43 | + |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + bool push_pop(T item) { |
| 48 | + if (is_empty()) return false; |
| 49 | + |
| 50 | + buffer[front] = item; |
| 51 | + front = move_forward(front, 1); |
| 52 | + back = move_forward(back, 1); |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + T &operator[](size_t index) { |
| 58 | + return buffer[move_backward(front, index + 1)]; |
| 59 | + } |
| 60 | + |
| 61 | + constexpr size_t capacity() { return N; } |
| 62 | + size_t size() { return stored_items; } |
| 63 | + bool is_full() { return size() >= capacity(); } |
| 64 | + bool is_empty() { return size() == 0; } |
80 | 65 | }; |
0 commit comments