1#ifndef CIRCULAR_BUFFER_H_
2#define CIRCULAR_BUFFER_H_
11 : max_length_(max_length),
26 return buffer_[cpos_];
30 return buffer_[(cpos_ + max_length_ - 1) % max_length_];
36 cpos_ = (cpos_ + 1) % max_length_;
38 if (size_ < max_length_) {
44 return buffer_[(cpos_ + i) % size_];
52 const size_t copy_to_array(T *
const buffer,
size_t buffer_size)
const {
53 size_t size = std::min(size_, buffer_size);
54 if (size_ < max_length_) {
55 std::copy(buffer_.begin(), buffer_.begin() +
size, buffer);
58 size_t len_to_end = max_length_ - cpos_;
59 if (
size < len_to_end) {
60 std::copy(buffer_.begin() + cpos_,
61 buffer_.begin() + cpos_ +
size, buffer);
64 std::copy(buffer_.begin() + cpos_,
65 buffer_.begin() + cpos_ + len_to_end,
67 std::copy(buffer_.begin(),
68 buffer_.begin() + (
size - len_to_end),
79 std::vector<T> buffer_;
Definition CircularBuffer.h:8
void clear()
Definition CircularBuffer.h:47
size_t max_size() const
Definition CircularBuffer.h:17
const T & last() const
Definition CircularBuffer.h:29
void push_back(const T &el)
Definition CircularBuffer.h:33
size_t size() const
Definition CircularBuffer.h:21
const size_t copy_to_array(T *const buffer, size_t buffer_size) const
Definition CircularBuffer.h:52
CircularBuffer(size_t max_length)
Definition CircularBuffer.h:10
const T & operator[](size_t i) const
Definition CircularBuffer.h:43
const T & first() const
Definition CircularBuffer.h:25