summaryrefslogtreecommitdiff
path: root/absl/container
diff options
context:
space:
mode:
Diffstat (limited to 'absl/container')
-rw-r--r--absl/container/flat_hash_map.h9
-rw-r--r--absl/container/flat_hash_map_test.cc38
-rw-r--r--absl/container/flat_hash_set.h8
-rw-r--r--absl/container/flat_hash_set_test.cc36
-rw-r--r--absl/container/internal/raw_hash_set.h11
-rw-r--r--absl/container/node_hash_map.h9
-rw-r--r--absl/container/node_hash_map_test.cc38
-rw-r--r--absl/container/node_hash_set.h8
-rw-r--r--absl/container/node_hash_set_test.cc36
9 files changed, 193 insertions, 0 deletions
diff --git a/absl/container/flat_hash_map.h b/absl/container/flat_hash_map.h
index fb570cd4..fcb70d86 100644
--- a/absl/container/flat_hash_map.h
+++ b/absl/container/flat_hash_map.h
@@ -532,6 +532,15 @@ class flat_hash_map : public absl::container_internal::raw_hash_map<
using Base::key_eq;
};
+// erase_if(flat_hash_map<>, Pred)
+//
+// Erases all elements that satisfy the predicate `pred` from the container `c`.
+template <typename K, typename V, typename H, typename E, typename A,
+ typename Predicate>
+void erase_if(flat_hash_map<K, V, H, E, A>& c, Predicate pred) {
+ container_internal::EraseIf(pred, &c);
+}
+
namespace container_internal {
template <class K, class V>
diff --git a/absl/container/flat_hash_map_test.cc b/absl/container/flat_hash_map_test.cc
index dae8e003..fd9c5604 100644
--- a/absl/container/flat_hash_map_test.cc
+++ b/absl/container/flat_hash_map_test.cc
@@ -30,6 +30,7 @@ namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
using ::testing::_;
+using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;
@@ -215,6 +216,43 @@ TEST(FlatHashMap, MergeExtractInsert) {
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9)));
}
+bool FirstIsEven(std::pair<const int, int> p) { return p.first % 2 == 0; }
+
+TEST(FlatHashMap, EraseIf) {
+ // Erase all elements.
+ {
+ flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, [](std::pair<const int, int>) { return true; });
+ EXPECT_THAT(s, IsEmpty());
+ }
+ // Erase no elements.
+ {
+ flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, [](std::pair<const int, int>) { return false; });
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3),
+ Pair(4, 4), Pair(5, 5)));
+ }
+ // Erase specific elements.
+ {
+ flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s,
+ [](std::pair<const int, int> kvp) { return kvp.first % 2 == 1; });
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4)));
+ }
+ // Predicate is function reference.
+ {
+ flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, FirstIsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
+ }
+ // Predicate is function pointer.
+ {
+ flat_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, &FirstIsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
+ }
+}
+
#if (defined(ABSL_USES_STD_ANY) || !defined(_LIBCPP_VERSION)) && \
!defined(__EMSCRIPTEN__)
TEST(FlatHashMap, Any) {
diff --git a/absl/container/flat_hash_set.h b/absl/container/flat_hash_set.h
index 930107ea..94be6e3d 100644
--- a/absl/container/flat_hash_set.h
+++ b/absl/container/flat_hash_set.h
@@ -439,6 +439,14 @@ class flat_hash_set
using Base::key_eq;
};
+// erase_if(flat_hash_set<>, Pred)
+//
+// Erases all elements that satisfy the predicate `pred` from the container `c`.
+template <typename T, typename H, typename E, typename A, typename Predicate>
+void erase_if(flat_hash_set<T, H, E, A>& c, Predicate pred) {
+ container_internal::EraseIf(pred, &c);
+}
+
namespace container_internal {
template <class T>
diff --git a/absl/container/flat_hash_set_test.cc b/absl/container/flat_hash_set_test.cc
index 6eacb1bb..40d7f85c 100644
--- a/absl/container/flat_hash_set_test.cc
+++ b/absl/container/flat_hash_set_test.cc
@@ -31,6 +31,7 @@ namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
+using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;
using ::testing::UnorderedElementsAreArray;
@@ -124,6 +125,41 @@ TEST(FlatHashSet, MergeExtractInsert) {
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
}
+bool IsEven(int k) { return k % 2 == 0; }
+
+TEST(FlatHashSet, EraseIf) {
+ // Erase all elements.
+ {
+ flat_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, [](int) { return true; });
+ EXPECT_THAT(s, IsEmpty());
+ }
+ // Erase no elements.
+ {
+ flat_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, [](int) { return false; });
+ EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
+ }
+ // Erase specific elements.
+ {
+ flat_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, [](int k) { return k % 2 == 1; });
+ EXPECT_THAT(s, UnorderedElementsAre(2, 4));
+ }
+ // Predicate is function reference.
+ {
+ flat_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, IsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
+ }
+ // Predicate is function pointer.
+ {
+ flat_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, &IsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
+ }
+}
+
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
diff --git a/absl/container/internal/raw_hash_set.h b/absl/container/internal/raw_hash_set.h
index 4103e02a..b1c686ed 100644
--- a/absl/container/internal/raw_hash_set.h
+++ b/absl/container/internal/raw_hash_set.h
@@ -1801,6 +1801,17 @@ class raw_hash_set {
settings_{0, hasher{}, key_equal{}, allocator_type{}};
};
+// Erases all elements that satisfy the predicate `pred` from the container `c`.
+template <typename P, typename H, typename E, typename A, typename Predicate>
+void EraseIf(Predicate pred, raw_hash_set<P, H, E, A>* c) {
+ for (auto it = c->begin(), last = c->end(); it != last;) {
+ auto copy_it = it++;
+ if (pred(*copy_it)) {
+ c->erase(copy_it);
+ }
+ }
+}
+
namespace hashtable_debug_internal {
template <typename Set>
struct HashtableDebugAccess<Set, absl::void_t<typename Set::raw_hash_set>> {
diff --git a/absl/container/node_hash_map.h b/absl/container/node_hash_map.h
index e8065a98..fccea184 100644
--- a/absl/container/node_hash_map.h
+++ b/absl/container/node_hash_map.h
@@ -522,6 +522,15 @@ class node_hash_map
void resize(typename Base::size_type hint) { this->rehash(hint); }
};
+// erase_if(node_hash_map<>, Pred)
+//
+// Erases all elements that satisfy the predicate `pred` from the container `c`.
+template <typename K, typename V, typename H, typename E, typename A,
+ typename Predicate>
+void erase_if(node_hash_map<K, V, H, E, A>& c, Predicate pred) {
+ container_internal::EraseIf(pred, &c);
+}
+
namespace container_internal {
template <class Key, class Value>
diff --git a/absl/container/node_hash_map_test.cc b/absl/container/node_hash_map_test.cc
index f923e915..5d74b814 100644
--- a/absl/container/node_hash_map_test.cc
+++ b/absl/container/node_hash_map_test.cc
@@ -26,6 +26,7 @@ namespace container_internal {
namespace {
using ::testing::Field;
+using ::testing::IsEmpty;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;
@@ -216,6 +217,43 @@ TEST(NodeHashMap, MergeExtractInsert) {
EXPECT_THAT(set2, UnorderedElementsAre(Elem(7, -70), Elem(17, 23)));
}
+bool FirstIsEven(std::pair<const int, int> p) { return p.first % 2 == 0; }
+
+TEST(NodeHashMap, EraseIf) {
+ // Erase all elements.
+ {
+ node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, [](std::pair<const int, int>) { return true; });
+ EXPECT_THAT(s, IsEmpty());
+ }
+ // Erase no elements.
+ {
+ node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, [](std::pair<const int, int>) { return false; });
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(2, 2), Pair(3, 3),
+ Pair(4, 4), Pair(5, 5)));
+ }
+ // Erase specific elements.
+ {
+ node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s,
+ [](std::pair<const int, int> kvp) { return kvp.first % 2 == 1; });
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(2, 2), Pair(4, 4)));
+ }
+ // Predicate is function reference.
+ {
+ node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, FirstIsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
+ }
+ // Predicate is function pointer.
+ {
+ node_hash_map<int, int> s = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ erase_if(s, &FirstIsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(Pair(1, 1), Pair(3, 3), Pair(5, 5)));
+ }
+}
+
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END
diff --git a/absl/container/node_hash_set.h b/absl/container/node_hash_set.h
index 43ada3f9..0e2dee54 100644
--- a/absl/container/node_hash_set.h
+++ b/absl/container/node_hash_set.h
@@ -435,6 +435,14 @@ class node_hash_set
void resize(typename Base::size_type hint) { this->rehash(hint); }
};
+// erase_if(node_hash_set<>, Pred)
+//
+// Erases all elements that satisfy the predicate `pred` from the container `c`.
+template <typename T, typename H, typename E, typename A, typename Predicate>
+void erase_if(node_hash_set<T, H, E, A>& c, Predicate pred) {
+ container_internal::EraseIf(pred, &c);
+}
+
namespace container_internal {
template <class T>
diff --git a/absl/container/node_hash_set_test.cc b/absl/container/node_hash_set_test.cc
index e1d544ff..7ddad202 100644
--- a/absl/container/node_hash_set_test.cc
+++ b/absl/container/node_hash_set_test.cc
@@ -25,6 +25,7 @@ namespace container_internal {
namespace {
using ::absl::container_internal::hash_internal::Enum;
using ::absl::container_internal::hash_internal::EnumClass;
+using ::testing::IsEmpty;
using ::testing::Pointee;
using ::testing::UnorderedElementsAre;
@@ -101,6 +102,41 @@ TEST(NodeHashSet, MergeExtractInsert) {
EXPECT_THAT(set2, UnorderedElementsAre(Pointee(7), Pointee(23)));
}
+bool IsEven(int k) { return k % 2 == 0; }
+
+TEST(NodeHashSet, EraseIf) {
+ // Erase all elements.
+ {
+ node_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, [](int) { return true; });
+ EXPECT_THAT(s, IsEmpty());
+ }
+ // Erase no elements.
+ {
+ node_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, [](int) { return false; });
+ EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5));
+ }
+ // Erase specific elements.
+ {
+ node_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, [](int k) { return k % 2 == 1; });
+ EXPECT_THAT(s, UnorderedElementsAre(2, 4));
+ }
+ // Predicate is function reference.
+ {
+ node_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, IsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
+ }
+ // Predicate is function pointer.
+ {
+ node_hash_set<int> s = {1, 2, 3, 4, 5};
+ erase_if(s, &IsEven);
+ EXPECT_THAT(s, UnorderedElementsAre(1, 3, 5));
+ }
+}
+
} // namespace
} // namespace container_internal
ABSL_NAMESPACE_END