mirror of
https://github.com/boostorg/circular_buffer.git
synced 2026-02-10 23:32:21 +00:00
consistent usage of capacity_type
[SVN r3716]
This commit is contained in:
@@ -379,7 +379,8 @@ public:
|
||||
/*!
|
||||
\param index The position of the element.
|
||||
\return A const reference to the element at the <code>index</code> position.
|
||||
\throws std::out_of_range when the <code>index</code> is invalid (when <code>index >= size()</code>).
|
||||
\throws <code>std::out_of_range</code> when the <code>index</code> is invalid (when
|
||||
<code>index >= size()</code>).
|
||||
\par Complexity
|
||||
Constant (in the size of the <code>circular_buffer</code>).
|
||||
\par Exception Safety
|
||||
@@ -397,7 +398,8 @@ public:
|
||||
/*!
|
||||
\param index The position of the element.
|
||||
\return A const reference to the element at the <code>index</code> position.
|
||||
\throws std::out_of_range when the <code>index</code> is invalid (when <code>index >= size()</code>).
|
||||
\throws <code>std::out_of_range</code> when the <code>index</code> is invalid (when
|
||||
<code>index >= size()</code>).
|
||||
\par Complexity
|
||||
Constant (in the size of the <code>circular_buffer</code>).
|
||||
\par Exception Safety
|
||||
@@ -742,7 +744,7 @@ public:
|
||||
Does not invalidate any iterators.
|
||||
\sa <code>reserve()</code>, <code>size()</code>, <code>max_size()</code>, <code>set_capacity()</code>
|
||||
*/
|
||||
size_type capacity() const { return m_end - m_buff; }
|
||||
capacity_type capacity() const { return m_end - m_buff; }
|
||||
|
||||
//! Change the capacity of the <code>circular_buffer</code>.
|
||||
/*!
|
||||
@@ -761,7 +763,7 @@ public:
|
||||
Invalidates all iterators pointing to the <code>circular_buffer</code>.
|
||||
\sa <code>rset_capacity()</code>, <code>resize()</code>
|
||||
*/
|
||||
void set_capacity(size_type new_capacity) {
|
||||
void set_capacity(capacity_type new_capacity) {
|
||||
if (new_capacity == capacity())
|
||||
return;
|
||||
pointer buff = allocate(new_capacity);
|
||||
@@ -824,7 +826,7 @@ public:
|
||||
Invalidates all iterators pointing to the <code>circular_buffer</code>.
|
||||
\sa <code>set_capacity()</code>, <code>rresize()</code>
|
||||
*/
|
||||
void rset_capacity(size_type new_capacity) {
|
||||
void rset_capacity(capacity_type new_capacity) {
|
||||
if (new_capacity == capacity())
|
||||
return;
|
||||
pointer buff = allocate(new_capacity);
|
||||
@@ -1403,8 +1405,8 @@ public:
|
||||
//! Insert <code>n</code> copies of the <code>item</code> at the specified position.
|
||||
/*!
|
||||
\pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
|
||||
\post The number of <code>min(n, (pos - begin()) + reserve())</code> elements will be inserted at the position
|
||||
<code>pos</code>.<br>The number of <code>min(pos - begin(), max(0, n - reserve()))</code> elements will
|
||||
\post The number of <code>min[n, (pos - begin()) + reserve()]</code> elements will be inserted at the position
|
||||
<code>pos</code>.<br>The number of <code>min[pos - begin(), max[0, n - reserve()]]</code> elements will
|
||||
be overwritten at the beginning of the <code>circular_buffer</code>.<br>(See Example for the
|
||||
explanation.)
|
||||
\param pos An iterator specifying the position where the <code>item</code>s will be inserted.
|
||||
@@ -1451,9 +1453,9 @@ public:
|
||||
Valid range <code>[first, last)</code> where <code>first</code> and <code>last</code> meet the
|
||||
requirements of an <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
|
||||
\post Elements from the range
|
||||
<code>[first + max(0, distance(first, last) - (pos - begin()) - reserve()), last)</code> will be
|
||||
inserted at the position <code>pos</code>.<br>The number of <code>min(pos - begin(), max(0,
|
||||
distance(first, last) - reserve()))</code> elements will be overwritten at the beginning of the
|
||||
<code>[first + max[0, distance(first, last) - (pos - begin()) - reserve()], last)</code> will be
|
||||
inserted at the position <code>pos</code>.<br>The number of <code>min[pos - begin(), max[0,
|
||||
distance(first, last) - reserve()]]</code> elements will be overwritten at the beginning of the
|
||||
<code>circular_buffer</code>.<br>(See Example for the explanation.)
|
||||
\param pos An iterator specifying the position where the range will be inserted.
|
||||
\param first The beginning of the range to be inserted.
|
||||
@@ -1561,8 +1563,8 @@ public:
|
||||
//! Insert <code>n</code> copies of the <code>item</code> before the specified position.
|
||||
/*!
|
||||
\pre <code>pos</code> is a valid iterator pointing to the <code>circular_buffer</code> or its end.
|
||||
\post The number of <code>min(n, (end() - pos) + reserve())</code> elements will be inserted before the
|
||||
position <code>pos</code>.<br>The number of <code>min(end() - pos, max(0, n - reserve()))</code> elements
|
||||
\post The number of <code>min[n, (end() - pos) + reserve()]</code> elements will be inserted before the
|
||||
position <code>pos</code>.<br>The number of <code>min[end() - pos, max[0, n - reserve()]]</code> elements
|
||||
will be overwritten at the end of the <code>circular_buffer</code>.<br>(See Example for the explanation.)
|
||||
\param pos An iterator specifying the position where the <code>item</code>s will be inserted.
|
||||
\param n The number of <code>item</code>s the to be inserted.
|
||||
@@ -1600,9 +1602,9 @@ public:
|
||||
Valid range <code>[first, last)</code> where <code>first</code> and <code>last</code> meet the
|
||||
requirements of an <a href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a>.
|
||||
\post Elements from the range
|
||||
<code>[first, last - max(0, distance(first, last) - (end() - pos) - reserve()))</code> will be inserted
|
||||
before the position <code>pos</code>.<br>The number of <code>min(end() - pos, max(0,
|
||||
distance(first, last) - reserve()))</code> elements will be overwritten at the end of the
|
||||
<code>[first, last - max[0, distance(first, last) - (end() - pos) - reserve()])</code> will be inserted
|
||||
before the position <code>pos</code>.<br>The number of <code>min[end() - pos, max[0,
|
||||
distance(first, last) - reserve()]]</code> elements will be overwritten at the end of the
|
||||
<code>circular_buffer</code>.<br>(See Example for the explanation.)
|
||||
\param pos An iterator specifying the position where the range will be inserted.
|
||||
\param first The beginning of the range to be inserted.
|
||||
@@ -2215,10 +2217,10 @@ private:
|
||||
template <class ForwardIterator>
|
||||
void insert(const iterator& pos, ForwardIterator first, ForwardIterator last, const std::forward_iterator_tag&) {
|
||||
BOOST_CB_ASSERT(std::distance(first, last) >= 0); // check for wrong range
|
||||
difference_type n = std::distance(first, last);
|
||||
size_type n = std::distance(first, last);
|
||||
if (n == 0)
|
||||
return;
|
||||
difference_type copy = capacity() - (end() - pos);
|
||||
size_type copy = capacity() - (end() - pos);
|
||||
if (copy == 0)
|
||||
return;
|
||||
if (n > copy) {
|
||||
|
||||
@@ -77,6 +77,9 @@ struct iterator_wrapper {
|
||||
mutable Iterator m_it;
|
||||
explicit iterator_wrapper(Iterator it) : m_it(it) {}
|
||||
Iterator operator () () const { return m_it++; }
|
||||
private:
|
||||
iterator_wrapper(const iterator_wrapper<Iterator>&); // do not generate
|
||||
iterator_wrapper<Iterator>& operator = (const iterator_wrapper<Iterator>&); // do not generate
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -88,6 +91,9 @@ struct item_wrapper {
|
||||
Value m_item;
|
||||
explicit item_wrapper(Value item) : m_item(item) {}
|
||||
Pointer operator () () const { return &m_item; }
|
||||
private:
|
||||
item_wrapper(const item_wrapper<Pointer, Value>&); // do not generate
|
||||
item_wrapper<Pointer, Value>& operator = (const item_wrapper<Pointer, Value>&); // do not generate
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -106,7 +112,8 @@ struct assign_n {
|
||||
uninitialized_fill_n(p, m_n, m_item, m_alloc);
|
||||
}
|
||||
private:
|
||||
assign_n& operator = (const assign_n&); // do not generate
|
||||
assign_n(const assign_n<Value, Alloc>&); // do not generate
|
||||
assign_n<Value, Alloc>& operator = (const assign_n<Value, Alloc>&); // do not generate
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -124,15 +131,20 @@ struct assign_range {
|
||||
uninitialized_copy(m_first, m_last, p, m_alloc);
|
||||
}
|
||||
private:
|
||||
assign_range& operator = (const assign_range&); // do not generate
|
||||
assign_range(const assign_range<Iterator, Alloc>&); // do not generate
|
||||
assign_range<Iterator, Alloc>& operator = (const assign_range<Iterator, Alloc>&); // do not generate
|
||||
};
|
||||
|
||||
/*!
|
||||
\struct capacity_control
|
||||
\brief Capacity controller of the space optimized circular buffer.
|
||||
*/
|
||||
#if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
||||
template <class Size, class T, class Alloc>
|
||||
#else
|
||||
template <class Size>
|
||||
struct capacity_control {
|
||||
#endif
|
||||
class capacity_control {
|
||||
|
||||
//! The capacity of the space optimized circular buffer.
|
||||
Size m_capacity;
|
||||
@@ -140,6 +152,14 @@ struct capacity_control {
|
||||
//! The lowest guaranteed capacity of the adapted circular buffer.
|
||||
Size m_min_capacity;
|
||||
|
||||
#if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
||||
friend circular_buffer_space_optimized<T, Alloc>;
|
||||
#else
|
||||
template <class T, class Alloc> friend class circular_buffer_space_optimized;
|
||||
#endif
|
||||
|
||||
public:
|
||||
|
||||
//! Constructor.
|
||||
capacity_control(Size capacity, Size min_capacity = 0)
|
||||
: m_capacity(capacity), m_min_capacity(min_capacity) {
|
||||
@@ -149,6 +169,15 @@ struct capacity_control {
|
||||
// Default copy constructor.
|
||||
|
||||
// Default assign operator.
|
||||
|
||||
// Get the capacity of the space optimized circular buffer.
|
||||
Size capacity() const { return m_capacity; }
|
||||
|
||||
// Get the minimal capacity of the space optimized circular buffer.
|
||||
Size min_capacity() const { return m_min_capacity; }
|
||||
|
||||
//! Size operator - returns the capacity of the space optimized circular buffer.
|
||||
operator Size() const { return m_capacity; }
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -393,8 +422,6 @@ private:
|
||||
return lhs.m_it < rhs.m_it;
|
||||
else if (rdiff == 0)
|
||||
return rhs.m_end;
|
||||
else
|
||||
return false;
|
||||
} else if (ldiff == 0) {
|
||||
if (rdiff < 0)
|
||||
return !lhs.m_end;
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace boost {
|
||||
For detailed documentation of the space_optimized_circular_buffer visit:
|
||||
http://www.boost.org/libs/circular_buffer/doc/circular_buffer_adaptor.html
|
||||
*/
|
||||
template<class T, class Alloc>
|
||||
template <class T, class Alloc>
|
||||
class circular_buffer_space_optimized : private circular_buffer<T, Alloc> {
|
||||
public:
|
||||
// Typedefs
|
||||
@@ -59,6 +59,7 @@ public:
|
||||
typedef typename circular_buffer<T, Alloc>::param_value_type param_value_type;
|
||||
typedef typename circular_buffer<T, Alloc>::return_value_type return_value_type;
|
||||
|
||||
#if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
||||
//! Capacity controller of the space optimized circular buffer.
|
||||
/*!
|
||||
<p><pre>
|
||||
@@ -77,7 +78,12 @@ public:
|
||||
which ensures compatibility of creating an instance of the
|
||||
<code>circular_buffer_space_optimized</code> with other STL containers.</p>
|
||||
*/
|
||||
typedef cb_details::capacity_control<size_type, T, Alloc> capacity_type;
|
||||
#else
|
||||
/*! \cond */
|
||||
typedef cb_details::capacity_control<size_type> capacity_type;
|
||||
/*! \endcond */
|
||||
#endif
|
||||
|
||||
// Inherited
|
||||
|
||||
@@ -118,32 +124,8 @@ public:
|
||||
//! See the circular_buffer source documentation.
|
||||
size_type reserve() const { return capacity() - size(); }
|
||||
|
||||
//! Return the minimal guaranteed amount of allocated memory.
|
||||
/*!
|
||||
The allocated memory will never drop under this value.
|
||||
*/
|
||||
size_type min_capacity() const { return m_capacity_ctrl.m_min_capacity; }
|
||||
|
||||
//! Change the minimal guaranteed amount of allocated memory.
|
||||
/*!
|
||||
\pre <code>(*this).capacity() >= new_min_capacity</code>
|
||||
\post <code>(*this).min_capacity() == new_min_capacity</code>
|
||||
Allocates memory specified by the <code>new_min_capacity</code> parameter.
|
||||
\note It is considered as a bug if the precondition is not met (i.e. if
|
||||
<code>new_min_capacity > (*this).capacity()</code>) and an assertion
|
||||
will be invoked in the debug mode.
|
||||
*/
|
||||
void set_min_capacity(size_type new_min_capacity) {
|
||||
BOOST_CB_ASSERT(capacity() >= new_min_capacity); // check for too large new min_capacity
|
||||
m_capacity_ctrl.m_min_capacity = new_min_capacity;
|
||||
if (new_min_capacity > circular_buffer<T, Alloc>::capacity())
|
||||
circular_buffer<T, Alloc>::set_capacity(new_min_capacity);
|
||||
else
|
||||
check_high_capacity();
|
||||
}
|
||||
|
||||
//! See the circular_buffer source documentation.
|
||||
size_type capacity() const { return m_capacity_ctrl.m_capacity; }
|
||||
const capacity_type& capacity() const { return m_capacity_ctrl; }
|
||||
|
||||
#if defined(BOOST_CB_TEST)
|
||||
|
||||
@@ -156,18 +138,25 @@ public:
|
||||
|
||||
#endif // #if defined(BOOST_CB_TEST)
|
||||
|
||||
//! See the circular_buffer source documentation.
|
||||
//! TODO Change the minimal guaranteed amount of allocated memory.
|
||||
/*!
|
||||
\pre <code>(*this).capacity() >= new_min_capacity</code>
|
||||
\post <code>(*this).min_capacity() == new_min_capacity</code>
|
||||
Allocates memory specified by the <code>new_min_capacity</code> parameter.
|
||||
\note It is considered as a bug if the precondition is not met (i.e. if
|
||||
<code>new_min_capacity > (*this).capacity()</code>) and an assertion
|
||||
will be invoked in the debug mode.
|
||||
|
||||
\pre <code>min_capacity() <= new_capacity</code>
|
||||
\note It is considered as a bug if the precondition is not met (i.e. if
|
||||
<code>new_capacity > min_capacity()</code>) and an assertion
|
||||
will be invoked in the debug mode.
|
||||
*/
|
||||
void set_capacity(size_type new_capacity) {
|
||||
BOOST_CB_ASSERT(new_capacity >= min_capacity()); // check for too low new capacity
|
||||
if (new_capacity < circular_buffer<T, Alloc>::capacity())
|
||||
circular_buffer<T, Alloc>::set_capacity(new_capacity);
|
||||
m_capacity_ctrl.m_capacity = new_capacity;
|
||||
void set_capacity(const capacity_type& new_capacity) {
|
||||
m_capacity_ctrl = new_capacity;
|
||||
if (new_capacity.capacity() < circular_buffer<T, Alloc>::capacity())
|
||||
circular_buffer<T, Alloc>::set_capacity(new_capacity.capacity());
|
||||
set_min_capacity(new_capacity.min_capacity());
|
||||
}
|
||||
|
||||
//! See the circular_buffer source documentation.
|
||||
@@ -188,11 +177,11 @@ public:
|
||||
<code>new_capacity > min_capacity()</code>) and an assertion
|
||||
will be invoked in the debug mode.
|
||||
*/
|
||||
void rset_capacity(size_type new_capacity) {
|
||||
BOOST_CB_ASSERT(new_capacity >= min_capacity()); // check for too low new capacity
|
||||
if (new_capacity < circular_buffer<T, Alloc>::capacity())
|
||||
circular_buffer<T, Alloc>::rset_capacity(new_capacity);
|
||||
m_capacity_ctrl.m_capacity = new_capacity;
|
||||
void rset_capacity(const capacity_type& new_capacity) {
|
||||
m_capacity_ctrl = new_capacity;
|
||||
if (new_capacity.capacity() < circular_buffer<T, Alloc>::capacity())
|
||||
circular_buffer<T, Alloc>::rset_capacity(new_capacity.capacity());
|
||||
set_min_capacity(new_capacity.min_capacity());
|
||||
}
|
||||
|
||||
//! See the circular_buffer source documentation.
|
||||
@@ -575,6 +564,14 @@ public:
|
||||
private:
|
||||
// Helper methods
|
||||
|
||||
//! Change the minimal guaranteed amount of allocated memory.
|
||||
void set_min_capacity(size_type new_min_capacity) {
|
||||
if (new_min_capacity > circular_buffer<T, Alloc>::capacity())
|
||||
circular_buffer<T, Alloc>::set_capacity(new_min_capacity);
|
||||
else
|
||||
check_high_capacity();
|
||||
}
|
||||
|
||||
//! Ensure the reserve for possible growth up.
|
||||
size_type ensure_reserve(size_type new_capacity, size_type size) const {
|
||||
if (size + new_capacity / 5 >= new_capacity)
|
||||
@@ -608,8 +605,8 @@ private:
|
||||
size_type new_capacity = circular_buffer<T, Alloc>::capacity();
|
||||
while (new_capacity / 3 >= size()) { // (new_capacity / 3) -> avoid oscillations
|
||||
new_capacity /= 2;
|
||||
if (new_capacity <= min_capacity()) {
|
||||
new_capacity = min_capacity();
|
||||
if (new_capacity <= m_capacity_ctrl.m_min_capacity) {
|
||||
new_capacity = m_capacity_ctrl.m_min_capacity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user