[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
GitOrigin-RevId: 2ac6babcc007ccbe7f18d71cd6188c925cf25813
diff --git a/include/iterator b/include/iterator
index d2db7de..684312e 100644
--- a/include/iterator
+++ b/include/iterator
@@ -2033,6 +2033,21 @@
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