raw_data.hpp 2.98 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
11
12
13
14
15
/**
 * @brief Provides a base class for common operations with raw buffer
 * 
 * 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
template <class Derived>
Paul's avatar
Paul committed
17
18
19
20
21
22
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
23
        bool result   = x.empty() && y.empty();
Paul's avatar
Paul committed
24
25
26
27
28
29
30
31
        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
32
                result     = xview == yview;
Paul's avatar
Paul committed
33
34
35
36
37
            });
        }
        return result;
    }

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

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

Paul's avatar
Paul committed
47
48
49
50
51
52
    /**
     * @brief Visits a single data element at a certain index.
     * 
     * @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
53
54
    template <class Visitor>
    void visit_at(Visitor v, std::size_t n = 0) const
Paul's avatar
Paul committed
55
    {
Paul's avatar
Paul committed
56
57
58
        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
59
60
    }

Paul's avatar
Paul committed
61
62
63
64
65
66
67
    /**
     * @brief Visits the data
     * 
     *  This will call the visitor function with a `tensor_view<T>` based on the shape of the data.
     * 
     * @param v A function to be called with `tensor_view<T>`
     */
Paul's avatar
Paul committed
68
    template <class Visitor>
Paul's avatar
Paul committed
69
70
    void visit(Visitor v) const
    {
Paul's avatar
Paul committed
71
72
73
        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
74
75
76
77
    }

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

Paul's avatar
Paul committed
82
83
84
85
86
87
88
89
    /**
     * @brief Retrieves a single element of data
     * @details [long description]
     * 
     * @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
99
100
101
        return result;
    }
};

} // namespace rtg

#endif