@@ -427,10 +427,11 @@ NPY_NO_EXPORT PyObject *
427
427
PyArray_PutMask (PyArrayObject * self , PyObject * values0 , PyObject * mask0 )
428
428
{
429
429
PyArray_FastPutmaskFunc * func ;
430
- PyArrayObject * mask , * values ;
430
+ PyArrayObject * mask , * values ;
431
431
PyArray_Descr * dtype ;
432
- npy_intp i , chunk , ni , max_item , nv , tmp ;
432
+ npy_intp i , j , chunk , ni , max_item , nv ;
433
433
char * src , * dest ;
434
+ npy_bool * mask_data ;
434
435
int copied = 0 ;
435
436
436
437
mask = NULL ;
@@ -469,6 +470,7 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0)
469
470
"the same size" );
470
471
goto fail ;
471
472
}
473
+ mask_data = PyArray_DATA (mask );
472
474
dtype = PyArray_DESCR (self );
473
475
Py_INCREF (dtype );
474
476
values = (PyArrayObject * )PyArray_FromAny (values0 , dtype ,
@@ -483,14 +485,20 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0)
483
485
Py_INCREF (Py_None );
484
486
return Py_None ;
485
487
}
488
+ src = PyArray_DATA (values );
489
+
486
490
if (PyDataType_REFCHK (PyArray_DESCR (self ))) {
487
- for (i = 0 ; i < ni ; i ++ ) {
488
- tmp = ((npy_bool * )(PyArray_DATA (mask )))[i ];
489
- if (tmp ) {
490
- src = PyArray_BYTES (values ) + chunk * (i % nv );
491
- PyArray_Item_INCREF (src , PyArray_DESCR (self ));
492
- PyArray_Item_XDECREF (dest + i * chunk , PyArray_DESCR (self ));
493
- memmove (dest + i * chunk , src , chunk );
491
+ for (i = 0 , j = 0 ; i < ni ; i ++ , j ++ ) {
492
+ if (j >= nv ) {
493
+ j = 0 ;
494
+ }
495
+ if (mask_data [i ]) {
496
+ char * src_ptr = src + j * chunk ;
497
+ char * dest_ptr = dest + i * chunk ;
498
+
499
+ PyArray_Item_INCREF (src_ptr , PyArray_DESCR (self ));
500
+ PyArray_Item_XDECREF (dest_ptr , PyArray_DESCR (self ));
501
+ memmove (dest_ptr , src_ptr , chunk );
494
502
}
495
503
}
496
504
}
@@ -499,16 +507,17 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0)
499
507
NPY_BEGIN_THREADS_DESCR (PyArray_DESCR (self ));
500
508
func = PyArray_DESCR (self )-> f -> fastputmask ;
501
509
if (func == NULL ) {
502
- for (i = 0 ; i < ni ; i ++ ) {
503
- tmp = ((npy_bool * )(PyArray_DATA (mask )))[i ];
504
- if (tmp ) {
505
- src = PyArray_BYTES (values ) + chunk * (i % nv );
506
- memmove (dest + i * chunk , src , chunk );
510
+ for (i = 0 , j = 0 ; i < ni ; i ++ , j ++ ) {
511
+ if (j >= nv ) {
512
+ j = 0 ;
513
+ }
514
+ if (mask_data [i ]) {
515
+ memmove (dest + i * chunk , src + j * chunk , chunk );
507
516
}
508
517
}
509
518
}
510
519
else {
511
- func (dest , PyArray_DATA ( mask ) , ni , PyArray_DATA ( values ) , nv );
520
+ func (dest , mask_data , ni , src , nv );
512
521
}
513
522
NPY_END_THREADS ;
514
523
}
0 commit comments