CatapultServer  v0.5.0.1 (Elephant)
CircularBuffer.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include <vector>
23 #include <stddef.h>
24 
25 namespace catapult { namespace utils {
26 
28  template<typename T>
30  public:
32  explicit CircularBuffer(size_t size)
33  : m_capacity(size)
34  , m_buffer(size)
35  , m_next(0)
36  {}
37 
38  public:
40  void push_back(const T& element) {
41  m_buffer[incrementNext()] = element;
42  }
43 
45  void push_back(T&& element) {
46  m_buffer[incrementNext()] = std::move(element);
47  }
48 
49  public:
51  constexpr size_t size() const {
52  return m_next < m_capacity ? m_next : m_capacity;
53  }
54 
56  constexpr size_t capacity() const {
57  return m_capacity;
58  }
59 
60  public:
62  T& operator[](size_t index) {
63  return m_buffer[truncateIndex(index)];
64  }
65 
67  const T& operator[](size_t index) const {
68  return m_buffer[truncateIndex(index)];
69  }
70 
71  private:
72  inline size_t incrementNext() {
73  size_t next = truncateIndex(m_next);
74  ++m_next;
75  return next;
76  }
77 
78  constexpr size_t truncateIndex(size_t index) const {
79  return index % m_capacity;
80  }
81 
82  private:
83  const size_t m_capacity;
84  std::vector<T> m_buffer;
85  size_t m_next;
86  };
87 }}
catapult::utils::CircularBuffer::push_back
void push_back(const T &element)
Appends element to the end of the buffer, possibly overwriting existing elements.
Definition: CircularBuffer.h:40
catapult::utils::CircularBuffer::incrementNext
size_t incrementNext()
Definition: CircularBuffer.h:72
catapult::utils::CircularBuffer::m_next
size_t m_next
Definition: CircularBuffer.h:85
catapult::utils::CircularBuffer::m_capacity
const size_t m_capacity
Definition: CircularBuffer.h:83
catapult::utils::CircularBuffer::truncateIndex
constexpr size_t truncateIndex(size_t index) const
Definition: CircularBuffer.h:78
catapult::utils::CircularBuffer
A fixed size circular buffer.
Definition: CircularBuffer.h:29
catapult::utils::CircularBuffer::operator[]
T & operator[](size_t index)
Gets the element at index.
Definition: CircularBuffer.h:62
catapult::utils::CircularBuffer::operator[]
const T & operator[](size_t index) const
Gets the element at index.
Definition: CircularBuffer.h:67
catapult::utils::CircularBuffer::m_buffer
std::vector< T > m_buffer
Definition: CircularBuffer.h:84
catapult::utils::CircularBuffer::size
constexpr size_t size() const
Gets the size of the buffer.
Definition: CircularBuffer.h:51
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::CircularBuffer::push_back
void push_back(T &&element)
Appends element to the end of the buffer, possibly overwriting existing elements.
Definition: CircularBuffer.h:45
catapult::utils::CircularBuffer::capacity
constexpr size_t capacity() const
Gets the capacity of the buffer.
Definition: CircularBuffer.h:56
catapult::utils::CircularBuffer::CircularBuffer
CircularBuffer(size_t size)
Creates a circular buffer with the specified size.
Definition: CircularBuffer.h:32