[libcxx-commits] [libcxx] [libc++] Implement P0429R9 `std::flat_map` (PR #98643)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Jul 19 09:56:37 PDT 2024
https://github.com/huixie90 updated https://github.com/llvm/llvm-project/pull/98643
>From 640e883e09f618e74cd0769bf619de7ff65930c3 Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Fri, 12 Jul 2024 13:01:52 +0100
Subject: [PATCH 1/2] [libc++] Implement P0429R9 `std::flat_map`
---
libcxx/docs/Status/Cxx23Papers.csv | 2 +-
libcxx/include/CMakeLists.txt | 3 +
libcxx/include/__flat_map/flat_map.h | 1231 ++++++++++++++++++++
libcxx/include/__flat_map/sorted_unique.h | 31 +
libcxx/include/__memory/allocator_traits.h | 3 +
libcxx/include/flat_map | 72 ++
libcxx/include/module.modulemap | 7 +
7 files changed, 1348 insertions(+), 1 deletion(-)
create mode 100644 libcxx/include/__flat_map/flat_map.h
create mode 100644 libcxx/include/__flat_map/sorted_unique.h
create mode 100644 libcxx/include/flat_map
diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index 36d7f78285aa9..88ff1d557c1b3 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -52,7 +52,7 @@
"`P2443R1 <https://wg21.link/P2443R1>`__","LWG","``views::chunk_by``","February 2022","|Complete|","18.0","|ranges|"
"","","","","","",""
"`P0009R18 <https://wg21.link/P0009R18>`__","LWG","mdspan: A Non-Owning Multidimensional Array Reference","July 2022","|Complete|","18.0"
-"`P0429R9 <https://wg21.link/P0429R9>`__","LWG","A Standard ``flat_map``","July 2022","",""
+"`P0429R9 <https://wg21.link/P0429R9>`__","LWG","A Standard ``flat_map``","July 2022","|In progress|",""
"`P1169R4 <https://wg21.link/P1169R4>`__","LWG","``static operator()``","July 2022","|Complete|","16.0"
"`P1222R4 <https://wg21.link/P1222R4>`__","LWG","A Standard ``flat_set``","July 2022","",""
"`P1223R5 <https://wg21.link/P1223R5>`__","LWG","``ranges::find_last()``, ``ranges::find_last_if()``, and ``ranges::find_last_if_not()``","July 2022","","","|ranges|"
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 8d0ffd6ed725b..24a7f69f2328f 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -351,6 +351,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
@@ -938,6 +940,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 0000000000000..25b0f5979de56
--- /dev/null
+++ b/libcxx/include/__flat_map/flat_map.h
@@ -0,0 +1,1231 @@
+// -*- 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/ranges_equal.h>
+#include <__algorithm/ranges_lexicographical_compare.h>
+#include <__algorithm/ranges_lower_bound.h>
+#include <__algorithm/ranges_sort.h>
+#include <__algorithm/ranges_unique.h>
+#include <__compare/synth_three_way.h>
+#include <__config>
+#include <__flat_map/sorted_unique.h>
+#include <__functional/is_transparent.h>
+#include <__functional/operations.h>
+#include <__iterator/distance.h>
+#include <__iterator/iterator_traits.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/container_compatible_range.h>
+#include <__ranges/zip_view.h>
+#include <__utility/pair.h>
+#include <initializer_list>
+#include <stdexcept>
+#include <vector>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#if _LIBCPP_STD_VER >= 23
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Key,
+ class _Tp,
+ class _Compare = less<_Key>,
+ class _KeyContainer = vector<_Key>,
+ class _MappedContainer = vector<_Tp>>
+class flat_map {
+ template <bool _Const>
+ struct __iterator;
+
+public:
+ // types
+ using key_type = _Key;
+ using mapped_type = _Tp;
+ using value_type = pair<key_type, mapped_type>;
+ using key_compare = _Compare;
+ using reference = pair<const key_type&, mapped_type&>;
+ using const_reference = pair<const key_type&, const mapped_type&>;
+ using size_type = size_t;
+ using difference_type = ptrdiff_t;
+ using iterator = __iterator<false>; // see [container.requirements]
+ using const_iterator = __iterator<true>; // see [container.requirements]
+ using reverse_iterator = std::reverse_iterator<iterator>;
+ using const_reverse_iterator = std::reverse_iterator<const_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) {}
+
+ 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 <class _Allocator>
+ _LIBCPP_HIDE_FROM_ABI static constexpr bool __allocator_ctor_constraint =
+ _And<uses_allocator<key_container_type, _Allocator>, uses_allocator<mapped_container_type, _Allocator>>::value;
+
+ _LIBCPP_HIDE_FROM_ABI static constexpr bool __is_compare_transparent = __is_transparent_v<_Compare, _Compare>;
+
+ template <bool _Const>
+ struct __iterator {
+ private:
+ using __key_iterator = ranges::iterator_t<__maybe_const<_Const, key_container_type>>;
+ using __mapped_iterator = ranges::iterator_t<__maybe_const<_Const, mapped_container_type>>;
+ using __reference = conditional_t<_Const, flat_map::const_reference, flat_map::reference>;
+
+ struct __arrow_proxy {
+ __reference __ref_;
+ _LIBCPP_HIDE_FROM_ABI __reference* operator->() { return std::addressof(__ref_); }
+ };
+
+ __key_iterator __key_iter_;
+ __mapped_iterator __mapped_iter_;
+
+ public:
+ using iterator_concept = random_access_iterator_tag;
+ using iterator_category = input_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<!_Const> __i)
+ requires _Const && convertible_to<ranges::iterator_t<key_container_type>, __key_iterator> &&
+ convertible_to<ranges::iterator_t<mapped_container_type>, __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() : flat_map(key_compare()) {}
+
+ template <class _Allocator>
+ 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 <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI flat_map(flat_map&& __other, const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_tag{},
+ __alloc,
+ std::move(__other.__containers_.keys),
+ std::move(__other.__containers_.values),
+ std::move(__other.__compare_)) {}
+
+ _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) {
+ __sort_and_unique();
+ }
+
+ template <class _Allocator>
+ 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) {
+ __sort_and_unique();
+ }
+
+ template <class _Allocator>
+ 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) {
+ __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) {}
+
+ template <class _Allocator>
+ 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) {}
+
+ template <class _Allocator>
+ 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_HIDE_FROM_ABI explicit flat_map(const key_compare& __comp) : __containers_(), __compare_(__comp) {}
+
+ template <class _Allocator>
+ 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 <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI explicit flat_map(const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {}
+
+ template <class _InputIterator>
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(_InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())
+ : __containers_(), __compare_(__comp) {
+ insert(__first, __last);
+ }
+
+ template <class _InputIterator, class _Allocator>
+ requires __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 <class _InputIterator, class _Allocator>
+ requires __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<value_type> _Range>
+ _LIBCPP_HIDE_FROM_ABI flat_map(from_range_t __fr, _Range&& __rg)
+ : flat_map(__fr, std::forward<_Range>(__rg), key_compare()) {}
+
+ template <_ContainerCompatibleRange<value_type> _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<value_type> _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<value_type> _Range, class _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 <class _InputIterator>
+ _LIBCPP_HIDE_FROM_ABI flat_map(
+ sorted_unique_t __s, _InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())
+ : __containers_(), __compare_(__comp) {
+ insert(__s, __first, __last);
+ }
+ template <class _InputIterator, class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(sorted_unique_t __s,
+ _InputIterator __first,
+ _InputIterator __last,
+ const key_compare& __comp,
+ const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) {
+ insert(__s, __first, __last);
+ }
+
+ template <class _InputIterator, class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(sorted_unique_t __s, _InputIterator __first, _InputIterator __last, const _Allocator& __alloc)
+ : flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {
+ insert(__s, __first, __last);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI flat_map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())
+ : flat_map(__il.begin(), __il.end(), __comp) {}
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)
+ : flat_map(__il.begin(), __il.end(), __comp, __alloc) {}
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI flat_map(initializer_list<value_type> __il, const _Allocator& __alloc)
+ : flat_map(__il.begin(), __il.end(), __alloc) {}
+
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(sorted_unique_t __s, initializer_list<value_type> __il, const key_compare& __comp = key_compare())
+ : flat_map(__s, __il.begin(), __il.end(), __comp) {}
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI
+ flat_map(sorted_unique_t __s, initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)
+ : flat_map(__s, __il.begin(), __il.end(), __comp, __alloc) {}
+
+ template <class _Allocator>
+ requires __allocator_ctor_constraint<_Allocator>
+ _LIBCPP_HIDE_FROM_ABI flat_map(sorted_unique_t __s, initializer_list<value_type> __il, const _Allocator& __alloc)
+ : flat_map(__s, __il.begin(), __il.end(), __alloc) {}
+
+ _LIBCPP_HIDE_FROM_ABI flat_map& operator=(initializer_list<value_type> __il) {
+ clear();
+ insert(__il);
+ 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 { 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<size_type>(__containers_.keys.max_size(), __containers_.values.max_size());
+ }
+
+ // [flat.map.access], element access
+ _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __x) { return try_emplace(__x).first->second; }
+
+ _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](key_type&& __x) { return try_emplace(std::move(__x)).first->second; }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _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()) {
+ __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()) {
+ __throw_out_of_range("flat_map::at(const key_type&) const: Key does not exist");
+ }
+ return (*__it).second;
+ }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI mapped_type& at(const _Kp& __x) {
+ static_assert(requires { find(__x); }, "flat_map::at(const K& x): find(x) needs to be well-formed");
+ auto __it = find(__x);
+ if (__it == end()) {
+ __throw_out_of_range("flat_map::at(const K&): Key does not exist");
+ }
+ return (*__it).second;
+ }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI const mapped_type& at(const _Kp& __x) const {
+ static_assert(requires { find(__x); }, "flat_map::at(const K& x) const: find(x) needs to be well-formed");
+ auto __it = find(__x);
+ if (__it == end()) {
+ __throw_out_of_range("flat_map::at(const K&) const: Key does not exist");
+ }
+ return (*__it).second;
+ }
+
+ // [flat.map.modifiers], modifiers
+ template <class... _Args>
+ requires is_constructible_v<pair<key_type, mapped_type>, _Args...>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> emplace(_Args&&... __args) {
+ std::pair<key_type, value_type> __pair(std::forward<_Args>(__args)...);
+ return __binary_search_emplace_impl(std::move(__pair));
+ }
+
+ template <class... _Args>
+ requires is_constructible_v<pair<key_type, mapped_type>, _Args...>
+ _LIBCPP_HIDE_FROM_ABI iterator emplace_hint(const_iterator __hint, _Args&&... __args) {
+ std::pair<key_type, value_type> __pair(std::forward<_Args>(__args)...);
+ if (__is_hint_correct(__hint, __pair.first)) {
+ if (__compare_(__pair.first, __hint->first)) {
+ return __emplace_impl(__hint, std::move(__pair));
+ } else {
+ // key equals
+ auto __dist = __hint - cbegin();
+ return iterator(__containers_.keys.begin() + __dist, __containers_.values.begin() + __dist);
+ }
+ } else {
+ return __binary_search_emplace_impl(std::move(__pair)).first;
+ }
+ }
+
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert(const value_type& __x) { return emplace(__x); }
+
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> 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 <class _Pp>
+ requires is_constructible_v<pair<key_type, mapped_type>, _Pp>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert(_Pp&& __x) {
+ return emplace(std::forward<_Pp>(__x));
+ }
+
+ template <class _Pp>
+ requires is_constructible_v<pair<key_type, mapped_type>, _Pp>
+ _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator __hint, _Pp&& __x) {
+ return emplace_hint(__hint, std::forward<_Pp>(__x));
+ }
+
+ template <class _InputIterator>
+ _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last) {
+ insert(ranges::subrange<_InputIterator>(std::move(__first), std::move(__last)));
+ }
+
+ template <class _InputIterator>
+ void insert(sorted_unique_t, _InputIterator __first, _InputIterator __last) {
+ if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {
+ __reserve_impl(__last - __first);
+ }
+
+ auto __it = begin();
+ auto __end = end();
+ while (__first != __last) {
+ value_type __pair(*__first);
+ __it = ranges::lower_bound(__it, __end, __compare_, &containers::keys);
+ if (__it == __end || __compare_(__pair.first, __it->first)) {
+ __it = __emplace_impl(__it, std::move(__pair));
+ }
+ ++__it;
+ ++__first;
+ }
+ }
+
+ template <_ContainerCompatibleRange<value_type> _Range>
+ _LIBCPP_HIDE_FROM_ABI void insert_range(_Range&& __range) {
+ if constexpr (ranges::sized_range<_Range>) {
+ __reserve_impl(ranges::size(__range));
+ }
+
+ auto __last = ranges::end(__range);
+ for (auto __it = ranges::begin(__range); __it != __last; ++__it) {
+ __binary_search_emplace_impl(value_type(*__it));
+ }
+ }
+
+ _LIBCPP_HIDE_FROM_ABI void insert(initializer_list<value_type> __il) { insert(__il.begin(), __il.end()); }
+
+ _LIBCPP_HIDE_FROM_ABI void insert(sorted_unique_t __s, initializer_list<value_type> __il) {
+ insert(__s, __il.begin(), __il.end());
+ }
+
+ _LIBCPP_HIDE_FROM_ABI containers extract() && {
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ return std::move(__containers_);
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (...) {
+ clear();
+ throw;
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+
+ _LIBCPP_HIDE_FROM_ABI void replace(key_container_type&& __key_cont, mapped_container_type&& __mapped_cont) {
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ __containers_.keys = std::move(__key_cont);
+ __containers_.values = std::move(__mapped_cont);
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (...) {
+ clear();
+ throw;
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+
+ template <class... _Args>
+ requires is_constructible_v<mapped_type, _Args...>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> try_emplace(const key_type& __key, _Args&&... __args) {
+ return __binary_search_try_emplace_impl(__key, std::forward<_Args>(__args)...);
+ }
+
+ template <class... _Args>
+ requires is_constructible_v<mapped_type, _Args...>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> try_emplace(key_type&& __key, _Args&&... __args) {
+ return __binary_search_try_emplace_impl(std::move(__key), std::forward<_Args>(__args)...);
+ }
+
+ template <class _Kp, class... _Args>
+ requires __is_compare_transparent && is_constructible_v<key_type, _Kp> &&
+ is_constructible_v<mapped_type, _Args...> && is_convertible_v<_Kp&&, const_iterator> &&
+ is_convertible_v<_Kp&&, iterator>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> try_emplace(_Kp&& __key, _Args&&... __args) {
+ return __binary_search_try_emplace_impl(std::forward<_Kp>(__key), std::forward<_Args>(__args)...);
+ }
+
+ template <class... _Args>
+ requires is_constructible_v<mapped_type, _Args...>
+ _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __hint, const key_type& __key, _Args&&... __args) {
+ return try_emplace_hint_impl(__hint, __key, std::forward<_Args>(__args)...);
+ }
+
+ template <class... _Args>
+ requires is_constructible_v<mapped_type, _Args...>
+ _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __hint, key_type&& __key, _Args&&... __args) {
+ return try_emplace_hint_impl(__hint, std::move(__key), std::forward<_Args>(__args)...);
+ }
+
+ template <class _Kp, class... _Args>
+ requires __is_compare_transparent && is_constructible_v<key_type, _Kp> && is_constructible_v<mapped_type, _Args...>
+ _LIBCPP_HIDE_FROM_ABI iterator try_emplace(const_iterator __hint, _Kp&& __key, _Args&&... __args) {
+ return try_emplace_hint_impl(__hint, std::forward<_Kp>(__key), std::forward<_Args>(__args)...);
+ }
+
+ template <class _Mapped>
+ requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert_or_assign(const key_type& __key, _Mapped&& __obj) {
+ return __insert_or_assign_impl(__key, std::forward<_Mapped>(__obj));
+ }
+
+ template <class _Mapped>
+ requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert_or_assign(key_type&& __key, _Mapped&& __obj) {
+ return __insert_or_assign_impl(std::move(__key), std::forward<_Mapped>(__obj));
+ }
+
+ template <class _Kp, class _Mapped>
+ requires __is_compare_transparent && is_constructible_v<key_type, _Kp> && is_assignable_v<mapped_type&, _Mapped> &&
+ is_constructible_v<mapped_type, _Mapped>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> insert_or_assign(_Kp&& __key, _Mapped&& __obj) {
+ return __insert_or_assign_impl(std::forward<_Kp>(__key), std::forward<_Mapped>(__obj));
+ }
+
+ template <class _Mapped>
+ requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>
+ _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __hint, const key_type& __key, _Mapped&& __obj) {
+ return __insert_or_assign_impl(__key, std::forward<_Mapped>(__obj), __hint).first;
+ }
+
+ template <class _Mapped>
+ requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>
+ _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __hint, key_type&& __key, _Mapped&& __obj) {
+ return __insert_or_assign_impl(std::move(__key), std::forward<_Mapped>(__obj), __hint).first;
+ }
+
+ template <class _Kp, class _Mapped>
+ requires __is_compare_transparent && is_constructible_v<key_type, _Kp> && is_assignable_v<mapped_type&, _Mapped> &&
+ is_constructible_v<mapped_type, _Mapped>
+ _LIBCPP_HIDE_FROM_ABI iterator insert_or_assign(const_iterator __hint, _Kp&& __key, _Mapped&& __obj) {
+ return __insert_or_assign_impl(std::forward<_Kp>(__key), std::forward<_Mapped>(__obj), __hint).first;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI iterator erase(iterator __position) {
+ return __erase_impl(__position.__key_iter_, __position.__mapped_iter);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position) {
+ return __erase_impl(__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 <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI size_type erase(_Kp&& __x) {
+ auto __iter = find(__x);
+ if (__iter != end()) {
+ erase(__iter);
+ return 1;
+ }
+ return 0;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last) {
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ auto __key_it = __containers_.keys.erase(__first.__key_iter, __last.__key_iter);
+ auto __mapped_it = __containers_.values.erase(__first.__mapped_iter, __last.__mapped_iter);
+ return iterator(std::move(__key_it), std::move(__mapped_it));
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (const exception& __ex) {
+ clear();
+ throw flat_map_restore_error(
+ std::string("flat_map::erase: "
+ "Unable to restore flat_map to previous state. Clear out the containers to make the two "
+ "containers consistent. Reason: ") +
+ __ex.what());
+ } catch (...) {
+ clear();
+ throw flat_map_restore_error(
+ "flat_map::erase: "
+ "Unable to restore flat_map to previous state. Clear out the containers to make the two "
+ "containers consistent.");
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+
+ _LIBCPP_HIDE_FROM_ABI void swap(flat_map& __y) noexcept {
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ swap(__compare_, __y.__compare_);
+ swap(__containers_.keys, __y.__containers_.keys);
+ swap(__containers_.values, __y.__containers_.values);
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (...) {
+ clear();
+ __y.clear();
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+
+ _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 <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI iterator find(const _Kp& __x) {
+ return __find_impl(*this, __x);
+ }
+
+ template <class _Kp>
+ 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 <class _Kp>
+ _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 <class _Kp>
+ _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_impl<iterator>(*this, __x); }
+
+ _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const key_type& __x) const {
+ return __lower_bound_impl<const_iterator>(*this, __x);
+ }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _Kp& __x) {
+ return __lower_bound_impl<iterator>(*this, __x);
+ }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _Kp& __x) const {
+ return __lower_bound_impl<const_iterator>(*this, __x);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const key_type& __x) { return __upper_bound_impl<iterator>(*this, __x); }
+
+ _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const key_type& __x) const {
+ return __upper_bound_impl<const_iterator>(*this, __x);
+ }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _Kp& __x) {
+ return __upper_bound_impl<iterator>(*this, __x);
+ }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _Kp& __x) const {
+ return __upper_bound_impl<iterator>(*this, __x);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const key_type& __x) {
+ return __equal_range_impl(*this, __x);
+ }
+
+ _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> equal_range(const key_type& __x) const {
+ return __equal_range_impl(*this, __x);
+ }
+
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> equal_range(const _Kp& __x) {
+ return __equal_range_impl(*this, __x);
+ }
+ template <class _Kp>
+ requires __is_compare_transparent
+ _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> 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 __synth_three_way_result<value_type>
+ operator<=>(const flat_map& __x, const flat_map& __y) {
+ return ranges::lexicographical_compare(__x, __y);
+ }
+
+ friend _LIBCPP_HIDE_FROM_ABI void swap(flat_map& __x, flat_map& __y) noexcept { __x.swap(__y); }
+
+private:
+ struct __ctor_uses_allocator_tag {};
+ struct __ctor_uses_allocator_empty_tag {};
+ _LIBCPP_HIDE_FROM_ABI void __sort_and_unique() {
+ auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
+ ranges::sort(__zv, __compare_, &containers::keys);
+ auto __it = ranges::unique(__zv, __key_equiv(__compare_)).begin();
+ auto __dist = ranges::distance(__zv.begin(), __it);
+ __containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end());
+ __containers_.values.erase(__containers_.values.begin() + __dist, __containers_.values.end());
+ }
+
+ template <class _Allocator, class _KeyCont, class _MappedCont, class... _CompArg>
+ _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<key_container_type>(
+ __alloc, std::forward<_KeyCont>(__key_cont)),
+ .values = std::make_obj_using_allocator<mapped_container_type>(
+ __alloc, std::forward<_MappedCont>(__mapped_cont))},
+ __compare_(std::forward<_CompArg>(__comp)...) {}
+
+ template <class _Allocator, class... _CompArg>
+ _LIBCPP_HIDE_FROM_ABI flat_map(__ctor_uses_allocator_empty_tag, const _Allocator& __alloc, _CompArg&&... __comp)
+ : __containers_{.keys = std::make_obj_using_allocator<key_container_type>(__alloc),
+ .values = std::make_obj_using_allocator<mapped_container_type>(__alloc)},
+ __compare_(std::forward<_CompArg>(__comp)...) {}
+
+ template <class _Self, class _Kp>
+ _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 <class _Self, class _Kp>
+ _LIBCPP_HIDE_FROM_ABI static auto __equal_range_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 std::make_pair(std::move(__it), std::move(__last));
+ }
+ return std::make_pair(__it, std::next(__it));
+ }
+
+ template <class _Res, class _Self, class _Kp>
+ _LIBCPP_HIDE_FROM_ABI static _Res __lower_bound_impl(_Self&& __self, _Kp& __x) {
+ return __binary_search_impl<_Res>(ranges::lower_bound, __self, __x);
+ }
+
+ template <class _Res, class _Self, class _Kp>
+ _LIBCPP_HIDE_FROM_ABI static _Res __upper_bound_impl(_Self&& __self, _Kp& __x) {
+ return __binary_search_impl<_Res>(ranges::upper_bound, __self, __x);
+ }
+
+ template <class _Kp>
+ _LIBCPP_HIDE_FROM_ABI bool __is_hint_correct(const_iterator __hint, _Kp&& __key) {
+ if (__hint != cbegin() && !__compare_(std::prev(__hint)->first, __key)) {
+ return false;
+ }
+ if (__hint != cend() && __compare(__hint->first, __key)) {
+ return false;
+ }
+ return true;
+ }
+
+ template <class _Res, class _Fn, class _Self, class _Kp>
+ _LIBCPP_HIDE_FROM_ABI static _Res __binary_search_impl(_Fn __search_fn, _Self&& __self, _Kp& __x) {
+ auto __key_iter = __search_fn(__self.__containers_.keys, __self.__compare_, __x);
+ auto __mapped_iter =
+ __self.__containers_.values.begin() +
+ static_cast<ranges::range_difference_t<mapped_container_type>>(
+ ranges::distance(__self.__containers_.values.begin(), __key_iter));
+
+ return _Res(std::move(__key_iter), std::move(__mapped_iter));
+ }
+
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __binary_search_emplace_impl(std::pair<key_type, value_type>&& __pair) {
+ if (auto __it = lower_bound(__pair.first); __it == end() || __compare_(__pair.first, (*__it).first)) {
+ return pair<iterator, bool>(__emplace_impl(__it, std::move(__pair)), true);
+ } else {
+ return pair<iterator, bool>(std::move(__it), false);
+ }
+ }
+
+ template <class _Iter>
+ _LIBCPP_HIDE_FROM_ABI iterator __emplace_impl(_Iter&& __it, std::pair<key_type, value_type>&& __pair) {
+ return __try_emplace_impl(__it.__key_iter, __it.__mapped_iter, std::move(__pair.first), std::move(__pair.second));
+ }
+
+ template <class _KeyArg, class... _MArgs>
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool>
+ __binary_search_try_emplace_impl(_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<iterator, bool>(
+ __try_emplace_impl(std::move(__key_it),
+ std::move(__mapped_it),
+ std::forward<_KeyArg>(__key),
+ std::forward<_MArgs>(__mapped_args)...),
+ true);
+ } else {
+ return pair<iterator, bool>(iterator(std::move(__key_it), std::move(__mapped_it)), false);
+ }
+ }
+
+ template <class _Kp, class... _Args>
+ _LIBCPP_HIDE_FROM_ABI iterator try_emplace_hint_impl(const_iterator __hint, _Kp&& __key, _Args&&... __args) {
+ if (__is_hint_correct(__hint, __key)) {
+ if (__compare_(__key, __hint->first)) {
+ return __try_emplace_impl(
+ __hint.__key_iter_, __hint.__mapped_iter_, std::forward<_Kp>(__key), std::forward<_Args>(__args)...);
+ } else {
+ // key equals
+ auto __dist = __hint - cbegin();
+ return iterator(__containers_.keys.begin() + __dist, __containers_.values.begin() + __dist);
+ }
+ } else {
+ __binary_search_try_emplace_impl(std::forward<_Kp>(__key), std::forward<_Args>(__args)...).first;
+ }
+ }
+
+ template <class _Container>
+ static consteval bool __failed_emplacement_has_side_effects() {
+ // [container.reqmts] If an exception is thrown by an insert() or emplace() function while inserting a single
+ // element, that function has no effects. Except that there is exceptional cases...
+
+ // according to http://eel.is/c++draft/deque.modifiers#3 and http://eel.is/c++draft/vector.modifiers#2,
+ // the only exceptions that can cause side effects on single emplacement are by move constructors of
+ // non-Cpp17CopyInsertable T
+
+ using _Element = typename _Container::value_type;
+ if constexpr (is_nothrow_move_constructible_v<_Element>) {
+ return false;
+ } else {
+ if constexpr (requires { typename _Container::allocator_type; }) {
+ return !__is_cpp17_copy_insertable<typename _Container::allocator_type>::value;
+ } else {
+ return !__is_cpp17_copy_insertable<std::allocator<_Element>>::value;
+ }
+ }
+ }
+
+ struct flat_map_restore_error : runtime_error {
+ using runtime_error::runtime_error;
+ };
+
+ template <class _Container, class _Iter, class... _Args>
+ _LIBCPP_HIDE_FROM_ABI auto __safe_emplace(_Container& __container, _Iter&& __iter, _Args&&... __args) {
+ if constexpr (!__failed_emplacement_has_side_effects<_Container>()) {
+ // just let the exception be thrown as the container is still in its original state on exception
+ return __container.emplace(__iter, std::forward<_Args>(__args)...);
+ } else {
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ return __container.emplace(__iter, std::forward<_Args>(__args)...);
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (const exception& __ex) {
+ // The container might be in some unknown state and we can't get flat_map into consistent state
+ // because we have two containers. The only possible solution is to clear them out
+ clear();
+ throw flat_map_restore_error(
+ std::string("flat_map::emplace: Emplacement on the underlying container has failed and has side effect. "
+ "Unable to restore flat_map to previous state. Clear out the containers to make the two "
+ "containers consistent. Reason: ") +
+ __ex.what());
+ } catch (...) {
+ // The container might be in some unknown state and we can't get flat_map into consistent state
+ // because we have two containers. The only possible solution is to clear them out
+ clear();
+ throw flat_map_restore_error(
+ "flat_map::emplace: Emplacement on the underlying container has failed and has side effect. "
+ "Unable to restore flat_map to previous state. Clear out the containers to make the two "
+ "containers consistent.");
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+ }
+
+ template <class _Container, class _Iter>
+ _LIBCPP_HIDE_FROM_ABI auto __safe_erase(_Container& __container, _Iter&& __iter) {
+ // [container.reqmts] No erase(), clear(), pop_back() or pop_front() function throws an exception,
+ // except that there are exceptional cases
+
+ // http://eel.is/c++draft/deque.modifiers#5
+ // http://eel.is/c++draft/vector.modifiers#4
+
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ return __container.erase(__iter);
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (const exception& __ex) {
+ // The container might be in some unknown state and we can't get flat_map into consistent state
+ // because we have two containers. The only possible solution is to clear them out
+ clear();
+ throw flat_map_restore_error(
+ std::string("flat_map: Erasing on the underlying container has failed. "
+ "Unable to restore flat_map to previous state. Clear out the containers to make the two "
+ "containers consistent. Reason: ") +
+ __ex.what());
+ } catch (...) {
+ // The container might be in some unknown state and we can't get flat_map into consistent state
+ // because we have two containers. The only possible solution is to clear them out
+ clear();
+ throw flat_map_restore_error(
+ "flat_map: Erasing on the underlying container has failed. "
+ "Unable to restore flat_map to previous state. Clear out the containers to make the two "
+ "containers consistent.");
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+
+ template <class _IterK, class _IterM, class _KeyArg, class... _MArgs>
+ _LIBCPP_HIDE_FROM_ABI iterator
+ __try_emplace_impl(_IterK&& __it_key, _IterM&& __it_mapped, _KeyArg&& __key, _MArgs&&... __mapped_args) {
+ auto __key_it = __safe_emplace(__containers_.keys, __it_key, std::forward<_KeyArg>(__key));
+
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ auto __mapped_it = __safe_emplace(__containers_.values, __it_mapped, std::forward<_MArgs>(__mapped_args)...);
+ return iterator(std::move(__key_it), std::move(__mapped_it));
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (const flat_map_restore_error&) {
+ // both containers already cleared out
+ throw;
+ } catch (...) {
+ // If the second emplace throws and it has no effects on `values`, we need to erase the emplaced key.
+ __safe_erase(__containers_.keys, __key_it);
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+
+ template <class _Kp, class _Mapped, class... _Hint>
+ _LIBCPP_HIDE_FROM_ABI auto __insert_or_assign_impl(_Kp&& __key, _Mapped&& __mapped, _Hint&&... __hint) {
+ auto __r = try_emplace(__hint..., std::forward<_Kp>(__key), std::forward<_Mapped>(__mapped));
+ if (!__r.second) {
+ __r.first->second = std::forward<_Mapped>(__mapped);
+ }
+ return __r;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI void __reserve_impl(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 <class _KIter, class _MIter>
+ _LIBCPP_HIDE_FROM_ABI iterator __erase_impl(_KIter __k_iter, _MIter __m_iter) {
+ auto __key_iter = __safe_erase(__containers_.keys, __k_iter);
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ auto __mapped_iter = __safe_erase(__containers_.values, __m_iter);
+ return iterator(std::move(__key_iter), std::move(__mapped_iter));
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (const flat_map_restore_error&) {
+ // both containers already cleared out
+ throw;
+ } catch (...) {
+ // If the second erase throws, the first erase already happened. The flat_map is inconsistent.
+ clear();
+ throw flat_map_restore_error(
+ "flat_map::erase: Key has been erased but exception thrown on erasing mapped value. To make flat_map in "
+ "consistent state, clear out the flat_map");
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+
+ containers __containers_;
+ [[no_unique_address]] key_compare __compare_;
+
+ struct __key_equiv {
+ __key_equiv(key_compare __c) : __comp_(__c) {}
+ bool operator()(const_reference __x, const_reference __y) const {
+ return !__comp_(__x.first, __y.first) && !__comp_(__y.first, __x.first);
+ }
+ key_compare __comp_;
+ };
+};
+
+template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+flat_map(_KeyContainer, _MappedContainer, _Compare = _Compare())
+ -> flat_map<typename _KeyContainer::value_type,
+ typename _MappedContainer::value_type,
+ _Compare,
+ _KeyContainer,
+ _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Allocator>
+flat_map(_KeyContainer, _MappedContainer, _Allocator)
+ -> flat_map<typename _KeyContainer::value_type,
+ typename _MappedContainer::value_type,
+ less<typename _KeyContainer::value_type>,
+ _KeyContainer,
+ _MappedContainer>;
+template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+flat_map(_KeyContainer, _MappedContainer, _Compare, _Allocator)
+ -> flat_map<typename _KeyContainer::value_type,
+ typename _MappedContainer::value_type,
+ _Compare,
+ _KeyContainer,
+ _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare = _Compare())
+ -> flat_map<typename _KeyContainer::value_type,
+ typename _MappedContainer::value_type,
+ _Compare,
+ _KeyContainer,
+ _MappedContainer>;
+
+template <class _KeyContainer, class _MappedContainer, class _Allocator>
+flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Allocator)
+ -> flat_map<typename _KeyContainer::value_type,
+ typename _MappedContainer::value_type,
+ less<typename _KeyContainer::value_type>,
+ _KeyContainer,
+ _MappedContainer>;
+template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare, _Allocator)
+ -> flat_map<typename _KeyContainer::value_type,
+ typename _MappedContainer::value_type,
+ _Compare,
+ _KeyContainer,
+ _MappedContainer>;
+
+template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+flat_map(_InputIterator, _InputIterator, _Compare = _Compare())
+ -> flat_map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
+
+template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+flat_map(sorted_unique_t, _InputIterator, _InputIterator, _Compare = _Compare())
+ -> flat_map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
+
+template <ranges::input_range _Range,
+ class _Compare = less<__iter_key_type<_Range>>,
+ class _Allocator = allocator<byte>>
+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>, __alloc_rebind<_Allocator, __range_key_type<_Range>>>,
+ vector<__range_mapped_type<_Range>, __alloc_rebind<_Allocator, __range_mapped_type<_Range>>>>;
+
+template <ranges::input_range _Range, class _Allocator>
+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>, __alloc_rebind<_Allocator, __range_key_type<_Range>>>,
+ vector<__range_mapped_type<_Range>, __alloc_rebind<_Allocator, __range_mapped_type<_Range>>>>;
+
+template <class _Key, class _Tp, class _Compare = less<_Key>>
+flat_map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>;
+
+template <class _Key, class _Tp, class _Compare = less<_Key>>
+flat_map(sorted_unique_t, initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>;
+
+template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Allocator>
+struct uses_allocator<flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>, _Allocator>
+ : bool_constant<uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator>> {};
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 23
+
+#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 0000000000000..0189a5ff1d568
--- /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/__memory/allocator_traits.h b/libcxx/include/__memory/allocator_traits.h
index ac564f0e6fa0c..30cb30630dd91 100644
--- a/libcxx/include/__memory/allocator_traits.h
+++ b/libcxx/include/__memory/allocator_traits.h
@@ -373,6 +373,9 @@ template <class _Traits, class _Tp>
using __rebind_alloc = typename _Traits::template rebind_alloc<_Tp>::other;
#endif
+template <class _Allocator, class _Tp>
+using __alloc_rebind = __rebind_alloc<allocator_traits<_Allocator>, _Tp>;
+
template <class _Alloc>
struct __check_valid_allocator : true_type {
using _Traits = std::allocator_traits<_Alloc>;
diff --git a/libcxx/include/flat_map b/libcxx/include/flat_map
new file mode 100644
index 0000000000000..7abda6ae5819d
--- /dev/null
+++ b/libcxx/include/flat_map
@@ -0,0 +1,72 @@
+// -*- 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 <flat_map> synopsis
+
+#include <compare> // see [compare.syn]
+#include <initializer_list> // see [initializer.list.syn]
+
+namespace std {
+ // [flat.map], class template flat_map
+ template<class Key, class T, class Compare = less<Key>,
+ class KeyContainer = vector<Key>, class MappedContainer = vector<T>>
+ class flat_map;
+
+ struct sorted_unique_t { explicit sorted_unique_t() = default; };
+ inline constexpr sorted_unique_t sorted_unique{};
+
+ template<class Key, class T, class Compare, class KeyContainer, class MappedContainer,
+ class Allocator>
+ struct uses_allocator<flat_map<Key, T, Compare, KeyContainer, MappedContainer>,
+ Allocator>;
+
+ // [flat.map.erasure], erasure for flat_map
+ template<class Key, class T, class Compare, class KeyContainer, class MappedContainer,
+ class Predicate>
+ typename flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type
+ erase_if(flat_map<Key, T, Compare, KeyContainer, MappedContainer>& c, Predicate pred);
+
+ // [flat.multimap], class template flat_multimap
+ template<class Key, class T, class Compare = less<Key>,
+ class KeyContainer = vector<Key>, class MappedContainer = vector<T>>
+ class flat_multimap;
+
+ struct sorted_equivalent_t { explicit sorted_equivalent_t() = default; };
+ inline constexpr sorted_equivalent_t sorted_equivalent{};
+
+ template<class Key, class T, class Compare, class KeyContainer, class MappedContainer,
+ class Allocator>
+ struct uses_allocator<flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>,
+ Allocator>;
+
+ // [flat.multimap.erasure], erasure for flat_multimap
+ template<class Key, class T, class Compare, class KeyContainer, class MappedContainer,
+ class Predicate>
+ typename flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>::size_type
+ erase_if(flat_multimap<Key, T, Compare, KeyContainer, MappedContainer>& 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 <version>
+
+// standard required includes
+#include <compare>
+#include <initializer_list>
+
+#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 9ffccf66ff094..e344c0c796c4c 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -79,6 +79,10 @@ module std_filesystem [system] {
header "filesystem"
export *
}
+module std_flat_map [system] {
+ head "flat_map"
+ export *
+}
module std_format [system] {
header "format"
export *
@@ -1276,6 +1280,9 @@ module std_private_filesystem_recursive_directory_iterator [system] {
module std_private_filesystem_space_info [system] { header "__filesystem/space_info.h" }
module std_private_filesystem_u8path [system] { header "__filesystem/u8path.h" }
+module std_private_flat_map_flat_map [system] { header "__flat_map/flat_map.h" }
+module std_private_flat_map_sorted_unique [system] { header "__flat_map/sorted_unique.h" }
+
module std_private_format_buffer [system] { header "__format/buffer.h" }
module std_private_format_concepts [system] { header "__format/concepts.h" }
module std_private_format_container_adaptor [system] { header "__format/container_adaptor.h" }
>From 7524429224449c7fa1eb674bfadd965427449b6e Mon Sep 17 00:00:00 2001
From: Hui <hui.xie0621 at gmail.com>
Date: Wed, 17 Jul 2024 12:22:03 +0100
Subject: [PATCH 2/2] tests
---
libcxx/include/__flat_map/flat_map.h | 144 ++++--
.../flat.map/container_stability.pass.cpp | 69 +++
.../container.adaptors/NaiveStaticVector.h | 90 ++++
.../flat.map/clear.pass.cpp | 56 ++
.../container.adaptors/flat.map/comp.pass.cpp | 96 ++++
.../flat.map/contains.pass.cpp | 74 +++
.../flat.map/contains_transparent.pass.cpp | 53 ++
.../flat.map/count.pass.cpp | 73 +++
.../flat.map/count_transparent.pass.cpp | 53 ++
.../flat.map/empty.pass.cpp | 63 +++
.../flat.map/empty.verify.cpp | 25 +
.../flat.map/equal_range.pass.cpp | 85 +++
.../flat.map/equal_range_transparent.pass.cpp | 57 ++
.../flat.map/erase_key.pass.cpp | 114 ++++
.../flat.map/erase_key_transparent.pass.cpp | 119 +++++
.../container.adaptors/flat.map/find.pass.cpp | 78 +++
.../flat.map/find_transparent.pass.cpp | 56 ++
.../flat.map/flat.map.cons/alloc.pass.cpp | 50 ++
.../assign_initializer_list.pass.cpp | 65 +++
.../flat.map/flat.map.cons/compare.pass.cpp | 88 ++++
.../flat.map.cons/containers.pass.cpp | 228 ++++++++
.../flat.map.cons/containers_compare.pass.cpp | 246 +++++++++
.../flat.map/flat.map.cons/copy.pass.cpp | 91 ++++
.../flat.map.cons/copy_alloc.pass.cpp | 78 +++
.../copy_assign.addressof.compile.pass.cpp | 30 ++
.../flat.map.cons/copy_assign.pass.cpp | 110 ++++
.../flat.map/flat.map.cons/deduct.pass.cpp | 489 ++++++++++++++++++
.../flat.map/flat.map.cons/deduct.verify.cpp | 97 ++++
.../flat.map/flat.map.cons/default.pass.cpp | 72 +++
.../flat.map.cons/default_noexcept.pass.cpp | 58 +++
.../flat.map.cons/dtor_noexcept.pass.cpp | 54 ++
.../flat.map.cons/initializer_list.pass.cpp | 86 +++
.../initializer_list_compare.pass.cpp | 79 +++
.../flat.map/flat.map.cons/iter_iter.pass.cpp | 93 ++++
.../flat.map.cons/iter_iter_comp.pass.cpp | 103 ++++
.../iter_iter_stability.pass.cpp | 66 +++
.../flat.map/flat.map.cons/move.pass.cpp | 77 +++
.../flat.map.cons/move_alloc.pass.cpp | 84 +++
.../flat.map.cons/move_assign.pass.cpp | 108 ++++
.../flat.map.cons/move_assign_clears.pass.cpp | 84 +++
.../move_assign_noexcept.pass.cpp | 90 ++++
.../flat.map.cons/move_exceptions.pass.cpp | 76 +++
.../flat.map.cons/move_noexcept.pass.cpp | 110 ++++
.../flat.map.cons/sorted_container.pass.cpp | 129 +++++
.../sorted_iter_iter_comp.pass.cpp | 106 ++++
.../flat.map.erasure/erase_if.pass.cpp | 102 ++++
.../erase_if_exceptions.pass.cpp | 162 ++++++
.../flat.map/incomplete_type.pass.cpp | 34 ++
.../flat.map/insert_range.pass.cpp | 94 ++++
.../flat.map/insert_range_stability.pass.cpp | 54 ++
.../flat.map/insert_transparent.pass.cpp | 121 +++++
.../flat.map/iterator.pass.cpp | 98 ++++
.../flat.map/iterator_comparison.pass.cpp | 72 +++
...rator_concept_conformance.compile.pass.cpp | 142 +++++
.../flat.map/lower_bound.pass.cpp | 77 +++
.../flat.map/lower_bound_transparent.pass.cpp | 54 ++
.../flat.map/max_size.pass.cpp | 77 +++
.../flat.map/op_compare.pass.cpp | 97 ++++
...range_concept_conformance.compile.pass.cpp | 66 +++
.../flat.map/reverse_iterator.pass.cpp | 102 ++++
.../flat.map/types.pass.cpp | 86 +++
.../flat.map/upper_bound.pass.cpp | 77 +++
.../flat.map/upper_bound_transparent.pass.cpp | 54 ++
63 files changed, 5885 insertions(+), 36 deletions(-)
create mode 100644 libcxx/test/libcxx/containers/containers.adaptors/flat.map/container_stability.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/clear.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/comp.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/contains.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/contains_transparent.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/count.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/count_transparent.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/empty.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/empty.verify.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/equal_range.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/equal_range_transparent.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/erase_key.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/erase_key_transparent.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/find.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/find_transparent.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/alloc.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/assign_initializer_list.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/compare.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers_compare.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_alloc.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.addressof.compile.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.verify.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default_noexcept.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/dtor_noexcept.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list_compare.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_comp.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_stability.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_alloc.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_clears.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_noexcept.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_exceptions.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_noexcept.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_container.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_iter_iter_comp.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if_exceptions.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/incomplete_type.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/insert_range.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/insert_range_stability.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/insert_transparent.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/iterator.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/iterator_comparison.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/iterator_concept_conformance.compile.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/lower_bound.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/lower_bound_transparent.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/max_size.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/op_compare.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/range_concept_conformance.compile.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/reverse_iterator.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/types.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/upper_bound.pass.cpp
create mode 100644 libcxx/test/std/containers/container.adaptors/flat.map/upper_bound_transparent.pass.cpp
diff --git a/libcxx/include/__flat_map/flat_map.h b/libcxx/include/__flat_map/flat_map.h
index 25b0f5979de56..dc75ea6d64f8d 100644
--- a/libcxx/include/__flat_map/flat_map.h
+++ b/libcxx/include/__flat_map/flat_map.h
@@ -12,13 +12,16 @@
#include <__algorithm/ranges_equal.h>
#include <__algorithm/ranges_lexicographical_compare.h>
#include <__algorithm/ranges_lower_bound.h>
-#include <__algorithm/ranges_sort.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 <__config>
#include <__flat_map/sorted_unique.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/ranges_iterator_traits.h>
@@ -26,11 +29,20 @@
#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/ref_view.h>
+#include <__ranges/subrange.h>
#include <__ranges/zip_view.h>
+#include <__type_traits/conjunction.h>
+#include <__type_traits/invoke.h>
+#include <__type_traits/is_allocator.h>
+#include <__type_traits/is_nothrow_default_constructible.h>
+#include <__type_traits/maybe_const.h>
#include <__utility/pair.h>
#include <initializer_list>
#include <stdexcept>
+#include <string>
#include <vector>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -50,14 +62,21 @@ class flat_map {
template <bool _Const>
struct __iterator;
+ template <class, class, class, class, class>
+ friend class flat_map;
+
public:
// types
- using key_type = _Key;
- using mapped_type = _Tp;
- using value_type = pair<key_type, mapped_type>;
- using key_compare = _Compare;
- using reference = pair<const key_type&, mapped_type&>;
- using const_reference = pair<const key_type&, const mapped_type&>;
+ using key_type = _Key;
+ using mapped_type = _Tp;
+ using value_type = pair<key_type, mapped_type>;
+ using key_compare = _Compare;
+ // TODO : the following is the spec, but not implementable for vector<bool>
+ // using reference = pair<const key_type&, mapped_type&>;
+ // using const_reference = pair<const key_type&, const mapped_type&>;
+ using reference = pair<ranges::range_reference_t<const _KeyContainer>, ranges::range_reference_t<_MappedContainer>>;
+ using const_reference =
+ pair<ranges::range_reference_t<const _KeyContainer>, ranges::range_reference_t<const _MappedContainer>>;
using size_type = size_t;
using difference_type = ptrdiff_t;
using iterator = __iterator<false>; // see [container.requirements]
@@ -71,6 +90,7 @@ class flat_map {
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 {
@@ -95,7 +115,7 @@ class flat_map {
private:
using __key_iterator = ranges::iterator_t<__maybe_const<_Const, key_container_type>>;
using __mapped_iterator = ranges::iterator_t<__maybe_const<_Const, mapped_container_type>>;
- using __reference = conditional_t<_Const, flat_map::const_reference, flat_map::reference>;
+ using __reference = pair<iter_reference_t<__key_iterator>, iter_reference_t<__mapped_iterator>>;
struct __arrow_proxy {
__reference __ref_;
@@ -105,6 +125,8 @@ class flat_map {
__key_iterator __key_iter_;
__mapped_iterator __mapped_iter_;
+ friend flat_map;
+
public:
using iterator_concept = random_access_iterator_tag;
using iterator_category = input_iterator_tag;
@@ -170,7 +192,7 @@ class flat_map {
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 !(__y < __x); }
@@ -203,7 +225,10 @@ class flat_map {
public:
// [flat.map.cons], construct/copy/destroy
- _LIBCPP_HIDE_FROM_ABI flat_map() : flat_map(key_compare()) {}
+ _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_() {}
template <class _Allocator>
requires __allocator_ctor_constraint<_Allocator>
@@ -286,14 +311,14 @@ class flat_map {
_LIBCPP_HIDE_FROM_ABI explicit flat_map(const _Allocator& __alloc)
: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {}
- template <class _InputIterator>
+ template <input_iterator _InputIterator>
_LIBCPP_HIDE_FROM_ABI
flat_map(_InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())
: __containers_(), __compare_(__comp) {
insert(__first, __last);
}
- template <class _InputIterator, class _Allocator>
+ template <input_iterator _InputIterator, class _Allocator>
requires __allocator_ctor_constraint<_Allocator>
_LIBCPP_HIDE_FROM_ABI
flat_map(_InputIterator __first, _InputIterator __last, const key_compare& __comp, const _Allocator& __alloc)
@@ -301,7 +326,7 @@ class flat_map {
insert(__first, __last);
}
- template <class _InputIterator, class _Allocator>
+ template <input_iterator _InputIterator, class _Allocator>
requires __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) {
@@ -330,13 +355,13 @@ class flat_map {
insert_range(std::forward<_Range>(__rg));
}
- template <class _InputIterator>
+ template <input_iterator _InputIterator>
_LIBCPP_HIDE_FROM_ABI flat_map(
sorted_unique_t __s, _InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())
: __containers_(), __compare_(__comp) {
insert(__s, __first, __last);
}
- template <class _InputIterator, class _Allocator>
+ template <input_iterator _InputIterator, class _Allocator>
requires __allocator_ctor_constraint<_Allocator>
_LIBCPP_HIDE_FROM_ABI
flat_map(sorted_unique_t __s,
@@ -348,7 +373,7 @@ class flat_map {
insert(__s, __first, __last);
}
- template <class _InputIterator, class _Allocator>
+ template <input_iterator _InputIterator, class _Allocator>
requires __allocator_ctor_constraint<_Allocator>
_LIBCPP_HIDE_FROM_ABI
flat_map(sorted_unique_t __s, _InputIterator __first, _InputIterator __last, const _Allocator& __alloc)
@@ -480,14 +505,14 @@ class flat_map {
template <class... _Args>
requires is_constructible_v<pair<key_type, mapped_type>, _Args...>
_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> emplace(_Args&&... __args) {
- std::pair<key_type, value_type> __pair(std::forward<_Args>(__args)...);
+ std::pair<key_type, mapped_type> __pair(std::forward<_Args>(__args)...);
return __binary_search_emplace_impl(std::move(__pair));
}
template <class... _Args>
requires is_constructible_v<pair<key_type, mapped_type>, _Args...>
_LIBCPP_HIDE_FROM_ABI iterator emplace_hint(const_iterator __hint, _Args&&... __args) {
- std::pair<key_type, value_type> __pair(std::forward<_Args>(__args)...);
+ std::pair<key_type, mapped_type> __pair(std::forward<_Args>(__args)...);
if (__is_hint_correct(__hint, __pair.first)) {
if (__compare_(__pair.first, __hint->first)) {
return __emplace_impl(__hint, std::move(__pair));
@@ -525,22 +550,30 @@ class flat_map {
return emplace_hint(__hint, std::forward<_Pp>(__x));
}
- template <class _InputIterator>
+ template <input_iterator _InputIterator>
_LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last) {
- insert(ranges::subrange<_InputIterator>(std::move(__first), std::move(__last)));
+ if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {
+ __reserve_impl(__last - __first);
+ }
+
+ for (; __first != __last; ++__first) {
+ __binary_search_emplace_impl(value_type(*__first));
+ }
}
- template <class _InputIterator>
+ template <input_iterator _InputIterator>
void insert(sorted_unique_t, _InputIterator __first, _InputIterator __last) {
if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {
__reserve_impl(__last - __first);
}
- auto __it = begin();
- auto __end = end();
+ auto __it = begin();
while (__first != __last) {
value_type __pair(*__first);
- __it = ranges::lower_bound(__it, __end, __compare_, &containers::keys);
+ auto __end = end();
+ __it = ranges::lower_bound(__it, __end, __pair.first, __compare_, [](const auto& __p) -> decltype(auto) {
+ return std::get<0>(__p);
+ });
if (__it == __end || __compare_(__pair.first, __it->first)) {
__it = __emplace_impl(__it, std::move(__pair));
}
@@ -727,6 +760,7 @@ class flat_map {
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try {
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ using std::swap;
swap(__compare_, __y.__compare_);
swap(__containers_.keys, __y.__containers_.keys);
swap(__containers_.values, __y.__containers_.values);
@@ -847,11 +881,15 @@ class flat_map {
friend _LIBCPP_HIDE_FROM_ABI void swap(flat_map& __x, flat_map& __y) noexcept { __x.swap(__y); }
private:
- struct __ctor_uses_allocator_tag {};
- struct __ctor_uses_allocator_empty_tag {};
+ struct __ctor_uses_allocator_tag {
+ explicit __ctor_uses_allocator_tag() = default;
+ };
+ struct __ctor_uses_allocator_empty_tag {
+ explicit __ctor_uses_allocator_empty_tag() = default;
+ };
_LIBCPP_HIDE_FROM_ABI void __sort_and_unique() {
auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);
- ranges::sort(__zv, __compare_, &containers::keys);
+ ranges::stable_sort(__zv, __compare_, [](const auto& __p) -> decltype(auto) { return std::get<0>(__p); });
auto __it = ranges::unique(__zv, __key_equiv(__compare_)).begin();
auto __dist = ranges::distance(__zv.begin(), __it);
__containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end());
@@ -920,16 +958,16 @@ class flat_map {
template <class _Res, class _Fn, class _Self, class _Kp>
_LIBCPP_HIDE_FROM_ABI static _Res __binary_search_impl(_Fn __search_fn, _Self&& __self, _Kp& __x) {
- auto __key_iter = __search_fn(__self.__containers_.keys, __self.__compare_, __x);
+ auto __key_iter = __search_fn(__self.__containers_.keys, __x, __self.__compare_);
auto __mapped_iter =
__self.__containers_.values.begin() +
static_cast<ranges::range_difference_t<mapped_container_type>>(
- ranges::distance(__self.__containers_.values.begin(), __key_iter));
+ ranges::distance(__self.__containers_.keys.begin(), __key_iter));
return _Res(std::move(__key_iter), std::move(__mapped_iter));
}
- _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __binary_search_emplace_impl(std::pair<key_type, value_type>&& __pair) {
+ _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __binary_search_emplace_impl(std::pair<key_type, mapped_type>&& __pair) {
if (auto __it = lower_bound(__pair.first); __it == end() || __compare_(__pair.first, (*__it).first)) {
return pair<iterator, bool>(__emplace_impl(__it, std::move(__pair)), true);
} else {
@@ -938,8 +976,8 @@ class flat_map {
}
template <class _Iter>
- _LIBCPP_HIDE_FROM_ABI iterator __emplace_impl(_Iter&& __it, std::pair<key_type, value_type>&& __pair) {
- return __try_emplace_impl(__it.__key_iter, __it.__mapped_iter, std::move(__pair.first), std::move(__pair.second));
+ _LIBCPP_HIDE_FROM_ABI iterator __emplace_impl(_Iter&& __it, std::pair<key_type, mapped_type>&& __pair) {
+ return __try_emplace_impl(__it.__key_iter_, __it.__mapped_iter_, std::move(__pair.first), std::move(__pair.second));
}
template <class _KeyArg, class... _MArgs>
@@ -1002,7 +1040,8 @@ class flat_map {
};
template <class _Container, class _Iter, class... _Args>
- _LIBCPP_HIDE_FROM_ABI auto __safe_emplace(_Container& __container, _Iter&& __iter, _Args&&... __args) {
+ _LIBCPP_HIDE_FROM_ABI ranges::iterator_t<_Container>
+ __safe_emplace(_Container& __container, _Iter&& __iter, _Args&&... __args) {
if constexpr (!__failed_emplacement_has_side_effects<_Container>()) {
// just let the exception be thrown as the container is still in its original state on exception
return __container.emplace(__iter, std::forward<_Args>(__args)...);
@@ -1010,7 +1049,6 @@ class flat_map {
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
try {
# endif // _LIBCPP_HAS_NO_EXCEPTIONS
- return __container.emplace(__iter, std::forward<_Args>(__args)...);
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
} catch (const exception& __ex) {
// The container might be in some unknown state and we can't get flat_map into consistent state
@@ -1143,6 +1181,11 @@ class flat_map {
};
template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+ requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
+ !__is_allocator<_MappedContainer>::value &&
+ is_invocable_v<const _Compare&,
+ const typename _KeyContainer::value_type&,
+ const typename _KeyContainer::value_type&>)
flat_map(_KeyContainer, _MappedContainer, _Compare = _Compare())
-> flat_map<typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
@@ -1151,13 +1194,22 @@ flat_map(_KeyContainer, _MappedContainer, _Compare = _Compare())
_MappedContainer>;
template <class _KeyContainer, class _MappedContainer, class _Allocator>
+ 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<typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
less<typename _KeyContainer::value_type>,
_KeyContainer,
_MappedContainer>;
+
template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+ 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<const _Compare&,
+ const typename _KeyContainer::value_type&,
+ const typename _KeyContainer::value_type&>)
flat_map(_KeyContainer, _MappedContainer, _Compare, _Allocator)
-> flat_map<typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
@@ -1166,6 +1218,11 @@ flat_map(_KeyContainer, _MappedContainer, _Compare, _Allocator)
_MappedContainer>;
template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>
+ requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&
+ !__is_allocator<_MappedContainer>::value &&
+ is_invocable_v<const _Compare&,
+ const typename _KeyContainer::value_type&,
+ const typename _KeyContainer::value_type&>)
flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare = _Compare())
-> flat_map<typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
@@ -1174,13 +1231,22 @@ flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare = _Compare()
_MappedContainer>;
template <class _KeyContainer, class _MappedContainer, class _Allocator>
+ 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<typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
less<typename _KeyContainer::value_type>,
_KeyContainer,
_MappedContainer>;
+
template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>
+ 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<const _Compare&,
+ const typename _KeyContainer::value_type&,
+ const typename _KeyContainer::value_type&>)
flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare, _Allocator)
-> flat_map<typename _KeyContainer::value_type,
typename _MappedContainer::value_type,
@@ -1188,17 +1254,20 @@ flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare, _Allocator)
_KeyContainer,
_MappedContainer>;
-template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+template <input_iterator _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+ requires(!__is_allocator<_Compare>::value)
flat_map(_InputIterator, _InputIterator, _Compare = _Compare())
-> flat_map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;
-template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+template <input_iterator _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>
+ requires(!__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 <ranges::input_range _Range,
class _Compare = less<__iter_key_type<_Range>>,
class _Allocator = allocator<byte>>
+ requires(!__is_allocator<_Compare>::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>,
@@ -1207,6 +1276,7 @@ flat_map(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator(
vector<__range_mapped_type<_Range>, __alloc_rebind<_Allocator, __range_mapped_type<_Range>>>>;
template <ranges::input_range _Range, class _Allocator>
+ requires __is_allocator<_Allocator>::value
flat_map(from_range_t, _Range&&, _Allocator)
-> flat_map<__range_key_type<_Range>,
__range_mapped_type<_Range>,
@@ -1215,9 +1285,11 @@ flat_map(from_range_t, _Range&&, _Allocator)
vector<__range_mapped_type<_Range>, __alloc_rebind<_Allocator, __range_mapped_type<_Range>>>>;
template <class _Key, class _Tp, class _Compare = less<_Key>>
+ requires(!__is_allocator<_Compare>::value)
flat_map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>;
template <class _Key, class _Tp, class _Compare = less<_Key>>
+ requires(!__is_allocator<_Compare>::value)
flat_map(sorted_unique_t, initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>;
template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Allocator>
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 0000000000000..a76d78ac816ea
--- /dev/null
+++ b/libcxx/test/libcxx/containers/containers.adaptors/flat.map/container_stability.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
+
+// <flat_map>
+
+// 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 <algorithm>
+#include <cassert>
+#include <cstdint>
+#include <flat_map>
+#include <random>
+#include <map>
+#include <vector>
+
+#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<uint16_t> values;
+ std::vector<std::pair<uint16_t, uint16_t>> pairs;
+ for (int i=0; i < 200; ++i) {
+ uint16_t r = randomness();
+ values.push_back(r);
+ pairs.emplace_back(r, r);
+ }
+
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs.begin(), pairs.end());
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(values, values);
+ assert(fm.size() == m.size());
+ LIBCPP_ASSERT(std::ranges::equal(fm, m));
+ }
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs.begin(), pairs.end());
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(values, values, Mod256());
+ assert(fm.size() == m.size());
+ LIBCPP_ASSERT(std::ranges::equal(fm, m));
+ }
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs.begin(), pairs.end());
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(values, values, std::allocator<int>());
+ assert(fm.size() == m.size());
+ LIBCPP_ASSERT(std::ranges::equal(fm, m));
+ }
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs.begin(), pairs.end());
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(values, values, Mod256(), std::allocator<int>());
+ assert(fm.size() == m.size());
+ LIBCPP_ASSERT(std::ranges::equal(fm, m));
+ }
+ return 0;
+}
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 0000000000000..1f5d24c0e4eab
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/NaiveStaticVector.h
@@ -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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_NAIVE_STATIC_VECTOR_H
+#define SUPPORT_NAIVE_STATIC_VECTOR_H
+
+#include "test_iterators.h"
+#include "test_macros.h"
+
+template<class T, size_t N>
+struct NaiveStaticVector {
+ struct CapacityError {};
+
+ using value_type = T;
+ using difference_type = short;
+ using size_type = unsigned short;
+ using iterator = random_access_iterator<T*>;
+ using const_iterator = random_access_iterator<const T*>;
+
+ explicit NaiveStaticVector() = default;
+ template<class It> 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<MoveOnly>
+ // 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<class It>
+ 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 <class... Args>
+ iterator emplace(const_iterator pos, Args&&... args) {
+ return insert(pos, T(std::forward<Args>(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];
+ size_t size_ = 0;
+};
+
+#endif // SUPPORT_NAIVE_STATIC_VECTOR_H
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/clear.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/clear.pass.cpp
new file mode 100644
index 0000000000000..ec0172347641d
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/clear.pass.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// class flat_map
+
+// void clear() noexcept;
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <vector>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, int>;
+ 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);
+ }
+ {
+ using M = std::flat_map<int, int, std::greater<int>, std::deque<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>;
+ 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);
+ }
+ {
+ using M = std::flat_map<bool, bool>;
+ M m = {{true,false}, {false,true}};
+ assert(m.size() == 2);
+ ASSERT_NOEXCEPT(m.clear());
+ ASSERT_SAME_TYPE(decltype(m.clear()), void);
+ m.clear();
+ assert(m.size() == 0);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/comp.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/comp.pass.cpp
new file mode 100644
index 0000000000000..c1cf9307ce498
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/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
+
+// <flat_map>
+
+// key_compare key_comp() const;
+// value_compare value_comp() const;
+
+#include <cassert>
+#include <flat_map>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#include "test_macros.h"
+
+int main(int, char**) {
+ {
+ using M = std::flat_map<int, char>;
+ using Comp = std::less<int>; // the default
+ M m = {};
+ ASSERT_SAME_TYPE(M::key_compare, Comp);
+ static_assert(!std::is_same_v<M::value_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();
+ 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<bool(int, int)>;
+ using M = std::flat_map<int, int, Comp>;
+ Comp comp = std::greater<int>();
+ 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<int, int, Comp>;
+ 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<bool(const std::vector<int>&, const std::vector<int>&)>;
+ using M = std::flat_map<std::vector<int>, 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<int>{2,1,4}, 42);
+ auto b = std::make_pair(std::vector<int>{1,2,3}, 42);
+ auto c = std::make_pair(std::vector<int>{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/contains.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/contains.pass.cpp
new file mode 100644
index 0000000000000..5f1def4304cf1
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/contains.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>
+
+// bool contains(const key_type& x) const;
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, const char*>;
+ M m = {{1,""}, {2,""}, {4,""}, {5,""}, {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<int, int, std::greater<int>, std::deque<int, min_allocator<int>>>;
+ 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));
+ }
+ {
+ using M = std::flat_map<bool, int>;
+ M m = {{true,1}, {false,2}};
+ assert( m.contains(true));
+ assert( m.contains(false));
+ m = {{true,3}};
+ assert( m.contains(true));
+ assert(!m.contains(false));
+ m = {{false,4}};
+ assert(!std::as_const(m).contains(true));
+ assert( std::as_const(m).contains(false));
+ m.clear();
+ assert(!m.contains(true));
+ assert(!m.contains(false));
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/contains_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/contains_transparent.pass.cpp
new file mode 100644
index 0000000000000..0c4c9fcefad8a
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/contains_transparent.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>
+
+// template<class K> bool contains(const K& x) const;
+
+#include <cassert>
+#include <flat_map>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+
+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_; }
+ };
+private:
+ std::string lower_;
+ std::string upper_;
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<std::string, int, StartsWith::Less>;
+ M m = { {"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5} };
+ ASSERT_SAME_TYPE(decltype(m.contains(StartsWith('b'))), bool);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).contains(StartsWith('b'))), bool);
+ assert(m.contains("beta") == true);
+ assert(m.contains("delta") == false);
+ assert(m.contains("zeta") == false);
+ assert(m.contains(StartsWith('b')) == true);
+ assert(m.contains(StartsWith('d')) == false);
+ assert(m.contains(StartsWith('e')) == true);
+ assert(m.contains(StartsWith('z')) == false);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/count.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/count.pass.cpp
new file mode 100644
index 0000000000000..bca4251bdb36b
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/count.pass.cpp
@@ -0,0 +1,73 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// size_type count(const key_type& x) const;
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, const char*>;
+ M m = {{1,""}, {2,""}, {4,""}, {5,""}, {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<int, int, std::greater<int>, std::deque<int, min_allocator<int>>>;
+ 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);
+ }
+ {
+ using M = std::flat_map<bool, int>;
+ M m = {{true,1}, {false,2}};
+ ASSERT_SAME_TYPE(decltype(m.count(0)), size_t);
+ assert(m.count(true) == 1);
+ assert(m.count(false) == 1);
+ m = {{true,3}};
+ assert(m.count(true) == 1);
+ assert(m.count(false) == 0);
+ m = {{false,4}};
+ assert(std::as_const(m).count(true) == 0);
+ assert(std::as_const(m).count(false) == 1);
+ m.clear();
+ assert(m.count(true) == 0);
+ assert(m.count(false) == 0);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/count_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/count_transparent.pass.cpp
new file mode 100644
index 0000000000000..2ca6bd9ec15de
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/count_transparent.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>
+
+// template<class K> size_type count(const K& x) const;
+
+#include <cassert>
+#include <flat_map>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+
+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_; }
+ };
+private:
+ std::string lower_;
+ std::string upper_;
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<std::string, int, StartsWith::Less>;
+ M m = { {"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5} };
+ ASSERT_SAME_TYPE(decltype(m.count(StartsWith('b'))), M::size_type);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).count(StartsWith('b'))), M::size_type);
+ assert(m.count("beta") == 1);
+ assert(m.count("delta") == 0);
+ assert(m.count("zeta") == 0);
+ assert(m.count(StartsWith('b')) == 1);
+ assert(m.count(StartsWith('d')) == 0);
+ assert(m.count(StartsWith('e')) == 2);
+ assert(m.count(StartsWith('z')) == 0);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/empty.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/empty.pass.cpp
new file mode 100644
index 0000000000000..000b36daa9988
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/empty.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
+
+// <flat_map>
+
+// [[nodiscard]] bool empty() const noexcept;
+
+#include <flat_map>
+#include <cassert>
+#include <deque>
+#include <functional>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ typedef std::flat_map<int, int> M;
+ 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}};
+ assert(!m.empty());
+ m.clear();
+ assert(m.empty());
+ }
+ {
+ typedef std::flat_map<int, int, std::less<int>, std::deque<int, min_allocator<int>>> M;
+ 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}};
+ assert(!m.empty());
+ m.clear();
+ assert(m.empty());
+ }
+ {
+ typedef std::flat_map<bool, bool> M;
+ M m;
+ ASSERT_SAME_TYPE(decltype(m.empty()), bool);
+ ASSERT_NOEXCEPT(m.empty());
+ assert(m.empty());
+ assert(std::as_const(m).empty());
+ m = {{false, false}};
+ assert(!m.empty());
+ m.clear();
+ assert(m.empty());
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/empty.verify.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/empty.verify.cpp
new file mode 100644
index 0000000000000..8d98abf10ad64
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/empty.verify.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// [[nodiscard]] bool empty() const noexcept;
+
+#include <flat_map>
+
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ std::flat_map<int, int> 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/equal_range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/equal_range.pass.cpp
new file mode 100644
index 0000000000000..6d1c3fd4d38d7
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/equal_range.pass.cpp
@@ -0,0 +1,85 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// pair<iterator,iterator> equal_range(const key_type& k);
+// pair<const_iterator,const_iterator> equal_range(const key_type& k) const;
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char>;
+ using R = std::pair<M::iterator, M::iterator>;
+ using CR = std::pair<M::const_iterator, M::const_iterator>;
+ 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<int, char, std::greater<int>, std::deque<int, min_allocator<int>>, std::deque<char, min_allocator<char>>>;
+ using R = std::pair<M::iterator, M::iterator>;
+ using CR = std::pair<M::const_iterator, M::const_iterator>;
+ 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()));
+ }
+ {
+ using M = std::flat_map<bool, bool>;
+ using R = std::pair<M::iterator, M::iterator>;
+ using CR = std::pair<M::const_iterator, M::const_iterator>;
+ M m = {{true,false}, {false,true}};
+ ASSERT_SAME_TYPE(decltype(m.equal_range(0)), R);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).equal_range(0)), CR);
+ assert(m.equal_range(true) == std::pair(m.begin()+1, m.end()));
+ assert(m.equal_range(false) == std::pair(m.begin(), m.begin()+1));
+ m = {{true,true}};
+ assert(m.equal_range(true) == std::pair(m.begin(), m.end()));
+ assert(m.equal_range(false) == std::pair(m.begin(), m.begin()));
+ m = {{false,false}};
+ assert(std::as_const(m).equal_range(true) == std::pair(m.cend(), m.cend()));
+ assert(std::as_const(m).equal_range(false) == std::pair(m.cbegin(), m.cend()));
+ m.clear();
+ assert(m.equal_range(true) == std::pair(m.begin(), m.begin()));
+ assert(m.equal_range(false) == std::pair(m.begin(), m.begin()));
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/equal_range_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/equal_range_transparent.pass.cpp
new file mode 100644
index 0000000000000..ee6bc7ef8868f
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/equal_range_transparent.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>
+
+// template<class K> pair<iterator,iterator> equal_range(const K& x);
+// template<class K> pair<const_iterator,const_iterator> equal_range(const K& x) const;
+
+#include <cassert>
+#include <flat_map>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+
+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_; }
+ };
+private:
+ std::string lower_;
+ std::string upper_;
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<std::string, int, StartsWith::Less>;
+ using R = std::pair<M::iterator, M::iterator>;
+ using CR = std::pair<M::const_iterator, M::const_iterator>;
+ M m = { {"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5} };
+ ASSERT_SAME_TYPE(decltype(m.equal_range(StartsWith('b'))), R);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).equal_range(StartsWith('b'))), CR);
+ auto begin = m.begin();
+ assert(m.equal_range("beta") == std::pair(begin+1, begin+2));
+ assert(m.equal_range("delta") == std::pair(begin+2, begin+2));
+ assert(m.equal_range("zeta") == std::pair(begin+5, begin+5));
+ assert(m.equal_range(StartsWith('b')) == std::pair(begin+1, begin+2));
+ assert(m.equal_range(StartsWith('d')) == std::pair(begin+2, begin+2));
+ assert(m.equal_range(StartsWith('e')) == std::pair(begin+2, begin+4));
+ assert(m.equal_range(StartsWith('z')) == std::pair(begin+5, begin+5));
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/erase_key.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/erase_key.pass.cpp
new file mode 100644
index 0000000000000..8b76882138921
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/erase_key.pass.cpp
@@ -0,0 +1,114 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// size_type erase(const key_type& k);
+
+#include <compare>
+#include <concepts>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char>;
+ auto make = [](std::initializer_list<int> 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)), 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());
+ }
+ {
+ using M = std::flat_map<int, int, std::greater<>, std::deque<int, min_allocator<int>>, std::deque<int>>;
+ auto make = [](std::initializer_list<int> il) {
+ M m;
+ for (int i : il) {
+ m.emplace(i, i);
+ }
+ return m;
+ };
+ M::key_container_type 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});
+ ASSERT_SAME_TYPE(decltype(m.erase(9)), 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());
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/erase_key_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/erase_key_transparent.pass.cpp
new file mode 100644
index 0000000000000..cad13d18f1fec
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/erase_key_transparent.pass.cpp
@@ -0,0 +1,119 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// size_type erase(K&& k);
+
+#include <compare>
+#include <concepts>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+template<class Key, class It>
+struct HeterogeneousKey {
+ explicit HeterogeneousKey(Key key, It it) : key_(key), it_(it) {}
+ operator It() && { return it_; }
+ auto operator<=>(Key key) const { return key_ <=> key; }
+ Key key_;
+ It it_;
+};
+
+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_; }
+ };
+private:
+ std::string lower_;
+ std::string upper_;
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<std::string, int, StartsWith::Less>;
+ M m = { {"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5} };
+ ASSERT_SAME_TYPE(decltype(m.erase(StartsWith('b'))), M::size_type);
+ M::size_type n = m.erase(StartsWith('e'));
+ assert(n == 2);
+ assert((m == M{ {"alpha", 1}, {"beta", 2}, {"gamma", 5} }));
+ n = m.erase(StartsWith('d'));
+ assert(n == 0);
+ assert((m == M{ {"alpha", 1}, {"beta", 2}, {"gamma", 5} }));
+ }
+ {
+ using M = std::flat_map<int, int, std::less<>>;
+ M m = {{1,1}, {2,2}, {3,3}, {4,4}};
+ ASSERT_SAME_TYPE(decltype(m.erase(9)), 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}}));
+ 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 M::key_type const_lvalue = 1;
+ n = m.erase(const_lvalue); // erase(const key_type&)
+ assert(n == 1);
+ assert((m == M{{4,4}}));
+ }
+ {
+ using M = std::flat_map<int, int, std::less<>, std::deque<int, min_allocator<int>>, std::deque<int>>;
+ M m = {{1,1}, {2,2}, {3,3}, {4,4}};
+ ASSERT_SAME_TYPE(decltype(m.erase(9)), 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}}));
+ 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 M::key_type const_lvalue = 1;
+ n = m.erase(const_lvalue); // erase(const key_type&)
+ assert(n == 1);
+ assert((m == M{{4,4}}));
+ }
+ {
+ // P2077's HeterogeneousKey example
+ using M = std::flat_map<int, int, std::less<>>;
+ M m = {{1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}, {8,8}};
+ auto h1 = HeterogeneousKey<int, M::iterator>(8, m.begin());
+ std::same_as<M::size_type> 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<M::iterator> 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<int, int, std::less<>>;
+ M m = {{1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}, {8,8}};
+ auto h1 = HeterogeneousKey<int, M::const_iterator>(8, m.begin());
+ std::same_as<M::size_type> 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<M::iterator> 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}}));
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/find.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/find.pass.cpp
new file mode 100644
index 0000000000000..4f3f4a832d557
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/find.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
+
+// <flat_map>
+
+// iterator find(const key_type& k);
+// const_iterator find(const key_type& k) const;
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char>;
+ M m = {{1,'a'}, {2,'b'}, {4,'d'}, {5,'e'}, {8,'h'}};
+ ASSERT_SAME_TYPE(decltype(m.find(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).find(0)), 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());
+ }
+ {
+ using M = std::flat_map<int, char, std::greater<int>, std::deque<int, min_allocator<int>>, std::string>;
+ M m = {{1,'a'}, {2,'b'}, {4,'d'}, {5,'e'}, {8,'h'}};
+ ASSERT_SAME_TYPE(decltype(m.find(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).find(0)), M::const_iterator);
+ assert(m.find(0) == m.end());
+ assert(m.find(1) == m.begin() + 4);
+ assert(m.find(2) == m.begin() + 3);
+ assert(m.find(3) == m.end());
+ assert(m.find(4) == m.begin() + 2);
+ assert(m.find(5) == m.begin() + 1);
+ assert(m.find(6) == m.end());
+ assert(m.find(7) == m.end());
+ assert(std::as_const(m).find(8) == m.begin());
+ assert(std::as_const(m).find(9) == m.end());
+ }
+ {
+ using M = std::flat_map<bool, bool>;
+ M m = {{true,false}, {false,true}};
+ ASSERT_SAME_TYPE(decltype(m.find(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).find(0)), M::const_iterator);
+ assert(m.find(true) == m.begin() + 1);
+ assert(m.find(false) == m.begin());
+ m = {{true,true}};
+ assert(m.find(true) == m.begin());
+ assert(m.find(false) == m.end());
+ m = {{false,false}};
+ assert(std::as_const(m).find(true) == m.end());
+ assert(std::as_const(m).find(false) == m.begin());
+ m.clear();
+ assert(m.find(true) == m.end());
+ assert(m.find(false) == m.end());
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/find_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/find_transparent.pass.cpp
new file mode 100644
index 0000000000000..860959ed4f01b
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/find_transparent.pass.cpp
@@ -0,0 +1,56 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<class K> iterator find(const K& x);
+// template<class K> const_iterator find(const K& x) const;
+
+#include <cassert>
+#include <flat_map>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+
+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_; }
+ };
+private:
+ std::string lower_;
+ std::string upper_;
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<std::string, int, StartsWith::Less>;
+ M m = { {"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5} };
+ ASSERT_SAME_TYPE(decltype(m.find(StartsWith('b'))), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).find(StartsWith('b'))), M::const_iterator);
+ assert(m.find("beta") == m.begin() + 1);
+ assert(m.find("delta") == m.end());
+ assert(m.find("zeta") == m.end());
+ assert(m.find(StartsWith('b')) == m.begin() + 1);
+ assert(m.find(StartsWith('d')) == m.end());
+ auto it = m.find(StartsWith('e'));
+ assert(m.begin() + 2 <= it && it <= m.begin() + 3); // either is acceptable
+ LIBCPP_ASSERT(it == m.begin() + 2); // return the earliest match
+ assert(m.find(StartsWith('z')) == m.end());
+ }
+ 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 0000000000000..cbfefa8a66b49
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/alloc.pass.cpp
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<class Allocator>
+// explicit flat_map(const Allocator& a);
+
+#include <cassert>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#include "test_macros.h"
+#include "test_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using A = test_allocator<short>;
+ using M = std::flat_map<int, long, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>;
+ 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);
+ }
+ {
+ using M = std::flat_map<int, short, std::less<int>, std::pmr::vector<int>, std::pmr::vector<short>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::polymorphic_allocator<int> 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);
+ }
+ 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 0000000000000..91e969b8e1000
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/assign_initializer_list.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
+
+// <flat_map>
+
+// flat_map& operator=(initializer_list<value_type> il);
+
+#include <algorithm>
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <ranges>
+#include <vector>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+
+int main(int, char**) {
+ {
+ using C = std::flat_map<int, int>;
+ C 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<int, int> 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));
+ }
+ {
+ using C = std::flat_map<int, int, std::less<>, std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>;
+ C m = {{1,1}, {2,1}, {3,1}, {4,1}, {5,1}, {6,1}, {7,1}, {8,1}, {9,1}, {10,1}};
+ assert(m.size() == 10);
+ m = {{1,1}, {3,2}, {4,3}, {5,4}, {6,5}, {5,6}, {2,7}};
+ std::pair<int, int> expected[] = {{1,1}, {2,7}, {3,2}, {4,3}, {5,4}, {6,5}};
+ assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
+ LIBCPP_ASSERT(std::ranges::equal(m, expected));
+ }
+ {
+ using C = std::flat_map<double, int, std::less<>, std::deque<double, min_allocator<double>>, std::vector<int, min_allocator<int>>>;
+ C m = {};
+ assert(m.size() == 0);
+ m = {{3,0}, {1,0}, {2,0}, {2,1}, {3,1}, {4,0}, {3,2}, {5,0}, {6,0}, {5,1}};
+ std::pair<double, int> 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));
+ }
+ {
+ using C = std::flat_map<double, double, std::less<>, std::deque<double>, std::deque<double>>;
+ C m = {{10,1}, {8,1}};
+ assert(m.size() == 2);
+ m = {{3,2}};
+ std::pair<double, double> expected[] = {{3,2}};
+ assert(std::ranges::equal(m, expected));
+ }
+
+ 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 0000000000000..445095742bbae
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/compare.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>
+
+// explicit flat_map(const key_compare& comp);
+// template <class Alloc>
+// flat_map(const key_compare& comp, const Alloc& a);
+
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using C = test_less<int>;
+ auto m = std::flat_map<int, char*, C>(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<int>;
+ static_assert(std::is_constructible_v<std::flat_map<int, char*, C>, C>);
+ static_assert(!std::is_convertible_v<C, std::flat_map<int, char*, C>>);
+
+ static_assert(std::is_constructible_v<std::flat_map<int, char*>, std::less<int>>);
+ static_assert(!std::is_convertible_v<std::less<int>, std::flat_map<int, char*>>);
+ }
+ {
+ using C = test_less<int>;
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<short>;
+ auto m = std::flat_map<int, short, C, std::vector<int, A1>, std::vector<short, A2>>(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));
+ }
+ {
+ using C = test_less<int>;
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<short>;
+ std::flat_map<int, short, C, std::deque<int, A1>, std::deque<short, A2>> m = { C(4), A1(5) }; // implicit ctor
+ 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));
+ }
+ {
+ using M = std::flat_map<int, int, std::function<bool(int, int)>, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ vm.emplace_back(std::greater<int>());
+ 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);
+ }
+ {
+ // If an allocator is given, it must be usable by both containers.
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, std::less<>, std::vector<int>, std::vector<int, A>>;
+ static_assert(std::is_constructible_v<M, std::less<>>);
+ static_assert(!std::is_constructible_v<M, std::less<>, std::allocator<int>>);
+ static_assert(!std::is_constructible_v<M, std::less<>, 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 0000000000000..0c8d414c9da7c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers.pass.cpp
@@ -0,0 +1,228 @@
+//===----------------------------------------------------------------------===//
+//
+// 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(key_container_type key_cont, mapped_container_type mapped_cont,
+// const key_compare& comp = key_compare());
+// template<class Allocator>
+// flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
+// const Allocator& a);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#include "min_allocator.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+struct P {
+ int first;
+ int second;
+ template<class T, class U>
+ bool operator==(const std::pair<T, U>& rhs) const {
+ return MoveOnly(first) == rhs.first && MoveOnly(second) == rhs.second;
+ }
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char>;
+ std::vector<int> ks = {1,1,1,2,2,3,2,3,3};
+ std::vector<char> vs = {1,2,3,4,5,6,7,8,9};
+ auto m = M(ks, vs);
+ assert((m.keys() == std::vector<int>{1,2,3}));
+ LIBCPP_ASSERT((m.values() == std::vector<char>{1,4,6}));
+ 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<int>{1,2,3}));
+ LIBCPP_ASSERT((m.values() == std::vector<char>{1,4,6}));
+ }
+ {
+ P expected[] = {{3,2}, {2,1}, {1,3}};
+ using Ks = std::deque<int, min_allocator<int>>;
+ using Vs = std::vector<MoveOnly, min_allocator<MoveOnly>>;
+ using M = std::flat_map<int, MoveOnly, std::greater<int>, 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<>()));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,1,1,2,2,3,2,3,3}, A(5));
+ auto vs = std::deque<int, A>({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));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,1,1,2,2,3,2,3,3}, A(5));
+ auto vs = std::deque<int, A>({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));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,1,1,2,2,3,2,3,3}, A(5));
+ auto vs = std::deque<int, A>({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));
+ }
+ {
+ using M = std::flat_map<int, int, std::less<int>, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks = {1,1,1,2,2,3,2,3,3};
+ std::pmr::vector<int> 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);
+ }
+ {
+ using M = std::flat_map<int, int, std::less<int>, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks = {1,1,1,2,2,3,2,3,3};
+ std::pmr::vector<int> 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(std::move(ks), std::move(vs));
+ LIBCPP_ASSERT(ks.size() == 9); // ks' size is unchanged, since it uses a different allocator
+ LIBCPP_ASSERT(vs.size() == 9); // vs' size is unchanged, since it uses a different allocator
+ 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);
+ }
+
+#if 0
+ // Test all combinations of lvalue and rvalue containers (LWG 3802).
+ {
+ int input[] = {1,1,1,2,2,3,2,3,3};
+ const P expected[] = {{1,1}, {2,2}, {3,3}};
+ {
+ using M = std::flat_map<int, MoveOnly, std::less<>, std::pmr::vector<int>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks(input, input + 9);
+ std::pmr::vector<MoveOnly> vs(input, input + 9);
+ vm.emplace_back(ks, std::move(vs)); // ill-formed before LWG 3802
+ assert(ks.size() == 9); // ks' value is unchanged, since it was an lvalue above
+ LIBCPP_ASSERT(vs.size() == 9); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, int, std::less<>, std::pmr::vector<MoveOnly>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 9);
+ std::pmr::vector<int> vs(input, input + 9);
+ vm.emplace_back(std::move(ks), vs); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 9); // ks' size is unchanged, since it uses a different allocator
+ assert(vs.size() == 9); // vs' value is unchanged, since it was an lvalue above
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, MoveOnly, std::less<>, std::pmr::vector<MoveOnly>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 9);
+ std::pmr::vector<MoveOnly> vs(input, input + 9);
+ vm.emplace_back(std::move(ks), std::move(vs)); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 9); // ks' size is unchanged, since it uses a different allocator
+ LIBCPP_ASSERT(vs.size() == 9); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ }
+ {
+ int input[] = {1,2,3};
+ const P expected[] = {{1,1}, {2,2}, {3,3}};
+ {
+ using M = std::flat_map<int, MoveOnly, std::less<>, std::pmr::vector<int>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks(input, input + 3);
+ std::pmr::vector<MoveOnly> vs(input, input + 3);
+ vm.emplace_back(std::sorted_unique, ks, std::move(vs)); // ill-formed before LWG 3802
+ assert(ks.size() == 3); // ks' value is unchanged, since it was an lvalue above
+ LIBCPP_ASSERT(vs.size() == 3); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, int, std::less<>, std::pmr::vector<MoveOnly>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 3);
+ std::pmr::vector<int> vs(input, input + 3);
+ vm.emplace_back(std::sorted_unique, std::move(ks), vs); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 3); // ks' size is unchanged, since it uses a different allocator
+ assert(vs.size() == 3); // vs' value is unchanged, since it was an lvalue above
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, MoveOnly, std::less<>, std::pmr::vector<MoveOnly>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 3);
+ std::pmr::vector<MoveOnly> vs(input, input + 3);
+ vm.emplace_back(std::sorted_unique, std::move(ks), std::move(vs)); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 3); // ks' size is unchanged, since it uses a different allocator
+ LIBCPP_ASSERT(vs.size() == 3); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ }
+ #endif
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers_compare.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers_compare.pass.cpp
new file mode 100644
index 0000000000000..e26f0162802ea
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/containers_compare.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
+
+// <flat_map>
+
+// flat_map(key_container_type key_cont, mapped_container_type mapped_cont,
+// const key_compare& comp = key_compare());
+// template<class Allocator>
+// flat_map(const key_container_type& key_cont, const mapped_container_type& mapped_cont,
+// const Allocator& a);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#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<class T, class U>
+ bool operator==(const std::pair<T, U>& rhs) const {
+ return MoveOnly(first) == rhs.first && MoveOnly(second) == rhs.second;
+ }
+};
+
+int main(int, char**)
+{
+ using C = test_less<int>;
+ {
+ using M = std::flat_map<int, char, C>;
+ std::vector<int> ks = {1,1,1,2,2,3,2,3,3};
+ std::vector<char> vs = {1,2,3,4,5,6,7,8,9};
+ auto m = M(ks, vs, C(2));
+ assert((m.keys() == std::vector<int>{1,2,3}));
+ LIBCPP_ASSERT((m.values() == std::vector<char>{1,4,6}));
+ assert(m.key_comp() == C(2));
+ m = M(std::move(ks), std::move(vs), C(3));
+ assert(ks.empty()); // it was moved-from
+ assert(vs.empty()); // it was moved-from
+ assert((m.keys() == std::vector<int>{1,2,3}));
+ LIBCPP_ASSERT((m.values() == std::vector<char>{1,4,6}));
+ assert(m.key_comp() == C(3));
+ }
+ {
+ P expected[] = {{1,3}, {2,1}, {3,2}};
+ using Ks = std::deque<int, min_allocator<int>>;
+ using Vs = std::vector<MoveOnly, min_allocator<MoveOnly>>;
+ using M = std::flat_map<int, MoveOnly, C, 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), C(2));
+ assert(ks.empty()); // it was moved-from
+ assert(vs.empty()); // it was moved-from
+ assert(std::ranges::equal(m, expected, std::equal_to<>()));
+ assert(m.key_comp() == C(2));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, C, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,1,1,2,2,3,2,3,3}, A(5));
+ auto vs = std::deque<int, A>({1,1,1,2,2,3,2,3,3}, A(6));
+ auto m = M(std::move(ks), std::move(vs), C(2));
+ 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.key_comp() == C(2));
+ assert(m.keys().get_allocator() == A(5));
+ assert(m.values().get_allocator() == A(6));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, C, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,1,1,2,2,3,2,3,3}, A(5));
+ auto vs = std::deque<int, A>({1,1,1,2,2,3,2,3,3}, A(6));
+ auto m = M(ks, vs, C(2), 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.key_comp() == C(2));
+ assert(m.keys().get_allocator() == A(4));
+ assert(m.values().get_allocator() == A(4));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, C, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,1,1,2,2,3,2,3,3}, A(5));
+ auto vs = std::deque<int, A>({1,1,1,2,2,3,2,3,3}, A(6));
+ M m = { ks, vs, C(2), 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.key_comp() == C(2));
+ assert(m.keys().get_allocator() == A(4));
+ assert(m.values().get_allocator() == A(4));
+ }
+ {
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks = {1,1,1,2,2,3,2,3,3};
+ std::pmr::vector<int> 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, C(2));
+ 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].key_comp() == C(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks = {1,1,1,2,2,3,2,3,3};
+ std::pmr::vector<int> 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(std::move(ks), std::move(vs), C(2));
+ LIBCPP_ASSERT(ks.size() == 9); // ks' size is unchanged, since it uses a different allocator
+ LIBCPP_ASSERT(vs.size() == 9); // vs' size is unchanged, since it uses a different allocator
+ assert((vm[0] == M{{1,1}, {2,2}, {3,3}}));
+ assert(vm[0].key_comp() == C(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+
+#if 0
+ // Test all combinations of lvalue and rvalue containers (LWG 3802).
+ {
+ using C2 = test_less<MoveOnly>;
+ int input[] = {1,1,1,2,2,3,2,3,3};
+ const P expected[] = {{1,1}, {2,2}, {3,3}};
+ {
+ using M = std::flat_map<int, MoveOnly, C, std::pmr::vector<int>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks(input, input + 9);
+ std::pmr::vector<MoveOnly> vs(input, input + 9);
+ vm.emplace_back(ks, std::move(vs), C(2)); // ill-formed before LWG 3802
+ assert(ks.size() == 9); // ks' value is unchanged, since it was an lvalue above
+ LIBCPP_ASSERT(vs.size() == 9); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].key_comp() == C(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, int, C2, std::pmr::vector<MoveOnly>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 9);
+ std::pmr::vector<int> vs(input, input + 9);
+ vm.emplace_back(std::move(ks), vs, C2(2)); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 9); // ks' size is unchanged, since it uses a different allocator
+ assert(vs.size() == 9); // vs' value is unchanged, since it was an lvalue above
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].key_comp() == C2(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, MoveOnly, C2, std::pmr::vector<MoveOnly>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 9);
+ std::pmr::vector<MoveOnly> vs(input, input + 9);
+ vm.emplace_back(std::move(ks), std::move(vs), C2(2)); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 9); // ks' size is unchanged, since it uses a different allocator
+ LIBCPP_ASSERT(vs.size() == 9); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].key_comp() == C2(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ }
+ {
+ using C2 = test_less<MoveOnly>;
+ int input[] = {1,2,3};
+ const P expected[] = {{1,1}, {2,2}, {3,3}};
+ {
+ using M = std::flat_map<int, MoveOnly, C, std::pmr::vector<int>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks(input, input + 3);
+ std::pmr::vector<MoveOnly> vs(input, input + 3);
+ vm.emplace_back(std::sorted_unique, ks, std::move(vs), C(2)); // ill-formed before LWG 3802
+ assert(ks.size() == 3); // ks' value is unchanged, since it was an lvalue above
+ LIBCPP_ASSERT(vs.size() == 3); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].key_comp() == C(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, int, C2, std::pmr::vector<MoveOnly>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 3);
+ std::pmr::vector<int> vs(input, input + 3);
+ vm.emplace_back(std::sorted_unique, std::move(ks), vs, C2(2)); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 3); // ks' size is unchanged, since it uses a different allocator
+ assert(vs.size() == 3); // vs' value is unchanged, since it was an lvalue above
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].key_comp() == C2(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using M = std::flat_map<MoveOnly, MoveOnly, C2, std::pmr::vector<MoveOnly>, std::pmr::vector<MoveOnly>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<MoveOnly> ks(input, input + 3);
+ std::pmr::vector<MoveOnly> vs(input, input + 3);
+ vm.emplace_back(std::sorted_unique, std::move(ks), std::move(vs), C2(2)); // ill-formed before LWG 3802
+ LIBCPP_ASSERT(ks.size() == 3); // ks' size is unchanged, since it uses a different allocator
+ LIBCPP_ASSERT(vs.size() == 3); // vs' size is unchanged, since it uses a different allocator
+ assert(std::ranges::equal(vm[0], expected, std::equal_to<>()));
+ assert(vm[0].key_comp() == C2(2));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ }
+ #endif
+ 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 0000000000000..bc1d713abb74d
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy.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
+
+// <flat_map>
+
+// flat_map(const flat_map& m);
+
+#include <cassert>
+#include <flat_map>
+#include <memory_resource>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using C = test_less<int>;
+ std::vector<int, test_allocator<int>> ks({1, 3, 5}, test_allocator<int>(6));
+ std::vector<char, test_allocator<char>> vs({2, 2, 1}, test_allocator<char>(7));
+ using M = std::flat_map<int, char, C, decltype(ks), decltype(vs)>;
+ 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<int>(6));
+ assert(m.values().get_allocator() == test_allocator<char>(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<int>(6));
+ assert(mo.values().get_allocator() == test_allocator<char>(7));
+ }
+ {
+ using C = test_less<int>;
+ using Ks = std::vector<int, other_allocator<int>>;
+ using Vs = std::vector<char, other_allocator<char>>;
+ auto ks = Ks({1, 3, 5}, other_allocator<int>(6));
+ auto vs = Vs({2, 2, 1}, other_allocator<char>(7));
+ using M = std::flat_map<int, char, C, Ks, Vs>;
+ auto mo = M(Ks(ks, other_allocator<int>(6)), Vs(vs, other_allocator<int>(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<int>(-2));
+ assert(m.values().get_allocator() == other_allocator<char>(-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<int>(6));
+ assert(mo.values().get_allocator() == other_allocator<char>(7));
+ }
+ {
+ using C = test_less<int>;
+ std::pmr::monotonic_buffer_resource mr;
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::vector<int>>;
+ 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);
+ }
+ 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 0000000000000..a7ff983a2219c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_alloc.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
+
+// <flat_map>
+
+// flat_map(const flat_map&, const allocator_type&);
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using C = test_less<int>;
+ std::vector<int, test_allocator<int>> ks({1, 3, 5}, test_allocator<int>(6));
+ std::vector<char, test_allocator<char>> vs({2, 2, 1}, test_allocator<char>(7));
+ using M = std::flat_map<int, char, C, decltype(ks), decltype(vs)>;
+ auto mo = M(ks, vs, C(5));
+ auto m = M(mo, test_allocator<int>(3));
+
+ assert(m.key_comp() == C(5));
+ assert(m.keys() == ks);
+ assert(m.values() == vs);
+ assert(m.keys().get_allocator() == test_allocator<int>(3));
+ assert(m.values().get_allocator() == test_allocator<char>(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<int>(6));
+ assert(mo.values().get_allocator() == test_allocator<char>(7));
+ }
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::vector<int>>;
+ 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<int>{1,2,3}));
+ assert((m.values() == std::pmr::vector<int>{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<int>{1,2,3}));
+ assert((mo.values() == std::pmr::vector<int>{2,2,1}));
+ assert(mo.keys().get_allocator().resource() == &mr1);
+ assert(mo.values().get_allocator().resource() == &mr1);
+ }
+ {
+ using M = std::flat_map<int, int, std::less<>, std::pmr::vector<int>, std::pmr::deque<int>>;
+ std::pmr::vector<M> vs;
+ M m = {{1,2}, {2,2}, {3,1}};
+ vs.push_back(m);
+ assert(vs[0] == m);
+ }
+ 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 0000000000000..e9b752d5eb12b
--- /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>
+
+// flat_map& operator=(const flat_map& s);
+
+// Validate whether the container can be copy-assigned (move-assigned, swapped)
+// with an ADL-hijacking operator&
+
+#include <flat_map>
+#include <utility>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+ std::flat_map<operator_hijacker, operator_hijacker> so;
+ std::flat_map<operator_hijacker, operator_hijacker> 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 0000000000000..9b78fbd7a786c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/copy_assign.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>
+
+// flat_map& operator=(const flat_map& m);
+
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#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<int>;
+ std::vector<int, test_allocator<int>> ks({1, 3, 5}, test_allocator<int>(6));
+ std::vector<char, test_allocator<char>> vs({2, 2, 1}, test_allocator<char>(7));
+ using M = std::flat_map<int, char, C, decltype(ks), decltype(vs)>;
+ auto mo = M(ks, vs, C(5));
+ auto m = M({{3,3}, {4,4}, {5,5}}, C(3), test_allocator<int>(2));
+ m = mo;
+
+ assert(m.key_comp() == C(5));
+ assert(m.keys() == ks);
+ assert(m.values() == vs);
+ assert(m.keys().get_allocator() == test_allocator<int>(2));
+ assert(m.values().get_allocator() == test_allocator<char>(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<int>(6));
+ assert(mo.values().get_allocator() == test_allocator<char>(7));
+ }
+ {
+ // other_allocator is propagated
+ using C = test_less<int>;
+ using Ks = std::vector<int, other_allocator<int>>;
+ using Vs = std::vector<char, other_allocator<char>>;
+ auto ks = Ks({1, 3, 5}, other_allocator<int>(6));
+ auto vs = Vs({2, 2, 1}, other_allocator<char>(7));
+ using M = std::flat_map<int, char, C, Ks, Vs>;
+ auto mo = M(Ks(ks, other_allocator<int>(6)), Vs(vs, other_allocator<int>(7)), C(5));
+ auto m = M({{3,3}, {4,4}, {5,5}}, C(3), other_allocator<int>(2));
+ m = mo;
+
+ assert(m.key_comp() == C(5));
+ assert(m.keys() == ks);
+ assert(m.values() == vs);
+ assert(m.keys().get_allocator() == other_allocator<int>(6));
+ assert(m.values().get_allocator() == other_allocator<char>(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<int>(6));
+ assert(mo.values().get_allocator() == other_allocator<char>(7));
+ }
+ {
+ // pmr allocator is not propagated
+ using M = std::flat_map<int, int, std::less<>, std::pmr::deque<int>, std::pmr::vector<int>>;
+ 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);
+ }
+ {
+ // comparator is copied and invariant is preserved
+ using M = std::flat_map<int, int, std::function<bool(int, int)>>;
+ M mo = M({{1,2}, {3,4}}, std::less<int>());
+ M m = M({{1,2}, {3,4}}, std::greater<int>());
+ 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<int, int>;
+ M m = {{1,2}, {3,4}};
+ m = static_cast<const M&>(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 0000000000000..56558bdf2e5f5
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/deduct.pass.cpp
@@ -0,0 +1,489 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <algorithm>
+#include <cassert>
+#include <climits>
+#include <deque>
+#include <initializer_list>
+#include <list>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <ranges>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "deduction_guides_sfinae_checks.h"
+#include "test_allocator.h"
+
+using P = std::pair<int, long>;
+using PC = std::pair<const int, long>;
+
+void test_copy() {
+ {
+ std::flat_map<long, short> source = {{1,2}, {2,3}};
+ std::flat_map s(source);
+ ASSERT_SAME_TYPE(decltype(s), decltype(source));
+ assert(s == source);
+ }
+ {
+ std::flat_map<long, short, std::greater<long>> 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<long, short, std::greater<long>> source = {{1,2}, {2,3}};
+ std::flat_map s(source, std::allocator<int>());
+ ASSERT_SAME_TYPE(decltype(s), decltype(source));
+ assert(s == source);
+ }
+}
+
+void test_containers() {
+ std::deque<int, test_allocator<int>> ks({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 42));
+ std::deque<short, test_allocator<short>> vs({ 1, 2, 1, 4, 5 }, test_allocator<int>(0, 43));
+ std::deque<int, test_allocator<int>> sorted_ks({ 1, 2, 3, INT_MAX }, test_allocator<int>(0, 42));
+ std::deque<short, test_allocator<short>> sorted_vs({ 1, 2, 5, 4 }, test_allocator<int>(0, 43));
+ const std::pair<int, short> expected[] = {
+ {1, 1}, {2, 2}, {3, 5}, {INT_MAX, 4}
+ };
+ {
+ std::flat_map s(ks, vs);
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::less<int>, 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<int, short, std::less<int>, 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<long>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::less<int>, 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<long>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::less<int>, 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::pmr::monotonic_buffer_resource mr;
+ std::pmr::monotonic_buffer_resource mr2;
+ std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
+ std::pmr::deque<short> 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<int, short, std::less<int>, std::pmr::deque<int>, std::pmr::deque<short>>);
+ 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<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
+ std::pmr::deque<short> 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<int, short, std::less<int>, std::pmr::deque<int>, std::pmr::deque<short>>);
+ 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<int, test_allocator<int>> ks({ 1, 2, 1, INT_MAX, 3 }, test_allocator<int>(0, 42));
+ std::deque<short, test_allocator<short>> vs({ 1, 2, 1, 4, 5 }, test_allocator<int>(0, 43));
+ std::deque<int, test_allocator<int>> sorted_ks({ INT_MAX, 3, 2, 1 }, test_allocator<int>(0, 42));
+ std::deque<short, test_allocator<short>> sorted_vs({ 4, 5, 2, 1 }, test_allocator<int>(0, 43));
+ const std::pair<int, short> expected[] = {
+ {INT_MAX, 4}, {3, 5}, {2, 2}, {1, 1}
+ };
+ {
+ std::flat_map s(ks, vs, std::greater<int>());
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>, 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<int>());
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>, 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<int>(), test_allocator<long>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>, 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<int>(), test_allocator<long>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>, 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::pmr::monotonic_buffer_resource mr;
+ std::pmr::monotonic_buffer_resource mr2;
+ std::pmr::deque<int> pks(ks.begin(), ks.end(), &mr);
+ std::pmr::deque<short> pvs(vs.begin(), vs.end(), &mr);
+ std::flat_map s(std::move(pks), std::move(pvs), std::greater<int>(), &mr2);
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>, std::pmr::deque<int>, std::pmr::deque<short>>);
+ 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<int> pks(sorted_ks.begin(), sorted_ks.end(), &mr);
+ std::pmr::deque<short> pvs(sorted_vs.begin(), sorted_vs.end(), &mr);
+ std::flat_map s(std::sorted_unique, std::move(pks), std::move(pvs), std::greater<int>(), &mr2);
+
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>, std::pmr::deque<int>, std::pmr::deque<short>>);
+ assert(std::ranges::equal(s, expected));
+ assert(s.keys().get_allocator().resource() == &mr2);
+ assert(s.values().get_allocator().resource() == &mr2);
+ }
+}
+
+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<int, long>);
+ 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<int, long>);
+ 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<int, long>);
+ 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<int, long>);
+ assert(std::ranges::equal(m, sorted_arr));
+ }
+ #if 0
+ {
+ std::flat_map m(std::begin(arr), std::end(arr), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ {
+ std::flat_map m(std::begin(arrc), std::end(arrc), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ {
+ std::flat_map m(std::sorted_unique, std::begin(sorted_arr), std::end(sorted_arr), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ {
+ std::flat_map m(std::sorted_unique, std::begin(sorted_arrc), std::end(sorted_arrc), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ #endif
+ {
+ std::flat_map<int, short> mo;
+ std::flat_map m(mo.begin(), mo.end());
+ ASSERT_SAME_TYPE(decltype(m), decltype(mo));
+ }
+ {
+ std::flat_map<int, short> 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<long long>;
+ {
+ std::flat_map m(std::begin(arr), std::end(arr), C());
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, C>);
+ 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<int, long, C>);
+ 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<int, long, C>);
+ 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<int, long, C>);
+ assert(std::ranges::equal(m, sorted_arr));
+ }
+ #if 0
+ {
+ std::flat_map m(std::begin(arr), std::end(arr), C(), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, C, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ {
+ std::flat_map m(std::begin(arrc), std::end(arrc), C(), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, C, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ {
+ std::flat_map m(std::sorted_unique, std::begin(sorted_arr), std::end(sorted_arr), C(), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, C, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ {
+ std::flat_map m(std::sorted_unique, std::begin(sorted_arrc), std::end(sorted_arrc), C(), test_allocator<short>(0, 44));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, C, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 44);
+ assert(m.values().get_allocator().get_id() == 44);
+ }
+ #endif
+ {
+ std::flat_map<int, short> mo;
+ std::flat_map m(mo.begin(), mo.end(), C());
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, short, C>);
+ }
+ {
+ std::flat_map<int, short> mo;
+ std::flat_map m(mo.cbegin(), mo.cend(), C());
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, short, C>);
+ }
+}
+
+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<int, long>);
+ 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<int, long>);
+ assert(std::ranges::equal(m, sorted_arr));
+ }
+ #if 0
+ {
+ std::flat_map m({ std::pair{1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} }, test_allocator<long>(0, 42));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 42);
+ assert(m.values().get_allocator().get_id() == 42);
+ }
+ {
+ std::flat_map m(std::sorted_unique, { std::pair{1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} }, test_allocator<long>(0, 42));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 42);
+ assert(m.values().get_allocator().get_id() == 42);
+ }
+ #endif
+}
+
+void test_initializer_list_compare() {
+ const P sorted_arr[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} };
+ using C = std::greater<long long>;
+ {
+ 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<int, long, C>);
+ 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<int, long, C>);
+ assert(std::ranges::equal(m, sorted_arr));
+ }
+ #if 0
+ {
+ std::flat_map m({ std::pair{1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} }, C(), test_allocator<long>(0, 42));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, C, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 42);
+ assert(m.values().get_allocator().get_id() == 42);
+ }
+ {
+ std::flat_map m(std::sorted_unique, { std::pair{INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} }, C(), test_allocator<long>(0, 42));
+
+ ASSERT_SAME_TYPE(decltype(m), std::flat_map<int, long, C, std::vector<int, test_allocator<int>>, std::vector<long, test_allocator<long>>>);
+ assert(std::ranges::equal(m, sorted_arr));
+ assert(m.keys().get_allocator().get_id() == 42);
+ assert(m.values().get_allocator().get_id() == 42);
+ }
+ #endif
+}
+
+void test_from_range() {
+ std::list<std::pair<int, short>> r = { {1,1}, {2,2}, {1,1}, {INT_MAX,4}, {3,5} };
+ const std::pair<int, short> 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<int, short, std::less<int>>);
+ assert(std::ranges::equal(s, expected));
+ }
+ {
+ std::flat_map s(std::from_range, r, test_allocator<long>(0, 42));
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<short, test_allocator<short>>>);
+ 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<std::pair<int, short>> r = { {1,1}, {2,2}, {1,1}, {INT_MAX,4}, {3,5} };
+ const std::pair<int, short> expected[] = { {INT_MAX,4}, {3,5}, {2,2}, {1,1} };
+ {
+ std::flat_map s(std::from_range, r, std::greater<int>());
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>>);
+ assert(std::ranges::equal(s, expected));
+ }
+ {
+ std::flat_map s(std::from_range, r, std::greater<int>(), test_allocator<long>(0, 42));
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, short, std::greater<int>, std::vector<int, test_allocator<int>>, std::vector<short, test_allocator<short>>>);
+ 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, std::flat_map<int, short>>();
+
+ {
+ std::flat_map s = { std::make_pair(1, 'a') }; // flat_map(initializer_list<pair<int, char>>)
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<int, char>);
+ assert(s.size() == 1);
+ }
+ {
+ using M = std::flat_map<int, short>;
+ M m;
+ std::flat_map s = { std::make_pair(m, m) }; // flat_map(initializer_list<pair<M, M>>)
+ ASSERT_SAME_TYPE(decltype(s), std::flat_map<M, M>);
+ assert(s.size() == 1);
+ assert(s[m] == m);
+ }
+
+ {
+ std::pair<int, int> 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<int, int>);
+ assert(s.size() == 3);
+ }
+ {
+ std::pair<int, int> 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<int, int>);
+ assert(s.size() == 3);
+ }
+ {
+ std::pair<int, int> 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<decltype(s), std::flat_map<int, int>>);
+ 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 0000000000000..1213279a8e173
--- /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
+
+// <flat_map>
+
+// Test CTAD on cases where deduction should fail.
+
+#include <flat_map>
+#include <functional>
+#include <memory>
+#include <utility>
+#include <vector>
+
+struct NotAnAllocator {
+ friend bool operator<(NotAnAllocator, NotAnAllocator) { return false; }
+};
+
+using P = std::pair<int, long>;
+using PC = std::pair<const int, long>;
+
+void test() {
+ {
+ // cannot deduce Key and T from just (KeyContainer), even if it's a container of pairs
+ std::vector<std::pair<int, int>> v;
+ std::flat_map s(v);
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::flat_map'}}
+ }
+ {
+ // cannot deduce Key and T from just (KeyContainer, Allocator)
+ std::vector<int> v;
+ std::flat_map s(v, std::allocator<std::pair<const int, int>>());
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::flat_map'}}
+ }
+ {
+ // cannot deduce Key and T from nothing
+ std::flat_map m;
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::flat_map'}}
+ }
+ {
+ // cannot deduce Key and T from just (Compare)
+ std::flat_map m(std::less<int>{});
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::flat_map'}}
+ }
+ {
+ // cannot deduce Key and T from just (Compare, Allocator)
+ std::flat_map m(std::less<int>{}, std::allocator<PC>{});
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::flat_map'}}
+ }
+ {
+ // cannot deduce Key and T from just (Allocator)
+ std::flat_map m(std::allocator<PC>{});
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::flat_map'}}
+ }
+ {
+ // cannot convert from some arbitrary unrelated type
+ NotAnAllocator a;
+ std::flat_map m(a);
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::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 at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::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<int>());
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::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<int>(), std::allocator<PC>());
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::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<PC>());
+ // expected-error at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::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 at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::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 at -1{{no viable constructor or deduction guide for deduction of template arguments of 'std::flat_map'}}
+ }
+}
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 0000000000000..4d39c094f1376
--- /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>
+
+// flat_map();
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <type_traits>
+#include <vector>
+
+#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<int, char*> m;
+ assert(m.empty());
+ }
+ {
+ std::flat_map<int, char*> m = {};
+ assert(m.empty());
+ }
+ {
+ std::flat_map<int, char*, DefaultCtableComp, std::deque<int, min_allocator<int>>> m;
+ assert(m.empty());
+ assert(m.begin() == m.end());
+ assert(m.key_comp().default_constructed_);
+ }
+ {
+ using A1 = explicit_allocator<int>;
+ using A2 = explicit_allocator<char*>;
+ {
+ std::flat_map<int, char*, DefaultCtableComp, std::vector<int, A1>, std::vector<char*, A2>> m;
+ assert(m.empty());
+ assert(m.key_comp().default_constructed_);
+ }
+ {
+ A1 a1;
+ std::flat_map<int, int, DefaultCtableComp, std::vector<int, A1>, std::vector<int, A1>> 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<int>;
+ using M = std::flat_map<int, int, std::less<>, std::vector<int>, std::vector<int, A>>;
+ static_assert(std::is_constructible_v<M>);
+ static_assert(!std::is_constructible_v<M, std::allocator<int>>);
+ static_assert(!std::is_constructible_v<M, A>);
+ }
+ 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 0000000000000..2b920cd28c878
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/default_noexcept.pass.cpp
@@ -0,0 +1,58 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_default_constructible_v<key_container_type> &&
+// is_nothrow_default_constructible_v<mapped_container_type> &&
+// is_nothrow_default_constructible_v<key_compare>);
+
+// This tests a conforming extension
+
+#include <cassert>
+#include <flat_map>
+#include <functional>
+#include <vector>
+
+#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<MoveOnly, MoveOnly>;
+ static_assert(std::is_nothrow_default_constructible_v<C>);
+ }
+ {
+ using C = std::flat_map<MoveOnly, MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
+ static_assert(std::is_nothrow_default_constructible_v<C>);
+ }
+#endif // _LIBCPP_VERSION
+ {
+ using C = std::flat_map<MoveOnly, MoveOnly, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
+ static_assert(!std::is_nothrow_default_constructible_v<C>);
+ C c;
+ }
+ {
+ using C = std::flat_map<MoveOnly, MoveOnly, ThrowingCtorComp>;
+ static_assert(!std::is_nothrow_default_constructible_v<C>);
+ 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 0000000000000..43f9dcedfd34a
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/dtor_noexcept.pass.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <vector>
+
+#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<MoveOnly, MoveOnly>;
+ static_assert(std::is_nothrow_destructible_v<C>);
+ }
+ {
+ using V = std::vector<MoveOnly, test_allocator<MoveOnly>>;
+ using C = std::flat_map<MoveOnly, MoveOnly, std::less<MoveOnly>, V, V>;
+ static_assert(std::is_nothrow_destructible_v<C>);
+ }
+ {
+ using V = std::deque<MoveOnly, other_allocator<MoveOnly>>;
+ using C = std::flat_map<MoveOnly, MoveOnly, std::greater<MoveOnly>, V, V>;
+ static_assert(std::is_nothrow_destructible_v<C>);
+ }
+#if defined(_LIBCPP_VERSION)
+ {
+ using C = std::flat_map<MoveOnly, MoveOnly, ThrowingDtorComp>;
+ static_assert(!std::is_nothrow_destructible_v<C>);
+ }
+#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 0000000000000..8e62164be72d7
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list.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
+
+// <flat_map>
+
+// flat_map(initializer_list<value_type> il, const key_compare& comp = key_compare());
+// template<class Alloc> flat_map(initializer_list<value_type> il, const Alloc& a);
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "min_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::pair<int, short> expected[] = {{1,1}, {2,2}, {3,3}, {5,2}};
+ {
+ using M = std::flat_map<int, short>;
+ M m = {{5,2}, {2,2}, {2,2}, {3,3}, {1,1}, {3,3}};
+ assert(std::equal(m.begin(), m.end(), expected, expected+4));
+ }
+ {
+ using M = std::flat_map<int, short, std::greater<int>, std::deque<int, min_allocator<int>>>;
+ 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 M = std::flat_map<int, short>;
+ std::initializer_list<M::value_type> 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));
+ static_assert( std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>>);
+ static_assert( std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>, std::allocator<int>>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>, std::allocator<int>>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>, std::allocator<int>>);
+ }
+ {
+ using A = explicit_allocator<int>;
+ {
+ using M = std::flat_map<int, int, DefaultCtableComp, std::vector<int, A>, std::deque<int, A>>;
+ 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_);
+ }
+ {
+ using M = std::flat_map<int, int, std::greater<int>, std::deque<int, A>, std::vector<int, A>>;
+ 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));
+ }
+ }
+ {
+ using M = std::flat_map<int, int, std::less<int>, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::initializer_list<M::value_type> 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);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list_compare.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list_compare.pass.cpp
new file mode 100644
index 0000000000000..10f42816bcdbc
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/initializer_list_compare.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
+
+// <flat_map>
+
+// flat_map(initializer_list<value_type> il, const key_compare& comp = key_compare());
+// template<class Alloc> flat_map(initializer_list<value_type> il, const key_compare& comp, const Alloc& a);
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "../../../test_compare.h"
+
+int main(int, char**)
+{
+ std::pair<int, short> expected[] = {{1,1}, {2,2}, {3,3}, {5,2}};
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, short, C>;
+ 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));
+ }
+ {
+ // Sorting uses the comparator that was passed in
+ using M = std::flat_map<int, short, std::function<bool(int, int)>, std::deque<int, min_allocator<int>>>;
+ auto m = M({{5,2}, {2,2}, {2,2}, {3,3}, {1,1}, {3,3}}, std::greater<int>());
+ assert(std::equal(m.rbegin(), m.rend(), expected, expected+4));
+ assert(m.key_comp()(2, 1) == true);
+ }
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, short, C>;
+ std::initializer_list<M::value_type> il = {{5,2}, {2,2}, {2,2}, {3,3}, {1,1}, {3,3}};
+ auto m = M(il, C(10));
+ assert(std::equal(m.begin(), m.end(), expected, expected+4));
+ assert(m.key_comp() == C(10));
+ static_assert( std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>, C>);
+ static_assert( std::is_constructible_v<M, std::initializer_list<std::pair<int, short>>, C, std::allocator<int>>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>, C>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, short>>, C, std::allocator<int>>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>, C>);
+ static_assert(!std::is_constructible_v<M, std::initializer_list<std::pair<const int, const short>>, C, std::allocator<int>>);
+ }
+ {
+ using A = explicit_allocator<int>;
+ using M = std::flat_map<int, int, std::greater<int>, std::deque<int, A>, std::vector<int, A>>;
+ 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));
+ }
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::deque<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::initializer_list<M::value_type> 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));
+ }
+ 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 0000000000000..389d3906b5f97
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter.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
+
+// <flat_map>
+
+// template <class InputIterator>
+// flat_map(InputIterator first, InputIterator last, const key_compare& comp = key_compare());
+// template<class InputIterator, class Allocator>
+// flat_map(InputIterator first, InputIterator last, const Allocator& a);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ using P = std::pair<int, short>;
+ 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}};
+ {
+ using M = std::flat_map<int, short>;
+ auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9));
+ assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
+ LIBCPP_ASSERT(std::ranges::equal(m, expected));
+ }
+ {
+ using M = std::flat_map<int, short, std::greater<int>, std::deque<int, min_allocator<int>>, std::deque<short>>;
+ auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9));
+ assert((m.keys() == std::deque<int, min_allocator<int>>{3,2,1}));
+ LIBCPP_ASSERT((m.values() == std::deque<short>{6,4,1}));
+ }
+ {
+ // Test when the operands are of array type (also contiguous iterator type)
+ using M = std::flat_map<int, short, std::greater<int>, std::vector<int, min_allocator<int>>>;
+ auto m = M(ar, ar);
+ assert(m.empty());
+ }
+ {
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<short>;
+ using M = std::flat_map<int, short, std::less<int>, std::vector<int, A1>, std::deque<short, A2>>;
+ 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));
+ }
+ {
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<short>;
+ using M = std::flat_map<int, short, std::less<int>, std::vector<int, A1>, std::deque<short, A2>>;
+ 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));
+ }
+ {
+ using M = std::flat_map<int, short, std::less<int>, std::pmr::vector<int>, std::pmr::vector<short>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ vm.emplace_back(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(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<int, short, std::less<int>, std::pmr::vector<int>, std::pmr::vector<short>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> 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);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_comp.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_comp.pass.cpp
new file mode 100644
index 0000000000000..e2e264edd8e39
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_comp.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
+
+// <flat_map>
+
+// template <class InputIterator>
+// flat_map(InputIterator first, InputIterator last, const key_compare& comp = key_compare());
+// template<class InputIterator, class Allocator>
+// flat_map(InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+int main(int, char**)
+{
+ using P = std::pair<int, short>;
+ 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}};
+ {
+ using M = std::flat_map<int, short, std::function<bool(int,int)>>;
+ auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9), std::less<int>());
+ assert(std::ranges::equal(m.keys(), expected | std::views::elements<0>));
+ LIBCPP_ASSERT(std::ranges::equal(m, expected));
+ assert(m.key_comp()(1, 2) == true);
+ }
+ {
+ using M = std::flat_map<int, short, std::greater<int>, std::deque<int, min_allocator<int>>>;
+ auto m = M(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9), std::greater<int>());
+ assert(std::ranges::equal(m.keys(), expected | std::views::reverse | std::views::elements<0>));
+ LIBCPP_ASSERT(std::ranges::equal(m, expected | std::views::reverse));
+ }
+ {
+ // Test when the operands are of array type (also contiguous iterator type)
+ using C = test_less<int>;
+ using M = std::flat_map<int, short, C, std::vector<int, min_allocator<int>>>;
+ auto m = M(ar, ar, C(5));
+ assert(m.empty());
+ assert(m.key_comp() == C(5));
+ }
+ {
+ using C = test_less<int>;
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<short>;
+ using M = std::flat_map<int, short, C, std::vector<int, A1>, std::deque<short, A2>>;
+ 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));
+ }
+ {
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<short>;
+ using M = std::flat_map<int, short, std::less<int>, std::deque<int, A1>, std::vector<short, A2>>;
+ 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));
+ }
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, short, C, std::pmr::vector<int>, std::pmr::deque<short>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ vm.emplace_back(cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 9), C(3));
+ assert(std::ranges::equal(vm[0].keys(), expected | std::views::elements<0>));
+ LIBCPP_ASSERT(std::ranges::equal(vm[0], expected));
+ assert(vm[0].key_comp() == C(3));
+ assert(vm[0].keys().get_allocator().resource() == &mr);
+ assert(vm[0].values().get_allocator().resource() == &mr);
+ }
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, short, C, std::pmr::vector<int>, std::pmr::vector<short>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ vm.emplace_back(ar, ar, C(4));
+ assert(vm[0].empty());
+ 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/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 0000000000000..6eb19ce0e2d95
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/iter_iter_stability.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
+
+// <flat_map>
+
+// template<class InputIterator>
+// 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 <algorithm>
+#include <cassert>
+#include <cstdint>
+#include <flat_map>
+#include <random>
+#include <map>
+
+#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<uint16_t, uint16_t> pairs[200];
+ for (auto& pair : pairs) {
+ pair = { uint16_t(randomness()), uint16_t(randomness()) };
+ }
+
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs, pairs + 200);
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(pairs, pairs + 200);
+ assert(fm.size() == m.size());
+ LIBCPP_ASSERT(std::ranges::equal(fm, m));
+ }
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs, pairs + 200, std::allocator<int>());
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(pairs, pairs + 200, std::allocator<int>());
+ assert(fm.size() == m.size());
+ LIBCPP_ASSERT(std::ranges::equal(fm, m));
+ }
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs, pairs + 200, Mod256());
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(pairs, pairs + 200, Mod256());
+ assert(fm.size() == m.size());
+ LIBCPP_ASSERT(std::ranges::equal(fm, m));
+ }
+ {
+ std::map<uint16_t, uint16_t, Mod256> m(pairs, pairs + 200, Mod256(), std::allocator<int>());
+ std::flat_map<uint16_t, uint16_t, Mod256> fm(pairs, pairs + 200, Mod256(), std::allocator<int>());
+ 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 0000000000000..20f5c4268921e
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move.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
+
+// <flat_map>
+
+// flat_map(flat_map&&);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using C = test_less<int>;
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, C, std::vector<int, A>, std::deque<int, A>>;
+ 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() == A(test_alloc_base::moved_value));
+ assert(mo.values().get_allocator() == A(test_alloc_base::moved_value));
+ }
+ {
+ using C = test_less<int>;
+ using A = min_allocator<int>;
+ using M = std::flat_map<int, int, C, std::vector<int, A>, std::deque<int, A>>;
+ 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<int, int, std::function<bool(int,int)>>;
+ M mo = M({{1,1},{2,2},{3,1}}, std::less<int>());
+ 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);
+ }
+ 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 0000000000000..745680c063ee0
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_alloc.pass.cpp
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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(flat_map&&, const allocator_type&);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <ranges>
+#include <vector>
+
+#include "test_macros.h"
+#include "../../../test_compare.h"
+#include "test_allocator.h"
+
+int main(int, char**)
+{
+ {
+ std::pair<int, int> expected[] = {{1,1}, {2,2}, {3,1}};
+ using C = test_less<int>;
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, C, std::vector<int, A>, std::deque<int, A>>;
+ 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()));
+ #if 0
+ assert(mo.empty());
+ #endif
+ assert(mo.key_comp() == C(5));
+ assert(mo.keys().get_allocator() == A(7));
+ assert(mo.values().get_allocator() == A(7));
+ }
+ {
+ std::pair<int, int> expected[] = {{1,1}, {2,2}, {3,1}};
+ using C = test_less<int>;
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::deque<int>>;
+ 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);
+ }
+ {
+ using M = std::flat_map<int, int, std::less<>, std::pmr::deque<int>, std::pmr::vector<int>>;
+ std::pmr::vector<M> vs;
+ M m = {{1,1}, {3,1}, {1,1}, {2,2}};
+ vs.push_back(std::move(m));
+ assert((vs[0].keys() == std::pmr::deque<int>{1,2,3}));
+ assert((vs[0].values() == std::pmr::vector<int>{1,2,1}));
+ }
+ 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 0000000000000..bf0b0e71a3a82
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign.pass.cpp
@@ -0,0 +1,108 @@
+//===----------------------------------------------------------------------===//
+//
+// 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& operator=(flat_map&&);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <string>
+#include <utility>
+#include <vector>
+
+#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<int>;
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<char>;
+ using M = std::flat_map<int, char, C, std::vector<int, A1>, std::vector<char, A2>>;
+ 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<int>;
+ using A1 = other_allocator<int>;
+ using A2 = other_allocator<char>;
+ using M = std::flat_map<int, char, C, std::deque<int, A1>, std::deque<char, A2>>;
+ 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<int>;
+ using M = std::flat_map<int, int, std::greater<int>, std::vector<int, A>, std::vector<int, A>>;
+ 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());
+ }
+ {
+ // A moved-from flat_map maintains its class invariant in the presence of moved-from elements.
+ using M = std::flat_map<std::pmr::string, int, std::less<>, std::pmr::vector<std::pmr::string>, std::pmr::vector<int>>;
+ 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);
+ }
+ {
+ // A moved-from flat_map maintains its class invariant in the presence of moved-from comparators.
+ using C = std::function<bool(int,int)>;
+ using M = std::flat_map<int, int, C>;
+ M mo = M({{1,3},{2,2},{3,1}}, std::less<int>());
+ M m = M({{1,1},{2,2}}, std::greater<int>());
+ 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,3},{2,2},{3,1}}); // insert has no preconditions
+ LIBCPP_ASSERT(m == mo);
+ }
+ 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 0000000000000..95fcb1e5e0c27
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_clears.pass.cpp
@@ -0,0 +1,84 @@
+//===----------------------------------------------------------------------===//
+//
+// 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& operator=(flat_map&&);
+// Preserves the class invariant for the moved-from flat_map.
+
+#include <algorithm>
+#include <cassert>
+#include <compare>
+#include <flat_map>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#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<int, int> expected[] = { {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}, {8,8} };
+ using M = std::flat_map<MoveNegates, int, std::less<MoveNegates>, std::vector<MoveNegates>>;
+ 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<int, int> expected[] = { {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}, {8,8} };
+ using M = std::flat_map<MoveClears, int, std::less<MoveClears>, std::vector<MoveClears>>;
+ 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());
+ }
+ 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 0000000000000..5c9514089cb55
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_assign_noexcept.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
+
+// <flat_map>
+
+// flat_map& operator=(flat_map&& c)
+// noexcept(
+// is_nothrow_move_assignable<key_container_type>::value &&
+// is_nothrow_move_assignable<mapped_container_type>::value &&
+// is_nothrow_copy_assignable<key_compare>::value);
+
+// This tests a conforming extension
+
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <type_traits>
+#include <vector>
+
+#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;
+};
+
+int main(int, char**)
+{
+ {
+ using C = std::flat_map<int, int>;
+ LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ using C = std::flat_map<MoveOnly, int, std::less<MoveOnly>, std::vector<MoveOnly, test_allocator<MoveOnly>>, std::vector<int, test_allocator<int>>>;
+ static_assert(!std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ using C = std::flat_map<int, MoveOnly, std::less<int>, std::vector<int, test_allocator<int>>, std::vector<MoveOnly, test_allocator<MoveOnly>>>;
+ static_assert(!std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ using C = std::flat_map<MoveOnly, int, std::less<MoveOnly>, std::vector<MoveOnly, other_allocator<MoveOnly>>, std::vector<int, other_allocator<int>>>;
+ LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ using C = std::flat_map<int, MoveOnly, std::less<int>, std::vector<int, other_allocator<int>>, std::vector<MoveOnly, other_allocator<MoveOnly>>>;
+ LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ // Test with a comparator that throws on copy-assignment.
+ using C = std::flat_map<int, int, std::function<bool(int,int)>>;
+ LIBCPP_STATIC_ASSERT(!std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ // Test with a container that throws on move-assignment.
+ using C = std::flat_map<int, int, std::less<int>, std::pmr::vector<int>, std::vector<int>>;
+ static_assert(!std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ // Test with a container that throws on move-assignment.
+ using C = std::flat_map<int, int, std::less<int>, std::vector<int>, std::pmr::vector<int>>;
+ static_assert(!std::is_nothrow_move_assignable_v<C>);
+ }
+ {
+ // Moving the flat_map copies the comparator (to support std::function comparators)
+ using C = std::flat_map<int, int, MoveSensitiveComp>;
+ LIBCPP_STATIC_ASSERT(std::is_nothrow_move_assignable_v<C>);
+ C c;
+ assert(!c.key_comp().is_moved_from_);
+ C d;
+ d = std::move(c);
+ LIBCPP_ASSERT(!c.key_comp().is_moved_from_);
+ assert(!d.key_comp().is_moved_from_);
+ }
+ 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 0000000000000..2e7d5c95a08d1
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_exceptions.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
+// UNSUPPORTED: no-exceptions
+
+// <flat_map>
+
+// flat_map(flat_map&& s);
+// If any member function in [flat.map.defn] exits via an exception, the invariant is restored.
+
+#include <algorithm>
+#include <cassert>
+#include <flat_map>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#include "test_macros.h"
+//#include "MinSequenceContainer.h"
+
+static int countdown = 0;
+
+struct EvilContainer : std::vector<int> {
+ 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<int, int, std::less<int>, EvilContainer, std::vector<int>>;
+ 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.
+ assert(mo.keys().size() == mo.values().size());
+ assert(std::is_sorted(mo.begin(), mo.end()));
+ assert(std::adjacent_find(mo.keys().begin(), mo.keys().end()) == mo.keys().end());
+ LIBCPP_ASSERT(mo.empty());
+ }
+ {
+ using M = std::flat_map<int, int, std::less<int>, std::vector<int>, 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.
+ assert(mo.keys().size() == mo.values().size());
+ assert(std::is_sorted(mo.begin(), mo.end()));
+ assert(std::adjacent_find(mo.keys().begin(), mo.keys().end()) == mo.keys().end());
+ 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 0000000000000..ee7798b8738f1
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/move_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>
+
+// flat_map(flat_map&&)
+// noexcept(is_nothrow_move_constructible<key_container_type>::value &&
+// is_nothrow_move_constructible<mapped_container_type>::value &&
+// is_nothrow_copy_constructible<key_compare>::value);
+
+// This tests a conforming extension
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+
+template <class T>
+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<T>().allocate(n); }
+ void deallocate(T *p, std::ptrdiff_t n) { return std::allocator<T>().deallocate(p, n); }
+ friend bool operator==(ThrowingMoveAllocator, ThrowingMoveAllocator) = default;
+};
+
+struct ThrowingCopyComp {
+ ThrowingCopyComp() = default;
+ ThrowingCopyComp(const ThrowingCopyComp&) noexcept(false) {}
+ ThrowingCopyComp(ThrowingCopyComp&&) noexcept {}
+ 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<int, int>;
+ LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
+ C c;
+ C d = std::move(c);
+ }
+ {
+ using C = std::flat_map<int, int, std::less<int>, std::deque<int, test_allocator<int>>>;
+ LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
+ 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<int, int, std::less<int>, std::deque<int, ThrowingMoveAllocator<int>>, std::vector<int>>;
+ static_assert(!std::is_nothrow_move_constructible_v<std::deque<int, ThrowingMoveAllocator<int>>>);
+ static_assert(!std::is_nothrow_move_constructible_v<C>);
+ 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<int, int, std::less<int>, std::vector<int>, std::deque<int, ThrowingMoveAllocator<int>>>;
+ static_assert(!std::is_nothrow_move_constructible_v<std::deque<int, ThrowingMoveAllocator<int>>>);
+ static_assert(!std::is_nothrow_move_constructible_v<C>);
+ C c;
+ C d = std::move(c);
+ }
+#endif // _LIBCPP_VERSION
+ {
+ // Comparator fails to be nothrow-copy-constructible
+ using C = std::flat_map<int, int, ThrowingCopyComp>;
+ static_assert(!std::is_nothrow_move_constructible_v<C>);
+ C c;
+ C d = std::move(c);
+ }
+ {
+ // Moving the flat_map copies the comparator (to support std::function comparators)
+ using C = std::flat_map<int, int, MoveSensitiveComp>;
+ LIBCPP_STATIC_ASSERT(std::is_nothrow_move_constructible_v<C>);
+ C c;
+ assert(!c.key_comp().is_moved_from_);
+ C d = std::move(c);
+ LIBCPP_ASSERT(!c.key_comp().is_moved_from_);
+ assert(!d.key_comp().is_moved_from_);
+ }
+ 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 0000000000000..106885cdd36b8
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_container.pass.cpp
@@ -0,0 +1,129 @@
+//===----------------------------------------------------------------------===//
+//
+// 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(sorted_unique_t, key_container_type key_cont, mapped_container_type mapped_cont,
+// const key_compare& comp = key_compare());
+
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#include "min_allocator.h"
+#include "MoveOnly.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char>;
+ std::vector<int> ks = {1,2,4,10};
+ std::vector<char> vs = {4,3,2,1};
+ 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}}));
+ }
+ {
+ using Ks = std::deque<int, min_allocator<int>>;
+ using Vs = std::deque<char, min_allocator<char>>;
+ using M = std::flat_map<int, char, std::greater<int>, 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}}));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,2,4,10}, A(4));
+ auto vs = std::deque<int, A>({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));
+ }
+ {
+ using A = test_allocator<int>;
+ using M = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::deque<int, A>>;
+ auto ks = std::vector<int, A>({1,2,4,10}, A(4));
+ auto vs = std::deque<int, A>({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));
+ }
+ {
+ using M = std::flat_map<int, int, std::less<int>, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks = {1,2,4,10};
+ std::pmr::vector<int> 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);
+ }
+ {
+ using M = std::flat_map<int, int, std::less<int>, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks = {1,2,4,10};
+ std::pmr::vector<int> vs = {4,3,2,1};
+ vm.emplace_back(std::sorted_unique, std::move(ks), std::move(vs));
+ LIBCPP_ASSERT(ks.size() == 4); // ks' size is unchanged, since it uses a different allocator
+ LIBCPP_ASSERT(vs.size() == 4); // vs' size is unchanged, since it uses a different allocator
+ 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);
+ }
+ #if 0
+ {
+ using M = std::flat_map<int, int, std::less<int>, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pmr::vector<int> ks({1,2,4,10}, &mr);
+ std::pmr::vector<int> vs({4,3,2,1}, &mr);
+ vm.emplace_back(std::sorted_unique, std::move(ks), std::move(vs));
+ assert(ks.empty()); // ks is moved-from (after LWG 3802)
+ assert(vs.empty()); // vs is moved-from (after LWG 3802)
+ 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);
+ }
+ {
+ using M = std::flat_map<MoveOnly, MoveOnly, std::less<>, std::pmr::vector<MoveOnly>, std::pmr::vector<MoveOnly>>;
+ std::pmr::vector<M> vm;
+ std::pmr::vector<MoveOnly> ks;
+ std::pmr::vector<MoveOnly> vs;
+ vm.emplace_back(std::sorted_unique, std::move(ks), std::move(vs)); // this was a hard error before LWG 3802
+ assert(vm.size() == 1);
+ assert(vm[0].empty());
+ }
+ #endif
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_iter_iter_comp.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_iter_iter_comp.pass.cpp
new file mode 100644
index 0000000000000..b5852d40ea910
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.cons/sorted_iter_iter_comp.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
+
+// <flat_map>
+
+// template <class InputIterator>
+// flat_map(sorted_unique_t, InputIterator first, InputIterator last, const key_compare& comp = key_compare());
+// template<class InputIterator, class Allocator>
+// flat_map(sorted_unique_t, InputIterator first, InputIterator last, const key_compare& comp, const Allocator& a);
+
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <memory_resource>
+#include <vector>
+
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+#include "../../../test_compare.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, int, std::function<bool(int,int)>>;
+ using P = std::pair<int, int>;
+ P ar[] = {{1,1}, {2,2}, {4,4}, {5,5}};
+ auto m = M(std::sorted_unique, cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 4), std::less<int>());
+ assert(m == M({{1,1}, {2,2}, {4,4}, {5,5}}, std::less<>()));
+ assert(m.key_comp()(1, 2) == true);
+ }
+ {
+ using M = std::flat_map<int, int, std::greater<int>, std::deque<int, min_allocator<int>>, std::vector<int>>;
+ using P = std::pair<int, int>;
+ P ar[] = {{5,5}, {4,4}, {2,2}, {1,1}};
+ auto m = M(std::sorted_unique, cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(ar + 4), std::greater<int>());
+ assert((m == M{{5,5}, {4,4}, {2,2}, {1,1}}));
+ }
+ {
+ // Test when the operands are of array type (also contiguous iterator type)
+ using C = test_less<int>;
+ using M = std::flat_map<int, int, C, std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>;
+ std::pair<int, int> ar[1] = {{42,42}};
+ auto m = M(std::sorted_unique, ar, ar, C(5));
+ assert(m.empty());
+ assert(m.key_comp() == C(5));
+ }
+ {
+ using C = test_less<int>;
+ using A1 = test_allocator<int>;
+ using A2 = test_allocator<short>;
+ using M = std::flat_map<int, short, C, std::vector<int, A1>, std::deque<short, A2>>;
+ using P = std::pair<int, int>;
+ 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));
+ }
+ {
+ using A1 = test_allocator<short>;
+ using A2 = test_allocator<int>;
+ using M = std::flat_map<short, int, std::less<int>, std::deque<short, A1>, std::vector<int, A2>>;
+ using P = std::pair<int, int>;
+ 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));
+ }
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ using P = std::pair<int, int>;
+ P ar[] = {{1,1}, {2,2}, {4,4}, {5,5}};
+ vm.emplace_back(std::sorted_unique, cpp17_input_iterator<const P*>(ar), cpp17_input_iterator<const P*>(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);
+ }
+ {
+ using C = test_less<int>;
+ using M = std::flat_map<int, int, C, std::pmr::vector<int>, std::pmr::vector<int>>;
+ std::pmr::monotonic_buffer_resource mr;
+ std::pmr::vector<M> vm(&mr);
+ std::pair<int, int> 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.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 0000000000000..c11aea1364e3a
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if.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>
+
+// template<class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Predicate>
+// typename flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type
+// erase_if(flat_map<Key, T, Compare, KeyContainer, MappedContainer>& c, Predicate pred);
+
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <initializer_list>
+#include <vector>
+
+#include "test_macros.h"
+#include "test_allocator.h"
+#include "min_allocator.h"
+
+// Verify that `flat_map` (like `map`) does NOT support std::erase.
+//
+template <class S>
+concept HasStdErase = requires (S& s, typename S::value_type x) {
+ std::erase(s, x);
+};
+static_assert(HasStdErase<std::vector<int>>);
+static_assert(!HasStdErase<std::flat_map<int, int>>);
+
+template <class M>
+M make(std::initializer_list<int> vals)
+{
+ M ret;
+ for (int v : vals)
+ ret[static_cast<typename M::key_type>(v)] = static_cast<typename M::mapped_type>(v + 10);
+ return ret;
+}
+
+template <class M, class Pred>
+void test0(std::initializer_list<int> vals, Pred p, std::initializer_list<int> expected, std::size_t expected_erased_count) {
+ M s = make<M>(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<M>(expected));
+}
+
+template <class S>
+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<S>({}, is1, {}, 0);
+
+ test0<S>({1}, is1, {}, 1);
+ test0<S>({1}, is2, {1}, 0);
+
+ test0<S>({1, 2}, is1, {2}, 1);
+ test0<S>({1, 2}, is2, {1}, 1);
+ test0<S>({1, 2}, is3, {1, 2}, 0);
+
+ test0<S>({1, 2, 3}, is1, {2, 3}, 1);
+ test0<S>({1, 2, 3}, is2, {1, 3}, 1);
+ test0<S>({1, 2, 3}, is3, {1, 2}, 1);
+ test0<S>({1, 2, 3}, is4, {1, 2, 3}, 0);
+
+ test0<S>({1, 2, 3}, True, {}, 3);
+ test0<S>({1, 2, 3}, False, {1, 2, 3}, 0);
+}
+
+int main(int, char**)
+{
+ test<std::flat_map<int, char>>();
+ test<std::flat_map<int, char, std::less<int>, std::vector<int, min_allocator<int>>, std::vector<char, min_allocator<char>>>>();
+ test<std::flat_map<int, char, std::greater<int>, std::vector<int, test_allocator<int>>>>();
+ test<std::flat_map<int, char, std::less<int>, std::deque<int, min_allocator<int>>>>();
+ test<std::flat_map<int, char, std::greater<int>, std::deque<int, test_allocator<int>>>>();
+ test<std::flat_map<long, int>>();
+ test<std::flat_map<double, int>>();
+ {
+ using M = std::flat_map<bool, bool>;
+ std::flat_map<bool, bool> fs = {{true,false}, {false,true}};
+ std::same_as<size_t> auto n = std::erase_if(fs, [](M::const_reference x) { return x.first; });
+ assert((fs == M{{false,true}}));
+ assert(n == 1);
+ n = std::erase_if(fs, [](const M::value_type& x) { return !x.first; });
+ assert(fs.empty());
+ assert(n == 1);
+ }
+ 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 0000000000000..fcda037112f94
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/flat.map.erasure/erase_if_exceptions.pass.cpp
@@ -0,0 +1,162 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<class Key, class T, class Compare, class KeyContainer, class MappedContainer, class Predicate>
+// typename flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type
+// erase_if(flat_map<Key, T, Compare, KeyContainer, MappedContainer>& 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 <algorithm>
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+#include <vector>
+
+#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<int, int> expected[] = {{1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}, {8,8}};
+ {
+ using M = std::flat_map<ThrowingAssignment, int, ThrowingComparator>;
+ 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);
+ assert(m.keys().size() == m.values().size()); // still sized correctly
+ assert(std::is_sorted(m.begin(), m.end())); // still sorted
+ assert(std::adjacent_find(m.begin(), m.end()) == m.end()); // still contains no duplicates
+ 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<int, ThrowingAssignment, ThrowingComparator>;
+ 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);
+ assert(m.keys().size() == m.values().size()); // still sized correctly
+ assert(std::is_sorted(m.begin(), m.end())); // still sorted
+ assert(std::adjacent_find(m.begin(), m.end()) == m.end()); // still contains no duplicates
+ 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<ThrowingAssignment, int, ThrowingComparator, std::deque<ThrowingAssignment>, std::deque<int>>;
+ 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<ThrowingAssignment> 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);
+ assert(m.keys().size() == m.values().size()); // still sized correctly
+ assert(std::is_sorted(m.begin(), m.end())); // still sorted
+ assert(std::adjacent_find(m.begin(), m.end()) == m.end()); // still contains no duplicates
+ 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/incomplete_type.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/incomplete_type.pass.cpp
new file mode 100644
index 0000000000000..68a40dd5b7f6d
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/incomplete_type.pass.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// Check that std::flat_map and its iterators can be instantiated with an incomplete
+// type.
+
+#include <flat_map>
+
+#include "test_macros.h"
+
+struct A {
+ using Map = std::flat_map<A, A>;
+ int data;
+ Map m;
+ Map::iterator it;
+ Map::const_iterator cit;
+};
+
+// Implement the operator< required in order to instantiate flat_map<A, X>
+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/insert_range.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/insert_range.pass.cpp
new file mode 100644
index 0000000000000..513f72f11cc3f
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/insert_range.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
+
+// <flat_map>
+
+// template<container-compatible-range<value_type> R>
+// void insert_range(R&& rg);
+
+#include <algorithm>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <ranges>
+#include <vector>
+
+#include "MoveOnly.h"
+#include "test_macros.h"
+#include "test_iterators.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using P = std::pair<int, int>;
+ using M = std::flat_map<int, int>;
+ using It = forward_iterator<const P*>;
+ 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<decltype(r)>);
+ 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<int, int>;
+ using M = std::flat_map<int, int, std::greater<>, std::deque<int, min_allocator<int>>>;
+ using It = cpp20_input_iterator<const P*>;
+ 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>(It(ar + 6)) };
+ static_assert(!std::ranges::common_range<decltype(r)>);
+ 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<int, int>;
+ using M = std::flat_map<int, int, ModTen>;
+ 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}}));
+ }
+ {
+ // 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<int, int>;
+ using M = std::flat_map<int, int, ModTen>;
+ M m = {{21,0}, {43,0}, {15,0}, {37,0}};
+ P ar[] = {{33,1}, {18,1}, {55,1}, {28,1}, {42,1}};
+ m.insert_range(ar);
+ LIBCPP_ASSERT((m == M{{21,0}, {42,1}, {43,0}, {15,0}, {37,0}, {18,1}}));
+ }
+ {
+ // Items are forwarded correctly from the input range (P2767).
+ std::pair<MoveOnly, MoveOnly> a[] = {{3,3}, {1,1}, {4,4}, {1,1}, {5,5}};
+ std::flat_map<MoveOnly, MoveOnly> m;
+ m.insert_range(a | std::views::as_rvalue);
+ std::pair<MoveOnly, MoveOnly> 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<int, int> pa[] = {{3,3}, {1,1}, {4,4}, {1,1}, {5,5}};
+ std::deque<std::reference_wrapper<std::pair<int, int>>> a(pa, pa+5);
+ std::flat_map<int, int> m;
+ m.insert_range(a);
+ std::pair<int, int> expected[] = {{1,1}, {3,3}, {4,4}, {5,5}};
+ assert(std::ranges::equal(m, expected));
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/insert_range_stability.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/insert_range_stability.pass.cpp
new file mode 100644
index 0000000000000..426067105b76d
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/insert_range_stability.pass.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<container-compatible-range<value_type> 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 <algorithm>
+#include <cassert>
+#include <cstdint>
+#include <flat_map>
+#include <random>
+#include <ranges>
+#include <map>
+
+#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<uint16_t, uint16_t> pairs[400];
+ for (int i = 0; i < 400; ++i) {
+ uint16_t r = randomness();
+ pairs[i] = {r, r};
+ }
+
+ std::map<uint16_t, uint16_t, Mod256> m(pairs, pairs + 200);
+ std::flat_map<uint16_t, uint16_t, Mod256> 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));
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/insert_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/insert_transparent.pass.cpp
new file mode 100644
index 0000000000000..14fc0fab59168
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/insert_transparent.pass.cpp
@@ -0,0 +1,121 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<class K> pair<iterator, bool> insert(P&& x);
+// template<class K> iterator insert(const_iterator hint, P&& x);
+
+#include <algorithm>
+#include <compare>
+#include <concepts>
+#include <flat_map>
+#include <functional>
+
+#include "test_macros.h"
+#include "test_iterators.h"
+#include "min_allocator.h"
+
+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;
+ }
+};
+
+int main(int, char**)
+{
+ const std::pair<int, int> 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.
+ using M = std::flat_map<CompareCounter, int, std::less<>>;
+ M m = {{1,1}, {2,2}, {4,4}, {5,5}};
+ expensive_comparisons = 0;
+ cheap_comparisons = 0;
+ std::same_as<std::pair<M::iterator, bool>> 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&&)
+ using M = std::flat_map<CompareCounter, int, std::less<>>;
+ M m = {{1,1}, {2,2}, {4,4}, {5,5}};
+ expensive_comparisons = 0;
+ cheap_comparisons = 0;
+ std::same_as<M::iterator> 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&&)
+ using M = std::flat_map<CompareCounter, int>;
+ M m = {{1,1}, {2,2}, {4,4}, {5,5}};
+ expensive_comparisons = 0;
+ cheap_comparisons = 0;
+ std::same_as<std::pair<M::iterator, bool>> 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&&)
+ using M = std::flat_map<CompareCounter, int>;
+ M m = {{1,1}, {2,2}, {4,4}, {5,5}};
+ expensive_comparisons = 0;
+ cheap_comparisons = 0;
+ std::same_as<M::iterator> 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&&...)
+ using M = std::flat_map<CompareCounter, int>;
+ M m = {{1,1}, {2,2}, {4,4}, {5,5}};
+ expensive_comparisons = 0;
+ cheap_comparisons = 0;
+ std::same_as<std::pair<M::iterator, bool>> 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));
+ }
+ {
+ // no ambiguity between insert(pos, P&&) and insert(first, last)
+ using M = std::flat_map<int, int>;
+ struct Evil {
+ operator M::value_type() const;
+ operator M::const_iterator() const;
+ };
+ std::flat_map<int, int> m;
+ ASSERT_SAME_TYPE(decltype(m.insert(Evil())), std::pair<M::iterator, bool>);
+ ASSERT_SAME_TYPE(decltype(m.insert(m.begin(), Evil())), M::iterator);
+ ASSERT_SAME_TYPE(decltype(m.insert(m.begin(), m.end())), void);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/iterator.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/iterator.pass.cpp
new file mode 100644
index 0000000000000..20e941cb64337
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/iterator.pass.cpp
@@ -0,0 +1,98 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// iterator begin();
+// const_iterator begin() const;
+// iterator end();
+// const_iterator end() const;
+//
+// const_iterator cbegin() const;
+// const_iterator cend() const;
+
+#include <cassert>
+#include <cstddef>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <string>
+
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char, std::less<int>, std::deque<int>, std::deque<char>>;
+ M m = {{1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}};
+ ASSERT_SAME_TYPE(decltype(m.begin()), M::iterator);
+ ASSERT_SAME_TYPE(decltype(m.cbegin()), M::const_iterator);
+ ASSERT_SAME_TYPE(decltype(m.end()), M::iterator);
+ ASSERT_SAME_TYPE(decltype(m.cend()), M::const_iterator);
+ assert(m.size() == 4);
+ assert(std::distance(m.begin(), m.end()) == 4);
+ assert(std::distance(m.cbegin(), m.cend()) == 4);
+ M::iterator i; // default-construct
+ i = m.begin(); // move-assignment
+ 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());
+ }
+ {
+ using M = std::flat_map<short, char, std::less<>, std::deque<short>, std::string>;
+ const M m = {{1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}};
+ ASSERT_SAME_TYPE(decltype(m.begin()), M::const_iterator);
+ ASSERT_SAME_TYPE(decltype(m.cbegin()), M::const_iterator);
+ ASSERT_SAME_TYPE(decltype(m.end()), M::const_iterator);
+ ASSERT_SAME_TYPE(decltype(m.cend()), M::const_iterator);
+ assert(m.size() == 4);
+ assert(std::distance(m.begin(), m.end()) == 4);
+ assert(std::distance(m.cbegin(), m.cend()) == 4);
+ M::const_iterator i; // default-construct
+ i = m.begin(); // move-assignment
+ for (int j = 1; j <= 4; ++j, ++i) { // pre-increment
+ assert(i->first == j);
+ 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());
+ }
+ {
+ // N3644 testing
+ using C = std::flat_map<int, char>;
+ 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/iterator_comparison.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/iterator_comparison.pass.cpp
new file mode 100644
index 0000000000000..22ebc599b3f24
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/iterator_comparison.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>
+
+// flat_map iterators should be C++20 random access iterators
+// supporting `operator<=>`, even when the underlying container's
+// iterators are not.
+
+#include <compare>
+#include <concepts>
+#include <flat_map>
+#include <functional>
+
+#include "MinSequenceContainer.h"
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ {
+ using V = MinSequenceContainer<int, random_access_iterator<int*>, random_access_iterator<const int*>>;
+ using M = std::flat_map<int, int, std::less<int>, V, V>;
+ using VI = V::iterator;
+ using VCI = V::const_iterator;
+ using I = M::iterator;
+ using CI = M::const_iterator;
+ using RI = M::reverse_iterator;
+ using CRI = M::const_reverse_iterator;
+ static_assert(!std::three_way_comparable<VI>); // But, despite this...
+ static_assert(!std::three_way_comparable<VCI>);
+ static_assert(std::three_way_comparable<I>); // ...the wrapped iterators support <=>.
+ static_assert(std::three_way_comparable<CI>);
+ static_assert(std::three_way_comparable<RI>);
+ static_assert(std::three_way_comparable<CRI>);
+ static_assert(std::same_as<decltype(I() <=> I()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(I() <=> CI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(CI() <=> CI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(RI() <=> RI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(RI() <=> CRI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(CRI() <=> CRI()), std::strong_ordering>);
+ }
+ {
+ using V = MinSequenceContainer<int, int*, const int*>;
+ using M = std::flat_map<int, int, std::less<int>, V, V>;
+ using VI = V::iterator;
+ using VCI = V::const_iterator;
+ using I = M::iterator;
+ using CI = M::const_iterator;
+ using RI = M::reverse_iterator;
+ using CRI = M::const_reverse_iterator;
+ static_assert(std::three_way_comparable<VI>); // And when VI does support <=>...
+ static_assert(std::three_way_comparable<VCI>);
+ static_assert(std::three_way_comparable<I>); // ...of course the wrapped iterators still support <=>.
+ static_assert(std::three_way_comparable<CI>);
+ static_assert(std::three_way_comparable<RI>);
+ static_assert(std::three_way_comparable<CRI>);
+ static_assert(std::same_as<decltype(I() <=> I()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(I() <=> CI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(CI() <=> CI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(RI() <=> RI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(RI() <=> CRI()), std::strong_ordering>);
+ static_assert(std::same_as<decltype(CRI() <=> CRI()), std::strong_ordering>);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/iterator_concept_conformance.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/iterator_concept_conformance.compile.pass.cpp
new file mode 100644
index 0000000000000..ddb68b11b20ee
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/iterator_concept_conformance.compile.pass.cpp
@@ -0,0 +1,142 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// iterator, const_iterator, reverse_iterator, const_reverse_iterator
+
+#include <flat_map>
+#include <deque>
+#include <functional>
+#include <iterator>
+#include <string>
+#include <vector>
+
+#include "test_macros.h"
+
+void test() {
+ {
+ using C = std::flat_map<int, char>;
+ 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<I>);
+ static_assert(std::random_access_iterator<CI>);
+ static_assert(std::random_access_iterator<RI>);
+ static_assert(std::random_access_iterator<CRI>);
+ static_assert(!std::contiguous_iterator<I>);
+ static_assert(!std::contiguous_iterator<CI>);
+ static_assert(!std::contiguous_iterator<RI>);
+ static_assert(!std::contiguous_iterator<CRI>);
+ static_assert(!std::indirectly_writable<I, std::pair<int, char>>);
+ static_assert(!std::indirectly_writable<CI, std::pair<int, char>>);
+ static_assert(!std::indirectly_writable<RI, std::pair<int, char>>);
+ static_assert(!std::indirectly_writable<CRI, std::pair<int, char>>);
+ static_assert( std::sentinel_for<I, I>);
+ static_assert( std::sentinel_for<I, CI>);
+ static_assert(!std::sentinel_for<I, RI>);
+ static_assert(!std::sentinel_for<I, CRI>);
+ static_assert( std::sentinel_for<CI, I>);
+ static_assert( std::sentinel_for<CI, CI>);
+ static_assert(!std::sentinel_for<CI, RI>);
+ static_assert(!std::sentinel_for<CI, CRI>);
+ static_assert(!std::sentinel_for<RI, I>);
+ static_assert(!std::sentinel_for<RI, CI>);
+ static_assert( std::sentinel_for<RI, RI>);
+ static_assert( std::sentinel_for<RI, CRI>);
+ static_assert(!std::sentinel_for<CRI, I>);
+ static_assert(!std::sentinel_for<CRI, CI>);
+ static_assert( std::sentinel_for<CRI, RI>);
+ static_assert( std::sentinel_for<CRI, CRI>);
+ static_assert(std::indirectly_movable_storable<I, std::pair<int, char>*>);
+ static_assert(std::indirectly_movable_storable<CI, std::pair<int, char>*>);
+ static_assert(std::indirectly_movable_storable<RI, std::pair<int, char>*>);
+ static_assert(std::indirectly_movable_storable<CRI, std::pair<int, char>*>);
+ }
+ {
+ using C = std::flat_map<char*, int, std::less<>, std::deque<char*>, std::vector<int>>;
+ 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<I>);
+ static_assert(std::random_access_iterator<CI>);
+ static_assert(std::random_access_iterator<RI>);
+ static_assert(std::random_access_iterator<CRI>);
+ static_assert(!std::contiguous_iterator<I>);
+ static_assert(!std::contiguous_iterator<CI>);
+ static_assert(!std::contiguous_iterator<RI>);
+ static_assert(!std::contiguous_iterator<CRI>);
+ static_assert(!std::indirectly_writable<I, std::pair<char*, int>>);
+ static_assert(!std::indirectly_writable<CI, std::pair<char*, int>>);
+ static_assert(!std::indirectly_writable<RI, std::pair<char*, int>>);
+ static_assert(!std::indirectly_writable<CRI, std::pair<char*, int>>);
+ static_assert( std::sentinel_for<I, I>);
+ static_assert( std::sentinel_for<I, CI>);
+ static_assert(!std::sentinel_for<I, RI>);
+ static_assert(!std::sentinel_for<I, CRI>);
+ static_assert( std::sentinel_for<CI, I>);
+ static_assert( std::sentinel_for<CI, CI>);
+ static_assert(!std::sentinel_for<CI, RI>);
+ static_assert(!std::sentinel_for<CI, CRI>);
+ static_assert(!std::sentinel_for<RI, I>);
+ static_assert(!std::sentinel_for<RI, CI>);
+ static_assert( std::sentinel_for<RI, RI>);
+ static_assert( std::sentinel_for<RI, CRI>);
+ static_assert(!std::sentinel_for<CRI, I>);
+ static_assert(!std::sentinel_for<CRI, CI>);
+ static_assert( std::sentinel_for<CRI, RI>);
+ static_assert( std::sentinel_for<CRI, CRI>);
+ static_assert(std::indirectly_movable_storable<I, std::pair<char*, int>*>);
+ static_assert(std::indirectly_movable_storable<CI, std::pair<char*, int>*>);
+ static_assert(std::indirectly_movable_storable<RI, std::pair<char*, int>*>);
+ static_assert(std::indirectly_movable_storable<CRI, std::pair<char*, int>*>);
+ }
+ {
+ using C = std::flat_map<char, bool, std::less<>, std::string, std::vector<bool>>;
+ 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<I>);
+ static_assert(std::random_access_iterator<CI>);
+ static_assert(std::random_access_iterator<RI>);
+ static_assert(std::random_access_iterator<CRI>);
+ static_assert(!std::contiguous_iterator<I>);
+ static_assert(!std::contiguous_iterator<CI>);
+ static_assert(!std::contiguous_iterator<RI>);
+ static_assert(!std::contiguous_iterator<CRI>);
+ static_assert(!std::indirectly_writable<I, std::pair<char, bool>>);
+ static_assert(!std::indirectly_writable<CI, std::pair<char, bool>>);
+ static_assert(!std::indirectly_writable<RI, std::pair<char, bool>>);
+ static_assert(!std::indirectly_writable<CRI, std::pair<char, bool>>);
+ static_assert( std::sentinel_for<I, I>);
+ static_assert( std::sentinel_for<I, CI>);
+ static_assert(!std::sentinel_for<I, RI>);
+ static_assert(!std::sentinel_for<I, CRI>);
+ static_assert( std::sentinel_for<CI, I>);
+ static_assert( std::sentinel_for<CI, CI>);
+ static_assert(!std::sentinel_for<CI, RI>);
+ static_assert(!std::sentinel_for<CI, CRI>);
+ static_assert(!std::sentinel_for<RI, I>);
+ static_assert(!std::sentinel_for<RI, CI>);
+ static_assert( std::sentinel_for<RI, RI>);
+ static_assert( std::sentinel_for<RI, CRI>);
+ static_assert(!std::sentinel_for<CRI, I>);
+ static_assert(!std::sentinel_for<CRI, CI>);
+ static_assert( std::sentinel_for<CRI, RI>);
+ static_assert( std::sentinel_for<CRI, CRI>);
+ static_assert(std::indirectly_movable_storable<I, std::pair<char, bool>*>);
+ static_assert(std::indirectly_movable_storable<CI, std::pair<char, bool>*>);
+ static_assert(std::indirectly_movable_storable<RI, std::pair<char, bool>*>);
+ static_assert(std::indirectly_movable_storable<CRI, std::pair<char, bool>*>);
+ }
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/lower_bound.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/lower_bound.pass.cpp
new file mode 100644
index 0000000000000..8094e7771fc60
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/lower_bound.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
+
+// <flat_map>
+
+// iterator lower_bound(const key_type& k);
+// const_iterator lower_bound(const key_type& k) const;
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char>;
+ M m = {{1,'a'}, {2,'b'}, {4,'d'}, {5,'e'}, {8,'h'}};
+ ASSERT_SAME_TYPE(decltype(m.lower_bound(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(0)), 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<int, char, std::greater<int>, std::deque<int, min_allocator<int>>, std::deque<char, min_allocator<char>>>;
+ M m = {{1,'a'}, {2,'b'}, {4,'d'}, {5,'e'}, {8,'h'}};
+ ASSERT_SAME_TYPE(decltype(m.lower_bound(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(0)), 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());
+ }
+ {
+ using M = std::flat_map<bool, bool>;
+ M m = {{true,false}, {false,true}};
+ ASSERT_SAME_TYPE(decltype(m.lower_bound(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(0)), M::const_iterator);
+ assert(m.lower_bound(true) == m.begin() + 1);
+ assert(m.lower_bound(false) == m.begin());
+ m = {{true,true}};
+ assert(m.lower_bound(true) == m.begin());
+ assert(m.lower_bound(false) == m.begin());
+ m = {{false,false}};
+ assert(std::as_const(m).lower_bound(true) == m.end());
+ assert(std::as_const(m).lower_bound(false) == m.begin());
+ m.clear();
+ assert(m.lower_bound(true) == m.end());
+ assert(m.lower_bound(false) == m.end());
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/lower_bound_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/lower_bound_transparent.pass.cpp
new file mode 100644
index 0000000000000..a391d099f3add
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/lower_bound_transparent.pass.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<class K> iterator lower_bound(const K& x);
+// template<class K> const_iterator lower_bound(const K& x) const;
+
+#include <cassert>
+#include <flat_map>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+
+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_; }
+ };
+private:
+ std::string lower_;
+ std::string upper_;
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<std::string, int, StartsWith::Less>;
+ M m = { {"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5} };
+ ASSERT_SAME_TYPE(decltype(m.lower_bound(StartsWith('b'))), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(StartsWith('b'))), M::const_iterator);
+ assert(m.lower_bound("beta") == m.begin() + 1);
+ assert(m.lower_bound("delta") == m.begin() + 2);
+ assert(m.lower_bound("zeta") == m.begin() + 5);
+ assert(m.lower_bound(StartsWith('b')) == m.begin() + 1);
+ assert(m.lower_bound(StartsWith('d')) == m.begin() + 2);
+ assert(m.lower_bound(StartsWith('e')) == m.begin() + 2);
+ assert(m.lower_bound(StartsWith('z')) == m.begin() + 5);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/max_size.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/max_size.pass.cpp
new file mode 100644
index 0000000000000..1c21704ebd64c
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/max_size.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
+
+// <flat_map>
+
+// size_type max_size() const noexcept;
+
+#include <cassert>
+#include <flat_map>
+#include <functional>
+#include <limits>
+#include <type_traits>
+#include <vector>
+
+#include "test_allocator.h"
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ {
+ using A1 = limited_allocator<int, 10>;
+ using A2 = limited_allocator<int, 20>;
+ using C = std::flat_map<int, int, std::less<int>, std::vector<int, A1>, std::vector<int, A2>>;
+ ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t);
+ ASSERT_SAME_TYPE(C::size_type, std::size_t);
+ 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<int, 10>;
+ using A2 = limited_allocator<int, 20>;
+ using C = std::flat_map<int, int, std::less<int>, std::vector<int, A2>, std::vector<int, A1>>;
+ ASSERT_SAME_TYPE(C::difference_type, std::ptrdiff_t);
+ ASSERT_SAME_TYPE(C::size_type, std::size_t);
+ 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<int, (size_t)-1>;
+ using C = std::flat_map<int, int, std::less<int>, std::vector<int, A>, std::vector<int, A>>;
+ 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<C::size_type>(std::numeric_limits<C::difference_type>::max());
+ 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<char, char> 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<C::size_type>(std::numeric_limits<C::difference_type>::max());
+ 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<char>()));
+ }
+ 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 0000000000000..586933c640360
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/op_compare.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
+
+// <flat_map>
+
+// friend bool operator==(const flat_map& x, const flat_map& y);
+// friend synth-three-way-result<value_type>
+// operator<=>(const flat_map& x, const flat_map& y);
+
+#include <algorithm>
+#include <cassert>
+#include <compare>
+#include <flat_map>
+#include <functional>
+#include <limits>
+
+#include "test_comparisons.h"
+#include "test_container_comparisons.h"
+
+int main(int, char**) {
+ {
+ using C = std::flat_map<int, int>;
+ C s1 = {{1,1}};
+ C s2 = {{2,0}}; // {{1,1}} versus {{2,0}}
+ ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering);
+ AssertComparisonsReturnBool<C>();
+ 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<int, int, std::greater<int>>;
+ C s1 = {{1,1}};
+ C s2 = {{2,0}}; // {{1,1}} versus {{2,0}}
+ ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::strong_ordering);
+ AssertComparisonsReturnBool<C>();
+ 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));
+ }
+ {
+ using C = std::flat_map<double, int>;
+ C s1 = { {1, 1} };
+ C s2 = C(std::sorted_unique, { {std::numeric_limits<double>::quiet_NaN(), 2} });
+ ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);
+ AssertComparisonsReturnBool<C>();
+ assert(testComparisonsComplete(s1, s2, false, false, false));
+ }
+ {
+ using C = std::flat_map<int, double>;
+ C s1 = { {1, 1} };
+ C s2 = C(std::sorted_unique, { {2, std::numeric_limits<double>::quiet_NaN()} });
+ ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);
+ AssertComparisonsReturnBool<C>();
+ assert(testComparisonsComplete(s1, s2, false, true, false));
+ s2 = C(std::sorted_unique, { {1, std::numeric_limits<double>::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<double, double, StrongComp>;
+ C s1 = {{1, 1}};
+ C s2 = { {std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()} };
+ ASSERT_SAME_TYPE(decltype(s1 <=> s2), std::partial_ordering);
+ AssertComparisonsReturnBool<C>();
+ assert(testComparisonsComplete(s1, s2, false, false, false));
+ s1 = { { {1, 1}, {std::numeric_limits<double>::quiet_NaN(), 1} } };
+ s2 = { { {std::numeric_limits<double>::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/range_concept_conformance.compile.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/range_concept_conformance.compile.pass.cpp
new file mode 100644
index 0000000000000..981267f6604a0
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/range_concept_conformance.compile.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
+
+#include <concepts>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <ranges>
+#include <string>
+#include <vector>
+
+void test() {
+ {
+ using C = std::flat_map<int, int, std::deque<int>, std::vector<int>>;
+
+ static_assert(std::same_as<std::ranges::iterator_t<C>, C::iterator>);
+ static_assert(std::ranges::random_access_range<C>);
+ static_assert(!std::ranges::contiguous_range<C>);
+ static_assert(std::ranges::common_range<C>);
+ static_assert(std::ranges::input_range<C>);
+ static_assert(!std::ranges::view<C>);
+ static_assert(std::ranges::sized_range<C>);
+ static_assert(!std::ranges::borrowed_range<C>);
+ static_assert(std::ranges::viewable_range<C>);
+
+ static_assert(std::same_as<std::ranges::iterator_t<const C>, C::const_iterator>);
+ static_assert(std::ranges::random_access_range<const C>);
+ static_assert(!std::ranges::contiguous_range<const C>);
+ static_assert(std::ranges::common_range<const C>);
+ static_assert(std::ranges::input_range<const C>);
+ static_assert(!std::ranges::view<const C>);
+ static_assert(std::ranges::sized_range<const C>);
+ static_assert(!std::ranges::borrowed_range<const C>);
+ static_assert(!std::ranges::viewable_range<const C>);
+ }
+ {
+ using C = std::flat_map<char, bool, std::less<>, std::string, std::vector<bool>>;
+
+ static_assert(std::same_as<std::ranges::iterator_t<C>, C::iterator>);
+ static_assert(std::ranges::random_access_range<C>);
+ static_assert(!std::ranges::contiguous_range<C>);
+ static_assert(std::ranges::common_range<C>);
+ static_assert(std::ranges::input_range<C>);
+ static_assert(!std::ranges::view<C>);
+ static_assert(std::ranges::sized_range<C>);
+ static_assert(!std::ranges::borrowed_range<C>);
+ static_assert(std::ranges::viewable_range<C>);
+
+ static_assert(std::same_as<std::ranges::iterator_t<const C>, C::const_iterator>);
+ static_assert(std::ranges::random_access_range<const C>);
+ static_assert(!std::ranges::contiguous_range<const C>);
+ static_assert(std::ranges::common_range<const C>);
+ static_assert(std::ranges::input_range<const C>);
+ static_assert(!std::ranges::view<const C>);
+ static_assert(std::ranges::sized_range<const C>);
+ static_assert(!std::ranges::borrowed_range<const C>);
+ static_assert(!std::ranges::viewable_range<const C>);
+ }
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/reverse_iterator.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/reverse_iterator.pass.cpp
new file mode 100644
index 0000000000000..3afdb56d48e28
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/reverse_iterator.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>
+
+// reverse_iterator rbegin();
+// const_reverse_iterator rbegin() const;
+// reverse_iterator rend();
+// const_reverse_iterator rend() const;
+//
+// const_reverse_iterator crbegin() const;
+// const_reverse_iterator crend() const;
+
+#include <cassert>
+#include <cstddef>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <string>
+
+#include "test_macros.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char, std::less<int>, std::deque<int>, std::deque<char>>;
+ M m = {{1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}};
+ ASSERT_SAME_TYPE(decltype(m.rbegin()), M::reverse_iterator);
+ ASSERT_SAME_TYPE(decltype(m.crbegin()), M::const_reverse_iterator);
+ ASSERT_SAME_TYPE(decltype(m.rend()), M::reverse_iterator);
+ ASSERT_SAME_TYPE(decltype(m.crend()), M::const_reverse_iterator);
+ assert(m.size() == 4);
+ assert(std::distance(m.rbegin(), m.rend()) == 4);
+ assert(std::distance(m.crbegin(), m.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());
+ }
+ {
+ using M = std::flat_map<short, char, std::less<>, std::deque<short>, std::string>;
+ const M m = {{1,'a'}, {2,'b'}, {3,'c'}, {4,'d'}};
+ ASSERT_SAME_TYPE(decltype(m.rbegin()), M::const_reverse_iterator);
+ ASSERT_SAME_TYPE(decltype(m.crbegin()), M::const_reverse_iterator);
+ ASSERT_SAME_TYPE(decltype(m.rend()), M::const_reverse_iterator);
+ ASSERT_SAME_TYPE(decltype(m.crend()), M::const_reverse_iterator);
+ assert(m.size() == 4);
+ assert(std::distance(m.rbegin(), m.rend()) == 4);
+ assert(std::distance(m.crbegin(), m.crend()) == 4);
+ M::const_reverse_iterator i; // default-construct
+ ASSERT_SAME_TYPE(decltype(i->first), const short&);
+ ASSERT_SAME_TYPE(decltype(i->second), const char&);
+ i = m.rbegin(); // move-assignment
+ for (int j = 4; j >= 1; --j, ++i) { // pre-increment
+ assert(i->first == j);
+ 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<int, char>;
+ 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/types.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/types.pass.cpp
new file mode 100644
index 0000000000000..d3aba8b14172e
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/types.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
+
+// <flat_map>
+
+// template<class Key, class T, class Compare = less<Key>,
+// class KeyContainer = vector<Key>, class MappedContainer = vector<T>>
+// class flat_map {
+// public:
+// // types
+// using key_type = Key;
+// using mapped_type = T;
+// using value_type = pair<key_type, mapped_type>;
+// using key_compare = Compare;
+// using reference = pair<const key_type&, mapped_type&>;
+// using const_reference = pair<const key_type&, const mapped_type&>;
+// 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<iterator>;
+// using const_reverse_iterator = std::reverse_iterator<const_iterator>;
+// using key_container_type = KeyContainer;
+// using mapped_container_type = MappedContainer;
+
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <type_traits>
+#include <vector>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using C = std::flat_map<int, short>;
+ static_assert(std::is_same_v<C::key_type, int>);
+ static_assert(std::is_same_v<C::mapped_type, short>);
+ static_assert(std::is_same_v<C::value_type, std::pair<int, short>>);
+ static_assert(std::is_same_v<C::key_compare, std::less<int>>);
+ static_assert(!std::is_same_v<C::value_compare, std::less<int>>);
+ static_assert(std::is_same_v<C::reference, std::pair<const int&, short&>>);
+ static_assert(std::is_same_v<C::const_reference, std::pair<const int&, const short&>>);
+ static_assert(std::random_access_iterator<C::iterator>);
+ static_assert(std::random_access_iterator<C::const_iterator>);
+ static_assert(std::random_access_iterator<C::reverse_iterator>);
+ static_assert(std::random_access_iterator<C::const_reverse_iterator>);
+ static_assert(std::is_same_v<C::reverse_iterator, std::reverse_iterator<C::iterator>>);
+ static_assert(std::is_same_v<C::const_reverse_iterator, std::reverse_iterator<C::const_iterator>>);
+ static_assert(std::is_same_v<C::size_type, std::size_t>);
+ static_assert(std::is_same_v<C::difference_type, std::ptrdiff_t>);
+ static_assert(std::is_same_v<C::key_container_type, std::vector<int>>);
+ static_assert(std::is_same_v<C::mapped_container_type, std::vector<short>>);
+ }
+ {
+ using C = std::flat_map<short, int, std::greater<long>, std::deque<short, min_allocator<short>>>;
+ static_assert(std::is_same_v<C::key_type, short>);
+ static_assert(std::is_same_v<C::mapped_type, int>);
+ static_assert(std::is_same_v<C::value_type, std::pair<short, int>>);
+ static_assert(std::is_same_v<C::key_compare, std::greater<long>>);
+ static_assert(!std::is_same_v<C::value_compare, std::greater<long>>);
+ static_assert(std::is_same_v<C::reference, std::pair<const short&, int&>>);
+ static_assert(std::is_same_v<C::const_reference, std::pair<const short&, const int&>>);
+ static_assert(std::random_access_iterator<C::iterator>);
+ static_assert(std::random_access_iterator<C::const_iterator>);
+ static_assert(std::random_access_iterator<C::reverse_iterator>);
+ static_assert(std::random_access_iterator<C::const_reverse_iterator>);
+ static_assert(std::is_same_v<C::reverse_iterator, std::reverse_iterator<C::iterator>>);
+ static_assert(std::is_same_v<C::const_reverse_iterator, std::reverse_iterator<C::const_iterator>>);
+ // size_type is invariably size_t
+ static_assert(std::is_same_v<C::size_type, std::size_t>);
+ static_assert(std::is_same_v<C::difference_type, std::ptrdiff_t>);
+ static_assert(std::is_same_v<C::key_container_type, std::deque<short, min_allocator<short>>>);
+ static_assert(std::is_same_v<C::mapped_container_type, std::vector<int>>);
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/upper_bound.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/upper_bound.pass.cpp
new file mode 100644
index 0000000000000..76238c16b1129
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/upper_bound.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
+
+// <flat_map>
+
+// iterator upper_bound(const key_type& k);
+// const_iterator upper_bound(const key_type& k) const;
+
+#include <cassert>
+#include <deque>
+#include <flat_map>
+#include <functional>
+#include <utility>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<int, char>;
+ M m = {{1,'a'}, {2,'b'}, {4,'d'}, {5,'e'}, {8,'h'}};
+ ASSERT_SAME_TYPE(decltype(m.upper_bound(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).upper_bound(0)), 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<int, char, std::greater<int>, std::deque<int, min_allocator<int>>, std::deque<char, min_allocator<char>>>;
+ M m = {{1,'a'}, {2,'b'}, {4,'d'}, {5,'e'}, {8,'h'}};
+ ASSERT_SAME_TYPE(decltype(m.upper_bound(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).upper_bound(0)), 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());
+ }
+ {
+ using M = std::flat_map<bool, bool>;
+ M m = {{true,false}, {false,true}};
+ ASSERT_SAME_TYPE(decltype(m.upper_bound(0)), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).upper_bound(0)), M::const_iterator);
+ assert(m.upper_bound(true) == m.end());
+ assert(m.upper_bound(false) == m.begin() + 1);
+ m = {{true,true}};
+ assert(m.upper_bound(true) == m.end());
+ assert(m.upper_bound(false) == m.begin());
+ m = {{false,false}};
+ assert(std::as_const(m).upper_bound(true) == m.end());
+ assert(std::as_const(m).upper_bound(false) == m.end());
+ m.clear();
+ assert(m.upper_bound(true) == m.end());
+ assert(m.upper_bound(false) == m.end());
+ }
+ return 0;
+}
diff --git a/libcxx/test/std/containers/container.adaptors/flat.map/upper_bound_transparent.pass.cpp b/libcxx/test/std/containers/container.adaptors/flat.map/upper_bound_transparent.pass.cpp
new file mode 100644
index 0000000000000..745a250c068fa
--- /dev/null
+++ b/libcxx/test/std/containers/container.adaptors/flat.map/upper_bound_transparent.pass.cpp
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<class K> iterator upper_bound(const K& x);
+// template<class K> const_iterator upper_bound(const K& x) const;
+
+#include <cassert>
+#include <flat_map>
+#include <string>
+#include <utility>
+
+#include "test_macros.h"
+
+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_; }
+ };
+private:
+ std::string lower_;
+ std::string upper_;
+};
+
+int main(int, char**)
+{
+ {
+ using M = std::flat_map<std::string, int, StartsWith::Less>;
+ M m = { {"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5} };
+ ASSERT_SAME_TYPE(decltype(m.upper_bound(StartsWith('b'))), M::iterator);
+ ASSERT_SAME_TYPE(decltype(std::as_const(m).upper_bound(StartsWith('b'))), M::const_iterator);
+ assert(m.upper_bound("beta") == m.begin() + 2);
+ assert(m.upper_bound("delta") == m.begin() + 2);
+ assert(m.upper_bound("zeta") == m.begin() + 5);
+ assert(m.upper_bound(StartsWith('b')) == m.begin() + 2);
+ assert(m.upper_bound(StartsWith('d')) == m.begin() + 2);
+ assert(m.upper_bound(StartsWith('e')) == m.begin() + 4);
+ assert(m.upper_bound(StartsWith('z')) == m.begin() + 5);
+ }
+ return 0;
+}
More information about the libcxx-commits
mailing list