-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.lua
More file actions
218 lines (183 loc) · 4.6 KB
/
index.lua
File metadata and controls
218 lines (183 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
--- Core index module.
-- Modules --
local af = require("arrayfire_lib")
-- Forward declarations --
-- Exports --
local M = {}
-- See also: https://github.com/arrayfire/arrayfire/blob/devel/src/api/cpp/index.cpp
--
function M.Add (array_module)
-- Import these here since the array module is not yet registered.
--[[
///
/// \brief Wrapper for af_index.
///
/// This class is a wrapper for the af_index struct in the C interface. It
/// allows implicit type conversion from valid indexing types like int,
/// \ref af::seq, \ref af_seq, and \ref af::array.
///
/// \note This is a helper class and does not necessarily need to be created
/// explicitly. It is used in the operator() overloads to simplify the API.
///
class AFAPI index {
af_index_t impl;
public:
///
/// \brief Default constructor. Equivalent to \ref af::span
///
index();
~index();
///
/// \brief Implicit int converter
///
/// Indexes the af::array at index \p idx
///
/// \param[in] idx is the id of the index
///
/// \sa indexing
///
index(const int idx);
///
/// \brief Implicit seq converter
///
/// Indexes the af::array using an \ref af::seq object
///
/// \param[in] s0 is the set of indices to parse
///
/// \sa indexing
///
index(const af::seq& s0);
///
/// \brief Implicit seq converter
///
/// Indexes the af::array using an \ref af_seq object
///
/// \param[in] s0 is the set of indices to parse
///
/// \sa indexing
///
index(const af_seq& s0);
///
/// \brief Implicit int converter
///
/// Indexes the af::array using an \ref af::array object
///
/// \param[in] idx0 is the set of indices to parse
///
/// \sa indexing
///
index(const af::array& idx0);
#if AF_API_VERSION >= 31
///
/// \brief Copy constructor
///
/// \param[in] idx0 is index to copy.
///
/// \sa indexing
///
index(const index& idx0);
#endif
///
/// \brief Returns true if the \ref af::index represents a af::span object
///
/// \returns true if the af::index is an af::span
///
bool isspan() const;
///
/// \brief Gets the underlying af_index_t object
///
/// \returns the af_index_t represented by this object
///
const af_index_t& get() const;
]]
--[[
array lookup(const array &in, const array &idx, const int dim)
{
af_array out = 0;
AF_THROW(af_lookup(&out, in.get(), idx.get(), getFNSD(dim, in.dims())));
return array(out);
}
void copy(array &dst, const array &src,
const index &idx0,
const index &idx1,
const index &idx2,
const index &idx3)
{
unsigned nd = dst.numdims();
af_index_t indices[] = {idx0.get(),
idx1.get(),
idx2.get(),
idx3.get()};
af_array lhs = dst.get();
const af_array rhs = src.get();
AF_THROW(af_assign_gen(&lhs, lhs, nd, indices, rhs));
}
index::index() {
impl.idx.seq = af_span;
impl.isSeq = true;
impl.isBatch = false;
}
index::index(const int idx) {
impl.idx.seq = af_make_seq(idx, idx, 1);
impl.isSeq = true;
impl.isBatch = false;
}
index::index(const af::seq& s0) {
impl.idx.seq = s0.s;
impl.isSeq = true;
impl.isBatch = s0.m_gfor;
}
index::index(const af_seq& s0) {
impl.idx.seq = s0;
impl.isSeq = true;
impl.isBatch = false;
}
index::index(const af::array& idx0) {
array idx = idx0.isbool() ? where(idx0) : idx0;
af_array arr = 0;
AF_THROW(af_retain_array(&arr, idx.get()));
impl.idx.arr = arr;
impl.isSeq = false;
impl.isBatch = false;
}
index::index(const af::index& idx0) {
*this = idx0;
}
index::~index() {
if (!impl.isSeq)
af_release_array(impl.idx.arr);
}
index & index::operator=(const index& idx0) {
impl = idx0.get();
if(impl.isSeq == false){
// increment reference count to avoid double free
// when/if idx0 is destroyed
AF_THROW(af_retain_array(&impl.idx.arr, impl.idx.arr));
}
return *this;
}
#if __cplusplus > 199711L
index::index(index &&idx0) {
impl = idx0.impl;
}
index& index::operator=(index &&idx0) {
impl = idx0.impl;
return *this;
}
#endif
static bool operator==(const af_seq& lhs, const af_seq& rhs) {
return lhs.begin == rhs.begin && lhs.end == rhs.end && lhs.step == rhs.step;
}
bool index::isspan() const
{
return impl.isSeq == true && impl.idx.seq == af_span;
}
const af_index_t& index::get() const
{
return impl;
}
]]
end
-- TODO: Add "index" environment type?
-- Export the module.
return M