mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 20:14:23 +08:00
Implement absl::erase_if for absl::InlinedVector
Fixes #1400 PiperOrigin-RevId: 798196403 Change-Id: I98528668f0db31cc5e4290ff88a5f33186588a8b
This commit is contained in:
committed by
Copybara-Service
parent
c5c771d121
commit
75daba77f9
@@ -1011,6 +1011,16 @@ H AbslHashValue(H h, const absl::InlinedVector<T, N, A>& a) {
|
||||
return H::combine_contiguous(std::move(h), a.data(), a.size());
|
||||
}
|
||||
|
||||
template <typename T, size_t N, typename A, typename Predicate>
|
||||
constexpr typename InlinedVector<T, N, A>::size_type erase_if(
|
||||
InlinedVector<T, N, A>& v, Predicate pred) {
|
||||
const auto it = std::remove_if(v.begin(), v.end(), std::move(pred));
|
||||
const auto removed = static_cast<typename InlinedVector<T, N, A>::size_type>(
|
||||
std::distance(it, v.end()));
|
||||
v.erase(it, v.end());
|
||||
return removed;
|
||||
}
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
|
||||
@@ -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<int>) / 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
|
||||
|
||||
Reference in New Issue
Block a user