NUMPY_API * Partition an array in-place */
| 1607 | * Partition an array in-place |
| 1608 | */ |
| 1609 | NPY_NO_EXPORT int |
| 1610 | PyArray_Partition(PyArrayObject *op, PyArrayObject * ktharray, int axis, |
| 1611 | NPY_SELECTKIND which) |
| 1612 | { |
| 1613 | PyArrayObject *kthrvl; |
| 1614 | PyArray_PartitionFunc *part; |
| 1615 | PyArray_SortFunc *sort; |
| 1616 | int n = PyArray_NDIM(op); |
| 1617 | int ret; |
| 1618 | |
| 1619 | if (check_and_adjust_axis(&axis, n) < 0) { |
| 1620 | return -1; |
| 1621 | } |
| 1622 | |
| 1623 | if (PyArray_FailUnlessWriteable(op, "partition array") < 0) { |
| 1624 | return -1; |
| 1625 | } |
| 1626 | |
| 1627 | if (which < 0 || which >= NPY_NSELECTS) { |
| 1628 | PyErr_SetString(PyExc_ValueError, "not a valid partition kind"); |
| 1629 | return -1; |
| 1630 | } |
| 1631 | part = get_partition_func(PyArray_TYPE(op), which); |
| 1632 | if (part == NULL) { |
| 1633 | /* Use sorting, slower but equivalent */ |
| 1634 | if (PyArray_DESCR(op)->f->compare) { |
| 1635 | sort = npy_quicksort; |
| 1636 | } |
| 1637 | else { |
| 1638 | PyErr_SetString(PyExc_TypeError, |
| 1639 | "type does not have compare function"); |
| 1640 | return -1; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | /* Process ktharray even if using sorting to do bounds checking */ |
| 1645 | kthrvl = partition_prep_kth_array(ktharray, op, axis); |
| 1646 | if (kthrvl == NULL) { |
| 1647 | return -1; |
| 1648 | } |
| 1649 | |
| 1650 | ret = _new_sortlike(op, axis, sort, part, |
| 1651 | PyArray_DATA(kthrvl), PyArray_SIZE(kthrvl)); |
| 1652 | |
| 1653 | Py_DECREF(kthrvl); |
| 1654 | |
| 1655 | return ret; |
| 1656 | } |
| 1657 | |
| 1658 | |
| 1659 | /*NUMPY_API |
no test coverage detected