WPILibC++  2019.1.1-beta-4-15-g8ac4b11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
circular_buffer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 FIRST. 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 circular_buffer(size_t size);
23 
24  using value_type = T;
25  using reference = value_type&;
26  using const_reference = const value_type&;
27  using pointer = value_type*;
28  using size_type = size_t;
29  using iterator_category = std::forward_iterator_tag;
30  using difference_type = std::ptrdiff_t;
31 
32  size_type size() const;
33  T& front();
34  const T& front() const;
35  T& back();
36  const T& back() const;
37  void push_front(T value);
38  void push_back(T value);
39  T pop_front();
40  T pop_back();
41  void resize(size_t size);
42  void reset();
43 
44  T& operator[](size_t index);
45  const T& operator[](size_t index) const;
46 
47  private:
48  std::vector<T> m_data;
49 
50  // Index of element at front of buffer
51  size_t m_front = 0;
52 
53  // Number of elements used in buffer
54  size_t m_length = 0;
55 
56  size_t ModuloInc(size_t index);
57  size_t ModuloDec(size_t index);
58 };
59 
60 } // namespace frc
61 
62 #include "frc/circular_buffer.inc"
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
This is a simple circular buffer so we don't need to "bucket brigade" copy old values.
Definition: circular_buffer.h:20
T & operator[](size_t index)
Definition: circular_buffer.inc:203
void push_front(T value)
Push new value onto front of the buffer.
Definition: circular_buffer.inc:72
T pop_back()
Pop value at back of buffer.
Definition: circular_buffer.inc:126
T & front()
Returns value at front of buffer.
Definition: circular_buffer.inc:29
size_type size() const
Returns number of elements in buffer.
Definition: circular_buffer.inc:21
T & back()
Returns value at back of buffer.
Definition: circular_buffer.inc:45
T pop_front()
Pop value at front of buffer.
Definition: circular_buffer.inc:110
void reset()
Sets internal buffer contents to zero.
Definition: circular_buffer.inc:193
void push_back(T value)
Push new value onto back of the buffer.
Definition: circular_buffer.inc:91
void resize(size_t size)
Resizes internal buffer to given size.
Definition: circular_buffer.inc:140