3 #ifndef MADARA_KNOWLEDGE_CIRCULAR_BUFFER_H_
4 #define MADARA_KNOWLEDGE_CIRCULAR_BUFFER_H_
15 #include <type_traits>
16 #include "madara/MadaraExport.h"
59 for (
const T& cur : other)
79 *
this = std::move(tmp);
98 operator delete(
data_);
211 return &(*buf_)[
idx_];
361 return &(*buf_)[
idx_];
496 return {*
this,
back_};
508 return {*
this,
back_};
520 return {*
this,
back_};
604 std::stringstream ss;
605 ss <<
"CircularBuffer::" << func <<
": index " << i
606 <<
" is before oldest index " <<
front_;
607 throw std::out_of_range(ss.str());
612 std::stringstream ss;
613 ss <<
"CircularBuffer::" << func <<
": index " << i
614 <<
" passes newest index " <<
back_;
615 throw std::out_of_range(ss.str());
634 const T&
at(
size_t i)
const
658 T ret = std::move(
front());
681 T ret = std::move(
back());
692 template<
typename... Args>
700 T& ret = *
new (&(
back())) T(std::forward<Args>(args)...);
Const input iterator for elements of a CircularBuffer. Usual semantics.
const_iterator & operator--()
const_iterator operator+(size_t n) const
std::random_access_iterator_tag iterator_category
const_iterator operator++(int)
bool operator>=(const const_iterator &other) const
bool operator>(const const_iterator &other) const
bool operator==(const const_iterator &other) const
bool operator<=(const const_iterator &other) const
const_iterator & operator+=(size_t n)
friend const_iterator operator+(size_t n, const const_iterator &me)
const_iterator & operator++()
size_t index_actual() const
Actual index within buffer this iterator refers to.
reference operator[](size_t i) const
reference operator*() const
const_iterator & operator-=(size_t n)
bool operator!=(const const_iterator &other) const
const CircularBuffer * buf_
reference at(size_t i) const
size_t index() const
Index this iterator refers to.
pointer operator->() const
size_t operator-(const const_iterator &other) const
const_iterator operator--(int)
const_iterator operator-(size_t n) const
const_iterator(const CircularBuffer &buf, size_t idx)
bool operator<(const const_iterator &other) const
Mutable input iterator for elements of a CircularBuffer. Usual semantics.
iterator operator+(size_t n) const
pointer operator->() const
std::random_access_iterator_tag iterator_category
bool operator==(const iterator &other) const
iterator & operator-=(size_t n)
size_t operator-(const iterator &other) const
iterator(CircularBuffer &buf, size_t idx)
bool operator>=(const iterator &other) const
bool operator!=(const iterator &other) const
bool operator<(const iterator &other) const
iterator operator-(size_t n) const
reference operator[](size_t i) const
reference operator*() const
iterator & operator+=(size_t n)
reference at(size_t i) const
size_t index_actual() const
Actual index within buffer this iterator refers to.
bool operator<=(const iterator &other) const
size_t index() const
Index this iterator refers to.
friend iterator operator+(size_t n, const iterator &me)
bool operator>(const iterator &other) const
General purpose circular buffer container.
void clear()
Empty the buffer, destructing all elements.
T get_front()
Get a copy of the front element, or a default constructed value if the buffer is empty.
void reserve(size_t size)
Change the capacity of this CircularBuffer.
CircularBuffer(const CircularBuffer &other)
Copy constructor.
~CircularBuffer() noexcept
Destructor.
void throw_out_of_range(const char *func, size_t i) const
iterator end()
Create a mutable iterator to the back of the buffer.
T & push_back(T &&val)
Move the value val to the back of the buffer.
bool empty() const
Is this CircularBuffer empty?
void discard_front()
Remove the front element and discard it.
const_iterator cbegin() const
Create a const iterator to the back of the buffer.
T pop_front()
Remove the front element and return it.
size_t size() const
Number of elements this buffer currently holds, up to capacity.
size_t front_index() const
Index of front element.
const T & front() const
Get a const reference to the front element. Undefined behavior if empty.
T & front()
Get a reference to the front element. Undefined behavior if empty.
size_t actual(size_t i) const
Convert an actual index, to the modulused actual index within buffer.
const T & back() const
Get a const reference to the back element. Undefined behavior if empty.
size_t front_actual() const
Actual index of front within the buffer.
T pop_back()
Remove the back element and return it.
size_t back_actual() const
Actual index of back within the buffer.
size_t capacity() const
Number of elements this buffer can hold without dropping any.
void swap(CircularBuffer &other) noexcept
Swaps contents with another CircularBuffer, without copying elements.
CircularBuffer(size_t capacity, size_t initial_index=0)
Initialize with given capacity.
CircularBuffer & operator=(const CircularBuffer &other)
Copy assignment operator.
const_iterator cend() const
Create a const iterator to the back of the buffer.
T & back()
Get a reference to the back element. Undefined behavior if empty.
const T & operator[](size_t i) const
Get a const reference to the ith element.
T & at(size_t i)
Get a reference to ith element.
const_iterator begin() const
Create a const iterator to the front of the buffer.
T get_back()
Get a copy of the front element, or a default constructed value if the buffer is empty.
CircularBuffer(CircularBuffer &&other) noexcept
Move constructor.
void discard_back()
Remove the back element and discard it.
T & emplace_back(Args &&... args)
Construct a new element in-place in the back of the buffer.
T & push_back(const T &val)
Copy the value val to the back of the buffer.
T & operator[](size_t i)
Get a reference to the ith element.
CircularBuffer()=default
Construct with zero capacity.
ssize_t check_range(size_t i) const
Compares given index relative to existing range of elements (front_index() to back_index())
const_iterator end() const
Create a const iterator to the back of the buffer.
iterator begin()
Create a mutable iterator to the front of the buffer.
size_t back_index() const
Index of back element.
const T & at(size_t i) const
Get a const reference to ith element.
CircularBuffer & operator=(CircularBuffer &&other) noexcept
Move assignment operator.
Provides utility functions and classes for common tasks and needs.
Copyright(c) 2020 Galois.