Templated Circular Buffer Container
circular_buffer<T, Alloc>
|
|
Contents
Description
Simple Example
Rationale
Synopsis
Header Files
Modeled Concepts
Template Parameters
Public Types
Constructors and Destructor
Public Member Functions
Standalone Functions
Semantics
Caveats
Debug Support
Example
Notes
See also
Acknowledgments
In general the term circular buffer refers to an area in memory which is
used to store incoming data. When the buffer is filled, new data is written
starting at the beginning of the buffer and overwriting the old. [1] (Also see the Figure.)
The circular_buffer is a STL compliant container. It is a kind of
sequence that, like std::vector or std::deque,
supports random access iterators. In addition, it supports constant time insert
and erase operations at the beginning or the end of the buffer. The
circular_buffer is specially designed to provide fixed capacity
storage. When its capacity is exhausted, newly inserted elements will cause
elements either at the beginning or end of the buffer (depending on what insert
operation is used) to be overwritten.
There are also several container adaptors which are bundled with the
circular_buffer:
TODO
The circular_buffer only allocates memory when created, when the
capacity is adjusted explicitly, or as necessary to accommodate a resizing or
assign operation. (There is also a circular_buffer_space_optimized
available. It is an adaptor of the circular_buffer which
does not allocate memory at once when created rather it allocates memory as
needed.
A brief example using the
circular_buffer
:
#include <boost/circular_buffer.hpp>
int main(int argc, char* argv[]) {
// Create a circular buffer with a capacity for 3 integers.
boost::circular_buffer<int> cb(3);
cb.push_back(1); // Insert the first element.
cb.push_back(2); // Insert the second element.
cb.push_back(3); // Insert the third element.
// The buffer is full now, pushing subsequent
// elements will overwrite the front-most elements.
cb.push_back(4); // Overwrite 1 with 4.
cb.push_back(5); // Overwrite 2 with 5.
// The buffer now contains 3, 4 and 5.
int a = cb[0]; // a == 3
int b = cb[1]; // b == 4
int c = cb[2]; // c == 5
// Elements can be popped from either the front or back.
cb.pop_back(); // 5 is removed.
cb.pop_front(); // 3 is removed.
int d = cb[0]; // d == 4
return 0;
}
TODO motivation
The most likely usage of the circular_buffer is as a storage of
the most recently received samples, overwriting the oldest as new samples
arrive.
A contiguous region of memory utilized as a circular buffer has several unique
and useful characteristics:
-
Fixed memory use and no implicit or unexpected memory allocation.
-
Fast constant-time insertion and removal of elements from the front and back.
-
Fast constant-time random access of elements.
-
Suitability for real-time and performance critical applications.
The
circular_buffer
container provides a similar interface to
std::vector
,
std::deque
and
std::list
including
push
,
pop
,
insert
,
erase
, iterators and
compatibility with
std
algorithms.
The design of the
circular_buffer
container is guided by the
following principles:
- Maximum
efficiency
for envisaged applications.
- Suitable for
general purpose
use.
-
Interoperable
with other
std
containers and algorithms.
- The behaviour of the buffer as
intuitive
as possible.
- Suitable for
specialization
by means of adaptors. (The
circular_buffer_space_optimized
is such an example of the adaptor.)
- Guarantee of (at least)
basic exception safety
.
Like all STL containers, also the circular_buffer is
not thread-safe.
E.g. if one thread writes to a
std::vector
and another reads from
it, the threads have to be synchronized. The same applies to threads
accessing the
circular_buffer
.
Writing to a Full Buffer
There are several options how to cope with the case if a data source produces
more data than can fit in the buffer [link to wikipedia]:
-
If possible, tell the data source to wait until there is room in the buffer.
-
If the latest data is the most important, write over the oldest data that has
not been read, and move the read pointer to the position of the oldest
remaining data.
-
If the oldest data is the most important, ignore new data from the source until
there is room again in the buffer.
Reading from an Empty Buffer
TODO
Iterator Invalidation
According to the STL container specification [link to container SGI STL] the
circular_buffer
provides several types of iterator. An iterator is usually considered to be
invalidated if an element, the iterator pointed to, had been removed or
overwritten by another element. The source documentation refers to this
definition of iterator invalidation and this definition is also enforced by the
Debug Support [link]. However, some applications utilizing
circular_buffer
may require less strict definition: an iterator is invalidated only if it
points to an uninitialized memory. Consider following example:
#include <boost/circular_buffer.hpp>
#include <assert.h>
int main(int argc, char* argv[]) {
boost::circular_buffer<int> cb(3);
cb.push_back(1);
cb.push_back(2);
cb.push_back(3);
boost::circular_buffer<int>::iterator it = cb.begin();
assert(*it == 1);
cb.push_back(4); // The iterator is still valid.
assert(*it == 4);
return 0;
}
TODO This example works only if the Debug Support is disabled otherwise the
code will produce a runtime error. [link how to disable debug support]
Overwrite Operation
There was a discussion what exactly "overwriting of an element" means. It can be
either a destruction of the original element and a consequent inplace
construction of a new element or it can be an assignment of a new element into
an old one.
Producer-Consumer Mode
It it misleading to claim that the
circular_buffer
can be used as a
bounded buffer
in a producer-consumer mode. Similarly to the
circular_buffer
the bounded buffer has a fixed size, but it was primarily designed to be used
in the producer-consumer mode when one thread is writing to the buffer and
another is reading from it, which implies the bounded buffer is thread-safe. As
discussed in the
Thread-Safety
section the
circular_buffer
is not thread-safe which prevents the
circular_buffer
to be used
as a bounded buffer or at least to be used directly. It is of course possible
to write a wrapper or an adaptor which will then synchronize the read/write
access to the
circular_buffer
.
Moreover the
circular_buffer
was designed to overwrite old elements
with new ones when its capacity is exhauted. Although this behaviour is very
useful it represents only one option how a bounded buffer can behave. E.g. when
a bounded buffer is full and one thread tries to push an element into it the
buffer suspends the thread until some other thread performs a pop operation.
Another example is that it can throw an overflow exception when the buffer is
full or an underflow exception when it is empty. Considering writing an adaptor
which would then serve as a bounded buffer, the
circular_buffer
does
not have to be always the best choice. The
circular_buffer
bears
an overhead for its "circular" behavior, so containers like
std::vector
or
std::deque
suit better if you do not require the bounded buffer
to be circular.
namespace boost {
template <class T, class Alloc>
class circular_buffer
{
public:
typedef Alloc allocator_type;
typedef implementation-defined array_range;
typedef implementation-defined const_array_range;
typedef implementation-defined const_iterator;
typedef typename Alloc::const_pointer const_pointer;
typedef typename Alloc::const_reference const_reference;
typedef implementation-defined const_reverse_iterator;
typedef typename Alloc::difference_type difference_type;
typedef implementation-defined iterator;
typedef typename Alloc::pointer pointer;
typedef typename Alloc::reference reference;
typedef implementation-defined reverse_iterator;
typedef typename Alloc::size_type size_type;
typedef typename Alloc::value_type value_type;
template <class InputIterator>
circular_buffer(size_type capacity,
InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
template <class InputIterator>
circular_buffer(InputIterator first,
InputIterator last, const allocator_type& alloc = allocator_type());
template <class InputIterator>
circular_buffer(size_type capacity, InputIterator first, InputIterator last);
template <class InputIterator>
circular_buffer(InputIterator first, InputIterator last);
circular_buffer(const circular_buffer<T,Alloc>& cb);
circular_buffer(size_type capacity,
size_type n, value_type item,
const allocator_type& alloc = allocator_type());
circular_buffer(size_type n,
value_type item, const allocator_type& alloc = allocator_type());
explicit circular_buffer(size_type capacity, const allocator_type& alloc = allocator_type());
explicit circular_buffer(const allocator_type& alloc = allocator_type());
~circular_buffer();
const_array_range array_one() const;
array_range array_one();
const_array_range array_two() const;
array_range array_two();
template <class InputIterator>
void assign(size_type capacity, InputIterator first, InputIterator last);
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
void assign(size_type capacity, size_type n, value_type item);
void assign(size_type n, value_type item);
value_type at(size_type index) const;
reference at(size_type index);
value_type back() const;
reference back();
const_iterator begin() const;
iterator begin();
size_type capacity() const;
void clear();
bool empty() const;
const_iterator end() const;
iterator end();
iterator erase(iterator first, iterator last);
iterator erase(iterator pos);
value_type front() const;
reference front();
bool full() const;
allocator_type& get_allocator();
allocator_type get_allocator() const;
template <class InputIterator>
void insert(iterator pos, InputIterator first, InputIterator last);
void insert(iterator pos, size_type n, value_type item);
iterator insert(iterator pos, value_type item = value_type());
pointer linearize();
size_type max_size() const;
circular_buffer<T,Alloc>& operator=(const circular_buffer<T,Alloc>& cb);
value_type operator[](size_type index) const;
reference operator[](size_type index);
void pop_back();
void pop_front();
void push_back(value_type item = value_type());
void push_front(value_type item = value_type());
const_reverse_iterator rbegin() const;
reverse_iterator rbegin();
const_reverse_iterator rend() const;
reverse_iterator rend();
iterator rerase(iterator first, iterator last);
iterator rerase(iterator pos);
void resize(size_type new_size, value_type item = value_type());
template <class InputIterator>
void rinsert(iterator pos, InputIterator first, InputIterator last);
void rinsert(iterator pos, size_type n, value_type item);
iterator rinsert(iterator pos, value_type item = value_type());
void rresize(size_type new_size, value_type item = value_type());
void rset_capacity(size_type new_capacity);
void set_capacity(size_type new_capacity);
size_type size() const;
void swap(circular_buffer<T,Alloc>& cb);
};
template <class T, class Alloc>
bool operator!=(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator<(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator<=(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator==(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator>(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
template <class T, class Alloc>
bool operator>=(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
template <class T, class Alloc>
void swap(circular_buffer<T,Alloc>& lhs, circular_buffer<T,Alloc>& rhs);
} // namespace boost
|
The
circular_buffer
is defined in the file
boost/circular_buffer.hpp
. There is also a forward
declaration for the
circular_buffer
in the header file
boost/circular_buffer_fwd.hpp
.
Random AccessContainer,
Front Insertion Sequence,
Back Insertion Sequence,
Assignable
(SGI specific),
Equality Comparable,
LessThan Comparable
(SGI specific)
| Parameter |
Description |
Default |
T
|
The type of the elements stored in the circular buffer. |
|
Alloc
|
The allocator type used for all internal memory management. |
std::allocator<T>
|
template <class InputIterator>
circular_buffer(size_type capacity,
InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
| |
|
Create a circular buffer with a copy of a range. |
| Precondition: |
Valid range [first, last). |
| Postcondition: |
(*this).capacity() == capacity
If the number of items to copy from the range [first, last) is greater than the specified capacity then only elements from the range [last - capacity, last) will be copied. |
|
|
template <class InputIterator>
circular_buffer(InputIterator first,
InputIterator last, const allocator_type& alloc = allocator_type());
|
template <class InputIterator>
circular_buffer(size_type capacity, InputIterator first, InputIterator last);
|
template <class InputIterator>
circular_buffer(InputIterator first, InputIterator last);
|
circular_buffer(const circular_buffer<T,Alloc>& cb);
| |
| Postcondition: |
*this == cb |
|
|
circular_buffer(size_type capacity,
size_type n, value_type item,
const allocator_type& alloc = allocator_type());
|
circular_buffer(size_type n,
value_type item, const allocator_type& alloc = allocator_type());
| |
Create a full circular buffer with a given capacity and filled with copies of item. |
| Postcondition: |
capacity() == n && size() == n && (*this)[0] == (*this)[1] == ... == (*this)[n - 1] == item |
|
|
explicit circular_buffer(size_type capacity, const allocator_type& alloc = allocator_type());
| |
|
Create an empty circular buffer with a given capacity. |
| Postcondition: |
(*this).capacity() == capacity && (*this).size == 0 |
|
|
explicit circular_buffer(const allocator_type& alloc = allocator_type());
|
~circular_buffer();
|
const_array_range array_one() const;
|
array_range array_one();
|
const_array_range array_two() const;
|
array_range array_two();
|
template <class InputIterator>
void assign(size_type capacity, InputIterator first, InputIterator last);
|
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
| |
| Precondition: |
Valid range [first, last). |
| Postcondition: |
(*this).capacity() == std::distance(first, last) && (*this).size() == std::distance(first, last) |
|
|
void assign(size_type capacity, size_type n, value_type item);
|
void assign(size_type n, value_type item);
| |
Assign n items into the circular buffer. |
| Postcondition: |
(*this).capacity() == n && (*this).size() == n && (*this)[0] == (*this)[1] == ... == (*this).back() == item |
|
|
value_type at(size_type index) const;
| |
Return the element at the index position. |
|
|
reference at(size_type index);
| |
Return the element at the index position. |
|
|
value_type back() const;
| |
|
Return the last (rightmost) element. |
|
|
reference back();
| |
|
Return the last (rightmost) element. |
|
|
const_iterator begin() const;
| |
|
Return a const iterator pointing to the beginning of the circular buffer. |
|
|
iterator begin();
| |
|
Return an iterator pointing to the beginning of the circular buffer. |
|
|
size_type capacity() const;
| |
|
Return the capacity of the circular buffer. |
|
|
void clear();
| |
|
Erase all stored elements. |
| Postcondition: |
(*this).size() == 0 |
|
|
bool empty() const;
| |
|
Is the circular buffer empty? |
| Returns: |
true if there are no elements stored in the circular buffer. false otherwise. |
|
|
const_iterator end() const;
| |
|
Return a const iterator pointing to the end of the circular buffer. |
|
|
iterator end();
| |
|
Return an iterator pointing to the end of the circular buffer. |
|
|
iterator erase(iterator first, iterator last);
| |
Erase the range [first, last). |
| Precondition: |
Valid range [first, last). size_type old_size = (*this).size() |
| Postcondition: |
(*this).size() == old_size - std::distance(first, last)
Removes the elements from the range [first, last). |
| Returns: |
iterator to the first element remaining beyond the removed element or (*this).end() if no such element exists. |
|
|
iterator erase(iterator pos);
| |
|
Erase the element at the given position. |
| Precondition: |
Valid pos iterator. size_type old_size = (*this).size() |
| Postcondition: |
(*this).size() == old_size - 1
Removes an element at the position pos. |
| Returns: |
iterator to the first element remaining beyond the removed element or (*this).end() if no such element exists. |
|
|
value_type front() const;
| |
|
Return the first (leftmost) element. |
|
|
reference front();
| |
|
Return the first (leftmost) element. |
|
|
bool full() const;
| |
|
Is the circular buffer full? |
| Returns: |
true if the number of elements stored in the circular buffer equals the capacity of the circular buffer. false otherwise. |
|
|
allocator_type& get_allocator();
| |
| Note: |
This method was added in order to optimize obtaining of the allocator with a state, although use of stateful allocators in STL is discouraged. |
|
|
allocator_type get_allocator() const;
|
template <class InputIterator>
void insert(iterator pos, InputIterator first, InputIterator last);
| |
Insert the range [first, last) before the given position. |
| Precondition: |
Valid pos iterator and valid range [first, last). |
| Postcondition: |
This operation preserves the capacity of the circular buffer. If the insertion would result in exceeding the capacity of the circular buffer then the necessary number of elements from the beginning (left) of the circular buffer will be removed or not the whole range will be inserted or both. In case the whole range cannot be inserted it will be inserted just some elements from the end (right) of the range (see the example).
Example:
array to insert: int array[] = { 5, 6, 7, 8, 9 };
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4
position ---------------------^
insert(position, array, array + 5);
(If the operation won't preserve capacity, the buffer would look like this |1|2|5|6|7|8|9|3|4|)
RESULTING circular buffer |6|7|8|9|3|4| - capacity: 6, size: 6 |
|
|
void insert(iterator pos, size_type n, value_type item);
| |
Insert n copies of the item before the given position. |
| Precondition: |
Valid pos iterator. |
| Postcondition: |
This operation preserves the capacity of the circular buffer. If the insertion would result in exceeding the capacity of the circular buffer then the necessary number of elements from the beginning (left) of the circular buffer will be removed or not all n elements will be inserted or both.
Example:
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4
position ---------------------^
insert(position, (size_t)5, 6);
(If the operation won't preserve capacity, the buffer would look like this |1|2|6|6|6|6|6|3|4|)
RESULTING circular buffer |6|6|6|6|3|4| - capacity: 6, size: 6 |
|
|
iterator insert(iterator pos, value_type item = value_type());
| |
Insert the item before the given position. |
| Precondition: |
Valid pos iterator. |
| Postcondition: |
The item will be inserted at the position pos.
If the circular buffer is full, the first (leftmost) element will be removed. |
| Returns: |
iterator to the inserted element. |
|
|
pointer linearize();
| |
|
TODO doc - Return pointer to data stored in the circular buffer as a continuous array of values. |
| This method can be useful e.g. when passing the stored data into the legacy C API.
|
| Postcondition: |
&(*this)[0] < &(*this)[1] < ... < &(*this).back() |
|
|
size_type max_size() const;
| |
|
Return the largest possible size (or capacity) of the circular buffer. |
|
|
circular_buffer<T,Alloc>& operator=(const circular_buffer<T,Alloc>& cb);
| |
| Postcondition: |
*this == cb |
|
|
value_type operator[](size_type index) const;
| |
Return the element at the index position. |
| Precondition: |
*(this).size() > index |
|
|
reference operator[](size_type index);
| |
Return the element at the index position. |
| Precondition: |
*(this).size() > index |
|
|
void pop_back();
| |
|
Remove the last (rightmost) element. |
| Precondition: |
!*(this).empty() iterator it = ((*this).end() - 1) |
| Postcondition: |
((*this).end() - 1) != it |
|
|
void pop_front();
| |
|
Remove the first (leftmost) element. |
| Postcondition: |
(*this).begin() != it |
|
|
void push_back(value_type item = value_type());
| |
|
Insert a new element at the end. |
| Postcondition: |
(*this).back() == item
If the circular buffer is full, the first (leftmost) element will be removed. |
|
|
void push_front(value_type item = value_type());
| |
|
Insert a new element at the start. |
| Postcondition: |
(*this).front() == item
If the circular buffer is full, the last (rightmost) element will be removed. |
|
|
const_reverse_iterator rbegin() const;
| |
|
Return a const reverse iterator pointing to the beginning of the reversed circular buffer. |
|
|
reverse_iterator rbegin();
| |
|
Return a reverse iterator pointing to the beginning of the reversed circular buffer. |
|
|
const_reverse_iterator rend() const;
| |
|
Return a const reverse iterator pointing to the end of the reversed circular buffer. |
|
|
reverse_iterator rend();
| |
|
Return a reverse iterator pointing to the end of the reversed circular buffer. |
|
|
iterator rerase(iterator first, iterator last);
| |
Erase the range [first, last). |
| Precondition: |
Valid range [first, last). size_type old_size = (*this).size() |
| Postcondition: |
(*this).size() == old_size - std::distance(first, last)
Removes the elements from the range [first, last). |
| Returns: |
iterator to the first element remaining in front of the removed element or (*this).begin() if no such element exists. |
|
|
iterator rerase(iterator pos);
| |
|
Erase the element at the given position. |
| Precondition: |
Valid pos iterator. size_type old_size = (*this).size() |
| Postcondition: |
(*this).size() == old_size - 1
Removes an element at the position pos. |
| Returns: |
iterator to the first element remaining in front of the removed element or (*this).begin() if no such element exists. |
|
|
void resize(size_type new_size, value_type item = value_type());
| |
|
Change the size of the circular buffer. |
| Postcondition: |
(*this).size() == new_size
If the new size is greater than the current size, the rest of the circular buffer is filled with copies of item. In case the resulting size exceeds the current capacity the capacity is set to new_size. If the new size is lower than the current size then ((*this).size() - new_size) elements will be removed according to the remove_front parameter. |
|
|
template <class InputIterator>
void rinsert(iterator pos, InputIterator first, InputIterator last);
| |
Insert the range [first, last) before the given position. |
| Precondition: |
Valid pos iterator and valid range [first, last). |
| Postcondition: |
This operation preserves the capacity of the circular buffer. If the insertion would result in exceeding the capacity of the circular buffer then the necessary number of elements from the end (right) of the circular buffer will be removed or not the whole range will be inserted or both. In case the whole range cannot be inserted it will be inserted just some elements from the beginning (left) of the range (see the example).
Example:
array to insert: int array[] = { 5, 6, 7, 8, 9 };
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4
position ---------------------^
insert(position, array, array + 5);
(If the operation won't preserve capacity, the buffer would look like this |1|2|5|6|7|8|9|3|4|)
RESULTING circular buffer |1|2|5|6|7|8| - capacity: 6, size: 6 |
|
|
void rinsert(iterator pos, size_type n, value_type item);
| |
Insert n copies of the item before the given position. |
| Precondition: |
Valid pos iterator. |
| Postcondition: |
This operation preserves the capacity of the circular buffer. If the insertion would result in exceeding the capacity of the circular buffer then the necessary number of elements from the end (right) of the circular buffer will be removed or not all n elements will be inserted or both.
Example:
original circular buffer |1|2|3|4| | | - capacity: 6, size: 4
position ---------------------^
insert(position, (size_t)5, 6);
(If the operation won't preserve capacity, the buffer would look like this |1|2|6|6|6|6|6|3|4|)
RESULTING circular buffer |1|2|6|6|6|6| - capacity: 6, size: 6 |
|
|
iterator rinsert(iterator pos, value_type item = value_type());
| |
Insert an item before the given position. |
| Precondition: |
Valid pos iterator. |
| Postcondition: |
The item will be inserted before the position pos.
If the circular buffer is full, the last element (rightmost) will be removed. |
| Returns: |
iterator to the inserted element. |
|
|
void rresize(size_type new_size, value_type item = value_type());
|
void rset_capacity(size_type new_capacity);
|
void set_capacity(size_type new_capacity);
| |
|
Change the capacity of the circular buffer. |
| Postcondition: |
(*this).capacity() == new_capacity
If the current number of elements stored in the circular buffer is greater than the desired new capacity then ((*this).size() - new_capacity) elements will be removed according to the remove_front parameter. |
|
|
size_type size() const;
| |
|
Return the number of elements currently stored in the circular buffer. |
|
|
void swap(circular_buffer<T,Alloc>& cb);
| |
|
Swap the contents of two circular buffers. |
| Postcondition: |
this contains elements of cb and vice versa. |
|
|
template <class T, class Alloc>
bool operator!=(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
| |
|
Test two circular buffers for non-equality. |
|
|
template <class T, class Alloc>
bool operator<(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
| |
|
Lexicographical comparison. |
|
|
template <class T, class Alloc>
bool operator<=(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
| |
|
Lexicographical comparison. |
|
|
template <class T, class Alloc>
bool operator==(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
| |
|
Test two circular buffers for equality. |
|
|
template <class T, class Alloc>
bool operator>(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
| |
|
Lexicographical comparison. |
|
|
template <class T, class Alloc>
bool operator>=(const circular_buffer<T,Alloc>& lhs, const circular_buffer<T,Alloc>& rhs);
| |
|
Lexicographical comparison. |
|
|
template <class T, class Alloc>
void swap(circular_buffer<T,Alloc>& lhs, circular_buffer<T,Alloc>& rhs);
| |
|
Swap the contents of two circular buffers. |
|
|
TODO remove this section
The behaviour of insertion for
circular_buffer
is as follows:
- The capacity of a
circular_buffer
remains fixed unless adjusted
via
set_capacity
or
resize
.
-
insert
will overwrite front elements as necessary.
-
rinsert
will overwrite back elements as necessary.
The behaviour of resizing a
circular_buffer
is as follows:
- The capacity will be adjusted to accommodate a
resize
. (The
capacity can be only increased, not decreased.)
The behaviour of assigning to a
circular_buffer
is as follows:
- The capacity will be adjusted to accommodate an
assign
. (The
capacity can be only increased, not decreased.)
The rules for iterator (and result of
data()
)
invalidation for
circular_buffer
are as follows:
-
insert
at the end of the
circular_buffer
(including
push_back
) does not invalidate any iterator except the case the
iterator points to the overwritten element.
-
rinsert
at the beginning of the
circular_buffer
(including
push_front
) does not invalidate any iterator except the case
the iterator points to the overwritten element.
-
insert
in the middle of the
circular_buffer
invalidates iterators pointing to the elements at the insertion point and
behind the insertion point. It also invalidates iterators pointing to the
overwritten element(s).
-
rinsert
in the middle of the
circular_buffer
invalidates iterators pointing to the elements before the insertion point and
iterators pointing to the overwritten element(s).
-
erase
at the end of the
circular_buffer
(including
pop_back
)
invalidates only iterators pointing to the erased element(s).
-
pop_front
invalidates only iterators pointing to the erased element.
-
erase
at the beginning or in the middle of the
circular_buffer
invalidates iterators pointing to the erased element(s) and iterators pointing
to the elements behind the erase point.
-
data
,
set_capacity
,
resize
,
operator=
,
assign
,
swap
and
clear
invalidate all
iterators pointing to the
circular_buffer
.
In addition to the preceding rules the iterators get also invalidated due to
overwriting (e.g. iterator pointing to the front-most element gets invalidated
when inserting into the full
circular_buffer
). They get
invalidated in that sense they do not point to the same element as before but
they do still point to the same
valid
place in the memory. If you want
to rely on this feature you have to turn of the
Debug Support
otherwise an assertion will report an error if such invalidated iterator is
used.
The
circular_buffer
should not be used for storing pointers to
dynamically allocated objects. When a
circular_buffer
becomes
full, further insertion will overwrite the stored pointers - resulting in a
memoryleak. One recommend alternative is the use of smart pointers
[2].
(Any container of
std::auto_ptr
is considered particularly
hazardous.
[3]
)
Elements inserted near the front of a full
circular_buffer
can be
lost. According to the
semantics
of
insert
,
insertion overwrites front-most items as necessary - possibly including
elements currently being
inserted at the front
of the buffer.
Conversely,
push_front
to a full
circular_buffer
is
guaranteed to overwrite the back-most element.
Elements inserted near the back of a full
circular_buffer
can be
lost. According to the
semantics
of
rinsert
,
insertion overwrites front-most items as necessary - possibly including
elements currently being
inserted at the back
of the buffer. Conversely,
push_back
to a full
circular_buffer
is guaranteed to
overwrite the front-most element.
While internals of a
circular_buffer
are circular, iterators are
not.
Iterators of a
circular_buffer
are only valid for the range
[begin(),
end()]
. E.g. iterators
(begin() - 1)
and
(end() + 1)
are invalid.
In order to help a programmer to avoid and find common bugs, the
circular_buffer
contains a kind of debug support.
The
circular_buffer
maintains a list of valid iterators. As soon as
any element gets destroyed all iterators pointing to this element are removed
from this list and explicitly invalidated (an invalidation flag is set). The
debug support also consists of many assertions (
BOOST_ASSERT
macros) which ensure the
circular_buffer
and its iterators are
used in the correct manner at runtime. In case an invalid iterator is used the
assertion will report an error. The connection of explicit iterator
invalidation and assertions makes a very robust debug technique which catches
most of the errors.
Moreover, the uninitialized memory allocated by
circular_buffer
is
filled with the value
0xcc
in the debug mode. This can help the
programmer when debugging the code to recognize the initialized memory from the
uninitialized. For details refer the
source code.
The debug support is enabled only in the debug mode (when the
NDEBUG
is not defined). It can also be explicitly disabled by defining
BOOST_CB_DISABLE_DEBUG
macro.
The following example includes various usage of the
circular_buffer
.
#include <boost/circular_buffer.hpp>
#include <numeric>
#include <assert.h>
int main(int argc, char* argv[])
{
// create a circular buffer of capacity 3
boost::circular_buffer<int> cb(3);
// insert some elements into the circular buffer
cb.push_back(1);
cb.push_back(2);
// assertions
assert(cb[0] == 1);
assert(cb[1] == 2);
assert(!cb.full());
assert(cb.size() == 2);
assert(cb.capacity() == 3);
// insert some other elements
cb.push_back(3);
cb.push_back(4);
// evaluate the sum
int sum = std::accumulate(cb.begin(), cb.end(), 0);
// assertions
assert(cb[0] == 2);
assert(cb[1] == 3);
assert(cb[2] == 4);
assert(sum == 9);
assert(cb.full());
assert(cb.size() == 3);
assert(cb.capacity() == 3);
return 0;
}
The
circular_buffer
has a capacity of three
int
.
Therefore, the size of the buffer will not exceed three. The
accumulate
algorithm evaluates the sum of the stored
elements. The semantics of the
circular_buffer
can be inferred
from the assertions.
[1]
A detailed description can be found at Wikipedia.
[2]
A good implementation of smart pointers is included in
Boost.
[3]
Never create a circular buffer of
std::auto_ptr
.
Refer to
Scott Meyers
' excellent book
Effective
STL
for a detailed discussion. (Meyers S.,
Effective STL: 50 SpecificWays to Improve Your Use of the Standard Template Library.
Addison-Wesley, 2001.)
boost::circular_buffer_space_optimized,
std::vector,
std::list,
std::deque
The
circular_buffer
has a short history. Its first version was a
std::deque
adaptor. This container was not very effective because of many reallocations
when inserting/removing an element. Thomas Wenish did a review of this version
and motivated me to create a circular buffer which allocates memory at once
when created.
The second version adapted
std::vector
but it has been abandoned
soon because of limited control over iterator invalidation.
The current version is a full-fledged STL compliant container. Pavel Vozenilek
did a thorough review of this version and came with many good ideas and
improvements. Also, I would like to thank Howard Hinnant, Nigel Stewart and
everyone who participated at the formal review for valuable comments and ideas.