functional.hpp 2.31 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 "sequence.hpp"
Chao Liu's avatar
Chao Liu committed
6
#include "type.hpp"
7

8
9
namespace ck {

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

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

Chao Liu's avatar
Chao Liu committed
28
29
30
31
32
33
34
35
36
37
38
39
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
40
41
42
43
44
45
template <typename T>
struct logical_not
{
    constexpr bool operator()(const T& x) const { return !x; }
};

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

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

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

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

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

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

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

Chao Liu's avatar
Chao Liu committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;

112
113
} // namespace ck
#endif