literal.hpp 2.12 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
5
6
7
#include <migraph/shape.hpp>
#include <migraph/argument.hpp>
#include <migraph/tensor_view.hpp>
#include <migraph/raw_data.hpp>
Paul's avatar
Paul committed
8

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

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

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

Paul's avatar
Paul committed
26
    template <class T>
Paul's avatar
Paul committed
27
    literal(shape s, const std::vector<T>& x) : buffer(s.bytes(), 0), m_shape(s)
Paul's avatar
Paul committed
28
    {
Paul's avatar
Paul committed
29
30
        assert(s.packed());
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
31
        s.visit_type([&](auto as) { std::copy(x.begin(), x.end(), as.from(buffer.data())); });
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
37
    {
        assert(s.packed());
Paul's avatar
Paul committed
38
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
39
        s.visit_type([&](auto as) { std::copy(x.begin(), x.end(), as.from(buffer.data())); });
Paul's avatar
Paul committed
40
41
    }

Paul's avatar
Paul committed
42
    template <class Iterator>
Paul's avatar
Paul committed
43
    literal(shape s, Iterator start, Iterator end) : buffer(s.bytes(), 0), m_shape(s)
Paul's avatar
Paul committed
44
45
    {
        assert(s.packed());
Paul's avatar
Paul committed
46
        s.visit_type([&](auto as) { std::copy(start, end, as.from(buffer.data())); });
Paul's avatar
Paul committed
47
48
    }

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

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

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

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

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

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

Paul's avatar
Paul committed
71
} // namespace migraph
Paul's avatar
Paul committed
72
73

#endif