blob: e4c773954e2177096f00bb57be6aa962a7c75eea [file] [log] [blame]
Brian Silverman1a675112016-02-20 20:42:49 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2015-2016. 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 <vector>
11#include <cstddef>
12
13/**
14 * This is a simple circular buffer so we don't need to "bucket brigade" copy
15 * old values.
16 */
17template <class T>
18class CircularBuffer {
19 public:
20 CircularBuffer(size_t size);
21
22 void PushFront(T value);
23 void PushBack(T value);
24 T PopFront();
25 T PopBack();
26 void Reset();
27
28 T& operator[](size_t index);
29 const T& operator[](size_t index) const;
30
31 private:
32 std::vector<T> m_data;
33
34 // Index of element at front of buffer
35 size_t m_front = 0;
36
37 // Number of elements used in buffer
38 size_t m_length = 0;
39
40 size_t ModuloInc(size_t index);
41 size_t ModuloDec(size_t index);
42};
43
44#include "CircularBuffer.inc"