-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathzarray_zarray.hpp
More file actions
395 lines (318 loc) · 10.6 KB
/
zarray_zarray.hpp
File metadata and controls
395 lines (318 loc) · 10.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_ZARRAY_ZARRAY_HPP
#define XTENSOR_ZARRAY_ZARRAY_HPP
#include <memory>
#include <nlohmann/json.hpp>
#include <xtl/xmultimethods.hpp>
#include <xtensor/xarray.hpp>
#include "zassign.hpp"
#include "zfunctors.hpp"
#include "zwrappers.hpp"
namespace xt
{
/**********
* zarray *
**********/
class zarray;
template <>
struct xcontainer_inner_types<zarray>
{
using temporary_type = zarray;
};
class zarray : public xcontainer_semantic<zarray>
{
public:
using expression_tag = zarray_expression_tag;
using semantic_base = xcontainer_semantic<zarray>;
using implementation_ptr = std::unique_ptr<zarray_impl>;
using shape_type = zarray_impl::shape_type;
zarray() = default;
~zarray() = default;
template <class T>
zarray(std::initializer_list<T> t);
template <class T>
zarray(std::initializer_list<std::initializer_list<T>> t);
template <class T>
zarray(std::initializer_list<
std::initializer_list<
std::initializer_list<T>
>
> t);
zarray(implementation_ptr&& impl);
zarray& operator=(implementation_ptr&& impl);
zarray(const zarray& rhs);
zarray& operator=(const zarray& rhs);
zarray(zarray&& rhs);
zarray& operator=(zarray&& rhs);
template <class E>
zarray(const xexpression<E>& e);
template <class E>
zarray(xexpression<E>& e);
template <class E>
zarray(xexpression<E>&& e);
template <class E>
zarray& operator=(const xexpression<E>&);
void swap(zarray& rhs);
bool has_implementation() const;
zarray_impl& get_implementation();
const zarray_impl& get_implementation() const;
template <class T>
bool can_get_array()const;
template <class T>
xarray<T>& get_array();
template <class T>
const xarray<T>& get_array() const;
void assign_to(zarray_impl& dst, const zassign_args& args) const;
std::size_t dimension() const;
const shape_type& shape() const;
void reshape(const shape_type& shape);
void reshape(shape_type&& shape);
void resize(const shape_type& shape);
void resize(shape_type&& shape);
bool broadcast_shape(shape_type& shape, bool reuse_cache = false) const;
const zchunked_array& as_chunked_array() const;
const nlohmann::json& get_metadata() const;
void set_metadata(const nlohmann::json& metadata);
private:
template <class E>
void init_implementation(E&& e, xtensor_expression_tag);
template <class E>
void init_implementation(const xexpression<E>& e, zarray_expression_tag);
template <class E>
zarray& assign_expression(const xexpression<E>& e, xtensor_expression_tag);
template <class E>
zarray& assign_expression(const xexpression<E>& e, zarray_expression_tag);
implementation_ptr p_impl;
};
zarray strided_view(zarray& z, xstrided_slice_vector& slices);
std::ostream& operator<<(std::ostream& out, const zarray& ar);
/*************************
* zarray implementation *
*************************/
template <class E>
inline void zarray::init_implementation(E&& e, xtensor_expression_tag)
{
p_impl = implementation_ptr(detail::build_zarray(std::forward<E>(e)));
}
template <class E>
inline void zarray::init_implementation(const xexpression<E>& e, zarray_expression_tag)
{
p_impl = e.derived_cast().allocate_result();
semantic_base::assign(e);
}
template <class E>
inline zarray& zarray::assign_expression(const xexpression<E>& e, xtensor_expression_tag)
{
zarray tmp(e);
return (*this = tmp);
}
template <class E>
inline zarray& zarray::assign_expression(const xexpression<E>& e, zarray_expression_tag)
{
return semantic_base::operator=(e);
}
template <class T>
inline zarray::zarray(std::initializer_list<T> t)
: zarray(xarray<T>(t))
{
}
template <class T>
inline zarray::zarray(std::initializer_list<std::initializer_list<T>> t)
: zarray(xarray<T>(t))
{
}
template <class T>
inline zarray::zarray(std::initializer_list<
std::initializer_list<
std::initializer_list<T>
>
> t)
: zarray(xarray<T>(t))
{
}
inline zarray::zarray(implementation_ptr&& impl)
: p_impl(std::move(impl))
{
}
inline zarray& zarray::operator=(implementation_ptr&& impl)
{
p_impl = std::move(impl);
return *this;
}
inline zarray::zarray(const zarray& rhs)
: p_impl()
{
if(rhs.has_implementation())
{
p_impl.reset(rhs.p_impl->clone());
}
}
inline zarray& zarray::operator=(const zarray& rhs)
{
if(this->has_implementation())
{
resize(rhs.shape());
zassign_args args;
args.trivial_broadcast = true;
if (p_impl->is_chunked())
{
auto l = [](zarray& lhs, const zarray& rhs, zassign_args& args)
{
zdispatcher_t<detail::xassign_dummy_functor, 1>::dispatch(*(rhs.p_impl), *(lhs.p_impl), args);
};
detail::run_chunked_assign_loop(*this, rhs, args, l);
}
else
{
zdispatcher_t<detail::xassign_dummy_functor, 1>::dispatch(*(rhs.p_impl), *p_impl, args);
}
}
else
{
p_impl.reset(rhs.p_impl->clone());
}
return *this;
}
inline zarray::zarray(zarray&& rhs)
: p_impl(std::move(rhs.p_impl))
{
}
inline zarray& zarray::operator=(zarray&& rhs)
{
if(this->has_implementation())
{
zassign_args args;
args.trivial_broadcast = true;
if (p_impl->is_chunked())
{
auto l = [](zarray& lhs, const zarray& rhs, zassign_args& args)
{
zdispatcher_t<detail::xmove_dummy_functor, 1>::dispatch(*(rhs.p_impl), *(lhs.p_impl), args);
};
detail::run_chunked_assign_loop(*this, rhs, args, l);
}
else
{
zdispatcher_t<detail::xmove_dummy_functor, 1>::dispatch(*(rhs.p_impl), *p_impl, args);
}
}
else
{
p_impl = std::move(rhs.p_impl);
}
return *this;
}
template <class E>
inline zarray::zarray(const xexpression<E>& e)
{
init_implementation(e.derived_cast(), extension::get_expression_tag_t<std::decay_t<E>>());
}
template <class E>
inline zarray::zarray(xexpression<E>& e)
{
init_implementation(e.derived_cast(), extension::get_expression_tag_t<std::decay_t<E>>());
}
template <class E>
inline zarray::zarray(xexpression<E>&& e)
{
init_implementation(std::move(e).derived_cast(), extension::get_expression_tag_t<std::decay_t<E>>());
}
template <class E>
inline zarray& zarray::operator=(const xexpression<E>& e)
{
return assign_expression(e, extension::get_expression_tag_t<std::decay_t<E>>());
}
inline void zarray::swap(zarray& rhs)
{
std::swap(p_impl, rhs.p_impl);
}
inline bool zarray::has_implementation() const
{
return bool(p_impl);
}
inline zarray_impl& zarray::get_implementation()
{
return *p_impl;
}
inline const zarray_impl& zarray::get_implementation() const
{
return *p_impl;
}
template <class T>
inline bool zarray::can_get_array()const
{
return dynamic_cast<ztyped_array<T>*>(p_impl.get()) != nullptr;
}
template <class T>
inline xarray<T>& zarray::get_array()
{
return dynamic_cast<ztyped_array<T>*>(p_impl.get())->get_array();
}
template <class T>
inline const xarray<T>& zarray::get_array() const
{
return dynamic_cast<const ztyped_array<T>*>(p_impl.get())->get_array();
}
inline void zarray::assign_to(zarray_impl& dst, const zassign_args& args) const
{
dst.resize(shape());
zdispatcher_t<detail::xassign_dummy_functor, 1>::dispatch(get_implementation(), dst, args);
}
inline std::size_t zarray::dimension() const
{
return p_impl->dimension();
}
inline auto zarray::shape() const -> const shape_type&
{
return p_impl->shape();
}
inline void zarray::reshape(const shape_type& shape)
{
p_impl->reshape(shape);
}
inline void zarray::reshape(shape_type&& shape)
{
p_impl->reshape(std::move(shape));
}
inline void zarray::resize(const shape_type& shape)
{
p_impl->resize(shape);
}
inline void zarray::resize(shape_type&& shape)
{
p_impl->resize(std::move(shape));
}
inline bool zarray::broadcast_shape(shape_type& shape, bool reuse_cache) const
{
return p_impl->broadcast_shape(shape, reuse_cache);
}
inline const zchunked_array& zarray::as_chunked_array() const
{
return dynamic_cast<const zchunked_array&>(*(p_impl.get()));
}
inline const nlohmann::json& zarray::get_metadata() const
{
return p_impl->get_metadata();
}
inline void zarray::set_metadata(const nlohmann::json& metadata)
{
return p_impl->set_metadata(metadata);
}
inline zarray strided_view(zarray& z, xstrided_slice_vector& slices)
{
std::unique_ptr<zarray_impl> p(z.get_implementation().strided_view(slices));
return zarray(std::move(p));
}
inline std::ostream& operator<<(std::ostream& out, const zarray& ar)
{
return ar.get_implementation().print(out);
}
}
#endif