diff --git a/include/boost/container/flat_map.hpp b/include/boost/container/flat_map.hpp index 60c7f28..ca2181d 100644 --- a/include/boost/container/flat_map.hpp +++ b/include/boost/container/flat_map.hpp @@ -31,6 +31,10 @@ #include #include +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) +#include +#endif + namespace boost { namespace container { @@ -223,6 +227,39 @@ class flat_map BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty flat_map using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin() ,il.end()). + //! + //! Complexity: Linear in N if the range [il.begin(), il.end()) is already sorted using + //! comp and otherwise N logN, where N is last - first. + flat_map(std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(true, il.begin(), il.end(), comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_map 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. + //! + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate and must be + //! unique values. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + flat_map(ordered_unique_range_t, std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(ordered_range, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } +#endif + //! Effects: Copy constructs a flat_map. //! //! Complexity: Linear in x.size(). @@ -286,6 +323,16 @@ class flat_map BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) { m_flat_tree = boost::move(x.m_flat_tree); return *this; } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assign elements from il to *this + flat_map& operator=(std::initializer_list il) + { + this->clear(); + this->insert(il.begin(), il.end()); + return *this; + } +#endif + //! Effects: Returns a copy of the Allocator that //! was passed to the object's constructor. //! @@ -728,6 +775,34 @@ class flat_map void insert(ordered_unique_range_t, InputIterator first, InputIterator last) { m_flat_tree.insert_unique(ordered_unique_range, first, last); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: 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. + //! + //! Complexity: At most N log(size()+N) (N is the distance from il.first() to il.end()) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + void insert(std::initializer_list il) + { m_flat_tree.insert_unique(il.begin(), il.end()); } + + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate and must be + //! unique values. + //! + //! Effects: 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. This + //! function is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + //! + //! Note: Non-standard extension. + void insert(ordered_unique_range_t, std::initializer_list il) + { m_flat_tree.insert_unique(ordered_unique_range, il.begin(), il.end()); } +#endif + //! Effects: Erases the element pointed to by p. //! //! Returns: Returns an iterator pointing to the element immediately @@ -1123,6 +1198,37 @@ class flat_multimap BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Constructs an empty flat_map using the specified comparison object and + //! allocator, and inserts elements from the range [il.begin(), il.end()). + //! + //! Complexity: Linear in N if the range [il.begin(), il.end()) is already sorted using + //! comp and otherwise N logN, where N is last - first. + flat_multimap(std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type()) + : m_flat_tree(false, il.begin(), il.end(), comp, container_detail::force(a)) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } + + //! Effects: Constructs an empty flat_multimap using the specified comparison object and + //! allocator, and inserts elements from the ordered range [il.begin(), il.end()). This function + //! is more efficient than the normal range creation for ordered ranges. + //! + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate. + //! + //! Complexity: Linear in N. + //! + //! Note: Non-standard extension. + flat_multimap(ordered_range_t, std::initializer_list il, const Compare& comp = Compare(), + const allocator_type& a = allocator_type()) + : m_flat_tree(ordered_range, il.begin(), il.end(), comp, a) + { + //Allocator type must be std::pair + BOOST_STATIC_ASSERT((container_detail::is_same, typename Allocator::value_type>::value)); + } +#endif + //! Effects: Copy constructs a flat_multimap. //! //! Complexity: Linear in x.size(). @@ -1179,6 +1285,18 @@ class flat_multimap BOOST_CONTAINER_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value) { m_flat_tree = boost::move(x.m_flat_tree); return *this; } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: Assign content of il to *this + //! + //! Complexity: Linear in il.size(). + flat_multimap& operator=(std::initializer_list il) + { + this->clear(); + this->insert(il.begin(), il.end()); + return *this; + } +#endif + //! Effects: Returns a copy of the Allocator that //! was passed to the object's constructor. //! @@ -1549,6 +1667,32 @@ class flat_multimap void insert(ordered_range_t, InputIterator first, InputIterator last) { m_flat_tree.insert_equal(ordered_range, first, last); } +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + //! Effects: inserts each element from the range [il.begin(), il.end()) . + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + void insert(std::initializer_list il) + { m_flat_tree.insert_equal(il.begin(), il.end()); } + + //! Requires: [il.begin(), il.end()) must be ordered according to the predicate. + //! + //! Effects: 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. This + //! function is more efficient than the normal range creation for ordered ranges. + //! + //! Complexity: At most N log(size()+N) (N is the distance from first to last) + //! search time plus N*size() insertion time. + //! + //! Note: If an element is inserted it might invalidate elements. + //! + //! Note: Non-standard extension. + void insert(ordered_range_t, std::initializer_list il) + { m_flat_tree.insert_equal(ordered_range, il.begin(), il.end()); } +#endif + //! Effects: Erases the element pointed to by p. //! //! Returns: Returns an iterator pointing to the element immediately diff --git a/test/flat_map_test.cpp b/test/flat_map_test.cpp index a2feb0e..b5d983c 100644 --- a/test/flat_map_test.cpp +++ b/test/flat_map_test.cpp @@ -389,6 +389,38 @@ int test_map_variants() return 0; } +template +bool test_support_for_initialization_list_for() +{ +#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) + const std::initializer_list> il + = {std::make_pair(1, 2), std::make_pair(3, 4)}; + + const FlatMapType expected(il.begin(), il.end()); + { + const FlatMapType sil = il; + if (sil != expected) + return false; + + const FlatMapType sil_ordered(ordered_unique_range_t{}, il); + if(sil_ordered != expected) + return false; + + FlatMapType sil_assign = {std::make_pair(99, 100)}; + sil_assign = il; + if(sil_assign != expected) + return false; + } + { + FlatMapType sil; + sil.insert(il); + if(sil != expected) + return false; + } + return true; +#endif + return true; +} int main() { @@ -436,6 +468,12 @@ int main() return 1; } + if(!test_support_for_initialization_list_for >()) + return 1; + + if(!test_support_for_initialization_list_for >()) + return 1; + //////////////////////////////////// // Emplace testing ////////////////////////////////////