circular_buffer_space_optimized<T, Alloc> |
|
The circular_buffer_space_optimized container is an adaptor of the circular_buffer. The functionality of the
circular_buffer_space_optimized is similar to the base circular_buffer except it does
not allocate memory at once when created rather it allocates memory as needed. (The predictive memory allocation
is similar to typical std::vector implementation.) Moreover the memory is automatically freed as the
size of the container decreases.
|
|
| Figure: |
The memory allocation process of the space optimized circular buffer. The min_capacity() of the
capacity controller represents the minimal guaranteed amount of allocated memory. The
allocated memory will never drop under this value. The default value of the min_capacity() is
set to 0.
|
Note that some of the links point to the original circular_buffer
if the functionality is the same.
namespace boost {
template <class T, class Alloc>
class circular_buffer_space_optimized
{
public:
typedef typename Alloc::value_type value_type;
typedef typename Alloc::pointer pointer;
typedef typename Alloc::const_pointer const_pointer;
typedef typename Alloc::reference reference;
typedef typename Alloc::const_reference const_reference;
typedef typename Alloc::size_type size_type;
typedef typename Alloc::difference_type difference_type;
typedef Alloc allocator_type;
typedef implementation-defined const_iterator;
typedef implementation-defined iterator;
typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
typedef boost::reverse_iterator<iterator> reverse_iterator;
typedef std::pair<pointer, size_type> array_range;
typedef std::pair<const_pointer, size_type> const_array_range;
typedef implementation-defined capacity_type;
explicit circular_buffer_space_optimized(const allocator_type& alloc = allocator_type());
explicit circular_buffer_space_optimized(capacity_type capacity_ctrl, const allocator_type& alloc = allocator_type());
circular_buffer_space_optimized(capacity_type capacity_ctrl, const_reference item, const allocator_type& alloc = allocator_type());
circular_buffer_space_optimized(capacity_type capacity_ctrl, size_type n, const_reference item, const allocator_type& alloc = allocator_type());
circular_buffer_space_optimized(const circular_buffer_space_optimized<T, Alloc>& cb);
template <class InputIterator>
circular_buffer_space_optimized(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
template <class InputIterator>
circular_buffer_space_optimized(capacity_type capacity_ctrl, InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
~circular_buffer_space_optimized();
allocator_type get_allocator() const;
allocator_type& get_allocator();
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
reverse_iterator rbegin();
reverse_iterator rend();
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;
reference operator[](size_type index);
const_reference operator[](size_type index) const;
reference at(size_type index);
const_reference at(size_type index) const;
reference front();
reference back();
const_reference front() const;
const_reference back() const;
array_range array_one();
array_range array_two();
const_array_range array_one() const;
const_array_range array_two() const;
pointer linearize();
size_type size() const;
size_type max_size() const;
bool empty() const;
explicit circular_buffer(const allocator_type& alloc = allocator_type());
explicit circular_buffer(capacity_type capacity, const allocator_type& alloc = allocator_type());
circular_buffer(size_type n, const_reference item, const allocator_type& alloc = allocator_type());
circular_buffer(capacity_type capacity, size_type n, const_reference item, const allocator_type& alloc = allocator_type());
circular_buffer(const circular_buffer<T, Alloc>& cb);
template <class InputIterator>
circular_buffer(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
template <class InputIterator>
circular_buffer(capacity_type capacity, InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
bool full() const;
size_type reserve() const;
const capacity_type& capacity() const;
void set_capacity(const capacity_type& capacity_ctrl);
void resize(size_type new_size, const_reference item = value_type());
void rset_capacity(const capacity_type& capacity_ctrl);
void rresize(size_type new_size, const_reference item = value_type());
circular_buffer_space_optimized<T, Alloc>& operator=(const circular_buffer_space_optimized<T, Alloc>& cb);
void assign(size_type n, const_reference item);
void assign(capacity_type capacity_ctrl, size_type n, const_reference item);
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
template <class InputIterator>
void assign(capacity_type capacity_ctrl, InputIterator first, InputIterator last);
void swap(circular_buffer_space_optimized<T, Alloc>& cb);
void push_back(const_reference item = value_type());
void push_front(const_reference item = value_type());
void pop_back();
void pop_front();
iterator insert(iterator pos, const_reference item = value_type());
void insert(iterator pos, size_type n, const_reference item);
template <class InputIterator>
void insert(iterator pos, InputIterator first, InputIterator last);
iterator rinsert(iterator pos, const_reference item = value_type());
void rinsert(iterator pos, size_type n, const_reference item);
template <class InputIterator>
void rinsert(iterator pos, InputIterator first, InputIterator last);
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);
iterator rerase(iterator pos);
iterator rerase(iterator first, iterator last);
void clear();
};
template <class T, class Alloc>
bool operator==(const circular_buffer_space_optimized<T, Alloc>& lhs, const circular_buffer_space_optimized<T, Alloc>& rhs);
template <class T, class Alloc>
bool operator<(const circular_buffer_space_optimized<T, Alloc>& lhs, const circular_buffer_space_optimized<T, Alloc>& rhs);
template <class T, class Alloc>
bool operator!=(const circular_buffer_space_optimized<T, Alloc>& lhs, const circular_buffer_space_optimized<T, Alloc>& rhs);
template <class T, class Alloc>
bool operator>(const circular_buffer_space_optimized<T, Alloc>& lhs, const circular_buffer_space_optimized<T, Alloc>& rhs);
template <class T, class Alloc>
bool operator<=(const circular_buffer_space_optimized<T, Alloc>& lhs, const circular_buffer_space_optimized<T, Alloc>& rhs);
template <class T, class Alloc>
bool operator>=(const circular_buffer_space_optimized<T, Alloc>& lhs, const circular_buffer_space_optimized<T, Alloc>& rhs);
template <class T, class Alloc>
void swap(circular_buffer_space_optimized<T, Alloc>& lhs, circular_buffer_space_optimized<T, Alloc>& rhs);
} // namespace boost
|
The auto-resizing mode of the space optimized circular buffer can be useful in situations when the container can
possibly store large number of elements but most of its lifetime the container stores just few of them. The usage
of the circular_buffer_space_optimized will result in decreased memory consumption and can improve
the CPU cache effectiveness.
The circular_buffer_space_optimized is defined in the file boost/circular_buffer.hpp. There is also a forward declaration
for the circular_buffer_space_optimized in the header file boost/circular_buffer_fwd.hpp.
Random Access Container, Front Insertion Sequence and Back Insertion Sequence.
Following public types are specific to the circular_buffer_space_optimized. Description of the
public types common with the circular_buffer can be found in the circular_buffer's
documentation.
| Type | Description |
|---|---|
capacity_type
|
Capacity controller of the space optimized circular buffer.
class capacity_control {
size_type m_capacity;
size_type m_min_capacity;
public:
capacity_control(size_type capacity, size_type min_capacity = 0) : m_capacity(capacity), m_min_capacity(min_capacity) {};
size_type capacity() const { return m_capacity; }
size_type min_capacity() const { return m_min_capacity; }
operator size_type() const { return m_capacity; }
};
m_capacity represents the capacity of the
circular_buffer_space_optimized and the m_min_capacity determines the minimal
allocated size of its internal buffer. The converting constructor of the capacity_control
allows implicit conversion from size_type-like types which ensures compatibility of creating
an instance of the circular_buffer_space_optimized with other STL containers. On the other
hand the operator size_type() (returning m_capacity) provides implicit conversion
to the size_type which allows to treat the capacity of the
circular_buffer_space_optimized the same way as in the circular_buffer.
|
explicit
circular_buffer_space_optimized(const allocator_type&
alloc = allocator_type());Create an empty space optimized circular buffer with a maximum capacity.
|
explicit
circular_buffer_space_optimized(capacity_type
capacity_ctrl, const allocator_type&
alloc = allocator_type());Create an empty space optimized circular buffer with the specified capacity.
|
circular_buffer_space_optimized(capacity_type
capacity_ctrl, const_reference
item, const allocator_type&
alloc = allocator_type());Create a full space optimized circular buffer with the specified capacity filled with capacity_ctrl.capacity() copies of item.
|
circular_buffer_space_optimized(capacity_type
capacity_ctrl, size_type n,
const_reference
item, const allocator_type&
alloc = allocator_type());Create a space optimized circular buffer with the specified capacity filled with n copies of
item.
|
circular_buffer_space_optimized(const
circular_buffer_space_optimized<T,Alloc>& cb);The copy constructor.
Creates a copy of the specified
|
template
<class InputIterator>Create a full space optimized circular buffer filled with a copy of the range.
|
template
<class InputIterator>Create a space optimized circular buffer with the specified capacity (and the minimal guaranteed amount of allocated memory) filled with a copy of the range.
|
~circular_buffer_space_optimized();The destructor.
Destroys the
|
Following public member functions of the circular_buffer_space_optimized have different behaviour
from the base circular_buffer. Description of the public member functions with the same behaviour
can be found in the circular_buffer's documentation.
bool
full() const;Is the circular_buffer_space_optimized full?
|
size_type
reserve() const;Get the maximum number of elements which can be inserted into the circular_buffer_space_optimized without overwriting any of already stored elements.
|
const
capacity_type&
capacity() const;Get the capacity of the circular_buffer_space_optimized.
|
void
set_capacity(const capacity_type&
capacity_ctrl);Change the capacity (and the minimal guaranteed amount of allocated memory) of the circular_buffer_space_optimized.
|
void
resize(size_type
new_size, const_reference
item = value_type());Change the size of the circular_buffer_space_optimized.
|
void
rset_capacity(const capacity_type&
capacity_ctrl);Change the capacity (and the minimal guaranteed amount of allocated memory) of the circular_buffer_space_optimized.
|
void
rresize(size_type
new_size, const_reference
item = value_type());Change the size of the circular_buffer_space_optimized.
|
circular_buffer_space_optimized<T,Alloc>&
operator=(const circular_buffer_space_optimized<T,Alloc>& cb);The assign operator.
Makes this
|
void
assign(size_type n,
const_reference
item);Assign n items into the space optimized circular buffer.
The content of the
|
void
assign(capacity_type
capacity_ctrl, size_type n,
const_reference
item);Assign n items into the space optimized circular buffer specifying the capacity.
The capacity of the
|
template
<class InputIterator>Assign a copy of the range into the space optimized circular buffer.
The content of the
|
template
<class InputIterator>Assign a copy of the range into the space optimized circular buffer specifying the capacity.
The capacity of the
|
void
swap(circular_buffer_space_optimized<T,Alloc>& cb);Swap the contents of two space optimized circular buffers.
|
void
push_back(const_reference
item = value_type());Insert a new element at the end of the space optimized circular buffer.
|
void
push_front(const_reference
item = value_type());Insert a new element at the beginning of the space optimized circular buffer.
|
void
pop_back();Remove the last element from the space optimized circular buffer.
|
void
pop_front();Remove the first element from the space optimized circular buffer.
|
iterator
insert(iterator pos,
const_reference
item = value_type());Insert an element at the specified position.
|
void
insert(iterator pos,
size_type n,
const_reference
item);Insert n copies of the item at the specified position.
|
template
<class InputIterator>Insert the range [first, last) at the specified position.
|
iterator
rinsert(iterator pos,
const_reference
item = value_type());Insert an element before the specified position.
|
void
rinsert(iterator pos,
size_type n,
const_reference
item);Insert n copies of the item before the specified position.
|
template
<class InputIterator>Insert the range [first, last) before the specified position.
|
iterator
erase(iterator
pos);Remove an element at the specified position.
|
iterator
erase(iterator first,
iterator
last);Erase the range [first, last).
|
iterator
rerase(iterator
pos);Remove an element at the specified position.
|
iterator
rerase(iterator first,
iterator
last);Erase the range [first, last).
|
void
clear();Remove all stored elements from the space optimized circular buffer.
|
boost::circular_buffer, std::vector
The idea of the space optimized circular buffer has been introduced by Pavel Vozenilek.
|
Copyright © 2003-2007 Jan Gaspar
Use, modification, and distribution is subject to the Boost Software License, Version 1.0. |
|