mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:37:44 +00:00
AK: Add first_matching and last_matching to Vector
first_matching returns the first item in the vector that matches the given condition. last_matching returns the last item in the vector that matches the given condition.
This commit is contained in:
parent
94ff04b536
commit
819f099a8e
1 changed files with 22 additions and 0 deletions
22
AK/Vector.h
22
AK/Vector.h
|
@ -336,6 +336,28 @@ public:
|
||||||
m_size += other.m_size;
|
m_size += other.m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
Optional<T> first_matching(Callback callback)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < size(); ++i) {
|
||||||
|
if (callback(at(i))) {
|
||||||
|
return at(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Callback>
|
||||||
|
Optional<T> last_matching(Callback callback)
|
||||||
|
{
|
||||||
|
for (ssize_t i = size() - 1; i >= 0; --i) {
|
||||||
|
if (callback(at(i))) {
|
||||||
|
return at(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
bool remove_first_matching(Callback callback)
|
bool remove_first_matching(Callback callback)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue