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

Function partition_prep_kth_array

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

* make kth array positive, ravel and sort it */

Source from the content-addressed store, hash-verified

1544 * make kth array positive, ravel and sort it
1545 */
1546static PyArrayObject *
1547partition_prep_kth_array(PyArrayObject * ktharray,
1548 PyArrayObject * op,
1549 int axis)
1550{
1551 const npy_intp * shape = PyArray_SHAPE(op);
1552 PyArrayObject * kthrvl;
1553 npy_intp * kth;
1554 npy_intp nkth, i;
1555
1556 if (PyArray_ISBOOL(ktharray)) {
1557 /* 2021-09-29, NumPy 1.22 */
1558 if (DEPRECATE(
1559 "Passing booleans as partition index is deprecated"
1560 " (warning added in NumPy 1.22)") < 0) {
1561 return NULL;
1562 }
1563 }
1564 else if (!PyArray_ISINTEGER(ktharray)) {
1565 PyErr_Format(PyExc_TypeError, "Partition index must be integer");
1566 return NULL;
1567 }
1568
1569 if (PyArray_NDIM(ktharray) > 1) {
1570 PyErr_Format(PyExc_ValueError, "kth array must have dimension <= 1");
1571 return NULL;
1572 }
1573 kthrvl = (PyArrayObject *)PyArray_Cast(ktharray, NPY_INTP);
1574
1575 if (kthrvl == NULL)
1576 return NULL;
1577
1578 kth = PyArray_DATA(kthrvl);
1579 nkth = PyArray_SIZE(kthrvl);
1580
1581 for (i = 0; i < nkth; i++) {
1582 if (kth[i] < 0) {
1583 kth[i] += shape[axis];
1584 }
1585 if (PyArray_SIZE(op) != 0 &&
1586 (kth[i] < 0 || kth[i] >= shape[axis])) {
1587 PyErr_Format(PyExc_ValueError, "kth(=%zd) out of bounds (%zd)",
1588 kth[i], shape[axis]);
1589 Py_XDECREF(kthrvl);
1590 return NULL;
1591 }
1592 }
1593
1594 /*
1595 * sort the array of kths so the partitions will
1596 * not trample on each other
1597 */
1598 if (PyArray_SIZE(kthrvl) > 1) {
1599 PyArray_Sort(kthrvl, -1, NPY_QUICKSORT);
1600 }
1601
1602 return kthrvl;
1603}

Callers 2

PyArray_PartitionFunction · 0.85
PyArray_ArgPartitionFunction · 0.85

Calls 4

PyArray_SHAPEFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_SortFunction · 0.85

Tested by

no test coverage detected