WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
CircularBuffer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015-2017. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <cstddef>
11 #include <vector>
12 
13 namespace frc {
14 
19 template <class T>
21  public:
22  explicit CircularBuffer(size_t size);
23 
24  void PushFront(T value);
25  void PushBack(T value);
26  T PopFront();
27  T PopBack();
28  void Resize(size_t size);
29  void Reset();
30 
31  T& operator[](size_t index);
32  const T& operator[](size_t index) const;
33 
34  private:
35  std::vector<T> m_data;
36 
37  // Index of element at front of buffer
38  size_t m_front = 0;
39 
40  // Number of elements used in buffer
41  size_t m_length = 0;
42 
43  size_t ModuloInc(size_t index);
44  size_t ModuloDec(size_t index);
45 };
46 
47 } // namespace frc
48 
49 #include "CircularBuffer.inc"
T & operator[](size_t index)
Definition: CircularBuffer.inc:153
This is a simple circular buffer so we don't need to "bucket brigade" copy old values.
Definition: CircularBuffer.h:20
T PopBack()
Pop value at back of buffer.
Definition: CircularBuffer.inc:76
void PushBack(T value)
Push new value onto back of the buffer.
Definition: CircularBuffer.inc:41
T PopFront()
Pop value at front of buffer.
Definition: CircularBuffer.inc:60
void PushFront(T value)
Push new value onto front of the buffer.
Definition: CircularBuffer.inc:22
void Reset()
Sets internal buffer contents to zero.
Definition: CircularBuffer.inc:143
void Resize(size_t size)
Resizes internal buffer to given size.
Definition: CircularBuffer.inc:90