diff --git a/absl/container/inlined_vector.h b/absl/container/inlined_vector.h index c53cbd21..a1610c9f 100644 --- a/absl/container/inlined_vector.h +++ b/absl/container/inlined_vector.h @@ -1011,6 +1011,16 @@ H AbslHashValue(H h, const absl::InlinedVector& a) { return H::combine_contiguous(std::move(h), a.data(), a.size()); } +template +constexpr typename InlinedVector::size_type erase_if( + InlinedVector& v, Predicate pred) { + const auto it = std::remove_if(v.begin(), v.end(), std::move(pred)); + const auto removed = static_cast::size_type>( + std::distance(it, v.end())); + v.erase(it, v.end()); + return removed; +} + ABSL_NAMESPACE_END } // namespace absl diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc index ff0e77b5..ba8c4a8b 100644 --- a/absl/container/inlined_vector_test.cc +++ b/absl/container/inlined_vector_test.cc @@ -51,6 +51,7 @@ using testing::AllOf; using testing::Each; using testing::ElementsAre; using testing::ElementsAreArray; +using testing::IsEmpty; using testing::Eq; using testing::Gt; using testing::Pointee; @@ -2254,4 +2255,22 @@ TEST(StorageTest, InlinedCapacityAutoIncrease) { sizeof(MySpan) / sizeof(int)); } +TEST(IntVec, EraseIf) { + IntVec v = {3, 1, 2, 0}; + EXPECT_EQ(absl::erase_if(v, [](int i) { return i > 1; }), 2u); + EXPECT_THAT(v, ElementsAre(1, 0)); +} + +TEST(IntVec, EraseIfMatchesNone) { + IntVec v = {1, 2, 3}; + EXPECT_EQ(absl::erase_if(v, [](int i) { return i > 10; }), 0u);; + EXPECT_THAT(v, ElementsAre(1, 2, 3)); +} + +TEST(IntVec, EraseIfMatchesAll) { + IntVec v = {1, 2, 3}; + EXPECT_EQ(absl::erase_if(v, [](int i) { return i > 0; }), 3u); + EXPECT_THAT(v, IsEmpty()); +} + } // anonymous namespace