Path: blob/main/contrib/llvm-project/libcxx/include/__flat_set/flat_set.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_SET_FLAT_SET_H10#define _LIBCPP___FLAT_SET_FLAT_SET_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/ptrdiff_t.h>27#include <__flat_map/sorted_unique.h>28#include <__flat_set/ra_iterator.h>29#include <__flat_set/utils.h>30#include <__functional/invoke.h>31#include <__functional/is_transparent.h>32#include <__functional/operations.h>33#include <__fwd/vector.h>34#include <__iterator/concepts.h>35#include <__iterator/distance.h>36#include <__iterator/iterator_traits.h>37#include <__iterator/next.h>38#include <__iterator/prev.h>39#include <__iterator/ranges_iterator_traits.h>40#include <__iterator/reverse_iterator.h>41#include <__memory/allocator_traits.h>42#include <__memory/uses_allocator.h>43#include <__memory/uses_allocator_construction.h>44#include <__ranges/access.h>45#include <__ranges/concepts.h>46#include <__ranges/container_compatible_range.h>47#include <__ranges/drop_view.h>48#include <__ranges/from_range.h>49#include <__ranges/ref_view.h>50#include <__ranges/size.h>51#include <__ranges/subrange.h>52#include <__type_traits/conjunction.h>53#include <__type_traits/container_traits.h>54#include <__type_traits/invoke.h>55#include <__type_traits/is_allocator.h>56#include <__type_traits/is_const.h>57#include <__type_traits/is_nothrow_constructible.h>58#include <__type_traits/is_same.h>59#include <__type_traits/remove_reference.h>60#include <__utility/as_const.h>61#include <__utility/exception_guard.h>62#include <__utility/move.h>63#include <__utility/pair.h>64#include <__utility/scope_guard.h>65#include <__vector/vector.h>66#include <initializer_list>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, class _Compare = less<_Key>, class _KeyContainer = vector<_Key>>80class flat_set {81template <class, class, class>82friend class flat_set;8384friend __flat_set_utils;8586static_assert(is_same_v<_Key, typename _KeyContainer::value_type>);87static_assert(!is_same_v<_KeyContainer, std::vector<bool>>, "vector<bool> is not a sequence container");8889using __key_iterator _LIBCPP_NODEBUG = typename _KeyContainer::const_iterator;9091public:92// types93using key_type = _Key;94using value_type = _Key;95using key_compare = __type_identity_t<_Compare>;96using value_compare = _Compare;97using reference = value_type&;98using const_reference = const value_type&;99using size_type = typename _KeyContainer::size_type;100using difference_type = typename _KeyContainer::difference_type;101using iterator = __ra_iterator<flat_set, typename _KeyContainer::const_iterator>;102using const_iterator = iterator;103using reverse_iterator = std::reverse_iterator<iterator>;104using const_reverse_iterator = std::reverse_iterator<const_iterator>;105using container_type = _KeyContainer;106107public:108// [flat.set.cons], construct/copy/destroy109_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26110flat_set() noexcept(is_nothrow_default_constructible_v<_KeyContainer> && is_nothrow_default_constructible_v<_Compare>)111: __keys_(), __compare_() {}112113_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(const flat_set&) = default;114115_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(flat_set&& __other) noexcept(116is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_Compare>)117# if _LIBCPP_HAS_EXCEPTIONS118try119# endif // _LIBCPP_HAS_EXCEPTIONS120: __keys_(std::move(__other.__keys_)), __compare_(std::move(__other.__compare_)) {121__other.clear();122# if _LIBCPP_HAS_EXCEPTIONS123} catch (...) {124__other.clear();125// gcc does not like the `throw` keyword in a conditionally noexcept function126if constexpr (!(is_nothrow_move_constructible_v<_KeyContainer> && is_nothrow_move_constructible_v<_Compare>)) {127throw;128}129# endif // _LIBCPP_HAS_EXCEPTIONS130}131132_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit flat_set(const key_compare& __comp)133: __keys_(), __compare_(__comp) {}134135_LIBCPP_HIDE_FROM_ABI136_LIBCPP_CONSTEXPR_SINCE_CXX26 explicit flat_set(container_type __keys, const key_compare& __comp = key_compare())137: __keys_(std::move(__keys)), __compare_(__comp) {138__sort_and_unique();139}140141_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26142flat_set(sorted_unique_t, container_type __keys, const key_compare& __comp = key_compare())143: __keys_(std::move(__keys)), __compare_(__comp) {144_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(145__is_sorted_and_unique(__keys_), "Either the key container is not sorted or it contains duplicates");146}147148template <class _InputIterator>149requires __has_input_iterator_category<_InputIterator>::value150_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26151flat_set(_InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())152: __keys_(), __compare_(__comp) {153insert(__first, __last);154}155156template <class _InputIterator>157requires __has_input_iterator_category<_InputIterator>::value158_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26159flat_set(sorted_unique_t, _InputIterator __first, _InputIterator __last, const key_compare& __comp = key_compare())160: __keys_(__first, __last), __compare_(__comp) {161_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(162__is_sorted_and_unique(__keys_), "Either the key container is not sorted or it contains duplicates");163}164165template <_ContainerCompatibleRange<value_type> _Range>166_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(from_range_t, _Range&& __rg)167: flat_set(from_range, std::forward<_Range>(__rg), key_compare()) {}168169template <_ContainerCompatibleRange<value_type> _Range>170_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(from_range_t, _Range&& __rg, const key_compare& __comp)171: flat_set(__comp) {172insert_range(std::forward<_Range>(__rg));173}174175_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26176flat_set(initializer_list<value_type> __il, const key_compare& __comp = key_compare())177: flat_set(__il.begin(), __il.end(), __comp) {}178179_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26180flat_set(sorted_unique_t, initializer_list<value_type> __il, const key_compare& __comp = key_compare())181: flat_set(sorted_unique, __il.begin(), __il.end(), __comp) {}182183template <class _Allocator>184requires uses_allocator<container_type, _Allocator>::value185_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 explicit flat_set(const _Allocator& __alloc)186: __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_() {}187188template <class _Allocator>189requires uses_allocator<container_type, _Allocator>::value190_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(const key_compare& __comp, const _Allocator& __alloc)191: __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_(__comp) {}192193template <class _Allocator>194requires uses_allocator<container_type, _Allocator>::value195_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(const container_type& __keys, const _Allocator& __alloc)196: __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_() {197__sort_and_unique();198}199200template <class _Allocator>201requires uses_allocator<container_type, _Allocator>::value202_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26203flat_set(const container_type& __keys, const key_compare& __comp, const _Allocator& __alloc)204: __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_(__comp) {205__sort_and_unique();206}207208template <class _Allocator>209requires uses_allocator<container_type, _Allocator>::value210_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26211flat_set(sorted_unique_t, const container_type& __keys, const _Allocator& __alloc)212: __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_() {213_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(214__is_sorted_and_unique(__keys_), "Either the key container is not sorted or it contains duplicates");215}216217template <class _Allocator>218requires uses_allocator<container_type, _Allocator>::value219_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26220flat_set(sorted_unique_t, const container_type& __keys, const key_compare& __comp, const _Allocator& __alloc)221: __keys_(std::make_obj_using_allocator<container_type>(__alloc, __keys)), __compare_(__comp) {222_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(223__is_sorted_and_unique(__keys_), "Either the key container is not sorted or it contains duplicates");224}225226template <class _Allocator>227requires uses_allocator<container_type, _Allocator>::value228_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(const flat_set& __other, const _Allocator& __alloc)229: __keys_(std::make_obj_using_allocator<container_type>(__alloc, __other.__keys_)),230__compare_(__other.__compare_) {}231232template <class _Allocator>233requires uses_allocator<container_type, _Allocator>::value234_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(flat_set&& __other, const _Allocator& __alloc)235# if _LIBCPP_HAS_EXCEPTIONS236try237# endif // _LIBCPP_HAS_EXCEPTIONS238: __keys_(std::make_obj_using_allocator<container_type>(__alloc, std::move(__other.__keys_))),239__compare_(std::move(__other.__compare_)) {240__other.clear();241# if _LIBCPP_HAS_EXCEPTIONS242} catch (...) {243__other.clear();244throw;245# endif // _LIBCPP_HAS_EXCEPTIONS246}247248template <class _InputIterator, class _Allocator>249requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)250_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26251flat_set(_InputIterator __first, _InputIterator __last, const _Allocator& __alloc)252: __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_() {253insert(__first, __last);254}255256template <class _InputIterator, class _Allocator>257requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)258_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26259flat_set(_InputIterator __first, _InputIterator __last, const key_compare& __comp, const _Allocator& __alloc)260: __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_(__comp) {261insert(__first, __last);262}263264template <class _InputIterator, class _Allocator>265requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)266_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26267flat_set(sorted_unique_t, _InputIterator __first, _InputIterator __last, const _Allocator& __alloc)268: __keys_(std::make_obj_using_allocator<container_type>(__alloc, __first, __last)), __compare_() {269_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(270__is_sorted_and_unique(__keys_), "Either the key container is not sorted or it contains duplicates");271}272273template <class _InputIterator, class _Allocator>274requires(__has_input_iterator_category<_InputIterator>::value && uses_allocator<container_type, _Allocator>::value)275_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(276sorted_unique_t,277_InputIterator __first,278_InputIterator __last,279const key_compare& __comp,280const _Allocator& __alloc)281: __keys_(std::make_obj_using_allocator<container_type>(__alloc, __first, __last)), __compare_(__comp) {282_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(283__is_sorted_and_unique(__keys_), "Either the key container is not sorted or it contains duplicates");284}285286template <_ContainerCompatibleRange<value_type> _Range, class _Allocator>287requires uses_allocator<container_type, _Allocator>::value288_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set(from_range_t, _Range&& __rg, const _Allocator& __alloc)289: __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_() {290insert_range(std::forward<_Range>(__rg));291}292293template <_ContainerCompatibleRange<value_type> _Range, class _Allocator>294requires uses_allocator<container_type, _Allocator>::value295_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26296flat_set(from_range_t, _Range&& __rg, const key_compare& __comp, const _Allocator& __alloc)297: __keys_(std::make_obj_using_allocator<container_type>(__alloc)), __compare_(__comp) {298insert_range(std::forward<_Range>(__rg));299}300301template <class _Allocator>302requires uses_allocator<container_type, _Allocator>::value303_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26304flat_set(initializer_list<value_type> __il, const _Allocator& __alloc)305: flat_set(__il.begin(), __il.end(), __alloc) {}306307template <class _Allocator>308requires uses_allocator<container_type, _Allocator>::value309_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26310flat_set(initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)311: flat_set(__il.begin(), __il.end(), __comp, __alloc) {}312313template <class _Allocator>314requires uses_allocator<container_type, _Allocator>::value315_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26316flat_set(sorted_unique_t, initializer_list<value_type> __il, const _Allocator& __alloc)317: flat_set(sorted_unique, __il.begin(), __il.end(), __alloc) {}318319template <class _Allocator>320requires uses_allocator<container_type, _Allocator>::value321_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26322flat_set(sorted_unique_t, initializer_list<value_type> __il, const key_compare& __comp, const _Allocator& __alloc)323: flat_set(sorted_unique, __il.begin(), __il.end(), __comp, __alloc) {}324325_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set& operator=(initializer_list<value_type> __il) {326clear();327insert(__il);328return *this;329}330331_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set& operator=(const flat_set&) = default;332333_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 flat_set& operator=(flat_set&& __other) noexcept(334is_nothrow_move_assignable_v<_KeyContainer> && is_nothrow_move_assignable_v<_Compare>) {335// No matter what happens, we always want to clear the other container before returning336// since we moved from it337auto __clear_other_guard = std::__make_scope_guard([&]() noexcept { __other.clear() /* noexcept */; });338{339// If an exception is thrown, we have no choice but to clear *this to preserve invariants340auto __on_exception = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });341__keys_ = std::move(__other.__keys_);342__compare_ = std::move(__other.__compare_);343__on_exception.__complete();344}345return *this;346}347348// iterators349_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator begin() noexcept {350return iterator(std::as_const(__keys_).begin());351}352_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator begin() const noexcept {353return const_iterator(__keys_.begin());354}355_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator end() noexcept {356return iterator(std::as_const(__keys_).end());357}358_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator end() const noexcept {359return const_iterator(__keys_.end());360}361362_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 reverse_iterator rbegin() noexcept {363return reverse_iterator(end());364}365_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator rbegin() const noexcept {366return const_reverse_iterator(end());367}368_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 reverse_iterator rend() noexcept {369return reverse_iterator(begin());370}371_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator rend() const noexcept {372return const_reverse_iterator(begin());373}374375_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator cbegin() const noexcept { return begin(); }376_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator cend() const noexcept { return end(); }377_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator crbegin() const noexcept {378return const_reverse_iterator(end());379}380_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_reverse_iterator crend() const noexcept {381return const_reverse_iterator(begin());382}383384// [flat.set.capacity], capacity385[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool empty() const noexcept {386return __keys_.empty();387}388389_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type size() const noexcept { return __keys_.size(); }390391_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type max_size() const noexcept { return __keys_.max_size(); }392393// [flat.set.modifiers], modifiers394template <class... _Args>395_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> emplace(_Args&&... __args) {396if constexpr (sizeof...(__args) == 1 && (is_same_v<remove_cvref_t<_Args>, _Key> && ...)) {397return __emplace(std::forward<_Args>(__args)...);398} else {399return __emplace(_Key(std::forward<_Args>(__args)...));400}401}402403template <class... _Args>404_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator emplace_hint(const_iterator __hint, _Args&&... __args) {405if constexpr (sizeof...(__args) == 1 && (is_same_v<remove_cvref_t<_Args>, _Key> && ...)) {406return __emplace_hint(std::move(__hint), std::forward<_Args>(__args)...);407} else {408return __emplace_hint(std::move(__hint), _Key(std::forward<_Args>(__args)...));409}410}411412_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> insert(const value_type& __x) {413return emplace(__x);414}415416_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> insert(value_type&& __x) {417return emplace(std::move(__x));418}419420template <class _Kp>421requires(__is_transparent_v<_Compare> && is_constructible_v<value_type, _Kp>)422_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> insert(_Kp&& __x) {423return __emplace(std::forward<_Kp>(__x));424}425_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator insert(const_iterator __hint, const value_type& __x) {426return emplace_hint(__hint, __x);427}428429_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator insert(const_iterator __hint, value_type&& __x) {430return emplace_hint(__hint, std::move(__x));431}432433template <class _Kp>434requires(__is_transparent_v<_Compare> && is_constructible_v<value_type, _Kp>)435_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator insert(const_iterator __hint, _Kp&& __x) {436return __emplace_hint(__hint, std::forward<_Kp>(__x));437}438439template <class _InputIterator>440requires __has_input_iterator_category<_InputIterator>::value441_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert(_InputIterator __first, _InputIterator __last) {442if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {443__reserve(__last - __first);444}445__append_sort_merge_unique</*WasSorted = */ false>(std::move(__first), std::move(__last));446}447448template <class _InputIterator>449requires __has_input_iterator_category<_InputIterator>::value450_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void451insert(sorted_unique_t, _InputIterator __first, _InputIterator __last) {452if constexpr (sized_sentinel_for<_InputIterator, _InputIterator>) {453__reserve(__last - __first);454}455456__append_sort_merge_unique</*WasSorted = */ true>(std::move(__first), std::move(__last));457}458459template <_ContainerCompatibleRange<value_type> _Range>460_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert_range(_Range&& __range) {461if constexpr (ranges::sized_range<_Range>) {462__reserve(ranges::size(__range));463}464465__append_sort_merge_unique</*WasSorted = */ false>(std::forward<_Range>(__range));466}467468_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert(initializer_list<value_type> __il) {469insert(__il.begin(), __il.end());470}471472_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void insert(sorted_unique_t, initializer_list<value_type> __il) {473insert(sorted_unique, __il.begin(), __il.end());474}475476_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 container_type extract() && {477auto __guard = std::__make_scope_guard([&]() noexcept { clear() /* noexcept */; });478auto __ret = std::move(__keys_);479return __ret;480}481482_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void replace(container_type&& __keys) {483_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(484__is_sorted_and_unique(__keys), "Either the key container is not sorted or it contains duplicates");485auto __guard = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });486__keys_ = std::move(__keys);487__guard.__complete();488}489490_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator erase(iterator __position) {491auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });492auto __key_iter = __keys_.erase(__position.__base());493__on_failure.__complete();494return iterator(__key_iter);495}496497// The following overload is the same as the iterator overload498// iterator erase(const_iterator __position);499500_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type erase(const key_type& __x) {501auto __iter = find(__x);502if (__iter != end()) {503erase(__iter);504return 1;505}506return 0;507}508509template <class _Kp>510requires(__is_transparent_v<_Compare> && !is_convertible_v<_Kp &&, iterator> &&511!is_convertible_v<_Kp &&, const_iterator>)512_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type erase(_Kp&& __x) {513auto [__first, __last] = equal_range(__x);514auto __res = __last - __first;515erase(__first, __last);516return __res;517}518519_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator erase(const_iterator __first, const_iterator __last) {520auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });521auto __key_it = __keys_.erase(__first.__base(), __last.__base());522__on_failure.__complete();523return iterator(std::move(__key_it));524}525526_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void swap(flat_set& __y) noexcept {527// warning: The spec has unconditional noexcept, which means that528// if any of the following functions throw an exception,529// std::terminate will be called.530// This is discussed in P2767, which hasn't been voted on yet.531ranges::swap(__compare_, __y.__compare_);532ranges::swap(__keys_, __y.__keys_);533}534535_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void clear() noexcept { __keys_.clear(); }536537// observers538_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 key_compare key_comp() const { return __compare_; }539_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 value_compare value_comp() const { return __compare_; }540541// set operations542_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator find(const key_type& __x) {543return __find_impl(*this, __x);544}545546_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator find(const key_type& __x) const {547return __find_impl(*this, __x);548}549550template <class _Kp>551requires __is_transparent_v<_Compare>552_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator find(const _Kp& __x) {553return __find_impl(*this, __x);554}555556template <class _Kp>557requires __is_transparent_v<_Compare>558_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator find(const _Kp& __x) const {559return __find_impl(*this, __x);560}561562_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type count(const key_type& __x) const {563return contains(__x) ? 1 : 0;564}565566template <class _Kp>567requires __is_transparent_v<_Compare>568_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 size_type count(const _Kp& __x) const {569return contains(__x) ? 1 : 0;570}571572_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool contains(const key_type& __x) const {573return find(__x) != end();574}575576template <class _Kp>577requires __is_transparent_v<_Compare>578_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool contains(const _Kp& __x) const {579return find(__x) != end();580}581582_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator lower_bound(const key_type& __x) {583const auto& __keys = __keys_;584return iterator(std::lower_bound(__keys.begin(), __keys.end(), __x, __compare_));585}586587_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator lower_bound(const key_type& __x) const {588return const_iterator(std::lower_bound(__keys_.begin(), __keys_.end(), __x, __compare_));589}590591template <class _Kp>592requires __is_transparent_v<_Compare>593_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator lower_bound(const _Kp& __x) {594const auto& __keys = __keys_;595return iterator(std::lower_bound(__keys.begin(), __keys.end(), __x, __compare_));596}597598template <class _Kp>599requires __is_transparent_v<_Compare>600_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator lower_bound(const _Kp& __x) const {601return const_iterator(std::lower_bound(__keys_.begin(), __keys_.end(), __x, __compare_));602}603604_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator upper_bound(const key_type& __x) {605const auto& __keys = __keys_;606return iterator(std::upper_bound(__keys.begin(), __keys.end(), __x, __compare_));607}608609_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator upper_bound(const key_type& __x) const {610return const_iterator(std::upper_bound(__keys_.begin(), __keys_.end(), __x, __compare_));611}612613template <class _Kp>614requires __is_transparent_v<_Compare>615_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator upper_bound(const _Kp& __x) {616const auto& __keys = __keys_;617return iterator(std::upper_bound(__keys.begin(), __keys.end(), __x, __compare_));618}619620template <class _Kp>621requires __is_transparent_v<_Compare>622_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 const_iterator upper_bound(const _Kp& __x) const {623return const_iterator(std::upper_bound(__keys_.begin(), __keys_.end(), __x, __compare_));624}625626_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, iterator> equal_range(const key_type& __x) {627return __equal_range_impl(*this, __x);628}629630_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<const_iterator, const_iterator>631equal_range(const key_type& __x) const {632return __equal_range_impl(*this, __x);633}634635template <class _Kp>636requires __is_transparent_v<_Compare>637_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, iterator> equal_range(const _Kp& __x) {638return __equal_range_impl(*this, __x);639}640template <class _Kp>641requires __is_transparent_v<_Compare>642_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<const_iterator, const_iterator>643equal_range(const _Kp& __x) const {644return __equal_range_impl(*this, __x);645}646647friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool operator==(const flat_set& __x, const flat_set& __y) {648return ranges::equal(__x, __y);649}650651friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 auto652operator<=>(const flat_set& __x, const flat_set& __y) {653return std::lexicographical_compare_three_way(654__x.begin(), __x.end(), __y.begin(), __y.end(), std::__synth_three_way);655}656657friend _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void swap(flat_set& __x, flat_set& __y) noexcept {658__x.swap(__y);659}660661private:662_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __is_sorted_and_unique(auto&& __key_container) const {663auto __greater_or_equal_to = [this](const auto& __x, const auto& __y) { return !__compare_(__x, __y); };664return ranges::adjacent_find(__key_container, __greater_or_equal_to) == ranges::end(__key_container);665}666667// This function is only used in constructors. So there is not exception handling in this function.668// If the function exits via an exception, there will be no flat_set object constructed, thus, there669// is no invariant state to preserve670_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __sort_and_unique() {671ranges::sort(__keys_, __compare_);672auto __dup_start = ranges::unique(__keys_, __key_equiv(__compare_)).begin();673__keys_.erase(__dup_start, __keys_.end());674}675676template <bool _WasSorted, class... _Args>677_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __append_sort_merge_unique(_Args&&... __args) {678auto __on_failure = std::__make_exception_guard([&]() noexcept { clear() /* noexcept */; });679size_type __old_size = size();680__flat_set_utils::__append(*this, std::forward<_Args>(__args)...);681if (size() != __old_size) {682if constexpr (!_WasSorted) {683ranges::sort(__keys_.begin() + __old_size, __keys_.end(), __compare_);684} else {685_LIBCPP_ASSERT_SEMANTIC_REQUIREMENT(__is_sorted_and_unique(__keys_ | ranges::views::drop(__old_size)),686"Either the key container is not sorted or it contains duplicates");687}688ranges::inplace_merge(__keys_.begin(), __keys_.begin() + __old_size, __keys_.end(), __compare_);689690auto __dup_start = ranges::unique(__keys_, __key_equiv(__compare_)).begin();691__keys_.erase(__dup_start, __keys_.end());692}693__on_failure.__complete();694}695696template <class _Self, class _Kp>697_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto __find_impl(_Self&& __self, const _Kp& __key) {698auto __it = __self.lower_bound(__key);699auto __last = __self.end();700if (__it == __last || __self.__compare_(__key, *__it)) {701return __last;702}703return __it;704}705706template <class _Self, class _Kp>707_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto __equal_range_impl(_Self&& __self, const _Kp& __key) {708using __iter = _If<is_const_v<__libcpp_remove_reference_t<_Self>>, const_iterator, iterator>;709auto __it = std::lower_bound(__self.__keys_.begin(), __self.__keys_.end(), __key, __self.__compare_);710auto __last = __self.__keys_.end();711if (__it == __last || __self.__compare_(__key, *__it)) {712return std::make_pair(__iter(__it), __iter(__it));713}714return std::make_pair(__iter(__it), __iter(std::next(__it)));715}716717template <class _Kp>718_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 pair<iterator, bool> __emplace(_Kp&& __key) {719auto __it = lower_bound(__key);720if (__it == end() || __compare_(__key, *__it)) {721return pair<iterator, bool>(__flat_set_utils::__emplace_exact_pos(*this, __it, std::forward<_Kp>(__key)), true);722} else {723return pair<iterator, bool>(std::move(__it), false);724}725}726727template <class _Kp>728_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool __is_hint_correct(const_iterator __hint, _Kp&& __key) {729if (__hint != cbegin() && !__compare_(*std::prev(__hint), __key)) {730return false;731}732if (__hint != cend() && __compare_(*__hint, __key)) {733return false;734}735return true;736}737738template <class _Kp>739_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 iterator __emplace_hint(const_iterator __hint, _Kp&& __key) {740if (__is_hint_correct(__hint, __key)) {741if (__hint == cend() || __compare_(__key, *__hint)) {742return __flat_set_utils::__emplace_exact_pos(*this, __hint, std::forward<_Kp>(__key));743} else {744// we already have an equal key745return __hint;746}747} else {748return __emplace(std::forward<_Kp>(__key)).first;749}750}751752_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 void __reserve(size_t __size) {753if constexpr (__container_traits<_KeyContainer>::__reservable) {754__keys_.reserve(__size);755}756}757758template <class _Key2, class _Compare2, class _KeyContainer2, class _Predicate>759friend typename flat_set<_Key2, _Compare2, _KeyContainer2>::size_type _LIBCPP_CONSTEXPR_SINCE_CXX26760erase_if(flat_set<_Key2, _Compare2, _KeyContainer2>&, _Predicate);761762_KeyContainer __keys_;763_LIBCPP_NO_UNIQUE_ADDRESS key_compare __compare_;764765struct __key_equiv {766_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 __key_equiv(key_compare __c) : __comp_(__c) {}767_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 bool768operator()(const_reference __x, const_reference __y) const {769return !__comp_(__x, __y) && !__comp_(__y, __x);770}771key_compare __comp_;772};773};774775template <class _KeyContainer, class _Compare = less<typename _KeyContainer::value_type>>776requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&777is_invocable_v<const _Compare&,778const typename _KeyContainer::value_type&,779const typename _KeyContainer::value_type&>)780flat_set(_KeyContainer, _Compare = _Compare()) -> flat_set<typename _KeyContainer::value_type, _Compare, _KeyContainer>;781782template <class _KeyContainer, class _Allocator>783requires(uses_allocator_v<_KeyContainer, _Allocator> && !__is_allocator<_KeyContainer>::value)784flat_set(_KeyContainer, _Allocator)785-> flat_set<typename _KeyContainer::value_type, less<typename _KeyContainer::value_type>, _KeyContainer>;786787template <class _KeyContainer, class _Compare, class _Allocator>788requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&789uses_allocator_v<_KeyContainer, _Allocator> &&790is_invocable_v<const _Compare&,791const typename _KeyContainer::value_type&,792const typename _KeyContainer::value_type&>)793flat_set(_KeyContainer, _Compare, _Allocator) -> flat_set<typename _KeyContainer::value_type, _Compare, _KeyContainer>;794795template <class _KeyContainer, class _Compare = less<typename _KeyContainer::value_type>>796requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&797is_invocable_v<const _Compare&,798const typename _KeyContainer::value_type&,799const typename _KeyContainer::value_type&>)800flat_set(sorted_unique_t, _KeyContainer, _Compare = _Compare())801-> flat_set<typename _KeyContainer::value_type, _Compare, _KeyContainer>;802803template <class _KeyContainer, class _Allocator>804requires(uses_allocator_v<_KeyContainer, _Allocator> && !__is_allocator<_KeyContainer>::value)805flat_set(sorted_unique_t, _KeyContainer, _Allocator)806-> flat_set<typename _KeyContainer::value_type, less<typename _KeyContainer::value_type>, _KeyContainer>;807808template <class _KeyContainer, class _Compare, class _Allocator>809requires(!__is_allocator<_Compare>::value && !__is_allocator<_KeyContainer>::value &&810uses_allocator_v<_KeyContainer, _Allocator> &&811is_invocable_v<const _Compare&,812const typename _KeyContainer::value_type&,813const typename _KeyContainer::value_type&>)814flat_set(sorted_unique_t, _KeyContainer, _Compare, _Allocator)815-> flat_set<typename _KeyContainer::value_type, _Compare, _KeyContainer>;816817template <class _InputIterator, class _Compare = less<__iter_value_type<_InputIterator>>>818requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)819flat_set(_InputIterator, _InputIterator, _Compare = _Compare())820-> flat_set<__iter_value_type<_InputIterator>, _Compare>;821822template <class _InputIterator, class _Compare = less<__iter_value_type<_InputIterator>>>823requires(__has_input_iterator_category<_InputIterator>::value && !__is_allocator<_Compare>::value)824flat_set(sorted_unique_t, _InputIterator, _InputIterator, _Compare = _Compare())825-> flat_set<__iter_value_type<_InputIterator>, _Compare>;826827template <ranges::input_range _Range,828class _Compare = less<ranges::range_value_t<_Range>>,829class _Allocator = allocator<ranges::range_value_t<_Range>>,830class = __enable_if_t<!__is_allocator<_Compare>::value && __is_allocator<_Allocator>::value>>831flat_set(from_range_t, _Range&&, _Compare = _Compare(), _Allocator = _Allocator()) -> flat_set<832ranges::range_value_t<_Range>,833_Compare,834vector<ranges::range_value_t<_Range>, __allocator_traits_rebind_t<_Allocator, ranges::range_value_t<_Range>>>>;835836template <ranges::input_range _Range, class _Allocator, class = __enable_if_t<__is_allocator<_Allocator>::value>>837flat_set(from_range_t, _Range&&, _Allocator) -> flat_set<838ranges::range_value_t<_Range>,839less<ranges::range_value_t<_Range>>,840vector<ranges::range_value_t<_Range>, __allocator_traits_rebind_t<_Allocator, ranges::range_value_t<_Range>>>>;841842template <class _Key, class _Compare = less<_Key>>843requires(!__is_allocator<_Compare>::value)844flat_set(initializer_list<_Key>, _Compare = _Compare()) -> flat_set<_Key, _Compare>;845846template <class _Key, class _Compare = less<_Key>>847requires(!__is_allocator<_Compare>::value)848flat_set(sorted_unique_t, initializer_list<_Key>, _Compare = _Compare()) -> flat_set<_Key, _Compare>;849850template <class _Key, class _Compare, class _KeyContainer, class _Allocator>851struct uses_allocator<flat_set<_Key, _Compare, _KeyContainer>, _Allocator>852: bool_constant<uses_allocator_v<_KeyContainer, _Allocator>> {};853854template <class _Key, class _Compare, class _KeyContainer, class _Predicate>855_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 typename flat_set<_Key, _Compare, _KeyContainer>::size_type856erase_if(flat_set<_Key, _Compare, _KeyContainer>& __flat_set, _Predicate __pred) {857auto __guard = std::__make_exception_guard([&] { __flat_set.clear(); });858auto __it = std::remove_if(__flat_set.__keys_.begin(), __flat_set.__keys_.end(), [&](const auto& __e) -> bool {859return static_cast<bool>(__pred(__e));860});861auto __res = __flat_set.__keys_.end() - __it;862__flat_set.__keys_.erase(__it, __flat_set.__keys_.end());863__guard.__complete();864return __res;865}866867_LIBCPP_END_NAMESPACE_STD868869#endif // _LIBCPP_STD_VER >= 23870871_LIBCPP_POP_MACROS872873#endif // _LIBCPP___FLAT_SET_FLAT_SET_H874875876