Path: blob/main/contrib/llvm-project/libcxx/include/__flat_map/utils.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_UTILS_H10#define _LIBCPP___FLAT_MAP_UTILS_H1112#include <__config>13#include <__iterator/product_iterator.h>14#include <__type_traits/container_traits.h>15#include <__utility/exception_guard.h>16#include <__utility/forward.h>17#include <__utility/move.h>1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223_LIBCPP_PUSH_MACROS24#include <__undef_macros>2526#if _LIBCPP_STD_VER >= 232728_LIBCPP_BEGIN_NAMESPACE_STD2930// These utilities are defined in a class instead of a namespace so that this class can be befriended more easily.31struct __flat_map_utils {32// Emplace a {key: value} into a flat_{multi}map, at the exact position that33// __it_key and __it_mapped point to, assuming that the key is not already present in the map.34// When an exception is thrown during the emplacement, the function will try its best to35// roll back the changes it made to the map. If it cannot roll back the changes, it will36// clear the map.37template <class _Map, class _IterK, class _IterM, class _KeyArg, class... _MArgs>38_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static typename _Map::iterator __emplace_exact_pos(39_Map& __map, _IterK&& __it_key, _IterM&& __it_mapped, _KeyArg&& __key, _MArgs&&... __mapped_args) {40auto __on_key_failed = std::__make_exception_guard([&]() noexcept {41using _KeyContainer = typename _Map::key_container_type;42if constexpr (__container_traits<_KeyContainer>::__emplacement_has_strong_exception_safety_guarantee) {43// Nothing to roll back!44} else {45// we need to clear both because we don't know the state of our keys anymore46__map.clear() /* noexcept */;47}48});49auto __key_it = __map.__containers_.keys.emplace(__it_key, std::forward<_KeyArg>(__key));50__on_key_failed.__complete();5152auto __on_value_failed = std::__make_exception_guard([&]() noexcept {53using _MappedContainer = typename _Map::mapped_container_type;54if constexpr (!__container_traits<_MappedContainer>::__emplacement_has_strong_exception_safety_guarantee) {55// we need to clear both because we don't know the state of our values anymore56__map.clear() /* noexcept */;57} else {58// In this case, we know the values are just like before we attempted emplacement,59// and we also know that the keys have been emplaced successfully. Just roll back the keys.60# if _LIBCPP_HAS_EXCEPTIONS61try {62# endif // _LIBCPP_HAS_EXCEPTIONS63__map.__containers_.keys.erase(__key_it);64# if _LIBCPP_HAS_EXCEPTIONS65} catch (...) {66// Now things are funky for real. We're failing to rollback the keys.67// Just give up and clear the whole thing.68//69// Also, swallow the exception that happened during the rollback and let the70// original value-emplacement exception propagate normally.71__map.clear() /* noexcept */;72}73# endif // _LIBCPP_HAS_EXCEPTIONS74}75});76auto __mapped_it = __map.__containers_.values.emplace(__it_mapped, std::forward<_MArgs>(__mapped_args)...);77__on_value_failed.__complete();7879return typename _Map::iterator(std::move(__key_it), std::move(__mapped_it));80}8182template <class _Map, class _InputIterator, class _Sentinel>83_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static typename _Map::size_type84__append(_Map& __map, _InputIterator __first, _Sentinel __last) {85typename _Map::size_type __num_appended = 0;86for (; __first != __last; ++__first) {87typename _Map::value_type __kv = *__first;88__map.__containers_.keys.insert(__map.__containers_.keys.end(), std::move(__kv.first));89__map.__containers_.values.insert(__map.__containers_.values.end(), std::move(__kv.second));90++__num_appended;91}92return __num_appended;93}9495template <class _Map, class _InputIterator>96_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static typename _Map::size_type97__append(_Map& __map, _InputIterator __first, _InputIterator __last)98requires __is_product_iterator_of_size<_InputIterator, 2>::value99{100auto __s1 = __map.__containers_.keys.size();101__map.__containers_.keys.insert(102__map.__containers_.keys.end(),103__product_iterator_traits<_InputIterator>::template __get_iterator_element<0>(__first),104__product_iterator_traits<_InputIterator>::template __get_iterator_element<0>(__last));105106__map.__containers_.values.insert(107__map.__containers_.values.end(),108__product_iterator_traits<_InputIterator>::template __get_iterator_element<1>(__first),109__product_iterator_traits<_InputIterator>::template __get_iterator_element<1>(__last));110111return __map.__containers_.keys.size() - __s1;112}113};114_LIBCPP_END_NAMESPACE_STD115116#endif // _LIBCPP_STD_VER >= 23117118_LIBCPP_POP_MACROS119120#endif // #define _LIBCPP___FLAT_MAP_UTILS_H121122123