Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa38c5b

Browse files
committed
Use null data instead of a separate flag
to mark empty array views
1 parent ed134d5 commit aa38c5b

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/numpy_cpp.h

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class array_view_accessors<AV, T, 1>
242242
T &operator()(npy_intp i)
243243
{
244244
AVC *self = static_cast<AVC *>(this);
245-
if (unlikely(self->m_empty)) {
245+
if (unlikely(self->m_data == NULL)) {
246246
throw std::out_of_range("indexing into an array_view with no data");
247247
}
248248

@@ -252,7 +252,7 @@ class array_view_accessors<AV, T, 1>
252252
const T &operator()(npy_intp i) const
253253
{
254254
const AVC *self = static_cast<const AVC *>(this);
255-
if (unlikely(self->m_empty)) {
255+
if (unlikely(self->m_data == NULL)) {
256256
throw std::out_of_range("indexing into an array_view with no data");
257257
}
258258

@@ -262,7 +262,7 @@ class array_view_accessors<AV, T, 1>
262262
T &operator[](npy_intp i)
263263
{
264264
AVC *self = static_cast<AVC *>(this);
265-
if (unlikely(self->m_empty)) {
265+
if (unlikely(self->m_data == NULL)) {
266266
throw std::out_of_range("indexing into an array_view with no data");
267267
}
268268

@@ -272,7 +272,7 @@ class array_view_accessors<AV, T, 1>
272272
const T &operator[](npy_intp i) const
273273
{
274274
const AVC *self = static_cast<const AVC *>(this);
275-
if (unlikely(self->m_empty)) {
275+
if (unlikely(self->m_data == NULL)) {
276276
throw std::out_of_range("indexing into an array_view with no data");
277277
}
278278

@@ -290,7 +290,7 @@ class array_view_accessors<AV, T, 2>
290290
T &operator()(npy_intp i, npy_intp j)
291291
{
292292
AVC *self = static_cast<AVC *>(this);
293-
if (unlikely(self->m_empty)) {
293+
if (unlikely(self->m_data == NULL)) {
294294
throw std::out_of_range("indexing into an array_view with no data");
295295
}
296296

@@ -301,7 +301,7 @@ class array_view_accessors<AV, T, 2>
301301
const T &operator()(npy_intp i, npy_intp j) const
302302
{
303303
const AVC *self = static_cast<const AVC *>(this);
304-
if (unlikely(self->m_empty)) {
304+
if (unlikely(self->m_data == NULL)) {
305305
throw std::out_of_range("indexing into an array_view with no data");
306306
}
307307

@@ -312,7 +312,7 @@ class array_view_accessors<AV, T, 2>
312312
sub_t operator[](npy_intp i) const
313313
{
314314
const AVC *self = static_cast<const AVC *>(this);
315-
if (unlikely(self->m_empty)) {
315+
if (unlikely(self->m_data == NULL)) {
316316
throw std::out_of_range("indexing into an array_view with no data");
317317
}
318318

@@ -333,7 +333,7 @@ class array_view_accessors<AV, T, 3>
333333
T &operator()(npy_intp i, npy_intp j, npy_intp k)
334334
{
335335
AVC *self = static_cast<AVC *>(this);
336-
if (unlikely(self->m_empty)) {
336+
if (unlikely(self->m_data == NULL)) {
337337
throw std::out_of_range("indexing into an array_view with no data");
338338
}
339339

@@ -344,7 +344,7 @@ class array_view_accessors<AV, T, 3>
344344
const T &operator()(npy_intp i, npy_intp j, npy_intp k) const
345345
{
346346
const AVC *self = static_cast<const AVC *>(this);
347-
if (unlikely(self->m_empty)) {
347+
if (unlikely(self->m_data == NULL)) {
348348
throw std::out_of_range("indexing into an array_view with no data");
349349
}
350350

@@ -355,7 +355,7 @@ class array_view_accessors<AV, T, 3>
355355
sub_t operator[](npy_intp i) const
356356
{
357357
const AVC *self = static_cast<const AVC *>(this);
358-
if (unlikely(self->m_empty)) {
358+
if (unlikely(self->m_data == NULL)) {
359359
throw std::out_of_range("indexing into an array_view with no data");
360360
}
361361

@@ -386,9 +386,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
386386
npy_intp *m_shape;
387387
npy_intp *m_strides;
388388
char *m_data;
389-
// Flag for a limited kind of bounds checking,
390-
// not the same as the empty() method which checks the outer dimension
391-
bool m_empty;
392389

393390
public:
394391
typedef T value_type;
@@ -401,7 +398,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
401398
{
402399
m_shape = zeros;
403400
m_strides = zeros;
404-
m_empty = true;
405401
}
406402

407403
array_view(PyObject *arr, bool contiguous = false) : m_arr(NULL), m_data(NULL)
@@ -418,22 +414,26 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
418414
m_data = other.m_data;
419415
m_shape = other.m_shape;
420416
m_strides = other.m_strides;
421-
m_empty = other.m_empty;
422417
}
423418

424419
array_view(PyArrayObject *arr, char *data, npy_intp *shape, npy_intp *strides)
425420
{
426-
m_arr = arr;
427-
Py_XINCREF(arr);
428-
m_data = data;
429-
m_shape = shape;
430-
m_strides = strides;
431-
m_empty = (ND == 0);
421+
bool empty = (ND == 0);
432422
for (size_t i = 0; i < ND; i++) {
433423
if (shape[i] == 0) {
434-
m_empty = true;
424+
empty = true;
435425
}
436426
}
427+
428+
m_arr = arr;
429+
Py_XINCREF(arr);
430+
if (empty) {
431+
m_data = NULL;
432+
} else {
433+
m_data = data;
434+
}
435+
m_shape = shape;
436+
m_strides = strides;
437437
}
438438

439439
array_view(npy_intp shape[ND]) : m_arr(NULL), m_shape(NULL), m_strides(NULL), m_data(NULL)
@@ -464,7 +464,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
464464
m_data = other.m_data;
465465
m_shape = other.m_shape;
466466
m_strides = other.m_strides;
467-
m_empty = other.m_empty;
468467
}
469468
return *this;
470469
}
@@ -479,7 +478,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
479478
m_data = NULL;
480479
m_shape = zeros;
481480
m_strides = zeros;
482-
m_empty = true;
483481
} else {
484482
if (contiguous) {
485483
tmp = (PyArrayObject *)PyArray_ContiguousFromAny(arr, type_num_of<T>::value, 0, ND);
@@ -498,7 +496,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
498496
m_strides = zeros;
499497
if (PyArray_NDIM(tmp) == 0 && ND == 0) {
500498
m_arr = tmp;
501-
m_empty = true;
502499
return 1;
503500
}
504501
}
@@ -516,13 +513,17 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
516513
m_arr = tmp;
517514
m_shape = PyArray_DIMS(m_arr);
518515
m_strides = PyArray_STRIDES(m_arr);
519-
m_data = (char *)PyArray_BYTES(tmp);
520-
m_empty = (ND == 0);
516+
bool empty = (ND == 0);
521517
for (size_t i = 0; i < ND; i++) {
522518
if (m_shape[i] == 0) {
523-
m_empty = true;
519+
empty = true;
524520
}
525521
}
522+
if (empty) {
523+
m_data = NULL;
524+
} else {
525+
m_data = (char *)PyArray_BYTES(tmp);
526+
}
526527
}
527528

528529
return 1;

0 commit comments

Comments
 (0)