xref: /llvm-project/libcxx/include/__flat_map/utils.h (revision def50f701f6a2c1e0550bb341fd8b64bed299e72)
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_MAP_UTILS_H
11 #define _LIBCPP___FLAT_MAP_UTILS_H
12 
13 #include <__config>
14 #include <__type_traits/container_traits.h>
15 #include <__utility/exception_guard.h>
16 #include <__utility/forward.h>
17 #include <__utility/move.h>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #  pragma GCC system_header
21 #endif
22 
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
25 
26 #if _LIBCPP_STD_VER >= 23
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 // These utilities are defined in a class instead of a namespace so that this class can be befriended more easily.
31 struct __flat_map_utils {
32   // Emplace a {key: value} into a flat_{multi}map, at the exact position that
33   // __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 to
35   // roll back the changes it made to the map. If it cannot roll back the changes, it will
36   // clear the map.
37   template <class _Map, class _IterK, class _IterM, class _KeyArg, class... _MArgs>
38   _LIBCPP_HIDE_FROM_ABI static typename _Map::iterator __emplace_exact_pos(
39       _Map& __map, _IterK&& __it_key, _IterM&& __it_mapped, _KeyArg&& __key, _MArgs&&... __mapped_args) {
40     auto __on_key_failed = std::__make_exception_guard([&]() noexcept {
41       using _KeyContainer = typename _Map::key_container_type;
42       if 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 anymore
46         __map.clear() /* noexcept */;
47       }
48     });
49     auto __key_it        = __map.__containers_.keys.emplace(__it_key, std::forward<_KeyArg>(__key));
50     __on_key_failed.__complete();
51 
52     auto __on_value_failed = std::__make_exception_guard([&]() noexcept {
53       using _MappedContainer = typename _Map::mapped_container_type;
54       if 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 anymore
56         __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_EXCEPTIONS
61         try {
62 #  endif // _LIBCPP_HAS_EXCEPTIONS
63           __map.__containers_.keys.erase(__key_it);
64 #  if _LIBCPP_HAS_EXCEPTIONS
65         } 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 the
70           // original value-emplacement exception propagate normally.
71           __map.clear() /* noexcept */;
72         }
73 #  endif // _LIBCPP_HAS_EXCEPTIONS
74       }
75     });
76     auto __mapped_it = __map.__containers_.values.emplace(__it_mapped, std::forward<_MArgs>(__mapped_args)...);
77     __on_value_failed.__complete();
78 
79     return typename _Map::iterator(std::move(__key_it), std::move(__mapped_it));
80   }
81 
82   // TODO: We could optimize this, see
83   // https://github.com/llvm/llvm-project/issues/108624
84   template <class _Map, class _InputIterator, class _Sentinel>
85   _LIBCPP_HIDE_FROM_ABI static typename _Map::size_type
86   __append(_Map& __map, _InputIterator __first, _Sentinel __last) {
87     typename _Map::size_type __num_appended = 0;
88     for (; __first != __last; ++__first) {
89       typename _Map::value_type __kv = *__first;
90       __map.__containers_.keys.insert(__map.__containers_.keys.end(), std::move(__kv.first));
91       __map.__containers_.values.insert(__map.__containers_.values.end(), std::move(__kv.second));
92       ++__num_appended;
93     }
94     return __num_appended;
95   }
96 };
97 _LIBCPP_END_NAMESPACE_STD
98 
99 #endif // _LIBCPP_STD_VER >= 23
100 
101 _LIBCPP_POP_MACROS
102 
103 #endif // #define _LIBCPP___FLAT_MAP_UTILS_H
104