"src/targets/vscode:/vscode.git/clone" did not exist on "992666e69285305914a291c2d01acbe879c3a2d0"
shape.hpp 3.3 KB
Newer Older
Paul's avatar
Paul committed
1
#ifndef RTG_GUARD_RTGLIB_SHAPE_HPP
Paul's avatar
Paul committed
2
#define RTG_GUARD_RTGLIB_SHAPE_HPP
Paul's avatar
Paul committed
3
4
5

#include <vector>
#include <cassert>
Paul's avatar
Paul committed
6
#include <ostream>
Paul's avatar
Paul committed
7

Paul's avatar
Paul committed
8
9
#include <rtg/errors.hpp>

Paul's avatar
Paul committed
10
11
12
13
namespace rtg {

struct shape
{
Paul's avatar
Paul committed
14
15

// Add new types here
Paul's avatar
Paul committed
16
// clang-format off
Paul's avatar
Paul committed
17
18
#define RTG_SHAPE_VISIT_TYPES(m) \
    m(float_type, float) \
Paul's avatar
Paul committed
19
20
21
22
23
24
25
26
    m(double_type, double) \
    m(uint8_type, uint8_t) \
    m(int8_type, int8_t) \
    m(uint16_type, uint16_t) \
    m(int16_type, int16_t) \
    m(int32_type, int32_t) \
    m(int64_type, int64_t) \
    m(uint32_type, uint32_t) \
Paul's avatar
Paul committed
27
    m(uint64_type, uint64_t)
Paul's avatar
Paul committed
28
// clang-format on
Paul's avatar
Paul committed
29

Paul's avatar
Paul committed
30
#define RTG_SHAPE_ENUM_TYPES(x, t) x,
Paul's avatar
Paul committed
31
32
    enum type_t
    {
Paul's avatar
Paul committed
33
        RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_ENUM_TYPES)
Paul's avatar
Paul committed
34
    };
Paul's avatar
Paul committed
35
36
#undef RTG_SHAPE_ENUM_TYPES

Paul's avatar
Paul committed
37
    template <class T, class = void>
Paul's avatar
Paul committed
38
    struct get_type;
Paul's avatar
Paul committed
39
40
#define RTG_SHAPE_GET_TYPE(x, t)                              \
    template <class T>                                        \
Paul's avatar
Paul committed
41
    struct get_type<t, T> : std::integral_constant<type_t, x> \
Paul's avatar
Paul committed
42
43
    {                                                         \
    };
Paul's avatar
Paul committed
44
45
    RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_GET_TYPE)
#undef RTG_SHAPE_GET_TYPE
Paul's avatar
Paul committed
46
47
48
49
50
51
52

    shape();
    shape(type_t t);
    shape(type_t t, std::vector<std::size_t> l);
    shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s);

    type_t type() const;
Paul's avatar
Paul committed
53
54
    const std::vector<std::size_t>& lens() const;
    const std::vector<std::size_t>& strides() const;
Paul's avatar
Paul committed
55
56
57
    std::size_t elements() const;
    std::size_t bytes() const;

Paul's avatar
Paul committed
58
59
60
    std::size_t index(std::initializer_list<std::size_t> l) const;
    std::size_t index(const std::vector<std::size_t>& l) const;

Paul's avatar
Paul committed
61
62
63
64
65
    // Map element index to space index
    std::size_t index(std::size_t i) const;

    bool packed() const;

Paul's avatar
Paul committed
66
67
    friend bool operator==(const shape& x, const shape& y);
    friend bool operator!=(const shape& x, const shape& y);
Paul's avatar
Paul committed
68
    friend std::ostream& operator<<(std::ostream& os, const shape& x);
Paul's avatar
Paul committed
69

Paul's avatar
Paul committed
70
    template <class T>
Paul's avatar
Paul committed
71
72
73
74
    struct as
    {
        using type = T;

Paul's avatar
Paul committed
75
        template <class U>
Paul's avatar
Paul committed
76
77
78
79
80
        T operator()(U u) const
        {
            return T(u);
        }

Paul's avatar
Paul committed
81
        template <class U>
Paul's avatar
Paul committed
82
83
84
85
86
        T* operator()(U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
87
        template <class U>
Paul's avatar
Paul committed
88
89
90
91
92
        const T* operator()(const U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
93
        T operator()() const { return {}; }
Paul's avatar
Paul committed
94

Paul's avatar
Paul committed
95
        std::size_t size(std::size_t n = 1) const { return sizeof(T) * n; }
Paul's avatar
Paul committed
96

Paul's avatar
Paul committed
97
98
        template <class U>
        T* from(U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
99
        {
Paul's avatar
Paul committed
100
            return reinterpret_cast<T*>(buffer) + n;
Paul's avatar
Paul committed
101
        }
Paul's avatar
Paul committed
102

Paul's avatar
Paul committed
103
104
        template <class U>
        const T* from(const U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
105
        {
Paul's avatar
Paul committed
106
            return reinterpret_cast<const T*>(buffer) + n;
Paul's avatar
Paul committed
107
        }
Paul's avatar
Paul committed
108
109
    };

Paul's avatar
Paul committed
110
    template <class Visitor>
Paul's avatar
Paul committed
111
112
    void visit_type(Visitor v) const
    {
Paul's avatar
Paul committed
113
        switch(this->m_type)
Paul's avatar
Paul committed
114
        {
Paul's avatar
Paul committed
115
#define RTG_SHAPE_VISITOR_CASE(x, t) \
Paul's avatar
Paul committed
116
    case x: v(as<t>()); return;
Paul's avatar
Paul committed
117
118
            RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_VISITOR_CASE)
#undef RTG_SHAPE_VISITOR_CASE
Paul's avatar
Paul committed
119
        }
Paul's avatar
Paul committed
120
        RTG_THROW("Unknown type");
Paul's avatar
Paul committed
121
    }
Paul's avatar
Paul committed
122
123

    private:
Paul's avatar
Paul committed
124
125
126
127
    type_t m_type;
    std::vector<std::size_t> m_lens;
    std::vector<std::size_t> m_strides;
    bool m_packed;
Paul's avatar
Paul committed
128
129
130

    void calculate_strides();
    std::size_t element_space() const;
Paul's avatar
Paul committed
131
    std::string type_string() const;
Paul's avatar
Paul committed
132
133
};

Paul's avatar
Paul committed
134
} // namespace rtg
Paul's avatar
Paul committed
135
136

#endif