[libcxx-commits] [libcxx] 2ac6bab - [libc++] Move __libcpp_erase_if_container into <iterator>, and ADL-proof it.

Arthur O'Dwyer via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 22 08:14:52 PDT 2021


Author: Arthur O'Dwyer
Date: 2021-03-22T11:13:33-04:00
New Revision: 2ac6babcc007ccbe7f18d71cd6188c925cf25813

URL: https://github.com/llvm/llvm-project/commit/2ac6babcc007ccbe7f18d71cd6188c925cf25813
DIFF: https://github.com/llvm/llvm-project/commit/2ac6babcc007ccbe7f18d71cd6188c925cf25813.diff

LOG: [libc++] Move __libcpp_erase_if_container into <iterator>, and ADL-proof it.

The container headers don't need to include <functional> for any other reason
(or at least, they wouldn't if we moved `less` and `equal_to` out of <functional>),
so let's put `__libcpp_erase_if_container` somewhere that's common to the
containers but outside of <functional>.

Also, calling `std::erase_if(c, pred)` should not trigger ADL.

Differential Revision: https://reviews.llvm.org/D99043

Added: 
    

Modified: 
    libcxx/include/functional
    libcxx/include/iterator
    libcxx/include/map
    libcxx/include/set
    libcxx/include/unordered_map
    libcxx/include/unordered_set

Removed: 
    


################################################################################
diff  --git a/libcxx/include/functional b/libcxx/include/functional
index 747636f195d2..8a724f64c942 100644
--- a/libcxx/include/functional
+++ b/libcxx/include/functional
@@ -3215,22 +3215,6 @@ template <class _Tp>
 using unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
 #endif // > C++17
 
-template <class _Container, class _Predicate>
-inline typename _Container::size_type
-__libcpp_erase_if_container(_Container& __c, _Predicate __pred) {
-  typename _Container::size_type __old_size = __c.size();
-
-  const typename _Container::iterator __last = __c.end();
-  for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
-    if (__pred(*__iter))
-      __iter = __c.erase(__iter);
-    else
-      ++__iter;
-  }
-
-  return __old_size - __c.size();
-}
-
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_FUNCTIONAL

diff  --git a/libcxx/include/iterator b/libcxx/include/iterator
index d2db7de0cabe..684312e0dfc7 100644
--- a/libcxx/include/iterator
+++ b/libcxx/include/iterator
@@ -2033,6 +2033,21 @@ _LIBCPP_INLINE_VISIBILITY
 constexpr const _Ep* data(initializer_list<_Ep> __il) noexcept { return __il.begin(); }
 #endif
 
+template <class _Container, class _Predicate>
+typename _Container::size_type
+__libcpp_erase_if_container(_Container& __c, _Predicate& __pred) {
+  typename _Container::size_type __old_size = __c.size();
+
+  const typename _Container::iterator __last = __c.end();
+  for (typename _Container::iterator __iter = __c.begin(); __iter != __last;) {
+    if (__pred(*__iter))
+      __iter = __c.erase(__iter);
+    else
+      ++__iter;
+  }
+
+  return __old_size - __c.size();
+}
 
 _LIBCPP_END_NAMESPACE_STD
 

diff  --git a/libcxx/include/map b/libcxx/include/map
index 6b2d41909579..9c3e5e64a098 100644
--- a/libcxx/include/map
+++ b/libcxx/include/map
@@ -480,7 +480,7 @@ erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred);  // C++20
 #include <__config>
 #include <__tree>
 #include <__node_handle>
-#include <iterator>
+#include <iterator> // __libcpp_erase_if_container
 #include <memory>
 #include <utility>
 #include <functional>
@@ -1660,7 +1660,7 @@ template <class _Key, class _Tp, class _Compare, class _Allocator,
 inline _LIBCPP_INLINE_VISIBILITY
     typename map<_Key, _Tp, _Compare, _Allocator>::size_type
     erase_if(map<_Key, _Tp, _Compare, _Allocator>& __c, _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 
@@ -2246,7 +2246,7 @@ inline _LIBCPP_INLINE_VISIBILITY
     typename multimap<_Key, _Tp, _Compare, _Allocator>::size_type
     erase_if(multimap<_Key, _Tp, _Compare, _Allocator>& __c,
              _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 

diff  --git a/libcxx/include/set b/libcxx/include/set
index 3a83827711f1..506a5d5af395 100644
--- a/libcxx/include/set
+++ b/libcxx/include/set
@@ -429,6 +429,7 @@ erase_if(multiset<Key, Compare, Allocator>& c, Predicate pred);  // C++20
 #include <__tree>
 #include <__node_handle>
 #include <functional>
+#include <iterator> // __libcpp_erase_if_container
 #include <version>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -964,7 +965,7 @@ template <class _Key, class _Compare, class _Allocator, class _Predicate>
 inline _LIBCPP_INLINE_VISIBILITY
     typename set<_Key, _Compare, _Allocator>::size_type
     erase_if(set<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 
@@ -1490,7 +1491,7 @@ template <class _Key, class _Compare, class _Allocator, class _Predicate>
 inline _LIBCPP_INLINE_VISIBILITY
     typename multiset<_Key, _Compare, _Allocator>::size_type
     erase_if(multiset<_Key, _Compare, _Allocator>& __c, _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 

diff  --git a/libcxx/include/unordered_map b/libcxx/include/unordered_map
index 83a41148b681..fc10c8b9c2a2 100644
--- a/libcxx/include/unordered_map
+++ b/libcxx/include/unordered_map
@@ -435,6 +435,7 @@ template <class Key, class T, class Hash, class Pred, class Alloc>
 #include <__hash_table>
 #include <__node_handle>
 #include <functional>
+#include <iterator> // __libcpp_erase_if_container
 #include <stdexcept>
 #include <tuple>
 #include <version>
@@ -1817,7 +1818,7 @@ inline _LIBCPP_INLINE_VISIBILITY
     typename unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type
     erase_if(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __c,
              _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 
@@ -2550,7 +2551,7 @@ inline _LIBCPP_INLINE_VISIBILITY
     typename unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type
     erase_if(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __c,
              _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 

diff  --git a/libcxx/include/unordered_set b/libcxx/include/unordered_set
index 8a0f3aacbf30..726e37389ff1 100644
--- a/libcxx/include/unordered_set
+++ b/libcxx/include/unordered_set
@@ -390,6 +390,7 @@ template <class Value, class Hash, class Pred, class Alloc>
 #include <__hash_table>
 #include <__node_handle>
 #include <functional>
+#include <iterator> // __libcpp_erase_if_container
 #include <version>
 
 #include <__debug>
@@ -1069,7 +1070,7 @@ inline _LIBCPP_INLINE_VISIBILITY
     typename unordered_set<_Value, _Hash, _Pred, _Alloc>::size_type
     erase_if(unordered_set<_Value, _Hash, _Pred, _Alloc>& __c,
              _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 
@@ -1735,7 +1736,7 @@ inline _LIBCPP_INLINE_VISIBILITY
     typename unordered_multiset<_Value, _Hash, _Pred, _Alloc>::size_type
     erase_if(unordered_multiset<_Value, _Hash, _Pred, _Alloc>& __c,
              _Predicate __pred) {
-  return __libcpp_erase_if_container(__c, __pred);
+  return _VSTD::__libcpp_erase_if_container(__c, __pred);
 }
 #endif
 


        


More information about the libcxx-commits mailing list