Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit fe6dfbf

Browse files
committed
Add an Implementation of std::erase_if for std::unordered_set.
1 parent 23b8dba commit fe6dfbf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

include/swift/Basic/STLExtras.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <iterator>
2828
#include <numeric>
2929
#include <type_traits>
30+
#include <unordered_set>
3031

3132
namespace swift {
3233

@@ -752,6 +753,22 @@ using all_true =
752753
template <class... Ts>
753754
using are_all_compound = all_true<std::is_compound<Ts>::value...>;
754755

756+
/// Erase all elements in \p c that match the given predicate \p pred.
757+
// FIXME: Remove this when C++20 is the new baseline.
758+
template <class Key, class Hash, class KeyEqual, class Alloc, class Pred>
759+
typename std::unordered_set<Key, Hash, KeyEqual, Alloc>::size_type
760+
erase_if(std::unordered_set<Key, Hash, KeyEqual, Alloc> &c, Pred pred) {
761+
auto startingSize = c.size();
762+
for (auto i = c.begin(), last = c.end(); i != last;) {
763+
if (pred(*i)) {
764+
i = c.erase(i);
765+
} else {
766+
++i;
767+
}
768+
}
769+
return startingSize - c.size();
770+
}
771+
755772
} // end namespace swift
756773

757774
#endif // SWIFT_BASIC_INTERLEAVE_H

0 commit comments

Comments
 (0)