Path: blob/main/contrib/llvm-project/libcxx/include/__flat_map/flat_map.h
213766 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___FLAT_MAP_FLAT_MAP_H10#define _LIBCPP___FLAT_MAP_FLAT_MAP_H1112#include <__algorithm/lexicographical_compare_three_way.h>13#include <__algorithm/lower_bound.h>14#include <__algorithm/min.h>15#include <__algorithm/ranges_adjacent_find.h>16#include <__algorithm/ranges_equal.h>17#include <__algorithm/ranges_inplace_merge.h>18#include <__algorithm/ranges_sort.h>19#include <__algorithm/ranges_unique.h>20#include <__algorithm/remove_if.h>21#include <__algorithm/upper_bound.h>22#include <__assert>23#include <__compare/synth_three_way.h>24#include <__concepts/swappable.h>25#include <__config>26#include <__cstddef/byte.h>27#include <__cstddef/ptrdiff_t.h>28#include <__flat_map/key_value_iterator.h>29#include <__flat_map/sorted_unique.h>30#include <__flat_map/utils.h>31#include <__functional/invoke.h>32#include <__functional/is_transparent.h>33#include <__functional/operations.h>34#include <__fwd/memory.h>35#include <__fwd/vector.h>36#include <__iterator/concepts.h>37#include <__iterator/distance.h>38#include <__iterator/iterator_traits.h>39#include <__iterator/next.h>40#include <__iterator/ranges_iterator_traits.h>41#include <__iterator/reverse_iterator.h>42#include <__memory/allocator_traits.h>43#include <__memory/uses_allocator.h>44#include <__memory/uses_allocator_construction.h>45#include <__ranges/access.h>46#include <__ranges/concepts.h>47#include <__ranges/container_compatible_range.h>48#include <__ranges/drop_view.h>49#include <__ranges/from_range.h>50#include <__ranges/ref_view.h>51#include <__ranges/size.h>52#include <__ranges/subrange.h>53#include <__ranges/zip_view.h>54#include <__type_traits/conjunction.h>55#include <__type_traits/container_traits.h>56#include <__type_traits/invoke.h>57#include <__type_traits/is_allocator.h>58#include <__type_traits/is_nothrow_constructible.h>59#include <__type_traits/is_same.h>60#include <__utility/exception_guard.h>61#include <__utility/move.h>62#include <__utility/pair.h>63#include <__utility/scope_guard.h>64#include <__vector/vector.h>65#include <initializer_list>66#include <stdexcept>6768#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)69# pragma GCC system_header70#endif7172_LIBCPP_PUSH_MACROS73#include <__undef_macros>7475#if _LIBCPP_STD_VER >= 237677_LIBCPP_BEGIN_NAMESPACE_STD7879template <class _Key,80class _Tp,81class _Compare = less<_Key>,82class _KeyContainer = vector<_Key>,83class _MappedContainer = vector<_Tp>>84class flat_map {85template <class, class, class, class, class>86friend class flat_map;8788static_assert(is_same_v<_Key, typename _KeyContainer::value_type>);89static_assert(is_same_v<_Tp, typename _MappedContainer::value_type>);90static_assert(!is_same_v<_KeyContainer, std::vector<bool>>, "vector<bool> is not a sequence container");91static_assert(!is_same_v<_MappedContainer, std::vector<bool>>, "vector<bool> is not a sequence container");9293template <bool _Const>94using __iterator _LIBCPP_NODEBUG = __key_value_iterator<flat_map, _KeyContainer, _MappedContainer, _Const>;9596public:97// types98using key_type = _Key;99using mapped_type = _Tp;100using value_type = pair<key_type, mapped_type>;101using key_compare = __type_identity_t<_Compare>;102using reference = pair<const key_type&, mapped_type&>;103using const_reference = pair<const key_type&, const mapped_type&>;104using size_type = size_t;105using difference_type = ptrdiff_t;106using iterator = __iterator<false>; // see [container.requirements]107using const_iterator = __iterator<true>; // see [container.requirements]108using reverse_iterator = std::reverse_iterator<iterator>;109using const_reverse_iterator = std::reverse_iterator<const_iterator>;110using key_container_type = _KeyContainer;111using mapped_container_type = _MappedContainer;112113class value_compare {114private:115_LIBCPP_NO_UNIQUE_ADDRESS key_compare __comp_;116_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 value_compare(key_compare __c) : __comp_(__c) {}117friend flat_map;118119public:120_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool121operator()(const_reference __x, const_reference __y) const {122return __comp_(__x.first, __y.first);123}124};125126struct containers {127key_container_type keys;128mapped_container_type values;129};130131private:132template <class _Allocator>133_LIBCPP_HIDE_FROM_ABI static constexpr bool __allocator_ctor_constraint =134_And<uses_allocator<key_container_type, _Allocator>, uses_allocator<mapped_container_type, _Allocator>>::value;135136_LIBCPP_HIDE_FROM_ABI static constexpr bool __is_compare_transparent = __is_transparent_v<_Compare>;137138public:139// [flat.map.cons], construct/copy/destroy140_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map() noexcept(141is_nothrow_default_constructible_v<_KeyContainer> && is_nothrow_default_constructible_v<_MappedContainer> &&142is_nothrow_default_constructible_v<_Compare>)143: __containers_(), __compare_() {}144145_LIBCPP_HIDE_FROM_ABI flat_map(const flat_map&) = default;146147_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(flat_map&& __other) noexcept(148is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_MappedContainer> &&149is_nothrow_move_constructible_v<_Compare>)150# if _LIBCPP_HAS_EXCEPTIONS151try152# endif // _LIBCPP_HAS_EXCEPTIONS153: __containers_(std::move(__other.__containers_)), __compare_(std::move(__other.__compare_)) {154__other.clear();155# if _LIBCPP_HAS_EXCEPTIONS156} catch (...) {157__other.clear();158// gcc does not like the `throw` keyword in a conditionally noexcept function159if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> &&160is_nothrow_move_constructible_v<_MappedContainer> && is_nothrow_move_constructible_v<_Compare>)) {161throw;162}163# endif // _LIBCPP_HAS_EXCEPTIONS164}165166template <class _Allocator>167requires __allocator_ctor_constraint<_Allocator>168_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(const flat_map& __other, const _Allocator& __alloc)169: flat_map(__ctor_uses_allocator_tag{},170__alloc,171__other.__containers_.keys,172__other.__containers_.values,173__other.__compare_) {}174175template <class _Allocator>176requires __allocator_ctor_constraint<_Allocator>177_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(flat_map&& __other, const _Allocator& __alloc)178# if _LIBCPP_HAS_EXCEPTIONS179try180# endif // _LIBCPP_HAS_EXCEPTIONS181: flat_map(__ctor_uses_allocator_tag{},182__alloc,183std::move(__other.__containers_.keys),184std::move(__other.__containers_.values),185std::move(__other.__compare_)) {186__other.clear();187# if _LIBCPP_HAS_EXCEPTIONS188} catch (...) {189__other.clear();190throw;191# endif // _LIBCPP_HAS_EXCEPTIONS192}193194_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(195key_container_type __key_cont, mapped_container_type __mapped_cont, const key_compare& __comp = key_compare())196: __containers_{.keys = std::move(__key_cont), .values = std::move(__mapped_cont)}, __compare_(__comp) {197_LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(),198"flat_map keys and mapped containers have different size");199__sort_and_unique();200}201202template <class _Allocator>203requires __allocator_ctor_constraint<_Allocator>204_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26205flat_map(const key_container_type& __key_cont, const mapped_container_type& __mapped_cont, const _Allocator& __alloc)206: flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont) {207_LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(),208"flat_map keys and mapped containers have different size");209__sort_and_unique();210}211212template <class _Allocator>213requires __allocator_ctor_constraint<_Allocator>214_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26215flat_map(const key_container_type& __key_cont,216const mapped_container_type& __mapped_cont,217const key_compare& __comp,218const _Allocator& __alloc)219: flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont, __comp) {220_LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(),221"flat_map keys and mapped containers have different size");222__sort_and_unique();223}224225_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26226flat_map(sorted_unique_t,227key_container_type __key_cont,228mapped_container_type __mapped_cont,229const key_compare& __comp = key_compare())230: __containers_{.keys = std::move(__key_cont), .values = std::move(__mapped_cont)}, __compare_(__comp) {231_LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(),232"flat_map keys and mapped containers have different size");233_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(234__is_sorted_and_unique(__containers_.keys), "Either the key container is not sorted or it contains duplicates");235}236237template <class _Allocator>238requires __allocator_ctor_constraint<_Allocator>239_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26240flat_map(sorted_unique_t,241const key_container_type& __key_cont,242const mapped_container_type& __mapped_cont,243const _Allocator& __alloc)244: flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont) {245_LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(),246"flat_map keys and mapped containers have different size");247_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(248__is_sorted_and_unique(__containers_.keys), "Either the key container is not sorted or it contains duplicates");249}250251template <class _Allocator>252requires __allocator_ctor_constraint<_Allocator>253_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(254sorted_unique_t,255const key_container_type& __key_cont,256const mapped_container_type& __mapped_cont,257const key_compare& __comp,258const _Allocator& __alloc)259: flat_map(__ctor_uses_allocator_tag{}, __alloc, __key_cont, __mapped_cont, __comp) {260_LIBCPP_ASSERT_VALID_INPUT_RANGE(__containers_.keys.size() == __containers_.values.size(),261"flat_map keys and mapped containers have different size");262_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(263__is_sorted_and_unique(__containers_.keys), "Either the key container is not sorted or it contains duplicates");264}265266_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit flat_map(const key_compare& __comp)267: __containers_(), __compare_(__comp) {}268269template <class _Allocator>270requires __allocator_ctor_constraint<_Allocator>271_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(const key_compare& __comp, const _Allocator& __alloc)272: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) {}273274template <class _Allocator>275requires __allocator_ctor_constraint<_Allocator>276_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit flat_map(const _Allocator& __alloc)277: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {}278279template <class _InputIterator>280requires __has_input_iterator_category<_InputIterator>::value281_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26282flat_map(_InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())283: __containers_(), __compare_(__comp) {284insert(__first, __last);285}286287template <class _InputIterator, class _Allocator>288requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>)289_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26290flat_map(_InputIterator __first, _InputIterator __last, const key_compare& __comp, const _Allocator& __alloc)291: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) {292insert(__first, __last);293}294295template <class _InputIterator, class _Allocator>296requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>)297_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26298flat_map(_InputIterator __first, _InputIterator __last, const _Allocator& __alloc)299: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {300insert(__first, __last);301}302303template <_ContainerCompatibleRange<value_type> _Range>304_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(from_range_t __fr, _Range&& __rg)305: flat_map(__fr, std::forward<_Range>(__rg), key_compare()) {}306307template <_ContainerCompatibleRange<value_type> _Range, class _Allocator>308requires __allocator_ctor_constraint<_Allocator>309_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(from_range_t, _Range&& __rg, const _Allocator& __alloc)310: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {311insert_range(std::forward<_Range>(__rg));312}313314template <_ContainerCompatibleRange<value_type> _Range>315_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(from_range_t, _Range&& __rg, const key_compare& __comp)316: flat_map(__comp) {317insert_range(std::forward<_Range>(__rg));318}319320template <_ContainerCompatibleRange<value_type> _Range, class _Allocator>321requires __allocator_ctor_constraint<_Allocator>322_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26323flat_map(from_range_t, _Range&& __rg, const key_compare& __comp, const _Allocator& __alloc)324: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) {325insert_range(std::forward<_Range>(__rg));326}327328template <class _InputIterator>329requires __has_input_iterator_category<_InputIterator>::value330_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26331flat_map(sorted_unique_t, _InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())332: __containers_(), __compare_(__comp) {333insert(sorted_unique, __first, __last);334}335template <class _InputIterator, class _Allocator>336requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>)337_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(338sorted_unique_t,339_InputIterator __first,340_InputIterator __last,341const key_compare& __comp,342const _Allocator& __alloc)343: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc, __comp) {344insert(sorted_unique, __first, __last);345}346347template <class _InputIterator, class _Allocator>348requires(__has_input_iterator_category<_InputIterator>::value && __allocator_ctor_constraint<_Allocator>)349_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26350flat_map(sorted_unique_t, _InputIterator __first, _InputIterator __last, const _Allocator& __alloc)351: flat_map(__ctor_uses_allocator_empty_tag{}, __alloc) {352insert(sorted_unique, __first, __last);353}354355_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26356flat_map(initializer_list<value_type> __il, const key_compare& __comp = key_compare())357: flat_map(__il.begin(), __il.end(), __comp) {}358359template <class _Allocator>360requires __allocator_ctor_constraint<_Allocator>361_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26362flat_map(initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)363: flat_map(__il.begin(), __il.end(), __comp, __alloc) {}364365template <class _Allocator>366requires __allocator_ctor_constraint<_Allocator>367_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26368flat_map(initializer_list<value_type> __il, const _Allocator& __alloc)369: flat_map(__il.begin(), __il.end(), __alloc) {}370371_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26372flat_map(sorted_unique_t, initializer_list<value_type> __il, const key_compare& __comp = key_compare())373: flat_map(sorted_unique, __il.begin(), __il.end(), __comp) {}374375template <class _Allocator>376requires __allocator_ctor_constraint<_Allocator>377_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26378flat_map(sorted_unique_t, initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)379: flat_map(sorted_unique, __il.begin(), __il.end(), __comp, __alloc) {}380381template <class _Allocator>382requires __allocator_ctor_constraint<_Allocator>383_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26384flat_map(sorted_unique_t, initializer_list<value_type> __il, const _Allocator& __alloc)385: flat_map(sorted_unique, __il.begin(), __il.end(), __alloc) {}386387_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map& operator=(initializer_list<value_type> __il) {388clear();389insert(__il);390return *this;391}392393_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map& operator=(const flat_map&) = default;394395_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map& operator=(flat_map&& __other) noexcept(396is_nothrow_move_assignable_v<_KeyContainer> && is_nothrow_move_assignable_v<_MappedContainer> &&397is_nothrow_move_assignable_v<_Compare>) {398// No matter what happens, we always want to clear the other container before returning399// since we moved from it400auto __clear_other_guard = std::__make_scope_guard([&]() noexcept { __other.clear() /* noexcept */; });401{402// If an exception is thrown, we have no choice but to clear *this to preserve invariants403auto __on_exception = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });404__containers_ = std::move(__other.__containers_);405__compare_ = std::move(__other.__compare_);406__on_exception.__complete();407}408return *this;409}410411// iterators412_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator begin() noexcept {413return iterator(__containers_.keys.begin(), __containers_.values.begin());414}415416_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator begin() const noexcept {417return const_iterator(__containers_.keys.begin(), __containers_.values.begin());418}419420_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator end() noexcept {421return iterator(__containers_.keys.end(), __containers_.values.end());422}423424_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator end() const noexcept {425return const_iterator(__containers_.keys.end(), __containers_.values.end());426}427428_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 reverse_iterator rbegin() noexcept {429return reverse_iterator(end());430}431_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator rbegin() const noexcept {432return const_reverse_iterator(end());433}434_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 reverse_iterator rend() noexcept {435return reverse_iterator(begin());436}437_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator rend() const noexcept {438return const_reverse_iterator(begin());439}440441_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator cbegin() const noexcept { return begin(); }442_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator cend() const noexcept { return end(); }443_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator crbegin() const noexcept {444return const_reverse_iterator(end());445}446_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator crend() const noexcept {447return const_reverse_iterator(begin());448}449450// [flat.map.capacity], capacity451[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool empty() const noexcept {452return __containers_.keys.empty();453}454455_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type size() const noexcept {456return __containers_.keys.size();457}458459_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type max_size() const noexcept {460return std::min<size_type>(__containers_.keys.max_size(), __containers_.values.max_size());461}462463// [flat.map.access], element access464_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](const key_type& __x)465requires is_constructible_v<mapped_type>466{467return try_emplace(__x).first->second;468}469470_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](key_type&& __x)471requires is_constructible_v<mapped_type>472{473return try_emplace(std::move(__x)).first->second;474}475476template <class _Kp>477requires(__is_compare_transparent && is_constructible_v<key_type, _Kp> && is_constructible_v<mapped_type> &&478!is_convertible_v<_Kp &&, const_iterator> && !is_convertible_v<_Kp &&, iterator>)479_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& operator[](_Kp&& __x) {480return try_emplace(std::forward<_Kp>(__x)).first->second;481}482483_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& at(const key_type& __x) {484auto __it = find(__x);485if (__it == end()) {486std::__throw_out_of_range("flat_map::at(const key_type&): Key does not exist");487}488return __it->second;489}490491_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const mapped_type& at(const key_type& __x) const {492auto __it = find(__x);493if (__it == end()) {494std::__throw_out_of_range("flat_map::at(const key_type&) const: Key does not exist");495}496return __it->second;497}498499template <class _Kp>500requires __is_compare_transparent501_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 mapped_type& at(const _Kp& __x) {502auto __it = find(__x);503if (__it == end()) {504std::__throw_out_of_range("flat_map::at(const K&): Key does not exist");505}506return __it->second;507}508509template <class _Kp>510requires __is_compare_transparent511_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const mapped_type& at(const _Kp& __x) const {512auto __it = find(__x);513if (__it == end()) {514std::__throw_out_of_range("flat_map::at(const K&) const: Key does not exist");515}516return __it->second;517}518519// [flat.map.modifiers], modifiers520template <class... _Args>521requires is_constructible_v<pair<key_type, mapped_type>, _Args...>522_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> emplace(_Args&&... __args) {523std::pair<key_type, mapped_type> __pair(std::forward<_Args>(__args)...);524return __try_emplace(std::move(__pair.first), std::move(__pair.second));525}526527template <class... _Args>528requires is_constructible_v<pair<key_type, mapped_type>, _Args...>529_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator emplace_hint(const_iterator __hint, _Args&&... __args) {530std::pair<key_type, mapped_type> __pair(std::forward<_Args>(__args)...);531return __try_emplace_hint(__hint, std::move(__pair.first), std::move(__pair.second)).first;532}533534_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> insert(const value_type& __x) {535return emplace(__x);536}537538_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> insert(value_type&& __x) {539return emplace(std::move(__x));540}541542_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator insert(const_iterator __hint, const value_type& __x) {543return emplace_hint(__hint, __x);544}545546_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator insert(const_iterator __hint, value_type&& __x) {547return emplace_hint(__hint, std::move(__x));548}549550template <class _PairLike>551requires is_constructible_v<pair<key_type, mapped_type>, _PairLike>552_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> insert(_PairLike&& __x) {553return emplace(std::forward<_PairLike>(__x));554}555556template <class _PairLike>557requires is_constructible_v<pair<key_type, mapped_type>, _PairLike>558_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator insert(const_iterator __hint, _PairLike&& __x) {559return emplace_hint(__hint, std::forward<_PairLike>(__x));560}561562template <class _InputIterator>563requires __has_input_iterator_category<_InputIterator>::value564_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert(_InputIterator __first, _InputIterator __last) {565if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {566__reserve(__last - __first);567}568__append_sort_merge_unique</*WasSorted = */ false>(std::move(__first), std::move(__last));569}570571template <class _InputIterator>572requires __has_input_iterator_category<_InputIterator>::value573_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void574insert(sorted_unique_t, _InputIterator __first, _InputIterator __last) {575if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {576__reserve(__last - __first);577}578579__append_sort_merge_unique</*WasSorted = */ true>(std::move(__first), std::move(__last));580}581582template <_ContainerCompatibleRange<value_type> _Range>583_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert_range(_Range&& __range) {584if constexpr (ranges::sized_range<_Range>) {585__reserve(ranges::size(__range));586}587588__append_sort_merge_unique</*WasSorted = */ false>(ranges::begin(__range), ranges::end(__range));589}590591_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert(initializer_list<value_type> __il) {592insert(__il.begin(), __il.end());593}594595_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert(sorted_unique_t, initializer_list<value_type> __il) {596insert(sorted_unique, __il.begin(), __il.end());597}598599_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 containers extract() && {600auto __guard = std::__make_scope_guard([&]() noexcept { clear() /* noexcept */; });601auto __ret = std::move(__containers_);602return __ret;603}604605_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void606replace(key_container_type&& __key_cont, mapped_container_type&& __mapped_cont) {607_LIBCPP_ASSERT_VALID_INPUT_RANGE(608__key_cont.size() == __mapped_cont.size(), "flat_map keys and mapped containers have different size");609610_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(611__is_sorted_and_unique(__key_cont), "Either the key container is not sorted or it contains duplicates");612auto __guard = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });613__containers_.keys = std::move(__key_cont);614__containers_.values = std::move(__mapped_cont);615__guard.__complete();616}617618template <class... _Args>619requires is_constructible_v<mapped_type, _Args...>620_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>621try_emplace(const key_type& __key, _Args&&... __args) {622return __try_emplace(__key, std::forward<_Args>(__args)...);623}624625template <class... _Args>626requires is_constructible_v<mapped_type, _Args...>627_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>628try_emplace(key_type&& __key, _Args&&... __args) {629return __try_emplace(std::move(__key), std::forward<_Args>(__args)...);630}631632template <class _Kp, class... _Args>633requires(__is_compare_transparent && is_constructible_v<key_type, _Kp> &&634is_constructible_v<mapped_type, _Args...> && !is_convertible_v<_Kp &&, const_iterator> &&635!is_convertible_v<_Kp &&, iterator>)636_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> try_emplace(_Kp&& __key, _Args&&... __args) {637return __try_emplace(std::forward<_Kp>(__key), std::forward<_Args>(__args)...);638}639640template <class... _Args>641requires is_constructible_v<mapped_type, _Args...>642_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator643try_emplace(const_iterator __hint, const key_type& __key, _Args&&... __args) {644return __try_emplace_hint(__hint, __key, std::forward<_Args>(__args)...).first;645}646647template <class... _Args>648requires is_constructible_v<mapped_type, _Args...>649_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator650try_emplace(const_iterator __hint, key_type&& __key, _Args&&... __args) {651return __try_emplace_hint(__hint, std::move(__key), std::forward<_Args>(__args)...).first;652}653654template <class _Kp, class... _Args>655requires __is_compare_transparent && is_constructible_v<key_type, _Kp> && is_constructible_v<mapped_type, _Args...>656_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator657try_emplace(const_iterator __hint, _Kp&& __key, _Args&&... __args) {658return __try_emplace_hint(__hint, std::forward<_Kp>(__key), std::forward<_Args>(__args)...).first;659}660661template <class _Mapped>662requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>663_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>664insert_or_assign(const key_type& __key, _Mapped&& __obj) {665return __insert_or_assign(__key, std::forward<_Mapped>(__obj));666}667668template <class _Mapped>669requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>670_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>671insert_or_assign(key_type&& __key, _Mapped&& __obj) {672return __insert_or_assign(std::move(__key), std::forward<_Mapped>(__obj));673}674675template <class _Kp, class _Mapped>676requires __is_compare_transparent && is_constructible_v<key_type, _Kp> && is_assignable_v<mapped_type&, _Mapped> &&677is_constructible_v<mapped_type, _Mapped>678_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>679insert_or_assign(_Kp&& __key, _Mapped&& __obj) {680return __insert_or_assign(std::forward<_Kp>(__key), std::forward<_Mapped>(__obj));681}682683template <class _Mapped>684requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>685_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator686insert_or_assign(const_iterator __hint, const key_type& __key, _Mapped&& __obj) {687return __insert_or_assign(__hint, __key, std::forward<_Mapped>(__obj));688}689690template <class _Mapped>691requires is_assignable_v<mapped_type&, _Mapped> && is_constructible_v<mapped_type, _Mapped>692_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator693insert_or_assign(const_iterator __hint, key_type&& __key, _Mapped&& __obj) {694return __insert_or_assign(__hint, std::move(__key), std::forward<_Mapped>(__obj));695}696697template <class _Kp, class _Mapped>698requires __is_compare_transparent && is_constructible_v<key_type, _Kp> && is_assignable_v<mapped_type&, _Mapped> &&699is_constructible_v<mapped_type, _Mapped>700_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator701insert_or_assign(const_iterator __hint, _Kp&& __key, _Mapped&& __obj) {702return __insert_or_assign(__hint, std::forward<_Kp>(__key), std::forward<_Mapped>(__obj));703}704705_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator erase(iterator __position) {706return __erase(__position.__key_iter_, __position.__mapped_iter_);707}708709_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator erase(const_iterator __position) {710return __erase(__position.__key_iter_, __position.__mapped_iter_);711}712713_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type erase(const key_type& __x) {714auto __iter = find(__x);715if (__iter != end()) {716erase(__iter);717return 1;718}719return 0;720}721722template <class _Kp>723requires(__is_compare_transparent && !is_convertible_v<_Kp &&, iterator> &&724!is_convertible_v<_Kp &&, const_iterator>)725_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type erase(_Kp&& __x) {726auto [__first, __last] = equal_range(__x);727auto __res = __last - __first;728erase(__first, __last);729return __res;730}731732_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator erase(const_iterator __first, const_iterator __last) {733auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });734auto __key_it = __containers_.keys.erase(__first.__key_iter_, __last.__key_iter_);735auto __mapped_it = __containers_.values.erase(__first.__mapped_iter_, __last.__mapped_iter_);736__on_failure.__complete();737return iterator(std::move(__key_it), std::move(__mapped_it));738}739740_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void swap(flat_map& __y) noexcept {741// warning: The spec has unconditional noexcept, which means that742// if any of the following functions throw an exception,743// std::terminate will be called.744// This is discussed in P2767, which hasn't been voted on yet.745ranges::swap(__compare_, __y.__compare_);746ranges::swap(__containers_.keys, __y.__containers_.keys);747ranges::swap(__containers_.values, __y.__containers_.values);748}749750_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void clear() noexcept {751__containers_.keys.clear();752__containers_.values.clear();753}754755// observers756_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 key_compare key_comp() const { return __compare_; }757_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 value_compare value_comp() const {758return value_compare(__compare_);759}760761_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const key_container_type& keys() const noexcept {762return __containers_.keys;763}764_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const mapped_container_type& values() const noexcept {765return __containers_.values;766}767768// map operations769_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator find(const key_type& __x) {770return __find_impl(*this, __x);771}772773_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator find(const key_type& __x) const {774return __find_impl(*this, __x);775}776777template <class _Kp>778requires __is_compare_transparent779_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator find(const _Kp& __x) {780return __find_impl(*this, __x);781}782783template <class _Kp>784requires __is_compare_transparent785_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator find(const _Kp& __x) const {786return __find_impl(*this, __x);787}788789_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type count(const key_type& __x) const {790return contains(__x) ? 1 : 0;791}792793template <class _Kp>794requires __is_compare_transparent795_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type count(const _Kp& __x) const {796return contains(__x) ? 1 : 0;797}798799_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool contains(const key_type& __x) const {800return find(__x) != end();801}802803template <class _Kp>804requires __is_compare_transparent805_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool contains(const _Kp& __x) const {806return find(__x) != end();807}808809_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator lower_bound(const key_type& __x) {810return __lower_bound<iterator>(*this, __x);811}812813_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator lower_bound(const key_type& __x) const {814return __lower_bound<const_iterator>(*this, __x);815}816817template <class _Kp>818requires __is_compare_transparent819_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator lower_bound(const _Kp& __x) {820return __lower_bound<iterator>(*this, __x);821}822823template <class _Kp>824requires __is_compare_transparent825_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator lower_bound(const _Kp& __x) const {826return __lower_bound<const_iterator>(*this, __x);827}828829_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator upper_bound(const key_type& __x) {830return __upper_bound<iterator>(*this, __x);831}832833_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator upper_bound(const key_type& __x) const {834return __upper_bound<const_iterator>(*this, __x);835}836837template <class _Kp>838requires __is_compare_transparent839_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator upper_bound(const _Kp& __x) {840return __upper_bound<iterator>(*this, __x);841}842843template <class _Kp>844requires __is_compare_transparent845_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator upper_bound(const _Kp& __x) const {846return __upper_bound<const_iterator>(*this, __x);847}848849_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, iterator> equal_range(const key_type& __x) {850return __equal_range_impl(*this, __x);851}852853_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<const_iterator, const_iterator>854equal_range(const key_type& __x) const {855return __equal_range_impl(*this, __x);856}857858template <class _Kp>859requires __is_compare_transparent860_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, iterator> equal_range(const _Kp& __x) {861return __equal_range_impl(*this, __x);862}863template <class _Kp>864requires __is_compare_transparent865_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<const_iterator, const_iterator>866equal_range(const _Kp& __x) const {867return __equal_range_impl(*this, __x);868}869870friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool operator==(const flat_map& __x, const flat_map& __y) {871return ranges::equal(__x, __y);872}873874friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 auto875operator<=>(const flat_map& __x, const flat_map& __y) {876return std::lexicographical_compare_three_way(877__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);878}879880friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void swap(flat_map& __x, flat_map& __y) noexcept {881__x.swap(__y);882}883884private:885struct __ctor_uses_allocator_tag {886explicit _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __ctor_uses_allocator_tag() = default;887};888struct __ctor_uses_allocator_empty_tag {889explicit _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __ctor_uses_allocator_empty_tag() = default;890};891892template <class _Allocator, class _KeyCont, class _MappedCont, class... _CompArg>893requires __allocator_ctor_constraint<_Allocator>894_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_map(895__ctor_uses_allocator_tag,896const _Allocator& __alloc,897_KeyCont&& __key_cont,898_MappedCont&& __mapped_cont,899_CompArg&&... __comp)900: __containers_{.keys = std::make_obj_using_allocator<key_container_type>(901__alloc, std::forward<_KeyCont>(__key_cont)),902.values = std::make_obj_using_allocator<mapped_container_type>(903__alloc, std::forward<_MappedCont>(__mapped_cont))},904__compare_(std::forward<_CompArg>(__comp)...) {}905906template <class _Allocator, class... _CompArg>907requires __allocator_ctor_constraint<_Allocator>908_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26909flat_map(__ctor_uses_allocator_empty_tag, const _Allocator& __alloc, _CompArg&&... __comp)910: __containers_{.keys = std::make_obj_using_allocator<key_container_type>(__alloc),911.values = std::make_obj_using_allocator<mapped_container_type>(__alloc)},912__compare_(std::forward<_CompArg>(__comp)...) {}913914_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __is_sorted_and_unique(auto&& __key_container) const {915auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) { return !__compare_(__x, __y); };916return ranges::adjacent_find(__key_container, __greater_or_equal_to) == ranges::end(__key_container);917}918919// This function is only used in constructors. So there is not exception handling in this function.920// If the function exits via an exception, there will be no flat_map object constructed, thus, there921// is no invariant state to preserve922_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __sort_and_unique() {923auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);924ranges::sort(__zv, __compare_, [](const auto& __p) -> decltype(auto) { return std::get<0>(__p); });925auto __dup_start = ranges::unique(__zv, __key_equiv(__compare_)).begin();926auto __dist = ranges::distance(__zv.begin(), __dup_start);927__containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end());928__containers_.values.erase(__containers_.values.begin() + __dist, __containers_.values.end());929}930931template <class _Self, class _KeyIter>932_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto933__corresponding_mapped_it(_Self&& __self, _KeyIter&& __key_iter) {934return __self.__containers_.values.begin() +935static_cast<ranges::range_difference_t<mapped_container_type>>(936ranges::distance(__self.__containers_.keys.begin(), __key_iter));937}938939template <bool _WasSorted, class _InputIterator, class _Sentinel>940_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void941__append_sort_merge_unique(_InputIterator __first, _Sentinel __last) {942auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });943size_t __num_of_appended = __flat_map_utils::__append(*this, std::move(__first), std::move(__last));944if (__num_of_appended != 0) {945auto __zv = ranges::views::zip(__containers_.keys, __containers_.values);946auto __append_start_offset = __containers_.keys.size() - __num_of_appended;947auto __end = __zv.end();948auto __compare_key = [this](const auto& __p1, const auto& __p2) {949return __compare_(std::get<0>(__p1), std::get<0>(__p2));950};951if constexpr (!_WasSorted) {952ranges::sort(__zv.begin() + __append_start_offset, __end, __compare_key);953} else {954_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(955__is_sorted_and_unique(__containers_.keys | ranges::views::drop(__append_start_offset)),956"Either the key container is not sorted or it contains duplicates");957}958ranges::inplace_merge(__zv.begin(), __zv.begin() + __append_start_offset, __end, __compare_key);959960auto __dup_start = ranges::unique(__zv, __key_equiv(__compare_)).begin();961auto __dist = ranges::distance(__zv.begin(), __dup_start);962__containers_.keys.erase(__containers_.keys.begin() + __dist, __containers_.keys.end());963__containers_.values.erase(__containers_.values.begin() + __dist, __containers_.values.end());964}965__on_failure.__complete();966}967968template <class _Self, class _Kp>969_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto __find_impl(_Self&& __self, const _Kp& __key) {970auto __it = __self.lower_bound(__key);971auto __last = __self.end();972if (__it == __last || __self.__compare_(__key, __it->first)) {973return __last;974}975return __it;976}977978template <class _Self, class _Kp>979_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto __key_equal_range(_Self&& __self, const _Kp& __key) {980auto __it =981std::lower_bound(__self.__containers_.keys.begin(), __self.__containers_.keys.end(), __key, __self.__compare_);982auto __last = __self.__containers_.keys.end();983if (__it == __last || __self.__compare_(__key, *__it)) {984return std::make_pair(__it, __it);985}986return std::make_pair(__it, std::next(__it));987}988989template <class _Self, class _Kp>990_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto __equal_range_impl(_Self&& __self, const _Kp& __key) {991auto [__key_first, __key_last] = __key_equal_range(__self, __key);992using __iterator_type = ranges::iterator_t<decltype(__self)>;993return std::make_pair(__iterator_type(__key_first, __corresponding_mapped_it(__self, __key_first)),994__iterator_type(__key_last, __corresponding_mapped_it(__self, __key_last)));995}996997template <class _Res, class _Self, class _Kp>998_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static _Res __lower_bound(_Self&& __self, _Kp& __x) {999auto __key_iter =1000std::lower_bound(__self.__containers_.keys.begin(), __self.__containers_.keys.end(), __x, __self.__compare_);1001auto __mapped_iter = __corresponding_mapped_it(__self, __key_iter);1002return _Res(std::move(__key_iter), std::move(__mapped_iter));1003}10041005template <class _Res, class _Self, class _Kp>1006_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static _Res __upper_bound(_Self&& __self, _Kp& __x) {1007auto __key_iter =1008std::upper_bound(__self.__containers_.keys.begin(), __self.__containers_.keys.end(), __x, __self.__compare_);1009auto __mapped_iter = __corresponding_mapped_it(__self, __key_iter);1010return _Res(std::move(__key_iter), std::move(__mapped_iter));1011}10121013template <class _KeyArg, class... _MArgs>1014_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>1015__try_emplace(_KeyArg&& __key, _MArgs&&... __mapped_args) {1016auto __key_it = std::lower_bound(__containers_.keys.begin(), __containers_.keys.end(), __key, __compare_);1017auto __mapped_it = __containers_.values.begin() + ranges::distance(__containers_.keys.begin(), __key_it);10181019if (__key_it == __containers_.keys.end() || __compare_(__key, *__key_it)) {1020return pair<iterator, bool>(1021__flat_map_utils::__emplace_exact_pos(1022*this,1023std::move(__key_it),1024std::move(__mapped_it),1025std::forward<_KeyArg>(__key),1026std::forward<_MArgs>(__mapped_args)...),1027true);1028} else {1029return pair<iterator, bool>(iterator(std::move(__key_it), std::move(__mapped_it)), false);1030}1031}10321033template <class _Kp>1034_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __is_hint_correct(const_iterator __hint, _Kp&& __key) {1035if (__hint != cbegin() && !__compare_((__hint - 1)->first, __key)) {1036return false;1037}1038if (__hint != cend() && __compare_(__hint->first, __key)) {1039return false;1040}1041return true;1042}10431044template <class _Kp, class... _Args>1045_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>1046__try_emplace_hint(const_iterator __hint, _Kp&& __key, _Args&&... __args) {1047if (__is_hint_correct(__hint, __key)) {1048if (__hint == cend() || __compare_(__key, __hint->first)) {1049return {__flat_map_utils::__emplace_exact_pos(1050*this,1051__hint.__key_iter_,1052__hint.__mapped_iter_,1053std::forward<_Kp>(__key),1054std::forward<_Args>(__args)...),1055true};1056} else {1057// key equals1058auto __dist = __hint - cbegin();1059return {iterator(__containers_.keys.begin() + __dist, __containers_.values.begin() + __dist), false};1060}1061} else {1062return __try_emplace(std::forward<_Kp>(__key), std::forward<_Args>(__args)...);1063}1064}10651066template <class _Kp, class _Mapped>1067_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool>1068__insert_or_assign(_Kp&& __key, _Mapped&& __mapped) {1069auto __r = try_emplace(std::forward<_Kp>(__key), std::forward<_Mapped>(__mapped));1070if (!__r.second) {1071__r.first->second = std::forward<_Mapped>(__mapped);1072}1073return __r;1074}10751076template <class _Kp, class _Mapped>1077_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator1078__insert_or_assign(const_iterator __hint, _Kp&& __key, _Mapped&& __mapped) {1079auto __r = __try_emplace_hint(__hint, std::forward<_Kp>(__key), std::forward<_Mapped>(__mapped));1080if (!__r.second) {1081__r.first->second = std::forward<_Mapped>(__mapped);1082}1083return __r.first;1084}10851086_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __reserve(size_t __size) {1087if constexpr (__container_traits<_KeyContainer>::__reservable) {1088__containers_.keys.reserve(__size);1089}10901091if constexpr (__container_traits<_MappedContainer>::__reservable) {1092__containers_.values.reserve(__size);1093}1094}10951096template <class _KIter, class _MIter>1097_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator1098__erase(_KIter __key_iter_to_remove, _MIter __mapped_iter_to_remove) {1099auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });1100auto __key_iter = __containers_.keys.erase(__key_iter_to_remove);1101auto __mapped_iter = __containers_.values.erase(__mapped_iter_to_remove);1102__on_failure.__complete();1103return iterator(std::move(__key_iter), std::move(__mapped_iter));1104}11051106template <class _Key2, class _Tp2, class _Compare2, class _KeyContainer2, class _MappedContainer2, class _Predicate>1107friend typename flat_map<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>::size_type1108_LIBCPP_CONSTEXPR_SINCE_CXX261109erase_if(flat_map<_Key2, _Tp2, _Compare2, _KeyContainer2, _MappedContainer2>&, _Predicate);11101111friend __flat_map_utils;11121113containers __containers_;1114_LIBCPP_NO_UNIQUE_ADDRESS key_compare __compare_;11151116struct __key_equiv {1117_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __key_equiv(key_compare __c) : __comp_(__c) {}1118_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool1119operator()(const_reference __x, const_reference __y) const {1120return !__comp_(std::get<0>(__x), std::get<0>(__y)) && !__comp_(std::get<0>(__y), std::get<0>(__x));1121}1122key_compare __comp_;1123};1124};11251126template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>1127requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&1128!__is_allocator<_MappedContainer>::value &&1129is_invocable_v<const _Compare&,1130const typename _KeyContainer::value_type&,1131const typename _KeyContainer::value_type&>)1132flat_map(_KeyContainer, _MappedContainer, _Compare = _Compare())1133-> flat_map<typename _KeyContainer::value_type,1134typename _MappedContainer::value_type,1135_Compare,1136_KeyContainer,1137_MappedContainer>;11381139template <class _KeyContainer, class _MappedContainer, class _Allocator>1140requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> &&1141!__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value)1142flat_map(_KeyContainer, _MappedContainer, _Allocator)1143-> flat_map<typename _KeyContainer::value_type,1144typename _MappedContainer::value_type,1145less<typename _KeyContainer::value_type>,1146_KeyContainer,1147_MappedContainer>;11481149template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>1150requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&1151!__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> &&1152uses_allocator_v<_MappedContainer, _Allocator> &&1153is_invocable_v<const _Compare&,1154const typename _KeyContainer::value_type&,1155const typename _KeyContainer::value_type&>)1156flat_map(_KeyContainer, _MappedContainer, _Compare, _Allocator)1157-> flat_map<typename _KeyContainer::value_type,1158typename _MappedContainer::value_type,1159_Compare,1160_KeyContainer,1161_MappedContainer>;11621163template <class _KeyContainer, class _MappedContainer, class _Compare = less<typename _KeyContainer::value_type>>1164requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&1165!__is_allocator<_MappedContainer>::value &&1166is_invocable_v<const _Compare&,1167const typename _KeyContainer::value_type&,1168const typename _KeyContainer::value_type&>)1169flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare = _Compare())1170-> flat_map<typename _KeyContainer::value_type,1171typename _MappedContainer::value_type,1172_Compare,1173_KeyContainer,1174_MappedContainer>;11751176template <class _KeyContainer, class _MappedContainer, class _Allocator>1177requires(uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator> &&1178!__is_allocator<_KeyContainer>::value && !__is_allocator<_MappedContainer>::value)1179flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Allocator)1180-> flat_map<typename _KeyContainer::value_type,1181typename _MappedContainer::value_type,1182less<typename _KeyContainer::value_type>,1183_KeyContainer,1184_MappedContainer>;11851186template <class _KeyContainer, class _MappedContainer, class _Compare, class _Allocator>1187requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&1188!__is_allocator<_MappedContainer>::value && uses_allocator_v<_KeyContainer, _Allocator> &&1189uses_allocator_v<_MappedContainer, _Allocator> &&1190is_invocable_v<const _Compare&,1191const typename _KeyContainer::value_type&,1192const typename _KeyContainer::value_type&>)1193flat_map(sorted_unique_t, _KeyContainer, _MappedContainer, _Compare, _Allocator)1194-> flat_map<typename _KeyContainer::value_type,1195typename _MappedContainer::value_type,1196_Compare,1197_KeyContainer,1198_MappedContainer>;11991200template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>1201requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)1202flat_map(_InputIterator, _InputIterator, _Compare = _Compare())1203-> flat_map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;12041205template <class _InputIterator, class _Compare = less<__iter_key_type<_InputIterator>>>1206requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)1207flat_map(sorted_unique_t, _InputIterator, _InputIterator, _Compare = _Compare())1208-> flat_map<__iter_key_type<_InputIterator>, __iter_mapped_type<_InputIterator>, _Compare>;12091210template <ranges::input_range _Range,1211class _Compare = less<__range_key_type<_Range>>,1212class _Allocator = allocator<byte>,1213class = __enable_if_t<!__is_allocator<_Compare>::value && __is_allocator<_Allocator>::value>>1214flat_map(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator()) -> flat_map<1215__range_key_type<_Range>,1216__range_mapped_type<_Range>,1217_Compare,1218vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>,1219vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>;12201221template <ranges::input_range _Range, class _Allocator, class = __enable_if_t<__is_allocator<_Allocator>::value>>1222flat_map(from_range_t, _Range&&, _Allocator) -> flat_map<1223__range_key_type<_Range>,1224__range_mapped_type<_Range>,1225less<__range_key_type<_Range>>,1226vector<__range_key_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_key_type<_Range>>>,1227vector<__range_mapped_type<_Range>, __allocator_traits_rebind_t<_Allocator, __range_mapped_type<_Range>>>>;12281229template <class _Key, class _Tp, class _Compare = less<_Key>>1230requires(!__is_allocator<_Compare>::value)1231flat_map(initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>;12321233template <class _Key, class _Tp, class _Compare = less<_Key>>1234requires(!__is_allocator<_Compare>::value)1235flat_map(sorted_unique_t, initializer_list<pair<_Key, _Tp>>, _Compare = _Compare()) -> flat_map<_Key, _Tp, _Compare>;12361237template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Allocator>1238struct uses_allocator<flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>, _Allocator>1239: bool_constant<uses_allocator_v<_KeyContainer, _Allocator> && uses_allocator_v<_MappedContainer, _Allocator>> {};12401241template <class _Key, class _Tp, class _Compare, class _KeyContainer, class _MappedContainer, class _Predicate>1242_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX261243typename flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::size_type1244erase_if(flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>& __flat_map, _Predicate __pred) {1245auto __zv = ranges::views::zip(__flat_map.__containers_.keys, __flat_map.__containers_.values);1246auto __first = __zv.begin();1247auto __last = __zv.end();1248auto __guard = std::__make_exception_guard([&] { __flat_map.clear(); });1249auto __it = std::remove_if(__first, __last, [&](auto&& __zipped) -> bool {1250using _Ref = typename flat_map<_Key, _Tp, _Compare, _KeyContainer, _MappedContainer>::const_reference;1251return __pred(_Ref(std::get<0>(__zipped), std::get<1>(__zipped)));1252});1253auto __res = __last - __it;1254auto __offset = __it - __first;12551256const auto __erase_container = [&](auto& __cont) { __cont.erase(__cont.begin() + __offset, __cont.end()); };12571258__erase_container(__flat_map.__containers_.keys);1259__erase_container(__flat_map.__containers_.values);12601261__guard.__complete();1262return __res;1263}12641265_LIBCPP_END_NAMESPACE_STD12661267#endif // _LIBCPP_STD_VER >= 2312681269_LIBCPP_POP_MACROS12701271#endif // _LIBCPP___FLAT_MAP_FLAT_MAP_H127212731274