Chris@16: // Implementation of the circular buffer adaptor. Chris@16: Chris@16: // Copyright (c) 2003-2008 Jan Gaspar Chris@16: // Copyright (c) 2013 Paul A. Bristow // Doxygen comments changed for new version of documentation. Chris@16: // Copyright (c) 2013 Antony Polukhin // Move semantics implementation. Chris@16: Chris@16: // Use, modification, and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(BOOST_CIRCULAR_BUFFER_SPACE_OPTIMIZED_HPP) Chris@16: #define BOOST_CIRCULAR_BUFFER_SPACE_OPTIMIZED_HPP Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: /*! Chris@16: \class circular_buffer_space_optimized Chris@16: \brief Space optimized circular buffer container adaptor. Chris@16: T must be a copyable class or must have an noexcept move constructor Chris@16: and move assignment operator. Chris@16: */ Chris@16: template Chris@16: class circular_buffer_space_optimized : Chris@16: /*! \cond */ Chris@16: #if BOOST_CB_ENABLE_DEBUG Chris@16: public Chris@16: #endif Chris@16: /*! \endcond */ Chris@16: circular_buffer { Chris@16: public: Chris@16: // Typedefs Chris@16: Chris@16: typedef typename circular_buffer::value_type value_type; Chris@16: typedef typename circular_buffer::pointer pointer; Chris@16: typedef typename circular_buffer::const_pointer const_pointer; Chris@16: typedef typename circular_buffer::reference reference; Chris@16: typedef typename circular_buffer::const_reference const_reference; Chris@16: typedef typename circular_buffer::size_type size_type; Chris@16: typedef typename circular_buffer::difference_type difference_type; Chris@16: typedef typename circular_buffer::allocator_type allocator_type; Chris@16: typedef typename circular_buffer::const_iterator const_iterator; Chris@16: typedef typename circular_buffer::iterator iterator; Chris@16: typedef typename circular_buffer::const_reverse_iterator const_reverse_iterator; Chris@16: typedef typename circular_buffer::reverse_iterator reverse_iterator; Chris@16: typedef typename circular_buffer::array_range array_range; Chris@16: typedef typename circular_buffer::const_array_range const_array_range; Chris@16: typedef typename circular_buffer::param_value_type param_value_type; Chris@16: typedef typename circular_buffer::rvalue_type rvalue_type; Chris@16: //typedef typename circular_buffer::return_value_type return_value_type; Chris@16: Chris@16: /*
 is not passed through to html or pdf. So 
is used in code section below. Ugly :-( Chris@16: Ideally want a link to capacity_control, but this would require include details Chris@16: and this would expose all the functions in details. Chris@16: There must be a better way of doing this. Chris@16: */ Chris@16: Chris@16: /*! Capacity controller of the space optimized circular buffer. Chris@16: Chris@16: \see capacity_control in details.hpp. Chris@16:

Chris@16: Chris@16: class capacity_control
Chris@16: {
Chris@16: size_type m_capacity; // Available capacity.
Chris@16: size_type m_min_capacity; // Minimum capacity.
Chris@16: public:
Chris@16: capacity_control(size_type capacity, size_type min_capacity = 0)
Chris@16: : m_capacity(capacity), m_min_capacity(min_capacity)
Chris@16: {};
Chris@16: size_type %capacity() const { return m_capacity; }
Chris@16: size_type min_capacity() const { return m_min_capacity; }
Chris@16: operator size_type() const { return m_capacity; }
Chris@16: };
Chris@16:
Chris@16:

Chris@16: Chris@16: Chris@16:

Always Chris@16: capacity >= min_capacity. Chris@16:

Chris@16:

Chris@16: The capacity() represents the capacity Chris@16: of the circular_buffer_space_optimized and Chris@16: the min_capacity() determines the minimal allocated size of its internal buffer. Chris@16:

Chris@16:

The converting constructor of the capacity_control allows implicit conversion from Chris@16: size_type-like types which ensures compatibility of creating an instance of the Chris@16: circular_buffer_space_optimized with other STL containers. Chris@16: Chris@16: On the other hand the operator %size_type() Chris@16: provides implicit conversion to the size_type which allows to treat the Chris@16: capacity of the circular_buffer_space_optimized the same way as in the Chris@16: circular_buffer. Chris@16:

Chris@16: */ Chris@16: typedef cb_details::capacity_control capacity_type; Chris@16: Chris@16: // Inherited Chris@16: Chris@16: using circular_buffer::get_allocator; Chris@16: using circular_buffer::begin; Chris@16: using circular_buffer::end; Chris@16: using circular_buffer::rbegin; Chris@16: using circular_buffer::rend; Chris@16: using circular_buffer::at; Chris@16: using circular_buffer::front; Chris@16: using circular_buffer::back; Chris@16: using circular_buffer::array_one; Chris@16: using circular_buffer::array_two; Chris@16: using circular_buffer::linearize; Chris@16: using circular_buffer::is_linearized; Chris@16: using circular_buffer::rotate; Chris@16: using circular_buffer::size; Chris@16: using circular_buffer::max_size; Chris@16: using circular_buffer::empty; Chris@16: Chris@16: #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) Chris@16: reference operator [] (size_type n) { return circular_buffer::operator[](n); } Chris@16: const_reference operator [] (size_type n) const { return circular_buffer::operator[](n); } Chris@16: #else Chris@16: using circular_buffer::operator[]; Chris@16: #endif Chris@16: Chris@16: private: Chris@16: // Member variables Chris@16: Chris@16: //! The capacity controller of the space optimized circular buffer. Chris@16: capacity_type m_capacity_ctrl; Chris@16: Chris@16: public: Chris@16: // Overridden Chris@16: Chris@16: //! Is the circular_buffer_space_optimized full? Chris@16: /*! Chris@16: \return true if the number of elements stored in the circular_buffer_space_optimized Chris@16: equals the capacity of the circular_buffer_space_optimized; false otherwise. Chris@16: \throws Nothing. Chris@16: \par Exception Safety Chris@16: No-throw. Chris@16: \par Iterator Invalidation Chris@16: Does not invalidate any iterators. Chris@16: \par Complexity Chris@16: Constant (in the size of the circular_buffer_space_optimized). Chris@16: \sa empty() Chris@16: */ Chris@16: bool full() const BOOST_NOEXCEPT { return m_capacity_ctrl == size(); } Chris@16: Chris@16: /*! \brief Get the maximum number of elements which can be inserted into the Chris@16: circular_buffer_space_optimized without overwriting any of already stored elements. Chris@16: \return capacity().%capacity() - size() Chris@16: \throws Nothing. Chris@16: \par Exception Safety Chris@16: No-throw. Chris@16: \par Iterator Invalidation Chris@16: Does not invalidate any iterators. Chris@16: \par Complexity Chris@16: Constant (in the size of the circular_buffer_space_optimized). Chris@16: \sa capacity(), size(), max_size() Chris@16: */ Chris@16: size_type reserve() const BOOST_NOEXCEPT { return m_capacity_ctrl - size(); } Chris@16: Chris@16: //! Get the capacity of the circular_buffer_space_optimized. Chris@16: /*! Chris@16: \return The capacity controller representing the maximum number of elements which can be stored in the Chris@16: circular_buffer_space_optimized and the minimal allocated size of the internal buffer. Chris@16: \throws Nothing. Chris@16: \par Exception Safety Chris@16: No-throw. Chris@16: \par Iterator Invalidation Chris@16: Does not invalidate any iterators. Chris@16: \par Complexity Chris@16: Constant (in the size of the circular_buffer_space_optimized). Chris@16: \sa reserve(), size(), max_size(), Chris@16: set_capacity(const capacity_type&) Chris@16: */ Chris@16: const capacity_type& capacity() const BOOST_NOEXCEPT { return m_capacity_ctrl; } Chris@16: Chris@16: #if defined(BOOST_CB_TEST) Chris@16: Chris@16: // Return the current capacity of the adapted circular buffer. Chris@16: /* Chris@16: \note This method is not intended to be used directly by the user. Chris@16: It is defined only for testing purposes. Chris@16: */ Chris@16: size_type internal_capacity() const BOOST_NOEXCEPT { return circular_buffer::capacity(); } Chris@16: Chris@16: #endif // #if defined(BOOST_CB_TEST) Chris@16: Chris@16: /*! \brief Change the capacity (and the minimal guaranteed amount of allocated memory) of the Chris@16: circular_buffer_space_optimized. Chris@16: \post capacity() == capacity_ctrl \&\& size() \<= capacity_ctrl.capacity()

Chris@16: If the current number of elements stored in the circular_buffer_space_optimized is greater Chris@16: than the desired new capacity then number of [size() - capacity_ctrl.capacity()] last Chris@16: elements will be removed and the new size will be equal to capacity_ctrl.capacity().

Chris@16: If the current number of elements stored in the circular_buffer_space_optimized is lower Chris@16: than the new capacity then the amount of allocated memory in the internal buffer may be accommodated as Chris@16: necessary but it will never drop below capacity_ctrl.min_capacity(). Chris@16: \param capacity_ctrl The new capacity controller. Chris@16: \throws "An allocation error" if memory is exhausted, (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Strong. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in min[size(), capacity_ctrl.%capacity()]). Chris@16: \note To explicitly clear the extra allocated memory use the shrink-to-fit technique:

Chris@16: %boost::%circular_buffer_space_optimized\ cb(1000);
Chris@16: ...
Chris@16: %boost::%circular_buffer_space_optimized\(cb).swap(cb);


Chris@16: For more information about the shrink-to-fit technique in STL see Chris@16: http://www.gotw.ca/gotw/054.htm. Chris@16: \sa rset_capacity(const capacity_type&), Chris@16: \link resize() resize(size_type, const_reference)\endlink Chris@16: */ Chris@16: void set_capacity(const capacity_type& capacity_ctrl) { Chris@16: m_capacity_ctrl = capacity_ctrl; Chris@16: if (capacity_ctrl < size()) { Chris@16: iterator e = end(); Chris@16: circular_buffer::erase(e - (size() - capacity_ctrl), e); Chris@16: } Chris@16: adjust_min_capacity(); Chris@16: } Chris@16: Chris@16: //! Change the size of the circular_buffer_space_optimized. Chris@16: /*! Chris@16: \post size() == new_size \&\& capacity().%capacity() >= new_size

Chris@16: If the new size is greater than the current size, copies of item will be inserted at the Chris@16: back of the of the circular_buffer_space_optimized in order to achieve the desired Chris@16: size. In the case the resulting size exceeds the current capacity the capacity will be set to Chris@16: new_size.

Chris@16: If the current number of elements stored in the circular_buffer_space_optimized is greater Chris@16: than the desired new size then number of [size() - new_size] last elements will be Chris@16: removed. (The capacity will remain unchanged.)

Chris@16: The amount of allocated memory in the internal buffer may be accommodated as necessary. Chris@16: \param new_size The new size. Chris@16: \param item The element the circular_buffer_space_optimized will be filled with in order to gain Chris@16: the requested size. (See the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the new size of the circular_buffer_space_optimized). Chris@16: \sa \link rresize() rresize(size_type, const_reference)\endlink, Chris@16: set_capacity(const capacity_type&) Chris@16: */ Chris@16: void resize(size_type new_size, param_value_type item = value_type()) { Chris@16: if (new_size > size()) { Chris@16: if (new_size > m_capacity_ctrl) Chris@16: m_capacity_ctrl = capacity_type(new_size, m_capacity_ctrl.min_capacity()); Chris@16: insert(end(), new_size - size(), item); Chris@16: } else { Chris@16: iterator e = end(); Chris@16: erase(e - (size() - new_size), e); Chris@16: } Chris@16: } Chris@16: Chris@16: /*! \brief Change the capacity (and the minimal guaranteed amount of allocated memory) of the Chris@16: circular_buffer_space_optimized. Chris@16: \post capacity() == capacity_ctrl \&\& size() \<= capacity_ctrl

Chris@16: If the current number of elements stored in the circular_buffer_space_optimized is greater Chris@16: than the desired new capacity then number of [size() - capacity_ctrl.capacity()] Chris@16: first elements will be removed and the new size will be equal to Chris@16: capacity_ctrl.capacity().

Chris@16: If the current number of elements stored in the circular_buffer_space_optimized is lower Chris@16: than the new capacity then the amount of allocated memory in the internal buffer may be accommodated as Chris@16: necessary but it will never drop below capacity_ctrl.min_capacity(). Chris@16: \param capacity_ctrl The new capacity controller. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Strong. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in min[size(), capacity_ctrl.%capacity()]). Chris@16: \sa set_capacity(const capacity_type&), Chris@16: \link rresize() rresize(size_type, const_reference)\endlink Chris@16: */ Chris@16: void rset_capacity(const capacity_type& capacity_ctrl) { Chris@16: m_capacity_ctrl = capacity_ctrl; Chris@16: if (capacity_ctrl < size()) { Chris@16: iterator b = begin(); Chris@16: circular_buffer::rerase(b, b + (size() - capacity_ctrl)); Chris@16: } Chris@16: adjust_min_capacity(); Chris@16: } Chris@16: Chris@16: //! Change the size of the circular_buffer_space_optimized. Chris@16: /*! Chris@16: \post size() == new_size \&\& capacity().%capacity() >= new_size

Chris@16: If the new size is greater than the current size, copies of item will be inserted at the Chris@16: front of the of the circular_buffer_space_optimized in order to achieve the desired Chris@16: size. In the case the resulting size exceeds the current capacity the capacity will be set to Chris@16: new_size.

Chris@16: If the current number of elements stored in the circular_buffer_space_optimized is greater Chris@16: than the desired new size then number of [size() - new_size] first elements will be Chris@16: removed. (The capacity will remain unchanged.)

Chris@16: The amount of allocated memory in the internal buffer may be accommodated as necessary. Chris@16: \param new_size The new size. Chris@16: \param item The element the circular_buffer_space_optimized will be filled with in order to gain Chris@16: the requested size. (See the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the new size of the circular_buffer_space_optimized). Chris@16: \sa \link resize() resize(size_type, const_reference)\endlink, Chris@16: rset_capacity(const capacity_type&) Chris@16: */ Chris@16: void rresize(size_type new_size, param_value_type item = value_type()) { Chris@16: if (new_size > size()) { Chris@16: if (new_size > m_capacity_ctrl) Chris@16: m_capacity_ctrl = capacity_type(new_size, m_capacity_ctrl.min_capacity()); Chris@16: rinsert(begin(), new_size - size(), item); Chris@16: } else { Chris@16: rerase(begin(), end() - new_size); Chris@16: } Chris@16: } Chris@16: Chris@16: //! Create an empty space optimized circular buffer with zero capacity. Chris@16: /*! Chris@16: \post capacity().%capacity() == 0 \&\& capacity().min_capacity() == 0 \&\& size() == 0 Chris@16: \param alloc The allocator. Chris@16: \throws Nothing. Chris@16: \par Complexity Chris@16: Constant. Chris@16: \warning Since Boost version 1.36 the behaviour of this constructor has changed. Now it creates a space Chris@16: optimized circular buffer with zero capacity. Chris@16: */ Chris@16: explicit circular_buffer_space_optimized(const allocator_type& alloc = allocator_type()) BOOST_NOEXCEPT Chris@16: : circular_buffer(0, alloc) Chris@16: , m_capacity_ctrl(0) {} Chris@16: Chris@16: //! Create an empty space optimized circular buffer with the specified capacity. Chris@16: /*! Chris@16: \post capacity() == capacity_ctrl \&\& size() == 0

Chris@16: The amount of allocated memory in the internal buffer is capacity_ctrl.min_capacity(). Chris@16: \param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in Chris@16: the circular_buffer_space_optimized and the minimal allocated size of the Chris@16: internal buffer. Chris@16: \param alloc The allocator. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: \par Complexity Chris@16: Constant. Chris@16: */ Chris@16: explicit circular_buffer_space_optimized(capacity_type capacity_ctrl, Chris@16: const allocator_type& alloc = allocator_type()) Chris@16: : circular_buffer(capacity_ctrl.min_capacity(), alloc) Chris@16: , m_capacity_ctrl(capacity_ctrl) {} Chris@16: Chris@16: /*! \brief Create a full space optimized circular buffer with the specified capacity filled with Chris@16: capacity_ctrl.%capacity() copies of item. Chris@16: \post capacity() == capacity_ctrl \&\& full() \&\& (*this)[0] == item \&\& (*this)[1] == item \&\& ... Chris@16: \&\& (*this) [capacity_ctrl.%capacity() - 1] == item

Chris@16: The amount of allocated memory in the internal buffer is capacity_ctrl.capacity(). Chris@16: \param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in Chris@16: the circular_buffer_space_optimized and the minimal allocated size of the Chris@16: internal buffer. Chris@16: \param item The element the created circular_buffer_space_optimized will be filled with. Chris@16: \param alloc The allocator. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: \throws Whatever T::T(const T&) throws. Chris@16: \par Complexity Chris@16: Linear (in the capacity_ctrl.%capacity()). Chris@16: */ Chris@16: circular_buffer_space_optimized(capacity_type capacity_ctrl, param_value_type item, Chris@16: const allocator_type& alloc = allocator_type()) Chris@16: : circular_buffer(capacity_ctrl.capacity(), item, alloc) Chris@16: , m_capacity_ctrl(capacity_ctrl) {} Chris@16: Chris@16: /*! \brief Create a space optimized circular buffer with the specified capacity filled with n copies Chris@16: of item. Chris@16: \pre capacity_ctrl.%capacity() >= n Chris@16: \post capacity() == capacity_ctrl \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item Chris@16: \&\& ... \&\& (*this)[n - 1] == item

Chris@16: The amount of allocated memory in the internal buffer is Chris@16: max[n, capacity_ctrl.min_capacity()]. Chris@16: \param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in Chris@16: the circular_buffer_space_optimized and the minimal allocated size of the Chris@16: internal buffer. Chris@16: \param n The number of elements the created circular_buffer_space_optimized will be filled with. Chris@16: \param item The element the created circular_buffer_space_optimized will be filled with. Chris@16: \param alloc The allocator. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Complexity Chris@16: Linear (in the n). Chris@16: */ Chris@16: circular_buffer_space_optimized(capacity_type capacity_ctrl, size_type n, param_value_type item, Chris@16: const allocator_type& alloc = allocator_type()) Chris@16: : circular_buffer(init_capacity(capacity_ctrl, n), n, item, alloc) Chris@16: , m_capacity_ctrl(capacity_ctrl) {} Chris@16: Chris@16: //! The copy constructor. Chris@16: /*! Chris@16: Creates a copy of the specified circular_buffer_space_optimized. Chris@16: \post *this == cb

Chris@16: The amount of allocated memory in the internal buffer is cb.size(). Chris@16: \param cb The circular_buffer_space_optimized to be copied. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Complexity Chris@16: Linear (in the size of cb). Chris@16: */ Chris@16: circular_buffer_space_optimized(const circular_buffer_space_optimized& cb) Chris@16: : circular_buffer(cb.begin(), cb.end(), cb.get_allocator()) Chris@16: , m_capacity_ctrl(cb.m_capacity_ctrl) {} Chris@16: Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: //! The move constructor. Chris@16: /*! \brief Move constructs a circular_buffer_space_optimized from cb, Chris@16: leaving cb empty. Chris@16: \pre C++ compiler with rvalue references support. Chris@16: \post cb.empty() Chris@16: \param cb circular_buffer to 'steal' value from. Chris@16: \throws Nothing. Chris@16: \par Constant. Chris@16: */ Chris@16: circular_buffer_space_optimized(circular_buffer_space_optimized&& cb) BOOST_NOEXCEPT Chris@16: : circular_buffer() Chris@16: , m_capacity_ctrl(0) { Chris@16: cb.swap(*this); Chris@16: } Chris@16: #endif // BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: Chris@16: //! Create a full space optimized circular buffer filled with a copy of the range. Chris@16: /*! Chris@16: \pre Valid range [first, last).
Chris@16: first and last have to meet the requirements of Chris@16: InputIterator. Chris@16: \post capacity().%capacity() == std::distance(first, last) \&\& capacity().min_capacity() == 0 \&\& Chris@16: full() \&\& (*this)[0]== *first \&\& (*this)[1] == *(first + 1) \&\& ... \&\& Chris@16: (*this)[std::distance(first, last) - 1] == *(last - 1)

Chris@16: The amount of allocated memory in the internal buffer is std::distance(first, last). Chris@16: \param first The beginning of the range to be copied. Chris@16: \param last The end of the range to be copied. Chris@16: \param alloc The allocator. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept Chris@16: and InputIterator is a move iterator. Chris@16: \par Complexity Chris@16: Linear (in the std::distance(first, last)). Chris@16: */ Chris@16: template Chris@16: circular_buffer_space_optimized(InputIterator first, InputIterator last, Chris@16: const allocator_type& alloc = allocator_type()) Chris@16: : circular_buffer(first, last, alloc) Chris@16: , m_capacity_ctrl(circular_buffer::capacity()) {} Chris@16: Chris@16: /*! \brief Create a space optimized circular buffer with the specified capacity (and the minimal guaranteed amount Chris@16: of allocated memory) filled with a copy of the range. Chris@16: \pre Valid range [first, last).
Chris@16: first and last have to meet the requirements of Chris@16: InputIterator. Chris@16: \post capacity() == capacity_ctrl \&\& size() \<= std::distance(first, last) \&\& (*this)[0]== Chris@16: *(last - capacity_ctrl.%capacity()) \&\& (*this)[1] == *(last - capacity_ctrl.%capacity() + 1) \&\& ... Chris@16: \&\& (*this)[capacity_ctrl.%capacity() - 1] == *(last - 1)

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

Chris@16: The amount of allocated memory in the internal buffer is max[capacity_ctrl.min_capacity(), Chris@16: min[capacity_ctrl.%capacity(), std::distance(first, last)]]. Chris@16: \param capacity_ctrl The capacity controller representing the maximum number of elements which can be stored in Chris@16: the circular_buffer_space_optimized and the minimal allocated size of the Chris@16: internal buffer. Chris@16: \param first The beginning of the range to be copied. Chris@16: \param last The end of the range to be copied. Chris@16: \param alloc The allocator. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Complexity Chris@16: Linear (in std::distance(first, last); in Chris@16: min[capacity_ctrl.%capacity(), std::distance(first, last)] if the InputIterator Chris@16: is a RandomAccessIterator). Chris@16: */ Chris@16: template Chris@16: circular_buffer_space_optimized(capacity_type capacity_ctrl, InputIterator first, InputIterator last, Chris@16: const allocator_type& alloc = allocator_type()) Chris@16: : circular_buffer( Chris@16: init_capacity(capacity_ctrl, first, last, is_integral()), Chris@16: first, last, alloc) Chris@16: , m_capacity_ctrl(capacity_ctrl) { Chris@16: reduce_capacity( Chris@101: is_same< BOOST_DEDUCED_TYPENAME iterator_category::type, std::input_iterator_tag >()); Chris@16: } Chris@16: Chris@16: #if defined(BOOST_CB_NEVER_DEFINED) Chris@16: // This section will never be compiled - the default destructor will be generated instead. Chris@16: // Declared only for documentation purpose. Chris@16: Chris@16: //! The destructor. Chris@16: /*! Chris@16: Destroys the circular_buffer_space_optimized. Chris@16: \throws Nothing. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (including Chris@16: iterators equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa clear() Chris@16: */ Chris@16: ~circular_buffer_space_optimized(); Chris@16: Chris@16: //! no-comment Chris@16: void erase_begin(size_type n); Chris@16: Chris@16: //! no-comment Chris@16: void erase_end(size_type n); Chris@16: Chris@16: #endif // #if defined(BOOST_CB_NEVER_DEFINED) Chris@16: Chris@16: //! The assign operator. Chris@16: /*! Chris@16: Makes this circular_buffer_space_optimized to become a copy of the specified Chris@16: circular_buffer_space_optimized. Chris@16: \post *this == cb

Chris@16: The amount of allocated memory in the internal buffer is cb.size(). Chris@16: \param cb The circular_buffer_space_optimized to be copied. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: \throws Whatever T::T(const T&) throws. Chris@16: \par Exception Safety Chris@16: Strong. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to this circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of cb). Chris@16: \sa \link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink, Chris@16: \link assign(capacity_type, size_type, param_value_type) Chris@16: assign(capacity_type, size_type, const_reference)\endlink, Chris@16: assign(InputIterator, InputIterator), Chris@16: assign(capacity_type, InputIterator, InputIterator) Chris@16: */ Chris@16: circular_buffer_space_optimized& operator = (const circular_buffer_space_optimized& cb) { Chris@16: if (this == &cb) Chris@16: return *this; Chris@16: circular_buffer::assign(cb.begin(), cb.end()); Chris@16: m_capacity_ctrl = cb.m_capacity_ctrl; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: /*! \brief Move assigns content of cb to *this, leaving cb empty. Chris@16: \pre C++ compiler with rvalue references support. Chris@16: \post cb.empty() Chris@16: \param cb circular_buffer to 'steal' value from. Chris@16: \throws Nothing. Chris@16: \par Complexity Chris@16: Constant. Chris@16: */ Chris@16: circular_buffer_space_optimized& operator = (circular_buffer_space_optimized&& cb) BOOST_NOEXCEPT { Chris@16: cb.swap(*this); // now `this` holds `cb` Chris@16: circular_buffer(get_allocator()) // temprary that holds initial `cb` allocator Chris@16: .swap(cb); // makes `cb` empty Chris@16: return *this; Chris@16: } Chris@16: #endif // BOOST_NO_CXX11_RVALUE_REFERENCES Chris@16: Chris@16: Chris@16: //! Assign n items into the space optimized circular buffer. Chris@16: /*! Chris@16: The content of the circular_buffer_space_optimized will be removed and replaced with Chris@16: n copies of the item. Chris@16: \post capacity().%capacity() == n \&\& capacity().min_capacity() == 0 \&\& size() == n \&\& (*this)[0] == Chris@16: item \&\& (*this)[1] == item \&\& ... \&\& (*this) [n - 1] == item

Chris@16: The amount of allocated memory in the internal buffer is n. Chris@16: \param n The number of elements the circular_buffer_space_optimized will be filled with. Chris@16: \param item The element the circular_buffer_space_optimized will be filled with. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the n). Chris@16: \sa \link operator=(const circular_buffer_space_optimized&) operator=\endlink, Chris@16: \link assign(capacity_type, size_type, param_value_type) Chris@16: assign(capacity_type, size_type, const_reference)\endlink, Chris@16: assign(InputIterator, InputIterator), Chris@16: assign(capacity_type, InputIterator, InputIterator) Chris@16: */ Chris@16: void assign(size_type n, param_value_type item) { Chris@16: circular_buffer::assign(n, item); Chris@16: m_capacity_ctrl = capacity_type(n); Chris@16: } Chris@16: Chris@16: //! Assign n items into the space optimized circular buffer specifying the capacity. Chris@16: /*! Chris@16: The capacity of the circular_buffer_space_optimized will be set to the specified value and the Chris@16: content of the circular_buffer_space_optimized will be removed and replaced with n Chris@16: copies of the item. Chris@16: \pre capacity_ctrl.%capacity() >= n Chris@16: \post capacity() == capacity_ctrl \&\& size() == n \&\& (*this)[0] == item \&\& (*this)[1] == item Chris@16: \&\& ... \&\& (*this) [n - 1] == item

Chris@16: The amount of allocated memory will be max[n, capacity_ctrl.min_capacity()]. Chris@16: \param capacity_ctrl The new capacity controller. Chris@16: \param n The number of elements the circular_buffer_space_optimized will be filled with. Chris@16: \param item The element the circular_buffer_space_optimized will be filled with. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the n). Chris@16: \sa \link operator=(const circular_buffer_space_optimized&) operator=\endlink, Chris@16: \link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink, Chris@16: assign(InputIterator, InputIterator), Chris@16: assign(capacity_type, InputIterator, InputIterator) Chris@16: */ Chris@16: void assign(capacity_type capacity_ctrl, size_type n, param_value_type item) { Chris@16: BOOST_CB_ASSERT(capacity_ctrl.capacity() >= n); // check for new capacity lower than n Chris@16: circular_buffer::assign((std::max)(capacity_ctrl.min_capacity(), n), n, item); Chris@16: m_capacity_ctrl = capacity_ctrl; Chris@16: } Chris@16: Chris@16: //! Assign a copy of the range into the space optimized circular buffer. Chris@16: /*! Chris@16: The content of the circular_buffer_space_optimized will be removed and replaced with copies of Chris@16: elements from the specified range. Chris@16: \pre Valid range [first, last).
Chris@16: first and last have to meet the requirements of Chris@16: InputIterator. Chris@16: \post capacity().%capacity() == std::distance(first, last) \&\& capacity().min_capacity() == 0 \&\& Chris@16: size() == std::distance(first, last) \&\& (*this)[0]== *first \&\& (*this)[1] == *(first + 1) \&\& ... Chris@16: \&\& (*this)[std::distance(first, last) - 1] == *(last - 1)

Chris@16: The amount of allocated memory in the internal buffer is std::distance(first, last). Chris@16: \param first The beginning of the range to be copied. Chris@16: \param last The end of the range to be copied. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept and Chris@16: InputIterator is a move iterator. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the std::distance(first, last)). Chris@16: \sa \link operator=(const circular_buffer_space_optimized&) operator=\endlink, Chris@16: \link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink, Chris@16: \link assign(capacity_type, size_type, param_value_type) Chris@16: assign(capacity_type, size_type, const_reference)\endlink, Chris@16: assign(capacity_type, InputIterator, InputIterator) Chris@16: */ Chris@16: template Chris@16: void assign(InputIterator first, InputIterator last) { Chris@16: circular_buffer::assign(first, last); Chris@16: m_capacity_ctrl = capacity_type(circular_buffer::capacity()); Chris@16: } Chris@16: Chris@16: //! Assign a copy of the range into the space optimized circular buffer specifying the capacity. Chris@16: /*! Chris@16: The capacity of the circular_buffer_space_optimized will be set to the specified value and the Chris@16: content of the circular_buffer_space_optimized will be removed and replaced with copies of Chris@16: elements from the specified range. Chris@16: \pre Valid range [first, last).
Chris@16: first and last have to meet the requirements of Chris@16: InputIterator. Chris@16: \post capacity() == capacity_ctrl \&\& size() \<= std::distance(first, last) \&\& Chris@16: (*this)[0]== *(last - capacity) \&\& (*this)[1] == *(last - capacity + 1) \&\& ... \&\& Chris@16: (*this)[capacity - 1] == *(last - 1)

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

The amount of allocated memory in the internal buffer is Chris@16: max[std::distance(first, last), capacity_ctrl.min_capacity()]. Chris@16: \param capacity_ctrl The new capacity controller. Chris@16: \param first The beginning of the range to be copied. Chris@16: \param last The end of the range to be copied. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept and Chris@16: InputIterator is a move iterator. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in std::distance(first, last); in Chris@16: min[capacity_ctrl.%capacity(), std::distance(first, last)] if the InputIterator Chris@16: is a RandomAccessIterator). Chris@16: \sa \link operator=(const circular_buffer_space_optimized&) operator=\endlink, Chris@16: \link assign(size_type, param_value_type) assign(size_type, const_reference)\endlink, Chris@16: \link assign(capacity_type, size_type, param_value_type) Chris@16: assign(capacity_type, size_type, const_reference)\endlink, Chris@16: assign(InputIterator, InputIterator) Chris@16: */ Chris@16: template Chris@16: void assign(capacity_type capacity_ctrl, InputIterator first, InputIterator last) { Chris@16: m_capacity_ctrl = capacity_ctrl; Chris@16: circular_buffer::assign(capacity_ctrl, first, last); Chris@16: } Chris@16: Chris@16: //! Swap the contents of two space-optimized circular-buffers. Chris@16: /*! Chris@16: \post this contains elements of cb and vice versa; the capacity and the amount of Chris@16: allocated memory in the internal buffer of this equal to the capacity and the amount of Chris@16: allocated memory of cb and vice versa. Chris@16: \param cb The circular_buffer_space_optimized whose content will be swapped. Chris@16: \throws Nothing. Chris@16: \par Exception Safety Chris@16: No-throw. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators of both circular_buffer_space_optimized containers. (On the other Chris@16: hand the iterators still point to the same elements but within another container. If you want to rely on Chris@16: this feature you have to turn the __debug_support off by defining macro BOOST_CB_DISABLE_DEBUG, Chris@16: otherwise an assertion will report an error if such invalidated iterator is used.) Chris@16: \par Complexity Chris@16: Constant (in the size of the circular_buffer_space_optimized). Chris@16: \sa swap(circular_buffer&, circular_buffer&), Chris@16: swap(circular_buffer_space_optimized&, circular_buffer_space_optimized&) Chris@16: Chris@16: Chris@16: */ Chris@16: // Note link does not work right. Asked on Doxygen forum for advice 23 May 2103. Chris@16: Chris@16: void swap(circular_buffer_space_optimized& cb) BOOST_NOEXCEPT { Chris@16: std::swap(m_capacity_ctrl, cb.m_capacity_ctrl); Chris@16: circular_buffer::swap(cb); Chris@16: } Chris@16: Chris@16: //! Insert a new element at the end of the space optimized circular buffer. Chris@16: /*! Chris@16: \post if capacity().%capacity() > 0 then back() == item
Chris@16: If the circular_buffer_space_optimized is full, the first element will be removed. If the Chris@16: capacity is 0, nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param item The element to be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link push_front() push_front(const_reference)\endlink, pop_back(), Chris@16: pop_front() Chris@16: */ Chris@16: void push_back(param_value_type item) { Chris@16: check_low_capacity(); Chris@16: circular_buffer::push_back(item); Chris@16: } Chris@16: Chris@16: //! Insert a new element at the end of the space optimized circular buffer. Chris@16: /*! Chris@16: \post if capacity().%capacity() > 0 then back() == item
Chris@16: If the circular_buffer_space_optimized is full, the first element will be removed. If the Chris@16: capacity is 0, nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param item The element to be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link push_front() push_front(const_reference)\endlink, pop_back(), Chris@16: pop_front() Chris@16: */ Chris@16: void push_back(rvalue_type item) { Chris@16: check_low_capacity(); Chris@16: circular_buffer::push_back(boost::move(item)); Chris@16: } Chris@16: Chris@16: //! Insert a new element at the end of the space optimized circular buffer. Chris@16: /*! Chris@16: \post if capacity().%capacity() > 0 then back() == item
Chris@16: If the circular_buffer_space_optimized is full, the first element will be removed. If the Chris@16: capacity is 0, nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T() throws. Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link push_front() push_front(const_reference)\endlink, pop_back(), Chris@16: pop_front() Chris@16: */ Chris@16: void push_back() { Chris@16: check_low_capacity(); Chris@16: circular_buffer::push_back(); Chris@16: } Chris@16: Chris@16: //! Insert a new element at the beginning of the space optimized circular buffer. Chris@16: /*! Chris@16: \post if capacity().%capacity() > 0 then front() == item
Chris@16: If the circular_buffer_space_optimized is full, the last element will be removed. If the Chris@16: capacity is 0, nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param item The element to be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link push_back() push_back(const_reference)\endlink, pop_back(), Chris@16: pop_front() Chris@16: */ Chris@16: void push_front(param_value_type item) { Chris@16: check_low_capacity(); Chris@16: circular_buffer::push_front(item); Chris@16: } Chris@16: Chris@16: //! Insert a new element at the beginning of the space optimized circular buffer. Chris@16: /*! Chris@16: \post if capacity().%capacity() > 0 then front() == item
Chris@16: If the circular_buffer_space_optimized is full, the last element will be removed. If the Chris@16: capacity is 0, nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param item The element to be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link push_back() push_back(const_reference)\endlink, pop_back(), Chris@16: pop_front() Chris@16: */ Chris@16: void push_front(rvalue_type item) { Chris@16: check_low_capacity(); Chris@16: circular_buffer::push_front(boost::move(item)); Chris@16: } Chris@16: Chris@16: //! Insert a new element at the beginning of the space optimized circular buffer. Chris@16: /*! Chris@16: \post if capacity().%capacity() > 0 then front() == item
Chris@16: If the circular_buffer_space_optimized is full, the last element will be removed. If the Chris@16: capacity is 0, nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T() throws. Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link push_back() push_back(const_reference)\endlink, pop_back(), Chris@16: pop_front() Chris@16: */ Chris@16: void push_front() { Chris@16: check_low_capacity(); Chris@16: circular_buffer::push_front(); Chris@16: } Chris@16: Chris@16: //! Remove the last element from the space optimized circular buffer. Chris@16: /*! Chris@16: \pre !empty() Chris@16: \post The last element is removed from the circular_buffer_space_optimized.

Chris@16: The amount of allocated memory in the internal buffer may be predictively decreased. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa pop_front(), \link push_back() push_back(const_reference)\endlink, Chris@16: \link push_front() push_front(const_reference)\endlink Chris@16: */ Chris@16: void pop_back() { Chris@16: circular_buffer::pop_back(); Chris@16: check_high_capacity(); Chris@16: } Chris@16: Chris@16: //! Remove the first element from the space optimized circular buffer. Chris@16: /*! Chris@16: \pre !empty() Chris@16: \post The first element is removed from the circular_buffer_space_optimized.

Chris@16: The amount of allocated memory in the internal buffer may be predictively decreased. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa pop_back(), \link push_back() push_back(const_reference)\endlink, Chris@16: \link push_front() push_front(const_reference)\endlink Chris@16: */ Chris@16: void pop_front() { Chris@16: circular_buffer::pop_front(); Chris@16: check_high_capacity(); Chris@16: } Chris@16: Chris@16: //! Insert an element at the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The item will be inserted at the position pos.
Chris@16: If the circular_buffer_space_optimized is full, the first element will be overwritten. If Chris@16: the circular_buffer_space_optimized is full and the pos points to Chris@16: begin(), then the item will not be inserted. If the capacity is 0, Chris@16: nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position where the item will be inserted. Chris@16: \param item The element to be inserted. Chris@16: \return Iterator to the inserted element or begin() if the item is not inserted. (See Chris@16: the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: Whatever T::operator = (const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator), Chris@16: \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink, Chris@16: \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: iterator insert(iterator pos, param_value_type item) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(); Chris@16: return circular_buffer::insert(begin() + index, item); Chris@16: } Chris@16: Chris@16: //! Insert an element at the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The item will be inserted at the position pos.
Chris@16: If the circular_buffer_space_optimized is full, the first element will be overwritten. If Chris@16: the circular_buffer_space_optimized is full and the pos points to Chris@16: begin(), then the item will not be inserted. If the capacity is 0, Chris@16: nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position where the item will be inserted. Chris@16: \param item The element to be inserted. Chris@16: \return Iterator to the inserted element or begin() if the item is not inserted. (See Chris@16: the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator), Chris@16: \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink, Chris@16: \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: iterator insert(iterator pos, rvalue_type item) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(); Chris@16: return circular_buffer::insert(begin() + index, boost::move(item)); Chris@16: } Chris@16: Chris@16: //! Insert an element at the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The item will be inserted at the position pos.
Chris@16: If the circular_buffer_space_optimized is full, the first element will be overwritten. If Chris@16: the circular_buffer_space_optimized is full and the pos points to Chris@16: begin(), then the item will not be inserted. If the capacity is 0, Chris@16: nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position where the item will be inserted. Chris@16: \return Iterator to the inserted element or begin() if the item is not inserted. (See Chris@16: the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T() throws. Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator), Chris@16: \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink, Chris@16: \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: iterator insert(iterator pos) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(); Chris@16: return circular_buffer::insert(begin() + index); Chris@16: } Chris@16: Chris@16: //! Insert n copies of the item at the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The number of min[n, (pos - begin()) + reserve()] elements will be inserted at the position Chris@16: pos.
The number of min[pos - begin(), max[0, n - reserve()]] elements will Chris@16: be overwritten at the beginning of the circular_buffer_space_optimized.
(See Chris@16: Example for the explanation.)

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position where the items will be inserted. Chris@16: \param n The number of items the to be inserted. Chris@16: \param item The element whose copies will be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: Whatever T::operator = (const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in min[capacity().%capacity(), size() + n]). Chris@16: \par Example Chris@16: Consider a circular_buffer_space_optimized with the capacity of 6 and the size of 4. Its Chris@16: internal buffer may look like the one below.

Chris@16: |1|2|3|4| | |
Chris@16: p ___^

After inserting 5 elements at the position p:

Chris@16: insert(p, (size_t)5, 0);

actually only 4 elements get inserted and elements Chris@16: 1 and 2 are overwritten. This is due to the fact the insert operation preserves Chris@16: the capacity. After insertion the internal buffer looks like this:

|0|0|0|0|3|4|
Chris@16:
For comparison if the capacity would not be preserved the internal buffer would then result in Chris@16: |1|2|0|0|0|0|0|3|4|. Chris@16: \sa \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator), Chris@16: \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink, Chris@16: \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: void insert(iterator pos, size_type n, param_value_type item) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(n); Chris@16: circular_buffer::insert(begin() + index, n, item); Chris@16: } Chris@16: Chris@16: //! Insert the range [first, last) at the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end.
Valid range [first, last) where first and last meet the Chris@16: requirements of an InputIterator. Chris@16: \post Elements from the range Chris@16: [first + max[0, distance(first, last) - (pos - begin()) - reserve()], last) will be Chris@16: inserted at the position pos.
The number of min[pos - begin(), max[0, Chris@16: distance(first, last) - reserve()]] elements will be overwritten at the beginning of the Chris@16: circular_buffer_space_optimized.
(See Example for the explanation.)

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position where the range will be inserted. Chris@16: \param first The beginning of the range to be inserted. Chris@16: \param last The end of the range to be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in [size() + std::distance(first, last)]; in Chris@16: min[capacity().%capacity(), size() + std::distance(first, last)] if the Chris@16: InputIterator is a Chris@16: RandomAccessIterator). Chris@16: \par Example Chris@16: Consider a circular_buffer_space_optimized with the capacity of 6 and the size of 4. Its Chris@16: internal buffer may look like the one below.

Chris@16: |1|2|3|4| | |
Chris@16: p ___^

After inserting a range of elements at the position p:

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

Chris@16: actually only elements 6, 7, 8 and 9 from the Chris@16: specified range get inserted and elements 1 and 2 are overwritten. This is due Chris@16: to the fact the insert operation preserves the capacity. After insertion the internal buffer looks like Chris@16: this:

|6|7|8|9|3|4|

For comparison if the capacity would not be preserved the Chris@16: internal buffer would then result in |1|2|5|6|7|8|9|3|4|. Chris@16: \sa \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink, Chris@16: \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, \link rinsert(iterator, param_value_type) Chris@16: rinsert(iterator, value_type)\endlink, \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: template Chris@16: void insert(iterator pos, InputIterator first, InputIterator last) { Chris@16: insert(pos, first, last, is_integral()); Chris@16: } Chris@16: Chris@16: //! Insert an element before the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The item will be inserted before the position pos.
Chris@16: If the circular_buffer_space_optimized is full, the last element will be overwritten. If the Chris@16: circular_buffer_space_optimized is full and the pos points to Chris@16: end(), then the item will not be inserted. If the capacity is 0, Chris@16: nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position before which the item will be inserted. Chris@16: \param item The element to be inserted. Chris@16: \return Iterator to the inserted element or end() if the item is not inserted. (See Chris@16: the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: Whatever T::operator = (const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator), Chris@16: \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink, Chris@16: \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: iterator rinsert(iterator pos, param_value_type item) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(); Chris@16: return circular_buffer::rinsert(begin() + index, item); Chris@16: } Chris@16: Chris@16: //! Insert an element before the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The item will be inserted before the position pos.
Chris@16: If the circular_buffer_space_optimized is full, the last element will be overwritten. If the Chris@16: circular_buffer_space_optimized is full and the pos points to Chris@16: end(), then the item will not be inserted. If the capacity is 0, Chris@16: nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position before which the item will be inserted. Chris@16: \param item The element to be inserted. Chris@16: \return Iterator to the inserted element or end() if the item is not inserted. (See Chris@16: the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator), Chris@16: \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink, Chris@16: \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: iterator rinsert(iterator pos, rvalue_type item) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(); Chris@16: return circular_buffer::rinsert(begin() + index, boost::move(item)); Chris@16: } Chris@16: Chris@16: //! Insert an element before the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The item will be inserted before the position pos.
Chris@16: If the circular_buffer_space_optimized is full, the last element will be overwritten. If the Chris@16: circular_buffer_space_optimized is full and the pos points to Chris@16: end(), then the item will not be inserted. If the capacity is 0, Chris@16: nothing will be inserted.

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position before which the item will be inserted. Chris@16: \return Iterator to the inserted element or end() if the item is not inserted. (See Chris@16: the Effect.) Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T() throws. Chris@16: Whatever T::T(const T&) throws or nothing if T::T(T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator), Chris@16: \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink, Chris@16: \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: iterator rinsert(iterator pos) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(); Chris@16: return circular_buffer::rinsert(begin() + index); Chris@16: } Chris@16: Chris@16: //! Insert n copies of the item before the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end. Chris@16: \post The number of min[n, (end() - pos) + reserve()] elements will be inserted before the Chris@16: position pos.
The number of min[end() - pos, max[0, n - reserve()]] elements Chris@16: will be overwritten at the end of the circular_buffer_space_optimized.
(See Chris@16: Example for the explanation.)

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position where the items will be inserted. Chris@16: \param n The number of items the to be inserted. Chris@16: \param item The element whose copies will be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: Whatever T::operator = (const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in min[capacity().%capacity(), size() + n]). Chris@16: \par Example Chris@16: Consider a circular_buffer_space_optimized with the capacity of 6 and the size of 4. Its Chris@16: internal buffer may look like the one below.

Chris@16: |1|2|3|4| | |
Chris@16: p ___^

After inserting 5 elements before the position p:

Chris@16: rinsert(p, (size_t)5, 0);

actually only 4 elements get inserted and elements Chris@16: 3 and 4 are overwritten. This is due to the fact the rinsert operation preserves Chris@16: the capacity. After insertion the internal buffer looks like this:

|1|2|0|0|0|0|
Chris@16:
For comparison if the capacity would not be preserved the internal buffer would then result in Chris@16: |1|2|0|0|0|0|0|3|4|. Chris@16: \sa \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink, Chris@16: rinsert(iterator, InputIterator, InputIterator), Chris@16: \link insert(iterator, param_value_type) insert(iterator, value_type)\endlink, Chris@16: \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: void rinsert(iterator pos, size_type n, param_value_type item) { Chris@16: size_type index = pos - begin(); Chris@16: check_low_capacity(n); Chris@16: circular_buffer::rinsert(begin() + index, n, item); Chris@16: } Chris@16: Chris@16: //! Insert the range [first, last) before the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized or its Chris@16: end.
Chris@16: Valid range [first, last) where first and last meet the Chris@16: requirements of an InputIterator. Chris@16: \post Elements from the range Chris@16: [first, last - max[0, distance(first, last) - (end() - pos) - reserve()]) will be inserted Chris@16: before the position pos.
The number of min[end() - pos, max[0, Chris@16: distance(first, last) - reserve()]] elements will be overwritten at the end of the Chris@16: circular_buffer.
(See Example for the explanation.)

Chris@16: The amount of allocated memory in the internal buffer may be predictively increased. Chris@16: \param pos An iterator specifying the position where the range will be inserted. Chris@16: \param first The beginning of the range to be inserted. Chris@16: \param last The end of the range to be inserted. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::T(const T&) throws. Chris@16: Whatever T::operator = (const T&) throws. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in [size() + std::distance(first, last)]; in Chris@16: min[capacity().%capacity(), size() + std::distance(first, last)] if the Chris@16: InputIterator is a Chris@16: RandomAccessIterator). Chris@16: \par Example Chris@16: Consider a circular_buffer_space_optimized with the capacity of 6 and the size of 4. Its Chris@16: internal buffer may look like the one below.

Chris@16: |1|2|3|4| | |
Chris@16: p ___^

After inserting a range of elements before the position p:

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

Chris@16: actually only elements 5, 6, 7 and 8 from the Chris@16: specified range get inserted and elements 3 and 4 are overwritten. This is due Chris@16: to the fact the rinsert operation preserves the capacity. After insertion the internal buffer looks like Chris@16: this:

|1|2|5|6|7|8|

For comparison if the capacity would not be preserved the Chris@16: internal buffer would then result in |1|2|5|6|7|8|9|3|4|. Chris@16: \sa \link rinsert(iterator, param_value_type) rinsert(iterator, value_type)\endlink, Chris@16: \link rinsert(iterator, size_type, param_value_type) Chris@16: rinsert(iterator, size_type, value_type)\endlink, \link insert(iterator, param_value_type) Chris@16: insert(iterator, value_type)\endlink, \link insert(iterator, size_type, param_value_type) Chris@16: insert(iterator, size_type, value_type)\endlink, Chris@16: insert(iterator, InputIterator, InputIterator) Chris@16: */ Chris@16: template Chris@16: void rinsert(iterator pos, InputIterator first, InputIterator last) { Chris@16: rinsert(pos, first, last, is_integral()); Chris@16: } Chris@16: Chris@16: //! Remove an element at the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized (but not Chris@16: an end()). Chris@16: \post The element at the position pos is removed.

Chris@16: The amount of allocated memory in the internal buffer may be predictively decreased. Chris@16: \param pos An iterator pointing at the element to be removed. Chris@16: \return Iterator to the first element remaining beyond the removed element or end() if no such Chris@16: element exists. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::operator = (const T&) throws or Chris@16: nothing if T::operator = (T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa erase(iterator, iterator), rerase(iterator), Chris@16: rerase(iterator, iterator), clear() Chris@16: */ Chris@16: iterator erase(iterator pos) { Chris@16: iterator it = circular_buffer::erase(pos); Chris@16: size_type index = it - begin(); Chris@16: check_high_capacity(); Chris@16: return begin() + index; Chris@16: } Chris@16: Chris@16: //! Erase the range [first, last). Chris@16: /*! Chris@16: \pre Valid range [first, last). Chris@16: \post The elements from the range [first, last) are removed. (If first == last Chris@16: nothing is removed.)

Chris@16: The amount of allocated memory in the internal buffer may be predictively decreased. Chris@16: \param first The beginning of the range to be removed. Chris@16: \param last The end of the range to be removed. Chris@16: \return Iterator to the first element remaining beyond the removed elements or end() if no such Chris@16: element exists. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::operator = (const T&) throws or Chris@16: nothing if T::operator = (T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \sa erase(iterator), rerase(iterator), rerase(iterator, iterator), Chris@16: clear() Chris@16: */ Chris@16: iterator erase(iterator first, iterator last) { Chris@16: iterator it = circular_buffer::erase(first, last); Chris@16: size_type index = it - begin(); Chris@16: check_high_capacity(); Chris@16: return begin() + index; Chris@16: } Chris@16: Chris@16: //! Remove an element at the specified position. Chris@16: /*! Chris@16: \pre pos is a valid iterator pointing to the circular_buffer_space_optimized (but not Chris@16: an end()).

Chris@16: The amount of allocated memory in the internal buffer may be predictively decreased. Chris@16: \post The element at the position pos is removed. Chris@16: \param pos An iterator pointing at the element to be removed. Chris@16: \return Iterator to the first element remaining in front of the removed element or begin() if no Chris@16: such element exists. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::operator = (const T&) throws or Chris@16: nothing if T::operator = (T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \note Basically there is no difference between erase(iterator) and this method. It is implemented Chris@16: only for consistency with the base circular_buffer. Chris@16: \sa erase(iterator), erase(iterator, iterator), Chris@16: rerase(iterator, iterator), clear() Chris@16: */ Chris@16: iterator rerase(iterator pos) { Chris@16: iterator it = circular_buffer::rerase(pos); Chris@16: size_type index = it - begin(); Chris@16: check_high_capacity(); Chris@16: return begin() + index; Chris@16: } Chris@16: Chris@16: //! Erase the range [first, last). Chris@16: /*! Chris@16: \pre Valid range [first, last). Chris@16: \post The elements from the range [first, last) are removed. (If first == last Chris@16: nothing is removed.)

Chris@16: The amount of allocated memory in the internal buffer may be predictively decreased. Chris@16: \param first The beginning of the range to be removed. Chris@16: \param last The end of the range to be removed. Chris@16: \return Iterator to the first element remaining in front of the removed elements or begin() if no Chris@16: such element exists. Chris@16: \throws "An allocation error" if memory is exhausted (std::bad_alloc if the standard allocator is Chris@16: used). Chris@16: Whatever T::operator = (const T&) throws or Chris@16: nothing if T::operator = (T&&) is noexcept. Chris@16: \par Exception Safety Chris@16: Basic. Chris@16: \par Iterator Invalidation Chris@16: Invalidates all iterators pointing to the circular_buffer_space_optimized (except iterators Chris@16: equal to end()). Chris@16: \par Complexity Chris@16: Linear (in the size of the circular_buffer_space_optimized). Chris@16: \note Basically there is no difference between erase(iterator, iterator) and this method. It is Chris@16: implemented only for consistency with the base Chris@16: . Chris@16: \sa erase(iterator), erase(iterator, iterator), rerase(iterator), Chris@16: clear() Chris@16: */ Chris@16: iterator rerase(iterator first, iterator last) { Chris@16: iterator it = circular_buffer::rerase(first, last); Chris@16: size_type index = it - begin(); Chris@16: check_high_capacity(); Chris@16: return begin() + index; Chris@16: } Chris@16: Chris@16: //! Remove all stored elements from the space optimized circular buffer. Chris@16: /*! Chris@16: \post size() == 0

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