raw_data.hpp 3.43 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4

#ifndef RTG_GUARD_RAW_DATA_HPP
#define RTG_GUARD_RAW_DATA_HPP

Paul's avatar
Paul committed
5
6
#include <rtg/tensor_view.hpp>

Paul's avatar
Paul committed
7
8
namespace rtg {

Paul's avatar
Paul committed
9
10
/**
 * @brief Provides a base class for common operations with raw buffer
Paul's avatar
Paul committed
11
12
13
14
15
 *
 * For classes that handle a raw buffer of data, this will provide common operations such as equals,
 * printing, and visitors. To use this class the derived class needs to provide a `data()` method to
 * retrieve a raw pointer to the data, and `get_shape` method that provides the shape of the data.
 *
Paul's avatar
Paul committed
16
 */
Paul's avatar
Paul committed
17
template <class Derived>
Paul's avatar
Paul committed
18
19
20
21
22
23
struct raw_data
{
    friend bool operator==(const Derived& x, const Derived& y)
    {
        auto&& xshape = x.get_shape();
        auto&& yshape = y.get_shape();
Paul's avatar
Paul committed
24
        bool result   = x.empty() && y.empty();
Paul's avatar
Paul committed
25
26
27
28
29
30
31
32
        if(not result && xshape == yshape)
        {
            auto&& xbuffer = x.data();
            auto&& ybuffer = y.data();
            // TODO: Dont use tensor view for single values
            xshape.visit_type([&](auto as) {
                auto xview = make_view(xshape, as.from(xbuffer));
                auto yview = make_view(yshape, as.from(ybuffer));
Paul's avatar
Paul committed
33
                result     = xview == yview;
Paul's avatar
Paul committed
34
35
36
37
38
            });
        }
        return result;
    }

Paul's avatar
Paul committed
39
40
41
    friend bool operator!=(const Derived& x, const Derived& y) { return !(x == y); }

    template <class Stream>
Paul's avatar
Paul committed
42
43
    friend Stream& operator<<(Stream& os, const Derived& d)
    {
Paul's avatar
Paul committed
44
        d.visit([&](auto x) { os << x; });
Paul's avatar
Paul committed
45
46
        return os;
    }
Paul's avatar
Paul committed
47

Paul's avatar
Paul committed
48
49
    /**
     * @brief Visits a single data element at a certain index.
Paul's avatar
Paul committed
50
     *
Paul's avatar
Paul committed
51
52
53
     * @param v A function which will be called with the type of data
     * @param n The index to read from
     */
Paul's avatar
Paul committed
54
55
    template <class Visitor>
    void visit_at(Visitor v, std::size_t n = 0) const
Paul's avatar
Paul committed
56
    {
Paul's avatar
Paul committed
57
58
59
        auto&& s      = static_cast<const Derived&>(*this).get_shape();
        auto&& buffer = static_cast<const Derived&>(*this).data();
        s.visit_type([&](auto as) { v(*(as.from(buffer) + s.index(n))); });
Paul's avatar
Paul committed
60
61
    }

Paul's avatar
Paul committed
62
63
    /**
     * @brief Visits the data
Paul's avatar
Paul committed
64
     *
Paul's avatar
Paul committed
65
     *  This will call the visitor function with a `tensor_view<T>` based on the shape of the data.
Paul's avatar
Paul committed
66
     *
Paul's avatar
Paul committed
67
68
     * @param v A function to be called with `tensor_view<T>`
     */
Paul's avatar
Paul committed
69
    template <class Visitor>
Paul's avatar
Paul committed
70
71
    void visit(Visitor v) const
    {
Paul's avatar
Paul committed
72
73
74
        auto&& s      = static_cast<const Derived&>(*this).get_shape();
        auto&& buffer = static_cast<const Derived&>(*this).data();
        s.visit_type([&](auto as) { v(make_view(s, as.from(buffer))); });
Paul's avatar
Paul committed
75
76
77
78
    }

    bool single() const
    {
Paul's avatar
Paul committed
79
        auto&& s = static_cast<const Derived&>(*this).get_shape();
Paul's avatar
Paul committed
80
        return s.elements() == 1;
Paul's avatar
Paul committed
81
82
    }

Paul's avatar
Paul committed
83
84
    /**
     * @brief Retrieves a single element of data
Paul's avatar
Paul committed
85
     *
Paul's avatar
Paul committed
86
87
88
89
     * @param n The index to retrieve the data from
     * @tparam T The type of data to be retrieved
     * @return The element as `T`
     */
Paul's avatar
Paul committed
90
91
    template <class T>
    T at(std::size_t n = 0) const
Paul's avatar
Paul committed
92
93
    {
        T result;
Paul's avatar
Paul committed
94
        this->visit_at([&](auto x) { result = x; }, n);
Paul's avatar
Paul committed
95
96
97
98
        return result;
    }
};

Paul's avatar
Paul committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
template<class T, class... Ts>
auto visit_all(T&& x, Ts&&... xs)
{
    auto&& s = x.get_shape();
    std::initializer_list<shape::type_t> types = {xs.get_shape().type()...};
    if (!std::all_of(types.begin(), types.end(), [&](shape::type_t t) { return t == s.type(); }))
        RTG_THROW("Types must be the same");
    return [&](auto v) {
        s.visit_type([&](auto as) { 
            v(make_view(s, as.from(x.data())), make_view(xs.get_shape(), as.from(xs.data()))...); 
        });
    };
}

Paul's avatar
Paul committed
113
114
115
} // namespace rtg

#endif