"vscode:/vscode.git/clone" did not exist on "5a62e9e7661af7d132be01216420265cb8c8380a"
literal.hpp 2.49 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_LITERAL_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_LITERAL_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <migraph/shape.hpp>
Paul's avatar
Paul committed
5
#include <migraph/shape_for_each.hpp>
Paul's avatar
Paul committed
6
7
8
#include <migraph/argument.hpp>
#include <migraph/tensor_view.hpp>
#include <migraph/raw_data.hpp>
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
namespace migraph {
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
13
14
15
/**
 * @brief Represents a raw literal
 * @details This stores the literal has a raw buffer that is owned by this class
 */
Paul's avatar
Paul committed
16
struct literal : raw_data<literal>
Paul's avatar
Paul committed
17
{
Paul's avatar
Paul committed
18
    literal() {}
Paul's avatar
Paul committed
19

Paul's avatar
Paul committed
20
    template <class T>
Paul's avatar
Paul committed
21
    literal(T x) : buffer(sizeof(T), 0), m_shape(shape::get_type<T>{})
Paul's avatar
Paul committed
22
23
24
25
26
    {
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
        *(reinterpret_cast<T*>(buffer.data())) = x;
    }

Paul's avatar
Paul committed
27
    template <class T>
Paul's avatar
Paul committed
28
    literal(shape s, const std::vector<T>& x) : buffer(s.bytes(), 0), m_shape(s)
Paul's avatar
Paul committed
29
    {
Paul's avatar
Paul committed
30
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
31
        fill(x.begin(), x.end());
Paul's avatar
Paul committed
32
33
    }

Paul's avatar
Paul committed
34
    template <class T>
Paul's avatar
Paul committed
35
    literal(shape s, const std::initializer_list<T>& x) : buffer(s.bytes(), 0), m_shape(s)
Paul's avatar
Paul committed
36
    {
Paul's avatar
Paul committed
37
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
38
        fill(x.begin(), x.end());
Paul's avatar
Paul committed
39
40
    }

Paul's avatar
Paul committed
41
    template <class Iterator>
Paul's avatar
Paul committed
42
    literal(shape s, Iterator start, Iterator end) : buffer(s.bytes(), 0), m_shape(s)
Paul's avatar
Paul committed
43
    {
Paul's avatar
Paul committed
44
        fill(start, end);
Paul's avatar
Paul committed
45
46
    }

Paul's avatar
Paul committed
47
    literal(shape s, const char* x) : buffer(x, x + s.bytes()), m_shape(s) {}
Paul's avatar
Paul committed
48

Paul's avatar
Paul committed
49
    /// Whether data is available
Paul's avatar
Paul committed
50
    bool empty() const { return this->buffer.empty(); }
Paul's avatar
Paul committed
51

Paul's avatar
Paul committed
52
    /// Provides a raw pointer to the data
Paul's avatar
Paul committed
53
54
    const char* data() const { return this->buffer.data(); }

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

Paul's avatar
Paul committed
57
    /// Convert the data to an argument
Paul's avatar
Paul committed
58
59
60
    argument get_argument() const
    {
        auto b = buffer;
Paul's avatar
Paul committed
61
        return {m_shape, [b]() mutable { return b.data(); }};
Paul's avatar
Paul committed
62
63
    }

Paul's avatar
Paul committed
64
    private:
Paul's avatar
Paul committed
65
    std::vector<char> buffer;
Paul's avatar
Paul committed
66
    shape m_shape;
Paul's avatar
Paul committed
67
68
69
70

    template <class Iterator>
    void fill(Iterator start, Iterator end)
    {
Paul's avatar
Paul committed
71
        if(m_shape.standard())
Paul's avatar
Paul committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        {
            m_shape.visit_type([&](auto as) { std::copy(start, end, as.from(buffer.data())); });
        }
        else
        {
            auto it = start;
            m_shape.visit_type([&](auto as) {
                auto output = make_view(m_shape, as.from(buffer.data()));
                shape_for_each(output.get_shape(), [&](const auto& idx) {
                    it++;
                    output(idx.begin(), idx.end()) = *it;
                });
            });
        }
    }
Paul's avatar
Paul committed
87
88
};

Paul's avatar
Paul committed
89
} // namespace migraph
Paul's avatar
Paul committed
90
91

#endif