1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___FLAT_SET_UTILS_H 11 #define _LIBCPP___FLAT_SET_UTILS_H 12 13 #include <__config> 14 #include <__iterator/iterator_traits.h> 15 #include <__ranges/access.h> 16 #include <__ranges/concepts.h> 17 #include <__type_traits/container_traits.h> 18 #include <__type_traits/decay.h> 19 #include <__utility/exception_guard.h> 20 #include <__utility/forward.h> 21 #include <__utility/move.h> 22 23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 24 # pragma GCC system_header 25 #endif 26 27 _LIBCPP_PUSH_MACROS 28 #include <__undef_macros> 29 30 #if _LIBCPP_STD_VER >= 23 31 32 _LIBCPP_BEGIN_NAMESPACE_STD 33 34 // These utilities are defined in a class instead of a namespace so that this class can be befriended more easily. 35 struct __flat_set_utils { 36 // Emplace a key into a flat_{multi}set, at the exact position that 37 // __it point to, assuming that the key is not already present in the set. 38 // When an exception is thrown during the emplacement, the function will clear the set if the container does not 39 // have strong exception safety guarantee on emplacement. 40 template <class _Set, class _Iter, class _KeyArg> 41 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static auto __emplace_exact_pos__flat_set_utils42 __emplace_exact_pos(_Set& __set, _Iter&& __iter, _KeyArg&& __key) { 43 using _KeyContainer = typename decay_t<_Set>::container_type; 44 auto __on_failure = std::__make_exception_guard([&]() noexcept { 45 if constexpr (!__container_traits<_KeyContainer>::__emplacement_has_strong_exception_safety_guarantee) { 46 __set.clear() /* noexcept */; 47 } 48 }); 49 auto __key_it = __set.__keys_.emplace(__iter.__base(), std::forward<_KeyArg>(__key)); 50 __on_failure.__complete(); 51 return typename decay_t<_Set>::iterator(std::move(__key_it)); 52 } 53 54 template <class _Set, class _InputIterator> 55 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static void __append__flat_set_utils56 __append(_Set& __set, _InputIterator __first, _InputIterator __last) { 57 __set.__keys_.insert(__set.__keys_.end(), std::move(__first), std::move(__last)); 58 } 59 60 template <class _Set, class _Range> __append__flat_set_utils61 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static void __append(_Set& __set, _Range&& __rng) { 62 if constexpr (requires { __set.__keys_.insert_range(__set.__keys_.end(), std::forward<_Range>(__rng)); }) { 63 // C++23 Sequence Container should have insert_range member function 64 // Note that not all Sequence Containers provide append_range. 65 __set.__keys_.insert_range(__set.__keys_.end(), std::forward<_Range>(__rng)); 66 } else if constexpr (ranges::common_range<_Range> && 67 __has_input_iterator_category<ranges::iterator_t<_Range>>::value) { 68 __set.__keys_.insert(__set.__keys_.end(), ranges::begin(__rng), ranges::end(__rng)); 69 } else { 70 for (auto&& __x : __rng) { 71 __set.__keys_.insert(__set.__keys_.end(), std::forward<decltype(__x)>(__x)); 72 } 73 } 74 } 75 }; 76 _LIBCPP_END_NAMESPACE_STD 77 78 #endif // _LIBCPP_STD_VER >= 23 79 80 _LIBCPP_POP_MACROS 81 82 #endif // #define _LIBCPP___FLAT_SET_UTILS_H 83