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 | |
| 6 | #include "Python.h" |
| 7 | |
| 8 | static int |
| 9 | internal_bisect_right(PyObject *list, PyObject *item, int lo, int hi) |
| 10 | { |
| 11 | PyObject *litem; |
| 12 | int mid, res; |
| 13 | |
| 14 | if (hi == -1) { |
| 15 | hi = PySequence_Size(list); |
| 16 | if (hi < 0) |
| 17 | return -1; |
| 18 | } |
| 19 | while (lo < hi) { |
| 20 | mid = (lo + hi) / 2; |
| 21 | litem = PySequence_GetItem(list, mid); |
| 22 | if (litem == NULL) |
| 23 | return -1; |
| 24 | res = PyObject_RichCompareBool(item, litem, Py_LT); |
| 25 | Py_DECREF(litem); |
| 26 | if (res < 0) |
| 27 | return -1; |
| 28 | if (res) |
| 29 | hi = mid; |
| 30 | else |
| 31 | lo = mid + 1; |
| 32 | } |
| 33 | return lo; |
| 34 | } |
| 35 | |
| 36 | static PyObject * |
| 37 | bisect_right(PyObject *self, PyObject *args) |
| 38 | { |
| 39 | PyObject *list, *item; |
| 40 | int lo = 0; |
| 41 | int hi = -1; |
| 42 | int index; |
| 43 | |
| 44 | if (!PyArg_ParseTuple(args, "OO|ii:bisect_right", |
| 45 | &list, &item, &lo, &hi)) |
| 46 | return NULL; |
| 47 | index = internal_bisect_right(list, item, lo, hi); |
| 48 | if (index < 0) |
| 49 | return NULL; |
| 50 | return PyInt_FromLong(index); |
| 51 | } |
| 52 | |
| 53 | PyDoc_STRVAR(bisect_right_doc, |
| 54 | "bisect_right(list, item[, lo[, hi]]) -> index\n\ |
| 55 | \n\ |
| 56 | Return the index where to insert item x in list a, assuming a is sorted.\n\ |
| 57 | \n\ |
| 58 | The return value i is such that all e in a[:i] have e <= x, and all e in\n\ |
| 59 | a[i:] have e > x. So if x already appears in the list, i points just\n\ |
| 60 | beyond the rightmost x already there\n\ |
| 61 | \n\ |
| 62 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 63 | slice of a to be searched.\n"); |
| 64 | |
| 65 | static PyObject * |
| 66 | insort_right(PyObject *self, PyObject *args) |
| 67 | { |
Michael W. Hudson | c9f510a | 2004-08-02 13:24:54 +0000 | [diff] [blame] | 68 | PyObject *list, *item, *result; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 69 | int lo = 0; |
| 70 | int hi = -1; |
| 71 | int index; |
| 72 | |
| 73 | if (!PyArg_ParseTuple(args, "OO|ii:insort_right", |
| 74 | &list, &item, &lo, &hi)) |
| 75 | return NULL; |
| 76 | index = internal_bisect_right(list, item, lo, hi); |
| 77 | if (index < 0) |
| 78 | return NULL; |
| 79 | if (PyList_Check(list)) { |
| 80 | if (PyList_Insert(list, index, item) < 0) |
| 81 | return NULL; |
| 82 | } else { |
Michael W. Hudson | c9f510a | 2004-08-02 13:24:54 +0000 | [diff] [blame] | 83 | result = PyObject_CallMethod(list, "insert", "iO", |
| 84 | index, item); |
| 85 | if (result == NULL) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 86 | return NULL; |
Michael W. Hudson | c9f510a | 2004-08-02 13:24:54 +0000 | [diff] [blame] | 87 | Py_DECREF(result); |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | Py_RETURN_NONE; |
| 91 | } |
| 92 | |
| 93 | PyDoc_STRVAR(insort_right_doc, |
| 94 | "insort_right(list, item[, lo[, hi]])\n\ |
| 95 | \n\ |
| 96 | Insert item x in list a, and keep it sorted assuming a is sorted.\n\ |
| 97 | \n\ |
| 98 | If x is already in a, insert it to the right of the rightmost x.\n\ |
| 99 | \n\ |
| 100 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 101 | slice of a to be searched.\n"); |
| 102 | |
| 103 | static int |
| 104 | internal_bisect_left(PyObject *list, PyObject *item, int lo, int hi) |
| 105 | { |
| 106 | PyObject *litem; |
| 107 | int mid, res; |
| 108 | |
| 109 | if (hi == -1) { |
| 110 | hi = PySequence_Size(list); |
| 111 | if (hi < 0) |
| 112 | return -1; |
| 113 | } |
| 114 | while (lo < hi) { |
| 115 | mid = (lo + hi) / 2; |
| 116 | litem = PySequence_GetItem(list, mid); |
| 117 | if (litem == NULL) |
| 118 | return -1; |
| 119 | res = PyObject_RichCompareBool(litem, item, Py_LT); |
| 120 | Py_DECREF(litem); |
| 121 | if (res < 0) |
| 122 | return -1; |
| 123 | if (res) |
| 124 | lo = mid + 1; |
| 125 | else |
| 126 | hi = mid; |
| 127 | } |
| 128 | return lo; |
| 129 | } |
| 130 | |
| 131 | static PyObject * |
| 132 | bisect_left(PyObject *self, PyObject *args) |
| 133 | { |
| 134 | PyObject *list, *item; |
| 135 | int lo = 0; |
| 136 | int hi = -1; |
| 137 | int index; |
| 138 | |
| 139 | if (!PyArg_ParseTuple(args, "OO|ii:bisect_left", |
| 140 | &list, &item, &lo, &hi)) |
| 141 | return NULL; |
| 142 | index = internal_bisect_left(list, item, lo, hi); |
| 143 | if (index < 0) |
| 144 | return NULL; |
| 145 | return PyInt_FromLong(index); |
| 146 | } |
| 147 | |
| 148 | PyDoc_STRVAR(bisect_left_doc, |
| 149 | "bisect_left(list, item[, lo[, hi]]) -> index\n\ |
| 150 | \n\ |
| 151 | Return the index where to insert item x in list a, assuming a is sorted.\n\ |
| 152 | \n\ |
| 153 | The return value i is such that all e in a[:i] have e < x, and all e in\n\ |
| 154 | a[i:] have e >= x. So if x already appears in the list, i points just\n\ |
| 155 | before the leftmost x already there.\n\ |
| 156 | \n\ |
| 157 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 158 | slice of a to be searched.\n"); |
| 159 | |
| 160 | static PyObject * |
| 161 | insort_left(PyObject *self, PyObject *args) |
| 162 | { |
Michael W. Hudson | c9f510a | 2004-08-02 13:24:54 +0000 | [diff] [blame] | 163 | PyObject *list, *item, *result; |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 164 | int lo = 0; |
| 165 | int hi = -1; |
| 166 | int index; |
| 167 | |
| 168 | if (!PyArg_ParseTuple(args, "OO|ii:insort_left", |
| 169 | &list, &item, &lo, &hi)) |
| 170 | return NULL; |
| 171 | index = internal_bisect_left(list, item, lo, hi); |
| 172 | if (index < 0) |
| 173 | return NULL; |
| 174 | if (PyList_Check(list)) { |
| 175 | if (PyList_Insert(list, index, item) < 0) |
| 176 | return NULL; |
| 177 | } else { |
Michael W. Hudson | c9f510a | 2004-08-02 13:24:54 +0000 | [diff] [blame] | 178 | result = PyObject_CallMethod(list, "insert", "iO", |
| 179 | index, item); |
| 180 | if (result == NULL) |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 181 | return NULL; |
Michael W. Hudson | c9f510a | 2004-08-02 13:24:54 +0000 | [diff] [blame] | 182 | Py_DECREF(result); |
Raymond Hettinger | 0c41027 | 2004-01-05 10:13:35 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | Py_RETURN_NONE; |
| 186 | } |
| 187 | |
| 188 | PyDoc_STRVAR(insort_left_doc, |
| 189 | "insort_left(list, item[, lo[, hi]])\n\ |
| 190 | \n\ |
| 191 | Insert item x in list a, and keep it sorted assuming a is sorted.\n\ |
| 192 | \n\ |
| 193 | If x is already in a, insert it to the left of the leftmost x.\n\ |
| 194 | \n\ |
| 195 | Optional args lo (default 0) and hi (default len(a)) bound the\n\ |
| 196 | slice of a to be searched.\n"); |
| 197 | |
| 198 | PyDoc_STRVAR(bisect_doc, "Alias for bisect_right().\n"); |
| 199 | PyDoc_STRVAR(insort_doc, "Alias for insort_right().\n"); |
| 200 | |
| 201 | static PyMethodDef bisect_methods[] = { |
| 202 | {"bisect_right", (PyCFunction)bisect_right, |
| 203 | METH_VARARGS, bisect_right_doc}, |
| 204 | {"bisect", (PyCFunction)bisect_right, |
| 205 | METH_VARARGS, bisect_doc}, |
| 206 | {"insort_right", (PyCFunction)insort_right, |
| 207 | METH_VARARGS, insort_right_doc}, |
| 208 | {"insort", (PyCFunction)insort_right, |
| 209 | METH_VARARGS, insort_doc}, |
| 210 | {"bisect_left", (PyCFunction)bisect_left, |
| 211 | METH_VARARGS, bisect_left_doc}, |
| 212 | {"insort_left", (PyCFunction)insort_left, |
| 213 | METH_VARARGS, insort_left_doc}, |
| 214 | {NULL, NULL} /* sentinel */ |
| 215 | }; |
| 216 | |
| 217 | PyDoc_STRVAR(module_doc, |
| 218 | "Bisection algorithms.\n\ |
| 219 | \n\ |
| 220 | This module provides support for maintaining a list in sorted order without\n\ |
| 221 | having to sort the list after each insertion. For long lists of items with\n\ |
| 222 | expensive comparison operations, this can be an improvement over the more\n\ |
| 223 | common approach.\n"); |
| 224 | |
| 225 | PyMODINIT_FUNC |
| 226 | init_bisect(void) |
| 227 | { |
| 228 | PyObject *m; |
| 229 | |
| 230 | m = Py_InitModule3("_bisect", bisect_methods, module_doc); |
| 231 | } |
| 232 | |