AK: Make Vector<T>::{first,last}_matching() return Optional<T&>

These functions are _very_ misleading, as `first()` and `last()` return
references, but `{first,last}_matching()` return copies of the values.
This commit makes it so that they now return Optional<T&>, eliminating
the copy and the confusion.
This commit is contained in:
Ali Mohammad Pur 2022-04-03 18:06:18 +04:30 committed by Andreas Kling
parent 33e27c545e
commit 188207ed79

View file

@ -163,7 +163,7 @@ public:
VisibleType& last() { return at(size() - 1); }
template<typename TUnaryPredicate>
Optional<VisibleType> first_matching(TUnaryPredicate predicate) requires(!contains_reference)
Optional<VisibleType&> first_matching(TUnaryPredicate predicate) requires(!contains_reference)
{
for (size_t i = 0; i < size(); ++i) {
if (predicate(at(i))) {
@ -174,7 +174,7 @@ public:
}
template<typename TUnaryPredicate>
Optional<VisibleType> last_matching(TUnaryPredicate predicate) requires(!contains_reference)
Optional<VisibleType&> last_matching(TUnaryPredicate predicate) requires(!contains_reference)
{
for (ssize_t i = size() - 1; i >= 0; --i) {
if (predicate(at(i))) {