Skip to content

Add emplace_{front,back} methods #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions include/boost/circular_buffer/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,82 @@ private empty_value<Alloc>
BOOST_CATCH_END
}

#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_back_impl(BOOST_FWD_REF(Args) ...args) {
if (full()) {
if (empty())
return;
replace(m_last, ::boost::forward<Args>(args)...);
increment(m_last);
m_first = m_last;
} else {
boost::allocator_construct(alloc(), boost::to_address(m_last), ::boost::forward<Args>(args)...);
increment(m_last);
++m_size;
}
}
#else
template <class V>
void emplace_back_impl(BOOST_FWD_REF(V) value) {
if (full()) {
if (empty())
return;
replace(m_last, ::boost::forward<V>(value));
increment(m_last);
m_first = m_last;
} else {
boost::allocator_construct(alloc(), boost::to_address(m_last), ::boost::forward<V>(value));
increment(m_last);
++m_size;
}
}
#endif

#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_front_impl(BOOST_FWD_REF(Args) ...args) {
BOOST_TRY {
if (full()) {
if (empty())
return;
decrement(m_first);
replace(m_first, ::boost::forward<Args>(args)...);
m_last = m_first;
} else {
decrement(m_first);
boost::allocator_construct(alloc(), boost::to_address(m_first), ::boost::forward<Args>(args)...);
++m_size;
}
} BOOST_CATCH(...) {
increment(m_first);
BOOST_RETHROW
}
BOOST_CATCH_END
}
#else
template <class V>
void emplace_front_impl(BOOST_FWD_REF(V) value) {
BOOST_TRY {
if (full()) {
if (empty())
return;
decrement(m_first);
replace(m_first, ::boost::forward<V>(value));
m_last = m_first;
} else {
decrement(m_first);
boost::allocator_construct(alloc(), boost::to_address(m_first), ::boost::forward<V>(value));
++m_size;
}
} BOOST_CATCH(...) {
increment(m_first);
BOOST_RETHROW
}
BOOST_CATCH_END
}
#endif

public:
//! Insert a new element at the end of the <code>circular_buffer</code>.
/*!
Expand Down Expand Up @@ -1583,6 +1659,64 @@ private empty_value<Alloc>
push_front(boost::move(temp));
}

//! Construct a new element at the end of the <code>circular_buffer</code>.
/*!
\post if <code>capacity() > 0</code> then <code>back() == item</code><br>
If the <code>circular_buffer</code> is full, the first element will be removed. If the capacity is
<code>0</code>, nothing will be inserted.
\param item The element to be inserted.
\throws Whatever <code>T::T(Args...)</code> throws.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
\par Iterator Invalidation
Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>\link push_back() push_back(const_reference)\endlink</code>,
<code>pop_back()</code>, <code>emplace_front()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_back(BOOST_FWD_REF(Args) ...args) {
emplace_back_impl(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_back(BOOST_FWD_REF(V) value) {
emplace_back_impl(::boost::forward<V>(value));
}
#endif

//! Construct a new element at the beginning of the <code>circular_buffer</code>.
/*!
\post if <code>capacity() > 0</code> then <code>back() == item</code><br>
If the <code>circular_buffer</code> is full, the last element will be removed. If the capacity is
<code>0</code>, nothing will be inserted.
\param item The element to be inserted.
\throws Whatever <code>T::T(Args...)</code> throws.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic; no-throw if the operation in the <i>Throws</i> section does not throw anything.
\par Iterator Invalidation
Does not invalidate any iterators with the exception of iterators pointing to the overwritten element.
\par Complexity
Constant (in the size of the <code>circular_buffer</code>).
\sa <code>\link push_front() push_front(const_reference)\endlink</code>,
<code>pop_front()</code>, <code>emplace_back()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_front(BOOST_FWD_REF(Args) ...args) {
emplace_front_impl(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_front(BOOST_FWD_REF(V) value) {
emplace_front_impl(::boost::forward<V>(value));
}
#endif

//! Remove the last element from the <code>circular_buffer</code>.
/*!
\pre <code>!empty()</code>
Expand Down Expand Up @@ -2426,6 +2560,28 @@ private empty_value<Alloc>
#endif
}

#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
/*! INTERNAL ONLY */
template <class ...Args>
void replace(pointer pos, BOOST_FWD_REF(Args) ...args) {
boost::allocator_destroy(alloc(), boost::to_address(pos));
boost::allocator_construct(alloc(), boost::to_address(pos), ::boost::forward<Args>(args)...);
#if BOOST_CB_ENABLE_DEBUG
invalidate_iterators(iterator(this, pos));
#endif
}
#else /* BOOST_NO_CXX11_VARIADIC_TEMPLATES */
/*! INTERNAL ONLY */
template <class V>
void replace(pointer pos, BOOST_FWD_REF(V) value) {
boost::allocator_destroy(alloc(), boost::to_address(pos));
boost::allocator_construct(alloc(), boost::to_address(pos), ::boost::forward<V>(value));
#if BOOST_CB_ENABLE_DEBUG
invalidate_iterators(iterator(this, pos));
#endif
}
#endif /* BOOST_NO_CXX11_VARIADIC_TEMPLATES */

/*! INTERNAL ONLY */
void construct_or_replace(bool construct, pointer pos, param_value_type item) {
if (construct)
Expand Down
70 changes: 70 additions & 0 deletions include/boost/circular_buffer/space_optimized.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,76 @@ public:<br>
circular_buffer<T, Alloc>::push_front();
}

//! Construct a new element at the end of the space optimized circular buffer.
/*!
\post if <code>capacity().%capacity() > 0</code> then <code>back() == item</code><br>
If the <code>circular_buffer_space_optimized</code> is full, the first element will be removed. If the
capacity is <code>0</code>, nothing will be inserted.<br><br>
The amount of allocated memory in the internal buffer may be predictively increased.
\param item The element to be inserted.
\throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(Args...)</code> throws.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer_space_optimized</code> (except iterators
equal to <code>end()</code>).
\par Complexity
Linear (in the size of the <code>circular_buffer_space_optimized</code>).
\sa <code>\link push_front() push_front(const_reference)\endlink</code>, <code>pop_back()</code>,
<code>pop_front()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_back(BOOST_FWD_REF(Args) ...args) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_back(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_back(BOOST_FWD_REF(V) value) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_back(::boost::forward<V>(value));
}
#endif

//! Construct a new element at the beginning of the space optimized circular buffer.
/*!
\post if <code>capacity().%capacity() > 0</code> then <code>front() == item</code><br>
If the <code>circular_buffer_space_optimized</code> is full, the last element will be removed. If the
capacity is <code>0</code>, nothing will be inserted.<br><br>
The amount of allocated memory in the internal buffer may be predictively increased.
\param item The element to be inserted.
\throws "An allocation error" if memory is exhausted (<code>std::bad_alloc</code> if the standard allocator is
used).
Whatever <code>T::T(Args...)</code> throws or nothing if <code>T::T(T&&)</code> is noexcept.
Whatever <code>T::operator = (T&&)</code> throws.
\par Exception Safety
Basic.
\par Iterator Invalidation
Invalidates all iterators pointing to the <code>circular_buffer_space_optimized</code> (except iterators
equal to <code>end()</code>).
\par Complexity
Linear (in the size of the <code>circular_buffer_space_optimized</code>).
\sa <code>\link push_back() push_back(const_reference)\endlink</code>, <code>pop_back()</code>,
<code>pop_front()</code>
*/
#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <class ...Args>
void emplace_front(BOOST_FWD_REF(Args) ...args) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_front(::boost::forward<Args>(args)...);
}
#else
template <class V>
void emplace_front(BOOST_FWD_REF(V) value) {
check_low_capacity();
circular_buffer<T, Alloc>::emplace_front(::boost::forward<V>(value));
}
#endif

//! Remove the last element from the space optimized circular buffer.
/*!
\pre <code>!empty()</code>
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ test-suite "circular_buffer"
[ run space_optimized_test.cpp : : : <threading>single <define>"BOOST_CB_ENABLE_DEBUG=1" : space_optimized_test_dbg ]
[ run soft_iterator_invalidation.cpp : : : <threading>single : ]
[ run constant_erase_test.cpp : : : <threading>single : ]
[ run emplace_test_copies.cpp : : : <threading>single : ]
[ compile bounded_buffer_comparison.cpp : <threading>multi : ]
;
28 changes: 28 additions & 0 deletions test/common.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ void cxx11_allocator_test() {
CB_CONTAINER<MyInteger, cxx11_allocator<MyInteger> > cb(10, 0);
generic_test(cb);
}

#endif

void begin_and_end_test() {
Expand Down Expand Up @@ -1153,6 +1154,32 @@ void pop_back_test() {
generic_test(cb);
}

void emplace_test(){
CB_CONTAINER<MyInteger> cb(4);
cb.emplace_back(4);
cb.emplace_back(5);
cb.emplace_back(6);
cb.emplace_back(7);

BOOST_TEST(cb.size() == 4);
BOOST_TEST(cb.front() == 4);
BOOST_TEST(cb.back() == 7);

cb.emplace_front(3);
cb.emplace_front(2);

BOOST_TEST(cb.front() == 2);
BOOST_TEST(cb.back() == 5);

cb.emplace_front(1);
cb.emplace_front(0);

BOOST_TEST(cb.size() == 4);
BOOST_TEST(*cb.begin() == 0);
BOOST_TEST(cb.front() == 0);
BOOST_TEST(cb.back() == 3);
}

void insert_test() {

CB_CONTAINER<MyInteger> cb1(4);
Expand Down Expand Up @@ -2467,6 +2494,7 @@ void run_common_tests()
swap_test();
push_back_test();
pop_back_test();
emplace_test();
insert_test();
insert_n_test();
insert_range_test();
Expand Down
Loading