]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: debug impls for heterogeneous erasure overloads (P2077) [PR117404]
authorNathan Myers <ncm@cantrip.org>
Fri, 6 Feb 2026 21:57:07 +0000 (16:57 -0500)
committerNathan Myers <ncm@cantrip.org>
Wed, 25 Feb 2026 21:43:52 +0000 (16:43 -0500)
Implement the debug versions of new overloads from P2077.

libstdc++-v3/ChangeLog:
PR libstdc++/117404
* include/debug/map.h (extract, erase): Define overloads.
* include/debug/multimap.h: Same.
* include/debug/multiset.h: Same.
* include/debug/set.h: Same.
* include/debug/unordered_map: Same (2x).
* include/debug/unordered_set: Same (2x), rename some locals.

libstdc++-v3/include/debug/map.h
libstdc++-v3/include/debug/multimap.h
libstdc++-v3/include/debug/multiset.h
libstdc++-v3/include/debug/set.h
libstdc++-v3/include/debug/unordered_map
libstdc++-v3/include/debug/unordered_set

index 0fc7afae385b000409d4cc7f0023d0963f77354b..47d1fe6b3efe14f3b7ab2e0a2a2a022c4cef8fac 100644 (file)
@@ -476,6 +476,18 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_tree_key<map> _Kt>
+       node_type
+       extract(_Kt&& __key)
+       {
+         const auto __position = find(__key);
+         if (__position != end())
+           return extract(__position);
+         return {};
+       }
+# endif
+
       insert_return_type
       insert(node_type&& __nh)
       {
@@ -538,6 +550,26 @@ namespace __debug
          }
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      // Note that for some types _Kt this may erase more than
+      // one element, such as if _Kt::operator< checks only part
+      // of the key.
+      template <__heterogeneous_tree_key<map> _Kt>
+       size_type
+       erase(_Kt&& __x)
+       {
+         auto __victims = _Base::equal_range(__x);
+         size_type __count = 0;
+         for (auto __victim = __victims.first; __victim != __victims.second;)
+           {
+             this->_M_invalidate_if(_Equal(__victim));
+             _Base::erase(__victim++);
+             ++__count;
+           }
+         return __count;
+       }
+# endif
+
 #if __cplusplus >= 201103L
       iterator
       erase(const_iterator __first, const_iterator __last)
index 4137186c5278bedb64fadcf6735fdaa8ea6b14e8..4b2591fdcfacb7b3223026ef5fd916c4461650c0 100644 (file)
@@ -360,6 +360,18 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_tree_key<multimap> _Kt>
+       node_type
+       extract(_Kt&& __key)
+       {
+         const auto __position = find(__key);
+         if (__position != end())
+           return extract(__position);
+         return {};
+       }
+# endif
+
       iterator
       insert(node_type&& __nh)
       { return { _Base::insert(std::move(__nh)), this }; }
@@ -420,6 +432,23 @@ namespace __debug
        return __count;
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_tree_key<multimap> _Kt>
+       size_type
+       erase(_Kt&& __x)
+       {
+         auto __victims = _Base::equal_range(__x);
+         size_type __count = 0;
+         for (auto __victim = __victims.first; __victim != __victims.second;)
+           {
+             this->_M_invalidate_if(_Equal(__victim));
+             _Base::erase(__victim++);
+             ++__count;
+           }
+         return __count;
+       }
+# endif
+
 #if __cplusplus >= 201103L
       iterator
       erase(const_iterator __first, const_iterator __last)
index 6fa499228f5e4cce29bbf4d95bcaefb31afbf835..2b70dcd8b1f477dc9279ad4a867cc109a846c5c3 100644 (file)
@@ -331,6 +331,18 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_tree_key<multiset> _Kt>
+       node_type
+       extract(_Kt&& __key)
+       {
+         const auto __position = find(__key);
+         if (__position != end())
+           return extract(__position);
+         return {};
+       }
+#endif
+
       iterator
       insert(node_type&& __nh)
       { return { _Base::insert(std::move(__nh)), this }; }
@@ -387,6 +399,23 @@ namespace __debug
        return __count;
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_tree_key<multiset> _Kt>
+       size_type
+       erase(_Kt&& __x)
+       {
+         auto __victims = _Base::equal_range(__x);
+         size_type __count = 0;
+         for (auto __victim = __victims.first; __victim != __victims.second;)
+           {
+             this->_M_invalidate_if(_Equal(__victim));
+             _Base::erase(__victim++);
+             ++__count;
+           }
+         return __count;
+       }
+# endif
+
 #if __cplusplus >= 201103L
       _GLIBCXX_ABI_TAG_CXX11
       iterator
index 99701d4a6d7e3ad092cabda78d75e7806fc80565..81c2fa6e739f515c1412d3aae655304f8e4c92cb 100644 (file)
@@ -340,6 +340,18 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_tree_key<set> _Kt>
+       node_type
+       extract(_Kt&& __key)
+       {
+         const auto __position = find(__key);
+         if (__position != end())
+           return extract(__position);
+         return {};
+       }
+#endif
+
       insert_return_type
       insert(node_type&& __nh)
       {
@@ -398,6 +410,26 @@ namespace __debug
          }
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      // Note that for some types _Kt this may erase more than
+      // one element, such as if _Kt::operator< checks only part
+      // of the key.
+      template <__heterogeneous_tree_key<set> _Kt>
+       size_type
+       erase(_Kt&& __x)
+       {
+         auto __victims = _Base::equal_range(__x);
+         size_type __count = 0;
+         for (auto __victim = __victims.first; __victim != __victims.second;)
+           {
+             this->_M_invalidate_if(_Equal(__victim));
+             _Base::erase(__victim++);
+             ++__count;
+           }
+         return __count;
+       }
+#endif
+
 #if __cplusplus >= 201103L
       _GLIBCXX_ABI_TAG_CXX11
       iterator
index 4bde18c917b2ce296aac4a1e556e43a8b0b05678..1ea480a4e8bac9e648c02a9a81f3840c67194552 100644 (file)
@@ -581,6 +581,18 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_map> _Kt>
+       node_type
+       extract(_Kt&& __key)
+       {
+         const auto __position = _Base::find(__key);
+         if (__position != _Base::end())
+           return _M_extract(__position);
+         return {};
+       }
+#endif
+
       insert_return_type
       insert(node_type&& __nh)
       {
@@ -713,6 +725,18 @@ namespace __debug
        return __ret;
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_map> _Kt>
+       size_type
+       erase(_Kt&& __key)
+       {
+         auto __victim = _Base::find(__key);
+         if (__victim != _Base::end())
+           return _M_erase(__victim), 1;
+         return 0;
+       }
+#endif
+
       iterator
       erase(const_iterator __it)
       {
@@ -1381,6 +1405,18 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_multimap> _Kt>
+       node_type
+       extract(_Kt&& __key)
+       {
+         const auto __position = _Base::find(__key);
+         if (__position != _Base::end())
+           return _M_extract(__position);
+         return {};
+       }
+#endif
+
       iterator
       insert(node_type&& __nh)
       { return { _Base::insert(std::move(__nh)), this }; }
@@ -1510,6 +1546,23 @@ namespace __debug
        return __ret;
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_multimap> _Kt>
+       size_type
+       erase(_Kt&& __key)
+       {
+         size_type __count(0);
+         auto __victims = _Base::equal_range(__key);
+         for (auto __victim = __victims.first; __victim != __victims.second;)
+           {
+             _M_invalidate(__victim);
+             __victim = _Base::erase(__victim);
+             ++__count;
+           }
+         return __count;
+       }
+#endif
+
       iterator
       erase(const_iterator __it)
       {
index de999a76890e98e4a35fc2ce2d0e0cddc63d48df..e26b52d70299bbf13a3b50684b77ac4406356233 100644 (file)
@@ -468,6 +468,18 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_set> _Kt>
+       node_type
+       extract(_Kt&& __key)
+       {
+         const auto __position = _Base::find(__key);
+         if (__position != _Base::end())
+           return _M_extract(__position);
+         return {};
+       }
+#endif
+
       insert_return_type
       insert(node_type&& __nh)
       {
@@ -598,6 +610,18 @@ namespace __debug
        return __ret;
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_set> _Kt>
+       size_type
+       erase(_Kt&& __key)
+       {
+         auto __victim = _Base::find(__key);
+         if (__victim != _Base::end())
+           return _M_erase(__victim), 1;
+         return 0;
+       }
+#endif
+
       iterator
       erase(const_iterator __it)
       {
@@ -1202,6 +1226,17 @@ namespace __debug
        return {};
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_multiset> _Kt>
+       node_type
+       extract(const _Kt& __key)
+       {
+         const auto __position = _Base::find(__key);
+         return __position != _Base::end() ?
+           _M_extract(__position) : node_type{};
+       }
+#endif
+
       iterator
       insert(node_type&& __nh)
       { return { _Base::insert(std::move(__nh)), this }; }
@@ -1318,18 +1353,34 @@ namespace __debug
       size_type
       erase(const key_type& __key)
       {
-       size_type __ret(0);
-       auto __pair = _Base::equal_range(__key);
-       for (auto __victim = __pair.first; __victim != __pair.second;)
+       size_type __count(0);
+       auto __victims = _Base::equal_range(__key);
+       for (auto __victim = __victims.first; __victim != __victims.second;)
          {
            _M_invalidate(__victim);
            __victim = _Base::erase(__victim);
-           ++__ret;
+           ++__count;
          }
-
-       return __ret;
+       return __count;
       }
 
+# ifdef __glibcxx_associative_heterogeneous_erasure
+      template <__heterogeneous_hash_key<unordered_multiset> _Kt>
+       size_type
+       erase(_Kt&& __key)
+       {
+         size_type __count(0);
+         auto __victims = _Base::equal_range(__key);
+         for (auto __victim = __victims.first; __victim != __victims.second;)
+           {
+             _M_invalidate(__victim);
+             __victim = _Base::erase(__victim);
+             ++__count;
+           }
+         return __count;
+       }
+#endif
+
       iterator
       erase(const_iterator __it)
       {