aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include/vcpkg_Util.h
blob: 6648302acb90364ea9e9af950585f9052bc9e4fb (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#pragma once

#include <utility>
#include <vector>

namespace vcpkg::Util
{
    template<class Cont, class Func>
    using FmapOut = decltype(std::declval<Func>()(std::declval<Cont>()[0]));

    template<class Cont, class Func, class Out = FmapOut<Cont, Func>>
    std::vector<Out> fmap(const Cont& xs, Func&& f)
    {
        using O = decltype(f(xs[0]));

        std::vector<O> ret;
        ret.reserve(xs.size());

        for (auto&& x : xs)
            ret.push_back(f(x));

        return ret;
    }

    template<class Container, class Pred>
    void unstable_keep_if(Container& cont, Pred pred)
    {
        cont.erase(std::partition(cont.begin(), cont.end(), pred), cont.end());
    }

    template<class Container, class Pred>
    void erase_remove_if(Container& cont, Pred pred)
    {
        cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end());
    }

    template<class Container, class Pred>
    auto find_if(const Container& cont, Pred pred)
    {
        return std::find_if(cont.cbegin(), cont.cend(), pred);
    }

    template<class Container, class Pred>
    auto find_if_not(const Container& cont, Pred pred)
    {
        return std::find_if_not(cont.cbegin(), cont.cend(), pred);
    }

    template<class K, class V, class Container, class Func>
    void group_by(const Container& cont, std::map<K, std::vector<const V*>>* output, Func f)
    {
        for (const V& element : cont)
        {
            K key = f(element);
            (*output)[key].push_back(&element);
        }
    }
}