Skip to content

Commit 75daba7

Browse files
Abseil Teamcopybara-github
authored andcommitted
Implement absl::erase_if for absl::InlinedVector
Fixes #1400 PiperOrigin-RevId: 798196403 Change-Id: I98528668f0db31cc5e4290ff88a5f33186588a8b
1 parent c5c771d commit 75daba7

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

absl/container/inlined_vector.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,16 @@ H AbslHashValue(H h, const absl::InlinedVector<T, N, A>& a) {
10111011
return H::combine_contiguous(std::move(h), a.data(), a.size());
10121012
}
10131013

1014+
template <typename T, size_t N, typename A, typename Predicate>
1015+
constexpr typename InlinedVector<T, N, A>::size_type erase_if(
1016+
InlinedVector<T, N, A>& v, Predicate pred) {
1017+
const auto it = std::remove_if(v.begin(), v.end(), std::move(pred));
1018+
const auto removed = static_cast<typename InlinedVector<T, N, A>::size_type>(
1019+
std::distance(it, v.end()));
1020+
v.erase(it, v.end());
1021+
return removed;
1022+
}
1023+
10141024
ABSL_NAMESPACE_END
10151025
} // namespace absl
10161026

absl/container/inlined_vector_test.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ using testing::AllOf;
5151
using testing::Each;
5252
using testing::ElementsAre;
5353
using testing::ElementsAreArray;
54+
using testing::IsEmpty;
5455
using testing::Eq;
5556
using testing::Gt;
5657
using testing::Pointee;
@@ -2254,4 +2255,22 @@ TEST(StorageTest, InlinedCapacityAutoIncrease) {
22542255
sizeof(MySpan<int>) / sizeof(int));
22552256
}
22562257

2258+
TEST(IntVec, EraseIf) {
2259+
IntVec v = {3, 1, 2, 0};
2260+
EXPECT_EQ(absl::erase_if(v, [](int i) { return i > 1; }), 2u);
2261+
EXPECT_THAT(v, ElementsAre(1, 0));
2262+
}
2263+
2264+
TEST(IntVec, EraseIfMatchesNone) {
2265+
IntVec v = {1, 2, 3};
2266+
EXPECT_EQ(absl::erase_if(v, [](int i) { return i > 10; }), 0u);;
2267+
EXPECT_THAT(v, ElementsAre(1, 2, 3));
2268+
}
2269+
2270+
TEST(IntVec, EraseIfMatchesAll) {
2271+
IntVec v = {1, 2, 3};
2272+
EXPECT_EQ(absl::erase_if(v, [](int i) { return i > 0; }), 3u);
2273+
EXPECT_THAT(v, IsEmpty());
2274+
}
2275+
22572276
} // anonymous namespace

0 commit comments

Comments
 (0)