add std::initializer_list support to flat_set and flat_multiset

This commit is contained in:
Robert Matusewicz
2014-08-28 13:14:59 +02:00
parent e2b815c196
commit cbe191b5e3
2 changed files with 157 additions and 0 deletions

View File

@@ -28,6 +28,9 @@
#include <boost/move/utility.hpp>
#include <boost/move/detail/move_helpers.hpp>
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
#include <initializer_list>
#endif
namespace boost {
namespace container {
@@ -145,6 +148,37 @@ class flat_set
: base_t(ordered_range, first, last, comp, a)
{}
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! <b>Effects</b>: Constructs an empty container using the specified comparison object and
//! allocator, and inserts elements from the range [il.begin(), il.end()).
//!
//! <b>Complexity</b>: Linear in N if the range [il.begin(), il.end()) is already sorted using
//! comp and otherwise N logN, where N is il.begin() - il.end().
flat_set(std::initializer_list<value_type> il, const Compare& comp = Compare(),
const allocator_type& a = allocator_type())
: base_t(true, il.begin(), il.end(), comp, a)
{
}
//! <b>Effects</b>: Constructs an empty container using the specified comparison object and
//! allocator, and inserts elements from the ordered unique range [il.begin(), il.end()). This function
//! is more efficient than the normal range creation for ordered ranges.
//!
//! <b>Requires</b>: [il.begin(), il.end()) must be ordered according to the predicate and must be
//! unique values.
//!
//! <b>Complexity</b>: Linear in N.
//!
//! <b>Note</b>: Non-standard extension.
flat_set(ordered_unique_range_t, std::initializer_list<value_type> il,
const Compare& comp = Compare(), const allocator_type& a = allocator_type())
: base_t(ordered_range, il.begin(), il.end(), comp, a)
{
}
#endif
//! <b>Effects</b>: Copy constructs the container.
//!
//! <b>Complexity</b>: Linear in x.size().
@@ -192,6 +226,18 @@ class flat_set
BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value)
{ return static_cast<flat_set&>(this->base_t::operator=(boost::move(static_cast<base_t&>(x)))); }
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! <b>Effects</b>: Copy all elements from il to *this.
//!
//! <b>Complexity</b>: Linear in il.size().
flat_set& operator=(std::initializer_list<value_type> il)
{
this->clear();
this->insert(il.begin(), il.end());
return *this;
}
#endif
#ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
//! <b>Effects</b>: Returns a copy of the Allocator that
//! was passed to the object's constructor.
@@ -504,6 +550,31 @@ class flat_set
void insert(ordered_unique_range_t, InputIterator first, InputIterator last)
{ this->base_t::insert_unique(ordered_unique_range, first, last); }
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! <b>Effects</b>: inserts each element from the range [il.begin(), il.end()) if and only
//! if there is no element with key equivalent to the key of that element.
//!
//! <b>Complexity</b>: At most N log(size()+N) (N is the distance from il.begin() to il.end())
//! search time plus N*size() insertion time.
//!
//! <b>Note</b>: If an element is inserted it might invalidate elements.
void insert(std::initializer_list<value_type> il)
{ this->base_t::insert_unique(il.begin(), il.end()); }
//! <b>Requires</b>: Range [il.begin(), il.end()) must be ordered according to the predicate
//! and must be unique values.
//!
//! <b>Effects</b>: inserts each element from the range [il.begin(), il.end()) .This function
//! is more efficient than the normal range creation for ordered ranges.
//!
//! <b>Complexity</b>: At most N log(size()+N) (N is the distance from il.begin() to il.end())
//! search time plus N*size() insertion time.
//!
//! <b>Note</b>: Non-standard extension. If an element is inserted it might invalidate elements.
void insert(ordered_unique_range_t, std::initializer_list<value_type> il)
{ this->base_t::insert_unique(ordered_unique_range, il.begin(), il.end()); }
#endif
#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//! <b>Effects</b>: Erases the element pointed to by p.
@@ -784,6 +855,20 @@ class flat_multiset
: base_t(ordered_range, first, last, comp, a)
{}
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! @copydoc ::boost::container::flat_set::flat_set(std::initializer_list<value_type>, const Compare& comp, const allocator_type&)
flat_multiset(std::initializer_list<value_type> il, const Compare& comp = Compare(),
const allocator_type& a = allocator_type())
: base_t(false, il.begin(), il.end(), comp, a)
{}
//! @copydoc ::boost::container::flat_set::flat_set(ordered_unique_range_t, std::initializer_list<value_type>, const Compare& comp, const allocator_type&)
flat_multiset(ordered_unique_range_t, std::initializer_list<value_type> il,
const Compare& comp = Compare(), const allocator_type& a = allocator_type())
: base_t(ordered_range, il.begin(), il.end(), comp, a)
{}
#endif
//! @copydoc ::boost::container::flat_set::flat_set(const flat_set &)
flat_multiset(const flat_multiset& x)
: base_t(static_cast<const base_t&>(x))
@@ -813,6 +898,16 @@ class flat_multiset
BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value)
{ return static_cast<flat_multiset&>(this->base_t::operator=(boost::move(static_cast<base_t&>(mx)))); }
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! @copydoc ::boost::container::flat_set::operator=(std::initializer_list<value_type>)
flat_multiset& operator=(std::initializer_list<value_type> il)
{
this->clear();
this->insert(il.begin(), il.end());
return *this;
}
#endif
#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//! @copydoc ::boost::container::flat_set::get_allocator()
@@ -1009,6 +1104,29 @@ class flat_multiset
void insert(ordered_range_t, InputIterator first, InputIterator last)
{ this->base_t::insert_equal(ordered_range, first, last); }
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
//! <b>Effects</b>: inserts each element from the range [il.begin(), il.end()).
//!
//! <b>Complexity</b>: At most N log(size()+N) (N is the distance from first to last)
//! search time plus N*size() insertion time.
//!
//! <b>Note</b>: If an element is inserted it might invalidate elements.
void insert(std::initializer_list<value_type> il)
{ this->base_t::insert_equal(il.begin(), il.end()); }
//! <b>Requires</b>: Range [il.begin(), il.end()) must be ordered according to the predicate.
//!
//! <b>Effects</b>: inserts each element from the range [il.begin(), il.end()). This function
//! is more efficient than the normal range creation for ordered ranges.
//!
//! <b>Complexity</b>: At most N log(size()+N) (N is the distance from il.begin() to il.end())
//! search time plus N*size() insertion time.
//!
//! <b>Note</b>: Non-standard extension. If an element is inserted it might invalidate elements.
void insert(ordered_range_t, std::initializer_list<value_type> il)
{ this->base_t::insert_equal(ordered_range, il.begin(), il.end()); }
#endif
#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
//! @copydoc ::boost::container::flat_set::erase(const_iterator)

View File

@@ -412,6 +412,39 @@ int test_set_variants()
}
template<typename FlatSetType>
bool test_support_for_initialization_list_for()
{
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
const std::initializer_list<int> il
= {1, 2};
const FlatSetType expected(il.begin(), il.end());
{
const FlatSetType sil = il;
if (sil != expected)
return false;
const FlatSetType sil_ordered(ordered_unique_range, il);
if(sil_ordered != expected)
return false;
FlatSetType sil_assign = {99};
sil_assign = il;
if(sil_assign != expected)
return false;
}
{
FlatSetType sil;
sil.insert(il);
if(sil != expected)
return false;
}
return true;
#endif
return true;
}
int main()
{
using namespace boost::container::test;
@@ -468,6 +501,12 @@ int main()
if(!boost::container::test::test_emplace<flat_multiset<test::EmplaceInt>, SetOptions>())
return 1;
if(!test_support_for_initialization_list_for<flat_set<int>>())
return 1;
if(!test_support_for_initialization_list_for<flat_multiset<int>>())
return 1;
////////////////////////////////////
// Allocator propagation testing
////////////////////////////////////