Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 1 | /* Bisection algorithms. Drop in replacement for bisect.py |
| 2 | |
| 3 | Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru). |
| 4 | */ |
| 5 | |
Antoine Pitrou | a103b96 | 2012-05-16 14:37:54 +0200 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 7 | #include "Python.h" |
| 8 | |
Martin v. Löwis | e75fc14 | 2013-11-07 18:46:53 +0100 | [diff] [blame] | 9 | _Py_IDENTIFIER(insert); |
| 10 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 11 | static Py_ssize_t |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 12 | internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 13 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | PyObject *litem; |
| 15 | Py_ssize_t mid, res; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 16 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | if (lo < 0) { |
| 18 | PyErr_SetString(PyExc_ValueError, "lo must be non-negative"); |
| 19 | return -1; |
| 20 | } |
| 21 | if (hi == -1) { |
| 22 | hi = PySequence_Size(list); |
| 23 | if (hi < 0) |
| 24 | return -1; |
| 25 | } |
| 26 | while (lo < hi) { |
Mark Dickinson | a13b109 | 2012-04-15 16:30:35 +0100 | [diff] [blame] | 27 | /* The (size_t)cast ensures that the addition and subsequent division |
| 28 | are performed as unsigned operations, avoiding difficulties from |
| 29 | signed overflow. (See issue 13496.) */ |
| 30 | mid = ((size_t)lo + hi) / 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | litem = PySequence_GetItem(list, mid); |
| 32 | if (litem == NULL) |
| 33 | return -1; |
| 34 | res = PyObject_RichCompareBool(item, litem, Py_LT); |
| 35 | Py_DECREF(litem); |
| 36 | if (res < 0) |
| 37 | return -1; |
| 38 | if (res) |
| 39 | hi = mid; |
| 40 | else |
| 41 | lo = mid + 1; |
| 42 | } |
| 43 | return lo; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static PyObject * |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 47 | bisect_right(PyObject *self, PyObject *args, PyObject *kw) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 48 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | PyObject *list, *item; |
| 50 | Py_ssize_t lo = 0; |
| 51 | Py_ssize_t hi = -1; |
| 52 | Py_ssize_t index; |
| 53 | static char *keywords[] = {"a", "x", "lo", "hi", NULL}; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 54 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 55 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right", |
| 56 | keywords, &list, &item, &lo, &hi)) |
| 57 | return NULL; |
| 58 | index = internal_bisect_right(list, item, lo, hi); |
| 59 | if (index < 0) |
| 60 | return NULL; |
| 61 | return PyLong_FromSsize_t(index); |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | PyDoc_STRVAR(bisect_right_doc, |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 65 | "bisect_right(a, x[, lo[, hi]]) -> index\n\ |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 66 | \n\ |
| 67 | Return the index where to insert item x in list a, assuming a is sorted.\n\ |
| 68 | \n\ |
| 69 | The return value i is such that all e in a[:i] have e <= x, and all e in\n\ |
| 70 | a[i:] have e > x. So if x already appears in the list, i points just\n\ |
| 71 | beyond the rightmost x already there\n\ |
| 72 | \n\ |
| 73 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 74 | slice of a to be searched.\n"); |
| 75 | |
| 76 | static PyObject * |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 77 | insort_right(PyObject *self, PyObject *args, PyObject *kw) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 78 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | PyObject *list, *item, *result; |
| 80 | Py_ssize_t lo = 0; |
| 81 | Py_ssize_t hi = -1; |
| 82 | Py_ssize_t index; |
| 83 | static char *keywords[] = {"a", "x", "lo", "hi", NULL}; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right", |
| 86 | keywords, &list, &item, &lo, &hi)) |
| 87 | return NULL; |
| 88 | index = internal_bisect_right(list, item, lo, hi); |
| 89 | if (index < 0) |
| 90 | return NULL; |
| 91 | if (PyList_CheckExact(list)) { |
| 92 | if (PyList_Insert(list, index, item) < 0) |
| 93 | return NULL; |
| 94 | } else { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 95 | result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | if (result == NULL) |
| 97 | return NULL; |
| 98 | Py_DECREF(result); |
| 99 | } |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | Py_RETURN_NONE; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | PyDoc_STRVAR(insort_right_doc, |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 105 | "insort_right(a, x[, lo[, hi]])\n\ |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 106 | \n\ |
| 107 | Insert item x in list a, and keep it sorted assuming a is sorted.\n\ |
| 108 | \n\ |
| 109 | If x is already in a, insert it to the right of the rightmost x.\n\ |
| 110 | \n\ |
| 111 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 112 | slice of a to be searched.\n"); |
| 113 | |
Benjamin Peterson | d631371 | 2008-07-31 16:23:04 +0000 | [diff] [blame] | 114 | static Py_ssize_t |
| 115 | internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | PyObject *litem; |
| 118 | Py_ssize_t mid, res; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | if (lo < 0) { |
| 121 | PyErr_SetString(PyExc_ValueError, "lo must be non-negative"); |
| 122 | return -1; |
| 123 | } |
| 124 | if (hi == -1) { |
| 125 | hi = PySequence_Size(list); |
| 126 | if (hi < 0) |
| 127 | return -1; |
| 128 | } |
| 129 | while (lo < hi) { |
Mark Dickinson | a13b109 | 2012-04-15 16:30:35 +0100 | [diff] [blame] | 130 | /* The (size_t)cast ensures that the addition and subsequent division |
| 131 | are performed as unsigned operations, avoiding difficulties from |
| 132 | signed overflow. (See issue 13496.) */ |
| 133 | mid = ((size_t)lo + hi) / 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | litem = PySequence_GetItem(list, mid); |
| 135 | if (litem == NULL) |
| 136 | return -1; |
| 137 | res = PyObject_RichCompareBool(litem, item, Py_LT); |
| 138 | Py_DECREF(litem); |
| 139 | if (res < 0) |
| 140 | return -1; |
| 141 | if (res) |
| 142 | lo = mid + 1; |
| 143 | else |
| 144 | hi = mid; |
| 145 | } |
| 146 | return lo; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static PyObject * |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 150 | bisect_left(PyObject *self, PyObject *args, PyObject *kw) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | PyObject *list, *item; |
| 153 | Py_ssize_t lo = 0; |
| 154 | Py_ssize_t hi = -1; |
| 155 | Py_ssize_t index; |
| 156 | static char *keywords[] = {"a", "x", "lo", "hi", NULL}; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left", |
| 159 | keywords, &list, &item, &lo, &hi)) |
| 160 | return NULL; |
| 161 | index = internal_bisect_left(list, item, lo, hi); |
| 162 | if (index < 0) |
| 163 | return NULL; |
| 164 | return PyLong_FromSsize_t(index); |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | PyDoc_STRVAR(bisect_left_doc, |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 168 | "bisect_left(a, x[, lo[, hi]]) -> index\n\ |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 169 | \n\ |
| 170 | Return the index where to insert item x in list a, assuming a is sorted.\n\ |
| 171 | \n\ |
| 172 | The return value i is such that all e in a[:i] have e < x, and all e in\n\ |
| 173 | a[i:] have e >= x. So if x already appears in the list, i points just\n\ |
| 174 | before the leftmost x already there.\n\ |
| 175 | \n\ |
| 176 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 177 | slice of a to be searched.\n"); |
| 178 | |
| 179 | static PyObject * |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 180 | insort_left(PyObject *self, PyObject *args, PyObject *kw) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | PyObject *list, *item, *result; |
| 183 | Py_ssize_t lo = 0; |
| 184 | Py_ssize_t hi = -1; |
| 185 | Py_ssize_t index; |
| 186 | static char *keywords[] = {"a", "x", "lo", "hi", NULL}; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left", |
| 189 | keywords, &list, &item, &lo, &hi)) |
| 190 | return NULL; |
| 191 | index = internal_bisect_left(list, item, lo, hi); |
| 192 | if (index < 0) |
| 193 | return NULL; |
| 194 | if (PyList_CheckExact(list)) { |
| 195 | if (PyList_Insert(list, index, item) < 0) |
| 196 | return NULL; |
| 197 | } else { |
Antoine Pitrou | b7d033d | 2012-05-16 14:39:36 +0200 | [diff] [blame] | 198 | result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | if (result == NULL) |
| 200 | return NULL; |
| 201 | Py_DECREF(result); |
| 202 | } |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | Py_RETURN_NONE; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | PyDoc_STRVAR(insort_left_doc, |
Raymond Hettinger | cc9a951 | 2005-10-05 11:39:12 +0000 | [diff] [blame] | 208 | "insort_left(a, x[, lo[, hi]])\n\ |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 209 | \n\ |
| 210 | Insert item x in list a, and keep it sorted assuming a is sorted.\n\ |
| 211 | \n\ |
| 212 | If x is already in a, insert it to the left of the leftmost x.\n\ |
| 213 | \n\ |
| 214 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 215 | slice of a to be searched.\n"); |
| 216 | |
| 217 | PyDoc_STRVAR(bisect_doc, "Alias for bisect_right().\n"); |
| 218 | PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n"); |
| 219 | |
| 220 | static PyMethodDef bisect_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | {"bisect_right", (PyCFunction)bisect_right, |
| 222 | METH_VARARGS|METH_KEYWORDS, bisect_right_doc}, |
| 223 | {"bisect", (PyCFunction)bisect_right, |
| 224 | METH_VARARGS|METH_KEYWORDS, bisect_doc}, |
| 225 | {"insort_right", (PyCFunction)insort_right, |
| 226 | METH_VARARGS|METH_KEYWORDS, insort_right_doc}, |
| 227 | {"insort", (PyCFunction)insort_right, |
| 228 | METH_VARARGS|METH_KEYWORDS, insort_doc}, |
| 229 | {"bisect_left", (PyCFunction)bisect_left, |
| 230 | METH_VARARGS|METH_KEYWORDS, bisect_left_doc}, |
| 231 | {"insort_left", (PyCFunction)insort_left, |
| 232 | METH_VARARGS|METH_KEYWORDS, insort_left_doc}, |
| 233 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | PyDoc_STRVAR(module_doc, |
| 237 | "Bisection algorithms.\n\ |
| 238 | \n\ |
| 239 | This module provides support for maintaining a list in sorted order without\n\ |
| 240 | having to sort the list after each insertion. For long lists of items with\n\ |
| 241 | expensive comparison operations, this can be an improvement over the more\n\ |
| 242 | common approach.\n"); |
| 243 | |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 244 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 245 | static struct PyModuleDef _bisectmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | PyModuleDef_HEAD_INIT, |
| 247 | "_bisect", |
| 248 | module_doc, |
| 249 | -1, |
| 250 | bisect_methods, |
| 251 | NULL, |
| 252 | NULL, |
| 253 | NULL, |
| 254 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | PyMODINIT_FUNC |
| 258 | PyInit__bisect(void) |
| 259 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | return PyModule_Create(&_bisectmodule); |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 261 | } |