literal.hpp 2.73 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
11
#include <memory>

Paul's avatar
Paul committed
12
namespace migraph {
Paul's avatar
Paul committed
13

Paul's avatar
Paul committed
14
15
16
17
/**
 * @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
18
struct literal : raw_data<literal>
Paul's avatar
Paul committed
19
{
Paul's avatar
Paul committed
20
    literal() {}
Paul's avatar
Paul committed
21

Paul's avatar
Paul committed
22
    template <class T>
Paul's avatar
Paul committed
23
    literal(T x) : buffer(std::make_unique<char[]>(sizeof(T))), m_shape(shape::get_type<T>{})
Paul's avatar
Paul committed
24
25
    {
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
26
        *(reinterpret_cast<T*>(buffer.get())) = x;
Paul's avatar
Paul committed
27
28
    }

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

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

Paul's avatar
Paul committed
43
    template <class Iterator>
Paul's avatar
Paul committed
44
    literal(shape s, Iterator start, Iterator end) : buffer(std::make_unique<char[]>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
45
    {
Paul's avatar
Paul committed
46
        fill(start, end);
Paul's avatar
Paul committed
47
48
    }

Paul's avatar
Paul committed
49
50
51
52
    literal(shape s, const char* x) : buffer(std::make_unique<char[]>(s.bytes())), m_shape(s) 
    {
        std::copy(x, x + s.bytes(), buffer.get());
    }
Paul's avatar
Paul committed
53

Paul's avatar
Paul committed
54
    /// Whether data is available
Paul's avatar
Paul committed
55
    bool empty() const { return this->buffer == nullptr; }
Paul's avatar
Paul committed
56

Paul's avatar
Paul committed
57
    /// Provides a raw pointer to the data
Paul's avatar
Paul committed
58
    const char* data() const { return this->buffer.get(); }
Paul's avatar
Paul committed
59

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

Paul's avatar
Paul committed
62
    /// Convert the data to an argument
Paul's avatar
Paul committed
63
64
    argument get_argument() const
    {
Paul's avatar
Paul committed
65
        std::vector<char> b(buffer.get(), buffer.get() + m_shape.bytes());
Paul's avatar
Paul committed
66
        return {m_shape, [b]() mutable { return b.data(); }};
Paul's avatar
Paul committed
67
68
    }

Paul's avatar
Paul committed
69
    private:
Paul's avatar
Paul committed
70
    std::shared_ptr<char> buffer;
Paul's avatar
Paul committed
71
    shape m_shape;
Paul's avatar
Paul committed
72
73
74
75

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

Paul's avatar
Paul committed
94
} // namespace migraph
Paul's avatar
Paul committed
95
96

#endif