MUDA
Loading...
Searching...
No Matches
dense_1d.h
Go to the documentation of this file.
1/*****************************************************************/
10#pragma once
11#include <muda/viewer/viewer_base.h>
12
13namespace muda
14{
15
16/*****************************************************************************
17 *
18 * Dense1D (1D array)
19 * indexing (x)
20 *
21 * A C/C++ array like viewer.
22 *
23 *****************************************************************************/
24
25template <bool IsConst, typename T>
26class Dense1DT : public ViewerBase<IsConst>
27{
29 template <typename U>
30 using auto_const_t = typename Base::template auto_const_t<U>;
31
32 MUDA_VIEWER_COMMON_NAME(Dense1DT);
33
34 public:
38
39 protected:
40 auto_const_t<T>* m_data = nullptr;
41 int m_dim = 0;
42
43 public:
44 using value_type = T;
45
46 MUDA_GENERIC Dense1DT() MUDA_NOEXCEPT = default;
47
48 MUDA_GENERIC Dense1DT(auto_const_t<T>* p, int dim) MUDA_NOEXCEPT : m_data(p),
49 m_dim(dim)
50 {
51 }
52
53 MUDA_GENERIC Dense1DT(const Dense1DT& other) = default;
54
55 template <bool OtherIsConst>
56 MUDA_GENERIC Dense1DT(const Dense1DT<OtherIsConst, T>& other) MUDA_NOEXCEPT
57 MUDA_REQUIRES(!OtherIsConst)
58 : m_data(other.data())
59 , m_dim(other.dim())
60 {
61 static_assert(OtherIsConst, "Only non-const viewer can be convert to const viewer");
62 }
63
64 MUDA_GENERIC auto as_const() const MUDA_NOEXCEPT
65 {
66 return ConstViewer{*this};
67 }
68
69 MUDA_GENERIC auto_const_t<T>& operator()(int x) const MUDA_NOEXCEPT
70 {
71 check();
72 return m_data[map(x)];
73 }
74
75 MUDA_GENERIC auto_const_t<T>* data() const MUDA_NOEXCEPT { return m_data; }
76
77 MUDA_GENERIC int total_size() const MUDA_NOEXCEPT { return m_dim; }
78
79 MUDA_GENERIC int dim() const MUDA_NOEXCEPT { return m_dim; }
80
81 MUDA_GENERIC ThisViewer subview(int offset) const MUDA_NOEXCEPT
82 {
83 auto size = this->m_dim - offset;
84 if constexpr(DEBUG_VIEWER)
85 {
86 if(offset < 0)
87 MUDA_KERNEL_ERROR("Dense1D[%s:%s]: subview out of range, offset=%d size=%d m_dim=(%d). %s(%d)",
88 this->name(),
89 this->kernel_name(),
90 offset,
91 size,
92 this->m_dim,
93 this->kernel_file(),
94 this->kernel_line());
95 }
96 return ThisViewer{this->m_data + offset, size};
97 }
98
99 MUDA_GENERIC ThisViewer subview(int offset, int size) const MUDA_NOEXCEPT
100 {
101 if constexpr(DEBUG_VIEWER)
102 {
103 if(offset < 0 || offset + size > m_dim)
104 MUDA_KERNEL_ERROR("Dense1D[%s:%s]: subview out of range, offset=%d size=%d m_dim=(%d). %s(%d)",
105 this->name(),
106 this->kernel_name(),
107 offset,
108 size,
109 this->m_dim,
110 this->kernel_file(),
111 this->kernel_line());
112 }
113 return ThisViewer{this->m_data + offset, size};
114 }
115
116 protected:
117 MUDA_INLINE MUDA_GENERIC void check() const MUDA_NOEXCEPT
118 {
119 if constexpr(DEBUG_VIEWER)
120 if(m_data == nullptr)
121 MUDA_KERNEL_ERROR("Dense1D[%s:%s]: m_data is null. %s(%d)",
122 this->name(),
123 this->kernel_name(),
124 this->kernel_file(),
125 this->kernel_line());
126 }
127
128 MUDA_GENERIC int map(int x) const MUDA_NOEXCEPT
129 {
130 if constexpr(DEBUG_VIEWER)
131 if(!(x >= 0 && x < m_dim))
132 MUDA_KERNEL_ERROR("Dense1D[%s:%s]: out of range, index=(%d) m_dim=(%d). %s(%d)",
133 this->name(),
134 this->kernel_name(),
135 x,
136 m_dim,
137 this->kernel_file(),
138 this->kernel_line());
139 return x;
140 }
141};
142
143template <typename T>
145
146template <typename T>
148
149// viewer traits
150template <typename T>
152{
153 using type = CDense1D<T>;
154};
155
156template <typename T>
158{
159 using type = Dense1D<T>;
160};
161
162// make functions
163template <typename T>
164MUDA_INLINE MUDA_GENERIC auto make_cdense_1d(const T* data, int dimx) MUDA_NOEXCEPT
165{
166 return CDense1D<T>(data, dimx);
167}
168
169template <typename T, int N>
170MUDA_INLINE MUDA_GENERIC auto make_cdense_1d(const T (&data)[N]) MUDA_NOEXCEPT
171{
172 return CDense1D<T>(data, N);
173}
174
175template <typename T>
176MUDA_INLINE MUDA_GENERIC auto make_dense_1d(T* data, int dimx) MUDA_NOEXCEPT
177{
178 return Dense1D<T>(data, dimx);
179}
180
181template <typename T, int N>
182MUDA_INLINE MUDA_GENERIC auto make_dense_1d(T (&data)[N]) MUDA_NOEXCEPT
183{
184 return Dense1D<T>(data, N);
185}
186} // namespace muda
Definition dense_1d.h:27
Definition viewer_base.h:18
Definition type_modifier.h:22
Definition type_modifier.h:28