mirror of
https://github.com/zebrajr/ladybird.git
synced 2025-12-06 00:19:53 +01:00
AK: Implement {first|last}_matching for Span
This commit is contained in:
parent
0890b10d11
commit
93d7a29306
22
AK/Span.h
22
AK/Span.h
|
|
@ -306,6 +306,28 @@ public:
|
|||
{
|
||||
return { data(), size() };
|
||||
}
|
||||
|
||||
template<typename TUnaryPredicate>
|
||||
Optional<T&> last_matching(TUnaryPredicate const& predicate)
|
||||
{
|
||||
for (ssize_t i = size() - 1; i >= 0; --i) {
|
||||
if (predicate(at(i))) {
|
||||
return at(i);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
template<typename TUnaryPredicate>
|
||||
Optional<T&> first_matching(TUnaryPredicate const& predicate)
|
||||
{
|
||||
for (size_t i = 0; i < size(); ++i) {
|
||||
if (predicate(at(i))) {
|
||||
return at(i);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user