mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-08 05:27:14 +09:00
AK: Implement {first|last}_matching for Span
This commit is contained in:
parent
0890b10d11
commit
93d7a29306
Notes:
github-actions[bot]
2025-05-06 09:18:26 +00:00
Author: https://github.com/stelar7
Commit: 93d7a29306
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4524
Reviewed-by: https://github.com/gmta ✅
1 changed files with 22 additions and 0 deletions
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…
Add table
Add a link
Reference in a new issue