diff --git a/libstdc++-v3/include/bits/stl_multiset.h b/libstdc++-v3/include/bits/stl_multiset.h index 57caf6e8cc4..7030f286750 100644 --- a/libstdc++-v3/include/bits/stl_multiset.h +++ b/libstdc++-v3/include/bits/stl_multiset.h @@ -60,6 +60,9 @@ #if __cplusplus >= 201103L #include #endif +#if __glibcxx_ranges_to_container // C++ >= 23 +# include // ranges::begin, ranges::distance etc. +#endif namespace std _GLIBCXX_VISIBILITY(default) { @@ -271,6 +274,25 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER : _M_t(_Key_alloc_type(__a)) { _M_t._M_insert_range_equal(__first, __last); } +#if __glibcxx_ranges_to_container // C++ >= 23 + /** + * @brief Builds a %multiset from a range. + * @since C++23 + */ + template<__detail::__container_compatible_range<_Key> _Rg> + multiset(from_range_t, _Rg&& __rg, + const _Compare& __comp, + const _Alloc& __a = _Alloc()) + : _M_t(__comp, _Key_alloc_type(__a)) + { insert_range(std::forward<_Rg>(__rg)); } + + /// Allocator-extended range constructor. + template<__detail::__container_compatible_range<_Key> _Rg> + multiset(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc()) + : _M_t(_Key_alloc_type(__a)) + { insert_range(std::forward<_Rg>(__rg)); } +#endif + /** * The dtor only erases the elements, and note that if the elements * themselves are pointers, the pointed-to memory is not touched in any @@ -566,6 +588,25 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { this->insert(__l.begin(), __l.end()); } #endif +#if __glibcxx_ranges_to_container // C++ >= 23 + /** + * @brief Inserts a range of elements. + * @since C++23 + * @param __rg An input range of elements that can be converted to + * the set's value type. + */ + template<__detail::__container_compatible_range<_Key> _Rg> + void + insert_range(_Rg&& __rg) + { + auto __first = ranges::begin(__rg); + const auto __last = ranges::end(__rg); + for (; __first != __last; ++__first) + _M_t._M_emplace_equal(*__first); + } +#endif + + #ifdef __glibcxx_node_extract // >= C++17 /// Extract a node. node_type @@ -955,6 +996,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER multiset(initializer_list<_Key>, _Allocator) -> multiset<_Key, less<_Key>, _Allocator>; +#if __glibcxx_ranges_to_container // C++ >= 23 + template>, + __allocator_like _Alloc = std::allocator>> + multiset(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc()) + -> multiset, _Compare, _Alloc>; + + template + multiset(from_range_t, _Rg&&, _Alloc) + -> multiset, less>, _Alloc>; +#endif #endif // deduction guides /** diff --git a/libstdc++-v3/include/bits/stl_set.h b/libstdc++-v3/include/bits/stl_set.h index f32323db368..124237edf8e 100644 --- a/libstdc++-v3/include/bits/stl_set.h +++ b/libstdc++-v3/include/bits/stl_set.h @@ -60,6 +60,9 @@ #if __cplusplus >= 201103L #include #endif +#if __glibcxx_ranges_to_container // C++ >= 23 +# include // ranges::begin, ranges::distance etc. +#endif namespace std _GLIBCXX_VISIBILITY(default) { @@ -275,6 +278,25 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER : _M_t(_Key_alloc_type(__a)) { _M_t._M_insert_range_unique(__first, __last); } +#if __glibcxx_ranges_to_container // C++ >= 23 + /** + * @brief Builds a %set from a range. + * @since C++23 + */ + template<__detail::__container_compatible_range<_Key> _Rg> + set(from_range_t, _Rg&& __rg, + const _Compare& __comp, + const _Alloc& __a = _Alloc()) + : _M_t(__comp, _Key_alloc_type(__a)) + { insert_range(std::forward<_Rg>(__rg)); } + + /// Allocator-extended range constructor. + template<__detail::__container_compatible_range<_Key> _Rg> + set(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc()) + : _M_t(_Key_alloc_type(__a)) + { insert_range(std::forward<_Rg>(__rg)); } +#endif + /** * The dtor only erases the elements, and note that if the elements * themselves are pointers, the pointed-to memory is not touched in any @@ -581,6 +603,28 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { this->insert(__l.begin(), __l.end()); } #endif +#if __glibcxx_ranges_to_container // C++ >= 23 + /** + * @brief Inserts a range of elements. + * @since C++23 + * @param __rg An input range of elements that can be converted to + * the set's value type. + */ + template<__detail::__container_compatible_range<_Key> _Rg> + void + insert_range(_Rg&& __rg) + { + auto __first = ranges::begin(__rg); + const auto __last = ranges::end(__rg); + using _Rv = remove_cvref_t>; + for (; __first != __last; ++__first) + if constexpr (is_same_v<_Rv, _Key>) + _M_t._M_insert_unique(*__first); + else + _M_t._M_emplace_unique(*__first); + } +#endif + #ifdef __glibcxx_node_extract // >= C++17 /// Extract a node. node_type @@ -970,6 +1014,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER set(initializer_list<_Key>, _Allocator) -> set<_Key, less<_Key>, _Allocator>; +#if __glibcxx_ranges_to_container // C++ >= 23 + template>, + __allocator_like _Alloc = std::allocator>> + set(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc()) + -> set, _Compare, _Alloc>; + + template + set(from_range_t, _Rg&&, _Alloc) + -> set, less>, _Alloc>; +#endif #endif // deduction guides /** diff --git a/libstdc++-v3/testsuite/23_containers/multiset/cons/from_range.cc b/libstdc++-v3/testsuite/23_containers/multiset/cons/from_range.cc new file mode 100644 index 00000000000..43821ca556a --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/multiset/cons/from_range.cc @@ -0,0 +1,118 @@ +// { dg-do run { target c++23 } } + +#include +#include +#include +#include +#include +#include +#include +#include + +struct StateCmp { + int state = 7; + + template + bool operator()(T const& l, U const & r) const { + return l > r; + } +}; + +void +test_deduction_guide(long* p) +{ + __gnu_test::test_input_range r(p, p); + std::set s(std::from_range, r); + static_assert(std::is_same_v>); + + StateCmp cmp; + std::set s2(std::from_range, r, cmp); + static_assert(std::is_same_v>); + + using Alloc = __gnu_test::SimpleAllocator; + Alloc alloc; + std::set s3(std::from_range, r, alloc); + static_assert(std::is_same_v, Alloc>>); + + std::set s4(std::from_range, r, cmp, alloc); + static_assert(std::is_same_v>); +} + +template +constexpr bool is_equal(std::less, std::less) +{ return true; } + +constexpr bool is_equal(StateCmp lhs, StateCmp rhs) +{ return lhs.state = rhs.state; } + +template +constexpr void +do_test(Alloc alloc, Cmp cmp) +{ + // The set's value_typ. + using V = typename Alloc::value_type; + + // The range's value_type. + using T = std::ranges::range_value_t; + T a[]{1,2,3,4,5,6,7,8,9,1,2,3,4,5}; + + auto eq = [&](std::set const& l, std::span r) { + if (l.size() != r.size()) + return false; + + std::vector s(r.begin(), r.end()); + std::ranges::sort(s, cmp); + for (auto const& [vl, vr] : std::views::zip(l, s)) { + if (vl != vr) + return false; + } + return true; + }; + + std::set s0(std::from_range, Range(a, a+0)); + VERIFY( s0.empty() ); + VERIFY( s0.get_allocator() == Alloc() ); + VERIFY( is_equal(s0.key_comp(), Cmp()) ); + + std::set s4(std::from_range, Range(a, a+4), cmp); + VERIFY( eq(s4, {a, 4}) ); + VERIFY( s4.get_allocator() == Alloc() ); + VERIFY( is_equal(s4.key_comp(), Cmp()) ); + + std::set s9(std::from_range, Range(a, a+9), alloc); + VERIFY( eq(s9, {a, 9}) ); + VERIFY( s9.get_allocator() == alloc ); + VERIFY( is_equal(s9.key_comp(), cmp) ); + + std::set sr(std::from_range, Range(a, a+14), cmp, alloc); + VERIFY( eq(sr, {a, 9}) ); + VERIFY( sr.get_allocator() == alloc ); + VERIFY( is_equal(sr.key_comp(), cmp) ); +} + +template +void +do_test_ac() +{ + do_test(std::allocator(), std::less()); + do_test(std::allocator(), StateCmp{17}); + do_test(__gnu_test::uneq_allocator(42), std::less()); + do_test(__gnu_test::uneq_allocator(42), StateCmp{17}); +} + +bool +test_ranges() +{ + using namespace __gnu_test; + + do_test_ac>(); + do_test_ac>(); + do_test_ac>(); + + return true; +} + +int main() +{ + test_ranges(); +} diff --git a/libstdc++-v3/testsuite/23_containers/multiset/modifiers/insert/insert_range.cc b/libstdc++-v3/testsuite/23_containers/multiset/modifiers/insert/insert_range.cc new file mode 100644 index 00000000000..26b5eb66a1f --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/multiset/modifiers/insert/insert_range.cc @@ -0,0 +1,76 @@ +// { dg-do run { target c++23 } } + +#include +#include +#include +#include +#include +#include +#include + +struct Gt { + template + bool operator()(T const& l, U const & r) const { + return l > r; + } +}; + +template +constexpr void +do_test(Cmp cmp = Cmp()) +{ + // The range's value_type. + using T = std::ranges::range_value_t; + T a[]{1,2,3,4,5,6,7,8,9,1,2,3,4,5}; + + auto eq = [&](std::multiset const& l, std::span r) { + if (l.size() != r.size()) + return false; + + std::vector s(r.begin(), r.end()); + std::ranges::sort(s, cmp); + for (auto const& [vl, vr] : std::views::zip(l, s)) { + if (vl != vr) + return false; + } + return true; + }; + + std::multiset s; + s.insert_range(Range(a, a+0)); + VERIFY( s.empty() ); + + s.insert_range(Range(a, a+4)); + VERIFY( eq(s, {a, 4}) ); + + s.insert_range(Range(a+4, a+9)); + VERIFY( eq(s, {a, 9}) ); + + s.insert_range(Range(a+9, a+14)); + VERIFY( eq(s, {a, 14}) ); +} + +template +void +do_test_c() +{ + do_test>(); + do_test(); +} + +bool +test_ranges() +{ + using namespace __gnu_test; + + do_test_c>(); + do_test_c>(); + do_test_c>(); + + return true; +} + +int main() +{ + test_ranges(); +} diff --git a/libstdc++-v3/testsuite/23_containers/set/cons/from_range.cc b/libstdc++-v3/testsuite/23_containers/set/cons/from_range.cc new file mode 100644 index 00000000000..869326f8976 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/set/cons/from_range.cc @@ -0,0 +1,117 @@ +// { dg-do run { target c++23 } } + +#include +#include +#include +#include +#include +#include +#include + +struct StateCmp { + int state = 7; + + template + bool operator()(T const& l, U const & r) const { + return l > r; + } +}; + +void +test_deduction_guide(long* p) +{ + __gnu_test::test_input_range r(p, p); + std::set s(std::from_range, r); + static_assert(std::is_same_v>); + + StateCmp cmp; + std::set s2(std::from_range, r, cmp); + static_assert(std::is_same_v>); + + using Alloc = __gnu_test::SimpleAllocator; + Alloc alloc; + std::set s3(std::from_range, r, alloc); + static_assert(std::is_same_v, Alloc>>); + + std::set s4(std::from_range, r, cmp, alloc); + static_assert(std::is_same_v>); +} + +template +constexpr bool is_equal(std::less, std::less) +{ return true; } + +constexpr bool is_equal(StateCmp lhs, StateCmp rhs) +{ return lhs.state = rhs.state; } + +template +constexpr void +do_test(Alloc alloc, Cmp cmp) +{ + // The set's value_typ. + using V = typename Alloc::value_type; + + // The range's value_type. + using T = std::ranges::range_value_t; + T a[]{1,2,3,4,5,6,7,8,9,1,2,3,4,5}; + + auto eq = [&](std::set const& l, std::span r) { + if (l.size() != r.size()) + return false; + + std::vector s(r.begin(), r.end()); + std::ranges::sort(s, cmp); + for (auto const& [vl, vr] : std::views::zip(l, s)) { + if (vl != vr) + return false; + } + return true; + }; + + std::set s0(std::from_range, Range(a, a+0)); + VERIFY( s0.empty() ); + VERIFY( s0.get_allocator() == Alloc() ); + VERIFY( is_equal(s0.key_comp(), Cmp()) ); + + std::set s4(std::from_range, Range(a, a+4), cmp); + VERIFY( eq(s4, {a, 4}) ); + VERIFY( s4.get_allocator() == Alloc() ); + VERIFY( is_equal(s4.key_comp(), Cmp()) ); + + std::set s9(std::from_range, Range(a, a+9), alloc); + VERIFY( eq(s9, {a, 9}) ); + VERIFY( s9.get_allocator() == alloc ); + VERIFY( is_equal(s9.key_comp(), cmp) ); + + std::set sr(std::from_range, Range(a, a+14), cmp, alloc); + VERIFY( eq(sr, {a, 9}) ); + VERIFY( sr.get_allocator() == alloc ); + VERIFY( is_equal(sr.key_comp(), cmp) ); +} + +template +void +do_test_ac() +{ + do_test(std::allocator(), std::less()); + do_test(std::allocator(), StateCmp{17}); + do_test(__gnu_test::uneq_allocator(42), std::less()); + do_test(__gnu_test::uneq_allocator(42), StateCmp{17}); +} + +bool +test_ranges() +{ + using namespace __gnu_test; + + do_test_ac>(); + do_test_ac>(); + do_test_ac>(); + + return true; +} + +int main() +{ + test_ranges(); +} diff --git a/libstdc++-v3/testsuite/23_containers/set/modifiers/insert/insert_range.cc b/libstdc++-v3/testsuite/23_containers/set/modifiers/insert/insert_range.cc new file mode 100644 index 00000000000..087b7c046cd --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/set/modifiers/insert/insert_range.cc @@ -0,0 +1,79 @@ +// { dg-do run { target c++23 } } + +#include +#include +#include +#include +#include +#include +#include + +struct Gt { + template + bool operator()(T const& l, U const & r) const { + return l > r; + } +}; + +template +constexpr void +do_test(Cmp cmp = Cmp()) +{ + // The range's value_type. + using T = std::ranges::range_value_t; + T a[]{1,2,3,4,5,6,7,8,9}; + + auto eq = [&](std::set const& l, std::span r) { + if (l.size() != r.size()) + return false; + + std::vector s(r.begin(), r.end()); + std::ranges::sort(s, cmp); + for (auto const& [vl, vr] : std::views::zip(l, s)) { + if (vl != vr) + return false; + } + return true; + }; + + std::set s; + s.insert_range(Range(a, a+0)); + VERIFY( s.empty() ); + + s.insert_range(Range(a, a+4)); + VERIFY( eq(s, {a, 4}) ); + + s.insert_range(Range(a+4, a+7)); + VERIFY( eq(s, {a, 7}) ); + + s.insert_range(Range(a, a+9)); + VERIFY( eq(s, {a, 9}) ); + + s.insert_range(Range(a, a+9)); + VERIFY( eq(s, {a, 9}) ); +} + +template +void +do_test_c() +{ + do_test>(); + do_test(); +} + +bool +test_ranges() +{ + using namespace __gnu_test; + + do_test_c>(); + do_test_c>(); + do_test_c>(); + + return true; +} + +int main() +{ + test_ranges(); +}