@@ -242,7 +242,7 @@ class array_view_accessors<AV, T, 1>
242
242
T &operator ()(npy_intp i)
243
243
{
244
244
AVC *self = static_cast <AVC *>(this );
245
- if (unlikely (self->m_empty )) {
245
+ if (unlikely (self->m_data == NULL )) {
246
246
throw std::out_of_range (" indexing into an array_view with no data" );
247
247
}
248
248
@@ -252,7 +252,7 @@ class array_view_accessors<AV, T, 1>
252
252
const T &operator ()(npy_intp i) const
253
253
{
254
254
const AVC *self = static_cast <const AVC *>(this );
255
- if (unlikely (self->m_empty )) {
255
+ if (unlikely (self->m_data == NULL )) {
256
256
throw std::out_of_range (" indexing into an array_view with no data" );
257
257
}
258
258
@@ -262,7 +262,7 @@ class array_view_accessors<AV, T, 1>
262
262
T &operator [](npy_intp i)
263
263
{
264
264
AVC *self = static_cast <AVC *>(this );
265
- if (unlikely (self->m_empty )) {
265
+ if (unlikely (self->m_data == NULL )) {
266
266
throw std::out_of_range (" indexing into an array_view with no data" );
267
267
}
268
268
@@ -272,7 +272,7 @@ class array_view_accessors<AV, T, 1>
272
272
const T &operator [](npy_intp i) const
273
273
{
274
274
const AVC *self = static_cast <const AVC *>(this );
275
- if (unlikely (self->m_empty )) {
275
+ if (unlikely (self->m_data == NULL )) {
276
276
throw std::out_of_range (" indexing into an array_view with no data" );
277
277
}
278
278
@@ -290,7 +290,7 @@ class array_view_accessors<AV, T, 2>
290
290
T &operator ()(npy_intp i, npy_intp j)
291
291
{
292
292
AVC *self = static_cast <AVC *>(this );
293
- if (unlikely (self->m_empty )) {
293
+ if (unlikely (self->m_data == NULL )) {
294
294
throw std::out_of_range (" indexing into an array_view with no data" );
295
295
}
296
296
@@ -301,7 +301,7 @@ class array_view_accessors<AV, T, 2>
301
301
const T &operator ()(npy_intp i, npy_intp j) const
302
302
{
303
303
const AVC *self = static_cast <const AVC *>(this );
304
- if (unlikely (self->m_empty )) {
304
+ if (unlikely (self->m_data == NULL )) {
305
305
throw std::out_of_range (" indexing into an array_view with no data" );
306
306
}
307
307
@@ -312,7 +312,7 @@ class array_view_accessors<AV, T, 2>
312
312
sub_t operator [](npy_intp i) const
313
313
{
314
314
const AVC *self = static_cast <const AVC *>(this );
315
- if (unlikely (self->m_empty )) {
315
+ if (unlikely (self->m_data == NULL )) {
316
316
throw std::out_of_range (" indexing into an array_view with no data" );
317
317
}
318
318
@@ -333,7 +333,7 @@ class array_view_accessors<AV, T, 3>
333
333
T &operator ()(npy_intp i, npy_intp j, npy_intp k)
334
334
{
335
335
AVC *self = static_cast <AVC *>(this );
336
- if (unlikely (self->m_empty )) {
336
+ if (unlikely (self->m_data == NULL )) {
337
337
throw std::out_of_range (" indexing into an array_view with no data" );
338
338
}
339
339
@@ -344,7 +344,7 @@ class array_view_accessors<AV, T, 3>
344
344
const T &operator ()(npy_intp i, npy_intp j, npy_intp k) const
345
345
{
346
346
const AVC *self = static_cast <const AVC *>(this );
347
- if (unlikely (self->m_empty )) {
347
+ if (unlikely (self->m_data == NULL )) {
348
348
throw std::out_of_range (" indexing into an array_view with no data" );
349
349
}
350
350
@@ -355,7 +355,7 @@ class array_view_accessors<AV, T, 3>
355
355
sub_t operator [](npy_intp i) const
356
356
{
357
357
const AVC *self = static_cast <const AVC *>(this );
358
- if (unlikely (self->m_empty )) {
358
+ if (unlikely (self->m_data == NULL )) {
359
359
throw std::out_of_range (" indexing into an array_view with no data" );
360
360
}
361
361
@@ -386,9 +386,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
386
386
npy_intp *m_shape;
387
387
npy_intp *m_strides;
388
388
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;
392
389
393
390
public:
394
391
typedef T value_type;
@@ -401,7 +398,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
401
398
{
402
399
m_shape = zeros;
403
400
m_strides = zeros;
404
- m_empty = true ;
405
401
}
406
402
407
403
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>
418
414
m_data = other.m_data ;
419
415
m_shape = other.m_shape ;
420
416
m_strides = other.m_strides ;
421
- m_empty = other.m_empty ;
422
417
}
423
418
424
419
array_view (PyArrayObject *arr, char *data, npy_intp *shape, npy_intp *strides)
425
420
{
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 );
432
422
for (size_t i = 0 ; i < ND; i++) {
433
423
if (shape[i] == 0 ) {
434
- m_empty = true ;
424
+ empty = true ;
435
425
}
436
426
}
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;
437
437
}
438
438
439
439
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>
464
464
m_data = other.m_data ;
465
465
m_shape = other.m_shape ;
466
466
m_strides = other.m_strides ;
467
- m_empty = other.m_empty ;
468
467
}
469
468
return *this ;
470
469
}
@@ -479,7 +478,6 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
479
478
m_data = NULL ;
480
479
m_shape = zeros;
481
480
m_strides = zeros;
482
- m_empty = true ;
483
481
} else {
484
482
if (contiguous) {
485
483
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>
498
496
m_strides = zeros;
499
497
if (PyArray_NDIM (tmp) == 0 && ND == 0 ) {
500
498
m_arr = tmp;
501
- m_empty = true ;
502
499
return 1 ;
503
500
}
504
501
}
@@ -516,13 +513,17 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
516
513
m_arr = tmp;
517
514
m_shape = PyArray_DIMS (m_arr);
518
515
m_strides = PyArray_STRIDES (m_arr);
519
- m_data = (char *)PyArray_BYTES (tmp);
520
- m_empty = (ND == 0 );
516
+ bool empty = (ND == 0 );
521
517
for (size_t i = 0 ; i < ND; i++) {
522
518
if (m_shape[i] == 0 ) {
523
- m_empty = true ;
519
+ empty = true ;
524
520
}
525
521
}
522
+ if (empty) {
523
+ m_data = NULL ;
524
+ } else {
525
+ m_data = (char *)PyArray_BYTES (tmp);
526
+ }
526
527
}
527
528
528
529
return 1 ;
0 commit comments