General Utility Library for C++14  2.11
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
gul14::SlidingBuffer< ElementT, fixed_capacity, Container > Class Template Reference

Detailed Description

template<typename ElementT, std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
class gul14::SlidingBuffer< ElementT, fixed_capacity, Container >

A circular data buffer of (semi-)fixed capacity to which elements can be added at the front or at the back.

A SlidingBuffer is a flat array containing zero to capacity() elements of an arbitrary type. The capacity can either be specified at compile time via a template parameter or at runtime via resize(). In the former case, the internal buffer is embedded in the object (std::array), in the latter case it is dynamically allocated (std::vector).

The SlidingBuffer shares many characteristics with traditional ring buffers: It has a fixed maximum size and new elements are added with push_front() or push_back(). If this is done when the buffer is filled() to capacity, an element automatically drops out at the other end of the sliding window:

SlidingBuffer<int, 2> buf; // Create a buffer with up to 2 entries
buf.push_back(1);
buf.push_back(2);
buf.push_back(3);
std::cout << buf[0] << ", " << buf[1] << "\n";
// prints "2, 3"
buf.push_front(1);
std::cout << buf[0] << ", " << buf[1] << "\n";
// prints "1, 2"

This buffer is not intended for producer-consumer problems. If the elements access is atomic it is thread-safe though. All auxiliary functions that mutate the data are not thread safe.

Differing from arrays this buffer will start empty (with a size of zero). Once the buffer has grown to its designated size (i.e. capacity), its size() only changes if clear(), pop_front(), or pop_back() are used.

A typical application would be to analyze an incoming stream of elements in a finite impulse response filter.

This container uses an accompanying iterator class called SlidingBufferIterator. See SlidingBufferExposed for a variant with a different (more performant) iterator interface.

Iterator invalidation is specified at SlidingBufferIterator.

Member types:
value_type Type of the elements
container_type Type of the underlying container (i.e. std::array<value_type, ..>)
size_type Unsigned integer type (usually std::size_t)
difference_type Signed integer type (usually std::ptrdiff_t)
iterator SlidingBufferIterator
const_iterator SlidingBufferIterator const
reverse_iterator SlidingBufferIterator
const_reverse_iterator SlidingBufferIterator const
Member functions:
SlidingBuffer Constructor
Element access:
push_back Insert an element at the back of the buffer
push_front Insert an element at the front of the buffer
operator[] Access element by index, unchecked
at Access element by index with bounds checking
front Access the foremost element (i.e. [0])
back Access the last element (i.e. [size() - 1])
Iterators:
begin, cbegin Return an iterator to the first element of the container
end, cend Return an iterator to the element following the last element of the container
rbegin, crbegin Return an iterator to the first element of the reversed container
rend, crend Return an iterator to the element following the last element of the reversed container
Capacity:
size Return number of used elements
capacity Return maximum number of elements
filled Check whether the buffer is completely filled
empty Check whether the buffer is empty
resize Change the maximum number of elements (only if fixed_size==0)
reserve Change the maximum number of elements (only if fixed_size==0)
Modifiers:
clear Empty the buffer
pop_back Drop the last element
pop_front Drop the foremost element
Non-member functions:
operator<< Dump the raw data of the buffer to an ostream
auto push_front(const value_type &in) -> void
Insert one element at the front of the buffer; if it is full, an element at the back is dropped to ma...
Definition: SlidingBuffer.h:344
typename Container::size_type size_type
Unsigned integer type (usually std::size_t)
Definition: SlidingBuffer.h:190
auto filled() const noexcept -> bool
Return true if the buffer is completely filled with elements.
Definition: SlidingBuffer.h:514
auto cbegin() const noexcept -> const_iterator
Return a read-only iterator to the first element of the container.
Definition: SlidingBuffer.h:901
auto size() const noexcept -> size_type
Return the number of elements in the container, i.e.
Definition: SlidingBuffer.h:483
std::reverse_iterator< const_iterator > const_reverse_iterator
Iterator to a const element in reversed container.
Definition: SlidingBuffer.h:208
auto cend() const noexcept -> const_iterator
Return a read-only iterator to the element following the last element of the container.
Definition: SlidingBuffer.h:923
auto at(const size_type idx) noexcept(false) -> reference
Access an element in the buffer by index with bounds checking.
Definition: SlidingBuffer.h:409
auto pop_front() -> void
Remove the first element from the buffer.
Definition: SlidingBuffer.h:287
auto reserve(size_type size, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
Resize the container (identical to resize()).
Definition: SlidingBuffer.h:566
SlidingBufferIterator< SlidingBuffer< ElementT, fixed_capacity, Container > const * > const_iterator
Iterator to a const element.
Definition: SlidingBuffer.h:204
auto crend() const noexcept -> const_reverse_iterator
Return a read-only iterator to the element following the last element of the reversed container.
Definition: SlidingBuffer.h:935
Container container_type
Type of the underlying container (e.g. std::array<value_type, ..>)
Definition: SlidingBuffer.h:186
typename Container::const_reference const_reference
Reference to a constant element.
Definition: SlidingBuffer.h:196
auto end() noexcept -> iterator
Return an iterator to the element following the last element of the container.
Definition: SlidingBuffer.h:873
auto rbegin() noexcept -> reverse_iterator
Return an iterator to the first element of the reversed container.
Definition: SlidingBuffer.h:862
typename Container::const_pointer const_pointer
Pointer to a constant element.
Definition: SlidingBuffer.h:200
SlidingBuffer()=default
Construct an empty sliding buffer.
typename Container::difference_type difference_type
Signed integer type (usually std::ptrdiff_t)
Definition: SlidingBuffer.h:192
std::reverse_iterator< iterator > reverse_iterator
Iterator to an element in reversed container.
Definition: SlidingBuffer.h:206
auto front() noexcept -> reference
Return the foremost element (the one with index 0).
Definition: SlidingBuffer.h:438
auto empty() const noexcept -> bool
Check if the buffer contains no elements, i.e.
Definition: SlidingBuffer.h:578
ElementT value_type
Type of the elements in the underlying container.
Definition: SlidingBuffer.h:188
auto clear() -> void
Empty the buffer.
Definition: SlidingBuffer.h:526
auto resize(size_type new_capacity, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
Resize the container.
Definition: SlidingBuffer.h:555
SlidingBufferIterator< SlidingBuffer< ElementT, fixed_capacity, Container > * > iterator
Iterator to an element.
Definition: SlidingBuffer.h:202
auto rend() noexcept -> reverse_iterator
Return an iterator to the element following the last element of the reversed container.
Definition: SlidingBuffer.h:891
constexpr auto capacity() const noexcept -> size_type
Return the maximum possible number of elements in the container.
Definition: SlidingBuffer.h:501
typename Container::reference reference
Reference to an element.
Definition: SlidingBuffer.h:194
auto pop_back() -> void
Remove the last element from the buffer.
Definition: SlidingBuffer.h:270
auto crbegin() const noexcept -> const_reverse_iterator
Return a read-only iterator to the first element of the reversed container.
Definition: SlidingBuffer.h:911
auto begin() noexcept -> iterator
Return an iterator to the first element of the container.
Definition: SlidingBuffer.h:846
typename Container::pointer pointer
Pointer to an element.
Definition: SlidingBuffer.h:198
auto back() noexcept -> reference
Return the backmost element (the one with the highest valid index).
Definition: SlidingBuffer.h:457
auto push_back(const value_type &in) -> void
Insert one element at the end of the buffer; if it is full, an element at the front is dropped to mak...
Definition: SlidingBuffer.h:306
auto maximum(ContainerT const &container, Accessor accessor=ElementAccessor< ElementT >()) -> DataT
Return the maximum element value in a container.
Definition: statistics.h:314

The sliding buffer can be instantiated in two slightly different versions:

ElementT must be default constructible.

Template Parameters
ElementTType of elements in the buffer
fixed_capacityMaximum number of elements in the buffer (capacity), zero if unspecified/dynamic
ContainerType of the underlying container, usually not specified

#include <SlidingBuffer.h>

Classes

class  SlidingBufferIterator
 Iterator of the SlidingBuffer container. More...
 

Public Types

using container_type = Container
 Type of the underlying container (e.g. std::array<value_type, ..>)
 
using value_type = ElementT
 Type of the elements in the underlying container.
 
using size_type = typename Container::size_type
 Unsigned integer type (usually std::size_t)
 
using difference_type = typename Container::difference_type
 Signed integer type (usually std::ptrdiff_t)
 
using reference = typename Container::reference
 Reference to an element.
 
using const_reference = typename Container::const_reference
 Reference to a constant element.
 
using pointer = typename Container::pointer
 Pointer to an element.
 
using const_pointer = typename Container::const_pointer
 Pointer to a constant element.
 
using iterator = SlidingBufferIterator< SlidingBuffer< ElementT, fixed_capacity, Container > * >
 Iterator to an element.
 
using const_iterator = SlidingBufferIterator< SlidingBuffer< ElementT, fixed_capacity, Container > const * >
 Iterator to a const element.
 
using reverse_iterator = std::reverse_iterator< iterator >
 Iterator to an element in reversed container.
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 Iterator to a const element in reversed container.
 

Public Member Functions

 SlidingBuffer ()=default
 Construct an empty sliding buffer. More...
 
 SlidingBuffer (SlidingBuffer const &)=default
 Default copy constructor.
 
 SlidingBuffer (SlidingBuffer &&) noexcept(std::is_nothrow_move_constructible< container_type >::value)=default
 Default move constructor.
 
SlidingBufferoperator= (SlidingBuffer const &)=default
 Default copy assignment operator.
 
SlidingBufferoperator= (SlidingBuffer &&) noexcept(std::is_nothrow_move_assignable< container_type >::value)=default
 Default move assignment operator.
 
virtual ~SlidingBuffer ()=default
 Default Destructor.
 
 SlidingBuffer (size_type count)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Only available for sliding buffers based on std::vector. More...
 
auto pop_back () -> void
 Remove the last element from the buffer. More...
 
auto pop_front () -> void
 Remove the first element from the buffer. More...
 
auto push_back (const value_type &in) -> void
 Insert one element at the end of the buffer; if it is full, an element at the front is dropped to make room. More...
 
auto push_back (value_type &&in) -> void
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto push_front (const value_type &in) -> void
 Insert one element at the front of the buffer; if it is full, an element at the back is dropped to make room. More...
 
auto push_front (value_type &&in) -> void
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto operator[] (size_type idx) noexcept -> reference
 Access an element in the buffer by index without bounds checking. More...
 
auto operator[] (size_type idx) const noexcept -> const_reference
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto at (const size_type idx) noexcept(false) -> reference
 Access an element in the buffer by index with bounds checking. More...
 
auto at (const size_type idx) const noexcept(false) -> const_reference
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto front () noexcept -> reference
 Return the foremost element (the one with index 0). More...
 
auto front () const noexcept -> const_reference
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto back () noexcept -> reference
 Return the backmost element (the one with the highest valid index). More...
 
auto back () const noexcept -> const_reference
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto size () const noexcept -> size_type
 Return the number of elements in the container, i.e. More...
 
constexpr auto capacity () const noexcept -> size_type
 Return the maximum possible number of elements in the container. More...
 
auto filled () const noexcept -> bool
 Return true if the buffer is completely filled with elements. More...
 
auto clear () -> void
 Empty the buffer. More...
 
auto resize (size_type new_capacity, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
 Resize the container. More...
 
auto reserve (size_type size, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
 Resize the container (identical to resize()). More...
 
auto empty () const noexcept -> bool
 Check if the buffer contains no elements, i.e. More...
 
auto friend operator<< (std::ostream &s, const SlidingBuffer< value_type, fixed_capacity, container_type > &buffer) -> std::ostream &
 Dump all buffer elements. More...
 
auto begin () noexcept -> iterator
 Return an iterator to the first element of the container. More...
 
auto begin () const noexcept -> const_iterator
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto rbegin () noexcept -> reverse_iterator
 Return an iterator to the first element of the reversed container. More...
 
auto end () noexcept -> iterator
 Return an iterator to the element following the last element of the container. More...
 
auto end () const noexcept -> const_iterator
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
auto rend () noexcept -> reverse_iterator
 Return an iterator to the element following the last element of the reversed container. More...
 
auto cbegin () const noexcept -> const_iterator
 Return a read-only iterator to the first element of the container. More...
 
auto crbegin () const noexcept -> const_reverse_iterator
 Return a read-only iterator to the first element of the reversed container. More...
 
auto cend () const noexcept -> const_iterator
 Return a read-only iterator to the element following the last element of the container. More...
 
auto crend () const noexcept -> const_reverse_iterator
 Return a read-only iterator to the element following the last element of the reversed container. More...
 

Protected Member Functions

auto change_capacity (size_type new_capacity, ShrinkBehavior shrink_behavior=ShrinkBehavior::keep_front_elements) -> void
 Change the underlying container's capacity. More...
 

Protected Attributes

size_type idx_begin_ { 0u }
 Index of the first SlidingBuffer element in the underlying container (the one retrieved by SlidingBuffer::front()).
 
size_type idx_end_ { 0u }
 Index pointing to the element in the underlying container that will be written to by the next call to push_back().
 
bool full_ { false }
 Indicates if the buffer is completely filled with elements.
 
Container storage_ { }
 Actual data is stored here, the underlying container.
 

Constructor & Destructor Documentation

◆ SlidingBuffer() [1/2]

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::SlidingBuffer ( )
default

Construct an empty sliding buffer.

The capacity of the buffer is given by the fixed_capacity template parameter. If that template argument is not zero, a std::array based SlidingBuffer with that (unchangeable) capacity is created.

If the template argument is zero, a SlidingBuffer based on std::vector with a capacity of zero elements is generated. Use the SlidingBuffer(size_type) constructor or call the resize() function afterwards to get a SlidingBuffer based on std::vector with nonzero capacity.

◆ SlidingBuffer() [2/2]

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::SlidingBuffer ( size_type  count)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Only available for sliding buffers based on std::vector.

Constructs a sliding buffer with a specified capacity.

For std::array based sliding buffers the capacity is specified by the fixed_capacity template parameter.

Member Function Documentation

◆ at()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::at ( const size_type  idx) -> reference
inlinenoexcept

Access an element in the buffer by index with bounds checking.

Index 0 is the foremost element, size() - 1 the backmost one. Access to elements beyond the last one causes an exception to be thrown.

Parameters
idxIndex of the element to return
Returns
a reference to the requested element.
Exceptions
std::out_of_rangeis thrown if idx >= size().

References gul14::cat().

◆ back()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::back ( ) -> reference
inlinenoexcept

Return the backmost element (the one with the highest valid index).

This call does not check if an element has ever been pushed into the buffer, so it might return a default-constructed element. In the case of a SlidingBuffer with zero capacity, calling back() results in undefined behavior.

◆ begin()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::begin ( ) -> iterator
inlinenoexcept

Return an iterator to the first element of the container.

If the container is empty, the returned iterator will be equal to end().

◆ capacity()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
constexpr auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::capacity ( ) const -> size_type
inlineconstexprnoexcept

Return the maximum possible number of elements in the container.

If the underlying type is std::vector, its size is returned, not its capacity, because only the active elements (i.e. size) participate in the sliding buffer.

◆ cbegin()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::cbegin ( ) const -> const_iterator
inlinenoexcept

Return a read-only iterator to the first element of the container.

If the container is empty, the returned iterator will be equal to cend().

◆ cend()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::cend ( ) const -> const_iterator
inlinenoexcept

Return a read-only iterator to the element following the last element of the container.

This element acts as a placeholder; attempting to access it results in undefined behavior.

◆ change_capacity()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::change_capacity ( size_type  new_capacity,
ShrinkBehavior  shrink_behavior = ShrinkBehavior::keep_front_elements 
) -> void
inlineprotected

Change the underlying container's capacity.

Only possible if the underlying container is a std::vector.

  • Shrinking: The excess elements are dropped according to shrink_behavior.
  • Growing: The capacity changes, but the (used) size does not. It will grow gradually when elements are pushed, as in the startup phase.
Parameters
new_capacityNew capacity (maximum size) of the sliding buffer.
shrink_behaviorSpecify the ShrinkBehavior.
See also
resize()

◆ clear()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::clear ( ) -> void
inline

Empty the buffer.

(Almost) all iterators will be invalidated. See SlidingBufferIterator.

Its size() will be zero afterwards.

◆ crbegin()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::crbegin ( ) const -> const_reverse_iterator
inlinenoexcept

Return a read-only iterator to the first element of the reversed container.

If the container is empty, the returned iterator will be equal to cend().

◆ crend()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::crend ( ) const -> const_reverse_iterator
inlinenoexcept

Return a read-only iterator to the element following the last element of the reversed container.

This element acts as a placeholder; attempting to access it results in undefined behavior.

◆ empty()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::empty ( ) const -> bool
inlinenoexcept

Check if the buffer contains no elements, i.e.

whether begin() == end().

Returns
true if the container is empty, false otherwise.

◆ end()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::end ( ) -> iterator
inlinenoexcept

Return an iterator to the element following the last element of the container.

This element acts as a placeholder; attempting to access it results in undefined behavior.

◆ filled()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::filled ( ) const -> bool
inlinenoexcept

Return true if the buffer is completely filled with elements.

If the buffer is used in filter contexts this means the filter is fully initialized and working.

If the buffer has zero capacity the value of filled() is false.

◆ front()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::front ( ) -> reference
inlinenoexcept

Return the foremost element (the one with index 0).

This call does not check if an element has ever been pushed into the buffer, so it might return a default-constructed element. In the case of a SlidingBuffer with zero capacity, calling front() results in undefined behavior.

◆ operator<<()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto friend gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::operator<< ( std::ostream &  s,
const SlidingBuffer< value_type, fixed_capacity, container_type > &  buffer 
) -> std::ostream&
inline

Dump all buffer elements.

Shown on the left is front(), on the right back().

Needs the elements to be dump-able to an ostream.

◆ operator[]()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::operator[] ( size_type  idx) -> reference
inlinenoexcept

Access an element in the buffer by index without bounds checking.

Index 0 is the foremost element, size() - 1 the backmost one. Access to elements outside the capacity is not allowed and results in undefined behavior. Access to elements inside the capacity is always allowed; a default-constructed element is returned if idx >= size().

Returns
a reference to the requested element.

◆ pop_back()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::pop_back ( ) -> void
inline

Remove the last element from the buffer.

The end() iterator and any iterators to the last element are invalidated.

Warning
Calling pop_back() on an empty SlidingBuffer results in undefined behavior.
Since
GUL version 2.4

◆ pop_front()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::pop_front ( ) -> void
inline

Remove the first element from the buffer.

The end() iterator and any iterators to the last element are invalidated. All other iterators can still be dereferenced, but point to elements that have been shifted.

Warning
Calling pop_front() on an empty SlidingBuffer results in undefined behavior.
Since
GUL version 2.4

◆ push_back()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::push_back ( const value_type in) -> void
inline

Insert one element at the end of the buffer; if it is full, an element at the front is dropped to make room.

At least some iterators can be invalidated. See SlidingBufferIterator.

Afterwards all iterators still point to the same logical element (i.e. element[n]).

Warning
Calling push_back() on a SlidingBuffer with zero capacity results in undefined behavior.
Since
GUL version 1.3

◆ push_front()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::push_front ( const value_type in) -> void
inline

Insert one element at the front of the buffer; if it is full, an element at the back is dropped to make room.

At least some iterators can be invalidated. See SlidingBufferIterator.

Afterwards all iterators still point to the same logical element, while the contents of all logical elements is shifted.

Warning
Calling push_front() on a SlidingBuffer with zero capacity results in undefined behavior.

◆ rbegin()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::rbegin ( ) -> reverse_iterator
inlinenoexcept

Return an iterator to the first element of the reversed container.

If the container is empty, the returned iterator will be equal to end().

◆ rend()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::rend ( ) -> reverse_iterator
inlinenoexcept

Return an iterator to the element following the last element of the reversed container.

This element acts as a placeholder; attempting to access it results in undefined behavior.

◆ reserve()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::reserve ( size_type  size,
ShrinkBehavior  shrink_behavior = ShrinkBehavior::keep_front_elements 
) -> void
inline

Resize the container (identical to resize()).

See also
resize()

◆ resize()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::resize ( size_type  new_capacity,
ShrinkBehavior  shrink_behavior = ShrinkBehavior::keep_front_elements 
) -> void
inline

Resize the container.

Only possible if the underlying container is a std::vector.

  • Shrinking: The excess elements are dropped according to shrink_behavior.
  • Growing: The capacity changes, but the (used) size does not. It will grow gradually when elements are pushed, as in the startup phase.
Parameters
new_capacityNew capacity (maximum size) of the sliding buffer.
shrink_behaviorSpecify the ShrinkBehavior.
Note
Think twice when shrinking the buffer: The default shrink_behavior (ShrinkBehavior::keep_front_elements) is transparent only if exclusively push_front() was used to add elements. If push_back() was used, resize() without a second argument discards the most recent elements; in these cases, make sure to specify ShrinkBehavior::keep_back_elements.

◆ size()

template<typename ElementT , std::size_t fixed_capacity = 0u, typename Container = typename std::conditional_t<(fixed_capacity >= 1u), std::array<ElementT, fixed_capacity>, std::vector<ElementT>>>
auto gul14::SlidingBuffer< ElementT, fixed_capacity, Container >::size ( ) const -> size_type
inlinenoexcept

Return the number of elements in the container, i.e.

std::distance(begin(), end()).

In the startup phase it can be 0 and up to the fixed_capacity or capacity(), after startup (filled() == true) it will always return fixed_capacity resp. capacity().


The documentation for this class was generated from the following file: