2015-10-30 16:01:57 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2020-02-08 16:35:21 -05:00
|
|
|
/* Copyright (c) 2015-2020 FIRST. All Rights Reserved. */
|
2015-10-30 16:01:57 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2016-09-14 20:52:06 -07:00
|
|
|
#include <cstddef>
|
2016-05-20 17:30:37 -07:00
|
|
|
#include <vector>
|
2015-10-30 16:01:57 -07:00
|
|
|
|
2019-08-24 21:35:19 -07:00
|
|
|
namespace wpi {
|
2016-11-01 22:33:12 -07:00
|
|
|
|
2015-10-30 16:01:57 -07:00
|
|
|
/**
|
|
|
|
|
* This is a simple circular buffer so we don't need to "bucket brigade" copy
|
|
|
|
|
* old values.
|
|
|
|
|
*/
|
|
|
|
|
template <class T>
|
2017-11-22 17:04:57 -08:00
|
|
|
class circular_buffer {
|
2015-10-30 16:01:57 -07:00
|
|
|
public:
|
2017-11-22 17:04:57 -08:00
|
|
|
explicit circular_buffer(size_t size);
|
|
|
|
|
|
2018-09-26 00:09:25 -07:00
|
|
|
using value_type = T;
|
|
|
|
|
using reference = value_type&;
|
|
|
|
|
using const_reference = const value_type&;
|
|
|
|
|
using pointer = value_type*;
|
|
|
|
|
using size_type = size_t;
|
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
|
using difference_type = std::ptrdiff_t;
|
2017-11-22 17:04:57 -08:00
|
|
|
|
|
|
|
|
size_type size() const;
|
|
|
|
|
T& front();
|
|
|
|
|
const T& front() const;
|
|
|
|
|
T& back();
|
|
|
|
|
const T& back() const;
|
|
|
|
|
void push_front(T value);
|
|
|
|
|
void push_back(T value);
|
|
|
|
|
T pop_front();
|
|
|
|
|
T pop_back();
|
|
|
|
|
void resize(size_t size);
|
|
|
|
|
void reset();
|
2015-10-30 16:01:57 -07:00
|
|
|
|
|
|
|
|
T& operator[](size_t index);
|
|
|
|
|
const T& operator[](size_t index) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<T> m_data;
|
|
|
|
|
|
2020-02-08 16:35:21 -05:00
|
|
|
T zero_val{0};
|
|
|
|
|
|
2015-10-30 16:01:57 -07:00
|
|
|
// Index of element at front of buffer
|
|
|
|
|
size_t m_front = 0;
|
|
|
|
|
|
|
|
|
|
// Number of elements used in buffer
|
|
|
|
|
size_t m_length = 0;
|
|
|
|
|
|
|
|
|
|
size_t ModuloInc(size_t index);
|
|
|
|
|
size_t ModuloDec(size_t index);
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-24 21:35:19 -07:00
|
|
|
} // namespace wpi
|
2016-11-01 22:33:12 -07:00
|
|
|
|
2019-08-24 21:35:19 -07:00
|
|
|
#include "wpi/circular_buffer.inc"
|