MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_SearchSorted

Function PyArray_SearchSorted

numpy/core/src/multiarray/item_selection.c:2056–2206  ·  view source on GitHub ↗

NUMPY_API * * Search the sorted array op1 for the location of the items in op2. The * result is an array of indexes, one for each element in op2, such that if * the item were to be inserted in op1 just before that index the array * would still be in sorted order. * * Parameters * ---------- * op1 : PyArrayObject * * Array to be searched, must be 1-D. * op2 : PyObject * * Array

Source from the content-addressed store, hash-verified

2054 * Binary search is used to find the indexes.
2055 */
2056NPY_NO_EXPORT PyObject *
2057PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2,
2058 NPY_SEARCHSIDE side, PyObject *perm)
2059{
2060 PyArrayObject *ap1 = NULL;
2061 PyArrayObject *ap2 = NULL;
2062 PyArrayObject *ap3 = NULL;
2063 PyArrayObject *sorter = NULL;
2064 PyArrayObject *ret = NULL;
2065 PyArray_Descr *dtype;
2066 int ap1_flags = NPY_ARRAY_NOTSWAPPED | NPY_ARRAY_ALIGNED;
2067 PyArray_BinSearchFunc *binsearch = NULL;
2068 PyArray_ArgBinSearchFunc *argbinsearch = NULL;
2069 NPY_BEGIN_THREADS_DEF;
2070
2071 /* Find common type */
2072 dtype = PyArray_DescrFromObject((PyObject *)op2, PyArray_DESCR(op1));
2073 if (dtype == NULL) {
2074 return NULL;
2075 }
2076 /* refs to dtype we own = 1 */
2077
2078 /* Look for binary search function */
2079 if (perm) {
2080 argbinsearch = get_argbinsearch_func(dtype, side);
2081 }
2082 else {
2083 binsearch = get_binsearch_func(dtype, side);
2084 }
2085 if (binsearch == NULL && argbinsearch == NULL) {
2086 PyErr_SetString(PyExc_TypeError, "compare not supported for type");
2087 /* refs to dtype we own = 1 */
2088 Py_DECREF(dtype);
2089 /* refs to dtype we own = 0 */
2090 return NULL;
2091 }
2092
2093 /* need ap2 as contiguous array and of right type */
2094 /* refs to dtype we own = 1 */
2095 Py_INCREF(dtype);
2096 /* refs to dtype we own = 2 */
2097 ap2 = (PyArrayObject *)PyArray_CheckFromAny(op2, dtype,
2098 0, 0,
2099 NPY_ARRAY_CARRAY_RO | NPY_ARRAY_NOTSWAPPED,
2100 NULL);
2101 /* refs to dtype we own = 1, array creation steals one even on failure */
2102 if (ap2 == NULL) {
2103 Py_DECREF(dtype);
2104 /* refs to dtype we own = 0 */
2105 return NULL;
2106 }
2107
2108 /*
2109 * If the needle (ap2) is larger than the haystack (op1) we copy the
2110 * haystack to a contiguous array for improved cache utilization.
2111 */
2112 if (PyArray_SIZE(ap2) > PyArray_SIZE(op1)) {
2113 ap1_flags |= NPY_ARRAY_CARRAY_RO;

Callers 1

array_searchsortedFunction · 0.85

Calls 11

PyArray_DescrFromObjectFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_CheckFromAnyFunction · 0.85
PyArray_FromArrayFunction · 0.85
PyArray_NewFromDescrFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DIMSFunction · 0.85
binsearchFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_STRIDESFunction · 0.85
argbinsearchFunction · 0.85

Tested by

no test coverage detected