diff --git a/libcxx/docs/ReleaseNotes/20.rst b/libcxx/docs/ReleaseNotes/20.rst index 39546493ae8d6f..84080e7cbafe2c 100644 --- a/libcxx/docs/ReleaseNotes/20.rst +++ b/libcxx/docs/ReleaseNotes/20.rst @@ -43,6 +43,7 @@ Implemented Papers - P2985R0: A type trait for detecting virtual base classes (`Github `__) - ``std::jthread`` and ```` are not guarded behind ``-fexperimental-library`` anymore - P2674R1: A trait for implicit lifetime types (`Github `__) +- P0429R9: A Standard ``flat_map`` is partially implemented and ``flat_map`` is provided (`Github `__) Improvements and New Features ----------------------------- diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv index c64f1c4171fce1..6f1626da73507e 100644 --- a/libcxx/docs/Status/Cxx23Papers.csv +++ b/libcxx/docs/Status/Cxx23Papers.csv @@ -52,7 +52,7 @@ "`P2443R1 `__","``views::chunk_by``","2022-02 (Virtual)","|Complete|","18.0","" "","","","","","" "`P0009R18 `__","mdspan: A Non-Owning Multidimensional Array Reference","2022-07 (Virtual)","|Complete|","18.0","" -"`P0429R9 `__","A Standard ``flat_map``","2022-07 (Virtual)","","","" +"`P0429R9 `__","A Standard ``flat_map``","2022-07 (Virtual)","|In progress|","","" "`P1169R4 `__","``static operator()``","2022-07 (Virtual)","|Complete|","16.0","" "`P1222R4 `__","A Standard ``flat_set``","2022-07 (Virtual)","","","" "`P1223R5 `__","``ranges::find_last()``, ``ranges::find_last_if()``, and ``ranges::find_last_if_not()``","2022-07 (Virtual)","|Complete|","19.0","" diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index bb152af82cad5c..e84a55e25f2fa4 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -358,6 +358,8 @@ set(files __filesystem/recursive_directory_iterator.h __filesystem/space_info.h __filesystem/u8path.h + __flat_map/flat_map.h + __flat_map/sorted_unique.h __format/buffer.h __format/concepts.h __format/container_adaptor.h @@ -959,6 +961,7 @@ set(files ext/hash_set fenv.h filesystem + flat_map float.h format forward_list diff --git a/libcxx/include/__flat_map/flat_map.h b/libcxx/include/__flat_map/flat_map.h new file mode 100644 index 00000000000000..9ca32d5295bd27 --- /dev/null +++ b/libcxx/include/__flat_map/flat_map.h @@ -0,0 +1,1359 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___FLAT_MAP_FLAT_MAP_H +#define _LIBCPP___FLAT_MAP_FLAT_MAP_H + +#include <__algorithm/lexicographical_compare_three_way.h> +#include <__algorithm/ranges_adjacent_find.h> +#include <__algorithm/ranges_equal.h> +#include <__algorithm/ranges_inplace_merge.h> +#include <__algorithm/ranges_lower_bound.h> +#include <__algorithm/ranges_partition_point.h> +#include <__algorithm/ranges_stable_sort.h> +#include <__algorithm/ranges_unique.h> +#include <__algorithm/ranges_upper_bound.h> +#include <__compare/synth_three_way.h> +#include <__concepts/convertible_to.h> +#include <__concepts/swappable.h> +#include <__config> +#include <__flat_map/sorted_unique.h> +#include <__functional/invoke.h> +#include <__functional/is_transparent.h> +#include <__functional/operations.h> +#include <__iterator/concepts.h> +#include <__iterator/distance.h> +#include <__iterator/iterator_traits.h> +#include <__iterator/next.h> +#include <__iterator/ranges_iterator_traits.h> +#include <__iterator/reverse_iterator.h> +#include <__memory/allocator_traits.h> +#include <__memory/uses_allocator.h> +#include <__memory/uses_allocator_construction.h> +#include <__ranges/concepts.h> +#include <__ranges/container_compatible_range.h> +#include <__ranges/drop_view.h> +#include <__ranges/ref_view.h> +#include <__ranges/subrange.h> +#include <__ranges/zip_view.h> +#include <__type_traits/conjunction.h> +#include <__type_traits/container_traits.h> +#include <__type_traits/invoke.h> +#include <__type_traits/is_allocator.h> +#include <__type_traits/is_nothrow_constructible.h> +#include <__type_traits/is_same.h> +#include <__type_traits/maybe_const.h> +#include <__utility/exception_guard.h> +#include <__utility/pair.h> +#include +#include +#include +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +#if _LIBCPP_STD_VER >= 23 + +_LIBCPP_BEGIN_NAMESPACE_STD + +template , + class _KeyContainer = vector<_Key>, + class _MappedContainer = vector<_Tp>> +class flat_map { + template + struct __iterator; + + template + friend class flat_map; + + static_assert(is_same_v<_Key, typename _KeyContainer::value_type>); + static_assert(is_same_v<_Tp, typename _MappedContainer::value_type>); + static_assert(!is_same_v<_KeyContainer, std::vector>, "vector is not a sequence container"); + static_assert(!is_same_v<_MappedContainer, std::vector>, "vector is not a sequence container"); + +public: + // types + using key_type = _Key; + using mapped_type = _Tp; + using value_type = pair; + using key_compare = __type_identity_t<_Compare>; + using reference = pair; + using const_reference = pair; + using size_type = size_t; + using difference_type = ptrdiff_t; + using iterator = __iterator; // see [container.requirements] + using const_iterator = __iterator; // see [container.requirements] + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + using key_container_type = _KeyContainer; + using mapped_container_type = _MappedContainer; + + class value_compare { + private: + key_compare __comp_; + value_compare(key_compare __c) : __comp_(__c) {} + friend flat_map; + + public: + _LIBCPP_HIDE_FROM_ABI bool operator()(const_reference __x, const_reference __y) const { + return __comp_(__x.first, __y.first); + } + }; + + struct containers { + key_container_type keys; + mapped_container_type values; + }; + +private: + template + _LIBCPP_HIDE_FROM_ABI static constexpr bool __allocator_ctor_constraint = + _And, uses_allocator>::value; + + _LIBCPP_HIDE_FROM_ABI static constexpr bool __is_compare_transparent = __is_transparent_v<_Compare, _Compare>; + + template + struct __iterator { + private: + using __key_iterator = ranges::iterator_t; + using __mapped_iterator = ranges::iterator_t<__maybe_const<_Const, mapped_container_type>>; + using __reference = pair, iter_reference_t<__mapped_iterator>>; + + struct __arrow_proxy { + __reference __ref_; + _LIBCPP_HIDE_FROM_ABI __reference* operator->() { return std::addressof(__ref_); } + }; + + __key_iterator __key_iter_; + __mapped_iterator __mapped_iter_; + + friend flat_map; + + public: + using iterator_concept = random_access_iterator_tag; + // `flat_map::iterator` only satisfy "Cpp17InputIterator" named requirements, because + // its `reference` is not a reference type. + // However, to avoid surprising runtime behaviour when it is used with the + // Cpp17 algorithms or operations, iterator_category is set to random_access_iterator_tag. + using iterator_category = random_access_iterator_tag; + using value_type = flat_map::value_type; + using difference_type = flat_map::difference_type; + + _LIBCPP_HIDE_FROM_ABI __iterator() = default; + + _LIBCPP_HIDE_FROM_ABI __iterator(__iterator __i) + requires _Const && convertible_to, __key_iterator> && + convertible_to, __mapped_iterator> + : __key_iter_(std::move(__i.__key_iter_)), __mapped_iter_(std::move(__i.__mapped_iter_)) {} + + _LIBCPP_HIDE_FROM_ABI __iterator(__key_iterator __key_iter, __mapped_iterator __mapped_iter) + : __key_iter_(std::move(__key_iter)), __mapped_iter_(std::move(__mapped_iter)) {} + + _LIBCPP_HIDE_FROM_ABI __reference operator*() const { return __reference(*__key_iter_, *__mapped_iter_); } + _LIBCPP_HIDE_FROM_ABI __arrow_proxy operator->() const { return __arrow_proxy{**this}; } + + _LIBCPP_HIDE_FROM_ABI __iterator& operator++() { + ++__key_iter_; + ++__mapped_iter_; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI __iterator operator++(int) { + __iterator __tmp(*this); + ++*this; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI __iterator& operator--() { + --__key_iter_; + --__mapped_iter_; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI __iterator operator--(int) { + __iterator __tmp(*this); + --*this; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI __iterator& operator+=(difference_type __x) { + __key_iter_ += __x; + __mapped_iter_ += __x; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI __iterator& operator-=(difference_type __x) { + __key_iter_ -= __x; + __mapped_iter_ -= __x; + return *this; + } + + _LIBCPP_HIDE_FROM_ABI __reference operator[](difference_type __n) const { return *(*this + __n); } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) { + return __x.__key_iter_ == __y.__key_iter_; + } + + _LIBCPP_HIDE_FROM_ABI friend bool operator<(const __iterator& __x, const __iterator& __y) { + return __x.__key_iter_ < __y.__key_iter_; + } + + _LIBCPP_HIDE_FROM_ABI friend bool operator>(const __iterator& __x, const __iterator& __y) { return __y < __x; } + + _LIBCPP_HIDE_FROM_ABI friend bool operator<=(const __iterator& __x, const __iterator& __y) { return !(__y < __x); } + + _LIBCPP_HIDE_FROM_ABI friend bool operator>=(const __iterator& __x, const __iterator& __y) { return !(__x < __y); } + + _LIBCPP_HIDE_FROM_ABI friend auto operator<=>(const __iterator& __x, const __iterator& __y) + requires three_way_comparable<__key_iterator> + { + return __x.__key_iter_ <=> __y.__key_iter_; + } + + _LIBCPP_HIDE_FROM_ABI friend __iterator operator+(const __iterator& __i, difference_type __n) { + auto __tmp = __i; + __tmp += __n; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI friend __iterator operator+(difference_type __n, const __iterator& __i) { return __i + __n; } + + _LIBCPP_HIDE_FROM_ABI friend __iterator operator-(const __iterator& __i, difference_type __n) { + auto __tmp = __i; + __tmp -= __n; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI friend difference_type operator-(const __iterator& __x, const __iterator& __y) { + return difference_type(__x.__key_iter_ - __y.__key_iter_); + } + }; + +public: + // [flat.map.cons], construct/copy/destroy + _LIBCPP_HIDE_FROM_ABI flat_map() noexcept( + is_nothrow_default_constructible_v<_KeyContainer> && is_nothrow_default_constructible_v<_MappedContainer> && + is_nothrow_default_constructible_v<_Compare>) + : __containers_(), __compare_() {} + + _LIBCPP_HIDE_FROM_ABI flat_map(const flat_map&) = default; + + _LIBCPP_HIDE_FROM_ABI flat_map(flat_map&& __other) noexcept( + is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_MappedContainer> && + is_nothrow_move_constructible_v<_Compare>) +# if _LIBCPP_HAS_EXCEPTIONS + try +# endif // _LIBCPP_HAS_EXCEPTIONS + : __containers_(std::move(__other.__containers_)), __compare_(std::move(__other.__compare_)) { + __other.clear(); +# if _LIBCPP_HAS_EXCEPTIONS + } catch (...) { + __other.clear(); + // gcc does not like the `throw` keyword in a conditional noexcept function + if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> && + is_nothrow_move_constructible_v<_MappedContainer> && is_nothrow_move_constructible_v<_Compare>)) { + throw; + } +# endif // _LIBCPP_HAS_EXCEPTIONS + } + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(const flat_map& __other, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_tag{}, + __alloc, + __other.__containers_.keys, + __other.__containers_.values, + __other.__compare_) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(flat_map&& __other, const _Allocator& __alloc) +# if _LIBCPP_HAS_EXCEPTIONS + try +# endif // _LIBCPP_HAS_EXCEPTIONS + : flat_map(__ctor_uses_allocator_tag{}, + __alloc, + std::move(__other.__containers_.keys), + std::move(__other.__containers_.values), + std::move(__other.__compare_)) { + __other.clear(); +# if _LIBCPP_HAS_EXCEPTIONS + } catch (...) { + __other.clear(); + throw; +# endif // _LIBCPP_HAS_EXCEPTIONS + } + + _LIBCPP_HIDE_FROM_ABI flat_map( + key_container_type __key_cont, mapped_container_type __mapped_cont, const key_compare& __comp = key_compare()) + : __containers_{.keys = std::move(__key_cont), .values = std::move(__mapped_cont)}, __compare_(__comp) { + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(), + "flat_map keys and mapped containers have different size"); + __sort_and_unique(); + } + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI + flat_map(const key_container_type& __key_cont, const mapped_container_type& __mapped_cont, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont) { + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(), + "flat_map keys and mapped containers have different size"); + __sort_and_unique(); + } + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI + flat_map(const key_container_type& __key_cont, + const mapped_container_type& __mapped_cont, + const key_compare& __comp, + const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont, __comp) { + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(), + "flat_map keys and mapped containers have different size"); + __sort_and_unique(); + } + + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, + key_container_type __key_cont, + mapped_container_type __mapped_cont, + const key_compare& __comp = key_compare()) + : __containers_{.keys = std::move(__key_cont), .values = std::move(__mapped_cont)}, __compare_(__comp) { + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(), + "flat_map keys and mapped containers have different size"); + _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT( + __is_sorted_and_unique(__containers_.keys), "Either the key container is not sorted or it contains duplicates"); + } + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, + const key_container_type& __key_cont, + const mapped_container_type& __mapped_cont, + const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont) { + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(), + "flat_map keys and mapped containers have different size"); + _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT( + __is_sorted_and_unique(__containers_.keys), "Either the key container is not sorted or it contains duplicates"); + } + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, + const key_container_type& __key_cont, + const mapped_container_type& __mapped_cont, + const key_compare& __comp, + const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont, __comp) { + _LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(), + "flat_map keys and mapped containers have different size"); + _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT( + __is_sorted_and_unique(__containers_.keys), "Either the key container is not sorted or it contains duplicates"); + } + + _LIBCPP_HIDE_FROM_ABI explicit flat_map(const key_compare& __comp) : __containers_(), __compare_(__comp) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(const key_compare& __comp, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI explicit flat_map(const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {} + + template + requires __has_input_iterator_category<_InputIterator>::value + _LIBCPP_HIDE_FROM_ABI + flat_map(_InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare()) + : __containers_(), __compare_(__comp) { + insert(__first, __last); + } + + template + requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>) + _LIBCPP_HIDE_FROM_ABI + flat_map(_InputIterator __first, _InputIterator __last, const key_compare& __comp, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) { + insert(__first, __last); + } + + template + requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>) + _LIBCPP_HIDE_FROM_ABI flat_map(_InputIterator __first, _InputIterator __last, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) { + insert(__first, __last); + } + + template <_ContainerCompatibleRange _Range> + _LIBCPP_HIDE_FROM_ABI flat_map(from_range_t __fr, _Range&& __rg) + : flat_map(__fr, std::forward<_Range>(__rg), key_compare()) {} + + template <_ContainerCompatibleRange _Range, class _Allocator> + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(from_range_t, _Range&& __rg, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) { + insert_range(std::forward<_Range>(__rg)); + } + + template <_ContainerCompatibleRange _Range> + _LIBCPP_HIDE_FROM_ABI flat_map(from_range_t, _Range&& __rg, const key_compare& __comp) : flat_map(__comp) { + insert_range(std::forward<_Range>(__rg)); + } + + template <_ContainerCompatibleRange _Range, class _Allocator> + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(from_range_t, _Range&& __rg, const key_compare& __comp, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) { + insert_range(std::forward<_Range>(__rg)); + } + + template + requires __has_input_iterator_category<_InputIterator>::value + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, _InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare()) + : __containers_(), __compare_(__comp) { + insert(sorted_unique, __first, __last); + } + template + requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>) + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, + _InputIterator __first, + _InputIterator __last, + const key_compare& __comp, + const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) { + insert(sorted_unique, __first, __last); + } + + template + requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>) + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, _InputIterator __first, _InputIterator __last, const _Allocator& __alloc) + : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) { + insert(sorted_unique, __first, __last); + } + + _LIBCPP_HIDE_FROM_ABI flat_map(initializer_list __il, const key_compare& __comp = key_compare()) + : flat_map(__il.begin(), __il.end(), __comp) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI + flat_map(initializer_list __il, const key_compare& __comp, const _Allocator& __alloc) + : flat_map(__il.begin(), __il.end(), __comp, __alloc) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(initializer_list __il, const _Allocator& __alloc) + : flat_map(__il.begin(), __il.end(), __alloc) {} + + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, initializer_list __il, const key_compare& __comp = key_compare()) + : flat_map(sorted_unique, __il.begin(), __il.end(), __comp) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI + flat_map(sorted_unique_t, initializer_list __il, const key_compare& __comp, const _Allocator& __alloc) + : flat_map(sorted_unique, __il.begin(), __il.end(), __comp, __alloc) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(sorted_unique_t, initializer_list __il, const _Allocator& __alloc) + : flat_map(sorted_unique, __il.begin(), __il.end(), __alloc) {} + + _LIBCPP_HIDE_FROM_ABI flat_map& operator=(initializer_list __il) { + clear(); + insert(__il); + return *this; + } + + _LIBCPP_HIDE_FROM_ABI flat_map& operator=(const flat_map&) = default; + + _LIBCPP_HIDE_FROM_ABI flat_map& operator=(flat_map&& __other) noexcept( + is_nothrow_move_assignable_v<_KeyContainer> && is_nothrow_move_assignable_v<_MappedContainer> && + is_nothrow_move_assignable_v<_Compare>) { + // No matter what happens, we always want to clear the other container before returning + // since we moved from it + auto __clear_other_guard = std::__make_scope_guard([&]() noexcept { __other.clear() /* noexcept */; }); + { + // If an exception is thrown, we have no choice but to clear *this to preserve invariants + auto __on_exception = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; }); + __containers_ = std::move(__other.__containers_); + __compare_ = std::move(__other.__compare_); + __on_exception.__complete(); + } + return *this; + } + + // iterators + _LIBCPP_HIDE_FROM_ABI iterator begin() noexcept { + return iterator(__containers_.keys.begin(), __containers_.values.begin()); + } + + _LIBCPP_HIDE_FROM_ABI const_iterator begin() const noexcept { + return const_iterator(__containers_.keys.begin(), __containers_.values.begin()); + } + + _LIBCPP_HIDE_FROM_ABI iterator end() noexcept { + return iterator(__containers_.keys.end(), __containers_.values.end()); + } + + _LIBCPP_HIDE_FROM_ABI const_iterator end() const noexcept { + return const_iterator(__containers_.keys.end(), __containers_.values.end()); + } + + _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } + _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() noexcept { return reverse_iterator(begin()); } + _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + + _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const noexcept { return begin(); } + _LIBCPP_HIDE_FROM_ABI const_iterator cend() const noexcept { return end(); } + _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } + _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } + + // [flat.map.capacity], capacity + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool empty() const noexcept { return __containers_.keys.empty(); } + + _LIBCPP_HIDE_FROM_ABI size_type size() const noexcept { return __containers_.keys.size(); } + + _LIBCPP_HIDE_FROM_ABI size_type max_size() const noexcept { + return std::min(__containers_.keys.max_size(), __containers_.values.max_size()); + } + + // [flat.map.access], element access + _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __x) + requires is_constructible_v + { + return try_emplace(__x).first->second; + } + + _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __x) + requires is_constructible_v + { + return try_emplace(std::move(__x)).first->second; + } + + template + requires(__is_compare_transparent && is_constructible_v && is_constructible_v && + !is_convertible_v<_Kp &&, const_iterator> && !is_convertible_v<_Kp &&, iterator>) + _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](_Kp&& __x) { + return try_emplace(std::forward<_Kp>(__x)).first->second; + } + + _LIBCPP_HIDE_FROM_ABI mapped_type& at(const key_type& __x) { + auto __it = find(__x); + if (__it == end()) { + std::__throw_out_of_range("flat_map::at(const key_type&): Key does not exist"); + } + return __it->second; + } + + _LIBCPP_HIDE_FROM_ABI const mapped_type& at(const key_type& __x) const { + auto __it = find(__x); + if (__it == end()) { + std::__throw_out_of_range("flat_map::at(const key_type&) const: Key does not exist"); + } + return __it->second; + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI mapped_type& at(const _Kp& __x) { + auto __it = find(__x); + if (__it == end()) { + std::__throw_out_of_range("flat_map::at(const K&): Key does not exist"); + } + return __it->second; + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI const mapped_type& at(const _Kp& __x) const { + auto __it = find(__x); + if (__it == end()) { + std::__throw_out_of_range("flat_map::at(const K&) const: Key does not exist"); + } + return __it->second; + } + + // [flat.map.modifiers], modifiers + template + requires is_constructible_v, _Args...> + _LIBCPP_HIDE_FROM_ABI pair emplace(_Args&&... __args) { + std::pair __pair(std::forward<_Args>(__args)...); + return __try_emplace(std::move(__pair.first), std::move(__pair.second)); + } + + template + requires is_constructible_v, _Args...> + _LIBCPP_HIDE_FROM_ABI iterator emplace_hint(const_iterator __hint, _Args&&... __args) { + std::pair __pair(std::forward<_Args>(__args)...); + return __try_emplace_hint(__hint, std::move(__pair.first), std::move(__pair.second)).first; + } + + _LIBCPP_HIDE_FROM_ABI pair insert(const value_type& __x) { return emplace(__x); } + + _LIBCPP_HIDE_FROM_ABI pair insert(value_type&& __x) { return emplace(std::move(__x)); } + + _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, const value_type& __x) { + return emplace_hint(__hint, __x); + } + + _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, value_type&& __x) { + return emplace_hint(__hint, std::move(__x)); + } + + template + requires is_constructible_v, _Pp> + _LIBCPP_HIDE_FROM_ABI pair insert(_Pp&& __x) { + return emplace(std::forward<_Pp>(__x)); + } + + template + requires is_constructible_v, _Pp> + _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, _Pp&& __x) { + return emplace_hint(__hint, std::forward<_Pp>(__x)); + } + + template + requires __has_input_iterator_category<_InputIterator>::value + _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last) { + if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) { + __reserve(__last - __first); + } + __append_sort_merge_unique(std::move(__first), std::move(__last)); + } + + template + requires __has_input_iterator_category<_InputIterator>::value + void insert(sorted_unique_t, _InputIterator __first, _InputIterator __last) { + if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) { + __reserve(__last - __first); + } + + __append_sort_merge_unique(std::move(__first), std::move(__last)); + } + + template <_ContainerCompatibleRange _Range> + _LIBCPP_HIDE_FROM_ABI void insert_range(_Range&& __range) { + if constexpr (ranges::sized_range<_Range>) { + __reserve(ranges::size(__range)); + } + + __append_sort_merge_unique(ranges::begin(__range), ranges::end(__range)); + } + + _LIBCPP_HIDE_FROM_ABI void insert(initializer_list __il) { insert(__il.begin(), __il.end()); } + + _LIBCPP_HIDE_FROM_ABI void insert(sorted_unique_t, initializer_list __il) { + insert(sorted_unique, __il.begin(), __il.end()); + } + + _LIBCPP_HIDE_FROM_ABI containers extract() && { + auto __guard = std::__make_scope_guard([&]() noexcept { clear() /* noexcept */; }); + auto __ret = std::move(__containers_); + return __ret; + } + + _LIBCPP_HIDE_FROM_ABI void replace(key_container_type&& __key_cont, mapped_container_type&& __mapped_cont) { + _LIBCPP_ASSERT_VALID_INPUT_RANGE( + __key_cont.size() == __mapped_cont.size(), "flat_map keys and mapped containers have different size"); + + _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT( + __is_sorted_and_unique(__key_cont), "Either the key container is not sorted or it contains duplicates"); + auto __guard = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; }); + __containers_.keys = std::move(__key_cont); + __containers_.values = std::move(__mapped_cont); + __guard.__complete(); + } + + template + requires is_constructible_v + _LIBCPP_HIDE_FROM_ABI pair try_emplace(const key_type& __key, _Args&&... __args) { + return __try_emplace(__key, std::forward<_Args>(__args)...); + } + + template + requires is_constructible_v + _LIBCPP_HIDE_FROM_ABI pair try_emplace(key_type&& __key, _Args&&... __args) { + return __try_emplace(std::move(__key), std::forward<_Args>(__args)...); + } + + template + requires(__is_compare_transparent && is_constructible_v && + is_constructible_v && !is_convertible_v<_Kp &&, const_iterator> && + !is_convertible_v<_Kp &&, iterator>) + _LIBCPP_HIDE_FROM_ABI pair try_emplace(_Kp&& __key, _Args&&... __args) { + return __try_emplace(std::forward<_Kp>(__key), std::forward<_Args>(__args)...); + } + + template + requires is_constructible_v + _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __hint, const key_type& __key, _Args&&... __args) { + return __try_emplace_hint(__hint, __key, std::forward<_Args>(__args)...).first; + } + + template + requires is_constructible_v + _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __hint, key_type&& __key, _Args&&... __args) { + return __try_emplace_hint(__hint, std::move(__key), std::forward<_Args>(__args)...).first; + } + + template + requires __is_compare_transparent && is_constructible_v && is_constructible_v + _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __hint, _Kp&& __key, _Args&&... __args) { + return __try_emplace_hint(__hint, std::forward<_Kp>(__key), std::forward<_Args>(__args)...).first; + } + + template + requires is_assignable_v && is_constructible_v + _LIBCPP_HIDE_FROM_ABI pair insert_or_assign(const key_type& __key, _Mapped&& __obj) { + return __insert_or_assign(__key, std::forward<_Mapped>(__obj)); + } + + template + requires is_assignable_v && is_constructible_v + _LIBCPP_HIDE_FROM_ABI pair insert_or_assign(key_type&& __key, _Mapped&& __obj) { + return __insert_or_assign(std::move(__key), std::forward<_Mapped>(__obj)); + } + + template + requires __is_compare_transparent && is_constructible_v && is_assignable_v && + is_constructible_v + _LIBCPP_HIDE_FROM_ABI pair insert_or_assign(_Kp&& __key, _Mapped&& __obj) { + return __insert_or_assign(std::forward<_Kp>(__key), std::forward<_Mapped>(__obj)); + } + + template + requires is_assignable_v && is_constructible_v + _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __hint, const key_type& __key, _Mapped&& __obj) { + return __insert_or_assign(__hint, __key, std::forward<_Mapped>(__obj)); + } + + template + requires is_assignable_v && is_constructible_v + _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __hint, key_type&& __key, _Mapped&& __obj) { + return __insert_or_assign(__hint, std::move(__key), std::forward<_Mapped>(__obj)); + } + + template + requires __is_compare_transparent && is_constructible_v && is_assignable_v && + is_constructible_v + _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __hint, _Kp&& __key, _Mapped&& __obj) { + return __insert_or_assign(__hint, std::forward<_Kp>(__key), std::forward<_Mapped>(__obj)); + } + + _LIBCPP_HIDE_FROM_ABI iterator erase(iterator __position) { + return __erase(__position.__key_iter_, __position.__mapped_iter_); + } + + _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position) { + return __erase(__position.__key_iter_, __position.__mapped_iter_); + } + + _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __x) { + auto __iter = find(__x); + if (__iter != end()) { + erase(__iter); + return 1; + } + return 0; + } + + template + requires(__is_compare_transparent && !is_convertible_v<_Kp &&, iterator> && + !is_convertible_v<_Kp &&, const_iterator>) + _LIBCPP_HIDE_FROM_ABI size_type erase(_Kp&& __x) { + auto [__first, __last] = equal_range(__x); + auto __res = __last - __first; + erase(__first, __last); + return __res; + } + + _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last) { + auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; }); + auto __key_it = __containers_.keys.erase(__first.__key_iter_, __last.__key_iter_); + auto __mapped_it = __containers_.values.erase(__first.__mapped_iter_, __last.__mapped_iter_); + __on_failure.__complete(); + return iterator(std::move(__key_it), std::move(__mapped_it)); + } + + _LIBCPP_HIDE_FROM_ABI void swap(flat_map& __y) noexcept { + // warning: The spec has unconditional noexcept, which means that + // if any of the following functions throw an exception, + // std::terminate will be called. + // This is discussed in P2767, which hasn't been voted on yet. + ranges::swap(__compare_, __y.__compare_); + ranges::swap(__containers_.keys, __y.__containers_.keys); + ranges::swap(__containers_.values, __y.__containers_.values); + } + + _LIBCPP_HIDE_FROM_ABI void clear() noexcept { + __containers_.keys.clear(); + __containers_.values.clear(); + } + + // observers + _LIBCPP_HIDE_FROM_ABI key_compare key_comp() const { return __compare_; } + _LIBCPP_HIDE_FROM_ABI value_compare value_comp() const { return value_compare(__compare_); } + + _LIBCPP_HIDE_FROM_ABI const key_container_type& keys() const noexcept { return __containers_.keys; } + _LIBCPP_HIDE_FROM_ABI const mapped_container_type& values() const noexcept { return __containers_.values; } + + // map operations + _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __x) { return __find_impl(*this, __x); } + + _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __x) const { return __find_impl(*this, __x); } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI iterator find(const _Kp& __x) { + return __find_impl(*this, __x); + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Kp& __x) const { + return __find_impl(*this, __x); + } + + _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __x) const { return contains(__x) ? 1 : 0; } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI size_type count(const _Kp& __x) const { + return contains(__x) ? 1 : 0; + } + + _LIBCPP_HIDE_FROM_ABI bool contains(const key_type& __x) const { return find(__x) != end(); } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI bool contains(const _Kp& __x) const { + return find(__x) != end(); + } + + _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const key_type& __x) { return __lower_bound(*this, __x); } + + _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __x) const { + return __lower_bound(*this, __x); + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _Kp& __x) { + return __lower_bound(*this, __x); + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _Kp& __x) const { + return __lower_bound(*this, __x); + } + + _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __x) { return __upper_bound(*this, __x); } + + _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __x) const { + return __upper_bound(*this, __x); + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _Kp& __x) { + return __upper_bound(*this, __x); + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _Kp& __x) const { + return __upper_bound(*this, __x); + } + + _LIBCPP_HIDE_FROM_ABI pair equal_range(const key_type& __x) { + return __equal_range_impl(*this, __x); + } + + _LIBCPP_HIDE_FROM_ABI pair equal_range(const key_type& __x) const { + return __equal_range_impl(*this, __x); + } + + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI pair equal_range(const _Kp& __x) { + return __equal_range_impl(*this, __x); + } + template + requires __is_compare_transparent + _LIBCPP_HIDE_FROM_ABI pair equal_range(const _Kp& __x) const { + return __equal_range_impl(*this, __x); + } + + friend _LIBCPP_HIDE_FROM_ABI bool operator==(const flat_map& __x, const flat_map& __y) { + return ranges::equal(__x, __y); + } + + friend _LIBCPP_HIDE_FROM_ABI auto operator<=>(const flat_map& __x, const flat_map& __y) { + return std::lexicographical_compare_three_way( + __x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way); + } + + friend _LIBCPP_HIDE_FROM_ABI void swap(flat_map& __x, flat_map& __y) noexcept { __x.swap(__y); } + +private: + struct __ctor_uses_allocator_tag { + explicit _LIBCPP_HIDE_FROM_ABI __ctor_uses_allocator_tag() = default; + }; + struct __ctor_uses_allocator_empty_tag { + explicit _LIBCPP_HIDE_FROM_ABI __ctor_uses_allocator_empty_tag() = default; + }; + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI + flat_map(__ctor_uses_allocator_tag, + const _Allocator& __alloc, + _KeyCont&& __key_cont, + _MappedCont&& __mapped_cont, + _CompArg&&... __comp) + : __containers_{.keys = std::make_obj_using_allocator( + __alloc, std::forward<_KeyCont>(__key_cont)), + .values = std::make_obj_using_allocator( + __alloc, std::forward<_MappedCont>(__mapped_cont))}, + __compare_(std::forward<_CompArg>(__comp)...) {} + + template + requires __allocator_ctor_constraint<_Allocator> + _LIBCPP_HIDE_FROM_ABI flat_map(__ctor_uses_allocator_empty_tag, const _Allocator& __alloc, _CompArg&&... __comp) + : __containers_{.keys = std::make_obj_using_allocator(__alloc), + .values = std::make_obj_using_allocator(__alloc)}, + __compare_(std::forward<_CompArg>(__comp)...) {} + + _LIBCPP_HIDE_FROM_ABI bool __is_sorted_and_unique(auto&& __key_container) const { + auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) { return !__compare_(__x, __y); }; + return ranges::adjacent_find(__key_container, __greater_or_equal_to) == ranges::end(__key_container); + } + + // This function is only used in constructors. So there is not exception handling in this function. + // If the function exits via an exception, there will be no flat_map object constructed, thus, there + // is no invariant state to preserve + _LIBCPP_HIDE_FROM_ABI void __sort_and_unique() { + auto __zv = ranges::views::zip(__containers_.keys, __containers_.values); + // To be consistent with std::map's behaviour, we use stable_sort instead of sort. + // As a result, if there are duplicated keys, the first value in the original order will be taken. + ranges::stable_sort(__zv, __compare_, [](const auto& __p) -> decltype(auto) { return std::get<0>(__p); }); + auto __dup_start = ranges::unique(__zv, __key_equiv(__compare_)).begin(); + auto __dist = ranges::distance(__zv.begin(), __dup_start); + __containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end()); + __containers_.values.erase(__containers_.values.begin() + __dist, __containers_.values.end()); + } + + template + _LIBCPP_HIDE_FROM_ABI size_type __append(_InputIterator __first, _Sentinel __last) { + size_type __num_of_appended = 0; + for (; __first != __last; ++__first) { + value_type __kv = *__first; + __containers_.keys.insert(__containers_.keys.end(), std::move(__kv.first)); + __containers_.values.insert(__containers_.values.end(), std::move(__kv.second)); + ++__num_of_appended; + } + return __num_of_appended; + } + + template + _LIBCPP_HIDE_FROM_ABI void __append_sort_merge_unique(_InputIterator __first, _Sentinel __last) { + auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; }); + size_t __num_of_appended = __append(std::move(__first), std::move(__last)); + if (__num_of_appended != 0) { + auto __zv = ranges::views::zip(__containers_.keys, __containers_.values); + auto __append_start_offset = __containers_.keys.size() - __num_of_appended; + auto __end = __zv.end(); + auto __compare_key = [this](const auto& __p1, const auto& __p2) { + return __compare_(std::get<0>(__p1), std::get<0>(__p2)); + }; + if constexpr (!_WasSorted) { + ranges::stable_sort(__zv.begin() + __append_start_offset, __end, __compare_key); + } else { + _LIBCPP_ASSERT_SEMANTIC_REQUIREMENT( + __is_sorted_and_unique(__containers_.keys | ranges::views::drop(__append_start_offset)), + "Either the key container is not sorted or it contains duplicates"); + } + ranges::inplace_merge(__zv.begin(), __zv.begin() + __append_start_offset, __end, __compare_key); + + auto __dup_start = ranges::unique(__zv, __key_equiv(__compare_)).begin(); + auto __dist = ranges::distance(__zv.begin(), __dup_start); + __containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end()); + __containers_.values.erase(__containers_.values.begin() + __dist, __containers_.values.end()); + } + __on_failure.__complete(); + } + + template + _LIBCPP_HIDE_FROM_ABI static auto __find_impl(_Self&& __self, const _Kp& __key) { + auto __it = __self.lower_bound(__key); + auto __last = __self.end(); + if (__it == __last || __self.__compare_(__key, __it->first)) { + return __last; + } + return __it; + } + + template + _LIBCPP_HIDE_FROM_ABI static auto __key_equal_range(_Self&& __self, const _Kp& __key) { + auto __it = ranges::lower_bound(__self.__containers_.keys, __key, __self.__compare_); + auto __last = __self.__containers_.keys.end(); + if (__it == __last || __self.__compare_(__key, *__it)) { + return std::make_pair(__it, __it); + } + return std::make_pair(__it, std::next(__it)); + } + + template + _LIBCPP_HIDE_FROM_ABI static auto __equal_range_impl(_Self&& __self, const _Kp& __key) { + auto [__key_first, __key_last] = __key_equal_range(__self, __key); + + const auto __make_mapped_iter = [&](const auto& __key_iter) { + return __self.__containers_.values.begin() + + static_cast>( + ranges::distance(__self.__containers_.keys.begin(), __key_iter)); + }; + + using __iterator_type = ranges::iterator_t; + return std::make_pair(__iterator_type(__key_first, __make_mapped_iter(__key_first)), + __iterator_type(__key_last, __make_mapped_iter(__key_last))); + } + + template + _LIBCPP_HIDE_FROM_ABI static _Res __lower_bound(_Self&& __self, _Kp& __x) { + return __binary_search<_Res>(__self, ranges::lower_bound, __x); + } + + template + _LIBCPP_HIDE_FROM_ABI static _Res __upper_bound(_Self&& __self, _Kp& __x) { + return __binary_search<_Res>(__self, ranges::upper_bound, __x); + } + + template + _LIBCPP_HIDE_FROM_ABI static _Res __binary_search(_Self&& __self, _Fn __search_fn, _Kp& __x) { + auto __key_iter = __search_fn(__self.__containers_.keys, __x, __self.__compare_); + auto __mapped_iter = + __self.__containers_.values.begin() + + static_cast>( + ranges::distance(__self.__containers_.keys.begin(), __key_iter)); + + return _Res(std::move(__key_iter), std::move(__mapped_iter)); + } + + template + _LIBCPP_HIDE_FROM_ABI pair __try_emplace(_KeyArg&& __key, _MArgs&&... __mapped_args) { + auto __key_it = ranges::lower_bound(__containers_.keys, __key, __compare_); + auto __mapped_it = __containers_.values.begin() + ranges::distance(__containers_.keys.begin(), __key_it); + + if (__key_it == __containers_.keys.end() || __compare_(__key, *__key_it)) { + return pair( + __try_emplace_exact_hint( + std::move(__key_it), + std::move(__mapped_it), + std::forward<_KeyArg>(__key), + std::forward<_MArgs>(__mapped_args)...), + true); + } else { + return pair(iterator(std::move(__key_it), std::move(__mapped_it)), false); + } + } + + template + _LIBCPP_HIDE_FROM_ABI bool __is_hint_correct(const_iterator __hint, _Kp&& __key) { + if (__hint != cbegin() && !__compare_((__hint - 1)->first, __key)) { + return false; + } + if (__hint != cend() && __compare_(__hint->first, __key)) { + return false; + } + return true; + } + + template + _LIBCPP_HIDE_FROM_ABI pair __try_emplace_hint(const_iterator __hint, _Kp&& __key, _Args&&... __args) { + if (__is_hint_correct(__hint, __key)) { + if (__hint == cend() || __compare_(__key, __hint->first)) { + return { + __try_emplace_exact_hint( + __hint.__key_iter_, __hint.__mapped_iter_, std::forward<_Kp>(__key), std::forward<_Args>(__args)...), + true}; + } else { + // key equals + auto __dist = __hint - cbegin(); + return {iterator(__containers_.keys.begin() + __dist, __containers_.values.begin() + __dist), false}; + } + } else { + return __try_emplace(std::forward<_Kp>(__key), std::forward<_Args>(__args)...); + } + } + + template + _LIBCPP_HIDE_FROM_ABI iterator + __try_emplace_exact_hint(_IterK&& __it_key, _IterM&& __it_mapped, _KeyArg&& __key, _MArgs&&... __mapped_args) { + auto __on_key_failed = std::__make_exception_guard([&]() noexcept { + if constexpr (__container_traits<_KeyContainer>::__emplacement_has_strong_exception_safety_guarantee) { + // Nothing to roll back! + } else { + // we need to clear both because we don't know the state of our keys anymore + clear() /* noexcept */; + } + }); + auto __key_it = __containers_.keys.emplace(__it_key, std::forward<_KeyArg>(__key)); + __on_key_failed.__complete(); + + auto __on_value_failed = std::__make_exception_guard([&]() noexcept { + if constexpr (!__container_traits<_MappedContainer>::__emplacement_has_strong_exception_safety_guarantee) { + // we need to clear both because we don't know the state of our values anymore + clear() /* noexcept */; + } else { + // In this case, we know the values are just like before we attempted emplacement, + // and we also know that the keys have been emplaced successfully. Just roll back the keys. +# if _LIBCPP_HAS_EXCEPTIONS + try { +# endif // _LIBCPP_HAS_EXCEPTIONS + __containers_.keys.erase(__key_it); +# if _LIBCPP_HAS_EXCEPTIONS + } catch (...) { + // Now things are funky for real. We're failing to rollback the keys. + // Just give up and clear the whole thing. + // + // Also, swallow the exception that happened during the rollback and let the + // original value-emplacement exception propagate normally. + clear() /* noexcept */; + } +# endif // _LIBCPP_HAS_EXCEPTIONS + } + }); + auto __mapped_it = __containers_.values.emplace(__it_mapped, std::forward<_MArgs>(__mapped_args)...); + __on_value_failed.__complete(); + + return iterator(std::move(__key_it), std::move(__mapped_it)); + } + + template + _LIBCPP_HIDE_FROM_ABI pair __insert_or_assign(_Kp&& __key, _Mapped&& __mapped) { + auto __r = try_emplace(std::forward<_Kp>(__key), std::forward<_Mapped>(__mapped)); + if (!__r.second) { + __r.first->second = std::forward<_Mapped>(__mapped); + } + return __r; + } + + template + _LIBCPP_HIDE_FROM_ABI iterator __insert_or_assign(const_iterator __hint, _Kp&& __key, _Mapped&& __mapped) { + auto __r = __try_emplace_hint(__hint, std::forward<_Kp>(__key), std::forward<_Mapped>(__mapped)); + if (!__r.second) { + __r.first->second = std::forward<_Mapped>(__mapped); + } + return __r.first; + } + + _LIBCPP_HIDE_FROM_ABI void __reserve(size_t __size) { + if constexpr (requires { __containers_.keys.reserve(__size); }) { + __containers_.keys.reserve(__size); + } + + if constexpr (requires { __containers_.values.reserve(__size); }) { + __containers_.values.reserve(__size); + } + } + + template + _LIBCPP_HIDE_FROM_ABI iterator __erase(_KIter __key_iter_to_remove, _MIter __mapped_iter_to_remove) { + auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; }); + auto __key_iter = __containers_.keys.erase(__key_iter_to_remove); + auto __mapped_iter = __containers_.values.erase(__mapped_iter_to_remove); + __on_failure.__complete(); + return iterator(std::move(__key_iter), std::move(__mapped_iter)); + } + + template + friend typename flat_map<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>::size_type + erase_if(flat_map<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>&, _Predicate); + + containers __containers_; + [[no_unique_address]] key_compare __compare_; + + struct __key_equiv { + _LIBCPP_HIDE_FROM_ABI __key_equiv(key_compare __c) : __comp_(__c) {} + _LIBCPP_HIDE_FROM_ABI bool operator()(const_reference __x, const_reference __y) const { + return !__comp_(std::get<0>(__x), std::get<0>(__y)) && !__comp_(std::get<0>(__y), std::get<0>(__x)); + } + key_compare __comp_; + }; +}; + +template > + requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value && + !__is_allocator<_MappedContainer>::value && + is_invocable_v) +flat_map(_KeyContainer, _MappedContainer, _Compare = _Compare()) + -> flat_map; + +template + requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> && + !__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value) +flat_map(_KeyContainer, _MappedContainer, _Allocator) + -> flat_map, + _KeyContainer, + _MappedContainer>; + +template + requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value && + !__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> && + uses_allocator_v<_MappedContainer, _Allocator> && + is_invocable_v) +flat_map(_KeyContainer, _MappedContainer, _Compare, _Allocator) + -> flat_map; + +template > + requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value && + !__is_allocator<_MappedContainer>::value && + is_invocable_v) +flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare = _Compare()) + -> flat_map; + +template + requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> && + !__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value) +flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Allocator) + -> flat_map, + _KeyContainer, + _MappedContainer>; + +template + requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value && + !__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> && + uses_allocator_v<_MappedContainer, _Allocator> && + is_invocable_v) +flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare, _Allocator) + -> flat_map; + +template >> + requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value) +flat_map(_InputIterator, _InputIterator, _Compare = _Compare()) + -> flat_map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>; + +template >> + requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value) +flat_map(sorted_unique_t, _InputIterator, _InputIterator, _Compare = _Compare()) + -> flat_map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>; + +template >, + class _Allocator = allocator, + class = __enable_if_t::value && __is_allocator<_Allocator>::value>> +flat_map(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator()) + -> flat_map< + __range_key_type<_Range>, + __range_mapped_type<_Range>, + _Compare, + vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>, + vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>; + +template ::value>> +flat_map(from_range_t, _Range&&, _Allocator) + -> flat_map< + __range_key_type<_Range>, + __range_mapped_type<_Range>, + less<__range_key_type<_Range>>, + vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>, + vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>; + +template > + requires(!__is_allocator<_Compare>::value) +flat_map(initializer_list>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>; + +template > + requires(!__is_allocator<_Compare>::value) +flat_map(sorted_unique_t, initializer_list>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>; + +template +struct uses_allocator, _Allocator> + : bool_constant && uses_allocator_v<_MappedContainer, _Allocator>> {}; + +template +_LIBCPP_HIDE_FROM_ABI typename flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::size_type +erase_if(flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>& __flat_map, _Predicate __pred) { + auto __zv = ranges::views::zip(__flat_map.__containers_.keys, __flat_map.__containers_.values); + auto __first = __zv.begin(); + auto __last = __zv.end(); + auto __guard = std::__make_exception_guard([&] { __flat_map.clear(); }); + auto __it = std::remove_if(__first, __last, [&](auto&& __zipped) -> bool { + using _Ref = typename flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::const_reference; + return __pred(_Ref(std::get<0>(__zipped), std::get<1>(__zipped))); + }); + auto __res = __last - __it; + auto __offset = __it - __first; + + const auto __erase_container = [&](auto& __cont) { __cont.erase(__cont.begin() + __offset, __cont.end()); }; + + __erase_container(__flat_map.__containers_.keys); + __erase_container(__flat_map.__containers_.values); + + __guard.__complete(); + return __res; +} + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER >= 23 + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___FLAT_MAP_FLAT_MAP_H diff --git a/libcxx/include/__flat_map/sorted_unique.h b/libcxx/include/__flat_map/sorted_unique.h new file mode 100644 index 00000000000000..0189a5ff1d5684 --- /dev/null +++ b/libcxx/include/__flat_map/sorted_unique.h @@ -0,0 +1,31 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#ifndef _LIBCPP___FLAT_MAP_SORTED_UNIQUE_H +#define _LIBCPP___FLAT_MAP_SORTED_UNIQUE_H + +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#if _LIBCPP_STD_VER >= 23 + +_LIBCPP_BEGIN_NAMESPACE_STD + +struct sorted_unique_t { + explicit sorted_unique_t() = default; +}; +inline constexpr sorted_unique_t sorted_unique{}; + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP_STD_VER >= 23 + +#endif // _LIBCPP___FLAT_MAP_SORTED_UNIQUE_H diff --git a/libcxx/include/__utility/exception_guard.h b/libcxx/include/__utility/exception_guard.h index a03bd7e8f35227..00b835d3e2a2fc 100644 --- a/libcxx/include/__utility/exception_guard.h +++ b/libcxx/include/__utility/exception_guard.h @@ -137,6 +137,12 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __exception_guard<_Rollback> __make_exce return __exception_guard<_Rollback>(std::move(__rollback)); } +template +_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __exception_guard_exceptions<_Rollback> +__make_scope_guard(_Rollback __rollback) { + return __exception_guard_exceptions<_Rollback>(std::move(__rollback)); +} + _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS diff --git a/libcxx/include/flat_map b/libcxx/include/flat_map new file mode 100644 index 00000000000000..15d79dd1ddca34 --- /dev/null +++ b/libcxx/include/flat_map @@ -0,0 +1,54 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP_FLAT_MAP +#define _LIBCPP_FLAT_MAP + +/* + Header synopsis + +#include // see [compare.syn] +#include // see [initializer.list.syn] + +namespace std { + // [flat.map], class template flat_map + template, + class KeyContainer = vector, class MappedContainer = vector> + class flat_map; + + struct sorted_unique_t { explicit sorted_unique_t() = default; }; + inline constexpr sorted_unique_t sorted_unique{}; + + template + struct uses_allocator, + Allocator>; + + // [flat.map.erasure], erasure for flat_map + template + typename flat_map::size_type + erase_if(flat_map& c, Predicate pred); +*/ + +#include <__assert> // all public C++ headers provide the assertion handler +#include <__config> +#include <__flat_map/flat_map.h> +#include <__flat_map/sorted_unique.h> +#include + +// standard required includes +#include +#include + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +#endif // _LIBCPP_FLAT_MAP diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap index 05d08cfbd7cd29..c3561590e06d8a 100644 --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -1222,6 +1222,14 @@ module std [system] { export * } + module flat_map { + module flat_map { header "__flat_map/flat_map.h" } + module sorted_unique { header "__flat_map/sorted_unique.h" } + + header "flat_map" + export * + } + module format { module buffer { header "__format/buffer.h" } module concepts { header "__format/concepts.h" } diff --git a/libcxx/modules/std.compat.cppm.in b/libcxx/modules/std.compat.cppm.in index 797b413f68e272..fbc2c7d94cfabb 100644 --- a/libcxx/modules/std.compat.cppm.in +++ b/libcxx/modules/std.compat.cppm.in @@ -53,9 +53,6 @@ module; # if __has_include() # error "please update the header information for in headers_not_available in utils/libcxx/header_information.py" # endif // __has_include() -# if __has_include() -# error "please update the header information for in headers_not_available in utils/libcxx/header_information.py" -# endif // __has_include() # if __has_include() # error "please update the header information for in headers_not_available in utils/libcxx/header_information.py" # endif // __has_include() diff --git a/libcxx/modules/std.cppm.in b/libcxx/modules/std.cppm.in index 64ed8d4088cc01..b4889e5a69e49b 100644 --- a/libcxx/modules/std.cppm.in +++ b/libcxx/modules/std.cppm.in @@ -64,6 +64,7 @@ module; #include #include #include +#include #include #include #if !defined(_LIBCPP_HAS_NO_LOCALIZATION) @@ -161,9 +162,6 @@ module; # if __has_include() # error "please update the header information for in headers_not_available in utils/libcxx/header_information.py" # endif // __has_include() -# if __has_include() -# error "please update the header information for in headers_not_available in utils/libcxx/header_information.py" -# endif // __has_include() # if __has_include() # error "please update the header information for in headers_not_available in utils/libcxx/header_information.py" # endif // __has_include() diff --git a/libcxx/modules/std/flat_map.inc b/libcxx/modules/std/flat_map.inc index 83cd20ad618946..6a86229bceaba9 100644 --- a/libcxx/modules/std/flat_map.inc +++ b/libcxx/modules/std/flat_map.inc @@ -8,8 +8,8 @@ //===----------------------------------------------------------------------===// export namespace std { -#if 0 - // [flat.map], class template flat_­map +#if _LIBCPP_STD_VER >= 23 + // [flat.map], class template flat_map using std::flat_map; using std::sorted_unique; @@ -17,15 +17,17 @@ export namespace std { using std::uses_allocator; - // [flat.map.erasure], erasure for flat_­map + // [flat.map.erasure], erasure for flat_map using std::erase_if; - // [flat.multimap], class template flat_­multimap +#endif // _LIBCPP_STD_VER >= 23 +#if 0 + // [flat.multimap], class template flat_multimap using std::flat_multimap; using std::sorted_equivalent; using std::sorted_equivalent_t; - // [flat.multimap.erasure], erasure for flat_­multimap + // [flat.multimap.erasure], erasure for flat_multimap #endif } // namespace std diff --git a/libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.input_range.pass.cpp b/libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.input_range.pass.cpp new file mode 100644 index 00000000000000..2db803b53441f1 --- /dev/null +++ b/libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.input_range.pass.cpp @@ -0,0 +1,66 @@ +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// UNSUPPORTED: libcpp-hardening-mode=none +// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing + +// + +// flat_map(key_container_type , mapped_container_type , const key_compare& __comp = key_compare()) +// flat_map(const key_container_type& , const mapped_container_type& , const _Allocator& ) +// flat_map(const key_container_type& , const mapped_container_type& , const key_compare&, const _Allocator& ) +// void replace(key_container_type&& , mapped_container_type&&) +// + +#include +#include +#include +#include + +#include "check_assertion.h" + +int main(int, char**) { + using M = std::flat_map; + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { M m({1, 2, 3}, {4}); }()), "flat_map keys and mapped containers have different size"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { M m({1, 2, 3}, {4}, std::less{}); }()), "flat_map keys and mapped containers have different size"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector keys{1, 2, 3}; + const std::vector values{4}; + const std::allocator alloc{}; + M m(keys, values, alloc); + }()), + "flat_map keys and mapped containers have different size"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector keys{1, 2, 3}; + const std::vector values{4}; + const std::less key_compare{}; + const std::allocator alloc{}; + M m(keys, values, key_compare, alloc); + }()), + "flat_map keys and mapped containers have different size"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::vector keys{1, 2, 3}; + std::vector values{4}; + M m; + m.replace(std::move(keys), std::move(values)); + }()), + "flat_map keys and mapped containers have different size"); + + return 0; +} diff --git a/libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.sorted_unique.pass.cpp b/libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.sorted_unique.pass.cpp new file mode 100644 index 00000000000000..e6bd3f385af9cb --- /dev/null +++ b/libcxx/test/libcxx/containers/containers.adaptors/flat.map/assert.sorted_unique.pass.cpp @@ -0,0 +1,225 @@ +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// REQUIRES: has-unix-headers +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// UNSUPPORTED: libcpp-hardening-mode=none +// REQUIRES: libcpp-hardening-mode=debug +// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing + +// + +// flat_map(key_container_type , mapped_container_type , const key_compare& __comp = key_compare()) +// flat_map(const key_container_type& , const mapped_container_type& , const _Allocator& ) +// flat_map(const key_container_type& , const mapped_container_type& , const key_compare&, const _Allocator& ) +// void replace(key_container_type&& , mapped_container_type&&) +// + +#include +#include +#include +#include +#include +#include + +#include "check_assertion.h" + +int main(int, char**) { + using M = std::flat_map; + + TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {2, 2, 3}, {4, 5, 6}); }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {4, 2, 3}, {4, 5, 6}); }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {2, 2, 3}, {4, 5, 6}, std::less{}); }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE(([] { M m(std::sorted_unique, {4, 2, 3}, {4, 5, 6}, std::less{}); }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector keys{2, 2, 3}; + const std::vector values{4, 5, 6}; + const std::allocator alloc{}; + M m(std::sorted_unique, keys, values, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector keys{4, 2, 3}; + const std::vector values{4, 5, 6}; + const std::allocator alloc{}; + M m(std::sorted_unique, keys, values, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector keys{2, 2, 3}; + const std::vector values{4, 5, 6}; + const std::allocator alloc{}; + const std::less comp{}; + M m(std::sorted_unique, keys, values, comp, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector keys{4, 2, 3}; + const std::vector values{4, 5, 6}; + const std::allocator alloc{}; + const std::less comp{}; + M m(std::sorted_unique, keys, values, comp, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{2, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + M m(std::sorted_unique, v.begin(), v.end(), comp); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{4, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + M m(std::sorted_unique, v.begin(), v.end(), comp); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{2, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + const std::allocator alloc{}; + M m(std::sorted_unique, v.begin(), v.end(), comp, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{4, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + const std::allocator alloc{}; + M m(std::sorted_unique, v.begin(), v.end(), comp, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{2, 4}, {2, 5}, {3, 6}}; + const std::allocator alloc{}; + M m(std::sorted_unique, v.begin(), v.end(), alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{4, 4}, {2, 5}, {3, 6}}; + const std::allocator alloc{}; + M m(std::sorted_unique, v.begin(), v.end(), alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{2, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + M m(std::sorted_unique, v, comp); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{4, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + M m(std::sorted_unique, v, comp); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{2, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + const std::allocator alloc{}; + M m(std::sorted_unique, v, comp, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{4, 4}, {2, 5}, {3, 6}}; + const std::less comp{}; + const std::allocator alloc{}; + M m(std::sorted_unique, v, comp, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{2, 4}, {2, 5}, {3, 6}}; + const std::allocator alloc{}; + M m(std::sorted_unique, v, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{4, 4}, {2, 5}, {3, 6}}; + const std::allocator alloc{}; + M m(std::sorted_unique, v, alloc); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{2, 4}, {2, 5}, {3, 6}}; + M m; + m.insert(std::sorted_unique, v.begin(), v.end()); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + const std::vector> v{{4, 4}, {2, 5}, {3, 6}}; + M m; + m.insert(std::sorted_unique, v.begin(), v.end()); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{2, 4}, {2, 5}, {3, 6}}; + M m; + m.insert(std::sorted_unique, v); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::initializer_list> v{{4, 4}, {2, 5}, {3, 6}}; + M m; + m.insert(std::sorted_unique, v); + }()), + "Either the key container is not sorted or it contains duplicates"); + + TEST_LIBCPP_ASSERT_FAILURE( + ([] { + std::vector keys{1, 1, 3}; + std::vector values{4, 5, 6}; + M m; + m.replace(std::move(keys), std::move(values)); + }()), + "Either the key container is not sorted or it contains duplicates"); + return 0; +} diff --git a/libcxx/test/libcxx/containers/containers.adaptors/flat.map/container_stability.pass.cpp b/libcxx/test/libcxx/containers/containers.adaptors/flat.map/container_stability.pass.cpp new file mode 100644 index 00000000000000..0d90c3250061ff --- /dev/null +++ b/libcxx/test/libcxx/containers/containers.adaptors/flat.map/container_stability.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(key_container_type key_cont, mapped_container_type mapped_cont); +// +// libc++ uses stable_sort to ensure that flat_map's behavior matches map's, +// in terms of which duplicate items are kept. +// This tests a conforming extension. + +#include +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" + +struct Mod256 { + bool operator()(int x, int y) const { return (x % 256) < (y % 256); } +}; + +int main(int, char**) { + std::mt19937 randomness; + std::vector values; + std::vector> pairs; + for (int i = 0; i < 200; ++i) { + uint16_t r = randomness(); + values.push_back(r); + pairs.emplace_back(r, r); + } + + { + std::map m(pairs.begin(), pairs.end()); + std::flat_map fm(values, values); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + { + std::map m(pairs.begin(), pairs.end()); + std::flat_map fm(values, values, Mod256()); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + { + std::map m(pairs.begin(), pairs.end()); + std::flat_map fm(values, values, std::allocator()); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + { + std::map m(pairs.begin(), pairs.end()); + std::flat_map fm(values, values, Mod256(), std::allocator()); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + return 0; +} diff --git a/libcxx/test/libcxx/transitive_includes/cxx03.csv b/libcxx/test/libcxx/transitive_includes/cxx03.csv index 506b5cd02c4495..2dc84963f0891e 100644 --- a/libcxx/test/libcxx/transitive_includes/cxx03.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx03.csv @@ -682,6 +682,50 @@ filesystem typeinfo filesystem utility filesystem variant filesystem version +flat_map algorithm +flat_map array +flat_map atomic +flat_map bit +flat_map cctype +flat_map cerrno +flat_map climits +flat_map clocale +flat_map cmath +flat_map compare +flat_map concepts +flat_map cstdarg +flat_map cstddef +flat_map cstdint +flat_map cstdio +flat_map cstdlib +flat_map cstring +flat_map ctime +flat_map cwchar +flat_map cwctype +flat_map exception +flat_map initializer_list +flat_map ios +flat_map iosfwd +flat_map iterator +flat_map limits +flat_map locale +flat_map memory +flat_map mutex +flat_map new +flat_map optional +flat_map ratio +flat_map stdexcept +flat_map streambuf +flat_map string +flat_map string_view +flat_map system_error +flat_map tuple +flat_map type_traits +flat_map typeinfo +flat_map utility +flat_map variant +flat_map vector +flat_map version format algorithm format array format atomic diff --git a/libcxx/test/libcxx/transitive_includes/cxx11.csv b/libcxx/test/libcxx/transitive_includes/cxx11.csv index 506b5cd02c4495..2dc84963f0891e 100644 --- a/libcxx/test/libcxx/transitive_includes/cxx11.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx11.csv @@ -682,6 +682,50 @@ filesystem typeinfo filesystem utility filesystem variant filesystem version +flat_map algorithm +flat_map array +flat_map atomic +flat_map bit +flat_map cctype +flat_map cerrno +flat_map climits +flat_map clocale +flat_map cmath +flat_map compare +flat_map concepts +flat_map cstdarg +flat_map cstddef +flat_map cstdint +flat_map cstdio +flat_map cstdlib +flat_map cstring +flat_map ctime +flat_map cwchar +flat_map cwctype +flat_map exception +flat_map initializer_list +flat_map ios +flat_map iosfwd +flat_map iterator +flat_map limits +flat_map locale +flat_map memory +flat_map mutex +flat_map new +flat_map optional +flat_map ratio +flat_map stdexcept +flat_map streambuf +flat_map string +flat_map string_view +flat_map system_error +flat_map tuple +flat_map type_traits +flat_map typeinfo +flat_map utility +flat_map variant +flat_map vector +flat_map version format algorithm format array format atomic diff --git a/libcxx/test/libcxx/transitive_includes/cxx14.csv b/libcxx/test/libcxx/transitive_includes/cxx14.csv index 828e1d62c6ec3e..27e22975573584 100644 --- a/libcxx/test/libcxx/transitive_includes/cxx14.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx14.csv @@ -700,6 +700,51 @@ filesystem typeinfo filesystem utility filesystem variant filesystem version +flat_map algorithm +flat_map array +flat_map atomic +flat_map bit +flat_map cctype +flat_map cerrno +flat_map climits +flat_map clocale +flat_map cmath +flat_map compare +flat_map concepts +flat_map cstdarg +flat_map cstddef +flat_map cstdint +flat_map cstdio +flat_map cstdlib +flat_map cstring +flat_map ctime +flat_map cwchar +flat_map cwctype +flat_map exception +flat_map execution +flat_map initializer_list +flat_map ios +flat_map iosfwd +flat_map iterator +flat_map limits +flat_map locale +flat_map memory +flat_map mutex +flat_map new +flat_map optional +flat_map ratio +flat_map stdexcept +flat_map streambuf +flat_map string +flat_map string_view +flat_map system_error +flat_map tuple +flat_map type_traits +flat_map typeinfo +flat_map utility +flat_map variant +flat_map vector +flat_map version format algorithm format array format atomic diff --git a/libcxx/test/libcxx/transitive_includes/cxx17.csv b/libcxx/test/libcxx/transitive_includes/cxx17.csv index 0bee6e9beb7af1..b17eb1f2347a86 100644 --- a/libcxx/test/libcxx/transitive_includes/cxx17.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx17.csv @@ -706,6 +706,50 @@ filesystem utility filesystem variant filesystem vector filesystem version +flat_map algorithm +flat_map array +flat_map atomic +flat_map bit +flat_map cctype +flat_map cerrno +flat_map climits +flat_map clocale +flat_map cmath +flat_map compare +flat_map concepts +flat_map cstdarg +flat_map cstddef +flat_map cstdint +flat_map cstdio +flat_map cstdlib +flat_map cstring +flat_map ctime +flat_map cwchar +flat_map cwctype +flat_map exception +flat_map initializer_list +flat_map ios +flat_map iosfwd +flat_map iterator +flat_map limits +flat_map locale +flat_map memory +flat_map mutex +flat_map new +flat_map optional +flat_map ratio +flat_map stdexcept +flat_map streambuf +flat_map string +flat_map string_view +flat_map system_error +flat_map tuple +flat_map type_traits +flat_map typeinfo +flat_map utility +flat_map variant +flat_map vector +flat_map version format algorithm format array format atomic diff --git a/libcxx/test/libcxx/transitive_includes/cxx20.csv b/libcxx/test/libcxx/transitive_includes/cxx20.csv index 026c26f3bd9819..9efec327889c1d 100644 --- a/libcxx/test/libcxx/transitive_includes/cxx20.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx20.csv @@ -694,6 +694,50 @@ filesystem utility filesystem variant filesystem vector filesystem version +flat_map algorithm +flat_map array +flat_map atomic +flat_map bit +flat_map cctype +flat_map cerrno +flat_map climits +flat_map clocale +flat_map cmath +flat_map compare +flat_map concepts +flat_map cstdarg +flat_map cstddef +flat_map cstdint +flat_map cstdio +flat_map cstdlib +flat_map cstring +flat_map ctime +flat_map cwchar +flat_map cwctype +flat_map exception +flat_map initializer_list +flat_map ios +flat_map iosfwd +flat_map iterator +flat_map limits +flat_map locale +flat_map memory +flat_map mutex +flat_map new +flat_map optional +flat_map ratio +flat_map stdexcept +flat_map streambuf +flat_map string +flat_map string_view +flat_map system_error +flat_map tuple +flat_map type_traits +flat_map typeinfo +flat_map utility +flat_map variant +flat_map vector +flat_map version format algorithm format array format atomic diff --git a/libcxx/test/libcxx/transitive_includes/cxx23.csv b/libcxx/test/libcxx/transitive_includes/cxx23.csv index c3db06f1547769..e17f732663a9b2 100644 --- a/libcxx/test/libcxx/transitive_includes/cxx23.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx23.csv @@ -381,6 +381,31 @@ filesystem string_view filesystem tuple filesystem typeinfo filesystem version +flat_map array +flat_map cctype +flat_map cerrno +flat_map climits +flat_map clocale +flat_map compare +flat_map cstddef +flat_map cstdint +flat_map cstdio +flat_map cstdlib +flat_map cstring +flat_map cwchar +flat_map cwctype +flat_map initializer_list +flat_map iosfwd +flat_map limits +flat_map new +flat_map optional +flat_map stdexcept +flat_map string +flat_map string_view +flat_map tuple +flat_map typeinfo +flat_map vector +flat_map version format array format cctype format cerrno diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv index 8d7560344ee541..c56f5cdfad0072 100644 --- a/libcxx/test/libcxx/transitive_includes/cxx26.csv +++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv @@ -381,6 +381,31 @@ filesystem string_view filesystem tuple filesystem typeinfo filesystem version +flat_map array +flat_map cctype +flat_map cerrno +flat_map climits +flat_map clocale +flat_map compare +flat_map cstddef +flat_map cstdint +flat_map cstdio +flat_map cstdlib +flat_map cstring +flat_map cwchar +flat_map cwctype +flat_map initializer_list +flat_map iosfwd +flat_map limits +flat_map new +flat_map optional +flat_map stdexcept +flat_map string +flat_map string_view +flat_map tuple +flat_map typeinfo +flat_map vector +flat_map version format array format cctype format cerrno diff --git a/libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h b/libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h new file mode 100644 index 00000000000000..61fa3504e34e3a --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_NAIVE_STATIC_VECTOR_H +#define SUPPORT_NAIVE_STATIC_VECTOR_H + +#include +#include +#include "test_iterators.h" +#include "test_macros.h" + +template +struct NaiveStaticVector { + struct CapacityError {}; + + using value_type = T; + using difference_type = short; + using size_type = unsigned short; + using iterator = random_access_iterator; + using const_iterator = random_access_iterator; + + explicit NaiveStaticVector() = default; + template + explicit NaiveStaticVector(It first, It last) { + while (first != last) + insert(*first++); + } + + // Moving-from a NaiveStaticVector leaves the source vector holding moved-from objects. + // This is intentional (the "Naive" in the name). + // Specifically, moving-out-of a sorted+uniqued NaiveStaticVector + // will leave it in a non-sorted+uniqued state. + + NaiveStaticVector(const NaiveStaticVector&) = default; + NaiveStaticVector(NaiveStaticVector&&) = default; // deliberately don't reset size_ + NaiveStaticVector& operator=(const NaiveStaticVector&) = default; + NaiveStaticVector& operator=(NaiveStaticVector&&) = default; + + iterator begin() { return iterator(data_); } + const_iterator begin() const { return const_iterator(data_); } + const_iterator cbegin() const { return const_iterator(data_); } + iterator end() { return begin() + size(); } + const_iterator end() const { return begin() + size(); } + size_type size() const { return size_; } + bool empty() const { return size_ == 0; } + + void clear() { size_ = 0; } + + template + iterator insert(const_iterator pos, It first, It last) { + iterator result = pos - cbegin() + begin(); + while (first != last) { + insert(pos++, *first++); + } + return result; + } + + iterator insert(const_iterator pos, T value) { + if (size_ == N) { + throw CapacityError(); + } + int i = pos - cbegin(); + size_ += 1; + std::move_backward(&data_[i], &data_[size_ - 1], &data_[size_]); + data_[i] = std::move(value); + return begin() + i; + } + + template + iterator emplace(const_iterator pos, Args&&... args) { + return insert(pos, T(std::forward(args)...)); + } + + iterator erase(const_iterator first, const_iterator last) { + int i = first - cbegin(); + int j = last - cbegin(); + std::move(&data_[j], &data_[size_], &data_[i]); + size_ -= (last - first); + return begin() + i; + } + + iterator erase(const_iterator pos) { return erase(pos, std::next(pos)); } + +private: + T data_[N]; + std::size_t size_ = 0; +}; + +#endif // SUPPORT_NAIVE_STATIC_VECTOR_H diff --git a/libcxx/test/std/containers/container.adaptors/flat.map.syn/sorted_unique.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map.syn/sorted_unique.pass.cpp new file mode 100644 index 00000000000000..c602d2d3d38f79 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map.syn/sorted_unique.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// struct sorted_unique_t { explicit sorted_unique_t() = default; }; +// inline constexpr sorted_unique_t sorted_unique{}; + +#include +#include +#include +#include + +template +void implicit_test(T) {} + +template +concept HasImplicitDefaultCtor = requires { implicit_test({}); }; + +static_assert(std::is_default_constructible_v); +static_assert(std::is_trivially_default_constructible_v); +static_assert(!HasImplicitDefaultCtor); + +constexpr bool test() { + { [[maybe_unused]] std::sorted_unique_t s; } + { [[maybe_unused]] std::same_as decltype(auto) s = (std::sorted_unique); } + { [[maybe_unused]] std::same_as decltype(auto) copy = std::sorted_unique; } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/at.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/at.pass.cpp new file mode 100644 index 00000000000000..d30055bf1701cd --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/at.pass.cpp @@ -0,0 +1,92 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// mapped_type& at(const key_type& k); +// const mapped_type& at(const key_type& k) const; + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "min_allocator.h" +#include "test_macros.h" + +template +void test() { + using P = std::pair; + P ar[] = { + P(1, 1.5), + P(2, 2.5), + P(3, 3.5), + P(4, 4.5), + P(5, 5.5), + P(7, 7.5), + P(8, 8.5), + }; + const int one = 1; + { + std::flat_map, KeyContainer, ValueContainer> m(ar, ar + sizeof(ar) / sizeof(ar[0])); + ASSERT_SAME_TYPE(decltype(m.at(one)), double&); + assert(m.size() == 7); + assert(m.at(one) == 1.5); + m.at(1) = -1.5; + assert(m.at(1) == -1.5); + assert(m.at(2) == 2.5); + assert(m.at(3) == 3.5); + assert(m.at(4) == 4.5); + assert(m.at(5) == 5.5); +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + TEST_IGNORE_NODISCARD m.at(6); + assert(false); + } catch (std::out_of_range&) { + } +#endif + assert(m.at(7) == 7.5); + assert(m.at(8) == 8.5); + assert(m.size() == 7); + } + { + const std::flat_map, KeyContainer, ValueContainer> m( + ar, ar + sizeof(ar) / sizeof(ar[0])); + ASSERT_SAME_TYPE(decltype(m.at(one)), const double&); + assert(m.size() == 7); + assert(m.at(one) == 1.5); + assert(m.at(2) == 2.5); + assert(m.at(3) == 3.5); + assert(m.at(4) == 4.5); + assert(m.at(5) == 5.5); +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + TEST_IGNORE_NODISCARD m.at(6); + assert(false); + } catch (std::out_of_range&) { + } +#endif + assert(m.at(7) == 7.5); + assert(m.at(8) == 8.5); + assert(m.size() == 7); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/at_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/at_transparent.pass.cpp new file mode 100644 index 00000000000000..13edca915fd005 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/at_transparent.pass.cpp @@ -0,0 +1,111 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template mapped_type& at(const K& x); +// template const mapped_type& at(const K& x) const; + +#include +#include +#include +#include +#include + +#include "../helpers.h" +#include "min_allocator.h" +#include "MinSequenceContainer.h" +#include "test_macros.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanAt = requires(M m, Transparent k) { m.at(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanAt); +static_assert(CanAt); +static_assert(!CanAt); +static_assert(!CanAt); + +template +void test() { + using P = std::pair; + P ar[] = { + P(1, 1.5), + P(2, 2.5), + P(3, 3.5), + P(4, 4.5), + P(5, 5.5), + P(7, 7.5), + P(8, 8.5), + }; + const Transparent one{1}; + { + std::flat_map m( + ar, ar + sizeof(ar) / sizeof(ar[0])); + ASSERT_SAME_TYPE(decltype(m.at(one)), double&); + assert(m.size() == 7); + assert(m.at(one) == 1.5); + m.at(one) = -1.5; + assert(m.at(Transparent{1}) == -1.5); + assert(m.at(Transparent{2}) == 2.5); + assert(m.at(Transparent{3}) == 3.5); + assert(m.at(Transparent{4}) == 4.5); + assert(m.at(Transparent{5}) == 5.5); +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + TEST_IGNORE_NODISCARD m.at(Transparent{6}); + assert(false); + } catch (std::out_of_range&) { + } +#endif + assert(m.at(Transparent{7}) == 7.5); + assert(m.at(Transparent{8}) == 8.5); + assert(m.size() == 7); + } + { + const std::flat_map m( + ar, ar + sizeof(ar) / sizeof(ar[0])); + ASSERT_SAME_TYPE(decltype(m.at(one)), const double&); + assert(m.size() == 7); + assert(m.at(Transparent{1}) == 1.5); + assert(m.at(Transparent{2}) == 2.5); + assert(m.at(Transparent{3}) == 3.5); + assert(m.at(Transparent{4}) == 4.5); + assert(m.at(Transparent{5}) == 5.5); +#ifndef TEST_HAS_NO_EXCEPTIONS + try { + TEST_IGNORE_NODISCARD m.at(Transparent{6}); + assert(false); + } catch (std::out_of_range&) { + } +#endif + assert(m.at(Transparent{7}) == 7.5); + assert(m.at(Transparent{8}) == 8.5); + assert(m.size() == 7); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + m.at(Transparent{3}); + assert(transparent_used); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_key.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_key.pass.cpp new file mode 100644 index 00000000000000..ea2f5d800878a2 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_key.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// mapped_type& operator[](const key_type& k); + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "min_allocator.h" +#include "test_macros.h" + +// Constraints: is_constructible_v is true. +template +concept CanIndex = requires(M m, Input k) { m[k]; }; + +static_assert(CanIndex, const int&>); +static_assert(!CanIndex, const int&>); + +template +void test() { + using P = std::pair; + P ar[] = { + P(1, 1.5), + P(2, 2.5), + P(3, 3.5), + P(4, 4.5), + P(5, 5.5), + P(7, 7.5), + P(8, 8.5), + }; + const int one = 1; + std::flat_map, KeyContainer, ValueContainer> m(ar, ar + sizeof(ar) / sizeof(ar[0])); + ASSERT_SAME_TYPE(decltype(m[one]), double&); + assert(m.size() == 7); + assert(m[one] == 1.5); + assert(m.size() == 7); + m[1] = -1.5; + assert(m[1] == -1.5); + assert(m.size() == 7); + assert(m[6] == 0); + assert(m.size() == 8); + m[6] = 6.5; + assert(m[6] == 6.5); + assert(m.size() == 8); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto index_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + const typename FlatMap::key_type key = key_arg; + const typename FlatMap::mapped_type value = value_arg; + m[key] = value; + }; + test_emplace_exception_guarantee(index_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_rv_key.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_rv_key.pass.cpp new file mode 100644 index 00000000000000..faacc3cfe8f96f --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_rv_key.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// mapped_type& operator[](key_type&& k); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "MoveOnly.h" +#include "min_allocator.h" + +// Constraints: is_constructible_v is true. +template +concept CanIndex = requires(M m, Input k) { m[k]; }; + +static_assert(CanIndex, int&&>); +static_assert(!CanIndex, int&&>); + +template +void test() { + { + std::flat_map, KeyContainer, ValueContainer> m; + ASSERT_SAME_TYPE(decltype(m[MoveOnly{}]), double&); + assert(m.size() == 0); + assert(m[1] == 0.0); + assert(m.size() == 1); + m[1] = -1.5; + assert(m[1] == -1.5); + assert(m.size() == 1); + assert(m[6] == 0); + assert(m.size() == 2); + m[6] = 6.5; + assert(m[6] == 6.5); + assert(m.size() == 2); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto index_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + typename FlatMap::key_type key = key_arg; + const typename FlatMap::mapped_type value = value_arg; + m[std::move(key)] = value; + }; + test_emplace_exception_guarantee(index_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_transparent.pass.cpp new file mode 100644 index 00000000000000..24c08464f3158c --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.access/index_transparent.pass.cpp @@ -0,0 +1,107 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template mapped_type& operator[](K&& x); + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: +// The qualified-id Compare::is_transparent is valid and denotes a type. +// is_constructible_v is true. +// is_constructible_v is true. +// is_convertible_v and is_convertible_v are both false +template +concept CanIndex = requires(M m, Input k) { m[k]; }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +using TransparentNoDefaultCtrValueMap = std::flat_map; + +static_assert(CanIndex>); +static_assert(!CanIndex>); + +static_assert(!CanIndex>); +static_assert(!CanIndex>); + +static_assert(!CanIndex>); +static_assert(!CanIndex>); + +static_assert(!CanIndex>); +static_assert(!CanIndex>); + +static_assert(!CanIndex); +static_assert(!CanIndex); + +template +void test() { + using P = std::pair; + P ar[] = { + P(1, 1.5), + P(2, 2.5), + P(3, 3.5), + P(4, 4.5), + P(5, 5.5), + P(7, 7.5), + P(8, 8.5), + }; + const ConvertibleTransparent one{1}; + const ConvertibleTransparent six{6}; + { + std::flat_map m( + ar, ar + sizeof(ar) / sizeof(ar[0])); + ASSERT_SAME_TYPE(decltype(m[one]), double&); + assert(m.size() == 7); + assert(m[one] == 1.5); + assert(m.size() == 7); + m[one] = -1.5; + assert(m[one] == -1.5); + assert(m.size() == 7); + assert(m[six] == 0); + assert(m.size() == 8); + m[six] = 6.5; + assert(m[six] == 6.5); + assert(m.size() == 8); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + m[ConvertibleTransparent{3}]; + assert(transparent_used); + } + { + auto index_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + using Key = typename FlatMap::key_type; + const typename FlatMap::mapped_type value = value_arg; + m[ConvertibleTransparent{key_arg}] = value; + }; + test_emplace_exception_guarantee(index_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/empty.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/empty.pass.cpp new file mode 100644 index 00000000000000..5ecc2cf7c917bd --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/empty.pass.cpp @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// [[nodiscard]] bool empty() const noexcept; + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m; + ASSERT_SAME_TYPE(decltype(m.empty()), bool); + ASSERT_NOEXCEPT(m.empty()); + assert(m.empty()); + assert(std::as_const(m).empty()); + m = {{1, 1.0}}; + assert(!m.empty()); + m.clear(); + assert(m.empty()); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/empty.verify.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/empty.verify.cpp new file mode 100644 index 00000000000000..cc8016182dcb66 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/empty.verify.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// [[nodiscard]] bool empty() const noexcept; + +#include + +#include "test_macros.h" + +int main(int, char**) { + std::flat_map c; + c.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/max_size.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/max_size.pass.cpp new file mode 100644 index 00000000000000..87acdfd2cf6250 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/max_size.pass.cpp @@ -0,0 +1,76 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// size_type max_size() const noexcept; + +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_allocator.h" +#include "test_macros.h" + +int main(int, char**) { + { + using A1 = limited_allocator; + using A2 = limited_allocator; + using C = std::flat_map, std::vector, std::vector>; + ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t); + ASSERT_SAME_TYPE(C::size_type, std::size_t); + const C c; + ASSERT_NOEXCEPT(c.max_size()); + ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type); + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + using A1 = limited_allocator; + using A2 = limited_allocator; + using C = std::flat_map, std::vector, std::vector>; + ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t); + ASSERT_SAME_TYPE(C::size_type, std::size_t); + const C c; + ASSERT_NOEXCEPT(c.max_size()); + ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type); + assert(c.max_size() <= 10); + LIBCPP_ASSERT(c.max_size() == 10); + } + { + using A = limited_allocator; + using C = std::flat_map, std::vector, std::vector>; + ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t); + ASSERT_SAME_TYPE(C::size_type, std::size_t); + const C::size_type max_dist = static_cast(std::numeric_limits::max()); + const C c; + ASSERT_NOEXCEPT(c.max_size()); + ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type); + assert(c.max_size() <= max_dist); + LIBCPP_ASSERT(c.max_size() == max_dist); + } + { + typedef std::flat_map C; + ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t); + ASSERT_SAME_TYPE(C::size_type, std::size_t); + const C::size_type max_dist = static_cast(std::numeric_limits::max()); + const C c; + ASSERT_NOEXCEPT(c.max_size()); + ASSERT_SAME_TYPE(decltype(c.max_size()), C::size_type); + assert(c.max_size() <= max_dist); + assert(c.max_size() <= alloc_max_size(std::allocator())); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/size.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/size.pass.cpp new file mode 100644 index 00000000000000..957a860450091f --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.capacity/size.pass.cpp @@ -0,0 +1,65 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// size_type size() const noexcept; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using M = std::flat_map, KeyContainer, ValueContainer>; + { + const M m = {{1, 'a'}, {1, 'b'}, {4, 'd'}, {5, 'e'}, {5, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.size()), std::size_t); + ASSERT_NOEXCEPT(m.size()); + assert(m.size() == 3); + } + { + const M m = {{1, 'a'}}; + ASSERT_SAME_TYPE(decltype(m.size()), std::size_t); + ASSERT_NOEXCEPT(m.size()); + assert(m.size() == 1); + } + { + const M m; + ASSERT_SAME_TYPE(decltype(m.size()), std::size_t); + ASSERT_NOEXCEPT(m.size()); + assert(m.size() == 0); + } + { + M m; + std::size_t s = 1000000; + for (auto i = 0u; i < s; ++i) { + m.emplace(i, 'a'); + } + ASSERT_SAME_TYPE(decltype(m.size()), std::size_t); + ASSERT_NOEXCEPT(m.size()); + assert(m.size() == s); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/alloc.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/alloc.pass.cpp new file mode 100644 index 00000000000000..3f8d2ed332d6b3 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/alloc.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// explicit flat_map(const Allocator& a); + +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_allocator.h" +#include "../../../test_compare.h" + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + // explicit + using M = + std::flat_map, + std::vector>, + std::vector>>; + + static_assert(std::is_constructible_v>); + static_assert(!std::is_convertible_v, M>); + } + { + using A = test_allocator; + using M = + std::flat_map, + std::vector>, + std::vector>>; + M m(A(0, 5)); + assert(m.empty()); + assert(m.begin() == m.end()); + assert(m.keys().get_allocator().get_id() == 5); + assert(m.values().get_allocator().get_id() == 5); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/assign_initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/assign_initializer_list.pass.cpp new file mode 100644 index 00000000000000..06bde71e79941e --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/assign_initializer_list.pass.cpp @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map& operator=(initializer_list il); + +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" +#include "test_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + { + M m = {{8, 8}, {10, 10}}; + assert(m.size() == 2); + m = {{3, 0}, {1, 0}, {2, 0}, {2, 1}, {3, 1}, {4, 0}, {3, 2}, {5, 0}, {6, 0}, {5, 1}}; + std::pair expected[] = {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}}; + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + } + { + M m = {{10, 1}, {8, 1}}; + assert(m.size() == 2); + m = {{3, 2}}; + std::pair expected[] = {{3, 2}}; + assert(std::ranges::equal(m, expected)); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/compare.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/compare.pass.cpp new file mode 100644 index 00000000000000..40a1710f55e422 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/compare.pass.cpp @@ -0,0 +1,93 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// explicit flat_map(const key_compare& comp); +// template +// flat_map(const key_compare& comp, const Alloc& a); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "../../../test_compare.h" +#include "test_allocator.h" + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using M1 = std::flat_map, std::vector>; + using M2 = std::flat_map, std::vector>; + using M3 = std::flat_map, std::vector>; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + using C = test_less; + auto m = std::flat_map(C(3)); + assert(m.empty()); + assert(m.begin() == m.end()); + assert(m.key_comp() == C(3)); + } + { + // The one-argument ctor is explicit. + using C = test_less; + static_assert(std::is_constructible_v, C>); + static_assert(!std::is_convertible_v>); + + static_assert(std::is_constructible_v, std::less>); + static_assert(!std::is_convertible_v, std::flat_map>); + } + { + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + auto m = std::flat_map, std::vector>(C(4), A1(5)); + assert(m.empty()); + assert(m.begin() == m.end()); + assert(m.key_comp() == C(4)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // explicit(false) + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + std::flat_map, std::deque> m = {C(4), A1(5)}; + assert(m.empty()); + assert(m.begin() == m.end()); + assert(m.key_comp() == C(4)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // If an allocator is given, it must be usable by both containers. + using A = test_allocator; + using M = std::flat_map, std::vector, std::vector>; + static_assert(std::is_constructible_v>); + static_assert(!std::is_constructible_v, std::allocator>); + static_assert(!std::is_constructible_v, A>); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers.pass.cpp new file mode 100644 index 00000000000000..812e2c3e4f02a8 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers.pass.cpp @@ -0,0 +1,184 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(key_container_type key_cont, mapped_container_type mapped_cont, +// const key_compare& comp = key_compare()); +// template +// flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont, +// const Allocator& a); +// template +// flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont, +// const key_compare& comp, const Alloc& a); + +#include +#include +#include +#include +#include + +#include "min_allocator.h" +#include "MoveOnly.h" +#include "test_allocator.h" +#include "test_iterators.h" +#include "test_macros.h" +#include "../../../test_compare.h" + +struct P { + int first; + int second; + template + bool operator==(const std::pair& rhs) const { + return MoveOnly(first) == rhs.first && MoveOnly(second) == rhs.second; + } +}; + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + // flat_map(key_container_type , mapped_container_type) + using M = std::flat_map; + std::vector ks = {1, 1, 1, 2, 2, 3, 2, 3, 3}; + std::vector vs = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + auto m = M(ks, vs); + assert((m.keys() == std::vector{1, 2, 3})); + LIBCPP_ASSERT((m.values() == std::vector{1, 4, 6})); + + // explicit(false) + M m2 = {ks, vs}; + assert(m2 == m); + + m = M(std::move(ks), std::move(vs)); + assert(ks.empty()); // it was moved-from + assert(vs.empty()); // it was moved-from + assert((m.keys() == std::vector{1, 2, 3})); + LIBCPP_ASSERT((m.values() == std::vector{1, 4, 6})); + } + { + // flat_map(key_container_type , mapped_container_type) + // move-only + P expected[] = {{3, 2}, {2, 1}, {1, 3}}; + using Ks = std::deque>; + using Vs = std::vector>; + using M = std::flat_map, Ks, Vs>; + Ks ks = {1, 3, 2}; + Vs vs; + vs.push_back(3); + vs.push_back(2); + vs.push_back(1); + auto m = M(std::move(ks), std::move(vs)); + assert(ks.empty()); // it was moved-from + assert(vs.empty()); // it was moved-from + assert(std::ranges::equal(m, expected, std::equal_to<>())); + } + { + // flat_map(key_container_type , mapped_container_type) + // container's allocators are used + using A = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + auto ks = std::vector({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5)); + auto vs = std::deque({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6)); + auto m = M(std::move(ks), std::move(vs)); + assert(ks.empty()); // it was moved-from + assert(vs.empty()); // it was moved-from + assert((m == M{{1, 1}, {2, 2}, {3, 3}})); + assert(m.keys().get_allocator() == A(5)); + assert(m.values().get_allocator() == A(6)); + } + { + // flat_map(key_container_type , mapped_container_type, key_compare) + using C = test_less; + using M = std::flat_map; + std::vector ks = {1, 1, 1, 2, 2, 3, 2, 3, 3}; + std::vector vs = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + auto m = M(ks, vs, C(4)); + assert((m.keys() == std::vector{1, 2, 3})); + LIBCPP_ASSERT((m.values() == std::vector{1, 4, 6})); + assert(m.key_comp() == C(4)); + + // explicit(false) + M m2 = {ks, vs, C(4)}; + assert(m2 == m); + assert(m2.key_comp() == C(4)); + } + { + // flat_map(key_container_type , mapped_container_type, const Allocator&) + using A = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + auto ks = std::vector({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5)); + auto vs = std::deque({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6)); + auto m = M(ks, vs, A(4)); // replaces the allocators + assert(!ks.empty()); // it was an lvalue above + assert(!vs.empty()); // it was an lvalue above + assert((m == M{{1, 1}, {2, 2}, {3, 3}})); + assert(m.keys().get_allocator() == A(4)); + assert(m.values().get_allocator() == A(4)); + } + { + // flat_map(key_container_type , mapped_container_type, const Allocator&) + // explicit(false) + using A = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + auto ks = std::vector({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(5)); + auto vs = std::deque({1, 1, 1, 2, 2, 3, 2, 3, 3}, A(6)); + M m = {ks, vs, A(4)}; // implicit ctor + assert(!ks.empty()); // it was an lvalue above + assert(!vs.empty()); // it was an lvalue above + assert((m == M{{1, 1}, {2, 2}, {3, 3}})); + assert(m.keys().get_allocator() == A(4)); + assert(m.values().get_allocator() == A(4)); + } + { + // flat_map(key_container_type , mapped_container_type, key_compare, const Allocator&) + using C = test_less; + using A = test_allocator; + using M = std::flat_map, std::vector>; + std::vector ks = {1, 1, 1, 2, 2, 3, 2, 3, 3}; + std::vector vs = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + auto m = M(ks, vs, C(4), A(5)); + assert((m.keys() == std::vector{1, 2, 3})); + LIBCPP_ASSERT((m.values() == std::vector{1, 4, 6})); + assert(m.key_comp() == C(4)); + assert(m.keys().get_allocator() == A(5)); + assert(m.values().get_allocator() == A(5)); + + // explicit(false) + M m2 = {ks, vs, C(4), A(5)}; + assert(m2 == m); + assert(m2.key_comp() == C(4)); + assert(m2.keys().get_allocator() == A(5)); + assert(m2.values().get_allocator() == A(5)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy.pass.cpp new file mode 100644 index 00000000000000..fcd0415088c1c9 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy.pass.cpp @@ -0,0 +1,70 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(const flat_map& m); + +#include +#include +#include + +#include "test_macros.h" +#include "../../../test_compare.h" +#include "test_allocator.h" + +int main(int, char**) { + { + using C = test_less; + std::vector> ks({1, 3, 5}, test_allocator(6)); + std::vector> vs({2, 2, 1}, test_allocator(7)); + using M = std::flat_map; + auto mo = M(ks, vs, C(5)); + auto m = mo; + + assert(m.key_comp() == C(5)); + assert(m.keys() == ks); + assert(m.values() == vs); + assert(m.keys().get_allocator() == test_allocator(6)); + assert(m.values().get_allocator() == test_allocator(7)); + + // mo is unchanged + assert(mo.key_comp() == C(5)); + assert(mo.keys() == ks); + assert(mo.values() == vs); + assert(mo.keys().get_allocator() == test_allocator(6)); + assert(mo.values().get_allocator() == test_allocator(7)); + } + { + using C = test_less; + using Ks = std::vector>; + using Vs = std::vector>; + auto ks = Ks({1, 3, 5}, other_allocator(6)); + auto vs = Vs({2, 2, 1}, other_allocator(7)); + using M = std::flat_map; + auto mo = M(Ks(ks, other_allocator(6)), Vs(vs, other_allocator(7)), C(5)); + auto m = mo; + + assert(m.key_comp() == C(5)); + assert(m.keys() == ks); + assert(m.values() == vs); + assert(m.keys().get_allocator() == other_allocator(-2)); + assert(m.values().get_allocator() == other_allocator(-2)); + + // mo is unchanged + assert(mo.key_comp() == C(5)); + assert(mo.keys() == ks); + assert(mo.values() == vs); + assert(mo.keys().get_allocator() == other_allocator(6)); + assert(mo.values().get_allocator() == other_allocator(7)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_alloc.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_alloc.pass.cpp new file mode 100644 index 00000000000000..cbda6ea853268a --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_alloc.pass.cpp @@ -0,0 +1,67 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(const flat_map&, const allocator_type&); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "../../../test_compare.h" +#include "test_allocator.h" + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + using C = test_less; + std::vector> ks({1, 3, 5}, test_allocator(6)); + std::vector> vs({2, 2, 1}, test_allocator(7)); + using M = std::flat_map; + auto mo = M(ks, vs, C(5)); + auto m = M(mo, test_allocator(3)); + + assert(m.key_comp() == C(5)); + assert(m.keys() == ks); + assert(m.values() == vs); + assert(m.keys().get_allocator() == test_allocator(3)); + assert(m.values().get_allocator() == test_allocator(3)); + + // mo is unchanged + assert(mo.key_comp() == C(5)); + assert(mo.keys() == ks); + assert(mo.values() == vs); + assert(mo.keys().get_allocator() == test_allocator(6)); + assert(mo.values().get_allocator() == test_allocator(7)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.addressof.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.addressof.compile.pass.cpp new file mode 100644 index 00000000000000..e9b752d5eb12b0 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.addressof.compile.pass.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map& operator=(const flat_map& s); + +// Validate whether the container can be copy-assigned (move-assigned, swapped) +// with an ADL-hijacking operator& + +#include +#include + +#include "test_macros.h" +#include "operator_hijacker.h" + +void test() { + std::flat_map so; + std::flat_map s; + s = so; + s = std::move(so); + swap(s, so); +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.pass.cpp new file mode 100644 index 00000000000000..4f9797d5bf810a --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.pass.cpp @@ -0,0 +1,92 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map& operator=(const flat_map& m); + +#include +#include +#include +#include + +#include "test_macros.h" +#include "../../../test_compare.h" +#include "test_allocator.h" + +int main(int, char**) { + { + // test_allocator is not propagated + using C = test_less; + std::vector> ks({1, 3, 5}, test_allocator(6)); + std::vector> vs({2, 2, 1}, test_allocator(7)); + using M = std::flat_map; + auto mo = M(ks, vs, C(5)); + auto m = M({{3, 3}, {4, 4}, {5, 5}}, C(3), test_allocator(2)); + m = mo; + + assert(m.key_comp() == C(5)); + assert(m.keys() == ks); + assert(m.values() == vs); + assert(m.keys().get_allocator() == test_allocator(2)); + assert(m.values().get_allocator() == test_allocator(2)); + + // mo is unchanged + assert(mo.key_comp() == C(5)); + assert(mo.keys() == ks); + assert(mo.values() == vs); + assert(mo.keys().get_allocator() == test_allocator(6)); + assert(mo.values().get_allocator() == test_allocator(7)); + } + { + // other_allocator is propagated + using C = test_less; + using Ks = std::vector>; + using Vs = std::vector>; + auto ks = Ks({1, 3, 5}, other_allocator(6)); + auto vs = Vs({2, 2, 1}, other_allocator(7)); + using M = std::flat_map; + auto mo = M(Ks(ks, other_allocator(6)), Vs(vs, other_allocator(7)), C(5)); + auto m = M({{3, 3}, {4, 4}, {5, 5}}, C(3), other_allocator(2)); + m = mo; + + assert(m.key_comp() == C(5)); + assert(m.keys() == ks); + assert(m.values() == vs); + assert(m.keys().get_allocator() == other_allocator(6)); + assert(m.values().get_allocator() == other_allocator(7)); + + // mo is unchanged + assert(mo.key_comp() == C(5)); + assert(mo.keys() == ks); + assert(mo.values() == vs); + assert(mo.keys().get_allocator() == other_allocator(6)); + assert(mo.values().get_allocator() == other_allocator(7)); + } + { + // comparator is copied and invariant is preserved + using M = std::flat_map>; + M mo = M({{1, 2}, {3, 4}}, std::less()); + M m = M({{1, 2}, {3, 4}}, std::greater()); + assert(m.key_comp()(2, 1) == true); + assert(m != mo); + m = mo; + assert(m.key_comp()(2, 1) == false); + assert(m == mo); + } + { + // self-assignment + using M = std::flat_map; + M m = {{1, 2}, {3, 4}}; + m = static_cast(m); + assert((m == M{{1, 2}, {3, 4}})); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.pass.cpp new file mode 100644 index 00000000000000..d01bee9aae9c08 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.pass.cpp @@ -0,0 +1,342 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "deduction_guides_sfinae_checks.h" +#include "test_allocator.h" + +using P = std::pair; +using PC = std::pair; + +void test_copy() { + { + std::flat_map source = {{1, 2}, {2, 3}}; + std::flat_map s(source); + ASSERT_SAME_TYPE(decltype(s), decltype(source)); + assert(s == source); + } + { + std::flat_map> source = {{1, 2}, {2, 3}}; + std::flat_map s{source}; // braces instead of parens + ASSERT_SAME_TYPE(decltype(s), decltype(source)); + assert(s == source); + } + { + std::flat_map> source = {{1, 2}, {2, 3}}; + std::flat_map s(source, std::allocator()); + ASSERT_SAME_TYPE(decltype(s), decltype(source)); + assert(s == source); + } +} + +void test_containers() { + std::deque> ks({1, 2, 1, INT_MAX, 3}, test_allocator(0, 42)); + std::deque> vs({1, 2, 1, 4, 5}, test_allocator(0, 43)); + std::deque> sorted_ks({1, 2, 3, INT_MAX}, test_allocator(0, 42)); + std::deque> sorted_vs({1, 2, 5, 4}, test_allocator(0, 43)); + const std::pair expected[] = {{1, 1}, {2, 2}, {3, 5}, {INT_MAX, 4}}; + { + std::flat_map s(ks, vs); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 42); + assert(s.values().get_allocator().get_id() == 43); + } + { + std::flat_map s(std::sorted_unique, sorted_ks, sorted_vs); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 42); + assert(s.values().get_allocator().get_id() == 43); + } + { + std::flat_map s(ks, vs, test_allocator(0, 44)); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 44); + assert(s.values().get_allocator().get_id() == 44); + } + { + std::flat_map s(std::sorted_unique, sorted_ks, sorted_vs, test_allocator(0, 44)); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 44); + assert(s.values().get_allocator().get_id() == 44); + } +} + +void test_containers_compare() { + std::deque> ks({1, 2, 1, INT_MAX, 3}, test_allocator(0, 42)); + std::deque> vs({1, 2, 1, 4, 5}, test_allocator(0, 43)); + std::deque> sorted_ks({INT_MAX, 3, 2, 1}, test_allocator(0, 42)); + std::deque> sorted_vs({4, 5, 2, 1}, test_allocator(0, 43)); + const std::pair expected[] = {{INT_MAX, 4}, {3, 5}, {2, 2}, {1, 1}}; + { + std::flat_map s(ks, vs, std::greater()); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 42); + assert(s.values().get_allocator().get_id() == 43); + } + { + std::flat_map s(std::sorted_unique, sorted_ks, sorted_vs, std::greater()); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 42); + assert(s.values().get_allocator().get_id() == 43); + } + { + std::flat_map s(ks, vs, std::greater(), test_allocator(0, 44)); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 44); + assert(s.values().get_allocator().get_id() == 44); + } + { + std::flat_map s(std::sorted_unique, sorted_ks, sorted_vs, std::greater(), test_allocator(0, 44)); + + ASSERT_SAME_TYPE(decltype(s), std::flat_map, decltype(ks), decltype(vs)>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 44); + assert(s.values().get_allocator().get_id() == 44); + } +} + +void test_iter_iter() { + const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}}; + const P sorted_arr[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}}; + const PC arrc[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}}; + const PC sorted_arrc[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}}; + { + std::flat_map m(std::begin(arr), std::end(arr)); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::begin(arrc), std::end(arrc)); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::sorted_unique, std::begin(sorted_arr), std::end(sorted_arr)); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::sorted_unique, std::begin(sorted_arrc), std::end(sorted_arrc)); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map mo; + std::flat_map m(mo.begin(), mo.end()); + ASSERT_SAME_TYPE(decltype(m), decltype(mo)); + } + { + std::flat_map mo; + std::flat_map m(mo.cbegin(), mo.cend()); + ASSERT_SAME_TYPE(decltype(m), decltype(mo)); + } +} + +void test_iter_iter_compare() { + const P arr[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}}; + const P sorted_arr[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}}; + const PC arrc[] = {{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}}; + const PC sorted_arrc[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}}; + using C = std::greater; + { + std::flat_map m(std::begin(arr), std::end(arr), C()); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::begin(arrc), std::end(arrc), C()); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::sorted_unique, std::begin(sorted_arr), std::end(sorted_arr), C()); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::sorted_unique, std::begin(sorted_arrc), std::end(sorted_arrc), C()); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map mo; + std::flat_map m(mo.begin(), mo.end(), C()); + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + } + { + std::flat_map mo; + std::flat_map m(mo.cbegin(), mo.cend(), C()); + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + } +} + +void test_initializer_list() { + const P sorted_arr[] = {{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}}; + { + std::flat_map m{std::pair{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}}; + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::sorted_unique, {std::pair{1, 1L}, {2, 2L}, {3, 1L}, {INT_MAX, 1L}}); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } +} + +void test_initializer_list_compare() { + const P sorted_arr[] = {{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}}; + using C = std::greater; + { + std::flat_map m({std::pair{1, 1L}, {2, 2L}, {1, 1L}, {INT_MAX, 1L}, {3, 1L}}, C()); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } + { + std::flat_map m(std::sorted_unique, {std::pair{INT_MAX, 1L}, {3, 1L}, {2, 2L}, {1, 1L}}, C()); + + ASSERT_SAME_TYPE(decltype(m), std::flat_map); + assert(std::ranges::equal(m, sorted_arr)); + } +} + +void test_from_range() { + std::list> r = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 4}, {3, 5}}; + const std::pair expected[] = {{1, 1}, {2, 2}, {3, 5}, {INT_MAX, 4}}; + { + std::flat_map s(std::from_range, r); + ASSERT_SAME_TYPE(decltype(s), std::flat_map>); + assert(std::ranges::equal(s, expected)); + } + { + std::flat_map s(std::from_range, r, test_allocator(0, 42)); + ASSERT_SAME_TYPE( + decltype(s), + std::flat_map, + std::vector>, + std::vector>>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 42); + assert(s.values().get_allocator().get_id() == 42); + } +} + +void test_from_range_compare() { + std::list> r = {{1, 1}, {2, 2}, {1, 1}, {INT_MAX, 4}, {3, 5}}; + const std::pair expected[] = {{INT_MAX, 4}, {3, 5}, {2, 2}, {1, 1}}; + { + std::flat_map s(std::from_range, r, std::greater()); + ASSERT_SAME_TYPE(decltype(s), std::flat_map>); + assert(std::ranges::equal(s, expected)); + } + { + std::flat_map s(std::from_range, r, std::greater(), test_allocator(0, 42)); + ASSERT_SAME_TYPE( + decltype(s), + std::flat_map, + std::vector>, + std::vector>>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().get_id() == 42); + assert(s.values().get_allocator().get_id() == 42); + } +} + +int main(int, char**) { + // Each test function also tests the sorted_unique-prefixed and allocator-suffixed overloads. + test_copy(); + test_containers(); + test_containers_compare(); + test_iter_iter(); + test_iter_iter_compare(); + test_initializer_list(); + test_initializer_list_compare(); + test_from_range(); + test_from_range_compare(); + + AssociativeContainerDeductionGuidesSfinaeAway>(); + { + std::flat_map s = {std::make_pair(1, 'a')}; // flat_map(initializer_list>) + ASSERT_SAME_TYPE(decltype(s), std::flat_map); + assert(s.size() == 1); + } + { + using M = std::flat_map; + M m; + std::flat_map s = {std::make_pair(m, m)}; // flat_map(initializer_list>) + ASSERT_SAME_TYPE(decltype(s), std::flat_map); + assert(s.size() == 1); + assert(s[m] == m); + } + + { + std::pair source[3] = {{1, 1}, {2, 2}, {3, 3}}; + std::flat_map s = {source, source + 3}; // flat_map(InputIterator, InputIterator) + ASSERT_SAME_TYPE(decltype(s), std::flat_map); + assert(s.size() == 3); + } + { + std::pair source[3] = {{1, 1}, {2, 2}, {3, 3}}; + std::flat_map s{source, source + 3}; // flat_map(InputIterator, InputIterator) + ASSERT_SAME_TYPE(decltype(s), std::flat_map); + assert(s.size() == 3); + } + { + std::pair source[3] = {{1, 1}, {2, 2}, {3, 3}}; + std::flat_map s{std::sorted_unique, source, source + 3}; // flat_map(sorted_unique_t, InputIterator, InputIterator) + static_assert(std::is_same_v>); + assert(s.size() == 3); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.verify.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.verify.cpp new file mode 100644 index 00000000000000..08244f01cb24e1 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.verify.cpp @@ -0,0 +1,97 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// Test CTAD on cases where deduction should fail. + +#include +#include +#include +#include +#include + +struct NotAnAllocator { + friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; } +}; + +using P = std::pair; +using PC = std::pair; + +void test() { + { + // cannot deduce Key and T from just (KeyContainer), even if it's a container of pairs + std::vector> v; + std::flat_map s(v); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce Key and T from just (KeyContainer, Allocator) + std::vector v; + std::flat_map s(v, std::allocator>()); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce Key and T from nothing + std::flat_map m; + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce Key and T from just (Compare) + std::flat_map m(std::less{}); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce Key and T from just (Compare, Allocator) + std::flat_map m(std::less{}, std::allocator{}); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce Key and T from just (Allocator) + std::flat_map m(std::allocator{}); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot convert from some arbitrary unrelated type + NotAnAllocator a; + std::flat_map m(a); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce that the inner braced things should be std::pair and not something else + std::flat_map m{{1, 1L}, {2, 2L}, {3, 3L}}; + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce that the inner braced things should be std::pair and not something else + std::flat_map m({{1, 1L}, {2, 2L}, {3, 3L}}, std::less()); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce that the inner braced things should be std::pair and not something else + std::flat_map m({{1, 1L}, {2, 2L}, {3, 3L}}, std::less(), std::allocator()); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // cannot deduce that the inner braced things should be std::pair and not something else + std::flat_map m({{1, 1L}, {2, 2L}, {3, 3L}}, std::allocator()); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // since we have parens, not braces, this deliberately does not find the initializer_list constructor + std::flat_map m(P{1, 1L}); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } + { + // since we have parens, not braces, this deliberately does not find the initializer_list constructor + std::flat_map m(PC{1, 1L}); + // expected-error-re@-1{{{{no viable constructor or deduction guide for deduction of template arguments of '.*flat_map'}}}} + } +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct_pmr.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct_pmr.pass.cpp new file mode 100644 index 00000000000000..11c18ac13c76a9 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct_pmr.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// UNSUPPORTED: availability-pmr-missing + +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test_allocator.h" + +using P = std::pair; +using PC = std::pair; + +void test_containers() { + std::deque> ks({1, 2, 1, INT_MAX, 3}, test_allocator(0, 42)); + std::deque> vs({1, 2, 1, 4, 5}, test_allocator(0, 43)); + std::deque> sorted_ks({1, 2, 3, INT_MAX}, test_allocator(0, 42)); + std::deque> sorted_vs({1, 2, 5, 4}, test_allocator(0, 43)); + const std::pair expected[] = {{1, 1}, {2, 2}, {3, 5}, {INT_MAX, 4}}; + { + std::pmr::monotonic_buffer_resource mr; + std::pmr::monotonic_buffer_resource mr2; + std::pmr::deque pks(ks.begin(), ks.end(), &mr); + std::pmr::deque pvs(vs.begin(), vs.end(), &mr); + std::flat_map s(std::move(pks), std::move(pvs), &mr2); + + ASSERT_SAME_TYPE( + decltype(s), std::flat_map, std::pmr::deque, std::pmr::deque>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().resource() == &mr2); + assert(s.values().get_allocator().resource() == &mr2); + } + { + std::pmr::monotonic_buffer_resource mr; + std::pmr::monotonic_buffer_resource mr2; + std::pmr::deque pks(sorted_ks.begin(), sorted_ks.end(), &mr); + std::pmr::deque pvs(sorted_vs.begin(), sorted_vs.end(), &mr); + std::flat_map s(std::sorted_unique, std::move(pks), std::move(pvs), &mr2); + + ASSERT_SAME_TYPE( + decltype(s), std::flat_map, std::pmr::deque, std::pmr::deque>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().resource() == &mr2); + assert(s.values().get_allocator().resource() == &mr2); + } +} + +void test_containers_compare() { + std::deque> ks({1, 2, 1, INT_MAX, 3}, test_allocator(0, 42)); + std::deque> vs({1, 2, 1, 4, 5}, test_allocator(0, 43)); + std::deque> sorted_ks({INT_MAX, 3, 2, 1}, test_allocator(0, 42)); + std::deque> sorted_vs({4, 5, 2, 1}, test_allocator(0, 43)); + const std::pair expected[] = {{INT_MAX, 4}, {3, 5}, {2, 2}, {1, 1}}; + { + std::pmr::monotonic_buffer_resource mr; + std::pmr::monotonic_buffer_resource mr2; + std::pmr::deque pks(ks.begin(), ks.end(), &mr); + std::pmr::deque pvs(vs.begin(), vs.end(), &mr); + std::flat_map s(std::move(pks), std::move(pvs), std::greater(), &mr2); + + ASSERT_SAME_TYPE( + decltype(s), std::flat_map, std::pmr::deque, std::pmr::deque>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().resource() == &mr2); + assert(s.values().get_allocator().resource() == &mr2); + } + { + std::pmr::monotonic_buffer_resource mr; + std::pmr::monotonic_buffer_resource mr2; + std::pmr::deque pks(sorted_ks.begin(), sorted_ks.end(), &mr); + std::pmr::deque pvs(sorted_vs.begin(), sorted_vs.end(), &mr); + std::flat_map s(std::sorted_unique, std::move(pks), std::move(pvs), std::greater(), &mr2); + + ASSERT_SAME_TYPE( + decltype(s), std::flat_map, std::pmr::deque, std::pmr::deque>); + assert(std::ranges::equal(s, expected)); + assert(s.keys().get_allocator().resource() == &mr2); + assert(s.values().get_allocator().resource() == &mr2); + } +} + +int main(int, char**) { + test_containers(); + test_containers_compare(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default.pass.cpp new file mode 100644 index 00000000000000..c5b94896b92931 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(); + +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "min_allocator.h" +#include "test_allocator.h" + +struct DefaultCtableComp { + explicit DefaultCtableComp() { default_constructed_ = true; } + bool operator()(int, int) const { return false; } + bool default_constructed_ = false; +}; + +int main(int, char**) { + { + std::flat_map m; + assert(m.empty()); + } + { + // explicit(false) + std::flat_map m = {}; + assert(m.empty()); + } + { + std::flat_map>> m; + assert(m.empty()); + assert(m.begin() == m.end()); + assert(m.key_comp().default_constructed_); + } + { + using A1 = explicit_allocator; + using A2 = explicit_allocator; + { + std::flat_map, std::vector> m; + assert(m.empty()); + assert(m.key_comp().default_constructed_); + } + { + A1 a1; + std::flat_map, std::vector> m(a1); + assert(m.empty()); + assert(m.key_comp().default_constructed_); + } + } + { + // If an allocator is given, it must be usable by both containers. + using A = test_allocator; + using M = std::flat_map, std::vector, std::vector>; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v>); + static_assert(!std::is_constructible_v); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default_noexcept.pass.cpp new file mode 100644 index 00000000000000..ac24c8a8ac067e --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default_noexcept.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map() +// noexcept( +// is_nothrow_default_constructible_v && +// is_nothrow_default_constructible_v && +// is_nothrow_default_constructible_v); + +// This tests a conforming extension + +#include +#include +#include +#include + +#include "test_macros.h" +#include "MoveOnly.h" +#include "test_allocator.h" + +struct ThrowingCtorComp { + ThrowingCtorComp() noexcept(false) {} + bool operator()(const auto&, const auto&) const { return false; } +}; + +int main(int, char**) { +#if defined(_LIBCPP_VERSION) + { + using C = std::flat_map; + static_assert(std::is_nothrow_default_constructible_v); + } + { + using C = std::flat_map, std::vector>>; + static_assert(std::is_nothrow_default_constructible_v); + } +#endif // _LIBCPP_VERSION + { + using C = std::flat_map, std::vector>>; + static_assert(!std::is_nothrow_default_constructible_v); + C c; + } + { + using C = std::flat_map; + static_assert(!std::is_nothrow_default_constructible_v); + C c; + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/dtor_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/dtor_noexcept.pass.cpp new file mode 100644 index 00000000000000..e3ab33a55d95bf --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/dtor_noexcept.pass.cpp @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// ~flat_map(); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "MoveOnly.h" +#include "test_allocator.h" + +struct ThrowingDtorComp { + bool operator()(const auto&, const auto&) const; + ~ThrowingDtorComp() noexcept(false); +}; + +int main(int, char**) { + { + using C = std::flat_map; + static_assert(std::is_nothrow_destructible_v); + } + { + using V = std::vector>; + using C = std::flat_map, V, V>; + static_assert(std::is_nothrow_destructible_v); + } + { + using V = std::deque>; + using C = std::flat_map, V, V>; + static_assert(std::is_nothrow_destructible_v); + } +#if defined(_LIBCPP_VERSION) + { + using C = std::flat_map; + static_assert(!std::is_nothrow_destructible_v); + } +#endif // _LIBCPP_VERSION + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list.pass.cpp new file mode 100644 index 00000000000000..7a22746845d002 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list.pass.cpp @@ -0,0 +1,157 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(initializer_list il, const key_compare& comp = key_compare()); +// template +// flat_map(initializer_list il, const Alloc& a); +// template +// flat_map(initializer_list il, const key_compare& comp, const Alloc& a); + +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "min_allocator.h" +#include "test_allocator.h" + +#include "../../../test_compare.h" + +struct DefaultCtableComp { + explicit DefaultCtableComp() { default_constructed_ = true; } + bool operator()(int, int) const { return false; } + bool default_constructed_ = false; +}; + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + using IL = std::initializer_list>; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + + { + // initializer_list needs to match exactly + using M = std::flat_map; + using C = typename M::key_compare; + static_assert(std::is_constructible_v>>); + static_assert(std::is_constructible_v>, C>); + static_assert(std::is_constructible_v>, C, std::allocator>); + static_assert(std::is_constructible_v>, std::allocator>); + static_assert(!std::is_constructible_v>>); + static_assert(!std::is_constructible_v>, C>); + static_assert( + !std::is_constructible_v>, C, std::allocator>); + static_assert(!std::is_constructible_v>, std::allocator>); + static_assert(!std::is_constructible_v>>); + static_assert(!std::is_constructible_v>, C>); + static_assert( + !std::is_constructible_v>, C, std::allocator>); + static_assert( + !std::is_constructible_v>, std::allocator>); + } + + std::pair expected[] = {{1, 1}, {2, 2}, {3, 3}, {5, 2}}; + { + // flat_map(initializer_list); + using M = std::flat_map; + std::initializer_list> il = {{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}; + M m(il); + assert(std::equal(m.begin(), m.end(), expected, expected + 4)); + } + { + // flat_map(initializer_list); + // explicit(false) + using M = std::flat_map; + M m = {{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}; + assert(std::equal(m.begin(), m.end(), expected, expected + 4)); + } + { + // flat_map(initializer_list); + using M = std::flat_map, std::deque>>; + M m = {{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}; + assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); + } + { + using A = explicit_allocator; + { + // flat_map(initializer_list); + // different comparator + using M = std::flat_map, std::deque>; + M m = {{1, 1}, {2, 2}, {3, 3}}; + assert(m.size() == 1); + assert(m.begin()->first == m.begin()->second); + LIBCPP_ASSERT(*m.begin() == std::make_pair(1, 1)); + assert(m.key_comp().default_constructed_); + } + { + // flat_map(initializer_list, const Allocator&); + using M = std::flat_map, std::deque, std::vector>; + A a; + M m({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, a); + assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); + } + } + { + // flat_map(initializer_list, const key_compare&); + using C = test_less; + using M = std::flat_map; + auto m = M({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, C(10)); + assert(std::equal(m.begin(), m.end(), expected, expected + 4)); + assert(m.key_comp() == C(10)); + + // explicit(false) + M m2 = {{{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, C(10)}; + assert(m2 == m); + assert(m2.key_comp() == C(10)); + } + { + // flat_map(initializer_list, const key_compare&); + // Sorting uses the comparator that was passed in + using M = std::flat_map, std::deque>>; + auto m = M({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, std::greater()); + assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); + assert(m.key_comp()(2, 1) == true); + } + { + // flat_map(initializer_list il, const key_compare& comp, const Alloc& a); + using A = explicit_allocator; + using M = std::flat_map, std::deque, std::vector>; + A a; + M m({{5, 2}, {2, 2}, {2, 2}, {3, 3}, {1, 1}, {3, 3}}, {}, a); + assert(std::equal(m.rbegin(), m.rend(), expected, expected + 4)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter.pass.cpp new file mode 100644 index 00000000000000..7c0c487969943d --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter.pass.cpp @@ -0,0 +1,154 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// flat_map(InputIterator first, InputIterator last, const key_compare& comp = key_compare()); +// template +// flat_map(InputIterator first, InputIterator last, const Allocator& a); +// template +// flat_map(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a); + +#include +#include +#include +#include +#include + +#include "min_allocator.h" +#include "test_allocator.h" +#include "test_iterators.h" +#include "test_macros.h" +#include "../../../test_compare.h" + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + using Iter1 = typename M1::iterator; + using Iter2 = typename M2::iterator; + using Iter3 = typename M3::iterator; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + + using P = std::pair; + P ar[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}, {3, 8}, {3, 9}}; + P expected[] = {{1, 1}, {2, 4}, {3, 6}}; + { + // flat_map(InputIterator , InputIterator) + // cpp17_input_iterator + using M = std::flat_map; + auto m = M(cpp17_input_iterator(ar), cpp17_input_iterator(ar + 9)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + + // explicit(false) + M m2 = {cpp17_input_iterator(ar), cpp17_input_iterator(ar + 9)}; + assert(m2 == m); + } + { + // flat_map(InputIterator , InputIterator) + // greater + using M = std::flat_map, std::deque>, std::deque>; + auto m = M(cpp17_input_iterator(ar), cpp17_input_iterator(ar + 9)); + assert((m.keys() == std::deque>{3, 2, 1})); + LIBCPP_ASSERT((m.values() == std::deque{6, 4, 1})); + } + { + // flat_map(InputIterator , InputIterator) + // Test when the operands are of array type (also contiguous iterator type) + using M = std::flat_map, std::vector>>; + auto m = M(ar, ar); + assert(m.empty()); + } + { + // flat_map(InputIterator , InputIterator, const key_compare&) + using C = test_less; + using M = std::flat_map, std::deque>; + auto m = M(ar, ar + 9, C(3)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.key_comp() == C(3)); + + // explicit(false) + M m2 = {ar, ar + 9, C(3)}; + assert(m2 == m); + assert(m2.key_comp() == C(3)); + } + { + // flat_map(InputIterator , InputIterator, const Allocator&) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + auto m = M(ar, ar + 9, A1(5)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(InputIterator , InputIterator, const Allocator&) + // explicit(false) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + M m = {ar, ar + 9, A1(5)}; // implicit ctor + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(InputIterator , InputIterator, const key_compare&, const Allocator&) + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque>; + auto m = M(ar, ar + 9, C(3), A1(5)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.key_comp() == C(3)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(InputIterator , InputIterator, const key_compare&, const Allocator&) + // explicit(false) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque, std::vector>; + M m = {ar, ar + 9, {}, A2(5)}; // implicit ctor + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_stability.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_stability.pass.cpp new file mode 100644 index 00000000000000..1ce859f6c737ea --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_stability.pass.cpp @@ -0,0 +1,65 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// flat_map(InputIterator first, InputIterator last, const key_compare& comp = key_compare()) +// +// libc++ uses stable_sort to ensure that flat_map's behavior matches map's, +// in terms of which duplicate items are kept. +// This tests a conforming extension. + +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" + +struct Mod256 { + bool operator()(int x, int y) const { return (x % 256) < (y % 256); } +}; + +int main(int, char**) { + std::mt19937 randomness; + std::pair pairs[200]; + for (auto& pair : pairs) { + pair = {uint16_t(randomness()), uint16_t(randomness())}; + } + + { + std::map m(pairs, pairs + 200); + std::flat_map fm(pairs, pairs + 200); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + { + std::map m(pairs, pairs + 200, std::allocator()); + std::flat_map fm(pairs, pairs + 200, std::allocator()); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + { + std::map m(pairs, pairs + 200, Mod256()); + std::flat_map fm(pairs, pairs + 200, Mod256()); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + { + std::map m(pairs, pairs + 200, Mod256(), std::allocator()); + std::flat_map fm(pairs, pairs + 200, Mod256(), std::allocator()); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move.pass.cpp new file mode 100644 index 00000000000000..955d3156064aae --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move.pass.cpp @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(flat_map&&); + +#include +#include +#include +#include +#include +#include + +#include "../helpers.h" +#include "test_macros.h" +#include "../../../test_compare.h" +#include "test_allocator.h" +#include "min_allocator.h" + +int main(int, char**) { + { + using C = test_less; + using A = test_allocator; + using M = std::flat_map, std::deque>; + M mo = M({{1, 1}, {2, 2}, {3, 1}}, C(5), A(7)); + M m = std::move(mo); + assert((m == M{{1, 1}, {2, 2}, {3, 1}})); + assert(m.key_comp() == C(5)); + assert(m.keys().get_allocator() == A(7)); + assert(m.values().get_allocator() == A(7)); + + assert(mo.empty()); + assert(mo.key_comp() == C(5)); + assert(mo.keys().get_allocator().get_id() == test_alloc_base::moved_value); + assert(mo.values().get_allocator().get_id() == test_alloc_base::moved_value); + } + { + using C = test_less; + using A = min_allocator; + using M = std::flat_map, std::deque>; + M mo = M({{1, 1}, {2, 2}, {3, 1}}, C(5), A()); + M m = std::move(mo); + assert((m == M{{1, 1}, {2, 2}, {3, 1}})); + assert(m.key_comp() == C(5)); + assert(m.keys().get_allocator() == A()); + assert(m.values().get_allocator() == A()); + + assert(mo.empty()); + assert(mo.key_comp() == C(5)); + assert(m.keys().get_allocator() == A()); + assert(m.values().get_allocator() == A()); + } + { + // A moved-from flat_map maintains its class invariant in the presence of moved-from comparators. + using M = std::flat_map>; + M mo = M({{1, 1}, {2, 2}, {3, 1}}, std::less()); + M m = std::move(mo); + assert(m.size() == 3); + assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); + assert(m.key_comp()(1, 2) == true); + + assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp())); + LIBCPP_ASSERT(m.key_comp()(1, 2) == true); + LIBCPP_ASSERT(mo.empty()); + mo.insert({{1, 1}, {2, 2}, {3, 1}}); // insert has no preconditions + assert(m == mo); + } + { + // moved-from object maintains invariant if one of underlying container does not clear after move + using M = std::flat_map, std::vector, CopyOnlyVector>; + M m1 = M({1, 2, 3}, {1, 2, 3}); + M m2 = std::move(m1); + assert(m2.size() == 3); + check_invariant(m1); + LIBCPP_ASSERT(m1.empty()); + LIBCPP_ASSERT(m1.keys().size() == 0); + LIBCPP_ASSERT(m1.values().size() == 0); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_alloc.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_alloc.pass.cpp new file mode 100644 index 00000000000000..93a39764225200 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_alloc.pass.cpp @@ -0,0 +1,82 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(flat_map&&, const allocator_type&); + +#include +#include +#include +#include +#include +#include + +#include "../helpers.h" +#include "test_macros.h" +#include "../../../test_compare.h" +#include "test_allocator.h" + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + std::pair expected[] = {{1, 1}, {2, 2}, {3, 1}}; + using C = test_less; + using A = test_allocator; + using M = std::flat_map, std::deque>; + auto mo = M(expected, expected + 3, C(5), A(7)); + auto m = M(std::move(mo), A(3)); + + assert(m.key_comp() == C(5)); + assert(m.size() == 3); + auto [keys, values] = std::move(m).extract(); + assert(keys.get_allocator() == A(3)); + assert(values.get_allocator() == A(3)); + assert(std::ranges::equal(keys, expected | std::views::elements<0>)); + assert(std::ranges::equal(values, expected | std::views::elements<1>)); + + // The original flat_map is moved-from. + assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp())); + assert(mo.empty()); + assert(mo.key_comp() == C(5)); + assert(mo.keys().get_allocator() == A(7)); + assert(mo.values().get_allocator() == A(7)); + } + { + // moved-from object maintains invariant if one of underlying container does not clear after move + using M = std::flat_map, std::vector, CopyOnlyVector>; + M m1 = M({1, 2, 3}, {1, 2, 3}); + M m2(std::move(m1), std::allocator{}); + assert(m2.size() == 3); + check_invariant(m1); + LIBCPP_ASSERT(m1.empty()); + LIBCPP_ASSERT(m1.keys().size() == 0); + LIBCPP_ASSERT(m1.values().size() == 0); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign.pass.cpp new file mode 100644 index 00000000000000..a94c442c695ddb --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign.pass.cpp @@ -0,0 +1,74 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map& operator=(flat_map&&); + +#include +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "MoveOnly.h" +#include "../../../test_compare.h" +#include "test_allocator.h" +#include "min_allocator.h" + +int main(int, char**) { + { + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector>; + M mo = M({{1, 1}, {2, 3}, {3, 2}}, C(5), A1(7)); + M m = M({}, C(3), A1(7)); + m = std::move(mo); + assert((m == M{{1, 1}, {2, 3}, {3, 2}})); + assert(m.key_comp() == C(5)); + auto [ks, vs] = std::move(m).extract(); + assert(ks.get_allocator() == A1(7)); + assert(vs.get_allocator() == A2(7)); + assert(mo.empty()); + } + { + using C = test_less; + using A1 = other_allocator; + using A2 = other_allocator; + using M = std::flat_map, std::deque>; + M mo = M({{4, 5}, {5, 4}}, C(5), A1(7)); + M m = M({{1, 1}, {2, 2}, {3, 3}, {4, 4}}, C(3), A1(7)); + m = std::move(mo); + assert((m == M{{4, 5}, {5, 4}})); + assert(m.key_comp() == C(5)); + auto [ks, vs] = std::move(m).extract(); + assert(ks.get_allocator() == A1(7)); + assert(vs.get_allocator() == A2(7)); + assert(mo.empty()); + } + { + using A = min_allocator; + using M = std::flat_map, std::vector, std::vector>; + M mo = M({{5, 1}, {4, 2}, {3, 3}}, A()); + M m = M({{4, 4}, {3, 3}, {2, 2}, {1, 1}}, A()); + m = std::move(mo); + assert((m == M{{5, 1}, {4, 2}, {3, 3}})); + auto [ks, vs] = std::move(m).extract(); + assert(ks.get_allocator() == A()); + assert(vs.get_allocator() == A()); + assert(mo.empty()); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_clears.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_clears.pass.cpp new file mode 100644 index 00000000000000..f28d52dd4e4633 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_clears.pass.cpp @@ -0,0 +1,104 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map& operator=(flat_map&&); +// Preserves the class invariant for the moved-from flat_map. + +#include +#include +#include +#include +#include +#include +#include + +#include "../helpers.h" +#include "test_macros.h" + +struct MoveNegates { + int value_ = 0; + MoveNegates() = default; + MoveNegates(int v) : value_(v) {} + MoveNegates(MoveNegates&& rhs) : value_(rhs.value_) { rhs.value_ = -rhs.value_; } + MoveNegates& operator=(MoveNegates&& rhs) { + value_ = rhs.value_; + rhs.value_ = -rhs.value_; + return *this; + } + ~MoveNegates() = default; + auto operator<=>(const MoveNegates&) const = default; +}; + +struct MoveClears { + int value_ = 0; + MoveClears() = default; + MoveClears(int v) : value_(v) {} + MoveClears(MoveClears&& rhs) : value_(rhs.value_) { rhs.value_ = 0; } + MoveClears& operator=(MoveClears&& rhs) { + value_ = rhs.value_; + rhs.value_ = 0; + return *this; + } + ~MoveClears() = default; + auto operator<=>(const MoveClears&) const = default; +}; + +int main(int, char**) { + auto value_eq = [](auto&& p, auto&& q) { return p.first == q.first; }; + { + const std::pair expected[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}; + using M = std::flat_map, std::vector>; + M m = M(expected, expected + 8); + M m2 = M(expected, expected + 3); + + m2 = std::move(m); + + assert(std::equal(m2.begin(), m2.end(), expected, expected + 8)); + LIBCPP_ASSERT(m.empty()); + assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); // still sorted + assert(std::adjacent_find(m.begin(), m.end(), value_eq) == m.end()); // still contains no duplicates + m.insert({1, 1}); + m.insert({2, 2}); + assert(m.contains(1)); + assert(m.find(2) != m.end()); + } + { + const std::pair expected[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}; + using M = std::flat_map, std::vector>; + M m = M(expected, expected + 8); + M m2 = M(expected, expected + 3); + + m2 = std::move(m); + + assert(std::equal(m2.begin(), m2.end(), expected, expected + 8)); + LIBCPP_ASSERT(m.empty()); + assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); // still sorted + assert(std::adjacent_find(m.begin(), m.end(), value_eq) == m.end()); // still contains no duplicates + m.insert({1, 1}); + m.insert({2, 2}); + assert(m.contains(1)); + assert(m.find(2) != m.end()); + } + { + // moved-from object maintains invariant if one of underlying container does not clear after move + using M = std::flat_map, std::vector, CopyOnlyVector>; + M m1 = M({1, 2, 3}, {1, 2, 3}); + M m2 = M({1, 2}, {1, 2}); + m2 = std::move(m1); + assert(m2.size() == 3); + check_invariant(m1); + LIBCPP_ASSERT(m1.empty()); + LIBCPP_ASSERT(m1.keys().size() == 0); + LIBCPP_ASSERT(m1.values().size() == 0); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_noexcept.pass.cpp new file mode 100644 index 00000000000000..665b763e6c4f75 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_noexcept.pass.cpp @@ -0,0 +1,110 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map& operator=(flat_map&& c) +// noexcept( +// is_nothrow_move_assignable::value && +// is_nothrow_move_assignable::value && +// is_nothrow_copy_assignable::value); + +// This tests a conforming extension + +#include +#include +#include +#include +#include + +#include "MoveOnly.h" +#include "test_allocator.h" +#include "test_macros.h" + +struct MoveSensitiveComp { + MoveSensitiveComp() noexcept(false) = default; + MoveSensitiveComp(const MoveSensitiveComp&) noexcept(false) = default; + MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; } + MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept = default; + MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) { + rhs.is_moved_from_ = true; + return *this; + } + bool operator()(const auto&, const auto&) const { return false; } + bool is_moved_from_ = false; +}; + +struct MoveThrowsComp { + MoveThrowsComp(MoveThrowsComp&&) noexcept(false); + MoveThrowsComp(const MoveThrowsComp&) noexcept(true); + MoveThrowsComp& operator=(MoveThrowsComp&&) noexcept(false); + MoveThrowsComp& operator=(const MoveThrowsComp&) noexcept(true); + bool operator()(const auto&, const auto&) const; +}; + +int main(int, char**) { + { + using C = std::flat_map; + LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v); + } + { + using C = + std::flat_map, + std::vector>, + std::vector>>; + static_assert(!std::is_nothrow_move_assignable_v); + } + { + using C = + std::flat_map, + std::vector>, + std::vector>>; + static_assert(!std::is_nothrow_move_assignable_v); + } + { + using C = + std::flat_map, + std::vector>, + std::vector>>; + LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v); + } + { + using C = + std::flat_map, + std::vector>, + std::vector>>; + LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v); + } + { + // Test with a comparator that throws on move-assignment. + using C = std::flat_map; + LIBCPP_STATIC_ASSERT(!std::is_nothrow_move_assignable_v); + } + { + // Test with a container that throws on move-assignment. + using C = std::flat_map, std::pmr::vector, std::vector>; + static_assert(!std::is_nothrow_move_assignable_v); + } + { + // Test with a container that throws on move-assignment. + using C = std::flat_map, std::vector, std::pmr::vector>; + static_assert(!std::is_nothrow_move_assignable_v); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_exceptions.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_exceptions.pass.cpp new file mode 100644 index 00000000000000..cb7e30c2b74fae --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_exceptions.pass.cpp @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// UNSUPPORTED: no-exceptions + +// + +// flat_map(flat_map&& s); +// If any member function in [flat.map.defn] exits via an exception, the invariant is restored. + +#include +#include +#include +#include +#include +#include + +#include "../helpers.h" +#include "test_macros.h" + +static int countdown = 0; + +struct EvilContainer : std::vector { + EvilContainer() = default; + EvilContainer(EvilContainer&& rhs) { + // Throw on move-construction. + if (--countdown == 0) { + rhs.insert(rhs.end(), 0); + rhs.insert(rhs.end(), 0); + throw 42; + } + } +}; + +int main(int, char**) { + { + using M = std::flat_map, EvilContainer, std::vector>; + M mo = {{1, 1}, {2, 2}, {3, 3}}; + countdown = 1; + try { + M m = std::move(mo); + assert(false); // not reached + } catch (int x) { + assert(x == 42); + } + // The source flat_map maintains its class invariant. + check_invariant(mo); + LIBCPP_ASSERT(mo.empty()); + } + { + using M = std::flat_map, std::vector, EvilContainer>; + M mo = {{1, 1}, {2, 2}, {3, 3}}; + countdown = 1; + try { + M m = std::move(mo); + assert(false); // not reached + } catch (int x) { + assert(x == 42); + } + // The source flat_map maintains its class invariant. + check_invariant(mo); + LIBCPP_ASSERT(mo.empty()); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_noexcept.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_noexcept.pass.cpp new file mode 100644 index 00000000000000..d281dafbcf72dd --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_noexcept.pass.cpp @@ -0,0 +1,102 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(flat_map&&) +// noexcept(is_nothrow_move_constructible::value && +// is_nothrow_move_constructible::value && +// is_nothrow_copy_constructible::value); + +// This tests a conforming extension + +#include +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "MoveOnly.h" +#include "test_allocator.h" + +template +struct ThrowingMoveAllocator { + using value_type = T; + explicit ThrowingMoveAllocator() = default; + ThrowingMoveAllocator(const ThrowingMoveAllocator&) = default; + ThrowingMoveAllocator(ThrowingMoveAllocator&&) noexcept(false) {} + T* allocate(std::ptrdiff_t n) { return std::allocator().allocate(n); } + void deallocate(T* p, std::ptrdiff_t n) { return std::allocator().deallocate(p, n); } + friend bool operator==(ThrowingMoveAllocator, ThrowingMoveAllocator) = default; +}; + +struct ThrowingMoveComp { + ThrowingMoveComp() = default; + ThrowingMoveComp(const ThrowingMoveComp&) noexcept(true) {} + ThrowingMoveComp(ThrowingMoveComp&&) noexcept(false) {} + bool operator()(const auto&, const auto&) const { return false; } +}; + +struct MoveSensitiveComp { + MoveSensitiveComp() noexcept(false) = default; + MoveSensitiveComp(const MoveSensitiveComp&) noexcept = default; + MoveSensitiveComp(MoveSensitiveComp&& rhs) { rhs.is_moved_from_ = true; } + MoveSensitiveComp& operator=(const MoveSensitiveComp&) noexcept(false) = default; + MoveSensitiveComp& operator=(MoveSensitiveComp&& rhs) { + rhs.is_moved_from_ = true; + return *this; + } + bool operator()(const auto&, const auto&) const { return false; } + bool is_moved_from_ = false; +}; + +int main(int, char**) { + { + using C = std::flat_map; + LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v); + C c; + C d = std::move(c); + } + { + using C = std::flat_map, std::deque>>; + LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v); + C c; + C d = std::move(c); + } +#if _LIBCPP_VERSION + { + // Container fails to be nothrow-move-constructible; this relies on libc++'s support for non-nothrow-copyable allocators + using C = std::flat_map, std::deque>, std::vector>; + static_assert(!std::is_nothrow_move_constructible_v>>); + static_assert(!std::is_nothrow_move_constructible_v); + C c; + C d = std::move(c); + } + { + // Container fails to be nothrow-move-constructible; this relies on libc++'s support for non-nothrow-copyable allocators + using C = std::flat_map, std::vector, std::deque>>; + static_assert(!std::is_nothrow_move_constructible_v>>); + static_assert(!std::is_nothrow_move_constructible_v); + C c; + C d = std::move(c); + } +#endif // _LIBCPP_VERSION + { + // Comparator fails to be nothrow-move-constructible + using C = std::flat_map; + static_assert(!std::is_nothrow_move_constructible_v); + C c; + C d = std::move(c); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/pmr.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/pmr.pass.cpp new file mode 100644 index 00000000000000..154af11bb9b4db --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/pmr.pass.cpp @@ -0,0 +1,361 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// UNSUPPORTED: availability-pmr-missing + +// + +// Test various constructors with pmr + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test_iterators.h" +#include "test_macros.h" +#include "test_allocator.h" +#include "../../../test_compare.h" + +int main(int, char**) { + { + // flat_map(const Allocator& a); + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::polymorphic_allocator pa = &mr; + auto m1 = M(pa); + assert(m1.empty()); + assert(m1.keys().get_allocator() == pa); + assert(m1.values().get_allocator() == pa); + auto m2 = M(&mr); + assert(m2.empty()); + assert(m2.keys().get_allocator() == pa); + assert(m2.values().get_allocator() == pa); + } + { + // flat_map(const key_compare& comp, const Alloc& a); + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + vm.emplace_back(std::greater()); + assert(vm[0] == M{}); + assert(vm[0].key_comp()(2, 1) == true); + assert(vm[0].value_comp()({2, 0}, {1, 0}) == true); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont, + // const Allocator& a); + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + std::pmr::vector ks = {1, 1, 1, 2, 2, 3, 2, 3, 3}; + std::pmr::vector vs = {1, 1, 1, 2, 2, 3, 2, 3, 3}; + assert(ks.get_allocator().resource() != &mr); + assert(vs.get_allocator().resource() != &mr); + vm.emplace_back(ks, vs); + assert(ks.size() == 9); // ks' value is unchanged, since it was an lvalue above + assert(vs.size() == 9); // vs' value is unchanged, since it was an lvalue above + assert((vm[0] == M{{1, 1}, {2, 2}, {3, 3}})); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(const flat_map&, const allocator_type&); + using C = test_less; + using M = std::flat_map, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr1; + std::pmr::monotonic_buffer_resource mr2; + M mo = M({1, 2, 3}, {2, 2, 1}, C(5), &mr1); + M m = {mo, &mr2}; // also test the implicitness of this constructor + + assert(m.key_comp() == C(5)); + assert((m.keys() == std::pmr::vector{1, 2, 3})); + assert((m.values() == std::pmr::vector{2, 2, 1})); + assert(m.keys().get_allocator().resource() == &mr2); + assert(m.values().get_allocator().resource() == &mr2); + + // mo is unchanged + assert(mo.key_comp() == C(5)); + assert((mo.keys() == std::pmr::vector{1, 2, 3})); + assert((mo.values() == std::pmr::vector{2, 2, 1})); + assert(mo.keys().get_allocator().resource() == &mr1); + assert(mo.values().get_allocator().resource() == &mr1); + } + { + // flat_map(const flat_map&, const allocator_type&); + using M = std::flat_map, std::pmr::vector, std::pmr::deque>; + std::pmr::vector vs; + M m = {{1, 2}, {2, 2}, {3, 1}}; + vs.push_back(m); + assert(vs[0] == m); + } + { + // flat_map& operator=(const flat_map& m); + // pmr allocator is not propagated + using M = std::flat_map, std::pmr::deque, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr1; + std::pmr::monotonic_buffer_resource mr2; + M mo = M({{1, 1}, {2, 2}, {3, 3}}, &mr1); + M m = M({{4, 4}, {5, 5}}, &mr2); + m = mo; + assert((m == M{{1, 1}, {2, 2}, {3, 3}})); + assert(m.keys().get_allocator().resource() == &mr2); + assert(m.values().get_allocator().resource() == &mr2); + + // mo is unchanged + assert((mo == M{{1, 1}, {2, 2}, {3, 3}})); + assert(mo.keys().get_allocator().resource() == &mr1); + } + { + // flat_map(const flat_map& m); + using C = test_less; + std::pmr::monotonic_buffer_resource mr; + using M = std::flat_map, std::pmr::vector>; + auto mo = M({{1, 1}, {2, 2}, {3, 3}}, C(5), &mr); + auto m = mo; + + assert(m.key_comp() == C(5)); + assert((m == M{{1, 1}, {2, 2}, {3, 3}})); + auto [ks, vs] = std::move(m).extract(); + assert(ks.get_allocator().resource() == std::pmr::get_default_resource()); + assert(vs.get_allocator().resource() == std::pmr::get_default_resource()); + + // mo is unchanged + assert(mo.key_comp() == C(5)); + assert((mo == M{{1, 1}, {2, 2}, {3, 3}})); + auto [kso, vso] = std::move(mo).extract(); + assert(kso.get_allocator().resource() == &mr); + assert(vso.get_allocator().resource() == &mr); + } + { + // flat_map(initializer_list il, const Alloc& a); + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + std::initializer_list il = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}}; + vm.emplace_back(il); + assert((vm[0] == M{{1, 1}, {3, 3}, {4, 4}, {5, 5}})); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(initializer_list il, const key_compare& comp, const Alloc& a); + using C = test_less; + using M = std::flat_map, std::pmr::deque>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + std::initializer_list il = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}}; + vm.emplace_back(il, C(5)); + assert((vm[0] == M{{1, 1}, {3, 3}, {4, 4}, {5, 5}})); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + assert(vm[0].key_comp() == C(5)); + } + { + // flat_map(InputIterator first, InputIterator last, const Allocator& a); + using P = std::pair; + P ar[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}, {3, 8}, {3, 9}}; + P expected[] = {{1, 1}, {2, 4}, {3, 6}}; + { + // cpp17 iterator + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + vm.emplace_back(cpp17_input_iterator(ar), cpp17_input_iterator(ar + 9)); + assert(std::ranges::equal(vm[0].keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(vm[0], expected)); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + vm.emplace_back(ar, ar); + assert(vm[0].empty()); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + } + { + // flat_map(flat_map&&, const allocator_type&); + std::pair expected[] = {{1, 1}, {2, 2}, {3, 1}}; + using C = test_less; + using M = std::flat_map, std::pmr::deque>; + std::pmr::monotonic_buffer_resource mr1; + std::pmr::monotonic_buffer_resource mr2; + M mo = M({{1, 1}, {3, 1}, {1, 1}, {2, 2}}, C(5), &mr1); + M m = {std::move(mo), &mr2}; // also test the implicitness of this constructor + + assert(m.key_comp() == C(5)); + assert(m.size() == 3); + assert(m.keys().get_allocator().resource() == &mr2); + assert(m.values().get_allocator().resource() == &mr2); + assert(std::equal(m.begin(), m.end(), expected, expected + 3)); + + // The original flat_map is moved-from. + assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp())); + assert(mo.key_comp() == C(5)); + assert(mo.keys().get_allocator().resource() == &mr1); + assert(mo.values().get_allocator().resource() == &mr1); + } + { + // flat_map(flat_map&&, const allocator_type&); + using M = std::flat_map, std::pmr::deque, std::pmr::vector>; + std::pmr::vector vs; + M m = {{1, 1}, {3, 1}, {1, 1}, {2, 2}}; + vs.push_back(std::move(m)); + assert((vs[0].keys() == std::pmr::deque{1, 2, 3})); + assert((vs[0].values() == std::pmr::vector{1, 2, 1})); + } + { + // flat_map& operator=(flat_map&&); + using M = + std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr1; + std::pmr::monotonic_buffer_resource mr2; + M mo = M({{"short", 1}, + {"very long string that definitely won't fit in the SSO buffer and therefore becomes empty on move", 2}}, + &mr1); + M m = M({{"don't care", 3}}, &mr2); + m = std::move(mo); + assert(m.size() == 2); + assert(std::is_sorted(m.begin(), m.end(), m.value_comp())); + assert(m.begin()->first.get_allocator().resource() == &mr2); + + assert(std::is_sorted(mo.begin(), mo.end(), mo.value_comp())); + mo.insert({"foo", 1}); + assert(mo.begin()->first.get_allocator().resource() == &mr1); + } + { + // flat_map(from_range_t, R&&, const Alloc&); + using P = std::pair; + P ar[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}, {3, 8}, {3, 9}}; + P expected[] = {{1, 1}, {2, 4}, {3, 6}}; + { + // input_range + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + using Iter = cpp20_input_iterator; + using Sent = sentinel_wrapper; + using R = std::ranges::subrange; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + vm.emplace_back(std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))); + assert(std::ranges::equal(vm[0].keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(vm[0], expected)); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + using R = std::ranges::subrange; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + vm.emplace_back(std::from_range, R(ar, ar)); + assert(vm[0].empty()); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + } + { + // flat_map(sorted_unique_t, const key_container_type& key_cont, + // const mapped_container_type& mapped_cont, const Alloc& a); + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + std::pmr::vector ks = {1, 2, 4, 10}; + std::pmr::vector vs = {4, 3, 2, 1}; + vm.emplace_back(std::sorted_unique, ks, vs); + assert(!ks.empty()); // it was an lvalue above + assert(!vs.empty()); // it was an lvalue above + assert((vm[0] == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(sorted_unique_t, const key_container_type& key_cont, + // const mapped_container_type& mapped_cont, const Alloc& a); + using M = std::flat_map, std::pmr::vector, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + std::pmr::vector ks({1, 2, 4, 10}, &mr); + std::pmr::vector vs({4, 3, 2, 1}, &mr); + vm.emplace_back(std::sorted_unique, ks, vs); + assert((vm[0] == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(sorted_unique_t, initializer_list il, const Alloc& a); + // cpp_17 + using C = test_less; + using M = std::flat_map, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + using P = std::pair; + P ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + vm.emplace_back( + std::sorted_unique, cpp17_input_iterator(ar), cpp17_input_iterator(ar + 4), C(3)); + assert((vm[0] == M{{1, 1}, {2, 2}, {4, 4}, {5, 5}})); + assert(vm[0].key_comp() == C(3)); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(sorted_unique_t, initializer_list il, const Alloc& a); + using C = test_less; + using M = std::flat_map, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + std::pair ar[1] = {{42, 42}}; + vm.emplace_back(std::sorted_unique, ar, ar, C(4)); + assert(vm[0] == M{}); + assert(vm[0].key_comp() == C(4)); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(InputIterator first, InputIterator last, const Alloc& a); + // cpp_17 + using C = test_less; + using M = std::flat_map, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + using P = std::pair; + P ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + vm.emplace_back( + std::sorted_unique, cpp17_input_iterator(ar), cpp17_input_iterator(ar + 4), C(3)); + assert((vm[0] == M{{1, 1}, {2, 2}, {4, 4}, {5, 5}})); + assert(vm[0].key_comp() == C(3)); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + { + // flat_map(InputIterator first, InputIterator last, const Alloc& a); + using C = test_less; + using M = std::flat_map, std::pmr::vector>; + std::pmr::monotonic_buffer_resource mr; + std::pmr::vector vm(&mr); + std::pair ar[1] = {{42, 42}}; + vm.emplace_back(std::sorted_unique, ar, ar, C(4)); + assert(vm[0] == M{}); + assert(vm[0].key_comp() == C(4)); + assert(vm[0].keys().get_allocator().resource() == &mr); + assert(vm[0].values().get_allocator().resource() == &mr); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/range.pass.cpp new file mode 100644 index 00000000000000..282cc71f31994f --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/range.pass.cpp @@ -0,0 +1,227 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template R> +// flat_map(from_range_t, R&&) +// template R> +// flat_map(from_range_t, R&&, const key_compare&) +// template R, class Alloc> +// flat_map(from_range_t, R&&, const Alloc&); +// template R, class Alloc> +// flat_map(from_range_t, R&&, const key_compare&, const Alloc&); + +#include +#include +#include +#include +#include +#include + +#include "min_allocator.h" +#include "test_allocator.h" +#include "test_iterators.h" +#include "test_macros.h" +#include "../../../test_compare.h" + +// test constraint container-compatible-range + +template +using RangeOf = std::ranges::subrange; +using Map = std::flat_map; + +static_assert(std::is_constructible_v>>); +static_assert(std::is_constructible_v>>); +static_assert(!std::is_constructible_v>); +static_assert(!std::is_constructible_v>); + +static_assert(std::is_constructible_v>, std::less>); +static_assert(std::is_constructible_v>, std::less>); +static_assert(!std::is_constructible_v, std::less>); +static_assert(!std::is_constructible_v, std::less>); + +static_assert(std::is_constructible_v>, std::allocator>); +static_assert(std::is_constructible_v>, std::allocator>); +static_assert(!std::is_constructible_v, std::allocator>); +static_assert(!std::is_constructible_v, std::allocator>); + +static_assert(std::is_constructible_v>, + std::less, + std::allocator>); +static_assert(std::is_constructible_v>, + std::less, + std::allocator>); +static_assert(!std::is_constructible_v, std::less, std::allocator>); +static_assert(!std::is_constructible_v, std::less, std::allocator>); + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + // container-compatible-range + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector>; + using Pair = std::pair; + using PairLike = std::tuple; + using NonPairLike = int; + + static_assert(std::is_constructible_v&>); + static_assert(std::is_constructible_v&>); + static_assert(!std::is_constructible_v&>); + + static_assert(std::is_constructible_v&, const C&>); + static_assert(std::is_constructible_v&, const C&>); + static_assert(!std::is_constructible_v&, const C&>); + + static_assert(std::is_constructible_v&, const A1&>); + static_assert(std::is_constructible_v&, const A1&>); + static_assert(!std::is_constructible_v&, const A1&>); + + static_assert(std::is_constructible_v&, const C&, const A1&>); + static_assert(std::is_constructible_v&, const C&, const A1&>); + static_assert(!std::is_constructible_v&, const C&, const A1&>); + } + + using P = std::pair; + P ar[] = {{1, 1}, {1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {2, 7}, {3, 8}, {3, 9}}; + P expected[] = {{1, 1}, {2, 4}, {3, 6}}; + { + // flat_map(from_range_t, R&&) + // input_range && !common + using M = std::flat_map; + using Iter = cpp20_input_iterator; + using Sent = sentinel_wrapper; + using R = std::ranges::subrange; + auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + + // explicit(false) + M m2 = {std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))}; + assert(m2 == m); + } + { + // flat_map(from_range_t, R&&) + // greater + using M = std::flat_map, std::deque>, std::deque>; + using Iter = cpp20_input_iterator; + using Sent = sentinel_wrapper; + using R = std::ranges::subrange; + auto m = M(std::from_range, R(Iter(ar), Sent(Iter(ar + 9)))); + assert((m.keys() == std::deque>{3, 2, 1})); + LIBCPP_ASSERT((m.values() == std::deque{6, 4, 1})); + } + { + // flat_map(from_range_t, R&&) + // contiguous range + using M = std::flat_map; + using R = std::ranges::subrange; + auto m = M(std::from_range, R(ar, ar + 9)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + } + { + // flat_map(from_range_t, R&&, const key_compare&) + using C = test_less; + using M = std::flat_map, std::deque>; + using R = std::ranges::subrange; + auto m = M(std::from_range, R(ar, ar + 9), C(3)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.key_comp() == C(3)); + + // explicit(false) + M m2 = {std::from_range, R(ar, ar + 9), C(3)}; + assert(m2 == m); + assert(m2.key_comp() == C(3)); + } + { + // flat_map(from_range_t, R&&, const Allocator&) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + using R = std::ranges::subrange; + auto m = M(std::from_range, R(ar, ar + 9), A1(5)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(from_range_t, R&&, const Allocator&) + // explicit(false) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + using R = std::ranges::subrange; + M m = {std::from_range, R(ar, ar + 9), A1(5)}; // implicit ctor + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(from_range_t, R&&, const key_compare&, const Allocator&) + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque>; + using R = std::ranges::subrange; + auto m = M(std::from_range, R(ar, ar + 9), C(3), A1(5)); + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.key_comp() == C(3)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(from_range_t, R&&, const key_compare&, const Allocator&) + // explicit(false) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque, std::vector>; + using R = std::ranges::subrange; + M m = {std::from_range, R(ar, ar + 9), {}, A2(5)}; // implicit ctor + assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>)); + LIBCPP_ASSERT(std::ranges::equal(m, expected)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_container.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_container.pass.cpp new file mode 100644 index 00000000000000..3c8868f2ff4247 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_container.pass.cpp @@ -0,0 +1,165 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map(sorted_unique_t, key_container_type key_cont, mapped_container_type mapped_cont, +// const key_compare& comp = key_compare()); +// +// template +// flat_map(sorted_unique_t, const key_container_type& key_cont, +// const mapped_container_type& mapped_cont, const Alloc& a); +// template +// flat_map(sorted_unique_t, const key_container_type& key_cont, +// const mapped_container_type& mapped_cont, +// const key_compare& comp, const Alloc& a); + +#include +#include +#include +#include + +#include "min_allocator.h" +#include "MoveOnly.h" +#include "test_allocator.h" +#include "test_iterators.h" +#include "test_macros.h" +#include "../../../test_compare.h" + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + // flat_map(sorted_unique_t, key_container_type , mapped_container_type) + using M = std::flat_map; + std::vector ks = {1, 2, 4, 10}; + std::vector vs = {4, 3, 2, 1}; + auto ks2 = ks; + auto vs2 = vs; + + auto m = M(std::sorted_unique, ks, vs); + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + m = M(std::sorted_unique, std::move(ks), std::move(vs)); + assert(ks.empty()); // it was moved-from + assert(vs.empty()); // it was moved-from + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + + // explicit(false) + M m2 = {std::sorted_unique, std::move(ks2), std::move(vs2)}; + assert(m == m2); + } + { + // flat_map(sorted_unique_t, key_container_type , mapped_container_type) + // non-default container, comparator and allocator type + using Ks = std::deque>; + using Vs = std::deque>; + using M = std::flat_map, Ks, Vs>; + Ks ks = {10, 4, 2, 1}; + Vs vs = {1, 2, 3, 4}; + auto m = M(std::sorted_unique, ks, vs); + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + m = M(std::sorted_unique, std::move(ks), std::move(vs)); + assert(ks.empty()); // it was moved-from + assert(vs.empty()); // it was moved-from + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + } + { + // flat_map(sorted_unique_t, key_container_type , mapped_container_type) + // allocator copied into the containers + using A = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + auto ks = std::vector({1, 2, 4, 10}, A(4)); + auto vs = std::deque({4, 3, 2, 1}, A(5)); + auto m = M(std::sorted_unique, std::move(ks), std::move(vs)); + assert(ks.empty()); // it was moved-from + assert(vs.empty()); // it was moved-from + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + assert(m.keys().get_allocator() == A(4)); + assert(m.values().get_allocator() == A(5)); + } + { + // flat_map(sorted_unique_t, key_container_type , mapped_container_type, key_compare) + using C = test_less; + using M = std::flat_map; + std::vector ks = {1, 2, 4, 10}; + std::vector vs = {4, 3, 2, 1}; + + auto m = M(std::sorted_unique, ks, vs, C(4)); + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + assert(m.key_comp() == C(4)); + + // explicit(false) + M m2 = {std::sorted_unique, ks, vs, C(4)}; + assert(m2 == m); + assert(m2.key_comp() == C(4)); + } + { + // flat_map(sorted_unique_t, key_container_type , mapped_container_type, key_compare, const Allocator&) + using C = test_less; + using A = test_allocator; + using M = std::flat_map, std::vector>; + std::vector ks = {1, 2, 4, 10}; + std::vector vs = {4, 3, 2, 1}; + auto m = M(std::sorted_unique, ks, vs, C(4), A(5)); + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + assert(m.key_comp() == C(4)); + assert(m.keys().get_allocator() == A(5)); + assert(m.values().get_allocator() == A(5)); + + // explicit(false) + M m2 = {ks, vs, C(4), A(5)}; + assert(m2 == m); + assert(m2.key_comp() == C(4)); + assert(m2.keys().get_allocator() == A(5)); + assert(m2.values().get_allocator() == A(5)); + } + { + // flat_map(sorted_unique_t, key_container_type , mapped_container_type, const Allocator&) + using A = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + auto ks = std::vector({1, 2, 4, 10}, A(4)); + auto vs = std::deque({4, 3, 2, 1}, A(5)); + auto m = M(std::sorted_unique, ks, vs, A(6)); // replaces the allocators + assert(!ks.empty()); // it was an lvalue above + assert(!vs.empty()); // it was an lvalue above + assert((m == M{{1, 4}, {2, 3}, {4, 2}, {10, 1}})); + assert(m.keys().get_allocator() == A(6)); + assert(m.values().get_allocator() == A(6)); + + // explicit(false) + M m2 = {std::sorted_unique, ks, vs, A(6)}; + assert(m2 == m); + assert(m2.keys().get_allocator() == A(6)); + assert(m2.values().get_allocator() == A(6)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_initializer_list.pass.cpp new file mode 100644 index 00000000000000..26452472ba2011 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_initializer_list.pass.cpp @@ -0,0 +1,179 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// flat_map(sorted_unique_t s, initializer_list il, +// const key_compare& comp = key_compare()) +// template +// flat_map(sorted_unique_t, initializer_list il, const Alloc& a); +// template +// flat_map(sorted_unique_t, initializer_list il, +// const key_compare& comp, const Alloc& a); + +#include +#include +#include +#include + +#include "min_allocator.h" +#include "test_allocator.h" +#include "test_iterators.h" +#include "test_macros.h" +#include "../../../test_compare.h" + +template +std::initializer_list> il = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + +const auto il1 = il; +const auto il2 = il; +const auto il3 = il; + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + using IL = std::initializer_list>; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + // initializer_list needs to match exactly + using M = std::flat_map; + using C = typename M::key_compare; + static_assert(std::is_constructible_v>>); + static_assert(std::is_constructible_v>, C>); + static_assert(std::is_constructible_v>, + C, + std::allocator>); + static_assert(std::is_constructible_v>, + std::allocator>); + static_assert( + !std::is_constructible_v>>); + static_assert( + !std::is_constructible_v>, C>); + static_assert(!std::is_constructible_v>, + C, + std::allocator>); + static_assert(!std::is_constructible_v>, + std::allocator>); + static_assert( + !std::is_constructible_v>>); + static_assert( + !std::is_constructible_v>, C>); + static_assert(!std::is_constructible_v>, + C, + std::allocator>); + static_assert(!std::is_constructible_v>, + std::allocator>); + } + + { + // flat_map(sorted_unique_t, initializer_list); + using M = std::flat_map; + auto m = M(std::sorted_unique, il1); + auto expected = M{{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + assert(m == expected); + + // explicit(false) + M m2 = {std::sorted_unique, il1}; + assert(m2 == m); + } + { + // flat_map(sorted_unique_t, initializer_list, const key_compare&); + using M = std::flat_map>; + auto m = M(std::sorted_unique, il1, std::less()); + assert(m == M({{1, 1}, {2, 2}, {4, 4}, {5, 5}}, std::less<>())); + assert(m.key_comp()(1, 2) == true); + + // explicit(false) + M m2 = {std::sorted_unique, il1, std::less()}; + assert(m2 == m); + } + { + // flat_map(sorted_unique_t, initializer_list, const key_compare&); + // greater + using M = std::flat_map, std::deque>, std::vector>; + std::initializer_list> il4{{5, 5}, {4, 4}, {2, 2}, {1, 1}}; + auto m = M(std::sorted_unique, il4, std::greater()); + assert((m == M{{5, 5}, {4, 4}, {2, 2}, {1, 1}})); + } + { + // flat_map(sorted_unique_t, initializer_list, const Allocator&) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + auto m = M(std::sorted_unique, il2, A1(5)); + auto expected = M{{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + assert(m == expected); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + + // explicit(false) + M m2 = {std::sorted_unique, il2, A1(5)}; + assert(m2 == m); + assert(m2.keys().get_allocator() == A1(5)); + assert(m2.values().get_allocator() == A2(5)); + } + { + // flat_map(sorted_unique_t, initializer_list, const key_compare&, const Allocator&); + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque>; + auto m = M(std::sorted_unique, il2, C(3), A1(5)); + assert((m == M{{1, 1}, {2, 2}, {4, 4}, {5, 5}})); + assert(m.key_comp() == C(3)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(sorted_unique_t, initializer_list, const key_compare&, const Allocator&); + // explicit(false) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque, std::vector>; + M m = {std::sorted_unique, il3, {}, A1(5)}; // implicit ctor + assert((m == M{{1, 1}, {2, 2}, {4, 4}, {5, 5}})); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_iter_iter.pass.cpp new file mode 100644 index 00000000000000..8eb7547e917cca --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_iter_iter.pass.cpp @@ -0,0 +1,171 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// flat_map(sorted_unique_t, InputIterator first, InputIterator last, const key_compare& comp = key_compare()); +// template +// flat_map(InputIterator first, InputIterator last, const Alloc& a); +// template +// flat_map(sorted_unique_t, InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a); + +#include +#include +#include +#include + +#include "min_allocator.h" +#include "test_allocator.h" +#include "test_iterators.h" +#include "test_macros.h" +#include "../../../test_compare.h" + +int main(int, char**) { + { + // The constructors in this subclause shall not participate in overload + // resolution unless uses_allocator_v is true + // and uses_allocator_v is true. + using C = test_less; + using A1 = test_allocator; + using A2 = other_allocator; + using V1 = std::vector; + using V2 = std::vector; + using M1 = std::flat_map; + using M2 = std::flat_map; + using M3 = std::flat_map; + using Iter1 = typename M1::iterator; + using Iter2 = typename M2::iterator; + using Iter3 = typename M3::iterator; + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + + static_assert(std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + static_assert(!std::is_constructible_v); + } + { + // flat_map(sorted_unique_t, InputIterator, InputIterator); + // cpp17_input_iterator + using M = std::flat_map; + using P = std::pair; + P ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + auto m = M(std::sorted_unique, cpp17_input_iterator(ar), cpp17_input_iterator(ar + 4)); + auto expected = M{{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + assert(m == expected); + + // explicit(false) + M m2 = {std::sorted_unique, cpp17_input_iterator(ar), cpp17_input_iterator(ar + 4)}; + assert(m2 == m); + } + { + // flat_map(sorted_unique_t, InputIterator, InputIterator); + // contiguous iterator + using C = test_less; + using M = std::flat_map>, std::vector>>; + std::pair ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + auto m = M(std::sorted_unique, ar, ar + 4); + auto expected = M{{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + assert(m == expected); + } + { + // flat_map(sorted_unique_t, InputIterator, InputIterator, const key_compare&); + // cpp_17_input_iterator + using M = std::flat_map>; + using P = std::pair; + P ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + auto m = M(std::sorted_unique, + cpp17_input_iterator(ar), + cpp17_input_iterator(ar + 4), + std::less()); + assert(m == M({{1, 1}, {2, 2}, {4, 4}, {5, 5}}, std::less<>())); + assert(m.key_comp()(1, 2) == true); + + // explicit(false) + M m2 = {std::sorted_unique, + cpp17_input_iterator(ar), + cpp17_input_iterator(ar + 4), + std::less()}; + assert(m2 == m); + } + { + // flat_map(sorted_unique_t, InputIterator, InputIterator, const key_compare&); + // greater + using M = std::flat_map, std::deque>, std::vector>; + using P = std::pair; + P ar[] = {{5, 5}, {4, 4}, {2, 2}, {1, 1}}; + auto m = M(std::sorted_unique, + cpp17_input_iterator(ar), + cpp17_input_iterator(ar + 4), + std::greater()); + assert((m == M{{5, 5}, {4, 4}, {2, 2}, {1, 1}})); + } + { + // flat_map(sorted_unique_t, InputIterator, InputIterator, const key_compare&); + // contiguous iterator + using C = test_less; + using M = std::flat_map>, std::vector>>; + std::pair ar[1] = {{42, 42}}; + auto m = M(std::sorted_unique, ar, ar, C(5)); + assert(m.empty()); + assert(m.key_comp() == C(5)); + } + { + // flat_map(sorted_unique_t, InputIterator , InputIterator, const Allocator&) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::vector, std::deque>; + using P = std::pair; + P ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + auto m = M(std::sorted_unique, ar, ar + 4, A1(5)); + auto expected = M{{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + assert(m == expected); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + + // explicit(false) + M m2 = {std::sorted_unique, ar, ar + 4, A1(5)}; + assert(m2 == m); + assert(m2.keys().get_allocator() == A1(5)); + assert(m2.values().get_allocator() == A2(5)); + } + { + // flat_map(sorted_unique_t, InputIterator, InputIterator, const key_compare&, const Allocator&); + using C = test_less; + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque>; + using P = std::pair; + P ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + auto m = M(std::sorted_unique, ar, ar + 4, C(3), A1(5)); + assert((m == M{{1, 1}, {2, 2}, {4, 4}, {5, 5}})); + assert(m.key_comp() == C(3)); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + { + // flat_map(sorted_unique_t, InputIterator, InputIterator, const key_compare&, const Allocator&); + // explicit(false) + using A1 = test_allocator; + using A2 = test_allocator; + using M = std::flat_map, std::deque, std::vector>; + using P = std::pair; + P ar[] = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + M m = {std::sorted_unique, ar, ar + 4, {}, A1(5)}; // implicit ctor + assert((m == M{{1, 1}, {2, 2}, {4, 4}, {5, 5}})); + assert(m.keys().get_allocator() == A1(5)); + assert(m.values().get_allocator() == A2(5)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if.pass.cpp new file mode 100644 index 00000000000000..fb0563eec5376b --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if.pass.cpp @@ -0,0 +1,93 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// typename flat_map::size_type +// erase_if(flat_map& c, Predicate pred); + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "test_allocator.h" +#include "min_allocator.h" + +// Verify that `flat_map` (like `map`) does NOT support std::erase. +// +template +concept HasStdErase = requires(S& s, typename S::value_type x) { std::erase(s, x); }; +static_assert(HasStdErase>); +static_assert(!HasStdErase>); + +template +M make(std::initializer_list vals) { + M ret; + for (int v : vals) + ret[static_cast(v)] = static_cast(v + 10); + return ret; +} + +template +void test0( + std::initializer_list vals, Pred p, std::initializer_list expected, std::size_t expected_erased_count) { + M s = make(vals); + ASSERT_SAME_TYPE(typename M::size_type, decltype(std::erase_if(s, p))); + assert(expected_erased_count == std::erase_if(s, p)); + assert(s == make(expected)); +} + +template +void test() { + // Test all the plausible signatures for this predicate. + auto is1 = [](typename S::const_reference v) { return v.first == 1; }; + auto is2 = [](typename S::value_type v) { return v.first == 2; }; + auto is3 = [](const typename S::value_type& v) { return v.first == 3; }; + auto is4 = [](auto v) { return v.first == 4; }; + auto True = [](const auto&) { return true; }; + auto False = [](auto&&) { return false; }; + + test0({}, is1, {}, 0); + + test0({1}, is1, {}, 1); + test0({1}, is2, {1}, 0); + + test0({1, 2}, is1, {2}, 1); + test0({1, 2}, is2, {1}, 1); + test0({1, 2}, is3, {1, 2}, 0); + + test0({1, 2, 3}, is1, {2, 3}, 1); + test0({1, 2, 3}, is2, {1, 3}, 1); + test0({1, 2, 3}, is3, {1, 2}, 1); + test0({1, 2, 3}, is4, {1, 2, 3}, 0); + + test0({1, 2, 3}, True, {}, 3); + test0({1, 2, 3}, False, {1, 2, 3}, 0); +} + +int main(int, char**) { + test>(); + test, + std::vector>, + std::vector>>>(); + test, std::vector>>>(); + test, std::deque>>>(); + test, std::deque>>>(); + test>(); + test>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if_exceptions.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if_exceptions.pass.cpp new file mode 100644 index 00000000000000..48fdec42db3fcb --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if_exceptions.pass.cpp @@ -0,0 +1,155 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// UNSUPPORTED: no-exceptions + +// + +// template +// typename flat_map::size_type +// erase_if(flat_map& c, Predicate pred); +// If any member function in [flat.set.defn] exits via an exception, the invariant is restored. +// (This is not a member function, but let's respect the invariant anyway.) + +#include +#include +#include +#include +#include +#include +#include + +#include "../helpers.h" +#include "test_macros.h" + +struct Counter { + int c1, c2, throws; + void tick() { + c1 -= 1; + if (c1 == 0) { + c1 = c2; + throws += 1; + throw 42; + } + } +}; +Counter g_counter = {0, 0, 0}; + +struct ThrowingAssignment { + ThrowingAssignment(int i) : i_(i) {} + ThrowingAssignment(const ThrowingAssignment&) = default; + ThrowingAssignment& operator=(const ThrowingAssignment& rhs) { + g_counter.tick(); + i_ = rhs.i_; + g_counter.tick(); + return *this; + } + operator int() const { return i_; } + int i_; +}; + +struct ThrowingComparator { + bool operator()(const ThrowingAssignment& a, const ThrowingAssignment& b) const { + g_counter.tick(); + return a.i_ < b.i_; + } +}; + +struct ErasurePredicate { + bool operator()(const auto& x) const { return (3 <= x.first && x.first <= 5); } +}; + +int main(int, char**) { + const std::pair expected[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}; + { + using M = std::flat_map; + for (int first_throw = 1; first_throw < 99; ++first_throw) { + for (int second_throw = 1; second_throw < 99; ++second_throw) { + g_counter = {0, 0, 0}; + M m = M({1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8}); + try { + g_counter = {first_throw, second_throw, 0}; + auto n = std::erase_if(m, ErasurePredicate()); + assert(n == 3); + // If it didn't throw at all, we're done. + g_counter = {0, 0, 0}; + assert((m == M{{1, 1}, {2, 2}, {6, 6}, {7, 7}, {8, 8}})); + first_throw = 99; // "done" + break; + } catch (int ex) { + assert(ex == 42); + check_invariant(m); + LIBCPP_ASSERT(m.empty() || std::equal(m.begin(), m.end(), expected, expected + 8)); + if (g_counter.throws == 1) { + // We reached the first throw but not the second throw. + break; + } + } + } + } + } + { + using M = std::flat_map; + for (int first_throw = 1; first_throw < 99; ++first_throw) { + for (int second_throw = 1; second_throw < 99; ++second_throw) { + g_counter = {0, 0, 0}; + M m = M({1, 2, 3, 4, 5, 6, 7, 8}, {1, 2, 3, 4, 5, 6, 7, 8}); + try { + g_counter = {first_throw, second_throw, 0}; + auto n = std::erase_if(m, ErasurePredicate()); + assert(n == 3); + // If it didn't throw at all, we're done. + g_counter = {0, 0, 0}; + assert((m == M{{1, 1}, {2, 2}, {6, 6}, {7, 7}, {8, 8}})); + first_throw = 99; // "done" + break; + } catch (int ex) { + assert(ex == 42); + check_invariant(m); + LIBCPP_ASSERT(m.empty() || std::equal(m.begin(), m.end(), expected, expected + 8)); + if (g_counter.throws == 1) { + // We reached the first throw but not the second throw. + break; + } + } + } + } + } + { + using M = + std::flat_map, std::deque>; + for (int first_throw = 1; first_throw < 99; ++first_throw) { + for (int second_throw = 1; second_throw < 99; ++second_throw) { + g_counter = {0, 0, 0}; + std::deque container = {5, 6, 7, 8}; + container.insert(container.begin(), {1, 2, 3, 4}); + M m = M(std::move(container), {1, 2, 3, 4, 5, 6, 7, 8}); + try { + g_counter = {first_throw, second_throw, 0}; + auto n = std::erase_if(m, ErasurePredicate()); + assert(n == 3); + // If it didn't throw at all, we're done. + g_counter = {0, 0, 0}; + assert((m == M{{1, 1}, {2, 2}, {6, 6}, {7, 7}, {8, 8}})); + first_throw = 99; // "done" + break; + } catch (int ex) { + assert(ex == 42); + check_invariant(m); + LIBCPP_ASSERT(m.empty() || std::equal(m.begin(), m.end(), expected, expected + 8)); + if (g_counter.throws == 1) { + // We reached the first throw but not the second throw. + break; + } + } + } + } + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator.pass.cpp new file mode 100644 index 00000000000000..b63ce6b19ee165 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator.pass.cpp @@ -0,0 +1,96 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator begin() noexcept; +// const_iterator begin() const noexcept +// iterator end() noexcept; +// const_iterator end() const noexcept; +// +// const_iterator cbegin() const noexcept; +// const_iterator cend() const noexcept; + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + M m = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}}; + const M& cm = m; + ASSERT_SAME_TYPE(decltype(m.begin()), typename M::iterator); + ASSERT_SAME_TYPE(decltype(m.cbegin()), typename M::const_iterator); + ASSERT_SAME_TYPE(decltype(cm.begin()), typename M::const_iterator); + ASSERT_SAME_TYPE(decltype(m.end()), typename M::iterator); + ASSERT_SAME_TYPE(decltype(m.cend()), typename M::const_iterator); + ASSERT_SAME_TYPE(decltype(cm.end()), typename M::const_iterator); + static_assert(noexcept(m.begin())); + static_assert(noexcept(cm.begin())); + static_assert(noexcept(m.cbegin())); + static_assert(noexcept(m.end())); + static_assert(noexcept(cm.end())); + static_assert(noexcept(m.cend())); + assert(m.size() == 4); + assert(std::distance(m.begin(), m.end()) == 4); + assert(std::distance(cm.begin(), cm.end()) == 4); + assert(std::distance(m.cbegin(), m.cend()) == 4); + typename M::iterator i; // default-construct + i = m.begin(); // move-assignment + typename M::const_iterator k = i; // converting constructor + assert(i == k); // comparison + for (int j = 1; j <= 4; ++j, ++i) { // pre-increment + assert(i->first == j); // operator-> + assert(i->second == 'a' + j - 1); + } + assert(i == m.end()); + for (int j = 4; j >= 1; --j) { + --i; // pre-decrement + assert((*i).first == j); + assert((*i).second == 'a' + j - 1); + } + assert(i == m.begin()); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + // N3644 testing + using C = std::flat_map; + C::iterator ii1{}, ii2{}; + C::iterator ii4 = ii1; + C::const_iterator cii{}; + assert(ii1 == ii2); + assert(ii1 == ii4); + assert(!(ii1 != ii2)); + + assert((ii1 == cii)); + assert((cii == ii1)); + assert(!(ii1 != cii)); + assert(!(cii != ii1)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator_comparison.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator_comparison.pass.cpp new file mode 100644 index 00000000000000..1975d0ed86cc8b --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator_comparison.pass.cpp @@ -0,0 +1,155 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// flat_map iterators should be C++20 random access iterators + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using KI = typename KeyContainer::iterator; + using I = M::iterator; + using CI = M::const_iterator; + using RI = M::reverse_iterator; + using CRI = M::const_reverse_iterator; + + static_assert(std::equality_comparable); + static_assert(std::equality_comparable); + static_assert(std::equality_comparable); + static_assert(std::equality_comparable); + + static_assert(std::totally_ordered); + static_assert(std::totally_ordered); + static_assert(std::totally_ordered); + static_assert(std::totally_ordered); + + M m = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}}; + + I i1 = m.begin(); + I i2 = m.begin() + 1; + + assert(i1 == i1); + assert(!(i1 != i1)); + assert(i1 != i2); + assert(!(i1 == i2)); + assert(i1 < i2); + assert(!(i1 < i1)); + assert(i1 <= i1); + assert(i1 <= i2); + assert(!(i2 <= i1)); + assert(i2 > i1); + assert(!(i2 > i2)); + assert(i2 >= i1); + assert(i2 >= i2); + assert(!(i1 >= i2)); + + CI ci1 = m.cbegin(); + CI ci2 = m.cbegin() + 1; + assert(ci1 == ci1); + assert(!(ci1 != ci1)); + assert(ci1 != ci2); + assert(!(ci1 == ci2)); + assert(ci1 < ci2); + assert(!(ci1 < ci1)); + assert(ci1 <= ci1); + assert(ci1 <= ci2); + assert(!(ci2 <= ci1)); + assert(ci2 > ci1); + assert(!(ci2 > ci2)); + assert(ci2 >= ci1); + assert(ci2 >= ci2); + assert(!(ci1 >= ci2)); + + RI ri1 = m.rbegin(); + RI ri2 = m.rbegin() + 1; + assert(ri1 == ri1); + assert(!(ri1 != ri1)); + assert(ri1 != ri2); + assert(!(ri1 == ri2)); + assert(ri1 < ri2); + assert(!(ri1 < ri1)); + assert(ri1 <= ri1); + assert(ri1 <= ri2); + assert(!(ri2 <= ri1)); + assert(ri2 > ri1); + assert(!(ri2 > ri2)); + assert(ri2 >= ri1); + assert(ri2 >= ri2); + assert(!(ri1 >= ri2)); + + CRI cri1 = m.crbegin(); + CRI cri2 = m.crbegin() + 1; + assert(cri1 == cri1); + assert(!(cri1 != cri1)); + assert(cri1 != cri2); + assert(!(cri1 == cri2)); + assert(cri1 < cri2); + assert(!(cri1 < cri1)); + assert(cri1 <= cri1); + assert(cri1 <= cri2); + assert(!(cri2 <= cri1)); + assert(cri2 > cri1); + assert(!(cri2 > cri2)); + assert(cri2 >= cri1); + assert(cri2 >= cri2); + assert(!(cri1 >= cri2)); + + if constexpr (std::three_way_comparable) { + static_assert(std::three_way_comparable); // ...of course the wrapped iterators still support <=>. + static_assert(std::three_way_comparable); + static_assert(std::three_way_comparable); + static_assert(std::three_way_comparable); + static_assert(std::same_as I()), std::strong_ordering>); + static_assert(std::same_as CI()), std::strong_ordering>); + static_assert(std::same_as CI()), std::strong_ordering>); + static_assert(std::same_as RI()), std::strong_ordering>); + static_assert(std::same_as CRI()), std::strong_ordering>); + static_assert(std::same_as CRI()), std::strong_ordering>); + + assert(i1 <=> i1 == std::strong_ordering::equivalent); + assert(i1 <=> i2 == std::strong_ordering::less); + assert(i2 <=> i1 == std::strong_ordering::greater); + + assert(ci1 <=> ci1 == std::strong_ordering::equivalent); + assert(ci1 <=> ci2 == std::strong_ordering::less); + assert(ci2 <=> ci1 == std::strong_ordering::greater); + + assert(ri1 <=> ri1 == std::strong_ordering::equivalent); + assert(ri1 <=> ri2 == std::strong_ordering::less); + assert(ri2 <=> ri1 == std::strong_ordering::greater); + + assert(cri1 <=> cri1 == std::strong_ordering::equivalent); + assert(cri1 <=> cri2 == std::strong_ordering::less); + assert(cri2 <=> cri1 == std::strong_ordering::greater); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator_concept_conformance.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator_concept_conformance.compile.pass.cpp new file mode 100644 index 00000000000000..28814e2e37e3c1 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/iterator_concept_conformance.compile.pass.cpp @@ -0,0 +1,82 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator, const_iterator, reverse_iterator, const_reverse_iterator + +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using C = std::flat_map, KeyContainer, ValueContainer>; + using I = C::iterator; + using CI = C::const_iterator; + using RI = C::reverse_iterator; + using CRI = C::const_reverse_iterator; + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(!std::contiguous_iterator); + static_assert(!std::contiguous_iterator); + static_assert(!std::contiguous_iterator); + static_assert(!std::contiguous_iterator); + static_assert(!std::indirectly_writable>); + static_assert(!std::indirectly_writable>); + static_assert(!std::indirectly_writable>); + static_assert(!std::indirectly_writable>); + static_assert(std::sentinel_for); + static_assert(std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(std::sentinel_for); + static_assert(std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(std::sentinel_for); + static_assert(std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(!std::sentinel_for); + static_assert(std::sentinel_for); + static_assert(std::sentinel_for); + static_assert(std::indirectly_movable_storable*>); + static_assert(std::indirectly_movable_storable*>); + static_assert(std::indirectly_movable_storable*>); + static_assert(std::indirectly_movable_storable*>); + +#ifdef _LIBCPP_VERSION + static_assert(std::is_same_v::iterator_category, std::random_access_iterator_tag>); + static_assert(std::is_same_v::iterator_category, std::random_access_iterator_tag>); + static_assert(std::is_same_v::iterator_category, std::random_access_iterator_tag>); + static_assert(std::is_same_v::iterator_category, std::random_access_iterator_tag>); +#endif +} + +void test() { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/range_concept_conformance.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/range_concept_conformance.compile.pass.cpp new file mode 100644 index 00000000000000..abbad310f49caf --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/range_concept_conformance.compile.pass.cpp @@ -0,0 +1,55 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +#include +#include +#include +#include +#include +#include +#include +#include "MinSequenceContainer.h" +#include "min_allocator.h" + +template +void test() { + { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using C = std::flat_map, KeyContainer, ValueContainer>; + + static_assert(std::same_as, typename C::iterator>); + static_assert(std::ranges::random_access_range); + static_assert(!std::ranges::contiguous_range); + static_assert(std::ranges::common_range); + static_assert(std::ranges::input_range); + static_assert(!std::ranges::view); + static_assert(std::ranges::sized_range); + static_assert(!std::ranges::borrowed_range); + static_assert(std::ranges::viewable_range); + + static_assert(std::same_as, typename C::const_iterator>); + static_assert(std::ranges::random_access_range); + static_assert(!std::ranges::contiguous_range); + static_assert(std::ranges::common_range); + static_assert(std::ranges::input_range); + static_assert(!std::ranges::view); + static_assert(std::ranges::sized_range); + static_assert(!std::ranges::borrowed_range); + static_assert(!std::ranges::viewable_range); + } +} + +void test() { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/reverse_iterator.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/reverse_iterator.pass.cpp new file mode 100644 index 00000000000000..09e18986a7e813 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.iterators/reverse_iterator.pass.cpp @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// reverse_iterator rbegin() noexcept; +// const_reverse_iterator rbegin() const noexcept; +// reverse_iterator rend() noexcept; +// const_reverse_iterator rend() const noexcept; +// +// const_reverse_iterator crbegin() const noexcept; +// const_reverse_iterator crend() const noexcept; + +#include +#include +#include +#include +#include +#include + +#include + +#include "test_macros.h" +#include + +int main(int, char**) { + { + using M = std::flat_map, std::deque, std::deque>; + M m = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}}; + const M& cm = m; + ASSERT_SAME_TYPE(decltype(m.rbegin()), M::reverse_iterator); + ASSERT_SAME_TYPE(decltype(m.crbegin()), M::const_reverse_iterator); + ASSERT_SAME_TYPE(decltype(cm.rbegin()), M::const_reverse_iterator); + ASSERT_SAME_TYPE(decltype(m.rend()), M::reverse_iterator); + ASSERT_SAME_TYPE(decltype(m.crend()), M::const_reverse_iterator); + ASSERT_SAME_TYPE(decltype(cm.rend()), M::const_reverse_iterator); + static_assert(noexcept(m.rbegin())); + static_assert(noexcept(cm.rbegin())); + static_assert(noexcept(m.crbegin())); + static_assert(noexcept(m.rend())); + static_assert(noexcept(cm.rend())); + static_assert(noexcept(m.crend())); + assert(m.size() == 4); + assert(std::distance(m.rbegin(), m.rend()) == 4); + assert(std::distance(cm.rbegin(), cm.rend()) == 4); + assert(std::distance(m.crbegin(), m.crend()) == 4); + assert(std::distance(cm.crbegin(), cm.crend()) == 4); + M::reverse_iterator i; // default-construct + ASSERT_SAME_TYPE(decltype(i->first), const int&); + ASSERT_SAME_TYPE(decltype(i->second), char&); + i = m.rbegin(); // move-assignment + M::const_reverse_iterator k = i; // converting constructor + assert(i == k); // comparison + for (int j = 4; j >= 1; --j, ++i) { // pre-increment + assert(i->first == j); // operator-> + assert(i->second == 'a' + j - 1); + } + assert(i == m.rend()); + for (int j = 1; j <= 4; ++j) { + --i; // pre-decrement + assert((*i).first == j); + assert((*i).second == 'a' + j - 1); + } + assert(i == m.rbegin()); + } + { + // N3644 testing + using C = std::flat_map; + C::reverse_iterator ii1{}, ii2{}; + C::reverse_iterator ii4 = ii1; + C::const_reverse_iterator cii{}; + assert(ii1 == ii2); + assert(ii1 == ii4); + assert(!(ii1 != ii2)); + + assert((ii1 == cii)); + assert((cii == ii1)); + assert(!(ii1 != cii)); + assert(!(cii != ii1)); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/clear.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/clear.pass.cpp new file mode 100644 index 00000000000000..30271eb55660bf --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/clear.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// class flat_map + +// void clear() noexcept; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// test noexcept + +template +concept NoExceptClear = requires(T t) { + { t.clear() } noexcept; +}; + +static_assert(NoExceptClear>); +#ifndef TEST_HAS_NO_EXCEPTIONS +static_assert( + NoExceptClear, ThrowOnMoveContainer, ThrowOnMoveContainer>>); +#endif + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + M m = {{1, 2}, {2, 1}, {3, 3}, {4, 1}, {5, 0}}; + assert(m.size() == 5); + ASSERT_NOEXCEPT(m.clear()); + ASSERT_SAME_TYPE(decltype(m.clear()), void); + m.clear(); + assert(m.size() == 0); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace.pass.cpp new file mode 100644 index 00000000000000..06631ac689f75d --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace.pass.cpp @@ -0,0 +1,103 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// pair emplace(Args&&... args); + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "../../../Emplaceable.h" +#include "DefaultOnly.h" +#include "min_allocator.h" + +// Constraints: is_constructible_v, Args...> is true. +template +concept CanEmplace = requires(M m, Args&&... args) { m.emplace(std::forward(args)...); }; + +using Map = std::flat_map; +static_assert(CanEmplace); +static_assert(CanEmplace); +static_assert(CanEmplace, std::tuple>); +static_assert(!CanEmplace); +static_assert(!CanEmplace); + +template +void test_simple() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = std::pair; + M m; + ASSERT_SAME_TYPE(decltype(m.emplace()), R); + R r = m.emplace(typename M::value_type(2, 3.5)); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 1); + assert(m.begin()->first == 2); + assert(m.begin()->second == 3.5); +} + +template +void test_emplaceable() { + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = std::pair; + + M m; + ASSERT_SAME_TYPE(decltype(m.emplace()), R); + R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(2), std::forward_as_tuple()); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 1); + assert(m.begin()->first == 2); + assert(m.begin()->second == Emplaceable()); + r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5)); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 2); + assert(m.begin()->first == 1); + assert(m.begin()->second == Emplaceable(2, 3.5)); + r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5)); + assert(!r.second); + assert(r.first == m.begin()); + assert(m.size() == 2); + assert(m.begin()->first == 1); + assert(m.begin()->second == Emplaceable(2, 3.5)); +} + +int main(int, char**) { + test_simple, std::vector>(); + test_simple, std::vector>(); + test_simple, MinSequenceContainer>(); + test_simple>, std::vector>>(); + + test_emplaceable, std::vector>(); + test_emplaceable, std::vector>(); + test_emplaceable, MinSequenceContainer>(); + test_emplaceable>, std::vector>>(); + + { + auto emplace_func = [](auto& m, auto key_arg, auto value_arg) { + m.emplace(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); + }; + test_emplace_exception_guarantee(emplace_func); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace_hint.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace_hint.pass.cpp new file mode 100644 index 00000000000000..cfee6cac5806cc --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace_hint.pass.cpp @@ -0,0 +1,102 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// iterator emplace_hint(const_iterator position, Args&&... args); + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "../../../Emplaceable.h" +#include "DefaultOnly.h" +#include "min_allocator.h" +#include "../helpers.h" + +#if defined(_LIBCPP_VERSION) +// spec only specifies `emplace(Args&&...)` is_constructible_v, Args...> is true. +// nothing mentioned for emplace_hint +template +concept CanEmplaceHint = + requires(M m, typename M::const_iterator i, Args&&... args) { m.emplace_hint(i, std::forward(args)...); }; + +using Map = std::flat_map; +static_assert(CanEmplaceHint); +static_assert(CanEmplaceHint); +static_assert(CanEmplaceHint, std::tuple>); +static_assert(!CanEmplaceHint); +static_assert(!CanEmplaceHint); +#endif + +template +void test_simple() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = M::iterator; + M m; + ASSERT_SAME_TYPE(decltype(m.emplace_hint(m.cbegin())), R); + R r = m.emplace_hint(m.end(), typename M::value_type(2, 3.5)); + assert(r == m.begin()); + assert(m.size() == 1); + assert(m.begin()->first == 2); + assert(m.begin()->second == 3.5); +} + +template +void test_emplaceable() { + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = M::iterator; + + M m; + ASSERT_SAME_TYPE(decltype(m.emplace_hint(m.cbegin())), R); + R r = m.emplace_hint(m.end(), std::piecewise_construct, std::forward_as_tuple(2), std::forward_as_tuple()); + assert(r == m.begin()); + assert(m.size() == 1); + assert(m.begin()->first == 2); + assert(m.begin()->second == Emplaceable()); + r = m.emplace_hint(m.end(), std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5)); + assert(r == m.begin()); + assert(m.size() == 2); + assert(m.begin()->first == 1); + assert(m.begin()->second == Emplaceable(2, 3.5)); + r = m.emplace_hint(m.end(), std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5)); + assert(r == m.begin()); + assert(m.size() == 2); + assert(m.begin()->first == 1); + assert(m.begin()->second == Emplaceable(2, 3.5)); +} + +int main(int, char**) { + test_simple, std::vector>(); + test_simple, std::vector>(); + test_simple, MinSequenceContainer>(); + test_simple>, std::vector>>(); + + test_emplaceable, std::vector>(); + test_emplaceable, std::vector>(); + test_emplaceable, MinSequenceContainer>(); + test_emplaceable>, std::vector>>(); + + { + auto emplace_func = [](auto& m, auto key_arg, auto value_arg) { + m.emplace_hint(m.begin(), std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); + }; + test_emplace_exception_guarantee(emplace_func); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_iter.pass.cpp new file mode 100644 index 00000000000000..914e8b676a6568 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_iter.pass.cpp @@ -0,0 +1,151 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator erase(iterator position); +// iterator erase(const_iterator position); + +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using P = std::pair; + using I = M::iterator; + + P ar[] = { + P(1, 1.5), + P(2, 2.5), + P(3, 3.5), + P(4, 4.5), + P(5, 5.5), + P(6, 6.5), + P(7, 7.5), + P(8, 8.5), + }; + M m(ar, ar + sizeof(ar) / sizeof(ar[0])); + assert(m.size() == 8); + std::same_as decltype(auto) i1 = m.erase(std::next(m.cbegin(), 3)); + assert(m.size() == 7); + assert(i1 == std::next(m.begin(), 3)); + assert(m.begin()->first == 1); + assert(m.begin()->second == 1.5); + assert(std::next(m.begin())->first == 2); + assert(std::next(m.begin())->second == 2.5); + assert(std::next(m.begin(), 2)->first == 3); + assert(std::next(m.begin(), 2)->second == 3.5); + assert(std::next(m.begin(), 3)->first == 5); + assert(std::next(m.begin(), 3)->second == 5.5); + assert(std::next(m.begin(), 4)->first == 6); + assert(std::next(m.begin(), 4)->second == 6.5); + assert(std::next(m.begin(), 5)->first == 7); + assert(std::next(m.begin(), 5)->second == 7.5); + assert(std::next(m.begin(), 6)->first == 8); + assert(std::next(m.begin(), 6)->second == 8.5); + + std::same_as decltype(auto) i2 = m.erase(std::next(m.begin(), 0)); + assert(m.size() == 6); + assert(i2 == m.begin()); + assert(m.begin()->first == 2); + assert(m.begin()->second == 2.5); + assert(std::next(m.begin())->first == 3); + assert(std::next(m.begin())->second == 3.5); + assert(std::next(m.begin(), 2)->first == 5); + assert(std::next(m.begin(), 2)->second == 5.5); + assert(std::next(m.begin(), 3)->first == 6); + assert(std::next(m.begin(), 3)->second == 6.5); + assert(std::next(m.begin(), 4)->first == 7); + assert(std::next(m.begin(), 4)->second == 7.5); + assert(std::next(m.begin(), 5)->first == 8); + assert(std::next(m.begin(), 5)->second == 8.5); + + std::same_as decltype(auto) i3 = m.erase(std::next(m.cbegin(), 5)); + assert(m.size() == 5); + assert(i3 == m.end()); + assert(m.begin()->first == 2); + assert(m.begin()->second == 2.5); + assert(std::next(m.begin())->first == 3); + assert(std::next(m.begin())->second == 3.5); + assert(std::next(m.begin(), 2)->first == 5); + assert(std::next(m.begin(), 2)->second == 5.5); + assert(std::next(m.begin(), 3)->first == 6); + assert(std::next(m.begin(), 3)->second == 6.5); + assert(std::next(m.begin(), 4)->first == 7); + assert(std::next(m.begin(), 4)->second == 7.5); + + std::same_as decltype(auto) i4 = m.erase(std::next(m.begin(), 1)); + assert(m.size() == 4); + assert(i4 == std::next(m.begin())); + assert(m.begin()->first == 2); + assert(m.begin()->second == 2.5); + assert(std::next(m.begin())->first == 5); + assert(std::next(m.begin())->second == 5.5); + assert(std::next(m.begin(), 2)->first == 6); + assert(std::next(m.begin(), 2)->second == 6.5); + assert(std::next(m.begin(), 3)->first == 7); + assert(std::next(m.begin(), 3)->second == 7.5); + + std::same_as decltype(auto) i5 = m.erase(std::next(m.cbegin(), 2)); + assert(m.size() == 3); + assert(i5 == std::next(m.begin(), 2)); + assert(m.begin()->first == 2); + assert(m.begin()->second == 2.5); + assert(std::next(m.begin())->first == 5); + assert(std::next(m.begin())->second == 5.5); + assert(std::next(m.begin(), 2)->first == 7); + assert(std::next(m.begin(), 2)->second == 7.5); + + std::same_as decltype(auto) i6 = m.erase(std::next(m.begin(), 2)); + assert(m.size() == 2); + assert(i6 == std::next(m.begin(), 2)); + assert(m.begin()->first == 2); + assert(m.begin()->second == 2.5); + assert(std::next(m.begin())->first == 5); + assert(std::next(m.begin())->second == 5.5); + + std::same_as decltype(auto) i7 = m.erase(std::next(m.cbegin(), 0)); + assert(m.size() == 1); + assert(i7 == std::next(m.begin(), 0)); + assert(m.begin()->first == 5); + assert(m.begin()->second == 5.5); + + std::same_as decltype(auto) i8 = m.erase(m.begin()); + assert(m.size() == 0); + assert(i8 == m.begin()); + assert(i8 == m.end()); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto erase_function = [](auto& m, auto) { m.erase(m.begin() + 2); }; + test_erase_exception_guarantee(erase_function); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_iter_iter.pass.cpp new file mode 100644 index 00000000000000..0bc92082940291 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_iter_iter.pass.cpp @@ -0,0 +1,109 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator erase(const_iterator first, const_iterator last); + +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using P = std::pair; + using I = M::iterator; + + P ar[] = { + P(1, 1.5), + P(2, 2.5), + P(3, 3.5), + P(4, 4.5), + P(5, 5.5), + P(6, 6.5), + P(7, 7.5), + P(8, 8.5), + }; + M m(ar, ar + sizeof(ar) / sizeof(ar[0])); + assert(m.size() == 8); + std::same_as decltype(auto) i1 = m.erase(m.cbegin(), m.cbegin()); + assert(m.size() == 8); + assert(i1 == m.begin()); + assert(m.begin()->first == 1); + assert(m.begin()->second == 1.5); + assert(std::next(m.begin())->first == 2); + assert(std::next(m.begin())->second == 2.5); + assert(std::next(m.begin(), 2)->first == 3); + assert(std::next(m.begin(), 2)->second == 3.5); + assert(std::next(m.begin(), 3)->first == 4); + assert(std::next(m.begin(), 3)->second == 4.5); + assert(std::next(m.begin(), 4)->first == 5); + assert(std::next(m.begin(), 4)->second == 5.5); + assert(std::next(m.begin(), 5)->first == 6); + assert(std::next(m.begin(), 5)->second == 6.5); + assert(std::next(m.begin(), 6)->first == 7); + assert(std::next(m.begin(), 6)->second == 7.5); + assert(std::next(m.begin(), 7)->first == 8); + assert(std::next(m.begin(), 7)->second == 8.5); + + std::same_as decltype(auto) i2 = m.erase(m.cbegin(), std::next(m.cbegin(), 2)); + assert(m.size() == 6); + assert(i2 == m.begin()); + assert(std::next(m.begin(), 0)->first == 3); + assert(std::next(m.begin(), 0)->second == 3.5); + assert(std::next(m.begin(), 1)->first == 4); + assert(std::next(m.begin(), 1)->second == 4.5); + assert(std::next(m.begin(), 2)->first == 5); + assert(std::next(m.begin(), 2)->second == 5.5); + assert(std::next(m.begin(), 3)->first == 6); + assert(std::next(m.begin(), 3)->second == 6.5); + assert(std::next(m.begin(), 4)->first == 7); + assert(std::next(m.begin(), 4)->second == 7.5); + assert(std::next(m.begin(), 5)->first == 8); + assert(std::next(m.begin(), 5)->second == 8.5); + + std::same_as decltype(auto) i3 = m.erase(std::next(m.cbegin(), 2), std::next(m.cbegin(), 6)); + assert(m.size() == 2); + assert(i3 == std::next(m.begin(), 2)); + assert(std::next(m.begin(), 0)->first == 3); + assert(std::next(m.begin(), 0)->second == 3.5); + assert(std::next(m.begin(), 1)->first == 4); + assert(std::next(m.begin(), 1)->second == 4.5); + + std::same_as decltype(auto) i4 = m.erase(m.cbegin(), m.cend()); + assert(m.size() == 0); + assert(i4 == m.begin()); + assert(i4 == m.end()); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto erase_function = [](auto& m, auto) { m.erase(m.begin(), m.begin() + 2); }; + test_erase_exception_guarantee(erase_function); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_key.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_key.pass.cpp new file mode 100644 index 00000000000000..ef57b1cb5512d5 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_key.pass.cpp @@ -0,0 +1,91 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// size_type erase(const key_type& k); + +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +template > +void test() { + using M = std::flat_map; + + auto make = [](std::initializer_list il) { + M m; + for (int i : il) { + m.emplace(i, i); + } + return m; + }; + M m = make({1, 2, 3, 4, 5, 6, 7, 8}); + ASSERT_SAME_TYPE(decltype(m.erase(9)), typename M::size_type); + auto n = m.erase(9); + assert(n == 0); + assert(m == make({1, 2, 3, 4, 5, 6, 7, 8})); + n = m.erase(4); + assert(n == 1); + assert(m == make({1, 2, 3, 5, 6, 7, 8})); + n = m.erase(1); + assert(n == 1); + assert(m == make({2, 3, 5, 6, 7, 8})); + n = m.erase(8); + assert(n == 1); + assert(m == make({2, 3, 5, 6, 7})); + n = m.erase(3); + assert(n == 1); + assert(m == make({2, 5, 6, 7})); + n = m.erase(4); + assert(n == 0); + assert(m == make({2, 5, 6, 7})); + n = m.erase(6); + assert(n == 1); + assert(m == make({2, 5, 7})); + n = m.erase(7); + assert(n == 1); + assert(m == make({2, 5})); + n = m.erase(2); + assert(n == 1); + assert(m == make({5})); + n = m.erase(5); + assert(n == 1); + assert(m.empty()); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector, std::greater<>>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto erase_function = [](auto& m, auto key_arg) { + using Map = std::decay_t; + using Key = typename Map::key_type; + const Key key{key_arg}; + m.erase(key); + }; + test_erase_exception_guarantee(erase_function); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_key_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_key_transparent.pass.cpp new file mode 100644 index 00000000000000..3ba30757bf2c7d --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/erase_key_transparent.pass.cpp @@ -0,0 +1,144 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// size_type erase(K&& k); + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanErase = requires(M m, Transparent k) { m.erase(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanErase); +static_assert(!CanErase); +static_assert(!CanErase); +static_assert(!CanErase); + +template +struct HeterogeneousKey { + explicit HeterogeneousKey(Key key, It it) : key_(key), it_(it) {} + operator It() && { return it_; } + auto operator<=>(Key key) const { return key_ <=> key; } + friend bool operator<(const HeterogeneousKey&, const HeterogeneousKey&) { + assert(false); + return false; + } + Key key_; + It it_; +}; + +template +void test_simple() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + M m = {{1, 1}, {2, 2}, {3, 3}, {4, 4}}; + ASSERT_SAME_TYPE(decltype(m.erase(9)), typename M::size_type); + auto n = m.erase(3); // erase(K&&) [with K=int] + assert(n == 1); + assert((m == M{{1, 1}, {2, 2}, {4, 4}})); + typename M::key_type lvalue = 2; + n = m.erase(lvalue); // erase(K&&) [with K=int&] + assert(n == 1); + assert((m == M{{1, 1}, {4, 4}})); + const typename M::key_type const_lvalue = 1; + n = m.erase(const_lvalue); // erase(const key_type&) + assert(n == 1); + assert((m == M{{4, 4}})); +} + +template +void test_transparent_comparator() { + using M = std::flat_map; + M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}}; + ASSERT_SAME_TYPE(decltype(m.erase(Transparent{"abc"})), typename M::size_type); + + auto n = m.erase(Transparent{"epsilon"}); + assert(n == 1); + + M expected = {{"alpha", 1}, {"beta", 2}, {"eta", 4}, {"gamma", 5}}; + assert(m == expected); + + auto n2 = m.erase(Transparent{"aaa"}); + assert(n2 == 0); + assert(m == expected); +} + +int main(int, char**) { + test_simple, std::vector>(); + test_simple, std::vector>(); + test_simple, MinSequenceContainer>(); + test_simple>, std::vector>>(); + + test_transparent_comparator, std::vector>(); + test_transparent_comparator, std::vector>(); + test_transparent_comparator, MinSequenceContainer>(); + test_transparent_comparator>, + std::vector>>(); + + { + // P2077's HeterogeneousKey example + using M = std::flat_map>; + M m = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}; + auto h1 = HeterogeneousKey(8, m.begin()); + std::same_as auto n = m.erase(h1); // lvalue is not convertible to It; erase(K&&) is the best match + assert(n == 1); + assert((m == M{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}})); + std::same_as auto it = m.erase(std::move(h1)); // rvalue is convertible to It; erase(K&&) drops out + assert(it == m.begin()); + assert((m == M{{2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}})); + } + { + using M = std::flat_map>; + M m = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}; + auto h1 = HeterogeneousKey(8, m.begin()); + std::same_as auto n = m.erase(h1); // lvalue is not convertible to It; erase(K&&) is the best match + assert(n == 1); + assert((m == M{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}})); + std::same_as auto it = m.erase(std::move(h1)); // rvalue is convertible to It; erase(K&&) drops out + assert(it == m.begin()); + assert((m == M{{2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}})); + } + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto n = m.erase(Transparent{3}); + assert(n == 1); + assert(transparent_used); + } + { + auto erase_transparent = [](auto& m, auto key_arg) { + using Map = std::decay_t; + using Key = typename Map::key_type; + m.erase(Transparent{key_arg}); + }; + test_erase_exception_guarantee(erase_transparent); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/extract.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/extract.pass.cpp new file mode 100644 index 00000000000000..d8e4ce94efb9e9 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/extract.pass.cpp @@ -0,0 +1,91 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// containers extract() &&; + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +concept CanExtract = requires(T&& t) { std::forward(t).extract(); }; + +static_assert(CanExtract&&>); +static_assert(!CanExtract&>); +static_assert(!CanExtract const&>); +static_assert(!CanExtract const&&>); + +template +void test() { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = M({1, 2, 3}, {4, 5, 6}); + + std::same_as auto containers = std::move(m).extract(); + + auto expected_keys = {1, 2, 3}; + auto expected_values = {4, 5, 6}; + assert(std::ranges::equal(containers.keys, expected_keys)); + assert(std::ranges::equal(containers.values, expected_values)); + check_invariant(m); + LIBCPP_ASSERT(m.empty()); + LIBCPP_ASSERT(m.keys().size() == 0); + LIBCPP_ASSERT(m.values().size() == 0); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + { + // extracted object maintains invariant if one of underlying container does not clear after move + using M = std::flat_map, std::vector, CopyOnlyVector>; + M m = M({1, 2, 3}, {1, 2, 3}); + std::same_as auto containers = std::move(m).extract(); + assert(containers.keys.size() == 3); + assert(containers.values.size() == 3); + check_invariant(m); + LIBCPP_ASSERT(m.empty()); + LIBCPP_ASSERT(m.keys().size() == 0); + LIBCPP_ASSERT(m.values().size() == 0); + } + + { +#ifndef TEST_HAS_NO_EXCEPTIONS + using KeyContainer = std::vector; + using ValueContainer = ThrowOnMoveContainer; + using M = std::flat_map; + + M m; + m.emplace(1, 1); + m.emplace(2, 2); + try { + auto c = std::move(m).extract(); + assert(false); + } catch (int) { + check_invariant(m); + // In libc++, we try to erase the key after value emplacement failure. + // and after erasure failure, we clear the flat_map + LIBCPP_ASSERT(m.size() == 0); + } +#endif + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_cv.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_cv.pass.cpp new file mode 100644 index 00000000000000..7e667c4e4877bf --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_cv.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// pair insert(const value_type& v); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "../helpers.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = std::pair; + using VT = typename M::value_type; + M m; + + const VT v1(2, 2.5); + std::same_as decltype(auto) r = m.insert(v1); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 1); + assert(r.first->first == 2); + assert(r.first->second == 2.5); + + const VT v2(1, 1.5); + r = m.insert(v2); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 2); + assert(r.first->first == 1); + assert(r.first->second == 1.5); + + const VT v3(3, 3.5); + r = m.insert(v3); + assert(r.second); + assert(r.first == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3.5); + + const VT v4(3, 4.5); + r = m.insert(v4); + assert(!r.second); + assert(r.first == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3.5); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto insert_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + using value_type = typename FlatMap::value_type; + const value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); + m.insert(p); + }; + test_emplace_exception_guarantee(insert_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_initializer_list.pass.cpp new file mode 100644 index 00000000000000..32be3ab8a95b3d --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_initializer_list.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// void insert(initializer_list il); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using V = std::pair; + + M m = {{1, 1}, {1, 1.5}, {1, 2}, {3, 1}, {3, 1.5}, {3, 2}}; + m.insert({ + {4, 1}, + {4, 1.5}, + {4, 2}, + {1, 1}, + {1, 1.5}, + {1, 2}, + {2, 1}, + {2, 1.5}, + {2, 2}, + }); + assert(m.size() == 4); + assert(std::distance(m.begin(), m.end()) == 4); + assert(*m.begin() == V(1, 1)); + assert(*std::next(m.begin()) == V(2, 1)); + assert(*std::next(m.begin(), 2) == V(3, 1)); + assert(*std::next(m.begin(), 3) == V(4, 1)); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto insert_func = [](auto& m, const auto& newValues) { + using FlatMap = std::decay_t; + using value_type = typename FlatMap::value_type; + std::initializer_list il = {{newValues[0].first, newValues[0].second}}; + m.insert(il); + }; + test_insert_range_exception_guarantee(insert_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_cv.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_cv.pass.cpp new file mode 100644 index 00000000000000..4bbe0628317dcb --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_cv.pass.cpp @@ -0,0 +1,79 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator insert(const_iterator position, const value_type& v); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "../helpers.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = typename M::iterator; + using VT = typename M::value_type; + + M m; + const VT v1(2, 2.5); + std::same_as decltype(auto) r = m.insert(m.end(), v1); + assert(r == m.begin()); + assert(m.size() == 1); + assert(r->first == 2); + assert(r->second == 2.5); + + const VT v2(1, 1.5); + r = m.insert(m.end(), v2); + assert(r == m.begin()); + assert(m.size() == 2); + assert(r->first == 1); + assert(r->second == 1.5); + + const VT v3(3, 3.5); + r = m.insert(m.end(), v3); + assert(r == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3.5); + + const VT v4(3, 4.5); + r = m.insert(m.end(), v4); + assert(r == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3.5); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto insert_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + using value_type = typename FlatMap::value_type; + const value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); + m.insert(m.begin(), p); + }; + test_emplace_exception_guarantee(insert_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_iter.pass.cpp new file mode 100644 index 00000000000000..8455b19475fe43 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_iter.pass.cpp @@ -0,0 +1,89 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// void insert(InputIterator first, InputIterator last); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "test_iterators.h" +#include "min_allocator.h" + +// test constraint InputIterator +template +concept CanInsert = requires(M m, Args&&... args) { m.insert(std::forward(args)...); }; + +using Map = std::flat_map; +using Pair = std::pair; + +static_assert(CanInsert); +static_assert(CanInsert, cpp17_input_iterator>); +static_assert(!CanInsert); +static_assert(!CanInsert, cpp20_input_iterator>); + +template +void test() { + using P = std::pair; + using M = std::flat_map, KeyContainer, ValueContainer>; + + P ar1[] = { + P(2, 1), + P(2, 1.5), + P(2, 2), + P(1, 1), + P(1, 1.5), + P(1, 2), + P(3, 1), + P(3, 1.5), + P(3, 2), + }; + P ar2[] = { + P(4, 1), + P(4, 1.5), + P(4, 2), + P(1, 1), + P(1, 1.5), + P(1, 2), + P(0, 1), + P(0, 1.5), + P(0, 2), + }; + + M m; + m.insert(cpp17_input_iterator(ar1), cpp17_input_iterator(ar1 + sizeof(ar1) / sizeof(ar1[0]))); + assert(m.size() == 3); + M expected{{1, 1}, {2, 1}, {3, 1}}; + assert(m == expected); + + m.insert(cpp17_input_iterator(ar2), cpp17_input_iterator(ar2 + sizeof(ar2) / sizeof(ar2[0]))); + assert(m.size() == 5); + M expected2{{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}}; + assert(m == expected2); +} +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto insert_func = [](auto& m, const auto& newValues) { m.insert(newValues.begin(), newValues.end()); }; + test_insert_range_exception_guarantee(insert_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_rv.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_rv.pass.cpp new file mode 100644 index 00000000000000..034941b55eb80b --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_iter_rv.pass.cpp @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// +// iterator insert(const_iterator position, value_type&&); + +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "MoveOnly.h" +#include "min_allocator.h" +#include "../helpers.h" +#include "test_macros.h" + +template +void do_insert_iter_rv_test() { + using M = Container; + using P = Pair; + using R = typename M::iterator; + M m; + std::same_as decltype(auto) r = m.insert(m.end(), P(2, 2)); + assert(r == m.begin()); + assert(m.size() == 1); + assert(r->first == 2); + assert(r->second == 2); + + r = m.insert(m.end(), P(1, 1)); + assert(r == m.begin()); + assert(m.size() == 2); + assert(r->first == 1); + assert(r->second == 1); + + r = m.insert(m.end(), P(3, 3)); + assert(r == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3); + + r = m.insert(m.end(), P(3, 4)); + assert(r == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r->first == 3); + assert(r->second == 3); +} + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using P = std::pair; + using CP = std::pair; + + do_insert_iter_rv_test(); + do_insert_iter_rv_test(); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, std::deque>(); + test, std::deque>(); + test, MinSequenceContainer>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + test>, std::vector>>(); + + { + auto insert_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + using value_type = typename FlatMap::value_type; + value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); + m.insert(m.begin(), std::move(p)); + }; + test_emplace_exception_guarantee(insert_func); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_or_assign.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_or_assign.pass.cpp new file mode 100644 index 00000000000000..398a7a1a4052e0 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_or_assign.pass.cpp @@ -0,0 +1,326 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "MoveOnly.h" +#include "min_allocator.h" +#include "test_macros.h" +#include "../helpers.h" + +// template +// pair insert_or_assign(const key_type& k, M&& obj); +// template +// pair insert_or_assign(key_type&& k, M&& obj); +// template +// iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj); +// template +// iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj); + +// Constraints: is_assignable_v is true and is_constructible_v is true. +template +concept CanInsertOrAssign = + requires(Map map, K&& k, M&& m) { map.insert_or_assign(std::forward(k), std::forward(m)); }; + +template +concept CanInsertOrAssignIter = requires(Map map, typename Map::const_iterator iter, K&& k, M&& m) { + map.insert_or_assign(iter, std::forward(k), std::forward(m)); +}; + +template +struct ConstructAndAssignFrom { + explicit ConstructAndAssignFrom(From); + ConstructAndAssignFrom& operator=(From); +}; + +template +struct ConstructFrom { + explicit ConstructFrom(From); +}; + +template +struct AssignFrom { + AssignFrom& operator=(From); +}; + +struct V {}; + +static_assert(CanInsertOrAssign>, const int&, V>); +static_assert(!CanInsertOrAssign>, const int&, int>); +static_assert(!CanInsertOrAssign>, const int&, V>); +static_assert(!CanInsertOrAssign>, const int&, V>); + +static_assert(CanInsertOrAssign>, int&&, V>); +static_assert(!CanInsertOrAssign>, int&&, int>); +static_assert(!CanInsertOrAssign>, int&&, V>); +static_assert(!CanInsertOrAssign>, int&&, V>); + +static_assert(CanInsertOrAssignIter>, const int&, V>); +static_assert(!CanInsertOrAssignIter>, const int&, int>); +static_assert(!CanInsertOrAssignIter>, const int&, V>); +static_assert(!CanInsertOrAssignIter>, const int&, V>); + +static_assert(CanInsertOrAssignIter>, int&&, V>); +static_assert(!CanInsertOrAssignIter>, int&&, int>); +static_assert(!CanInsertOrAssignIter>, int&&, V>); +static_assert(!CanInsertOrAssignIter>, int&&, V>); + +template +void test_cv_key() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + { // pair insert_or_assign(const key_type& k, M&& obj); + using R = std::pair; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(i, Moveable(i, (double)i)); + assert(m.size() == 10); + + for (int i = 0; i < 20; i += 2) { + Moveable mv(i + 1, i + 1); + std::same_as decltype(auto) r1 = m.insert_or_assign(i, std::move(mv)); + assert(m.size() == 10); + assert(!r1.second); // was not inserted + assert(mv.moved()); // was moved from + assert(r1.first->first == i); // key + assert(r1.first->second.get() == i + 1); // value + } + + Moveable mv1(5, 5.0); + std::same_as decltype(auto) r2 = m.insert_or_assign(-1, std::move(mv1)); + assert(m.size() == 11); + assert(r2.second); // was inserted + assert(mv1.moved()); // was moved from + assert(r2.first->first == -1); // key + assert(r2.first->second.get() == 5); // value + + Moveable mv2(9, 9.0); + std::same_as decltype(auto) r3 = m.insert_or_assign(3, std::move(mv2)); + assert(m.size() == 12); + assert(r3.second); // was inserted + assert(mv2.moved()); // was moved from + assert(r3.first->first == 3); // key + assert(r3.first->second.get() == 9); // value + + Moveable mv3(-1, 5.0); + std::same_as decltype(auto) r4 = m.insert_or_assign(117, std::move(mv3)); + assert(m.size() == 13); + assert(r4.second); // was inserted + assert(mv3.moved()); // was moved from + assert(r4.first->first == 117); // key + assert(r4.first->second.get() == -1); // value + } + + { // iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj); + M m; + using R = M::iterator; + for (int i = 0; i < 20; i += 2) + m.emplace(i, Moveable(i, (double)i)); + assert(m.size() == 10); + typename M::const_iterator it = m.find(2); + + Moveable mv1(3, 3.0); + std::same_as decltype(auto) r1 = m.insert_or_assign(it, 2, std::move(mv1)); + assert(m.size() == 10); + assert(mv1.moved()); // was moved from + assert(r1->first == 2); // key + assert(r1->second.get() == 3); // value + + Moveable mv2(5, 5.0); + std::same_as decltype(auto) r2 = m.insert_or_assign(it, 3, std::move(mv2)); + assert(m.size() == 11); + assert(mv2.moved()); // was moved from + assert(r2->first == 3); // key + assert(r2->second.get() == 5); // value + + // wrong hint: begin() + Moveable mv3(7, 7.0); + std::same_as decltype(auto) r3 = m.insert_or_assign(m.begin(), 4, std::move(mv3)); + assert(m.size() == 11); + assert(mv3.moved()); // was moved from + assert(r3->first == 4); // key + assert(r3->second.get() == 7); // value + + Moveable mv4(9, 9.0); + std::same_as decltype(auto) r4 = m.insert_or_assign(m.begin(), 5, std::move(mv4)); + assert(m.size() == 12); + assert(mv4.moved()); // was moved from + assert(r4->first == 5); // key + assert(r4->second.get() == 9); // value + + // wrong hint: end() + Moveable mv5(11, 11.0); + std::same_as decltype(auto) r5 = m.insert_or_assign(m.end(), 6, std::move(mv5)); + assert(m.size() == 12); + assert(mv5.moved()); // was moved from + assert(r5->first == 6); // key + assert(r5->second.get() == 11); // value + + Moveable mv6(13, 13.0); + std::same_as decltype(auto) r6 = m.insert_or_assign(m.end(), 7, std::move(mv6)); + assert(m.size() == 13); + assert(mv6.moved()); // was moved from + assert(r6->first == 7); // key + assert(r6->second.get() == 13); // value + + // wrong hint: third element + Moveable mv7(15, 15.0); + std::same_as decltype(auto) r7 = m.insert_or_assign(std::next(m.begin(), 2), 8, std::move(mv7)); + assert(m.size() == 13); + assert(mv7.moved()); // was moved from + assert(r7->first == 8); // key + assert(r7->second.get() == 15); // value + + Moveable mv8(17, 17.0); + std::same_as decltype(auto) r8 = m.insert_or_assign(std::next(m.begin(), 2), 9, std::move(mv8)); + assert(m.size() == 14); + assert(mv8.moved()); // was moved from + assert(r8->first == 9); // key + assert(r8->second.get() == 17); // value + } +} + +template +void test_rv_key() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + { // pair insert_or_assign(key_type&& k, M&& obj); + using R = std::pair; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(Moveable(i, (double)i), Moveable(i + 1, (double)i + 1)); + assert(m.size() == 10); + + Moveable mvkey1(2, 2.0); + Moveable mv1(4, 4.0); + std::same_as decltype(auto) r1 = m.insert_or_assign(std::move(mvkey1), std::move(mv1)); + assert(m.size() == 10); + assert(!r1.second); // was not inserted + assert(!mvkey1.moved()); // was not moved from + assert(mv1.moved()); // was moved from + assert(r1.first->first == mvkey1); // key + assert(r1.first->second.get() == 4); // value + + Moveable mvkey2(3, 3.0); + Moveable mv2(5, 5.0); + std::same_as decltype(auto) r2 = m.try_emplace(std::move(mvkey2), std::move(mv2)); + assert(m.size() == 11); + assert(r2.second); // was inserted + assert(mv2.moved()); // was moved from + assert(mvkey2.moved()); // was moved from + assert(r2.first->first.get() == 3); // key + assert(r2.first->second.get() == 5); // value + } + { // iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj); + using R = M::iterator; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(Moveable(i, (double)i), Moveable(i + 1, (double)i + 1)); + assert(m.size() == 10); + typename M::const_iterator it = std::next(m.cbegin()); + + Moveable mvkey1(2, 2.0); + Moveable mv1(4, 4.0); + std::same_as decltype(auto) r1 = m.insert_or_assign(it, std::move(mvkey1), std::move(mv1)); + assert(m.size() == 10); + assert(mv1.moved()); // was moved from + assert(!mvkey1.moved()); // was not moved from + assert(r1->first == mvkey1); // key + assert(r1->second.get() == 4); // value + + Moveable mvkey2(3, 3.0); + Moveable mv2(5, 5.0); + std::same_as decltype(auto) r2 = m.insert_or_assign(it, std::move(mvkey2), std::move(mv2)); + assert(m.size() == 11); + assert(mv2.moved()); // was moved from + assert(mvkey2.moved()); // was moved from + assert(r2->first.get() == 3); // key + assert(r2->second.get() == 5); // value + + // wrong hint: begin() + Moveable mvkey3(6, 6.0); + Moveable mv3(8, 8.0); + std::same_as decltype(auto) r3 = m.insert_or_assign(m.begin(), std::move(mvkey3), std::move(mv3)); + assert(m.size() == 11); + assert(mv3.moved()); // was moved from + assert(!mvkey3.moved()); // was not moved from + assert(r3->first == mvkey3); // key + assert(r3->second.get() == 8); // value + + Moveable mvkey4(7, 7.0); + Moveable mv4(9, 9.0); + std::same_as decltype(auto) r4 = m.insert_or_assign(m.begin(), std::move(mvkey4), std::move(mv4)); + assert(m.size() == 12); + assert(mv4.moved()); // was moved from + assert(mvkey4.moved()); // was moved from + assert(r4->first.get() == 7); // key + assert(r4->second.get() == 9); // value + + // wrong hint: end() + Moveable mvkey5(8, 8.0); + Moveable mv5(10, 10.0); + std::same_as decltype(auto) r5 = m.insert_or_assign(m.end(), std::move(mvkey5), std::move(mv5)); + assert(m.size() == 12); + assert(mv5.moved()); // was moved from + assert(!mvkey5.moved()); // was not moved from + assert(r5->first == mvkey5); // key + assert(r5->second.get() == 10); // value + + Moveable mvkey6(9, 9.0); + Moveable mv6(11, 11.0); + std::same_as decltype(auto) r6 = m.insert_or_assign(m.end(), std::move(mvkey6), std::move(mv6)); + assert(m.size() == 13); + assert(mv6.moved()); // was moved from + assert(mvkey6.moved()); // was moved from + assert(r6->first.get() == 9); // key + assert(r6->second.get() == 11); // value + + // wrong hint: third element + Moveable mvkey7(10, 10.0); + Moveable mv7(12, 12.0); + std::same_as decltype(auto) r7 = m.insert_or_assign(std::next(m.begin(), 2), std::move(mvkey7), std::move(mv7)); + assert(m.size() == 13); + assert(mv7.moved()); // was moved from + assert(!mvkey7.moved()); // was not moved from + assert(r7->first == mvkey7); // key + assert(r7->second.get() == 12); // value + + Moveable mvkey8(11, 11.0); + Moveable mv8(13, 13.0); + std::same_as decltype(auto) r8 = m.insert_or_assign(std::next(m.begin(), 2), std::move(mvkey8), std::move(mv8)); + assert(m.size() == 14); + assert(mv8.moved()); // was moved from + assert(mvkey8.moved()); // was moved from + assert(r8->first.get() == 11); // key + assert(r8->second.get() == 13); // value + } +} + +int main(int, char**) { + test_cv_key, std::vector>(); + test_cv_key, std::vector>(); + test_cv_key, MinSequenceContainer>(); + test_cv_key>, std::vector>>(); + + test_rv_key, std::vector>(); + test_rv_key, std::vector>(); + test_rv_key, MinSequenceContainer>(); + test_rv_key>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_or_assign_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_or_assign_transparent.pass.cpp new file mode 100644 index 00000000000000..636c4edfe551de --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_or_assign_transparent.pass.cpp @@ -0,0 +1,259 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "MoveOnly.h" +#include "min_allocator.h" +#include "test_macros.h" +#include "../helpers.h" + +// template +// pair insert_or_assign(K&& k, M&& obj); +// template +// iterator insert_or_assign(const_iterator hint, K&& k, M&& obj); + +// Constraints: +// The qualified-id Compare::is_transparent is valid and denotes a type. +// is_constructible_v is true. +// is_assignable_v is true. +// is_constructible_v is true. + +template +concept CanInsertOrAssign = + requires(Map map, K&& k, M&& m) { map.insert_or_assign(std::forward(k), std::forward(m)); }; + +template +concept CanInsertOrAssignIter = requires(Map map, typename Map::const_iterator iter, K&& k, M&& m) { + map.insert_or_assign(iter, std::forward(k), std::forward(m)); +}; + +template +struct ConstructAndAssignFrom { + explicit ConstructAndAssignFrom(From); + ConstructAndAssignFrom& operator=(From); +}; + +template +struct ConstructFrom { + explicit ConstructFrom(From); +}; + +template +struct AssignFrom { + AssignFrom& operator=(From); +}; + +struct V {}; + +static_assert(CanInsertOrAssign, TransparentComparator>, + ConvertibleTransparent, + V>); +static_assert(!CanInsertOrAssign, TransparentComparator>, + NonConvertibleTransparent, + V>); +static_assert(!CanInsertOrAssign, NonTransparentComparator>, + NonConvertibleTransparent, + V>); +static_assert(!CanInsertOrAssign, TransparentComparator>, + ConvertibleTransparent, + int>); +static_assert( + !CanInsertOrAssign, TransparentComparator>, ConvertibleTransparent, V>); +static_assert( + !CanInsertOrAssign, TransparentComparator>, ConvertibleTransparent, V>); + +static_assert(CanInsertOrAssignIter, TransparentComparator>, + ConvertibleTransparent, + V>); +static_assert(!CanInsertOrAssignIter, TransparentComparator>, + NonConvertibleTransparent, + V>); +static_assert(!CanInsertOrAssignIter, NonTransparentComparator>, + NonConvertibleTransparent, + V>); +static_assert(!CanInsertOrAssignIter, TransparentComparator>, + ConvertibleTransparent, + int>); +static_assert(!CanInsertOrAssignIter, TransparentComparator>, + ConvertibleTransparent, + V>); +static_assert( + !CanInsertOrAssignIter, TransparentComparator>, ConvertibleTransparent, V>); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + { + // pair insert_or_assign(const key_type& k, M&& obj); + using R = std::pair; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(i, Moveable(i, (double)i)); + assert(m.size() == 10); + + for (int i = 0; i < 20; i += 2) { + Moveable mv(i + 1, i + 1); + std::same_as decltype(auto) r1 = m.insert_or_assign(ConvertibleTransparent{i}, std::move(mv)); + assert(m.size() == 10); + assert(!r1.second); // was not inserted + assert(mv.moved()); // was moved from + assert(r1.first->first == i); // key + assert(r1.first->second.get() == i + 1); // value + } + + Moveable mv1(5, 5.0); + std::same_as decltype(auto) r2 = m.insert_or_assign(ConvertibleTransparent{-1}, std::move(mv1)); + assert(m.size() == 11); + assert(r2.second); // was inserted + assert(mv1.moved()); // was moved from + assert(r2.first->first == -1); // key + assert(r2.first->second.get() == 5); // value + + Moveable mv2(9, 9.0); + std::same_as decltype(auto) r3 = m.insert_or_assign(ConvertibleTransparent{3}, std::move(mv2)); + assert(m.size() == 12); + assert(r3.second); // was inserted + assert(mv2.moved()); // was moved from + assert(r3.first->first == 3); // key + assert(r3.first->second.get() == 9); // value + + Moveable mv3(-1, 5.0); + std::same_as decltype(auto) r4 = m.insert_or_assign(ConvertibleTransparent{117}, std::move(mv3)); + assert(m.size() == 13); + assert(r4.second); // was inserted + assert(mv3.moved()); // was moved from + assert(r4.first->first == 117); // key + assert(r4.first->second.get() == -1); // value + } + { + // iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj); + using R = M::iterator; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(i, Moveable(i, (double)i)); + assert(m.size() == 10); + typename M::const_iterator it = m.find(2); + + Moveable mv1(3, 3.0); + std::same_as decltype(auto) r1 = m.insert_or_assign(it, ConvertibleTransparent{2}, std::move(mv1)); + assert(m.size() == 10); + assert(mv1.moved()); // was moved from + assert(r1->first == 2); // key + assert(r1->second.get() == 3); // value + + Moveable mv2(5, 5.0); + std::same_as decltype(auto) r2 = m.insert_or_assign(it, ConvertibleTransparent{3}, std::move(mv2)); + assert(m.size() == 11); + assert(mv2.moved()); // was moved from + assert(r2->first == 3); // key + assert(r2->second.get() == 5); // value + + // wrong hint: begin() + Moveable mv3(7, 7.0); + std::same_as decltype(auto) r3 = m.insert_or_assign(m.begin(), ConvertibleTransparent{4}, std::move(mv3)); + assert(m.size() == 11); + assert(mv3.moved()); // was moved from + assert(r3->first == 4); // key + assert(r3->second.get() == 7); // value + + Moveable mv4(9, 9.0); + std::same_as decltype(auto) r4 = m.insert_or_assign(m.begin(), ConvertibleTransparent{5}, std::move(mv4)); + assert(m.size() == 12); + assert(mv4.moved()); // was moved from + assert(r4->first == 5); // key + assert(r4->second.get() == 9); // value + + // wrong hint: end() + Moveable mv5(11, 11.0); + std::same_as decltype(auto) r5 = m.insert_or_assign(m.end(), ConvertibleTransparent{6}, std::move(mv5)); + assert(m.size() == 12); + assert(mv5.moved()); // was moved from + assert(r5->first == 6); // key + assert(r5->second.get() == 11); // value + + Moveable mv6(13, 13.0); + std::same_as decltype(auto) r6 = m.insert_or_assign(m.end(), ConvertibleTransparent{7}, std::move(mv6)); + assert(m.size() == 13); + assert(mv6.moved()); // was moved from + assert(r6->first == 7); // key + assert(r6->second.get() == 13); // value + + // wrong hint: third element + Moveable mv7(15, 15.0); + std::same_as decltype(auto) r7 = + m.insert_or_assign(std::next(m.begin(), 2), ConvertibleTransparent{8}, std::move(mv7)); + assert(m.size() == 13); + assert(mv7.moved()); // was moved from + assert(r7->first == 8); // key + assert(r7->second.get() == 15); // value + + Moveable mv8(17, 17.0); + std::same_as decltype(auto) r8 = + m.insert_or_assign(std::next(m.begin(), 2), ConvertibleTransparent{9}, std::move(mv8)); + assert(m.size() == 14); + assert(mv8.moved()); // was moved from + assert(r8->first == 9); // key + assert(r8->second.get() == 17); // value + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto p = m.insert_or_assign(ConvertibleTransparent{3}, 5); + assert(!p.second); + assert(transparent_used); + } + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto it = m.insert_or_assign(m.begin(), ConvertibleTransparent{3}, 5); + assert(it->second == 5); + assert(transparent_used); + } + + { + auto insert_or_assign = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + m.insert_or_assign(ConvertibleTransparent{key_arg}, value_arg); + }; + test_emplace_exception_guarantee(insert_or_assign); + } + + { + auto insert_or_assign_iter = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + m.insert_or_assign(m.begin(), ConvertibleTransparent{key_arg}, value_arg); + }; + test_emplace_exception_guarantee(insert_or_assign_iter); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range.pass.cpp new file mode 100644 index 00000000000000..a2e64431a3c255 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range.pass.cpp @@ -0,0 +1,109 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template R> +// void insert_range(R&& rg); + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "MoveOnly.h" +#include "test_macros.h" +#include "test_iterators.h" +#include "min_allocator.h" + +// test constraint container-compatible-range +template +concept CanInsertRange = requires(M m, R&& r) { m.insert_range(std::forward(r)); }; + +using Map = std::flat_map; + +static_assert(CanInsertRange*>>); +static_assert(CanInsertRange*>>); +static_assert(!CanInsertRange>); +static_assert(!CanInsertRange>); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + + { + using P = std::pair; + using M = std::flat_map, KeyContainer, ValueContainer>; + using It = forward_iterator; + M m = {{10, 1}, {8, 2}, {5, 3}, {2, 4}, {1, 5}}; + P ar[] = {{3, 1}, {1, 2}, {4, 3}, {1, 4}, {5, 5}, {9, 6}}; + std::ranges::subrange r = {It(ar), It(ar + 6)}; + static_assert(std::ranges::common_range); + m.insert_range(r); + assert((m == M{{1, 5}, {2, 4}, {3, 1}, {4, 3}, {5, 3}, {8, 2}, {9, 6}, {10, 1}})); + } + { + using P = std::pair; + using M = std::flat_map, KeyContainer, ValueContainer>; + using It = cpp20_input_iterator; + M m = {{8, 1}, {5, 2}, {3, 3}, {2, 4}}; + P ar[] = {{3, 1}, {1, 2}, {4, 3}, {1, 4}, {5, 5}, {9, 6}}; + std::ranges::subrange r = {It(ar), sentinel_wrapper(It(ar + 6))}; + static_assert(!std::ranges::common_range); + m.insert_range(r); + assert((m == M{{1, 2}, {2, 4}, {3, 3}, {4, 3}, {5, 2}, {8, 1}, {9, 6}})); + } + { + // The "uniquing" part uses the comparator, not operator==. + struct ModTen { + bool operator()(int a, int b) const { return (a % 10) < (b % 10); } + }; + using P = std::pair; + using M = std::flat_map; + M m = {{21, 0}, {43, 0}, {15, 0}, {37, 0}}; + P ar[] = {{33, 1}, {18, 1}, {55, 1}, {18, 1}, {42, 1}}; + m.insert_range(ar); + assert((m == M{{21, 0}, {42, 1}, {43, 0}, {15, 0}, {37, 0}, {18, 1}})); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + { + // Items are forwarded correctly from the input range (P2767). + std::pair a[] = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}}; + std::flat_map m; + m.insert_range(a | std::views::as_rvalue); + std::pair expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}}; + assert(std::ranges::equal(m, expected)); + } + { + // The element type of the range doesn't need to be std::pair (P2767). + std::pair pa[] = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}}; + std::deque>> a(pa, pa + 5); + std::flat_map m; + m.insert_range(a); + std::pair expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}}; + assert(std::ranges::equal(m, expected)); + } + { + auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); }; + test_insert_range_exception_guarantee(insert_func); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range_stability.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range_stability.pass.cpp new file mode 100644 index 00000000000000..fabcb1d216a78a --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_range_stability.pass.cpp @@ -0,0 +1,63 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template R> +// void insert_range(R&& rg); +// +// libc++ uses stable_sort to ensure that flat_map's behavior matches map's, +// in terms of which duplicate items are kept. +// This tests a conforming extension. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "test_macros.h" + +struct Mod256 { + bool operator()(int x, int y) const { return (x % 256) < (y % 256); } +}; + +int main(int, char**) { + { + std::mt19937 randomness; + std::pair pairs[400]; + for (int i = 0; i < 400; ++i) { + uint16_t r = randomness(); + pairs[i] = {r, r}; + } + + std::map m(pairs, pairs + 200); + std::flat_map fm(std::sorted_unique, m.begin(), m.end()); + assert(std::ranges::equal(fm, m)); + + fm.insert_range(std::views::counted(pairs + 200, 200)); + m.insert(pairs + 200, pairs + 400); + assert(fm.size() == m.size()); + LIBCPP_ASSERT(std::ranges::equal(fm, m)); + } + + { + std::vector> v{{1, 2}, {1, 3}}; + std::flat_map m; + m.insert_range(v); + assert(m.size() == 1); + LIBCPP_ASSERT(m[1] == 2); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_rv.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_rv.pass.cpp new file mode 100644 index 00000000000000..9ea7a6a6366664 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_rv.pass.cpp @@ -0,0 +1,124 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// class flat_map + +// pair insert( value_type&& v); + +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "MoveOnly.h" +#include "min_allocator.h" +#include "test_macros.h" +#include "../helpers.h" + +template +void do_insert_rv_test() { + using M = Container; + using P = Pair; + using R = std::pair; + M m; + std::same_as decltype(auto) r = m.insert(P(2, 2)); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 1); + assert(r.first->first == 2); + assert(r.first->second == 2); + + r = m.insert(P(1, 1)); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 2); + assert(r.first->first == 1); + assert(r.first->second == 1); + + r = m.insert(P(3, 3)); + assert(r.second); + assert(r.first == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3); + + r = m.insert(P(3, 3)); + assert(!r.second); + assert(r.first == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3); +} + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + using P = std::pair; + using CP = std::pair; + + do_insert_rv_test(); + do_insert_rv_test(); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + using M = std::flat_map; + using R = std::pair; + M m; + R r = m.insert({2, MoveOnly(2)}); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 1); + assert(r.first->first == 2); + assert(r.first->second == 2); + + r = m.insert({1, MoveOnly(1)}); + assert(r.second); + assert(r.first == m.begin()); + assert(m.size() == 2); + assert(r.first->first == 1); + assert(r.first->second == 1); + + r = m.insert({3, MoveOnly(3)}); + assert(r.second); + assert(r.first == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3); + + r = m.insert({3, MoveOnly(3)}); + assert(!r.second); + assert(r.first == std::ranges::prev(m.end())); + assert(m.size() == 3); + assert(r.first->first == 3); + assert(r.first->second == 3); + } + { + auto insert_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + using value_type = typename FlatMap::value_type; + value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg)); + m.insert(std::move(p)); + }; + test_emplace_exception_guarantee(insert_func); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_sorted_initializer_list.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_sorted_initializer_list.pass.cpp new file mode 100644 index 00000000000000..08d2caf3498791 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_sorted_initializer_list.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// void insert(initializer_list il); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + using V = std::pair; + M m = {{1, 1}, {1, 1.5}, {1, 2}, {3, 1}, {3, 1.5}, {3, 2}}; + m.insert(std::sorted_unique, + { + {0, 1}, + {1, 2}, + {2, 1}, + {4, 1}, + }); + assert(m.size() == 5); + assert(std::distance(m.begin(), m.end()) == 5); + assert(*m.begin() == V(0, 1)); + assert(*std::next(m.begin()) == V(1, 1)); + assert(*std::next(m.begin(), 2) == V(2, 1)); + assert(*std::next(m.begin(), 3) == V(3, 1)); + assert(*std::next(m.begin(), 4) == V(4, 1)); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto insert_func = [](auto& m, const auto& newValues) { + using FlatMap = std::decay_t; + using value_type = typename FlatMap::value_type; + std::initializer_list il = {{newValues[0].first, newValues[0].second}}; + m.insert(std::sorted_unique, il); + }; + test_insert_range_exception_guarantee(insert_func); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_sorted_iter_iter.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_sorted_iter_iter.pass.cpp new file mode 100644 index 00000000000000..18a3b571a41994 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_sorted_iter_iter.pass.cpp @@ -0,0 +1,86 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// void insert(sorted_unique_t, InputIterator first, InputIterator last); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "test_iterators.h" +#include "min_allocator.h" + +// test constraint InputIterator +template +concept CanInsert = requires(M m, Args&&... args) { m.insert(std::forward(args)...); }; + +using Map = std::flat_map; +using Pair = std::pair; + +static_assert(CanInsert); +static_assert(CanInsert, cpp17_input_iterator>); +static_assert(!CanInsert); +static_assert(!CanInsert, cpp20_input_iterator>); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using P = std::pair; + + P ar1[] = { + P(1, 1), + P(2, 1), + P(3, 1), + }; + + P ar2[] = { + P(0, 1), + P(2, 2), + P(4, 1), + }; + + M m; + m.insert( + std::sorted_unique, cpp17_input_iterator(ar1), cpp17_input_iterator(ar1 + sizeof(ar1) / sizeof(ar1[0]))); + assert(m.size() == 3); + M expected{{1, 1}, {2, 1}, {3, 1}}; + assert(m == expected); + + m.insert( + std::sorted_unique, cpp17_input_iterator(ar2), cpp17_input_iterator(ar2 + sizeof(ar2) / sizeof(ar2[0]))); + assert(m.size() == 5); + M expected2{{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}}; + assert(m == expected2); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + auto insert_func = [](auto& m, const auto& newValues) { + m.insert(std::sorted_unique, newValues.begin(), newValues.end()); + }; + test_insert_range_exception_guarantee(insert_func); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_transparent.pass.cpp new file mode 100644 index 00000000000000..75cabb70630f32 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/insert_transparent.pass.cpp @@ -0,0 +1,167 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template pair insert(P&& x); +// template iterator insert(const_iterator hint, P&& x); + +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "test_iterators.h" +#include "min_allocator.h" + +// Constraints: is_constructible_v, P> is true. +template +concept CanInsert = requires(M m, Args&&... args) { m.insert(std::forward(args)...); }; + +using Map = std::flat_map; +using Iter = Map::const_iterator; + +static_assert(CanInsert&&>); +static_assert(CanInsert&&>); +static_assert(CanInsert&&>); +static_assert(CanInsert&&>); +static_assert(!CanInsert); +static_assert(!CanInsert); + +static int expensive_comparisons = 0; +static int cheap_comparisons = 0; + +struct CompareCounter { + int i_ = 0; + CompareCounter(int i) : i_(i) {} + friend auto operator<=>(const CompareCounter& x, const CompareCounter& y) { + expensive_comparisons += 1; + return x.i_ <=> y.i_; + } + bool operator==(const CompareCounter&) const = default; + friend auto operator<=>(const CompareCounter& x, int y) { + cheap_comparisons += 1; + return x.i_ <=> y; + } +}; + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + const std::pair expected[] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; + { + // insert(P&&) + // Unlike flat_set, here we can't use key_compare to compare value_type versus P, + // so we must eagerly convert to value_type. + M m = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + expensive_comparisons = 0; + cheap_comparisons = 0; + std::same_as> auto p = + m.insert(std::make_pair(3, 3)); // conversion happens first + assert(expensive_comparisons >= 2); + assert(cheap_comparisons == 0); + assert(p == std::make_pair(m.begin() + 2, true)); + assert(std::ranges::equal(m, expected)); + } + { + // insert(const_iterator, P&&) + M m = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + expensive_comparisons = 0; + cheap_comparisons = 0; + std::same_as auto it = m.insert(m.begin(), std::make_pair(3, 3)); + assert(expensive_comparisons >= 2); + assert(cheap_comparisons == 0); + assert(it == m.begin() + 2); + assert(std::ranges::equal(m, expected)); + } + { + // insert(value_type&&) + M m = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + expensive_comparisons = 0; + cheap_comparisons = 0; + std::same_as> auto p = + m.insert(std::make_pair(3, 3)); // conversion happens last + assert(expensive_comparisons >= 2); + assert(cheap_comparisons == 0); + assert(p == std::make_pair(m.begin() + 2, true)); + assert(std::ranges::equal(m, expected)); + } + { + // insert(const_iterator, value_type&&) + M m = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + expensive_comparisons = 0; + cheap_comparisons = 0; + std::same_as auto it = m.insert(m.begin(), std::make_pair(3, 3)); + assert(expensive_comparisons >= 2); + assert(cheap_comparisons == 0); + assert(it == m.begin() + 2); + assert(std::ranges::equal(m, expected)); + } + { + // emplace(Args&&...) + M m = {{1, 1}, {2, 2}, {4, 4}, {5, 5}}; + expensive_comparisons = 0; + cheap_comparisons = 0; + std::same_as> auto p = + m.emplace(std::make_pair(3, 3)); // conversion happens first + assert(expensive_comparisons >= 2); + assert(cheap_comparisons == 0); + assert(p == std::make_pair(m.begin() + 2, true)); + assert(std::ranges::equal(m, expected)); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + // no ambiguity between insert(pos, P&&) and insert(first, last) + using M = std::flat_map; + struct Evil { + operator M::value_type() const; + operator M::const_iterator() const; + }; + std::flat_map m; + ASSERT_SAME_TYPE(decltype(m.insert(Evil())), std::pair); + ASSERT_SAME_TYPE(decltype(m.insert(m.begin(), Evil())), M::iterator); + ASSERT_SAME_TYPE(decltype(m.insert(m.begin(), m.end())), void); + } + { + auto insert_func = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + using tuple_type = std::tuple; + tuple_type t(key_arg, value_arg); + m.insert(t); + }; + test_emplace_exception_guarantee(insert_func); + } + { + auto insert_func_iter = [](auto& m, auto key_arg, auto value_arg) { + using FlatMap = std::decay_t; + using tuple_type = std::tuple; + tuple_type t(key_arg, value_arg); + m.insert(m.begin(), t); + }; + test_emplace_exception_guarantee(insert_func_iter); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/replace.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/replace.pass.cpp new file mode 100644 index 00000000000000..5ca811d7615201 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/replace.pass.cpp @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// void replace(key_container_type&& key_cont, mapped_container_type&& mapped_cont); + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +concept CanReplace = requires(T t, Args&&... args) { t.replace(std::forward(args)...); }; + +using Map = std::flat_map; +static_assert(CanReplace, std::vector>); +static_assert(!CanReplace&, std::vector>); +static_assert(!CanReplace, const std::vector&>); +static_assert(!CanReplace&, const std::vector&>); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + M m = M({1, 2, 3}, {4, 5, 6}); + KeyContainer new_keys = {7, 8}; + ValueContainer new_values = {9, 10}; + auto expected_keys = new_keys; + auto expected_values = new_values; + m.replace(std::move(new_keys), std::move(new_values)); + assert(m.size() == 2); + assert(std::ranges::equal(m.keys(), expected_keys)); + assert(std::ranges::equal(m.values(), expected_values)); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { +#ifndef TEST_HAS_NO_EXCEPTIONS + using KeyContainer = std::vector; + using ValueContainer = ThrowOnMoveContainer; + using M = std::flat_map; + + M m; + m.emplace(1, 1); + m.emplace(2, 2); + try { + KeyContainer new_keys{3, 4}; + ValueContainer new_values{5, 6}; + m.replace(std::move(new_keys), std::move(new_values)); + assert(false); + } catch (int) { + check_invariant(m); + // In libc++, we clear the map + LIBCPP_ASSERT(m.size() == 0); + } +#endif + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_exception.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_exception.pass.cpp new file mode 100644 index 00000000000000..f9708aac62c7ee --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_exception.pass.cpp @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 +// `check_assertion.h` requires Unix headers and regex support. +// REQUIRES: has-unix-headers +// UNSUPPORTED: no-localization +// UNSUPPORTED: no-exceptions + +// + +// void swap(flat_map& y) noexcept; +// friend void swap(flat_map& x, flat_map& y) noexcept + +// Test that std::terminate is called if any exception is thrown during swap + +#include +#include +#include +#include +#include + +#include "test_macros.h" +#include "../helpers.h" +#include "check_assertion.h" + +template +void test_swap_exception_guarantee([[maybe_unused]] F&& swap_function) { + { + // key swap throws + using KeyContainer = ThrowOnMoveContainer; + using ValueContainer = std::vector; + using M = std::flat_map; + + M m1, m2; + m1.emplace(1, 1); + m1.emplace(2, 2); + m2.emplace(3, 3); + m2.emplace(4, 4); + // swap is noexcept + EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); }); + } + + { + // value swap throws + using KeyContainer = std::vector; + using ValueContainer = ThrowOnMoveContainer; + using M = std::flat_map; + + M m1, m2; + m1.emplace(1, 1); + m1.emplace(2, 2); + m2.emplace(3, 3); + m2.emplace(4, 4); + + // swap is noexcept + EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); }); + } +} + +int main(int, char**) { + { + auto swap_func = [](auto& m1, auto& m2) { swap(m1, m2); }; + test_swap_exception_guarantee(swap_func); + } + + { + auto swap_func = [](auto& m1, auto& m2) { m1.swap(m2); }; + test_swap_exception_guarantee(swap_func); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_free.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_free.pass.cpp new file mode 100644 index 00000000000000..98c60c1488cf53 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_free.pass.cpp @@ -0,0 +1,97 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// friend void swap(flat_map& x, flat_map& y) noexcept + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "MoveOnly.h" +#include "min_allocator.h" +#include "test_macros.h" +#include "../helpers.h" + +// test noexcept + +template +concept NoExceptAdlSwap = requires(T t1, T t2) { + { swap(t1, t2) } noexcept; +}; + +static_assert(NoExceptAdlSwap>); + +#ifndef TEST_HAS_NO_EXCEPTIONS +static_assert( + NoExceptAdlSwap, ThrowOnMoveContainer, ThrowOnMoveContainer>>); +#endif + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using V = std::pair; + + { + M m1; + M m2; + M m1_save = m1; + M m2_save = m2; + swap(m1, m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } + { + V ar2[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; + M m1; + M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0])); + M m1_save = m1; + M m2_save = m2; + swap(m1, m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } + { + V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)}; + M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0])); + M m2; + M m1_save = m1; + M m2_save = m2; + swap(m1, m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } + { + V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)}; + V ar2[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; + M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0])); + M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0])); + M m1_save = m1; + M m2_save = m2; + swap(m1, m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_member.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_member.pass.cpp new file mode 100644 index 00000000000000..d2d8f5673edeb4 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/swap_member.pass.cpp @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// void swap(flat_map& y) noexcept; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "MoveOnly.h" +#include "min_allocator.h" +#include "test_macros.h" +#include "../helpers.h" + +// test noexcept + +template +concept NoExceptMemberSwap = requires(T t1, T t2) { + { t1.swap(t2) } noexcept; +}; + +static_assert(NoExceptMemberSwap>); +#ifndef TEST_HAS_NO_EXCEPTIONS +static_assert( + NoExceptMemberSwap, ThrowOnMoveContainer, ThrowOnMoveContainer>>); +#endif + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + using V = std::pair; + { + M m1; + M m2; + M m1_save = m1; + M m2_save = m2; + m1.swap(m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } + { + V ar2[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; + M m1; + M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0])); + M m1_save = m1; + M m2_save = m2; + m1.swap(m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } + { + V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)}; + M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0])); + M m2; + M m1_save = m1; + M m2_save = m2; + m1.swap(m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } + { + V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)}; + V ar2[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)}; + M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0])); + M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0])); + M m1_save = m1; + M m2_save = m2; + m1.swap(m2); + assert(m1 == m2_save); + assert(m2 == m1_save); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/try_emplace.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/try_emplace.pass.cpp new file mode 100644 index 00000000000000..4be2fe1c4333e0 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/try_emplace.pass.cpp @@ -0,0 +1,246 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// pair try_emplace(const key_type& k, Args&&... args); +// template +// pair try_emplace(key_type&& k, Args&&... args); +// template +// iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); +// template +// iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args); + +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "../helpers.h" +#include "min_allocator.h" +#include "../../../Emplaceable.h" + +// Constraints: is_constructible_v is true. +template +concept CanTryEmplace = requires(M m, Args&&... args) { m.try_emplace(std::forward(args)...); }; + +using Map = std::flat_map; +using Iter = typename Map::const_iterator; +static_assert(!CanTryEmplace); + +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(!CanTryEmplace); +static_assert(!CanTryEmplace); + +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(!CanTryEmplace); +static_assert(!CanTryEmplace); + +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(!CanTryEmplace); +static_assert(!CanTryEmplace); + +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(CanTryEmplace); +static_assert(!CanTryEmplace); +static_assert(!CanTryEmplace); + +template +void test_ck() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + { // pair try_emplace(const key_type& k, Args&&... args); + using R = std::pair; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(i, Moveable(i, (double)i)); + + assert(m.size() == 10); + + Moveable mv1(3, 3.0); + for (int i = 0; i < 20; i += 2) { + std::same_as decltype(auto) r = m.try_emplace(i, std::move(mv1)); + assert(m.size() == 10); + assert(!r.second); // was not inserted + assert(!mv1.moved()); // was not moved from + assert(r.first->first == i); // key + } + + std::same_as decltype(auto) r2 = m.try_emplace(-1, std::move(mv1)); + assert(m.size() == 11); + assert(r2.second); // was inserted + assert(mv1.moved()); // was moved from + assert(r2.first->first == -1); // key + assert(r2.first->second.get() == 3); // value + + Moveable mv2(5, 3.0); + std::same_as decltype(auto) r3 = m.try_emplace(5, std::move(mv2)); + assert(m.size() == 12); + assert(r3.second); // was inserted + assert(mv2.moved()); // was moved from + assert(r3.first->first == 5); // key + assert(r3.first->second.get() == 5); // value + + Moveable mv3(-1, 3.0); + std::same_as decltype(auto) r4 = m.try_emplace(117, std::move(mv2)); + assert(m.size() == 13); + assert(r4.second); // was inserted + assert(mv2.moved()); // was moved from + assert(r4.first->first == 117); // key + assert(r4.first->second.get() == -1); // value + } + + { // iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args); + using R = typename M::iterator; + M m; + for (int i = 0; i < 20; i += 2) + m.try_emplace(i, Moveable(i, (double)i)); + assert(m.size() == 10); + typename M::const_iterator it = m.find(2); + + Moveable mv1(3, 3.0); + for (int i = 0; i < 20; i += 2) { + std::same_as decltype(auto) r1 = m.try_emplace(it, i, std::move(mv1)); + assert(m.size() == 10); + assert(!mv1.moved()); // was not moved from + assert(r1->first == i); // key + assert(r1->second.get() == i); // value + } + + std::same_as decltype(auto) r2 = m.try_emplace(it, 3, std::move(mv1)); + assert(m.size() == 11); + assert(mv1.moved()); // was moved from + assert(r2->first == 3); // key + assert(r2->second.get() == 3); // value + } +} + +template +void test_rk() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + { // pair try_emplace(key_type&& k, Args&&... args); + using R = std::pair; + M m; + for (int i = 0; i < 20; i += 2) { + m.emplace(Moveable(i, (double)i), Moveable(i + 1, (double)i + 1)); + } + assert(m.size() == 10); + + Moveable mvkey1(2, 2.0); + Moveable mv1(4, 4.0); + std::same_as decltype(auto) r1 = m.try_emplace(std::move(mvkey1), std::move(mv1)); + assert(m.size() == 10); + assert(!r1.second); // was not inserted + assert(!mv1.moved()); // was not moved from + assert(!mvkey1.moved()); // was not moved from + assert(r1.first->first == mvkey1); // key + + Moveable mvkey2(3, 3.0); + std::same_as decltype(auto) r2 = m.try_emplace(std::move(mvkey2), std::move(mv1)); + assert(m.size() == 11); + assert(r2.second); // was inserted + assert(mv1.moved()); // was moved from + assert(mvkey2.moved()); // was moved from + assert(r2.first->first.get() == 3); // key + assert(r2.first->second.get() == 4); // value + } + + { // iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args); + using R = typename M::iterator; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(Moveable(i, (double)i), Moveable(i + 1, (double)i + 1)); + assert(m.size() == 10); + typename M::const_iterator it = std::next(m.cbegin()); + + Moveable mvkey1(2, 2.0); + Moveable mv1(4, 4.0); + std::same_as decltype(auto) r1 = m.try_emplace(it, std::move(mvkey1), std::move(mv1)); + assert(m.size() == 10); + assert(!mv1.moved()); // was not moved from + assert(!mvkey1.moved()); // was not moved from + assert(r1->first == mvkey1); // key + + Moveable mvkey2(3, 3.0); + std::same_as decltype(auto) r2 = m.try_emplace(it, std::move(mvkey2), std::move(mv1)); + assert(m.size() == 11); + assert(mv1.moved()); // was moved from + assert(mvkey2.moved()); // was moved from + assert(r2->first.get() == 3); // key + assert(r2->second.get() == 4); // value + } +} + +int main(int, char**) { + test_ck, std::vector>(); + test_ck, std::vector>(); + test_ck, MinSequenceContainer>(); + test_ck>, std::vector>>(); + + test_rk, std::vector>(); + test_rk, std::vector>(); + test_rk, MinSequenceContainer>(); + test_rk>, std::vector>>(); + + { + auto try_emplace_ck = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + const Key key{key_arg}; + m.try_emplace(key, value_arg); + }; + test_emplace_exception_guarantee(try_emplace_ck); + } + + { + auto try_emplace_rk = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + m.try_emplace(Key{key_arg}, value_arg); + }; + test_emplace_exception_guarantee(try_emplace_rk); + } + + { + auto try_emplace_iter_ck = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + const Key key{key_arg}; + m.try_emplace(m.begin(), key, value_arg); + }; + test_emplace_exception_guarantee(try_emplace_iter_ck); + } + + { + auto try_emplace_iter_rk = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + m.try_emplace(m.begin(), Key{key_arg}, value_arg); + }; + test_emplace_exception_guarantee(try_emplace_iter_rk); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/try_emplace_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/try_emplace_transparent.pass.cpp new file mode 100644 index 00000000000000..21fda437809674 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/try_emplace_transparent.pass.cpp @@ -0,0 +1,182 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template +// pair try_emplace(K&& k, Args&&... args); +// template +// iterator try_emplace(const_iterator hint, K&& k, Args&&... args); + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "../helpers.h" +#include "min_allocator.h" +#include "../../../Emplaceable.h" + +// Constraints: +// The qualified-id Compare::is_transparent is valid and denotes a type. +// is_constructible_v is true. +// is_constructible_v is true. +// For the first overload, is_convertible_v and is_convertible_v are both false +template +concept CanTryEmplace = requires(M m, Args&&... args) { m.try_emplace(std::forward(args)...); }; + +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; + +using TransparentMapIter = typename TransparentMap::iterator; +using TransparentMapConstIter = typename TransparentMap::const_iterator; + +static_assert(!CanTryEmplace); +static_assert(!CanTryEmplace); + +static_assert(CanTryEmplace>); +static_assert(CanTryEmplace, Emplaceable>); +static_assert(CanTryEmplace, int, double>); +static_assert(!CanTryEmplace, const Emplaceable&>); +static_assert(!CanTryEmplace, int>); +static_assert(!CanTryEmplace, Emplaceable>); +static_assert(!CanTryEmplace, Emplaceable>); +static_assert(!CanTryEmplace, int>); +static_assert(!CanTryEmplace); +static_assert(!CanTryEmplace); + +static_assert(CanTryEmplace>); +static_assert(CanTryEmplace, Emplaceable>); +static_assert(CanTryEmplace, int, double>); +static_assert(!CanTryEmplace, const Emplaceable&>); +static_assert(!CanTryEmplace, int>); +static_assert(!CanTryEmplace, Emplaceable>); +static_assert(!CanTryEmplace, Emplaceable>); +static_assert(!CanTryEmplace, int>); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + { // pair try_emplace(K&& k, Args&&... args); + using R = std::pair; + M m; + for (int i = 0; i < 20; i += 2) + m.emplace(i, Moveable(i, (double)i)); + + assert(m.size() == 10); + + Moveable mv1(3, 3.0); + for (int i = 0; i < 20; i += 2) { + std::same_as decltype(auto) r = m.try_emplace(ConvertibleTransparent{i}, std::move(mv1)); + assert(m.size() == 10); + assert(!r.second); // was not inserted + assert(!mv1.moved()); // was not moved from + assert(r.first->first == i); // key + } + + std::same_as decltype(auto) r2 = m.try_emplace(ConvertibleTransparent{-1}, std::move(mv1)); + assert(m.size() == 11); + assert(r2.second); // was inserted + assert(mv1.moved()); // was moved from + assert(r2.first->first == -1); // key + assert(r2.first->second.get() == 3); // value + + Moveable mv2(5, 3.0); + std::same_as decltype(auto) r3 = m.try_emplace(ConvertibleTransparent{5}, std::move(mv2)); + assert(m.size() == 12); + assert(r3.second); // was inserted + assert(mv2.moved()); // was moved from + assert(r3.first->first == 5); // key + assert(r3.first->second.get() == 5); // value + + Moveable mv3(-1, 3.0); + std::same_as decltype(auto) r4 = m.try_emplace(ConvertibleTransparent{117}, std::move(mv2)); + assert(m.size() == 13); + assert(r4.second); // was inserted + assert(mv2.moved()); // was moved from + assert(r4.first->first == 117); // key + assert(r4.first->second.get() == -1); // value + } + + { // iterator try_emplace(const_iterator hint, K&& k, Args&&... args); + using R = typename M::iterator; + M m; + for (int i = 0; i < 20; i += 2) + m.try_emplace(i, Moveable(i, (double)i)); + assert(m.size() == 10); + typename M::const_iterator it = m.find(2); + + Moveable mv1(3, 3.0); + for (int i = 0; i < 20; i += 2) { + std::same_as decltype(auto) r1 = m.try_emplace(it, ConvertibleTransparent{i}, std::move(mv1)); + assert(m.size() == 10); + assert(!mv1.moved()); // was not moved from + assert(r1->first == i); // key + assert(r1->second.get() == i); // value + } + + std::same_as decltype(auto) r2 = m.try_emplace(it, ConvertibleTransparent{3}, std::move(mv1)); + assert(m.size() == 11); + assert(mv1.moved()); // was moved from + assert(r2->first == 3); // key + assert(r2->second.get() == 3); // value + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto p = m.try_emplace(ConvertibleTransparent{3}, 3); + assert(!p.second); + assert(transparent_used); + } + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto p = m.try_emplace(m.begin(), ConvertibleTransparent{3}, 3); + assert(p->second == 3); + assert(transparent_used); + } + { + auto try_emplace = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + m.try_emplace(ConvertibleTransparent{key_arg}, value_arg); + }; + test_emplace_exception_guarantee(try_emplace); + } + + { + auto try_emplace_iter = [](auto& m, auto key_arg, auto value_arg) { + using M = std::decay_t; + using Key = typename M::key_type; + m.try_emplace(m.begin(), ConvertibleTransparent{key_arg}, value_arg); + }; + test_emplace_exception_guarantee(try_emplace_iter); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.observers/comp.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.observers/comp.pass.cpp new file mode 100644 index 00000000000000..d86224952dee45 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.observers/comp.pass.cpp @@ -0,0 +1,96 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// key_compare key_comp() const; +// value_compare value_comp() const; + +#include +#include +#include +#include +#include + +#include "test_macros.h" + +int main(int, char**) { + { + using M = std::flat_map; + using Comp = std::less; // the default + M m = {}; + ASSERT_SAME_TYPE(M::key_compare, Comp); + static_assert(!std::is_same_v); + ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); + ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare); + Comp kc = m.key_comp(); + assert(kc(1, 2)); + assert(!kc(2, 1)); + auto vc = m.value_comp(); + ASSERT_SAME_TYPE(decltype(vc(std::make_pair(1, 2), std::make_pair(1, 2))), bool); + assert(vc({1, '2'}, {2, '1'})); + assert(!vc({2, '1'}, {1, '2'})); + } + { + using Comp = std::function; + using M = std::flat_map; + Comp comp = std::greater(); + M m({}, comp); + ASSERT_SAME_TYPE(M::key_compare, Comp); + ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); + ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare); + Comp kc = m.key_comp(); + assert(!kc(1, 2)); + assert(kc(2, 1)); + auto vc = m.value_comp(); + auto a = std::make_pair(1, 2); + ASSERT_SAME_TYPE(decltype(vc(a, a)), bool); + static_assert(!noexcept(vc(a, a))); + assert(!vc({1, 2}, {2, 1})); + assert(vc({2, 1}, {1, 2})); + } + { + using Comp = std::less<>; + using M = std::flat_map; + M m = {}; + ASSERT_SAME_TYPE(M::key_compare, Comp); + ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp); + ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare); + Comp kc = m.key_comp(); + assert(kc(1, 2)); + assert(!kc(2, 1)); + auto vc = m.value_comp(); + auto a = std::make_pair(1, 2); + ASSERT_SAME_TYPE(decltype(vc(a, a)), bool); + assert(vc({1, 2}, {2, 1})); + assert(!vc({2, 1}, {1, 2})); + } + { + using Comp = std::function&, const std::vector&)>; + using M = std::flat_map, int, Comp>; + Comp comp = [i = 1](const auto& x, const auto& y) { return x[i] < y[i]; }; + M m({}, comp); + auto vc = m.value_comp(); + static_assert(sizeof(vc) >= sizeof(Comp)); + comp = nullptr; + m = M({}, nullptr); + assert(m.key_comp() == nullptr); + // At this point, m.key_comp() is disengaged. + // But the std::function captured by copy inside `vc` remains valid. + auto a = std::make_pair(std::vector{2, 1, 4}, 42); + auto b = std::make_pair(std::vector{1, 2, 3}, 42); + auto c = std::make_pair(std::vector{0, 3, 2}, 42); + assert(vc(a, b)); + assert(vc(b, c)); + assert(!vc(b, a)); + assert(!vc(c, b)); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.observers/keys_values.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.observers/keys_values.pass.cpp new file mode 100644 index 00000000000000..84d8f8344aaa67 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.observers/keys_values.pass.cpp @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// const key_container_type& keys() const noexcept +// const mapped_container_type& values() const noexcept + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "test_allocator.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + const M m = {{4, 'a'}, {2, 'b'}, {3, 'c'}}; + std::same_as decltype(auto) keys = m.keys(); + std::same_as decltype(auto) values = m.values(); + + // noexcept + static_assert(noexcept(m.keys())); + static_assert(noexcept(m.values())); + + auto expected_keys = {2, 3, 4}; + auto expected_values = {'b', 'c', 'a'}; + assert(std::ranges::equal(keys, expected_keys)); + assert(std::ranges::equal(values, expected_values)); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/contains.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/contains.pass.cpp new file mode 100644 index 00000000000000..208d6138fa6836 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/contains.pass.cpp @@ -0,0 +1,70 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// bool contains(const key_type& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 1}, {2, 2}, {4, 4}, {5, 5}, {8, 8}}; + assert(!m.contains(0)); + assert(m.contains(1)); + assert(m.contains(2)); + assert(!m.contains(3)); + assert(m.contains(4)); + assert(m.contains(5)); + assert(!m.contains(6)); + assert(!m.contains(7)); + assert(std::as_const(m).contains(8)); + assert(!std::as_const(m).contains(9)); + m.clear(); + assert(!m.contains(1)); + } + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 0}, {2, 0}, {4, 0}, {5, 0}, {8, 0}}; + assert(!m.contains(0)); + assert(m.contains(1)); + assert(m.contains(2)); + assert(!m.contains(3)); + assert(m.contains(4)); + assert(m.contains(5)); + assert(!m.contains(6)); + assert(!m.contains(7)); + assert(std::as_const(m).contains(8)); + assert(!std::as_const(m).contains(9)); + m.clear(); + assert(!m.contains(1)); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/contains_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/contains_transparent.pass.cpp new file mode 100644 index 00000000000000..0493538ab6dadc --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/contains_transparent.pass.cpp @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template bool contains(const K& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanContains = requires(M m, Transparent k) { m.contains(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanContains); +static_assert(CanContains); +static_assert(!CanContains); +static_assert(!CanContains); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}}; + ASSERT_SAME_TYPE(decltype(m.contains(Transparent{"abc"})), bool); + ASSERT_SAME_TYPE(decltype(std::as_const(m).contains(Transparent{"b"})), bool); + assert(m.contains(Transparent{"alpha"}) == true); + assert(m.contains(Transparent{"beta"}) == true); + assert(m.contains(Transparent{"epsilon"}) == true); + assert(m.contains(Transparent{"eta"}) == true); + assert(m.contains(Transparent{"gamma"}) == true); + assert(m.contains(Transparent{"al"}) == false); + assert(m.contains(Transparent{""}) == false); + assert(m.contains(Transparent{"g"}) == false); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto b = m.contains(Transparent{3}); + assert(b); + assert(transparent_used); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/count.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/count.pass.cpp new file mode 100644 index 00000000000000..db675854d5e98b --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/count.pass.cpp @@ -0,0 +1,69 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// size_type count(const key_type& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 1}, {2, 2}, {4, 4}, {5, 5}, {8, 8}}; + ASSERT_SAME_TYPE(decltype(m.count(0)), size_t); + assert(m.count(0) == 0); + assert(m.count(1) == 1); + assert(m.count(2) == 1); + assert(m.count(3) == 0); + assert(m.count(4) == 1); + assert(m.count(5) == 1); + assert(m.count(6) == 0); + assert(m.count(7) == 0); + assert(std::as_const(m).count(8) == 1); + assert(std::as_const(m).count(9) == 0); + } + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 0}, {2, 0}, {4, 0}, {5, 0}, {8, 0}}; + ASSERT_SAME_TYPE(decltype(m.count(0)), size_t); + assert(m.count(0) == 0); + assert(m.count(1) == 1); + assert(m.count(2) == 1); + assert(m.count(3) == 0); + assert(m.count(4) == 1); + assert(m.count(5) == 1); + assert(m.count(6) == 0); + assert(m.count(7) == 0); + assert(std::as_const(m).count(8) == 1); + assert(std::as_const(m).count(9) == 0); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/count_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/count_transparent.pass.cpp new file mode 100644 index 00000000000000..cd195ff1fa8b43 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/count_transparent.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template size_type count(const K& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanCount = requires(M m, Transparent k) { m.count(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanCount); +static_assert(CanCount); +static_assert(!CanCount); +static_assert(!CanCount); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}}; + ASSERT_SAME_TYPE(decltype(m.count(Transparent{"abc"})), typename M::size_type); + ASSERT_SAME_TYPE(decltype(std::as_const(m).count(Transparent{"b"})), typename M::size_type); + assert(m.count(Transparent{"alpha"}) == 1); + assert(m.count(Transparent{"beta"}) == 1); + assert(m.count(Transparent{"epsilon"}) == 1); + assert(m.count(Transparent{"eta"}) == 1); + assert(m.count(Transparent{"gamma"}) == 1); + assert(m.count(Transparent{"al"}) == 0); + assert(m.count(Transparent{""}) == 0); + assert(m.count(Transparent{"g"}) == 0); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto n = m.count(Transparent{3}); + assert(n == 1); + assert(transparent_used); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/equal_range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/equal_range.pass.cpp new file mode 100644 index 00000000000000..8fa73d2a2eb51d --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/equal_range.pass.cpp @@ -0,0 +1,78 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// pair equal_range(const key_type& k); +// pair equal_range(const key_type& k) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + { + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = std::pair; + using CR = std::pair; + M m = {{1, 'a'}, {2, 'b'}, {4, 'd'}, {5, 'e'}, {8, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.equal_range(0)), R); + ASSERT_SAME_TYPE(decltype(std::as_const(m).equal_range(0)), CR); + auto begin = m.begin(); + assert(m.equal_range(0) == std::pair(begin, begin)); + assert(m.equal_range(1) == std::pair(begin, begin + 1)); + assert(m.equal_range(2) == std::pair(begin + 1, begin + 2)); + assert(m.equal_range(3) == std::pair(begin + 2, begin + 2)); + assert(m.equal_range(4) == std::pair(begin + 2, begin + 3)); + assert(m.equal_range(5) == std::pair(begin + 3, begin + 4)); + assert(m.equal_range(6) == std::pair(begin + 4, begin + 4)); + assert(m.equal_range(7) == std::pair(begin + 4, begin + 4)); + assert(std::as_const(m).equal_range(8) == std::pair(m.cbegin() + 4, m.cbegin() + 5)); + assert(std::as_const(m).equal_range(9) == std::pair(m.cbegin() + 5, m.cbegin() + 5)); + } + + { + using M = std::flat_map, KeyContainer, ValueContainer>; + using R = std::pair; + using CR = std::pair; + M m = {{1, 'a'}, {2, 'b'}, {4, 'd'}, {5, 'e'}, {8, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.equal_range(0)), R); + ASSERT_SAME_TYPE(decltype(std::as_const(m).equal_range(0)), CR); + auto begin = m.begin(); + assert(m.equal_range(0) == std::pair(begin + 5, begin + 5)); + assert(m.equal_range(1) == std::pair(begin + 4, begin + 5)); + assert(m.equal_range(2) == std::pair(begin + 3, begin + 4)); + assert(m.equal_range(3) == std::pair(begin + 3, begin + 3)); + assert(m.equal_range(4) == std::pair(begin + 2, begin + 3)); + assert(m.equal_range(5) == std::pair(begin + 1, begin + 2)); + assert(m.equal_range(6) == std::pair(begin + 1, begin + 1)); + assert(m.equal_range(7) == std::pair(begin + 1, begin + 1)); + assert(std::as_const(m).equal_range(8) == std::pair(m.cbegin(), m.cbegin() + 1)); + assert(std::as_const(m).equal_range(9) == std::pair(m.cbegin(), m.cbegin())); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/equal_range_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/equal_range_transparent.pass.cpp new file mode 100644 index 00000000000000..0198f433bdc4f1 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/equal_range_transparent.pass.cpp @@ -0,0 +1,100 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template pair equal_range(const K& x); +// template pair equal_range(const K& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanEqualRange = requires(M m, Transparent k) { m.equal_range(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanEqualRange); +static_assert(CanEqualRange); +static_assert(!CanEqualRange); +static_assert(!CanEqualRange); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + using R = std::pair; + using CR = std::pair; + M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}}; + const auto& cm = m; + ASSERT_SAME_TYPE(decltype(m.equal_range(Transparent{"abc"})), R); + ASSERT_SAME_TYPE(decltype(std::as_const(m).equal_range(Transparent{"b"})), CR); + + auto test_found = [&](auto&& map, const std::string& expected_key, int expected_value) { + auto [first, last] = map.equal_range(Transparent{expected_key}); + assert(last - first == 1); + auto [key, value] = *first; + assert(key == expected_key); + assert(value == expected_value); + }; + + auto test_not_found = [&](auto&& map, const std::string& expected_key, long expected_offset) { + auto [first, last] = map.equal_range(Transparent{expected_key}); + assert(first == last); + assert(first - m.begin() == expected_offset); + }; + + test_found(m, "alpha", 1); + test_found(m, "beta", 2); + test_found(m, "epsilon", 3); + test_found(m, "eta", 4); + test_found(m, "gamma", 5); + test_found(cm, "alpha", 1); + test_found(cm, "beta", 2); + test_found(cm, "epsilon", 3); + test_found(cm, "eta", 4); + test_found(cm, "gamma", 5); + + test_not_found(m, "charlie", 2); + test_not_found(m, "aaa", 0); + test_not_found(m, "zzz", 5); + test_not_found(cm, "charlie", 2); + test_not_found(cm, "aaa", 0); + test_not_found(cm, "zzz", 5); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto p = m.equal_range(Transparent{3}); + assert(p.first != p.second); + assert(transparent_used); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/find.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/find.pass.cpp new file mode 100644 index 00000000000000..9fae407c7d8f7c --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/find.pass.cpp @@ -0,0 +1,55 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator find(const key_type& k); +// const_iterator find(const key_type& k) const; + +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map, KeyContainer, ValueContainer>; + + M m = {{1, 'a'}, {2, 'b'}, {4, 'd'}, {5, 'e'}, {8, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.find(0)), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).find(0)), typename M::const_iterator); + assert(m.find(0) == m.end()); + assert(m.find(1) == m.begin()); + assert(m.find(2) == m.begin() + 1); + assert(m.find(3) == m.end()); + assert(m.find(4) == m.begin() + 2); + assert(m.find(5) == m.begin() + 3); + assert(m.find(6) == m.end()); + assert(m.find(7) == m.end()); + assert(std::as_const(m).find(8) == m.begin() + 4); + assert(std::as_const(m).find(9) == m.end()); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/find_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/find_transparent.pass.cpp new file mode 100644 index 00000000000000..291577a89fc8f4 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/find_transparent.pass.cpp @@ -0,0 +1,88 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template iterator find(const K& x); +// template const_iterator find(const K& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanFind = requires(M m, Transparent k) { m.find(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanFind); +static_assert(CanFind); +static_assert(!CanFind); +static_assert(!CanFind); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}}; + const auto& cm = m; + ASSERT_SAME_TYPE(decltype(m.find(Transparent{"abc"})), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).find(Transparent{"b"})), typename M::const_iterator); + + auto test_find = [&](auto&& map, const std::string& expected_key, long expected_offset) { + auto iter = map.find(Transparent{expected_key}); + assert(iter - map.begin() == expected_offset); + }; + + test_find(m, "alpha", 0); + test_find(m, "beta", 1); + test_find(m, "epsilon", 2); + test_find(m, "eta", 3); + test_find(m, "gamma", 4); + test_find(m, "charlie", 5); + test_find(m, "aaa", 5); + test_find(m, "zzz", 5); + test_find(cm, "alpha", 0); + test_find(cm, "beta", 1); + test_find(cm, "epsilon", 2); + test_find(cm, "eta", 3); + test_find(cm, "gamma", 4); + test_find(cm, "charlie", 5); + test_find(cm, "aaa", 5); + test_find(cm, "zzz", 5); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto it = m.find(Transparent{3}); + assert(it != m.end()); + assert(transparent_used); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/lower_bound.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/lower_bound.pass.cpp new file mode 100644 index 00000000000000..b5491f3b226746 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/lower_bound.pass.cpp @@ -0,0 +1,71 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator lower_bound(const key_type& k); +// const_iterator lower_bound(const key_type& k) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 'a'}, {2, 'b'}, {4, 'd'}, {5, 'e'}, {8, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.lower_bound(0)), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(0)), typename M::const_iterator); + assert(m.lower_bound(0) == m.begin()); + assert(m.lower_bound(1) == m.begin()); + assert(m.lower_bound(2) == m.begin() + 1); + assert(m.lower_bound(3) == m.begin() + 2); + assert(m.lower_bound(4) == m.begin() + 2); + assert(m.lower_bound(5) == m.begin() + 3); + assert(m.lower_bound(6) == m.begin() + 4); + assert(m.lower_bound(7) == m.begin() + 4); + assert(std::as_const(m).lower_bound(8) == m.begin() + 4); + assert(std::as_const(m).lower_bound(9) == m.end()); + } + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 'a'}, {2, 'b'}, {4, 'd'}, {5, 'e'}, {8, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.lower_bound(0)), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(0)), typename M::const_iterator); + assert(m.lower_bound(0) == m.end()); + assert(m.lower_bound(1) == m.begin() + 4); + assert(m.lower_bound(2) == m.begin() + 3); + assert(m.lower_bound(3) == m.begin() + 3); + assert(m.lower_bound(4) == m.begin() + 2); + assert(m.lower_bound(5) == m.begin() + 1); + assert(m.lower_bound(6) == m.begin() + 1); + assert(m.lower_bound(7) == m.begin() + 1); + assert(std::as_const(m).lower_bound(8) == m.begin()); + assert(std::as_const(m).lower_bound(9) == m.begin()); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/lower_bound_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/lower_bound_transparent.pass.cpp new file mode 100644 index 00000000000000..6a923c197e91ea --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/lower_bound_transparent.pass.cpp @@ -0,0 +1,95 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template iterator lower_bound(const K& x); +// template const_iterator lower_bound(const K& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanLowerBound = requires(M m, Transparent k) { m.lower_bound(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanLowerBound); +static_assert(CanLowerBound); +static_assert(!CanLowerBound); +static_assert(!CanLowerBound); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}}; + const auto& cm = m; + ASSERT_SAME_TYPE(decltype(m.lower_bound(Transparent{"abc"})), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(Transparent{"b"})), typename M::const_iterator); + + auto test_lower_bound = [&](auto&& map, const std::string& expected_key, long expected_offset) { + auto iter = map.lower_bound(Transparent{expected_key}); + assert(iter - map.begin() == expected_offset); + }; + + test_lower_bound(m, "abc", 0); + test_lower_bound(m, "alpha", 0); + test_lower_bound(m, "beta", 1); + test_lower_bound(m, "bets", 2); + test_lower_bound(m, "charlie", 2); + test_lower_bound(m, "echo", 2); + test_lower_bound(m, "epsilon", 2); + test_lower_bound(m, "eta", 3); + test_lower_bound(m, "gamma", 4); + test_lower_bound(m, "golf", 5); + test_lower_bound(m, "zzz", 5); + + test_lower_bound(cm, "abc", 0); + test_lower_bound(cm, "alpha", 0); + test_lower_bound(cm, "beta", 1); + test_lower_bound(cm, "bets", 2); + test_lower_bound(cm, "charlie", 2); + test_lower_bound(cm, "echo", 2); + test_lower_bound(cm, "epsilon", 2); + test_lower_bound(cm, "eta", 3); + test_lower_bound(cm, "gamma", 4); + test_lower_bound(cm, "golf", 5); + test_lower_bound(cm, "zzz", 5); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto it = m.lower_bound(Transparent{3}); + assert(it != m.end()); + assert(transparent_used); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/upper_bound.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/upper_bound.pass.cpp new file mode 100644 index 00000000000000..775e53286d6295 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/upper_bound.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// iterator upper_bound(const key_type& k); +// const_iterator upper_bound(const key_type& k) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 'a'}, {2, 'b'}, {4, 'd'}, {5, 'e'}, {8, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.upper_bound(0)), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).upper_bound(0)), typename M::const_iterator); + assert(m.upper_bound(0) == m.begin()); + assert(m.upper_bound(1) == m.begin() + 1); + assert(m.upper_bound(2) == m.begin() + 2); + assert(m.upper_bound(3) == m.begin() + 2); + assert(m.upper_bound(4) == m.begin() + 3); + assert(m.upper_bound(5) == m.begin() + 4); + assert(m.upper_bound(6) == m.begin() + 4); + assert(std::as_const(m).upper_bound(7) == m.begin() + 4); + assert(std::as_const(m).upper_bound(8) == m.end()); + assert(std::as_const(m).upper_bound(9) == m.end()); + } + + { + using M = std::flat_map, KeyContainer, ValueContainer>; + M m = {{1, 'a'}, {2, 'b'}, {4, 'd'}, {5, 'e'}, {8, 'h'}}; + ASSERT_SAME_TYPE(decltype(m.upper_bound(0)), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).upper_bound(0)), typename M::const_iterator); + assert(m.upper_bound(0) == m.end()); + assert(m.upper_bound(1) == m.end()); + assert(m.upper_bound(2) == m.begin() + 4); + assert(m.upper_bound(3) == m.begin() + 3); + assert(m.upper_bound(4) == m.begin() + 3); + assert(m.upper_bound(5) == m.begin() + 2); + assert(m.upper_bound(6) == m.begin() + 1); + assert(m.upper_bound(7) == m.begin() + 1); + assert(std::as_const(m).upper_bound(8) == m.begin() + 1); + assert(std::as_const(m).upper_bound(9) == m.begin()); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/upper_bound_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/upper_bound_transparent.pass.cpp new file mode 100644 index 00000000000000..4e83f920835dff --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.operations/upper_bound_transparent.pass.cpp @@ -0,0 +1,94 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// template iterator upper_bound(const K& x); +// template const_iterator upper_bound(const K& x) const; + +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "../helpers.h" +#include "test_macros.h" +#include "min_allocator.h" + +// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. +template +concept CanUpperBound = requires(M m, Transparent k) { m.upper_bound(k); }; +using TransparentMap = std::flat_map; +using NonTransparentMap = std::flat_map; +static_assert(CanUpperBound); +static_assert(CanUpperBound); +static_assert(!CanUpperBound); +static_assert(!CanUpperBound); + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + using M = std::flat_map; + + M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}}; + const auto& cm = m; + ASSERT_SAME_TYPE(decltype(m.lower_bound(Transparent{"abc"})), typename M::iterator); + ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(Transparent{"b"})), typename M::const_iterator); + + auto test_upper_bound = [&](auto&& map, const std::string& expected_key, long expected_offset) { + auto iter = map.upper_bound(Transparent{expected_key}); + assert(iter - map.begin() == expected_offset); + }; + + test_upper_bound(m, "abc", 0); + test_upper_bound(m, "alpha", 1); + test_upper_bound(m, "beta", 2); + test_upper_bound(m, "bets", 2); + test_upper_bound(m, "charlie", 2); + test_upper_bound(m, "echo", 2); + test_upper_bound(m, "epsilon", 3); + test_upper_bound(m, "eta", 4); + test_upper_bound(m, "gamma", 5); + test_upper_bound(m, "golf", 5); + test_upper_bound(m, "zzz", 5); + + test_upper_bound(cm, "abc", 0); + test_upper_bound(cm, "alpha", 1); + test_upper_bound(cm, "beta", 2); + test_upper_bound(cm, "bets", 2); + test_upper_bound(cm, "charlie", 2); + test_upper_bound(cm, "echo", 2); + test_upper_bound(cm, "epsilon", 3); + test_upper_bound(cm, "eta", 4); + test_upper_bound(cm, "gamma", 5); + test_upper_bound(cm, "golf", 5); + test_upper_bound(cm, "zzz", 5); +} + +int main(int, char**) { + test, std::vector>(); + test, std::vector>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + { + bool transparent_used = false; + TransparentComparator c(transparent_used); + std::flat_map m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c); + assert(!transparent_used); + auto it = m.upper_bound(Transparent{2}); + assert(it != m.end()); + assert(transparent_used); + } + + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/helpers.h b/libcxx/test/std/containers/container.adaptors/flat.map/helpers.h new file mode 100644 index 00000000000000..8dbb85a6c0acf1 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/helpers.h @@ -0,0 +1,394 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_FLAT_MAP_HELPERS_H +#define SUPPORT_FLAT_MAP_HELPERS_H + +#include +#include +#include +#include +#include + +#include "test_allocator.h" +#include "test_macros.h" + +template +void check_invariant(const std::flat_map& m) { + assert(m.keys().size() == m.values().size()); + const auto& keys = m.keys(); + assert(std::is_sorted(keys.begin(), keys.end(), m.key_comp())); + auto key_equal = [&](const auto& x, const auto& y) { + const auto& c = m.key_comp(); + return !c(x, y) && !c(y, x); + }; + assert(std::adjacent_find(keys.begin(), keys.end(), key_equal) == keys.end()); +} + +struct StartsWith { + explicit StartsWith(char ch) : lower_(1, ch), upper_(1, ch + 1) {} + StartsWith(const StartsWith&) = delete; + void operator=(const StartsWith&) = delete; + struct Less { + using is_transparent = void; + bool operator()(const std::string& a, const std::string& b) const { return a < b; } + bool operator()(const StartsWith& a, const std::string& b) const { return a.upper_ <= b; } + bool operator()(const std::string& a, const StartsWith& b) const { return a < b.lower_; } + bool operator()(const StartsWith&, const StartsWith&) const { + assert(false); // should not be called + return false; + } + }; + +private: + std::string lower_; + std::string upper_; +}; + +template +struct CopyOnlyVector : std::vector { + using std::vector::vector; + + CopyOnlyVector(const CopyOnlyVector&) = default; + CopyOnlyVector(CopyOnlyVector&& other) : CopyOnlyVector(other) {} + CopyOnlyVector(CopyOnlyVector&& other, std::vector::allocator_type alloc) : CopyOnlyVector(other, alloc) {} + + CopyOnlyVector& operator=(const CopyOnlyVector&) = default; + CopyOnlyVector& operator=(CopyOnlyVector& other) { return this->operator=(other); } +}; + +template +struct Transparent { + T t; + + operator T() const + requires ConvertibleToT + { + return t; + } +}; + +template +using ConvertibleTransparent = Transparent; + +template +using NonConvertibleTransparent = Transparent; + +struct TransparentComparator { + using is_transparent = void; + + bool* transparent_used = nullptr; + TransparentComparator() = default; + TransparentComparator(bool& used) : transparent_used(&used) {} + + template + bool operator()(const T& t, const Transparent& transparent) const { + if (transparent_used != nullptr) { + *transparent_used = true; + } + return t < transparent.t; + } + + template + bool operator()(const Transparent& transparent, const T& t) const { + if (transparent_used != nullptr) { + *transparent_used = true; + } + return transparent.t < t; + } + + template + bool operator()(const T& t1, const T& t2) const { + return t1 < t2; + } +}; + +struct NonTransparentComparator { + template + bool operator()(const T&, const Transparent&) const; + + template + bool operator()(const Transparent&, const T&) const; + + template + bool operator()(const T&, const T&) const; +}; + +struct NoDefaultCtr { + NoDefaultCtr() = delete; +}; + +#ifndef TEST_HAS_NO_EXCEPTIONS +template +struct EmplaceUnsafeContainer : std::vector { + using std::vector::vector; + + template + auto emplace(Args&&... args) -> decltype(std::declval>().emplace(std::forward(args)...)) { + if (this->size() > 1) { + auto it1 = this->begin(); + auto it2 = it1 + 1; + // messing up the container + std::iter_swap(it1, it2); + } + + throw 42; + } + + template + auto insert(Args&&... args) -> decltype(std::declval>().insert(std::forward(args)...)) { + if (this->size() > 1) { + auto it1 = this->begin(); + auto it2 = it1 + 1; + // messing up the container + std::iter_swap(it1, it2); + } + + throw 42; + } +}; + +template +struct ThrowOnEraseContainer : std::vector { + using std::vector::vector; + + template + auto erase(Args&&... args) -> decltype(std::declval>().erase(std::forward(args)...)) { + throw 42; + } +}; + +template +struct ThrowOnMoveContainer : std::vector { + using std::vector::vector; + + ThrowOnMoveContainer(ThrowOnMoveContainer&&) { throw 42; } + + ThrowOnMoveContainer& operator=(ThrowOnMoveContainer&&) { throw 42; } +}; + +#endif + +template +void test_emplace_exception_guarantee([[maybe_unused]] F&& emplace_function) { +#ifndef TEST_HAS_NO_EXCEPTIONS + using C = TransparentComparator; + { + // Throw on emplace the key, and underlying has strong exception guarantee + using KeyContainer = std::vector>; + using M = std::flat_map; + + LIBCPP_STATIC_ASSERT(std::__container_traits::__emplacement_has_strong_exception_safety_guarantee); + + test_allocator_statistics stats; + + KeyContainer a({1, 2, 3, 4}, test_allocator{&stats}); + std::vector b = {5, 6, 7, 8}; + [[maybe_unused]] auto expected_keys = a; + [[maybe_unused]] auto expected_values = b; + M m(std::sorted_unique, std::move(a), std::move(b)); + + stats.throw_after = 1; + try { + emplace_function(m, 0, 0); + assert(false); + } catch (const std::bad_alloc&) { + check_invariant(m); + // In libc++, the flat_map is unchanged + LIBCPP_ASSERT(m.size() == 4); + LIBCPP_ASSERT(m.keys() == expected_keys); + LIBCPP_ASSERT(m.values() == expected_values); + } + } + { + // Throw on emplace the key, and underlying has no strong exception guarantee + using KeyContainer = EmplaceUnsafeContainer; + using M = std::flat_map; + + LIBCPP_STATIC_ASSERT(!std::__container_traits::__emplacement_has_strong_exception_safety_guarantee); + KeyContainer a = {1, 2, 3, 4}; + std::vector b = {5, 6, 7, 8}; + M m(std::sorted_unique, std::move(a), std::move(b)); + try { + emplace_function(m, 0, 0); + assert(false); + } catch (int) { + check_invariant(m); + // In libc++, the flat_map is cleared + LIBCPP_ASSERT(m.size() == 0); + } + } + { + // Throw on emplace the value, and underlying has strong exception guarantee + using ValueContainer = std::vector>; + ; + using M = std::flat_map, ValueContainer>; + + LIBCPP_STATIC_ASSERT(std::__container_traits::__emplacement_has_strong_exception_safety_guarantee); + + std::vector a = {1, 2, 3, 4}; + test_allocator_statistics stats; + ValueContainer b({1, 2, 3, 4}, test_allocator{&stats}); + + [[maybe_unused]] auto expected_keys = a; + [[maybe_unused]] auto expected_values = b; + M m(std::sorted_unique, std::move(a), std::move(b)); + + stats.throw_after = 1; + try { + emplace_function(m, 0, 0); + assert(false); + } catch (const std::bad_alloc&) { + check_invariant(m); + // In libc++, the emplaced key is erased and the flat_map is unchanged + LIBCPP_ASSERT(m.size() == 4); + LIBCPP_ASSERT(m.keys() == expected_keys); + LIBCPP_ASSERT(m.values() == expected_values); + } + } + { + // Throw on emplace the value, and underlying has no strong exception guarantee + using ValueContainer = EmplaceUnsafeContainer; + using M = std::flat_map, ValueContainer>; + + LIBCPP_STATIC_ASSERT(!std::__container_traits::__emplacement_has_strong_exception_safety_guarantee); + std::vector a = {1, 2, 3, 4}; + ValueContainer b = {1, 2, 3, 4}; + + M m(std::sorted_unique, std::move(a), std::move(b)); + + try { + emplace_function(m, 0, 0); + assert(false); + } catch (int) { + check_invariant(m); + // In libc++, the flat_map is cleared + LIBCPP_ASSERT(m.size() == 0); + } + } + { + // Throw on emplace the value, then throw again on erasing the key + using KeyContainer = ThrowOnEraseContainer; + using ValueContainer = std::vector>; + using M = std::flat_map; + + LIBCPP_STATIC_ASSERT(std::__container_traits::__emplacement_has_strong_exception_safety_guarantee); + + KeyContainer a = {1, 2, 3, 4}; + test_allocator_statistics stats; + ValueContainer b({1, 2, 3, 4}, test_allocator{&stats}); + + M m(std::sorted_unique, std::move(a), std::move(b)); + stats.throw_after = 1; + try { + emplace_function(m, 0, 0); + assert(false); + } catch (const std::bad_alloc&) { + check_invariant(m); + // In libc++, we try to erase the key after value emplacement failure. + // and after erasure failure, we clear the flat_map + LIBCPP_ASSERT(m.size() == 0); + } + } +#endif +} + +template +void test_insert_range_exception_guarantee([[maybe_unused]] F&& insert_function) { +#ifndef TEST_HAS_NO_EXCEPTIONS + using KeyContainer = EmplaceUnsafeContainer; + using ValueContainer = std::vector; + using M = std::flat_map; + test_allocator_statistics stats; + KeyContainer a{1, 2, 3, 4}; + ValueContainer b{1, 2, 3, 4}; + M m(std::sorted_unique, std::move(a), std::move(b)); + + std::vector> newValues = {{0, 0}, {1, 1}, {5, 5}, {6, 6}, {7, 7}, {8, 8}}; + stats.throw_after = 1; + try { + insert_function(m, newValues); + assert(false); + } catch (int) { + check_invariant(m); + // In libc++, we clear if anything goes wrong when inserting a range + LIBCPP_ASSERT(m.size() == 0); + } +#endif +} + +template +void test_erase_exception_guarantee([[maybe_unused]] F&& erase_function) { +#ifndef TEST_HAS_NO_EXCEPTIONS + { + // key erase throws + using KeyContainer = ThrowOnEraseContainer; + using ValueContainer = std::vector; + using M = std::flat_map; + + KeyContainer a{1, 2, 3, 4}; + ValueContainer b{1, 2, 3, 4}; + M m(std::sorted_unique, std::move(a), std::move(b)); + try { + erase_function(m, 3); + assert(false); + } catch (int) { + check_invariant(m); + // In libc++, we clear if anything goes wrong when erasing + LIBCPP_ASSERT(m.size() == 0); + } + } + { + // key erase throws + using KeyContainer = std::vector; + using ValueContainer = ThrowOnEraseContainer; + using M = std::flat_map; + + KeyContainer a{1, 2, 3, 4}; + ValueContainer b{1, 2, 3, 4}; + M m(std::sorted_unique, std::move(a), std::move(b)); + try { + erase_function(m, 3); + assert(false); + } catch (int) { + check_invariant(m); + // In libc++, we clear if anything goes wrong when erasing + LIBCPP_ASSERT(m.size() == 0); + } + } +#endif +} +class Moveable { + int int_; + double double_; + +public: + Moveable() : int_(0), double_(0) {} + Moveable(int i, double d) : int_(i), double_(d) {} + Moveable(Moveable&& x) : int_(x.int_), double_(x.double_) { + x.int_ = -1; + x.double_ = -1; + } + Moveable& operator=(Moveable&& x) { + int_ = x.int_; + x.int_ = -1; + double_ = x.double_; + x.double_ = -1; + return *this; + } + + Moveable(const Moveable&) = delete; + Moveable& operator=(const Moveable&) = delete; + bool operator==(const Moveable& x) const { return int_ == x.int_ && double_ == x.double_; } + bool operator<(const Moveable& x) const { return int_ < x.int_ || (int_ == x.int_ && double_ < x.double_); } + + int get() const { return int_; } + bool moved() const { return int_ == -1; } +}; + +#endif // SUPPORT_FLAT_MAP_HELPERS_H diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/incomplete_type.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/incomplete_type.pass.cpp new file mode 100644 index 00000000000000..81c590ba73a157 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/incomplete_type.pass.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// Check that std::flat_map and its iterators can be instantiated with an incomplete +// type. + +#include + +struct A { + using Map = std::flat_map; + int data; + Map m; + Map::iterator it; + Map::const_iterator cit; +}; + +// Implement the operator< required in order to instantiate flat_map +bool operator<(A const& L, A const& R) { return L.data < R.data; } + +int main(int, char**) { + A a; + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/op_compare.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/op_compare.pass.cpp new file mode 100644 index 00000000000000..fffe7115807040 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/op_compare.pass.cpp @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// + +// friend bool operator==(const flat_map& x, const flat_map& y); +// friend synth-three-way-result +// operator<=>(const flat_map& x, const flat_map& y); + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "MinSequenceContainer.h" +#include "test_macros.h" +#include "min_allocator.h" +#include "test_allocator.h" +#include "test_comparisons.h" +#include "test_container_comparisons.h" + +template +void test() { + using Key = typename KeyContainer::value_type; + using Value = typename ValueContainer::value_type; + + { + using C = std::flat_map; + C s1 = {{1, 1}}; + C s2 = {{2, 0}}; // {{1,1}} versus {{2,0}} + ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering); + AssertComparisonsReturnBool(); + assert(testComparisons(s1, s2, false, true)); + s2 = {{1, 1}}; // {{1,1}} versus {{1,1}} + assert(testComparisons(s1, s2, true, false)); + s2 = {{1, 1}, {2, 0}}; // {{1,1}} versus {{1,1},{2,0}} + assert(testComparisons(s1, s2, false, true)); + s1 = {{0, 0}, {1, 1}, {2, 2}}; // {{0,0},{1,1},{2,2}} versus {{1,1},{2,0}} + assert(testComparisons(s1, s2, false, true)); + s2 = {{0, 0}, {1, 1}, {2, 3}}; // {{0,0},{1,1},{2,2}} versus {{0,0},{1,1},{2,3}} + assert(testComparisons(s1, s2, false, true)); + } + { + // Comparisons use value_type's native operators, not the comparator + using C = std::flat_map>; + C s1 = {{1, 1}}; + C s2 = {{2, 0}}; // {{1,1}} versus {{2,0}} + ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering); + AssertComparisonsReturnBool(); + assert(testComparisons(s1, s2, false, true)); + s2 = {{1, 1}}; // {{1,1}} versus {{1,1}} + assert(testComparisons(s1, s2, true, false)); + s2 = {{1, 1}, {2, 0}}; // {{1,1}} versus {{2,0},{1,1}} + assert(testComparisons(s1, s2, false, true)); + s1 = {{0, 0}, {1, 1}, {2, 2}}; // {{2,2},{1,1},{0,0}} versus {2,0},{1,1}} + assert(testComparisons(s1, s2, false, false)); + s2 = {{0, 0}, {1, 1}, {2, 3}}; // {{2,2},{1,1},{0,0}} versus {{2,3},{1,1},{0,0}} + assert(testComparisons(s1, s2, false, true)); + } +} + +int main(int, char**) { + test, std::vector>(); + test, std::deque>(); + test, MinSequenceContainer>(); + test>, std::vector>>(); + test>, std::vector>>(); + + { + using C = std::flat_map; + C s1 = {{1, 1}}; + C s2 = C(std::sorted_unique, {{std::numeric_limits::quiet_NaN(), 2}}); + ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering); + AssertComparisonsReturnBool(); + assert(testComparisonsComplete(s1, s2, false, false, false)); + } + { + using C = std::flat_map; + C s1 = {{1, 1}}; + C s2 = C(std::sorted_unique, {{2, std::numeric_limits::quiet_NaN()}}); + ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering); + AssertComparisonsReturnBool(); + assert(testComparisonsComplete(s1, s2, false, true, false)); + s2 = C(std::sorted_unique, {{1, std::numeric_limits::quiet_NaN()}}); + assert(testComparisonsComplete(s1, s2, false, false, false)); + } + { + // Comparisons use value_type's native operators, not the comparator + struct StrongComp { + bool operator()(double a, double b) const { return std::strong_order(a, b) < 0; } + }; + using C = std::flat_map; + C s1 = {{1, 1}}; + C s2 = {{std::numeric_limits::quiet_NaN(), std::numeric_limits::quiet_NaN()}}; + ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering); + AssertComparisonsReturnBool(); + assert(testComparisonsComplete(s1, s2, false, false, false)); + s1 = {{{1, 1}, {std::numeric_limits::quiet_NaN(), 1}}}; + s2 = {{{std::numeric_limits::quiet_NaN(), 1}, {1, 1}}}; + assert(std::lexicographical_compare_three_way( + s1.keys().begin(), s1.keys().end(), s2.keys().begin(), s2.keys().end(), std::strong_order) == + std::strong_ordering::equal); + assert(s1 != s2); + assert((s1 <=> s2) == std::partial_ordering::unordered); + } + return 0; +} diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/types.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/types.compile.pass.cpp new file mode 100644 index 00000000000000..ea9d4d7fca67f0 --- /dev/null +++ b/libcxx/test/std/containers/container.adaptors/flat.map/types.compile.pass.cpp @@ -0,0 +1,133 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 + +// using key_type = Key; +// using mapped_type = T; +// using value_type = pair; +// using key_compare = Compare; +// using reference = pair; +// using const_reference = pair; +// using size_type = size_t; +// using difference_type = ptrdiff_t; +// using iterator = implementation-defined; // see [container.requirements] +// using const_iterator = implementation-defined; // see [container.requirements] +// using reverse_iterator = std::reverse_iterator; +// using const_reverse_iterator = std::reverse_iterator; +// using key_container_type = KeyContainer; +// using mapped_container_type = MappedContainer; + +// class value_compare; + +// struct containers { +// key_container_type keys; +// mapped_container_type values; +// }; + +#include +#include +#include +#include +#include +#include +#include +#include "min_allocator.h" + +void test() { + { + using M = std::flat_map; + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(requires { typename M::iterator; }); + static_assert(requires { typename M::const_iterator; }); + static_assert(std::is_same_v>); + static_assert( + std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(requires { typename M::value_compare; }); + static_assert(requires { typename M::containers; }); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + } + + { + struct A {}; + struct Compare { + bool operator()(const std::string&, const std::string&) const; + }; + using M = std::flat_map, std::deque>; + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v>); + static_assert(std::is_same_v); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(requires { typename M::iterator; }); + static_assert(requires { typename M::const_iterator; }); + static_assert(std::is_same_v>); + static_assert( + std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(requires { typename M::value_compare; }); + static_assert(requires { typename M::containers; }); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + } + { + using C = std::flat_map; + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(!std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + } + { + using C = std::flat_map, std::deque>>; + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(!std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::random_access_iterator); + static_assert(std::is_same_v>); + static_assert(std::is_same_v>); + // size_type is invariably size_t + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v>>); + static_assert(std::is_same_v>); + } +} diff --git a/libcxx/test/support/MinSequenceContainer.h b/libcxx/test/support/MinSequenceContainer.h new file mode 100644 index 00000000000000..d0e29ae40c400d --- /dev/null +++ b/libcxx/test/support/MinSequenceContainer.h @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_MIN_SEQUENCE_CONTAINER_H +#define SUPPORT_MIN_SEQUENCE_CONTAINER_H + +#include +#include + +#include "test_iterators.h" + +template , class ConstIterator = random_access_iterator> +struct MinSequenceContainer { + using value_type = T; + using difference_type = int; + using size_type = unsigned int; + using iterator = Iterator; + using const_iterator = ConstIterator; + + explicit MinSequenceContainer() = default; + template + explicit MinSequenceContainer(It first, It last) : data_(first, last) {} + MinSequenceContainer(std::initializer_list il) : data_(il) {} + iterator begin() { return iterator(data_.data()); } + const_iterator begin() const { return const_iterator(data_.data()); } + const_iterator cbegin() const { return const_iterator(data_.data()); } + iterator end() { return begin() + size(); } + const_iterator end() const { return begin() + size(); } + size_type size() const { return data_.size(); } + bool empty() const { return data_.empty(); } + + void clear() { data_.clear(); } + + template + iterator insert(const_iterator p, It first, It last) { + return from_vector_iterator(data_.insert(to_vector_iterator(p), first, last)); + } + + iterator insert(const_iterator p, T value) { + return from_vector_iterator(data_.insert(to_vector_iterator(p), std::move(value))); + } + + iterator erase(const_iterator first, const_iterator last) { + return from_vector_iterator(data_.erase(to_vector_iterator(first), to_vector_iterator(last))); + } + + iterator erase(const_iterator iter) { return from_vector_iterator(data_.erase(to_vector_iterator(iter))); } + + template + iterator emplace(const_iterator pos, Args&&... args) { + return from_vector_iterator(data_.emplace(to_vector_iterator(pos), std::forward(args)...)); + } + +private: + std::vector::const_iterator to_vector_iterator(const_iterator cit) const { return cit - cbegin() + data_.begin(); } + + iterator from_vector_iterator(std::vector::iterator it) { return it - data_.begin() + begin(); } + + std::vector data_; +}; + +namespace MinSequenceContainer_detail { + +// MinSequenceContainer is non-allocator-aware, because flat_set supports +// such (non-STL) container types, and we want to make sure they are supported. +template +concept HasAllocatorType = requires { typename T::allocator_type; }; +static_assert(!HasAllocatorType>); + +// MinSequenceContainer by itself doesn't support .emplace(), because we want +// to at least somewhat support (non-STL) container types with nothing but .insert(). +template +concept HasEmplace = requires(T& t) { t.emplace(42); }; +static_assert(!HasEmplace>); + +} // namespace MinSequenceContainer_detail + +#endif // SUPPORT_MIN_SEQUENCE_CONTAINER_H diff --git a/libcxx/utils/libcxx/header_information.py b/libcxx/utils/libcxx/header_information.py index 528eb9995e19f4..3b12dcb9f56c0b 100644 --- a/libcxx/utils/libcxx/header_information.py +++ b/libcxx/utils/libcxx/header_information.py @@ -163,7 +163,6 @@ def __hash__(self) -> int: # modules will fail to build if a header is added but this list is not updated. headers_not_available = list(map(Header, [ "debugging", - "flat_map", "flat_set", "generator", "hazard_pointer", @@ -251,6 +250,7 @@ def __hash__(self) -> int: "coroutine": ["compare"], "deque": ["compare", "initializer_list"], "filesystem": ["compare"], + "flat_map": ["compare", "initializer_list"], "forward_list": ["compare", "initializer_list"], "ios": ["iosfwd"], "iostream": ["ios", "istream", "ostream", "streambuf"],