|
| 1 | +/* selectlib.c */ |
| 2 | + |
| 3 | +#include <Python.h> |
| 4 | +#include <listobject.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <time.h> |
| 7 | + |
| 8 | +#ifndef PY_SSIZE_T_CLEAN |
| 9 | +#define PY_SSIZE_T_CLEAN |
| 10 | +#endif |
| 11 | + |
| 12 | +/* |
| 13 | + Helper function that compares two PyObject*s using the < operator. |
| 14 | + Returns 1 if a < b, 0 if not, or -1 if an error occurred. |
| 15 | +*/ |
| 16 | +static int |
| 17 | +less_than(PyObject *a, PyObject *b) |
| 18 | +{ |
| 19 | + int cmp = PyObject_RichCompareBool(a, b, Py_LT); |
| 20 | + return cmp; |
| 21 | +} |
| 22 | + |
| 23 | +/* |
| 24 | + Swap the elements at indices i and j in the Python list. |
| 25 | + If keys is not NULL, also swap the corresponding keys. |
| 26 | + This function directly manipulates the list internals. |
| 27 | +*/ |
| 28 | +static void |
| 29 | +swap_items(PyObject *list, Py_ssize_t i, Py_ssize_t j, PyObject **keys) |
| 30 | +{ |
| 31 | + /* Cast to PyListObject to access the internal array */ |
| 32 | + PyListObject *lst = (PyListObject *)list; |
| 33 | + PyObject *temp = lst->ob_item[i]; |
| 34 | + lst->ob_item[i] = lst->ob_item[j]; |
| 35 | + lst->ob_item[j] = temp; |
| 36 | + |
| 37 | + if (keys != NULL) { |
| 38 | + PyObject *temp_key = keys[i]; |
| 39 | + keys[i] = keys[j]; |
| 40 | + keys[j] = temp_key; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/* |
| 45 | + Partition the subarray [left, right] around a pivot. |
| 46 | + The pivot is initially at pivot_index. After partitioning, |
| 47 | + the pivot is placed at new_pivot_index (returned via pointer). |
| 48 | + Returns 0 on success or -1 if an error occurred. |
| 49 | +*/ |
| 50 | +static int |
| 51 | +partition(PyObject *list, PyObject **keys, Py_ssize_t left, Py_ssize_t right, |
| 52 | + Py_ssize_t pivot_index, Py_ssize_t *new_pivot_index) |
| 53 | +{ |
| 54 | + /* Move pivot to the end */ |
| 55 | + swap_items(list, pivot_index, right, keys); |
| 56 | + |
| 57 | + PyObject *pivot_val; |
| 58 | + if (keys != NULL) |
| 59 | + pivot_val = keys[right]; |
| 60 | + else |
| 61 | + pivot_val = PyList_GET_ITEM(list, right); |
| 62 | + |
| 63 | + Py_ssize_t store_index = left; |
| 64 | + for (Py_ssize_t i = left; i < right; i++) { |
| 65 | + PyObject *current; |
| 66 | + if (keys != NULL) |
| 67 | + current = keys[i]; |
| 68 | + else |
| 69 | + current = PyList_GET_ITEM(list, i); |
| 70 | + |
| 71 | + int cmp = less_than(current, pivot_val); |
| 72 | + if (cmp < 0) |
| 73 | + return -1; |
| 74 | + if (cmp) { |
| 75 | + swap_items(list, i, store_index, keys); |
| 76 | + store_index++; |
| 77 | + } |
| 78 | + } |
| 79 | + swap_items(list, store_index, right, keys); |
| 80 | + *new_pivot_index = store_index; |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | + In-place quickselect algorithm on the list. |
| 86 | + It partitions the list (and the keys array if provided) so that |
| 87 | + the element at index k is in its final sorted position. |
| 88 | + Operates on indices in [left, right]. |
| 89 | + Returns 0 on success or -1 on error. |
| 90 | +*/ |
| 91 | +static int |
| 92 | +quickselect_inplace(PyObject *list, PyObject **keys, |
| 93 | + Py_ssize_t left, Py_ssize_t right, Py_ssize_t k) |
| 94 | +{ |
| 95 | + /* Seed the random number generator once (if needed) */ |
| 96 | + static int seeded = 0; |
| 97 | + if (!seeded) { |
| 98 | + srand((unsigned)time(NULL)); |
| 99 | + seeded = 1; |
| 100 | + } |
| 101 | + |
| 102 | + while (left < right) { |
| 103 | + /* Choose a random pivot_index between left and right (inclusive) */ |
| 104 | + Py_ssize_t pivot_index = left + rand() % (right - left + 1); |
| 105 | + Py_ssize_t pos; |
| 106 | + if (partition(list, keys, left, right, pivot_index, &pos) < 0) |
| 107 | + return -1; |
| 108 | + if (pos == k) |
| 109 | + return 0; |
| 110 | + else if (k < pos) |
| 111 | + right = pos - 1; |
| 112 | + else |
| 113 | + left = pos + 1; |
| 114 | + } |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +/* |
| 119 | + quickselect(values: list[Any], index: int, key=None) -> None |
| 120 | +
|
| 121 | + Partition the list in-place such that the element in the specified |
| 122 | + index is the one that would be there in a sorted list. An optional |
| 123 | + key function may be provided to extract a comparison key from each element. |
| 124 | +*/ |
| 125 | +static PyObject * |
| 126 | +selectlib_quickselect(PyObject *self, PyObject *args, PyObject *kwargs) |
| 127 | +{ |
| 128 | + static char *kwlist[] = {"values", "index", "key", NULL}; |
| 129 | + PyObject *values; |
| 130 | + Py_ssize_t target_index; |
| 131 | + PyObject *key = Py_None; |
| 132 | + |
| 133 | + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "On|O:quickselect", |
| 134 | + kwlist, &values, &target_index, &key)) |
| 135 | + return NULL; |
| 136 | + |
| 137 | + if (!PyList_Check(values)) { |
| 138 | + PyErr_SetString(PyExc_TypeError, "values must be a list"); |
| 139 | + return NULL; |
| 140 | + } |
| 141 | + |
| 142 | + Py_ssize_t n = PyList_Size(values); |
| 143 | + if (target_index < 0 || target_index >= n) { |
| 144 | + PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 145 | + return NULL; |
| 146 | + } |
| 147 | + |
| 148 | + int use_key = 0; |
| 149 | + if (key != Py_None) { |
| 150 | + if (!PyCallable_Check(key)) { |
| 151 | + PyErr_SetString(PyExc_TypeError, "key must be callable"); |
| 152 | + return NULL; |
| 153 | + } |
| 154 | + use_key = 1; |
| 155 | + } |
| 156 | + |
| 157 | + PyObject **keys_arr = NULL; |
| 158 | + if (use_key) { |
| 159 | + keys_arr = PyMem_New(PyObject *, n); |
| 160 | + if (keys_arr == NULL) { |
| 161 | + PyErr_NoMemory(); |
| 162 | + return NULL; |
| 163 | + } |
| 164 | + for (Py_ssize_t i = 0; i < n; i++) { |
| 165 | + PyObject *item = PyList_GET_ITEM(values, i); |
| 166 | + PyObject *key_val = PyObject_CallFunctionObjArgs(key, item, NULL); |
| 167 | + if (key_val == NULL) { |
| 168 | + for (Py_ssize_t j = 0; j < i; j++) |
| 169 | + Py_DECREF(keys_arr[j]); |
| 170 | + PyMem_Free(keys_arr); |
| 171 | + return NULL; |
| 172 | + } |
| 173 | + keys_arr[i] = key_val; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (n > 0) { |
| 178 | + if (quickselect_inplace(values, keys_arr, 0, n - 1, target_index) < 0) { |
| 179 | + if (use_key) { |
| 180 | + for (Py_ssize_t i = 0; i < n; i++) |
| 181 | + Py_DECREF(keys_arr[i]); |
| 182 | + PyMem_Free(keys_arr); |
| 183 | + } |
| 184 | + return NULL; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if (use_key) { |
| 189 | + for (Py_ssize_t i = 0; i < n; i++) |
| 190 | + Py_DECREF(keys_arr[i]); |
| 191 | + PyMem_Free(keys_arr); |
| 192 | + } |
| 193 | + |
| 194 | + Py_RETURN_NONE; |
| 195 | +} |
| 196 | + |
| 197 | +static PyMethodDef selectlib_methods[] = { |
| 198 | + {"quickselect", (PyCFunction)selectlib_quickselect, |
| 199 | + METH_VARARGS | METH_KEYWORDS, |
| 200 | + "quickselect(values: list[Any], index: int, key=None) -> None\n\n" |
| 201 | + "Partition the list in-place so that the element at the given index is in its " |
| 202 | + "final sorted position."}, |
| 203 | + {NULL, NULL, 0, NULL} |
| 204 | +}; |
| 205 | + |
| 206 | +static struct PyModuleDef selectlibmodule = { |
| 207 | + PyModuleDef_HEAD_INIT, |
| 208 | + "selectlib", |
| 209 | + "Module that implements the quickselect algorithm.", |
| 210 | + -1, |
| 211 | + selectlib_methods, |
| 212 | +}; |
| 213 | + |
| 214 | +PyMODINIT_FUNC |
| 215 | +PyInit_selectlib(void) |
| 216 | +{ |
| 217 | + return PyModule_Create(&selectlibmodule); |
| 218 | +} |
0 commit comments