aboutsummaryrefslogtreecommitdiff
path: root/src/common/algorithm.h
blob: c27c9241d93a58074af5e08fe1ac9d7d753c06c3 (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <algorithm>
#include <functional>

// Algorithms that operate on iterators, much like the <algorithm> header.
//
// Note: If the algorithm is not general-purpose and/or doesn't operate on iterators,
//       it should probably not be placed within this header.

namespace Common {

template <class ForwardIt, class T, class Compare = std::less<>>
[[nodiscard]] ForwardIt BinaryFind(ForwardIt first, ForwardIt last, const T& value,
                                   Compare comp = {}) {
    // Note: BOTH type T and the type after ForwardIt is dereferenced
    // must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
    // This is stricter than lower_bound requirement (see above)

    first = std::lower_bound(first, last, value, comp);
    return first != last && !comp(value, *first) ? first : last;
}

template <typename T, typename Func, typename... Args>
T FoldRight(T initial_value, Func&& func, Args&&... args) {
    T value{initial_value};
    const auto high_func = [&value, &func]<typename U>(U x) { value = func(value, x); };
    (std::invoke(high_func, std::forward<Args>(args)), ...);
    return value;
}

} // namespace Common