// Implementation of the circular buffer adaptor.
// Copyright (c) 2003-2007 Jan Gaspar
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(BOOST_CIRCULAR_BUFFER_SPACE_OPTIMIZED_HPP)
#define BOOST_CIRCULAR_BUFFER_SPACE_OPTIMIZED_HPP
#if defined(_MSC_VER) && _MSC_VER >= 1200
#pragma once
#endif
#include
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; }
};
capacity >= min_capacity
The capacity()
represents the capacity of the circular_buffer_space_optimized
and
the 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()
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
.
circular_buffer_space_optimized
full?
/*!
\return true
if the number of elements stored in the circular_buffer_space_optimized
equals the capacity of the circular_buffer_space_optimized
; false
otherwise.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the circular_buffer_space_optimized
).
\sa empty()
*/
bool full() const { return m_capacity_ctrl == size(); }
/*! \brief Get the maximum number of elements which can be inserted into the
circular_buffer_space_optimized
without overwriting any of already stored elements.
\return capacity().%capacity() - size()
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the circular_buffer_space_optimized
).
\sa capacity()
, size()
, max_size()
*/
size_type reserve() const { return m_capacity_ctrl - size(); }
//! Get the capacity of the circular_buffer_space_optimized
.
/*!
\return The capacity controller representing the maximum number of elements which can be stored in the
circular_buffer_space_optimized
and the minimal allocated size of the internal buffer.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Does not invalidate any iterators.
\par Complexity
Constant (in the size of the circular_buffer_space_optimized
).
\sa reserve()
, size()
, max_size()
,
set_capacity(const capacity_type&)
*/
const capacity_type& capacity() const { return m_capacity_ctrl; }
#if defined(BOOST_CB_TEST)
// Return the current capacity of the adapted circular buffer.
/*
\note This method is not intended to be used directly by the user.
It is defined only for testing purposes.
*/
size_type internal_capacity() const { return circular_buffercircular_buffer_space_optimized
.
\post capacity() == capacity_ctrl \&\& size() \<= capacity_ctrl.capacity()
circular_buffer_space_optimized
is greater
than the desired new capacity then number of [size() - capacity_ctrl.capacity()]
last
elements will be removed and the new size will be equal to capacity_ctrl.capacity()
.circular_buffer_space_optimized
is lower
than the new capacity then the amount of allocated memory in the internal buffer may be accommodated as
necessary but it will never drop below capacity_ctrl.min_capacity()
.
\param capacity_ctrl The new capacity controller.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Strong.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in min[size(), capacity_ctrl.%capacity()]
).
\note To explicitly clear the extra allocated memory use the shrink-to-fit technique:boost::%circular_buffer_space_optimized\ cb(1000);
...
boost::%circular_buffer_space_optimized\(cb).swap(cb);
rset_capacity(const capacity_type&)
,
\link resize() resize(size_type, const_reference)\endlink
*/
void set_capacity(const capacity_type& capacity_ctrl) {
m_capacity_ctrl = capacity_ctrl;
if (capacity_ctrl < size()) {
iterator e = end();
circular_buffercircular_buffer_space_optimized
.
/*!
\post size() == new_size \&\& capacity().%capacity() >= new_size
item
will be inserted at the
back of the of the circular_buffer_space_optimized
in order to achieve the desired
size. In the case the resulting size exceeds the current capacity the capacity will be set to
new_size
.circular_buffer_space_optimized
is greater
than the desired new size then number of [size() - new_size]
last elements will be
removed. (The capacity will remain unchanged.)circular_buffer_space_optimized
will be filled with in order to gain
the requested size. (See the Effect.)
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the new size of the circular_buffer_space_optimized
).
\sa \link rresize() rresize(size_type, const_reference)\endlink
,
set_capacity(const capacity_type&)
*/
void resize(size_type new_size, param_value_type item = value_type()) {
if (new_size > size()) {
if (new_size > m_capacity_ctrl)
m_capacity_ctrl = capacity_type(new_size, m_capacity_ctrl.min_capacity());
insert(end(), new_size - size(), item);
} else {
iterator e = end();
erase(e - (size() - new_size), e);
}
}
/*! \brief Change the capacity (and the minimal guaranteed amount of allocated memory) of the
circular_buffer_space_optimized
.
\post capacity() == capacity_ctrl \&\& size() \<= capacity_ctrl
circular_buffer_space_optimized
is greater
than the desired new capacity then number of [size() - capacity_ctrl.capacity()]
first elements will be removed and the new size will be equal to
capacity_ctrl.capacity()
.circular_buffer_space_optimized
is lower
than the new capacity then the amount of allocated memory in the internal buffer may be accommodated as
necessary but it will never drop below capacity_ctrl.min_capacity()
.
\param capacity_ctrl The new capacity controller.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Strong.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in min[size(), capacity_ctrl.%capacity()]
).
\sa set_capacity(const capacity_type&)
,
\link rresize() rresize(size_type, const_reference)\endlink
*/
void rset_capacity(const capacity_type& capacity_ctrl) {
m_capacity_ctrl = capacity_ctrl;
if (capacity_ctrl < size()) {
iterator b = begin();
circular_buffercircular_buffer_space_optimized
.
/*!
\post size() == new_size \&\& capacity().%capacity() >= new_size
item
will be inserted at the
front of the of the circular_buffer_space_optimized
in order to achieve the desired
size. In the case the resulting size exceeds the current capacity the capacity will be set to
new_size
.circular_buffer_space_optimized
is greater
than the desired new size then number of [size() - new_size]
first elements will be
removed. (The capacity will remain unchanged.)circular_buffer_space_optimized
will be filled with in order to gain
the requested size. (See the Effect.)
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the new size of the circular_buffer_space_optimized
).
\sa \link resize() resize(size_type, const_reference)\endlink
,
rset_capacity(const capacity_type&)
*/
void rresize(size_type new_size, param_value_type item = value_type()) {
if (new_size > size()) {
if (new_size > m_capacity_ctrl)
m_capacity_ctrl = capacity_type(new_size, m_capacity_ctrl.min_capacity());
rinsert(begin(), new_size - size(), item);
} else {
rerase(begin(), end() - new_size);
}
}
//! Create an empty space optimized circular buffer with a maximum capacity.
/*!
\post capacity().%capacity() == max_size() \&\& capacity().min_capacity() == 0 \&\& size() == 0
capacity() == capacity_ctrl \&\& size() == 0
capacity_ctrl.min_capacity()
.
\param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in
the circular_buffer_space_optimized
and the minimal allocated size of the
internal buffer.
\param alloc The allocator.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\par Complexity
Constant.
*/
explicit circular_buffer_space_optimized(capacity_type capacity_ctrl,
const allocator_type& alloc = allocator_type())
: circular_buffercapacity_ctrl.%capacity()
copies of item
.
\post capacity() == capacity_ctrl \&\& full() \&\& (*this)[0] == item \&\& (*this)[1] == item \&\& ...
\&\& (*this) [capacity_ctrl.%capacity() - 1] == item
capacity_ctrl.capacity()
.
\param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in
the circular_buffer_space_optimized
and the minimal allocated size of the
internal buffer.
\param item The element the created circular_buffer_space_optimized
will be filled with.
\param alloc The allocator.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Complexity
Linear (in the capacity_ctrl.%capacity()
).
*/
circular_buffer_space_optimized(capacity_type capacity_ctrl, param_value_type item,
const allocator_type& alloc = allocator_type())
: circular_buffern
copies
of item
.
\pre capacity_ctrl.%capacity() >= n
\post capacity() == capacity_ctrl \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item
\&\& ... \&\& (*this)[n - 1] == item
max[n, capacity_ctrl.min_capacity()]
.
\param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in
the circular_buffer_space_optimized
and the minimal allocated size of the
internal buffer.
\param n The number of elements the created circular_buffer_space_optimized
will be filled with.
\param item The element the created circular_buffer_space_optimized
will be filled with.
\param alloc The allocator.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Complexity
Linear (in the n
).
*/
circular_buffer_space_optimized(capacity_type capacity_ctrl, size_type n, param_value_type item,
const allocator_type& alloc = allocator_type())
: circular_buffercircular_buffer_space_optimized
.
\post *this == cb
cb.size()
.
\param cb The circular_buffer_space_optimized
to be copied.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Complexity
Linear (in the size of cb
).
*/
circular_buffer_space_optimized(const circular_buffer_space_optimized[first, last)
.first
and last
have to meet the requirements of
InputIterator.
\post capacity().%capacity() == std::distance(first, last) \&\& capacity().min_capacity() == 0 \&\&
full() \&\& (*this)[0]== *first \&\& (*this)[1] == *(first + 1) \&\& ... \&\&
(*this)[std::distance(first, last) - 1] == *(last - 1)
std::distance(first, last)
.
\param first The beginning of the range to be copied.
\param last The end of the range to be copied.
\param alloc The allocator.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Complexity
Linear (in the std::distance(first, last)
).
*/
template [first, last)
.first
and last
have to meet the requirements of
InputIterator.
\post capacity() == capacity_ctrl \&\& size() \<= std::distance(first, last) \&\& (*this)[0]==
*(last - capacity_ctrl.%capacity()) \&\& (*this)[1] == *(last - capacity_ctrl.%capacity() + 1) \&\& ...
\&\& (*this)[capacity_ctrl.%capacity() - 1] == *(last - 1)
[first, last)
is greater than the
specified capacity_ctrl.%capacity()
then only elements from the range
[last - capacity_ctrl.%capacity(), last)
will be copied.max[capacity_ctrl.min_capacity(),
min[capacity_ctrl.%capacity(), std::distance(first, last)]]
.
\param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in
the circular_buffer_space_optimized
and the minimal allocated size of the
internal buffer.
\param first The beginning of the range to be copied.
\param last The end of the range to be copied.
\param alloc The allocator.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Complexity
Linear (in std::distance(first, last)
; in
min[capacity_ctrl.%capacity(), std::distance(first, last)]
if the InputIterator
is a RandomAccessIterator).
*/
template circular_buffer_space_optimized
.
\throws Nothing.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(including
iterators equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa clear()
*/
~circular_buffer_space_optimized();
#endif // #if defined(BOOST_CB_NEVER_DEFINED)
//! The assign operator.
/*!
Makes this circular_buffer_space_optimized
to become a copy of the specified
circular_buffer_space_optimized
.
\post *this == cb
cb.size()
.
\param cb The circular_buffer_space_optimized
to be copied.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Strong.
\par Iterator Invalidation
Invalidates all iterators pointing to this circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of cb
).
\sa \link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink
,
\link assign(capacity_type, size_type, param_value_type)
assign(capacity_type, size_type, const_reference)\endlink
,
assign(InputIterator, InputIterator)
,
assign(capacity_type, InputIterator, InputIterator)
*/
circular_buffer_space_optimizedn
items into the space optimized circular buffer.
/*!
The content of the circular_buffer_space_optimized
will be removed and replaced with
n
copies of the item
.
\post capacity().%capacity() == n \&\& capacity().min_capacity() == 0 \&\& size() == n \&\& (*this)[0] ==
item \&\& (*this)[1] == item \&\& ... \&\& (*this) [n - 1] == item
n
.
\param n The number of elements the circular_buffer_space_optimized
will be filled with.
\param item The element the circular_buffer_space_optimized
will be filled with.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the n
).
\sa operator=
, \link assign(capacity_type, size_type, param_value_type)
assign(capacity_type, size_type, const_reference)\endlink
,
assign(InputIterator, InputIterator)
,
assign(capacity_type, InputIterator, InputIterator)
*/
void assign(size_type n, param_value_type item) {
circular_buffern
items into the space optimized circular buffer specifying the capacity.
/*!
The capacity of the circular_buffer_space_optimized
will be set to the specified value and the
content of the circular_buffer_space_optimized
will be removed and replaced with n
copies of the item
.
\pre capacity_ctrl.%capacity() >= n
\post capacity() == capacity_ctrl \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item
\&\& ... \&\& (*this) [n - 1] == item
max[n, capacity_ctrl.min_capacity()]
.
\param capacity_ctrl The new capacity controller.
\param n The number of elements the circular_buffer_space_optimized
will be filled with.
\param item The element the circular_buffer_space_optimized
will be filled with.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the n
).
\sa operator=
, \link assign(size_type, param_value_type)
assign(size_type, const_reference)\endlink
, assign(InputIterator, InputIterator)
,
assign(capacity_type, InputIterator, InputIterator)
*/
void assign(capacity_type capacity_ctrl, size_type n, param_value_type item) {
BOOST_CB_ASSERT(capacity_ctrl.capacity() >= n); // check for new capacity lower than n
circular_buffercircular_buffer_space_optimized
will be removed and replaced with copies of
elements from the specified range.
\pre Valid range [first, last)
.first
and last
have to meet the requirements of
InputIterator.
\post capacity().%capacity() == std::distance(first, last) \&\& capacity().min_capacity() == 0 \&\&
size() == std::distance(first, last) \&\& (*this)[0]== *first \&\& (*this)[1] == *(first + 1) \&\& ...
\&\& (*this)[std::distance(first, last) - 1] == *(last - 1)
std::distance(first, last)
.
\param first The beginning of the range to be copied.
\param last The end of the range to be copied.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the std::distance(first, last)
).
\sa operator=
, \link assign(size_type, param_value_type)
assign(size_type, const_reference)\endlink
,
\link assign(capacity_type, size_type, param_value_type)
assign(capacity_type, size_type, const_reference)\endlink
,
assign(capacity_type, InputIterator, InputIterator)
*/
template circular_buffer_space_optimized
will be set to the specified value and the
content of the circular_buffer_space_optimized
will be removed and replaced with copies of
elements from the specified range.
\pre Valid range [first, last)
.first
and last
have to meet the requirements of
InputIterator.
\post capacity() == capacity_ctrl \&\& size() \<= std::distance(first, last) \&\&
(*this)[0]== *(last - capacity) \&\& (*this)[1] == *(last - capacity + 1) \&\& ... \&\&
(*this)[capacity - 1] == *(last - 1)
[first, last)
is greater than the
specified capacity
then only elements from the range [last - capacity, last)
will be copied.max[std::distance(first, last), capacity_ctrl.min_capacity()]
.
\param capacity_ctrl The new capacity controller.
\param first The beginning of the range to be copied.
\param last The end of the range to be copied.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in std::distance(first, last)
; in
min[capacity_ctrl.%capacity(), std::distance(first, last)]
if the InputIterator
is a RandomAccessIterator).
\sa operator=
, \link assign(size_type, param_value_type)
assign(size_type, const_reference)\endlink
,
\link assign(capacity_type, size_type, param_value_type)
assign(capacity_type, size_type, const_reference)\endlink
,
assign(InputIterator, InputIterator)
*/
template this
contains elements of cb
and vice versa; the capacity and the amount of
allocated memory in the internal buffer of this
equal to the capacity and the amount of
allocated memory of cb
and vice versa.
\param cb The circular_buffer_space_optimized
whose content will be swapped.
\throws Nothing.
\par Exception Safety
No-throw.
\par Iterator Invalidation
Invalidates all iterators of both circular_buffer_space_optimized
containers. (On the other
hand the iterators still point to the same elements but within another container. If you want to rely on
this feature you have to turn the Debug Support off otherwise an
assertion will report an error if such invalidated iterator is used.)
\par Complexity
Constant (in the size of the circular_buffer_space_optimized
).
\sa \link swap(circular_buffer&, circular_buffer&)
swap(circular_buffer_space_optimized&, circular_buffer_space_optimized&)\endlink
*/
void swap(circular_buffer_space_optimizedcapacity().%capacity() > 0
then back() == item
circular_buffer_space_optimized
is full, the first element will be removed. If the
capacity is 0
, nothing will be inserted.std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa \link push_front() push_front(const_reference)\endlink
, pop_back()
,
pop_front()
*/
void push_back(param_value_type item = value_type()) {
check_low_capacity();
circular_buffercapacity().%capacity() > 0
then front() == item
circular_buffer_space_optimized
is full, the last element will be removed. If the
capacity is 0
, nothing will be inserted.std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa \link push_back() push_back(const_reference)\endlink
, pop_back()
,
pop_front()
*/
void push_front(param_value_type item = value_type()) {
check_low_capacity();
circular_buffer!empty()
\post The last element is removed from the circular_buffer_space_optimized
.std::bad_alloc
if the standard allocator is
used).
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa pop_front()
, \link push_back() push_back(const_reference)\endlink
,
\link push_front() push_front(const_reference)\endlink
*/
void pop_back() {
circular_buffer!empty()
\post The first element is removed from the circular_buffer_space_optimized
.std::bad_alloc
if the standard allocator is
used).
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa pop_back()
, \link push_back() push_back(const_reference)\endlink
,
\link push_front() push_front(const_reference)\endlink
*/
void pop_front() {
circular_bufferpos
is a valid iterator pointing to the circular_buffer_space_optimized
or its
end.
\post The item
will be inserted at the position pos
.circular_buffer_space_optimized
is full, the first element will be overwritten. If
the circular_buffer_space_optimized
is full and the pos
points to
begin()
, then the item
will not be inserted. If the capacity is 0
,
nothing will be inserted.item
will be inserted.
\param item The element to be inserted.
\return Iterator to the inserted element or begin()
if the item
is not inserted. (See
the Effect.)
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa \link insert(iterator, size_type, param_value_type)
insert(iterator, size_type, value_type)\endlink
,
insert(iterator, InputIterator, InputIterator)
,
\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink
,
\link rinsert(iterator, size_type, param_value_type)
rinsert(iterator, size_type, value_type)\endlink
,
rinsert(iterator, InputIterator, InputIterator)
*/
iterator insert(iterator pos, param_value_type item = value_type()) {
size_type index = pos - begin();
check_low_capacity();
return circular_buffern
copies of the item
at the specified position.
/*!
\pre pos
is a valid iterator pointing to the circular_buffer_space_optimized
or its
end.
\post The number of min[n, (pos - begin()) + reserve()]
elements will be inserted at the position
pos
.min[pos - begin(), max[0, n - reserve()]]
elements will
be overwritten at the beginning of the circular_buffer_space_optimized
.item
s will be inserted.
\param n The number of item
s the to be inserted.
\param item The element whose copies will be inserted.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in min[capacity().%capacity(), size() + n]
).
\par Example
Consider a circular_buffer_space_optimized
with the capacity of 6 and the size of 4. Its
internal buffer may look like the one below.|1|2|3|4| | |
p ---^
p
:insert(p, (size_t)5, 0);
1
and 2
are overwritten. This is due to the fact the insert operation preserves
the capacity. After insertion the internal buffer looks like this:|0|0|0|0|3|4|
|1|2|0|0|0|0|0|3|4|
.
\sa \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink
,
insert(iterator, InputIterator, InputIterator)
,
\link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink
,
\link rinsert(iterator, size_type, param_value_type)
rinsert(iterator, size_type, value_type)\endlink
,
rinsert(iterator, InputIterator, InputIterator)
*/
void insert(iterator pos, size_type n, param_value_type item) {
size_type index = pos - begin();
check_low_capacity(n);
circular_buffer[first, last)
at the specified position.
/*!
\pre pos
is a valid iterator pointing to the circular_buffer_space_optimized
or its
end.[first, last)
where first
and last
meet the
requirements of an InputIterator.
\post Elements from the range
[first + max[0, distance(first, last) - (pos - begin()) - reserve()], last)
will be
inserted at the position pos
.min[pos - begin(), max[0,
distance(first, last) - reserve()]]
elements will be overwritten at the beginning of the
circular_buffer_space_optimized
.std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in [size() + std::distance(first, last)]
; in
min[capacity().%capacity(), size() + std::distance(first, last)]
if the
InputIterator
is a
RandomAccessIterator).
\par Example
Consider a circular_buffer_space_optimized
with the capacity of 6 and the size of 4. Its
internal buffer may look like the one below.|1|2|3|4| | |
p ---^
p
:int array[] = { 5, 6, 7, 8, 9 };
insert(p, array, array + 5);
6
, 7
, 8
and 9
from the
specified range get inserted and elements 1
and 2
are overwritten. This is due
to the fact the insert operation preserves the capacity. After insertion the internal buffer looks like
this:|6|7|8|9|3|4|
|1|2|5|6|7|8|9|3|4|
.
\sa \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink
,
\link insert(iterator, size_type, param_value_type)
insert(iterator, size_type, value_type)\endlink
, \link rinsert(iterator, param_value_type)
rinsert(iterator, value_type)\endlink
, \link rinsert(iterator, size_type, param_value_type)
rinsert(iterator, size_type, value_type)\endlink
,
rinsert(iterator, InputIterator, InputIterator)
*/
template pos
is a valid iterator pointing to the circular_buffer_space_optimized
or its
end.
\post The item
will be inserted before the position pos
.circular_buffer_space_optimized
is full, the last element will be overwritten. If the
circular_buffer_space_optimized
is full and the pos
points to
end()
, then the item
will not be inserted. If the capacity is 0
,
nothing will be inserted.item
will be inserted.
\param item The element to be inserted.
\return Iterator to the inserted element or end()
if the item
is not inserted. (See
the Effect.)
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa \link rinsert(iterator, size_type, param_value_type)
rinsert(iterator, size_type, value_type)\endlink
,
rinsert(iterator, InputIterator, InputIterator)
,
\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink
,
\link insert(iterator, size_type, param_value_type)
insert(iterator, size_type, value_type)\endlink
,
insert(iterator, InputIterator, InputIterator)
*/
iterator rinsert(iterator pos, param_value_type item = value_type()) {
size_type index = pos - begin();
check_low_capacity();
return circular_buffern
copies of the item
before the specified position.
/*!
\pre pos
is a valid iterator pointing to the circular_buffer_space_optimized
or its
end.
\post The number of min[n, (end() - pos) + reserve()]
elements will be inserted before the
position pos
.min[end() - pos, max[0, n - reserve()]]
elements
will be overwritten at the end of the circular_buffer_space_optimized
.item
s will be inserted.
\param n The number of item
s the to be inserted.
\param item The element whose copies will be inserted.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in min[capacity().%capacity(), size() + n]
).
\par Example
Consider a circular_buffer_space_optimized
with the capacity of 6 and the size of 4. Its
internal buffer may look like the one below.|1|2|3|4| | |
p ---^
p
:rinsert(p, (size_t)5, 0);
3
and 4
are overwritten. This is due to the fact the rinsert operation preserves
the capacity. After insertion the internal buffer looks like this:|1|2|0|0|0|0|
|1|2|0|0|0|0|0|3|4|
.
\sa \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink
,
rinsert(iterator, InputIterator, InputIterator)
,
\link insert(iterator, param_value_type) insert(iterator, value_type)\endlink
,
\link insert(iterator, size_type, param_value_type)
insert(iterator, size_type, value_type)\endlink
,
insert(iterator, InputIterator, InputIterator)
*/
void rinsert(iterator pos, size_type n, param_value_type item) {
size_type index = pos - begin();
check_low_capacity(n);
circular_buffer[first, last)
before the specified position.
/*!
\pre pos
is a valid iterator pointing to the circular_buffer_space_optimized
or its
end.[first, last)
where first
and last
meet the
requirements of an InputIterator.
\post Elements from the range
[first, last - max[0, distance(first, last) - (end() - pos) - reserve()])
will be inserted
before the position pos
.min[end() - pos, max[0,
distance(first, last) - reserve()]]
elements will be overwritten at the end of the
circular_buffer
.std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::T(const T&)
throws.
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in [size() + std::distance(first, last)]
; in
min[capacity().%capacity(), size() + std::distance(first, last)]
if the
InputIterator
is a
RandomAccessIterator).
\par Example
Consider a circular_buffer_space_optimized
with the capacity of 6 and the size of 4. Its
internal buffer may look like the one below.|1|2|3|4| | |
p ---^
p
:int array[] = { 5, 6, 7, 8, 9 };
insert(p, array, array + 5);
5
, 6
, 7
and 8
from the
specified range get inserted and elements 3
and 4
are overwritten. This is due
to the fact the rinsert operation preserves the capacity. After insertion the internal buffer looks like
this:|1|2|5|6|7|8|
|1|2|5|6|7|8|9|3|4|
.
\sa \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink
,
\link rinsert(iterator, size_type, param_value_type)
rinsert(iterator, size_type, value_type)\endlink
, \link insert(iterator, param_value_type)
insert(iterator, value_type)\endlink
, \link insert(iterator, size_type, param_value_type)
insert(iterator, size_type, value_type)\endlink
,
insert(iterator, InputIterator, InputIterator)
*/
template pos
is a valid iterator pointing to the circular_buffer_space_optimized
(but not
an end()
).
\post The element at the position pos
is removed.end()
if no such
element exists.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa erase(iterator, iterator)
, rerase(iterator)
,
rerase(iterator, iterator)
, clear()
*/
iterator erase(iterator pos) {
iterator it = circular_buffer[first, last)
.
/*!
\pre Valid range [first, last)
.
\post The elements from the range [first, last)
are removed. (If first == last
nothing is removed.)end()
if no such
element exists.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa erase(iterator)
, rerase(iterator)
, rerase(iterator, iterator)
,
clear()
*/
iterator erase(iterator first, iterator last) {
iterator it = circular_bufferpos
is a valid iterator pointing to the circular_buffer_space_optimized
(but not
an end()
).pos
is removed.
\param pos An iterator pointing at the element to be removed.
\return Iterator to the first element remaining in front of the removed element or begin()
if no
such element exists.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\note Basically there is no difference between erase(iterator)
and this method. It is implemented
only for consistency with the base circular_buffer
.
\sa erase(iterator)
, erase(iterator, iterator)
,
rerase(iterator, iterator)
, clear()
*/
iterator rerase(iterator pos) {
iterator it = circular_buffer[first, last)
.
/*!
\pre Valid range [first, last)
.
\post The elements from the range [first, last)
are removed. (If first == last
nothing is removed.)begin()
if no
such element exists.
\throws "An allocation error" if memory is exhausted (std::bad_alloc
if the standard allocator is
used).
\throws Whatever T::operator = (const T&)
throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\note Basically there is no difference between erase(iterator, iterator)
and this method. It is
implemented only for consistency with the base
circular_buffer
.
\sa erase(iterator)
, erase(iterator, iterator)
, rerase(iterator)
,
clear()
*/
iterator rerase(iterator first, iterator last) {
iterator it = circular_buffersize() == 0
std::bad_alloc
if the standard allocator is
used).
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the circular_buffer_space_optimized
(except iterators
equal to end()
).
\par Complexity
Linear (in the size of the circular_buffer_space_optimized
).
\sa ~circular_buffer_space_optimized()
, erase(iterator)
,
erase(iterator, iterator)
, rerase(iterator)
,
rerase(iterator, iterator)
*/
void clear() { erase(begin(), end()); }
private:
// Helper methods
//! Adjust the amount of allocated memory.
void adjust_min_capacity() {
if (m_capacity_ctrl.min_capacity() > circular_buffer