tensor_view.hpp 3.12 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
#ifndef RTG_GUARD_TENSOR_VIEW_HPP
#define RTG_GUARD_TENSOR_VIEW_HPP

#include <rtg/shape.hpp>
Paul's avatar
Paul committed
5
#include <rtg/float_equal.hpp>
Paul's avatar
Paul committed
6
7
8
9
10

#include <iostream>

namespace rtg {

Paul's avatar
Paul committed
11
template <class T>
Paul's avatar
Paul committed
12
13
struct tensor_view
{
14
    using value_type = T;
Paul's avatar
Paul committed
15
16
    tensor_view() : m_data(nullptr) {}
    tensor_view(shape s, T* d) : m_data(d), m_shape(s) {}
Paul's avatar
Paul committed
17

Paul's avatar
Paul committed
18
    const shape& get_shape() const { return this->m_shape; }
Paul's avatar
Paul committed
19

Paul's avatar
Paul committed
20
    bool empty() const { return m_data == nullptr || m_shape.lens().empty(); }
Paul's avatar
Paul committed
21

Paul's avatar
Paul committed
22
    std::size_t size() const { return m_shape.elements(); }
Paul's avatar
Paul committed
23

Paul's avatar
Paul committed
24
    T* data() { return this->m_data; }
Paul's avatar
Paul committed
25

Paul's avatar
Paul committed
26
    const T* data() const { return this->m_data; }
Paul's avatar
Paul committed
27
28

    template <class... Ts>
Paul's avatar
Paul committed
29
30
    const T& operator()(Ts... xs) const
    {
Paul's avatar
Paul committed
31
        return m_data[m_shape.index({xs...})];
Paul's avatar
Paul committed
32
33
    }

Paul's avatar
Paul committed
34
    template <class... Ts>
Paul's avatar
Paul committed
35
36
    T& operator()(Ts... xs)
    {
Paul's avatar
Paul committed
37
        return m_data[m_shape.index({static_cast<std::size_t>(xs)...})];
Paul's avatar
Paul committed
38
39
40
41
42
    }

    T& operator[](std::size_t i)
    {
        assert(!this->empty() && i < this->size());
Paul's avatar
Paul committed
43
        return m_data[m_shape.index(i)];
Paul's avatar
Paul committed
44
45
46
47
48
    }

    const T& operator[](std::size_t i) const
    {
        assert(!this->empty() && i < this->size());
Paul's avatar
Paul committed
49
        return m_data[m_shape.index(i)];
Paul's avatar
Paul committed
50
51
52
53
54
    }

    T& front()
    {
        assert(!this->empty());
Paul's avatar
Paul committed
55
        return m_data[0];
Paul's avatar
Paul committed
56
57
58
59
60
    }

    const T& front() const
    {
        assert(!this->empty());
Paul's avatar
Paul committed
61
        return m_data[0];
Paul's avatar
Paul committed
62
63
64
65
66
    }

    T& back()
    {
        assert(!this->empty());
Paul's avatar
Paul committed
67
        return m_data[m_shape.index(this->size() - 1)];
Paul's avatar
Paul committed
68
69
70
71
72
    }

    const T& back() const
    {
        assert(!this->empty());
Paul's avatar
Paul committed
73
        return m_data[m_shape.index(this->size() - 1)];
Paul's avatar
Paul committed
74
75
76
77
78
    }

    // TODO: Add iterators so it can handle nonpacked tensors
    T* begin()
    {
Paul's avatar
Paul committed
79
80
        assert(this->m_shape.packed());
        return m_data;
Paul's avatar
Paul committed
81
82
83
84
    }

    T* end()
    {
Paul's avatar
Paul committed
85
        assert(this->m_shape.packed());
Paul's avatar
Paul committed
86
        if(this->empty())
Paul's avatar
Paul committed
87
            return m_data;
Paul's avatar
Paul committed
88
        else
Paul's avatar
Paul committed
89
            return m_data + this->size();
Paul's avatar
Paul committed
90
91
92
93
    }

    const T* begin() const
    {
Paul's avatar
Paul committed
94
95
        assert(this->m_shape.packed());
        return m_data;
Paul's avatar
Paul committed
96
97
98
99
    }

    const T* end() const
    {
Paul's avatar
Paul committed
100
        assert(this->m_shape.packed());
Paul's avatar
Paul committed
101
        if(this->empty())
Paul's avatar
Paul committed
102
            return m_data;
Paul's avatar
Paul committed
103
        else
Paul's avatar
Paul committed
104
            return m_data + this->size();
Paul's avatar
Paul committed
105
106
107
108
    }

    friend bool operator==(const tensor_view<T>& x, const tensor_view<T>& y)
    {
Paul's avatar
Paul committed
109
        if(x.m_shape == y.m_shape)
Paul's avatar
Paul committed
110
        {
Paul's avatar
Paul committed
111
            for(std::size_t i = 0; i < x.m_shape.elements(); i++)
Paul's avatar
Paul committed
112
            {
Paul's avatar
Paul committed
113
114
                if(!float_equal(x[i], y[i]))
                    return false;
Paul's avatar
Paul committed
115
116
117
118
119
120
            }
            return true;
        }
        return false;
    }

Paul's avatar
Paul committed
121
    friend bool operator!=(const tensor_view<T>& x, const tensor_view<T>& y) { return !(x == y); }
Paul's avatar
Paul committed
122

Paul's avatar
Paul committed
123
124
125
126
127
    friend std::ostream& operator<<(std::ostream& os, const tensor_view<T>& x)
    {
        if(!x.empty())
        {
            os << x.front();
Paul's avatar
Paul committed
128
            for(std::size_t i = 1; i < x.m_shape.elements(); i++)
Paul's avatar
Paul committed
129
            {
Paul's avatar
Paul committed
130
                os << ", " << x.m_data[x.m_shape.index(i)];
Paul's avatar
Paul committed
131
132
133
134
135
            }
        }
        return os;
    }

Paul's avatar
Paul committed
136
    private:
Paul's avatar
Paul committed
137
138
    T* m_data;
    shape m_shape;
Paul's avatar
Paul committed
139
140
};

Paul's avatar
Paul committed
141
template <class T>
Paul's avatar
Paul committed
142
143
144
145
146
147
148
149
tensor_view<T> make_view(shape s, T* data)
{
    return {s, data};
}

} // namespace rtg

#endif