// Implementation of the circular buffer adaptor.
// Copyright (c) 2003
// Jan Gaspar, Whitestein Technologies
// Permission to use or copy this software for any purpose is hereby granted
// without fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is granted,
// provided the above notices are retained, and a notice that the code was
// modified is included with the above copyright notice.
// This material is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
#if !defined(BOOST_CIRCULAR_BUFFER_ADAPTOR_HPP)
#define BOOST_CIRCULAR_BUFFER_ADAPTOR_HPP
namespace boost {
/*!
\class circular_buffer_space_optimized
\brief Space optimized circular buffer container adaptor.
\param T The type of the elements stored in the space optimized circular buffer.
\param Alloc The allocator type used for all internal memory management.
\author Jan Gaspar
\version 1.0
\date 2003
For more information how to use the space optimized circular
buffer see the
documentation.
*/
template
class circular_buffer_space_optimized : private circular_buffer {
public:
// Typedefs
typedef typename circular_buffer::value_type value_type;
typedef typename circular_buffer::pointer pointer;
typedef typename circular_buffer::const_pointer const_pointer;
typedef typename circular_buffer::reference reference;
typedef typename circular_buffer::const_reference const_reference;
typedef typename circular_buffer::size_type size_type;
typedef typename circular_buffer::difference_type difference_type;
typedef typename circular_buffer::allocator_type allocator_type;
typedef typename circular_buffer::param_value_type param_value_type;
typedef typename circular_buffer::const_iterator const_iterator;
typedef typename circular_buffer::iterator iterator;
typedef typename circular_buffer::const_reverse_iterator const_reverse_iterator;
typedef typename circular_buffer::reverse_iterator reverse_iterator;
// Inherited
using circular_buffer::get_allocator;
using circular_buffer::begin;
using circular_buffer::end;
using circular_buffer::rbegin;
using circular_buffer::rend;
using circular_buffer::operator[];
using circular_buffer::at;
using circular_buffer::front;
using circular_buffer::back;
using circular_buffer::data;
using circular_buffer::size;
using circular_buffer::max_size;
using circular_buffer::empty;
private:
// Member variables
//! The capacity of the optimized circular buffer.
size_type m_final_capacity;
public:
// Overridden
//! See the circular_buffer source documentation.
bool full() const { return size() == capacity(); }
//! See the circular_buffer source documentation.
size_type capacity() const { return m_final_capacity; }
//! See the circular_buffer source documentation.
void set_capacity(size_type new_capacity) {
if (m_final_capacity > new_capacity)
circular_buffer::set_capacity(new_capacity);
m_final_capacity = new_capacity;
}
//! See the circular_buffer source documentation.
void resize(size_type new_size, param_value_type item = T()) {
if (new_size > size()) {
if (new_size > capacity())
m_final_capacity = new_size;
insert(end(), new_size - size(), item);
} else {
erase(begin(), end() - new_size);
}
}
//! See the circular_buffer source documentation.
explicit circular_buffer_space_optimized(
size_type capacity,
const allocator_type& a = allocator_type())
: circular_buffer(0, a), m_final_capacity(capacity) {}
//! See the circular_buffer source documentation.
circular_buffer_space_optimized(
size_type capacity,
param_value_type item,
const allocator_type& a = allocator_type())
: circular_buffer(capacity, item, a), m_final_capacity(capacity) {}
// Default copy constructor
//! See the circular_buffer source documentation.
template
circular_buffer_space_optimized(
size_type capacity,
InputIterator first,
InputIterator last,
const allocator_type& a = allocator_type())
: circular_buffer(init_capacity(capacity, first, last), first, last, a)
, m_final_capacity(capacity) {}
// Default destructor
// Default assign operator
//! See the circular_buffer source documentation.
void assign(size_type n, param_value_type item) {
if (n > m_final_capacity)
m_final_capacity = n;
circular_buffer::assign(n, item);
}
//! See the circular_buffer source documentation.
template
void assign(InputIterator first, InputIterator last) {
circular_buffer::assign(first, last);
size_type capacity = circular_buffer::capacity();
if (capacity > m_final_capacity)
m_final_capacity = capacity;
}
//! See the circular_buffer source documentation.
void swap(circular_buffer_space_optimized& cb) {
std::swap(m_final_capacity, cb.m_final_capacity);
circular_buffer::swap(cb);
}
//! See the circular_buffer source documentation.
void push_back(param_value_type item) {
check_low_capacity();
circular_buffer::push_back(item);
}
//! See the circular_buffer source documentation.
void push_back() { push_back(value_type()); }
//! See the circular_buffer source documentation.
void push_front(param_value_type item) {
check_low_capacity();
circular_buffer::push_front(item);
}
//! See the circular_buffer source documentation.
void push_front() { push_front(value_type()); }
//! See the circular_buffer source documentation.
void pop_back() {
circular_buffer::pop_back();
check_high_capacity();
}
//! See the circular_buffer source documentation.
void pop_front() {
circular_buffer::pop_front();
check_high_capacity();
}
//! See the circular_buffer source documentation.
iterator insert(iterator pos, param_value_type item) {
size_type index = pos - begin();
check_low_capacity();
return circular_buffer::insert(begin() + index, item);
}
//! See the circular_buffer source documentation.
iterator insert(iterator pos) { return insert(pos, value_type()); }
//! See the circular_buffer source documentation.
void insert(iterator pos, size_type n, param_value_type item) {
size_type index = pos - begin();
check_low_capacity(n);
circular_buffer::insert(begin() + index, n, item);
}
//! See the circular_buffer source documentation.
template
void insert(iterator pos, InputIterator first, InputIterator last) {
insert(pos, first, last, cb_details::cb_iterator_category_traits::tag());
}
//! See the circular_buffer source documentation.
iterator rinsert(iterator pos, param_value_type item) {
size_type index = pos - begin();
check_low_capacity();
return circular_buffer::rinsert(begin() + index, item);
}
//! See the circular_buffer source documentation.
iterator rinsert(iterator pos) { return rinsert(pos, value_type()); }
//! See the circular_buffer source documentation.
void rinsert(iterator pos, size_type n, param_value_type item) {
size_type index = pos - begin();
check_low_capacity(n);
circular_buffer::rinsert(begin() + index, n, item);
}
//! See the circular_buffer source documentation.
template
void rinsert(iterator pos, InputIterator first, InputIterator last) {
rinsert(pos, first, last, cb_details::cb_iterator_category_traits::tag());
}
//! See the circular_buffer source documentation.
iterator erase(iterator pos) {
iterator it = circular_buffer::erase(pos);
size_type index = it - begin();
check_high_capacity();
return begin() + index;
}
//! See the circular_buffer source documentation.
iterator erase(iterator first, iterator last) {
iterator it = circular_buffer::erase(first, last);
size_type index = it - begin();
circular_buffer::set_capacity(size());
return begin() + index;
}
//! See the circular_buffer source documentation.
void clear() { circular_buffer::set_capacity(0); }
private:
// Helper methods
//! Check for low capacity.
/*
\post If the capacity is low it will be increased.
*/
void check_low_capacity() {
if (circular_buffer::full() && size() < m_final_capacity) {
size_type new_capacity = empty() ? 1 : size() << 1; // (size * 2)
if (new_capacity > m_final_capacity)
new_capacity = m_final_capacity;
circular_buffer::set_capacity(new_capacity);
}
}
//! Check for low capacity.
/*
\post If the capacity is low it will be increased.
*/
void check_low_capacity(size_type n) {
size_type new_capacity = size() + n;
if (new_capacity > m_final_capacity)
new_capacity = m_final_capacity;
if (new_capacity > circular_buffer::capacity())
circular_buffer::set_capacity(new_capacity);
}
//! Check for high capacity.
/*
\post If the capacity is high it will be decreased.
*/
void check_high_capacity() {
size_type new_capacity = circular_buffer::capacity() >> 1; // (capacity / 2)
if (new_capacity >= size())
circular_buffer::set_capacity(new_capacity);
}
//! Determine the initial capacity.
template
static size_type init_capacity(size_type capacity, InputIterator first, InputIterator last) {
size_type diff = std::distance(first, last);
return diff > capacity ? capacity : diff;
}
//! Helper insert method.
template
void insert(iterator pos, InputIterator n, InputIterator item, cb_details::cb_int_iterator_tag) {
insert(pos, (size_type)n, item);
}
//! Helper insert method.
template
void insert(iterator pos, InputIterator first, InputIterator last, std::input_iterator_tag) {
size_type index = pos - begin();
check_low_capacity(std::distance(first, last));
circular_buffer::insert(begin() + index, first, last);
}
//! Helper rinsert method.
template
void rinsert(iterator pos, InputIterator n, InputIterator item, cb_details::cb_int_iterator_tag) {
rinsert(pos, (size_type)n, item);
}
//! Helper rinsert method.
template
void rinsert(iterator pos, InputIterator first, InputIterator last, std::input_iterator_tag) {
size_type index = pos - begin();
check_low_capacity(std::distance(first, last));
circular_buffer::rinsert(begin() + index, first, last);
}
};
// Non-member functions
//! Test two space optimized circular buffers for equality.
template
inline bool operator == (const circular_buffer_space_optimized& lhs,
const circular_buffer_space_optimized& rhs) {
return lhs.size() == rhs.size() &&
std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
//! Lexicographical comparison.
template
inline bool operator < (const circular_buffer_space_optimized& lhs,
const circular_buffer_space_optimized& rhs) {
return std::lexicographical_compare(
lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
#if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
//! Test two space optimized circular buffers for non-equality.
template
inline bool operator != (const circular_buffer_space_optimized& lhs,
const circular_buffer_space_optimized& rhs) {
return !(lhs == rhs);
}
//! Lexicographical comparison.
template
inline bool operator > (const circular_buffer_space_optimized& lhs,
const circular_buffer_space_optimized& rhs) {
return rhs < lhs;
}
//! Lexicographical comparison.
template
inline bool operator <= (const circular_buffer_space_optimized& lhs,
const circular_buffer_space_optimized& rhs) {
return !(rhs < lhs);
}
//! Lexicographical comparison.
template
inline bool operator >= (const circular_buffer_space_optimized& lhs,
const circular_buffer_space_optimized& rhs) {
return !(lhs < rhs);
}
//! Swap the contents of two space optimized circular buffers.
template
inline void swap(circular_buffer_space_optimized& lhs,
circular_buffer_space_optimized& rhs) {
lhs.swap(rhs);
}
#endif // #if !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) || defined(BOOST_MSVC)
} // namespace boost
#endif // #if !defined(BOOST_CIRCULAR_BUFFER_ADAPTOR_HPP)