1*def50f70SHui // -*- C++ -*- 2*def50f70SHui //===----------------------------------------------------------------------===// 3*def50f70SHui // 4*def50f70SHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*def50f70SHui // See https://llvm.org/LICENSE.txt for license information. 6*def50f70SHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*def50f70SHui // 8*def50f70SHui //===----------------------------------------------------------------------===// 9*def50f70SHui 10*def50f70SHui #ifndef _LIBCPP___FLAT_MAP_UTILS_H 11*def50f70SHui #define _LIBCPP___FLAT_MAP_UTILS_H 12*def50f70SHui 13*def50f70SHui #include <__config> 14*def50f70SHui #include <__type_traits/container_traits.h> 15*def50f70SHui #include <__utility/exception_guard.h> 16*def50f70SHui #include <__utility/forward.h> 17*def50f70SHui #include <__utility/move.h> 18*def50f70SHui 19*def50f70SHui #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20*def50f70SHui # pragma GCC system_header 21*def50f70SHui #endif 22*def50f70SHui 23*def50f70SHui _LIBCPP_PUSH_MACROS 24*def50f70SHui #include <__undef_macros> 25*def50f70SHui 26*def50f70SHui #if _LIBCPP_STD_VER >= 23 27*def50f70SHui 28*def50f70SHui _LIBCPP_BEGIN_NAMESPACE_STD 29*def50f70SHui 30*def50f70SHui // These utilities are defined in a class instead of a namespace so that this class can be befriended more easily. 31*def50f70SHui struct __flat_map_utils { 32*def50f70SHui // Emplace a {key: value} into a flat_{multi}map, at the exact position that 33*def50f70SHui // __it_key and __it_mapped point to, assuming that the key is not already present in the map. 34*def50f70SHui // When an exception is thrown during the emplacement, the function will try its best to 35*def50f70SHui // roll back the changes it made to the map. If it cannot roll back the changes, it will 36*def50f70SHui // clear the map. 37*def50f70SHui template <class _Map, class _IterK, class _IterM, class _KeyArg, class... _MArgs> 38*def50f70SHui _LIBCPP_HIDE_FROM_ABI static typename _Map::iterator __emplace_exact_pos( 39*def50f70SHui _Map& __map, _IterK&& __it_key, _IterM&& __it_mapped, _KeyArg&& __key, _MArgs&&... __mapped_args) { 40*def50f70SHui auto __on_key_failed = std::__make_exception_guard([&]() noexcept { 41*def50f70SHui using _KeyContainer = typename _Map::key_container_type; 42*def50f70SHui if constexpr (__container_traits<_KeyContainer>::__emplacement_has_strong_exception_safety_guarantee) { 43*def50f70SHui // Nothing to roll back! 44*def50f70SHui } else { 45*def50f70SHui // we need to clear both because we don't know the state of our keys anymore 46*def50f70SHui __map.clear() /* noexcept */; 47*def50f70SHui } 48*def50f70SHui }); 49*def50f70SHui auto __key_it = __map.__containers_.keys.emplace(__it_key, std::forward<_KeyArg>(__key)); 50*def50f70SHui __on_key_failed.__complete(); 51*def50f70SHui 52*def50f70SHui auto __on_value_failed = std::__make_exception_guard([&]() noexcept { 53*def50f70SHui using _MappedContainer = typename _Map::mapped_container_type; 54*def50f70SHui if constexpr (!__container_traits<_MappedContainer>::__emplacement_has_strong_exception_safety_guarantee) { 55*def50f70SHui // we need to clear both because we don't know the state of our values anymore 56*def50f70SHui __map.clear() /* noexcept */; 57*def50f70SHui } else { 58*def50f70SHui // In this case, we know the values are just like before we attempted emplacement, 59*def50f70SHui // and we also know that the keys have been emplaced successfully. Just roll back the keys. 60*def50f70SHui # if _LIBCPP_HAS_EXCEPTIONS 61*def50f70SHui try { 62*def50f70SHui # endif // _LIBCPP_HAS_EXCEPTIONS 63*def50f70SHui __map.__containers_.keys.erase(__key_it); 64*def50f70SHui # if _LIBCPP_HAS_EXCEPTIONS 65*def50f70SHui } catch (...) { 66*def50f70SHui // Now things are funky for real. We're failing to rollback the keys. 67*def50f70SHui // Just give up and clear the whole thing. 68*def50f70SHui // 69*def50f70SHui // Also, swallow the exception that happened during the rollback and let the 70*def50f70SHui // original value-emplacement exception propagate normally. 71*def50f70SHui __map.clear() /* noexcept */; 72*def50f70SHui } 73*def50f70SHui # endif // _LIBCPP_HAS_EXCEPTIONS 74*def50f70SHui } 75*def50f70SHui }); 76*def50f70SHui auto __mapped_it = __map.__containers_.values.emplace(__it_mapped, std::forward<_MArgs>(__mapped_args)...); 77*def50f70SHui __on_value_failed.__complete(); 78*def50f70SHui 79*def50f70SHui return typename _Map::iterator(std::move(__key_it), std::move(__mapped_it)); 80*def50f70SHui } 81*def50f70SHui 82*def50f70SHui // TODO: We could optimize this, see 83*def50f70SHui // https://github.com/llvm/llvm-project/issues/108624 84*def50f70SHui template <class _Map, class _InputIterator, class _Sentinel> 85*def50f70SHui _LIBCPP_HIDE_FROM_ABI static typename _Map::size_type 86*def50f70SHui __append(_Map& __map, _InputIterator __first, _Sentinel __last) { 87*def50f70SHui typename _Map::size_type __num_appended = 0; 88*def50f70SHui for (; __first != __last; ++__first) { 89*def50f70SHui typename _Map::value_type __kv = *__first; 90*def50f70SHui __map.__containers_.keys.insert(__map.__containers_.keys.end(), std::move(__kv.first)); 91*def50f70SHui __map.__containers_.values.insert(__map.__containers_.values.end(), std::move(__kv.second)); 92*def50f70SHui ++__num_appended; 93*def50f70SHui } 94*def50f70SHui return __num_appended; 95*def50f70SHui } 96*def50f70SHui }; 97*def50f70SHui _LIBCPP_END_NAMESPACE_STD 98*def50f70SHui 99*def50f70SHui #endif // _LIBCPP_STD_VER >= 23 100*def50f70SHui 101*def50f70SHui _LIBCPP_POP_MACROS 102*def50f70SHui 103*def50f70SHui #endif // #define _LIBCPP___FLAT_MAP_UTILS_H 104