forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrided_array.hpp
More file actions
235 lines (198 loc) · 4.89 KB
/
Copy pathstrided_array.hpp
File metadata and controls
235 lines (198 loc) · 4.89 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#pragma once
#include "../array.hpp"
#include "../exceptions.hpp"
#include "../shape_utils.hpp"
#include "../stride.hpp"
#include <base/assert.hpp>
#include <icm/index_mapping.hpp>
#include <icm/indexable.hpp>
#include <icm/shape.hpp>
#include <icm/slice.hpp>
#include <icm/slice_shape.hpp>
namespace nd {
namespace impl {
template <typename I>
int new_to_old_offset(int new_offset, const icm::shape& old_shape, I begin, I end)
{
icm::small_vector<int> old_strides(old_shape.size(), 1);
icm::small_vector<int> new_strides(old_shape.size(), 1);
auto os = 1;
auto ns = 1;
auto distance = std::distance(begin, end);
for (auto i = old_shape.size() - 1; i > 0; --i) {
os *= old_shape[i];
if (i < distance) {
ns *= (begin + i)->size();
} else {
ns *= old_shape[i];
}
old_strides[i - 1] = os;
new_strides[i - 1] = ns;
}
auto old_offset = 0;
I it = begin;
for (auto i = 0; i < old_shape.size(); ++i) {
if (it != end) {
auto ci = (*it)[new_offset / new_strides[i]];
if (ci >= old_shape[i]) {
throw invalid_operation("Subscript index is out of array bounds.");
}
old_offset += old_strides[i] * ci;
++it;
} else {
old_offset += old_strides[i] * (new_offset / new_strides[i]);
}
new_offset %= new_strides[i];
}
return old_offset;
}
template <typename T, typename I>
class single_strided_array
{
public:
using value_type = T;
public:
single_strided_array(array a, icm::index_mapping_t<I> index)
: shape_(icm::slice_shape(a.shape(), &index, &index + 1))
, a_(std::move(a))
, index_(std::move(index))
{}
enum dtype dtype() const
{
return dtype_enum_v<value_type>;
}
const icm::shape& shape() const
{
return shape_;
}
value_type value(int64_t index) const
{
auto o = new_to_old_offset(index, a_.shape(), &index_, &index_ + 1);
return a_.value<value_type>(o);
}
array get(int64_t index) const
{
return a_[index_[index]];
}
constexpr bool is_dynamic() const noexcept
{
return false;
}
private:
const icm::shape shape_;
const array a_;
const icm::index_mapping_t<I> index_;
};
template <typename T>
class strided_array
{
public:
using value_type = T;
public:
strided_array(array a, icm::index_mapping_vector indices)
: shape_(icm::slice_shape(a.shape(), indices.begin(), indices.end()))
, a_(std::move(a))
, indices_(std::move(indices))
{}
enum dtype dtype() const
{
return dtype_enum_v<value_type>;
}
const icm::shape& shape() const
{
return shape_;
}
value_type value(int64_t index) const
{
auto o = new_to_old_offset(index, a_.shape(), indices_.begin(), indices_.end());
return a_.value<value_type>(o);
}
array get(int64_t index) const
{
return nd::stride(a_[indices_.front()[index]],
icm::index_mapping_vector{indices_.begin() + 1, indices_.end()});
}
constexpr bool is_dynamic() const noexcept
{
return false;
}
private:
const icm::shape shape_;
const array a_;
const icm::index_mapping_vector indices_;
};
template <typename I>
class single_dynamic_strided_array
{
public:
single_dynamic_strided_array(array a, icm::index_mapping_t<I> index)
: a_(std::move(a))
, index_(std::move(index))
, shape_(index_.size())
{
ASSERT(a_.is_dynamic());
}
enum dtype dtype() const
{
return a_.dtype();
}
const icm::shape& shape() const
{
return shape_;
}
array get(int64_t index) const
{
return a_[index_[index]];
}
uint8_t dimensions() const
{
return a_.dimensions();
}
constexpr bool is_dynamic() const noexcept
{
return true;
}
private:
const array a_;
const icm::index_mapping_t<I> index_;
const icm::shape shape_;
};
class strided_dynamic_array
{
public:
strided_dynamic_array(array a, icm::index_mapping index, icm::indexable_vector slices)
: shape_(index.size())
, a_(std::move(a))
, index_(std::move(index))
, slices_(std::move(slices))
{
ASSERT(a_.is_dynamic());
}
enum dtype dtype() const
{
return a_.dtype();
}
const icm::shape& shape() const
{
return shape_;
}
array get(int64_t index) const
{
return nd::stride(a_[index_[index]], slices_);
}
uint8_t dimensions() const
{
return a_.dimensions();
}
constexpr bool is_dynamic() const noexcept
{
return true;
}
private:
const icm::shape shape_;
const array a_;
const icm::index_mapping index_;
const icm::indexable_vector slices_;
};
}
}