@@ -615,26 +615,23 @@ get_new_indexers_and_screen(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kw
615
615
& PyArray_Type , & indexers ,
616
616
& PyArray_Type , & positions
617
617
))
618
- {
618
+ {
619
619
return NULL ;
620
620
}
621
621
622
- if (PyArray_NDIM (indexers ) != 1 )
623
- {
622
+ if (PyArray_NDIM (indexers ) != 1 ) {
624
623
PyErr_SetString (PyExc_ValueError , "indexers must be 1-dimensional" );
625
624
return NULL ;
626
625
}
627
626
628
- if (PyArray_TYPE (indexers ) != NPY_INT64 )
629
- {
627
+ if (PyArray_TYPE (indexers ) != NPY_INT64 ) {
630
628
PyErr_SetString (PyExc_ValueError , "Array must be of type np.int64" );
631
629
return NULL ;
632
630
}
633
631
634
632
npy_intp num_unique = PyArray_SIZE (positions );
635
633
636
- if (num_unique > PyArray_SIZE (indexers ))
637
- {
634
+ if (num_unique > PyArray_SIZE (indexers )) {
638
635
// This algorithm is only optimal if the number of unique elements is
639
636
// less than the number of elements in the indexers.
640
637
// Otherwise, the most optimal code is ``np.unique(indexers, return_index=True)[1]``
@@ -648,10 +645,10 @@ get_new_indexers_and_screen(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kw
648
645
649
646
npy_intp dims = {num_unique };
650
647
PyArrayObject * element_locations = (PyArrayObject * )PyArray_Empty (
651
- 1 , // ndim
652
- & dims , // shape
653
- PyArray_DescrFromType (NPY_INT64 ), // dtype
654
- 0 // fortran
648
+ 1 , // ndim
649
+ & dims , // shape
650
+ PyArray_DescrFromType (NPY_INT64 ), // dtype
651
+ 0 // fortran
655
652
);
656
653
if (element_locations == NULL ) {
657
654
return NULL ;
@@ -688,15 +685,11 @@ get_new_indexers_and_screen(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kw
688
685
// over using numpy's iteration APIs.
689
686
npy_int64 * element_location_values = (npy_int64 * )PyArray_DATA (element_locations );
690
687
npy_int64 * new_indexers_values = (npy_int64 * )PyArray_DATA (new_indexers );
691
- npy_int64 * array_values = (npy_int64 * )PyArray_DATA (indexers );
692
-
693
- npy_int64 num_found = 0 ;
694
688
695
689
// Now, implement the core algorithm by looping over the ``indexers``.
696
690
// We need to use numpy's iteration API, as the ``indexers`` could be
697
691
// C-contiguous, F-contiguous, both, or neither.
698
692
// See https://numpy.org/doc/stable/reference/c-api/iterator.html#simple-iteration-example
699
-
700
693
NpyIter * indexer_iter = NpyIter_New (
701
694
indexers ,
702
695
NPY_ITER_READONLY | NPY_ITER_EXTERNAL_LOOP ,
@@ -725,6 +718,7 @@ get_new_indexers_and_screen(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kw
725
718
npy_intp * innersizeptr = NpyIter_GetInnerLoopSizePtr (indexer_iter );
726
719
727
720
size_t i = 0 ;
721
+ npy_int64 num_found = 0 ;
728
722
do {
729
723
// Get the inner loop data/stride/inner_size values
730
724
char * data = * dataptr ;
@@ -733,15 +727,13 @@ get_new_indexers_and_screen(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kw
733
727
npy_int64 element ;
734
728
735
729
while (inner_size -- ) {
736
- memcpy (& element , data , sizeof (long ));
730
+ memcpy (& element , data , sizeof (npy_int64 ));
737
731
738
- if (element_location_values [element ] == num_unique )
739
- {
732
+ if (element_location_values [element ] == num_unique ) {
740
733
element_location_values [element ] = num_found ;
741
734
++ num_found ;
742
735
743
- if (num_found == num_unique )
744
- {
736
+ if (num_found == num_unique ) {
745
737
// This insight is core to the performance of the algorithm.
746
738
// If we have found every possible indexer, we can simply return
747
739
// back the inputs! Essentially, we can observe on <= single pass
@@ -770,7 +762,6 @@ get_new_indexers_and_screen(PyObject *Py_UNUSED(m), PyObject *args, PyObject *kw
770
762
return result ;
771
763
}
772
764
773
-
774
765
//------------------------------------------------------------------------------
775
766
// ArrayGO
776
767
//------------------------------------------------------------------------------
0 commit comments