| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef _LIBCPP___FLAT_MAP_UTILS_H |
| #define _LIBCPP___FLAT_MAP_UTILS_H |
|
|
| #include <__config> |
| #include <__iterator/product_iterator.h> |
| #include <__type_traits/container_traits.h> |
| #include <__utility/exception_guard.h> |
| #include <__utility/forward.h> |
| #include <__utility/move.h> |
|
|
| #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| # pragma GCC system_header |
| #endif |
|
|
| _LIBCPP_PUSH_MACROS |
| #include <__undef_macros> |
|
|
| #if _LIBCPP_STD_VER >= 23 |
|
|
| _LIBCPP_BEGIN_NAMESPACE_STD |
|
|
| |
| struct __flat_map_utils { |
| |
| |
| |
| |
| |
| template <class _Map, class _IterK, class _IterM, class _KeyArg, class... _MArgs> |
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static typename _Map::iterator __emplace_exact_pos( |
| _Map& __map, _IterK&& __it_key, _IterM&& __it_mapped, _KeyArg&& __key, _MArgs&&... __mapped_args) { |
| auto __on_key_failed = std::__make_exception_guard([&]() noexcept { |
| using _KeyContainer = typename _Map::key_container_type; |
| if constexpr (__container_traits<_KeyContainer>::__emplacement_has_strong_exception_safety_guarantee) { |
| |
| } else { |
| |
| __map.clear() ; |
| } |
| }); |
| auto __key_it = __map.__containers_.keys.emplace(__it_key, std::forward<_KeyArg>(__key)); |
| __on_key_failed.__complete(); |
|
|
| auto __on_value_failed = std::__make_exception_guard([&]() noexcept { |
| using _MappedContainer = typename _Map::mapped_container_type; |
| if constexpr (!__container_traits<_MappedContainer>::__emplacement_has_strong_exception_safety_guarantee) { |
| |
| __map.clear() ; |
| } else { |
| |
| |
| # if _LIBCPP_HAS_EXCEPTIONS |
| try { |
| # endif |
| __map.__containers_.keys.erase(__key_it); |
| # if _LIBCPP_HAS_EXCEPTIONS |
| } catch (...) { |
| |
| |
| |
| |
| |
| __map.clear() ; |
| } |
| # endif |
| } |
| }); |
| auto __mapped_it = __map.__containers_.values.emplace(__it_mapped, std::forward<_MArgs>(__mapped_args)...); |
| __on_value_failed.__complete(); |
|
|
| return typename _Map::iterator(std::move(__key_it), std::move(__mapped_it)); |
| } |
|
|
| template <class _Map, class _InputIterator, class _Sentinel> |
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static typename _Map::size_type |
| __append(_Map& __map, _InputIterator __first, _Sentinel __last) { |
| typename _Map::size_type __num_appended = 0; |
| for (; __first != __last; ++__first) { |
| typename _Map::value_type __kv = *__first; |
| __map.__containers_.keys.insert(__map.__containers_.keys.end(), std::move(__kv.first)); |
| __map.__containers_.values.insert(__map.__containers_.values.end(), std::move(__kv.second)); |
| ++__num_appended; |
| } |
| return __num_appended; |
| } |
|
|
| template <class _Map, class _InputIterator> |
| _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 static typename _Map::size_type |
| __append(_Map& __map, _InputIterator __first, _InputIterator __last) |
| requires __is_product_iterator_of_size<_InputIterator, 2>::value |
| { |
| auto __s1 = __map.__containers_.keys.size(); |
| __map.__containers_.keys.insert( |
| __map.__containers_.keys.end(), |
| __product_iterator_traits<_InputIterator>::template __get_iterator_element<0>(__first), |
| __product_iterator_traits<_InputIterator>::template __get_iterator_element<0>(__last)); |
|
|
| __map.__containers_.values.insert( |
| __map.__containers_.values.end(), |
| __product_iterator_traits<_InputIterator>::template __get_iterator_element<1>(__first), |
| __product_iterator_traits<_InputIterator>::template __get_iterator_element<1>(__last)); |
|
|
| return __map.__containers_.keys.size() - __s1; |
| } |
| }; |
| _LIBCPP_END_NAMESPACE_STD |
|
|
| #endif |
|
|
| _LIBCPP_POP_MACROS |
|
|
| #endif |
|
|