// Implementation of the circular buffer adaptor. // Copyright (c) 2003-2008 Jan Gaspar // Copyright (c) 2013 Paul A. Bristow // Doxygen comments changed for new version of documentation. // Copyright (c) 2013 Antony Polukhin // Move semantics implementation. // 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) #pragma once #endif #include #include namespace boost { /*! \class circular_buffer_space_optimized \brief Space optimized circular buffer container adaptor. T must be a copyable class or must have an noexcept move constructor and move assignment operator. */ template class circular_buffer_space_optimized : /*! \cond */ #if BOOST_CB_ENABLE_DEBUG public #endif /*! \endcond */ circular_buffer { public: // Typedefs typedef typename circular_buffer::value_type value_type; typedef typename circular_buffer::pointer pointer; typedef typename circular_buffer::const_pointer const_pointer; typedef typename circular_buffer::reference reference; typedef typename circular_buffer::const_reference const_reference; typedef typename circular_buffer::size_type size_type; typedef typename circular_buffer::difference_type difference_type; typedef typename circular_buffer::allocator_type allocator_type; typedef typename circular_buffer::const_iterator const_iterator; typedef typename circular_buffer::iterator iterator; typedef typename circular_buffer::const_reverse_iterator const_reverse_iterator; typedef typename circular_buffer::reverse_iterator reverse_iterator; typedef typename circular_buffer::array_range array_range; typedef typename circular_buffer::const_array_range const_array_range; typedef typename circular_buffer::param_value_type param_value_type; typedef typename circular_buffer::rvalue_type rvalue_type; //typedef typename circular_buffer::return_value_type return_value_type; /*
 is not passed through to html or pdf. So 
is used in code section below. Ugly :-( Ideally want a link to capacity_control, but this would require include details and this would expose all the functions in details. There must be a better way of doing this. */ /*! Capacity controller of the space optimized circular buffer. \see capacity_control in details.hpp.

class capacity_control
{
size_type m_capacity; // Available capacity.
size_type m_min_capacity; // Minimum 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; }
};

Always 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.

*/ typedef cb_details::capacity_control capacity_type; // Inherited using circular_buffer::get_allocator; using circular_buffer::begin; using circular_buffer::end; using circular_buffer::rbegin; using circular_buffer::rend; using circular_buffer::at; using circular_buffer::front; using circular_buffer::back; using circular_buffer::array_one; using circular_buffer::array_two; using circular_buffer::linearize; using circular_buffer::is_linearized; using circular_buffer::rotate; using circular_buffer::size; using circular_buffer::max_size; using circular_buffer::empty; #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) reference operator [] (size_type n) { return circular_buffer::operator[](n); } const_reference operator [] (size_type n) const { return circular_buffer::operator[](n); } #else using circular_buffer::operator[]; #endif private: // Member variables //! The capacity controller of the space optimized circular buffer. capacity_type m_capacity_ctrl; public: // Overridden //! Is the 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 BOOST_NOEXCEPT { 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 BOOST_NOEXCEPT { 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 BOOST_NOEXCEPT { 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 BOOST_NOEXCEPT { return circular_buffer::capacity(); } #endif // #if defined(BOOST_CB_TEST) /*! \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.capacity()

If the current number of elements stored in the 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().

If the current number of elements stored in the 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). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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);


For more information about the shrink-to-fit technique in STL see http://www.gotw.ca/gotw/054.htm. \sa 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_buffer::erase(e - (size() - capacity_ctrl), e); } adjust_min_capacity(); } //! Change the size of the circular_buffer_space_optimized. /*! \post size() == new_size \&\& capacity().%capacity() >= new_size

If the new size is greater than the current size, copies of 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.

If the current number of elements stored in the 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.)

The amount of allocated memory in the internal buffer may be accommodated as necessary. \param new_size The new size. \param item The element the 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). 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

If the current number of elements stored in the 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().

If the current number of elements stored in the 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). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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_buffer::rerase(b, b + (size() - capacity_ctrl)); } adjust_min_capacity(); } //! Change the size of the circular_buffer_space_optimized. /*! \post size() == new_size \&\& capacity().%capacity() >= new_size

If the new size is greater than the current size, copies of 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.

If the current number of elements stored in the 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.)

The amount of allocated memory in the internal buffer may be accommodated as necessary. \param new_size The new size. \param item The element the 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). 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 zero capacity. /*! \post capacity().%capacity() == 0 \&\& capacity().min_capacity() == 0 \&\& size() == 0 \param alloc The allocator. \throws Nothing. \par Complexity Constant. \warning Since Boost version 1.36 the behaviour of this constructor has changed. Now it creates a space optimized circular buffer with zero capacity. */ explicit circular_buffer_space_optimized(const allocator_type& alloc = allocator_type()) BOOST_NOEXCEPT : circular_buffer(0, alloc) , m_capacity_ctrl(0) {} //! Create an empty space optimized circular buffer with the specified capacity. /*! \post capacity() == capacity_ctrl \&\& size() == 0

The amount of allocated memory in the internal buffer is 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_buffer(capacity_ctrl.min_capacity(), alloc) , m_capacity_ctrl(capacity_ctrl) {} /*! \brief Create a full space optimized circular buffer with the specified capacity filled with capacity_ctrl.%capacity() copies of item. \post capacity() == capacity_ctrl \&\& full() \&\& (*this)[0] == item \&\& (*this)[1] == item \&\& ... \&\& (*this) [capacity_ctrl.%capacity() - 1] == item

The amount of allocated memory in the internal buffer is 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_buffer(capacity_ctrl.capacity(), item, alloc) , m_capacity_ctrl(capacity_ctrl) {} /*! \brief Create a space optimized circular buffer with the specified capacity filled with n copies of item. \pre capacity_ctrl.%capacity() >= n \post capacity() == capacity_ctrl \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item \&\& ... \&\& (*this)[n - 1] == item

The amount of allocated memory in the internal buffer is 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). 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_buffer(init_capacity(capacity_ctrl, n), n, item, alloc) , m_capacity_ctrl(capacity_ctrl) {} //! The copy constructor. /*! Creates a copy of the specified circular_buffer_space_optimized. \post *this == cb

The amount of allocated memory in the internal buffer is 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). Whatever T::T(const T&) throws. \par Complexity Linear (in the size of cb). */ circular_buffer_space_optimized(const circular_buffer_space_optimized& cb) : circular_buffer(cb.begin(), cb.end(), cb.get_allocator()) , m_capacity_ctrl(cb.m_capacity_ctrl) {} #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES //! The move constructor. /*! \brief Move constructs a circular_buffer_space_optimized from cb, leaving cb empty. \pre C++ compiler with rvalue references support. \post cb.empty() \param cb circular_buffer to 'steal' value from. \throws Nothing. \par Constant. */ circular_buffer_space_optimized(circular_buffer_space_optimized&& cb) BOOST_NOEXCEPT : circular_buffer() , m_capacity_ctrl(0) { cb.swap(*this); } #endif // BOOST_NO_CXX11_RVALUE_REFERENCES //! Create a full space optimized circular buffer filled with a copy of the 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 \&\& full() \&\& (*this)[0]== *first \&\& (*this)[1] == *(first + 1) \&\& ... \&\& (*this)[std::distance(first, last) - 1] == *(last - 1)

The amount of allocated memory in the internal buffer is 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). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept and InputIterator is a move iterator. \par Complexity Linear (in the std::distance(first, last)). */ template circular_buffer_space_optimized(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()) : circular_buffer(first, last, alloc) , m_capacity_ctrl(circular_buffer::capacity()) {} /*! \brief 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. \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_ctrl.%capacity()) \&\& (*this)[1] == *(last - capacity_ctrl.%capacity() + 1) \&\& ... \&\& (*this)[capacity_ctrl.%capacity() - 1] == *(last - 1)

If the number of items to be copied from the range [first, last) is greater than the specified capacity_ctrl.%capacity() then only elements from the range [last - capacity_ctrl.%capacity(), last) will be copied.

The amount of allocated memory in the internal buffer is 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). 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(capacity_type capacity_ctrl, InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()) : circular_buffer( init_capacity(capacity_ctrl, first, last, is_integral()), first, last, alloc) , m_capacity_ctrl(capacity_ctrl) { reduce_capacity( is_same< BOOST_DEDUCED_TYPENAME iterator_category::type, std::input_iterator_tag >()); } #if defined(BOOST_CB_NEVER_DEFINED) // This section will never be compiled - the default destructor will be generated instead. // Declared only for documentation purpose. //! The destructor. /*! Destroys the 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(); //! no-comment void erase_begin(size_type n); //! no-comment void erase_end(size_type n); #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

The amount of allocated memory in the internal buffer is 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_optimized& operator = (const circular_buffer_space_optimized& cb) { if (this == &cb) return *this; circular_buffer::assign(cb.begin(), cb.end()); m_capacity_ctrl = cb.m_capacity_ctrl; return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES /*! \brief Move assigns content of cb to *this, leaving cb empty. \pre C++ compiler with rvalue references support. \post cb.empty() \param cb circular_buffer to 'steal' value from. \throws Nothing. \par Complexity Constant. */ circular_buffer_space_optimized& operator = (circular_buffer_space_optimized&& cb) BOOST_NOEXCEPT { cb.swap(*this); // now `this` holds `cb` circular_buffer(get_allocator()) // temprary that holds initial `cb` allocator .swap(cb); // makes `cb` empty return *this; } #endif // BOOST_NO_CXX11_RVALUE_REFERENCES //! Assign n 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

The amount of allocated memory in the internal buffer is 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). 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 \link operator=(const circular_buffer_space_optimized&) operator=\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) */ void assign(size_type n, param_value_type item) { circular_buffer::assign(n, item); m_capacity_ctrl = capacity_type(n); } //! Assign n 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

The amount of allocated memory will be 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). 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 \link operator=(const circular_buffer_space_optimized&) operator=\endlink, \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_buffer::assign((std::max)(capacity_ctrl.min_capacity(), n), n, item); m_capacity_ctrl = capacity_ctrl; } //! Assign a copy of the range into the space optimized circular buffer. /*! 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() == 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)

The amount of allocated memory in the internal buffer is 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). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept and InputIterator is a move iterator. \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 \link operator=(const circular_buffer_space_optimized&) operator=\endlink, \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 void assign(InputIterator first, InputIterator last) { circular_buffer::assign(first, last); m_capacity_ctrl = capacity_type(circular_buffer::capacity()); } //! Assign a copy of the range 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 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)

If the number of items to be copied from the range [first, last) is greater than the specified capacity then only elements from the range [last - capacity, last) will be copied.

The amount of allocated memory in the internal buffer is 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). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept and InputIterator is a move iterator. \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 \link operator=(const circular_buffer_space_optimized&) operator=\endlink, \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 void assign(capacity_type capacity_ctrl, InputIterator first, InputIterator last) { m_capacity_ctrl = capacity_ctrl; circular_buffer::assign(capacity_ctrl, first, last); } //! Swap the contents of two space-optimized circular-buffers. /*! \post 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 by defining macro BOOST_CB_DISABLE_DEBUG, 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 swap(circular_buffer&, circular_buffer&), swap(circular_buffer_space_optimized&, circular_buffer_space_optimized&) */ // Note link does not work right. Asked on Doxygen forum for advice 23 May 2103. void swap(circular_buffer_space_optimized& cb) BOOST_NOEXCEPT { std::swap(m_capacity_ctrl, cb.m_capacity_ctrl); circular_buffer::swap(cb); } //! Insert a new element at the end of the space optimized circular buffer. /*! \post if capacity().%capacity() > 0 then back() == item
If the circular_buffer_space_optimized is full, the first element will be removed. If the capacity is 0, nothing will be inserted.

The amount of allocated memory in the internal buffer may be predictively increased. \param item The element to be inserted. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). 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) { check_low_capacity(); circular_buffer::push_back(item); } //! Insert a new element at the end of the space optimized circular buffer. /*! \post if capacity().%capacity() > 0 then back() == item
If the circular_buffer_space_optimized is full, the first element will be removed. If the capacity is 0, nothing will be inserted.

The amount of allocated memory in the internal buffer may be predictively increased. \param item The element to be inserted. \throws "An allocation error" if memory is exhausted (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 \link push_front() push_front(const_reference)\endlink, pop_back(), pop_front() */ void push_back(rvalue_type item) { check_low_capacity(); circular_buffer::push_back(boost::move(item)); } //! Insert a new element at the end of the space optimized circular buffer. /*! \post if capacity().%capacity() > 0 then back() == item
If the circular_buffer_space_optimized is full, the first element will be removed. If the capacity is 0, nothing will be inserted.

The amount of allocated memory in the internal buffer may be predictively increased. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::T() throws. Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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() { check_low_capacity(); circular_buffer::push_back(); } //! Insert a new element at the beginning of the space optimized circular buffer. /*! \post if capacity().%capacity() > 0 then front() == item
If the circular_buffer_space_optimized is full, the last element will be removed. If the capacity is 0, nothing will be inserted.

The amount of allocated memory in the internal buffer may be predictively increased. \param item The element to be inserted. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). 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) { check_low_capacity(); circular_buffer::push_front(item); } //! Insert a new element at the beginning of the space optimized circular buffer. /*! \post if capacity().%capacity() > 0 then front() == item
If the circular_buffer_space_optimized is full, the last element will be removed. If the capacity is 0, nothing will be inserted.

The amount of allocated memory in the internal buffer may be predictively increased. \param item The element to be inserted. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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(rvalue_type item) { check_low_capacity(); circular_buffer::push_front(boost::move(item)); } //! Insert a new element at the beginning of the space optimized circular buffer. /*! \post if capacity().%capacity() > 0 then front() == item
If the circular_buffer_space_optimized is full, the last element will be removed. If the capacity is 0, nothing will be inserted.

The amount of allocated memory in the internal buffer may be predictively increased. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::T() throws. Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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() { check_low_capacity(); circular_buffer::push_front(); } //! Remove the last element from the space optimized circular buffer. /*! \pre !empty() \post The last element is removed from the circular_buffer_space_optimized.

The amount of allocated memory in the internal buffer may be predictively decreased. \throws "An allocation error" if memory is exhausted (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::pop_back(); check_high_capacity(); } //! Remove the first element from the space optimized circular buffer. /*! \pre !empty() \post The first element is removed from the circular_buffer_space_optimized.

The amount of allocated memory in the internal buffer may be predictively decreased. \throws "An allocation error" if memory is exhausted (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_buffer::pop_front(); check_high_capacity(); } //! Insert an element at the specified position. /*! \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its end. \post The item will be inserted at the position pos.
If the 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.

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position where the 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). Whatever T::T(const T&) 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) { size_type index = pos - begin(); check_low_capacity(); return circular_buffer::insert(begin() + index, item); } //! Insert an element at the specified position. /*! \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its end. \post The item will be inserted at the position pos.
If the 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.

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position where the 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). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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, rvalue_type item) { size_type index = pos - begin(); check_low_capacity(); return circular_buffer::insert(begin() + index, boost::move(item)); } //! Insert an element at the specified position. /*! \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its end. \post The item will be inserted at the position pos.
If the 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.

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position where the item will 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). Whatever T::T() throws. Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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) { size_type index = pos - begin(); check_low_capacity(); return circular_buffer::insert(begin() + index); } //! Insert n 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.
The number of min[pos - begin(), max[0, n - reserve()]] elements will be overwritten at the beginning of the circular_buffer_space_optimized.
(See Example for the explanation.)

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position where the items will be inserted. \param n The number of items 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). Whatever T::T(const T&) 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 ___^

After inserting 5 elements at the position p:

insert(p, (size_t)5, 0);

actually only 4 elements 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:

|0|0|0|0|3|4|

For comparison if the capacity would not be preserved the internal buffer would then result in |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::insert(begin() + index, n, item); } //! Insert the range [first, last) at the specified position. /*! \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its end.
Valid range [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.
The number of min[pos - begin(), max[0, distance(first, last) - reserve()]] elements will be overwritten at the beginning of the circular_buffer_space_optimized.
(See Example for the explanation.)

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position where the range will be inserted. \param first The beginning of the range to be inserted. \param last The end of the range to be inserted. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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 ___^

After inserting a range of elements at the position p:

int array[] = { 5, 6, 7, 8, 9 };
insert(p, array, array + 5);

actually only elements 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|

For comparison if the capacity would not be preserved the internal buffer would then result in |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 void insert(iterator pos, InputIterator first, InputIterator last) { insert(pos, first, last, is_integral()); } //! Insert an element before the specified position. /*! \pre 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.
If the 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.

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position before which the 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). Whatever T::T(const T&) 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) { size_type index = pos - begin(); check_low_capacity(); return circular_buffer::rinsert(begin() + index, item); } //! Insert an element before the specified position. /*! \pre 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.
If the 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.

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position before which the 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). Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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, rvalue_type item) { size_type index = pos - begin(); check_low_capacity(); return circular_buffer::rinsert(begin() + index, boost::move(item)); } //! Insert an element before the specified position. /*! \pre 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.
If the 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.

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position before which the item will 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). Whatever T::T() throws. Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. \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) { size_type index = pos - begin(); check_low_capacity(); return circular_buffer::rinsert(begin() + index); } //! Insert n 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.
The number of min[end() - pos, max[0, n - reserve()]] elements will be overwritten at the end of the circular_buffer_space_optimized.
(See Example for the explanation.)

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position where the items will be inserted. \param n The number of items 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). Whatever T::T(const T&) 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 ___^

After inserting 5 elements before the position p:

rinsert(p, (size_t)5, 0);

actually only 4 elements 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|0|0|0|0|

For comparison if the capacity would not be preserved the internal buffer would then result in |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::rinsert(begin() + index, n, item); } //! Insert the range [first, last) before the specified position. /*! \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its end.
Valid range [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.
The number of min[end() - pos, max[0, distance(first, last) - reserve()]] elements will be overwritten at the end of the circular_buffer.
(See Example for the explanation.)

The amount of allocated memory in the internal buffer may be predictively increased. \param pos An iterator specifying the position where the range will be inserted. \param first The beginning of the range to be inserted. \param last The end of the range to be inserted. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::T(const T&) 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 ___^

After inserting a range of elements before the position p:

int array[] = { 5, 6, 7, 8, 9 };
insert(p, array, array + 5);

actually only elements 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|

For comparison if the capacity would not be preserved the internal buffer would then result in |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 void rinsert(iterator pos, InputIterator first, InputIterator last) { rinsert(pos, first, last, is_integral()); } //! Remove an element at the specified position. /*! \pre 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.

The amount of allocated memory in the internal buffer may be predictively decreased. \param pos An iterator pointing at the element to be removed. \return Iterator to the first element remaining beyond the removed element or end() if no such element exists. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::operator = (const T&) throws or nothing if T::operator = (T&&) is noexcept. \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::erase(pos); size_type index = it - begin(); check_high_capacity(); return begin() + index; } //! Erase the range [first, last). /*! \pre Valid range [first, last). \post The elements from the range [first, last) are removed. (If first == last nothing is removed.)

The amount of allocated memory in the internal buffer may be predictively decreased. \param first The beginning of the range to be removed. \param last The end of the range to be removed. \return Iterator to the first element remaining beyond the removed elements or end() if no such element exists. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::operator = (const T&) throws or nothing if T::operator = (T&&) is noexcept. \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_buffer::erase(first, last); size_type index = it - begin(); check_high_capacity(); return begin() + index; } //! Remove an element at the specified position. /*! \pre pos is a valid iterator pointing to the circular_buffer_space_optimized (but not an end()).

The amount of allocated memory in the internal buffer may be predictively decreased. \post The element at the position 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). Whatever T::operator = (const T&) throws or nothing if T::operator = (T&&) is noexcept. \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::rerase(pos); size_type index = it - begin(); check_high_capacity(); return begin() + index; } //! Erase the range [first, last). /*! \pre Valid range [first, last). \post The elements from the range [first, last) are removed. (If first == last nothing is removed.)

The amount of allocated memory in the internal buffer may be predictively decreased. \param first The beginning of the range to be removed. \param last The end of the range to be removed. \return Iterator to the first element remaining in front of the removed elements or begin() if no such element exists. \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is used). Whatever T::operator = (const T&) throws or nothing if T::operator = (T&&) is noexcept. \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 . \sa erase(iterator), erase(iterator, iterator), rerase(iterator), clear() */ iterator rerase(iterator first, iterator last) { iterator it = circular_buffer::rerase(first, last); size_type index = it - begin(); check_high_capacity(); return begin() + index; } //! Remove all stored elements from the space optimized circular buffer. /*! \post size() == 0

The amount of allocated memory in the internal buffer may be predictively decreased. \throws "An allocation error" if memory is exhausted (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::capacity()) circular_buffer::set_capacity(m_capacity_ctrl.min_capacity()); else check_high_capacity(); } //! Ensure the reserve for possible growth up. size_type ensure_reserve(size_type new_capacity, size_type buffer_size) const { if (buffer_size + new_capacity / 5 >= new_capacity) new_capacity *= 2; // ensure at least 20% reserve if (new_capacity > m_capacity_ctrl) return m_capacity_ctrl; return new_capacity; } //! Check for low capacity. /* \post If the capacity is low it will be increased. */ void check_low_capacity(size_type n = 1) { size_type new_size = size() + n; size_type new_capacity = circular_buffer::capacity(); if (new_size > new_capacity) { if (new_capacity == 0) new_capacity = 1; for (; new_size > new_capacity; new_capacity *= 2) {} circular_buffer::set_capacity( ensure_reserve(new_capacity, new_size)); } #if BOOST_CB_ENABLE_DEBUG this->invalidate_iterators_except(end()); #endif } //! Check for high capacity. /* \post If the capacity is high it will be decreased. */ void check_high_capacity() { size_type new_capacity = circular_buffer::capacity(); while (new_capacity / 3 >= size()) { // (new_capacity / 3) -> avoid oscillations new_capacity /= 2; if (new_capacity <= m_capacity_ctrl.min_capacity()) { new_capacity = m_capacity_ctrl.min_capacity(); break; } } circular_buffer::set_capacity( ensure_reserve(new_capacity, size())); #if BOOST_CB_ENABLE_DEBUG this->invalidate_iterators_except(end()); #endif } //! Specialized method for reducing the capacity. void reduce_capacity(const true_type&) { circular_buffer::set_capacity((std::max)(m_capacity_ctrl.min_capacity(), size())); } //! Specialized method for reducing the capacity. void reduce_capacity(const false_type&) {} //! Determine the initial capacity. static size_type init_capacity(const capacity_type& capacity_ctrl, size_type n) { BOOST_CB_ASSERT(capacity_ctrl.capacity() >= n); // check for capacity lower than n return (std::max)(capacity_ctrl.min_capacity(), n); } //! Specialized method for determining the initial capacity. template static size_type init_capacity(const capacity_type& capacity_ctrl, IntegralType n, IntegralType, const true_type&) { return init_capacity(capacity_ctrl, static_cast(n)); } //! Specialized method for determining the initial capacity. template static size_type init_capacity(const capacity_type& capacity_ctrl, Iterator first, Iterator last, const false_type&) { BOOST_CB_IS_CONVERTIBLE(Iterator, value_type); // check for invalid iterator type #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581)) return init_capacity(capacity_ctrl, first, last, iterator_category::type()); #else return init_capacity( capacity_ctrl, first, last, BOOST_DEDUCED_TYPENAME iterator_category::type()); #endif } //! Specialized method for determining the initial capacity. template static size_type init_capacity(const capacity_type& capacity_ctrl, InputIterator, InputIterator, const std::input_iterator_tag&) { return capacity_ctrl.capacity(); } //! Specialized method for determining the initial capacity. template static size_type init_capacity(const capacity_type& capacity_ctrl, ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) { BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range return (std::max)(capacity_ctrl.min_capacity(), (std::min)(capacity_ctrl.capacity(), static_cast(std::distance(first, last)))); } //! Specialized insert method. template void insert(const iterator& pos, IntegralType n, IntegralType item, const true_type&) { insert(pos, static_cast(n), static_cast(item)); } //! Specialized insert method. template void insert(const iterator& pos, Iterator first, Iterator last, const false_type&) { size_type index = pos - begin(); check_low_capacity(std::distance(first, last)); circular_buffer::insert(begin() + index, first, last); } //! Specialized rinsert method. template void rinsert(const iterator& pos, IntegralType n, IntegralType item, const true_type&) { rinsert(pos, static_cast(n), static_cast(item)); } //! Specialized rinsert method. template void rinsert(const iterator& pos, Iterator first, Iterator last, const false_type&) { size_type index = pos - begin(); check_low_capacity(std::distance(first, last)); circular_buffer::rinsert(begin() + index, first, last); } }; // Non-member functions //! Test two space optimized circular buffers for equality. template inline bool operator == (const circular_buffer_space_optimized& lhs, const circular_buffer_space_optimized& rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); } //! Lexicographical comparison. template inline bool operator < (const circular_buffer_space_optimized& lhs, const circular_buffer_space_optimized& rhs) { return std::lexicographical_compare( lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310)) //! Test two space optimized circular buffers for non-equality. template inline bool operator != (const circular_buffer_space_optimized& lhs, const circular_buffer_space_optimized& rhs) { return !(lhs == rhs); } //! Lexicographical comparison. template inline bool operator > (const circular_buffer_space_optimized& lhs, const circular_buffer_space_optimized& rhs) { return rhs < lhs; } //! Lexicographical comparison. template inline bool operator <= (const circular_buffer_space_optimized& lhs, const circular_buffer_space_optimized& rhs) { return !(rhs < lhs); } //! Lexicographical comparison. template inline bool operator >= (const circular_buffer_space_optimized& lhs, const circular_buffer_space_optimized& rhs) { return !(lhs < rhs); } //! Swap the contents of two space optimized circular buffers. template inline void swap(circular_buffer_space_optimized& lhs, circular_buffer_space_optimized& rhs) BOOST_NOEXCEPT { lhs.swap(rhs); } #endif // #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310)) } // namespace boost #endif // #if !defined(BOOST_CIRCULAR_BUFFER_SPACE_OPTIMIZED_HPP)