summaryrefslogtreecommitdiff
path: root/AK/Find.h
blob: f0f3359172c1fbfcd4e5a0845198e38afcf80c3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Traits.h>
#include <AK/Types.h>

namespace AK {

template<typename TIterator, typename TUnaryPredicate>
constexpr TIterator find_if(TIterator first, TIterator last, TUnaryPredicate&& pred)
{
    for (; first != last; ++first) {
        if (pred(*first)) {
            return first;
        }
    }
    return last;
}

template<typename TIterator, typename T>
constexpr TIterator find(TIterator first, TIterator last, const T& value)
{
    return find_if(first, last, [&](const auto& v) { return Traits<T>::equals(value, v); });
}

template<typename TIterator, typename T>
constexpr size_t find_index(TIterator first, TIterator last, const T& value)
{
    return find_if(first, last, [&](const auto& v) { return Traits<T>::equals(value, v); }).index();
}

}