"tests/configs/ppocr_det_server_params.txt" did not exist on "b4d7be33f0a37bf729316491cc75e927895d5bff"
raw_data.hpp 1.96 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
template <class Derived>
Paul's avatar
Paul committed
10
11
12
13
14
15
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
16
        bool result   = x.empty() && y.empty();
Paul's avatar
Paul committed
17
18
19
20
21
22
23
24
        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
25
                result     = xview == yview;
Paul's avatar
Paul committed
26
27
28
29
30
            });
        }
        return result;
    }

Paul's avatar
Paul committed
31
32
33
    friend bool operator!=(const Derived& x, const Derived& y) { return !(x == y); }

    template <class Stream>
Paul's avatar
Paul committed
34
35
    friend Stream& operator<<(Stream& os, const Derived& d)
    {
Paul's avatar
Paul committed
36
        d.visit([&](auto x) { os << x; });
Paul's avatar
Paul committed
37
38
        return os;
    }
Paul's avatar
Paul committed
39

Paul's avatar
Paul committed
40
41
    template <class Visitor>
    void visit_at(Visitor v, std::size_t n = 0) const
Paul's avatar
Paul committed
42
    {
Paul's avatar
Paul committed
43
44
45
        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
46
47
    }

Paul's avatar
Paul committed
48
    template <class Visitor>
Paul's avatar
Paul committed
49
50
    void visit(Visitor v) const
    {
Paul's avatar
Paul committed
51
52
53
        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
54
55
56
57
    }

    bool single() const
    {
Paul's avatar
Paul committed
58
        auto&& s = static_cast<const Derived&>(*this).get_shape();
Paul's avatar
Paul committed
59
        return s.elements() == 1;
Paul's avatar
Paul committed
60
61
    }

Paul's avatar
Paul committed
62
63
    template <class T>
    T at(std::size_t n = 0) const
Paul's avatar
Paul committed
64
65
    {
        T result;
Paul's avatar
Paul committed
66
        this->visit_at([&](auto x) { result = x; }, n);
Paul's avatar
Paul committed
67
68
69
70
71
72
73
        return result;
    }
};

} // namespace rtg

#endif