blob: d2739923d6398e927f4bdc9cd655fd414eadaaef [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
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
13namespace frc {
14
15/**
16 * This is a simple circular buffer so we don't need to "bucket brigade" copy
17 * old values.
18 */
19template <class T>
20class CircularBuffer {
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"