Use std::remove_if to implement erase_if

I needed to do something similar in crrev/c/4024485.

Change-Id: I99c97b4bb1739fcc453659b10b378d1a04f18c8f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4022374
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Auto-Submit: S Ganesh <ganesh@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84285}
This commit is contained in:
S. Ganesh 2022-11-15 09:35:54 -08:00 committed by V8 LUCI CQ
parent 9b92249200
commit 0fe2597dd0

View File

@ -6,6 +6,7 @@
#define V8_BASE_CONTAINER_UTILS_H_
#include <algorithm>
#include <iterator>
#include <optional>
#include <vector>
@ -60,15 +61,10 @@ inline size_t erase_at(C& container, size_t index, size_t count = 1) {
// TODO(C++20): Replace with std::erase_if.
template <typename C, typename P>
inline size_t erase_if(C& container, const P& predicate) {
size_t count = 0;
auto e = end(container);
for (auto it = begin(container); it != e;) {
it = std::find_if(it, e, predicate);
if (it == e) break;
it = container.erase(it);
e = end(container);
++count;
}
auto it =
std::remove_if(std::begin(container), std::end(container), predicate);
auto count = std::distance(it, std::end(container));
container.erase(it, std::end(container));
return count;
}