[libcxx-commits] [libcxx] [libc++] Implement `std::flat_multiset` (PR #128363)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 28 12:05:17 PDT 2025


================
@@ -0,0 +1,80 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___FLAT_SET_UTILS_H
+#define _LIBCPP___FLAT_SET_UTILS_H
+
+#include <__config>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__type_traits/container_traits.h>
+#include <__type_traits/decay.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
+
+// These utilities are defined in a class instead of a namespace so that this class can be befriended more easily.
+struct __flat_set_utils {
+  // Emplace a key into a flat_{multi}set, at the exact position that
+  // __it point to, assuming that the key is not already present in the set.
+  // When an exception is thrown during the emplacement, the function will clear the set if the container does not
+  // have strong exception safety guarantee on emplacement.
+  template <class _Set, class _Iter, class _KeyArg>
+  _LIBCPP_HIDE_FROM_ABI static auto __emplace_exact_pos(_Set& __set, _Iter&& __iter, _KeyArg&& __key) {
+    using _KeyContainer = typename decay_t<_Set>::container_type;
+    auto __on_failure   = std::__make_exception_guard([&]() noexcept {
+      if constexpr (!__container_traits<_KeyContainer>::__emplacement_has_strong_exception_safety_guarantee) {
+        __set.clear() /* noexcept */;
+      }
+    });
+    auto __key_it       = __set.__keys_.emplace(__iter.__base(), std::forward<_KeyArg>(__key));
+    __on_failure.__complete();
+    return typename decay_t<_Set>::iterator(std::move(__key_it));
+  }
+
+  // TODO: We could optimize this, see
+  // https://github.com/llvm/llvm-project/issues/108624
----------------
ldionne wrote:

I don't think this comment applies to the set containers, only the map ones.

https://github.com/llvm/llvm-project/pull/128363


More information about the libcxx-commits mailing list