2020-12-23 11:35:01 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-23 11:35:01 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-22 19:19:34 +04:30
|
|
|
#include <AK/Concepts.h>
|
2025-04-05 09:06:51 -04:00
|
|
|
#include <AK/Optional.h>
|
2020-12-23 11:35:01 -07:00
|
|
|
#include <AK/Traits.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2021-07-22 19:19:34 +04:30
|
|
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename TUnaryPredicate>
|
2022-06-26 10:21:14 -06:00
|
|
|
[[nodiscard]] constexpr TIterator find_if(TIterator first, TEndIterator last, TUnaryPredicate&& pred)
|
2020-12-23 11:35:01 -07:00
|
|
|
{
|
|
|
|
for (; first != last; ++first) {
|
|
|
|
if (pred(*first)) {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2023-08-21 13:48:34 +02:00
|
|
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename V>
|
|
|
|
[[nodiscard]] constexpr TIterator find(TIterator first, TEndIterator last, V const& value)
|
2020-12-23 11:35:01 -07:00
|
|
|
{
|
2023-08-21 13:48:34 +02:00
|
|
|
return find_if(first, last, [&]<typename T>(T const& entry) { return Traits<T>::equals(entry, value); });
|
2020-12-23 11:35:01 -07:00
|
|
|
}
|
|
|
|
|
2023-08-21 13:48:34 +02:00
|
|
|
template<typename TEndIterator, IteratorPairWith<TEndIterator> TIterator, typename V>
|
|
|
|
[[nodiscard]] constexpr size_t find_index(TIterator first, TEndIterator last, V const& value)
|
2022-10-17 00:06:11 +02:00
|
|
|
requires(requires(TIterator it) { it.index(); })
|
2020-12-23 11:35:01 -07:00
|
|
|
{
|
2023-08-21 13:48:34 +02:00
|
|
|
return find_if(first, last, [&]<typename T>(T const& entry) { return Traits<T>::equals(entry, value); }).index();
|
2020-12-23 11:35:01 -07:00
|
|
|
}
|
|
|
|
|
2025-04-05 09:06:51 -04:00
|
|
|
template<IterableContainer Container, typename TUnaryPredicate>
|
|
|
|
[[nodiscard]] constexpr auto find_value(Container const& container, TUnaryPredicate&& pred)
|
|
|
|
-> Optional<decltype(*container.begin())>
|
|
|
|
{
|
|
|
|
auto it = find_if(container.begin(), container.end(), forward<TUnaryPredicate>(pred));
|
|
|
|
if (it != container.end())
|
|
|
|
return *it;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-12-23 11:35:01 -07:00
|
|
|
}
|
2024-05-04 13:38:00 -04:00
|
|
|
|
|
|
|
#if USING_AK_GLOBALLY
|
|
|
|
using AK::find;
|
|
|
|
using AK::find_if;
|
|
|
|
using AK::find_index;
|
2025-04-05 09:06:51 -04:00
|
|
|
using AK::find_value;
|
2024-05-04 13:38:00 -04:00
|
|
|
#endif
|