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
represents the minimal guaranteed amount of allocated memory. The allocated memory will never drop under this
value. By default 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 reverse_iterator<const_iterator> const_reverse_iterator;
typedef 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_control;
explicit circular_buffer_space_optimized(const allocator_type& alloc = allocator_type());
explicit circular_buffer_space_optimized(capacity_control capacity_ctrl, const allocator_type& alloc = allocator_type());
circular_buffer_space_optimized(capacity_control capacity_ctrl, const_reference item, const allocator_type& alloc = allocator_type());
circular_buffer_space_optimized(capacity_control 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_control 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;
bool full() const;
size_type min_capacity() const;
void set_min_capacity(size_type new_min_capacity);
size_type capacity() const;
void set_capacity(size_type new_capacity);
void resize(size_type new_size, const_reference item = value_type());
void rset_capacity(size_type new_capacity);
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_control capacity_ctrl, size_type n, const_reference item);
template <class InputIterator>
void assign(InputIterator first, InputIterator last);
template <class InputIterator>
void assign(capacity_control 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 AccessContainer, Front Insertion Sequence and Back Insertion Sequence.
Template parameters, members and friend functions of the circular_buffer_space_optimized are almost
the same as for the base circular_buffer . Refer the circular_buffer documentation and also its source code
documentation for a detailed description.
| Type | Description |
|---|---|
capacity_control
|
Capacity controller of the space optimized circular buffer.
struct capacity_control {
capacity_control(size_type capacity, size_type min_capacity = 0) m_capacity(capacity), m_min_capacity(min_capacity) {};
size_type m_capacity;
size_type m_min_capacity;
};
m_capacity denotes 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.
|
The specific methods of the circular_buffer_space_optimized are listed below.
explicit
circular_buffer_space_optimized(const allocator_type&
alloc = allocator_type());Create an empty space optimized circular buffer with a maximum capacity. TODO - doc |
explicit
circular_buffer_space_optimized(capacity_control
capacity_ctrl, const allocator_type&
alloc = allocator_type());Create an empty space optimized circular buffer with a given capacity.
|
circular_buffer_space_optimized(capacity_control
capacity_ctrl, const_reference
item, const allocator_type&
alloc = allocator_type());Create a full space optimized circular buffer filled with copies of item.
|
circular_buffer_space_optimized(capacity_control
capacity_ctrl, size_type n,
const_reference
item, const allocator_type&
alloc = allocator_type());TODO doc. |
circular_buffer_space_optimized(const
circular_buffer_space_optimized<T,Alloc>& cb);TODO doc. |
template
<class InputIterator>TODO doc. |
template
<class InputIterator>Create a space optimized circular buffer with a copy of a range.
|
~circular_buffer_space_optimized();TODO doc. |
bool
full() const;See the circular_buffer source documentation. |
size_type
min_capacity() const;Return the minimal guaranteed amount of allocated memory. The allocated memory will never drop under this value. |
void
set_min_capacity(size_type
new_min_capacity);Change the minimal guaranteed amount of allocated memory.
|
size_type
capacity() const;See the circular_buffer source documentation. |
void
set_capacity(size_type
new_capacity);See the circular_buffer source documentation.
|
void
resize(size_type
new_size, const_reference
item = value_type());See the circular_buffer source documentation. |
void
rset_capacity(size_type
new_capacity);See the circular_buffer source documentation.
|
void
rresize(size_type
new_size, const_reference
item = value_type());See the circular_buffer source documentation. |
circular_buffer_space_optimized<T,Alloc>&
operator=(const circular_buffer_space_optimized<T,Alloc>& cb);TODO doc. |
void
assign(size_type n,
const_reference
item);See the circular_buffer source documentation. |
void
assign(capacity_control
capacity_ctrl, size_type n,
const_reference
item);See the circular_buffer source documentation. |
template
<class InputIterator>See the circular_buffer source documentation. |
template
<class InputIterator>See the circular_buffer source documentation. |
void
swap(circular_buffer_space_optimized<T,Alloc>& cb);See the circular_buffer source documentation. |
void
push_back(const_reference
item = value_type());See the circular_buffer source documentation.
|
void
push_front(const_reference
item = value_type());See the circular_buffer source documentation.
|
void
pop_back();See the circular_buffer source documentation.
|
void
pop_front();See the circular_buffer source documentation.
|
iterator
insert(iterator pos,
const_reference
item = value_type());See the circular_buffer source documentation.
|
void
insert(iterator pos,
size_type n,
const_reference
item);See the circular_buffer source documentation.
|
template
<class InputIterator>See the circular_buffer source documentation.
|
iterator
rinsert(iterator pos,
const_reference
item = value_type());See the circular_buffer source documentation.
|
void
rinsert(iterator pos,
size_type n,
const_reference
item);See the circular_buffer source documentation.
|
template
<class InputIterator>See the circular_buffer source documentation.
|
iterator
erase(iterator
pos);See the circular_buffer source documentation.
|
iterator
erase(iterator first,
iterator
last);See the circular_buffer source documentation.
|
iterator
rerase(iterator
pos);See the circular_buffer source documentation.
|
iterator
rerase(iterator first,
iterator
last);See the circular_buffer source documentation.
|
void
clear();See the circular_buffer source documentation. |
TODO remove this section
The behaviour of memory auto-resizing is as follows:
circular_buffer_space_optimized is created.
circular_buffer_space_optimized container is created the allocated memory
reflects the size of the container.
push_back , push_front , insert and rinsert will
predictively increase the allocated memory if necessary. (The predictive memory allocation is similar to
std::vector .)
set_capacity , resize , assign , insert (range or n items),
rinsert (range or n items), erase (range) and clear will accommodate the
allocated memory as necessary.
pop_back , pop_front , erase and clear will predictively
decrease the allocated memory.
The semantics of the circular_buffer_space_optimized then follows the semantics of the base circular_buffer except the invalidation
rules.
The rule for iterator invalidation for
circular_buffer_space_optimized is as follows:
data , set_capacity , resize , operator= ,
assign , swap , push_back , push_front ,
pop_back , pop_front , insert , rinsert ,
erase and clear invalidate all iterators pointing to the
circular_buffer_space_optimized .
boost::circular_buffer, std::vector
The idea of the space optimized circular buffer has been introduced by Pavel Vozenilek.
|
Copyright © 2003-2006 Jan Gaspar
Use, modification, and distribution is subject to the Boost Software License, Version 1.0. |
|