"vscode:/vscode.git/clone" did not exist on "03b7890163290ab871752dd85a94b51b7bd72442"
functional.hpp 2.33 KB
Newer Older
1
2
3
#ifndef CK_FUNCTIONAL_HPP
#define CK_FUNCTIONAL_HPP

Chao Liu's avatar
Chao Liu committed
4
#include "integral_constant.hpp"
Chao Liu's avatar
Chao Liu committed
5
#include "type.hpp"
6

7
8
namespace ck {

Chao Liu's avatar
Chao Liu committed
9
// TODO: right? wrong?
Chao Liu's avatar
Chao Liu committed
10
11
12
struct forwarder
{
    template <typename T>
Chao Liu's avatar
Chao Liu committed
13
    __host__ __device__ constexpr T&& operator()(T&& x) const
Chao Liu's avatar
Chao Liu committed
14
    {
Chao Liu's avatar
Chao Liu committed
15
        return static_cast<T&&>(x);
Chao Liu's avatar
Chao Liu committed
16
17
18
    }
};

Chao Liu's avatar
Chao Liu committed
19
20
struct swallow
{
Chao Liu's avatar
Chao Liu committed
21
    template <typename... Ts>
Chao Liu's avatar
Chao Liu committed
22
    __host__ __device__ constexpr swallow(Ts&&...)
Chao Liu's avatar
Chao Liu committed
23
24
25
26
    {
    }
};

Chao Liu's avatar
Chao Liu committed
27
28
29
30
31
32
33
34
35
36
37
38
template <typename T>
struct logical_and
{
    constexpr bool operator()(const T& x, const T& y) const { return x && y; }
};

template <typename T>
struct logical_or
{
    constexpr bool operator()(const T& x, const T& y) const { return x || y; }
};

Chao Liu's avatar
Chao Liu committed
39
40
41
42
43
44
template <typename T>
struct logical_not
{
    constexpr bool operator()(const T& x) const { return !x; }
};

Chao Liu's avatar
Chao Liu committed
45
// Emulate if constexpr
Chao Liu's avatar
Chao Liu committed
46
47
template <bool>
struct static_if;
Chao Liu's avatar
Chao Liu committed
48
49
50
51
52
53

template <>
struct static_if<true>
{
    using Type = static_if<true>;

Chao Liu's avatar
Chao Liu committed
54
    template <typename F>
Chao Liu's avatar
Chao Liu committed
55
56
57
    __host__ __device__ constexpr auto operator()(F f) const
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
58
59
60
61
        //   Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will
        //   use it,
        //   this will make "f" a generic lambda, so that "f" won't be compiled
        //   until being
Chao Liu's avatar
Chao Liu committed
62
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
63
64
65
66
        f(forwarder{});
        return Type{};
    }

Chao Liu's avatar
Chao Liu committed
67
    template <typename F>
Chao Liu's avatar
Chao Liu committed
68
    __host__ __device__ static void Else(F)
Chao Liu's avatar
Chao Liu committed
69
70
71
72
73
74
75
76
77
    {
    }
};

template <>
struct static_if<false>
{
    using Type = static_if<false>;

Chao Liu's avatar
Chao Liu committed
78
    template <typename F>
Chao Liu's avatar
Chao Liu committed
79
80
81
82
83
    __host__ __device__ constexpr auto operator()(F) const
    {
        return Type{};
    }

Chao Liu's avatar
Chao Liu committed
84
    template <typename F>
Chao Liu's avatar
Chao Liu committed
85
    __host__ __device__ static void Else(F f)
Chao Liu's avatar
Chao Liu committed
86
87
    {
        // This is a trick for compiler:
Chao Liu's avatar
Chao Liu committed
88
89
90
91
        //   Pass forwarder to lambda "f" as "auto" argument, and make sure "f" will
        //   use it,
        //   this will make "f" a generic lambda, so that "f" won't be compiled
        //   until being
Chao Liu's avatar
Chao Liu committed
92
        //   instantiated here
Chao Liu's avatar
Chao Liu committed
93
94
95
        f(forwarder{});
    }
};
96

Chao Liu's avatar
Chao Liu committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
template <bool predicate, class X, class Y>
struct conditional;

template <class X, class Y>
struct conditional<true, X, Y>
{
    using type = X;
};

template <class X, class Y>
struct conditional<false, X, Y>
{
    using type = Y;
};

template <bool predicate, class X, class Y>
using conditional_t = typename conditional<predicate, X, Y>::type;

115
116
} // namespace ck
#endif