forked from xtensor-stack/xtensor-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyarray.hpp
More file actions
672 lines (552 loc) · 20.3 KB
/
Copy pathpyarray.hpp
File metadata and controls
672 lines (552 loc) · 20.3 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/***************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef PY_ARRAY_HPP
#define PY_ARRAY_HPP
#include <cstddef>
#include <algorithm>
#include <vector>
#include "pybind11/numpy.h"
#include "xtensor/xexpression.hpp"
#include "xtensor/xsemantic.hpp"
#include "xtensor/xiterator.hpp"
namespace xt
{
template <class T, int ExtraFlags>
class pyarray;
}
namespace pybind11
{
namespace detail
{
template <typename T, int ExtraFlags>
struct pyobject_caster<xt::pyarray<T, ExtraFlags>>
{
using type = xt::pyarray<T, ExtraFlags>;
bool load(handle src, bool)
{
value = type::ensure(src);
return static_cast<bool>(value);
}
static handle cast(const handle &src, return_value_policy, handle)
{
return src.inc_ref();
}
PYBIND11_TYPE_CASTER(type, handle_type_name<type>::name());
};
}
}
namespace xt
{
using pybind_array = pybind11::array;
/***********************
* pyarray declaration *
***********************/
template <class T, int ExtraFlags>
class pyarray;
template <class T, int ExtraFlags>
struct xcontainer_inner_types<pyarray<T, ExtraFlags>>
{
using temporary_type = pyarray<T, ExtraFlags>;
};
template <class A>
class pyarray_backstrides
{
public:
using array_type = A;
using value_type = typename array_type::size_type;
using size_type = typename array_type::size_type;
pyarray_backstrides(const A& a);
value_type operator[](size_type i) const;
private:
const pybind_array* p_a;
};
/**
* @class pyarray
* @brief Wrapper on the Python buffer protocol.
*/
template <class T, int ExtraFlags = pybind_array::forcecast>
class pyarray : public pybind_array,
public xcontainer_semantic<pyarray<T, ExtraFlags>>
{
public:
using self_type = pyarray<T, ExtraFlags>;
using base_type = pybind_array;
using semantic_base = xcontainer_semantic<self_type>;
using value_type = T;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using storage_iterator = T*;
using const_storage_iterator = const T*;
using shape_type = std::vector<size_type>;
using strides_type = std::vector<size_type>;
using backstrides_type = pyarray_backstrides<self_type>;
using stepper = xstepper<self_type>;
using const_stepper = xstepper<const self_type>;
using iterator = xiterator<stepper, shape_type>;
using const_iterator = xiterator<const_stepper, shape_type>;
using closure_type = const self_type&;
pyarray();
pyarray(pybind11::handle h, borrowed_t);
pyarray(pybind11::handle h, stolen_t);
pyarray(const pybind11::object &o);
pyarray(const shape_type& shape,
const strides_type& strides,
const T* ptr = nullptr,
handle base = handle());
explicit pyarray(const shape_type& shape,
const T* ptr = nullptr,
handle base = handle());
explicit pyarray(size_type count,
const T* ptr = nullptr,
handle base = handle());
size_type dimension() const;
const shape_type& shape() const;
const strides_type& strides() const;
backstrides_type backstrides() const;
void reshape(const shape_type& shape);
void reshape(const shape_type& shape, layout l);
void reshape(const shape_type& shape, const strides_type& strides);
template<typename... Args>
reference operator()(Args... args);
template<typename... Args>
const_reference operator()(Args... args) const;
reference operator[](const xindex& index);
const_reference operator[](const xindex& index) const;
template<typename... Args>
pointer data(Args... args);
template<typename... Args>
const_pointer data(Args... args) const;
template <class S>
bool broadcast_shape(S& shape) const;
template <class S>
bool is_trivial_broadcast(const S& strides) const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
const_iterator cbegin() const;
const_iterator cend() const;
template <class S>
xiterator<stepper, S> xbegin(const S& shape);
template <class S>
xiterator<stepper, S> xend(const S& shape);
template <class S>
xiterator<const_stepper, S> xbegin(const S& shape) const;
template <class S>
xiterator<const_stepper, S> xend(const S& shape) const;
template <class S>
xiterator<const_stepper, S> cxbegin(const S& shape) const;
template <class S>
xiterator<const_stepper, S> cxend(const S& shape) const;
template <class S>
stepper stepper_begin(const S& shape);
template <class S>
stepper stepper_end(const S& shape);
template <class S>
const_stepper stepper_begin(const S& shape) const;
template <class S>
const_stepper stepper_end(const S& shape) const;
storage_iterator storage_begin();
storage_iterator storage_end();
const_storage_iterator storage_begin() const;
const_storage_iterator storage_end() const;
const_storage_iterator storage_cbegin() const;
const_storage_iterator storage_cend() const;
template <class E>
pyarray(const xexpression<E>& e);
template <class E>
pyarray& operator=(const xexpression<E>& e);
static pyarray ensure(pybind11::handle h);
static bool _check(pybind11::handle h);
private:
template<typename... Args>
size_type index_at(Args... args) const;
size_type data_offset(const xindex& index) const;
static constexpr size_type itemsize();
static bool is_non_null(PyObject* ptr);
mutable shape_type m_shape;
mutable strides_type m_strides;
static PyObject* raw_array_t(PyObject* ptr);
};
/**************************************
* pyarray_backstrides implementation *
**************************************/
template <class A>
inline pyarray_backstrides<A>::pyarray_backstrides(const A& a)
: p_a(&a)
{
}
template <class A>
inline auto pyarray_backstrides<A>::operator[](size_type i) const -> value_type
{
value_type sh = p_a->shape()[i];
value_type res = sh == 1 ? 0 : (sh - 1) * p_a->strides()[i] / sizeof(typename A::value_type);
return res;
}
/**************************
* pyarray implementation *
**************************/
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray()
: pybind_array(0, static_cast<const_pointer>(nullptr))
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(pybind11::handle h, borrowed_t) : pybind_array(h, borrowed)
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(pybind11::handle h, stolen_t) : pybind_array(h, stolen)
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(const pybind11::object &o) : pybind_array(raw_array_t(o.ptr()), stolen)
{
if (!m_ptr)
{
throw pybind11::error_already_set();
}
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(const shape_type& shape,
const strides_type& strides,
const T *ptr,
handle base)
: pybind_array(shape, strides, ptr, base)
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(const shape_type& shape,
const T* ptr,
handle base)
: pybind_array(shape, ptr, base)
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(size_type count,
const T* ptr,
handle base)
: pybind_array(count, ptr, base)
{
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::dimension() const -> size_type
{
return pybind_array::ndim();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::shape() const -> const shape_type&
{
// Until we have the CRTP on shape types, we copy the shape.
m_shape.resize(dimension());
std::copy(pybind_array::shape(), pybind_array::shape() + dimension(), m_shape.begin());
return m_shape;
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::strides() const -> const strides_type&
{
m_strides.resize(dimension());
std::transform(pybind_array::strides(), pybind_array::strides() + dimension(), m_strides.begin(),
[](size_type str) { return str / sizeof(value_type); });
return m_strides;
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::backstrides() const -> backstrides_type
{
backstrides_type tmp(*this);
return tmp;
}
template <class T, int ExtraFlags>
void pyarray<T, ExtraFlags>::reshape(const shape_type& shape)
{
if (!m_ptr || shape.size() != dimension() || !std::equal(shape.begin(), shape.end(), pybind_array::shape()))
{
reshape(shape, layout::row_major);
}
}
template <class T, int ExtraFlags>
void pyarray<T, ExtraFlags>::reshape(const shape_type& shape, layout l)
{
strides_type strides(shape.size());
size_type data_size = sizeof(value_type);
if (l == layout::row_major)
{
for (size_type i = strides.size(); i != 0; --i)
{
strides[i - 1] = data_size;
data_size = strides[i - 1] * shape[i - 1];
if (shape[i - 1] == 1)
{
strides[i - 1] = 0;
}
}
}
else
{
for (size_type i = 0; i < strides.size(); ++i)
{
strides[i] = data_size;
data_size = strides[i] * shape[i];
if (shape[i] == 1)
{
strides[i] = 0;
}
}
}
reshape(shape, strides);
}
template <class T, int ExtraFlags>
void pyarray<T, ExtraFlags>::reshape(const shape_type& shape, const strides_type& strides)
{
self_type tmp(shape, strides);
*this = std::move(tmp);
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::operator()(Args... args) -> reference
{
return *(static_cast<pointer>(pybind_array::mutable_data()) + pybind_array::byte_offset(args...) / itemsize());
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::operator()(Args... args) const -> const_reference
{
return *(static_cast<const_pointer>(pybind_array::data()) + pybind_array::byte_offset(args...) / itemsize());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::operator[](const xindex& index) -> reference
{
return *(static_cast<pointer>(pybind_array::mutable_data()) + data_offset(index));
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::operator[](const xindex& index) const -> const_reference
{
return *(static_cast<const_pointer>(pybind_array::data()) + data_offset(index));
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::data(Args... args) -> pointer
{
return static_cast<pointer>(pybind_array::mutable_data(args...));
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::data(Args... args) const -> const_pointer
{
return static_cast<const T*>(pybind_array::data(args...));
}
template <class T, int ExtraFlags>
template <class S>
bool pyarray<T, ExtraFlags>::broadcast_shape(S& shape) const
{
return xt::broadcast_shape(this->shape(), shape);
}
template <class T, int ExtraFlags>
template <class S>
bool pyarray<T, ExtraFlags>::is_trivial_broadcast(const S& strides) const
{
return strides.size() == dimension() &&
std::equal(strides.begin(), strides.end(), this->strides().begin());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::begin() -> iterator
{
return xbegin(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::end() -> iterator
{
return xend(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::begin() const -> const_iterator
{
return xbegin(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::end() const -> const_iterator
{
return xend(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::cbegin() const -> const_iterator
{
return begin();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::cend() const -> const_iterator
{
return end();
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::xbegin(const S& shape) -> xiterator<stepper, S>
{
return xiterator<stepper, S>(stepper_begin(shape), shape);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::xend(const S& shape) -> xiterator<stepper, S>
{
return xiterator<stepper, S>(stepper_end(shape), shape);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::xbegin(const S& shape) const -> xiterator<const_stepper, S>
{
return xiterator<const_stepper, S>(stepper_begin(shape), shape);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::xend(const S& shape) const -> xiterator<const_stepper, S>
{
return xiterator<const_stepper, S>(stepper_end(shape), shape);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::cxbegin(const S& shape) const -> xiterator<const_stepper, S>
{
return xbegin(shape);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::cxend(const S& shape) const -> xiterator<const_stepper, S>
{
return xend(shape);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::stepper_begin(const S& shape) -> stepper
{
size_type offset = shape.size() - dimension();
return stepper(this, storage_begin(), offset);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::stepper_end(const S& shape) -> stepper
{
size_type offset = shape.size() - dimension();
return stepper(this, storage_end(), offset);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::stepper_begin(const S& shape) const -> const_stepper
{
size_type offset = shape.size() - dimension();
return const_stepper(this, storage_begin(), offset);
}
template <class T, int ExtraFlags>
template <class S>
inline auto pyarray<T, ExtraFlags>::stepper_end(const S& shape) const -> const_stepper
{
size_type offset = shape.size() - dimension();
return const_stepper(this, storage_end(), offset);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_begin() -> storage_iterator
{
return reinterpret_cast<storage_iterator>(pybind11::detail::array_proxy(m_ptr)->data);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_end() -> storage_iterator
{
return storage_begin() + pybind_array::size();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_begin() const -> const_storage_iterator
{
return reinterpret_cast<const_storage_iterator>(pybind11::detail::array_proxy(m_ptr)->data);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_end() const -> const_storage_iterator
{
return storage_begin() + pybind_array::size();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_cbegin() const -> const_storage_iterator
{
return reinterpret_cast<const_storage_iterator>(pybind11::detail::array_proxy(m_ptr)->data);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_cend() const -> const_storage_iterator
{
return storage_begin() + pybind_array::size();
}
template <class T, int ExtraFlags>
template <class E>
inline pyarray<T, ExtraFlags>::pyarray(const xexpression<E>& e)
: pybind_array()
{
semantic_base::assign(e);
}
template <class T, int ExtraFlags>
template <class E>
inline auto pyarray<T, ExtraFlags>::operator=(const xexpression<E>& e) -> self_type&
{
return semantic_base::operator=(e);
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags> pyarray<T, ExtraFlags>::ensure(pybind11::handle h)
{
auto result = pybind11::reinterpret_steal<pyarray>(raw_array_t(h.ptr()));
if (!pybind11::handle(result))
{
PyErr_Clear();
}
return result;
}
template <class T, int ExtraFlags>
inline bool pyarray<T, ExtraFlags>::_check(pybind11::handle h)
{
const auto &api = pybind11::detail::npy_api::get();
return api.PyArray_Check_(h.ptr())
&& api.PyArray_EquivTypes_(pybind11::detail::array_proxy(h.ptr())->descr, pybind11::dtype::of<T>().ptr());
}
// Private methods
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::index_at(Args... args) const -> size_type
{
return pybind_array::byte_offset(args...) / itemsize();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::data_offset(const xindex& index) const -> size_type
{
const strides_type& str = strides();
auto iter = index.begin();
iter += index.size() - str.size();
return std::inner_product(str.begin(), str.end(), iter, size_type(0)) / itemsize();
}
template <class T, int ExtraFlags>
constexpr auto pyarray<T, ExtraFlags>::itemsize() -> size_type
{
return sizeof(value_type);
}
template <class T, int ExtraFlags>
inline bool pyarray<T, ExtraFlags>::is_non_null(PyObject* ptr)
{
return ptr != nullptr;
}
template <class T, int ExtraFlags>
inline PyObject* pyarray<T, ExtraFlags>::raw_array_t(PyObject* ptr)
{
if (ptr == nullptr)
{
return nullptr;
}
return pybind11::detail::npy_api::get().PyArray_FromAny_(
ptr, pybind11::dtype::of<T>().release().ptr(), 0, 0,
pybind11::detail::npy_api::NPY_ENSURE_ARRAY_ | ExtraFlags, nullptr
);
}
}
#endif