Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1 | /* Drop in replacement for heapq.py |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 2 | |
| 3 | C implementation derived directly from heapq.py in Py2.3 |
| 4 | which was written by Kevin O'Connor, augmented by Tim Peters, |
Éric Araujo | 1670b43 | 2010-09-03 22:03:10 +0000 | [diff] [blame] | 5 | annotated by François Pinard, and converted to C by Raymond Hettinger. |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 6 | |
| 7 | */ |
| 8 | |
| 9 | #include "Python.h" |
| 10 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 11 | static int |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 12 | _siftdown(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 13 | { |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 14 | PyObject *newitem, *parent, *olditem; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | int cmp; |
| 16 | Py_ssize_t parentpos; |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 17 | Py_ssize_t size; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 18 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | assert(PyList_Check(heap)); |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 20 | size = PyList_GET_SIZE(heap); |
| 21 | if (pos >= size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 23 | return -1; |
| 24 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 25 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | newitem = PyList_GET_ITEM(heap, pos); |
| 27 | Py_INCREF(newitem); |
| 28 | /* Follow the path to the root, moving parents down until finding |
| 29 | a place newitem fits. */ |
| 30 | while (pos > startpos){ |
| 31 | parentpos = (pos - 1) >> 1; |
| 32 | parent = PyList_GET_ITEM(heap, parentpos); |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 33 | cmp = PyObject_RichCompareBool(newitem, parent, Py_LT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | if (cmp == -1) { |
| 35 | Py_DECREF(newitem); |
| 36 | return -1; |
| 37 | } |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 38 | if (size != PyList_GET_SIZE(heap)) { |
| 39 | Py_DECREF(newitem); |
| 40 | PyErr_SetString(PyExc_RuntimeError, |
| 41 | "list changed size during iteration"); |
| 42 | return -1; |
| 43 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | if (cmp == 0) |
| 45 | break; |
| 46 | Py_INCREF(parent); |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 47 | olditem = PyList_GET_ITEM(heap, pos); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | PyList_SET_ITEM(heap, pos, parent); |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 49 | Py_DECREF(olditem); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | pos = parentpos; |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 51 | if (size != PyList_GET_SIZE(heap)) { |
| 52 | PyErr_SetString(PyExc_RuntimeError, |
| 53 | "list changed size during iteration"); |
| 54 | return -1; |
| 55 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | } |
| 57 | Py_DECREF(PyList_GET_ITEM(heap, pos)); |
| 58 | PyList_SET_ITEM(heap, pos, newitem); |
| 59 | return 0; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static int |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 63 | _siftup(PyListObject *heap, Py_ssize_t pos) |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 64 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | Py_ssize_t startpos, endpos, childpos, rightpos; |
| 66 | int cmp; |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 67 | PyObject *newitem, *tmp, *olditem; |
| 68 | Py_ssize_t size; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | assert(PyList_Check(heap)); |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 71 | size = PyList_GET_SIZE(heap); |
| 72 | endpos = size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | startpos = pos; |
| 74 | if (pos >= endpos) { |
| 75 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 76 | return -1; |
| 77 | } |
| 78 | newitem = PyList_GET_ITEM(heap, pos); |
| 79 | Py_INCREF(newitem); |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | /* Bubble up the smaller child until hitting a leaf. */ |
| 82 | childpos = 2*pos + 1; /* leftmost child position */ |
| 83 | while (childpos < endpos) { |
| 84 | /* Set childpos to index of smaller child. */ |
| 85 | rightpos = childpos + 1; |
| 86 | if (rightpos < endpos) { |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 87 | cmp = PyObject_RichCompareBool( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | PyList_GET_ITEM(heap, childpos), |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 89 | PyList_GET_ITEM(heap, rightpos), |
| 90 | Py_LT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | if (cmp == -1) { |
| 92 | Py_DECREF(newitem); |
| 93 | return -1; |
| 94 | } |
| 95 | if (cmp == 0) |
| 96 | childpos = rightpos; |
| 97 | } |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 98 | if (size != PyList_GET_SIZE(heap)) { |
| 99 | Py_DECREF(newitem); |
| 100 | PyErr_SetString(PyExc_RuntimeError, |
| 101 | "list changed size during iteration"); |
| 102 | return -1; |
| 103 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | /* Move the smaller child up. */ |
| 105 | tmp = PyList_GET_ITEM(heap, childpos); |
| 106 | Py_INCREF(tmp); |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 107 | olditem = PyList_GET_ITEM(heap, pos); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | PyList_SET_ITEM(heap, pos, tmp); |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 109 | Py_DECREF(olditem); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | pos = childpos; |
| 111 | childpos = 2*pos + 1; |
Antoine Pitrou | 44d5214 | 2013-03-04 20:30:01 +0100 | [diff] [blame] | 112 | if (size != PyList_GET_SIZE(heap)) { |
| 113 | PyErr_SetString(PyExc_RuntimeError, |
| 114 | "list changed size during iteration"); |
| 115 | return -1; |
| 116 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 118 | |
Terry Jan Reedy | 0158af3 | 2013-03-11 17:42:46 -0400 | [diff] [blame] | 119 | /* The leaf at pos is empty now. Put newitem there, and bubble |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 120 | it up to its final resting place (by sifting its parents down). */ |
| 121 | Py_DECREF(PyList_GET_ITEM(heap, pos)); |
| 122 | PyList_SET_ITEM(heap, pos, newitem); |
| 123 | return _siftdown(heap, startpos, pos); |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static PyObject * |
| 127 | heappush(PyObject *self, PyObject *args) |
| 128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | PyObject *heap, *item; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | if (!PyArg_UnpackTuple(args, "heappush", 2, 2, &heap, &item)) |
| 132 | return NULL; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | if (!PyList_Check(heap)) { |
| 135 | PyErr_SetString(PyExc_TypeError, "heap argument must be a list"); |
| 136 | return NULL; |
| 137 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | if (PyList_Append(heap, item) == -1) |
| 140 | return NULL; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | if (_siftdown((PyListObject *)heap, 0, PyList_GET_SIZE(heap)-1) == -1) |
| 143 | return NULL; |
| 144 | Py_INCREF(Py_None); |
| 145 | return Py_None; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | PyDoc_STRVAR(heappush_doc, |
Raymond Hettinger | bd8f290 | 2013-01-18 17:35:25 -0800 | [diff] [blame] | 149 | "heappush(heap, item) -> None. Push item onto heap, maintaining the heap invariant."); |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 150 | |
| 151 | static PyObject * |
| 152 | heappop(PyObject *self, PyObject *heap) |
| 153 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | PyObject *lastelt, *returnitem; |
| 155 | Py_ssize_t n; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | if (!PyList_Check(heap)) { |
| 158 | PyErr_SetString(PyExc_TypeError, "heap argument must be a list"); |
| 159 | return NULL; |
| 160 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | /* # raises appropriate IndexError if heap is empty */ |
| 163 | n = PyList_GET_SIZE(heap); |
| 164 | if (n == 0) { |
| 165 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 166 | return NULL; |
| 167 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | lastelt = PyList_GET_ITEM(heap, n-1) ; |
| 170 | Py_INCREF(lastelt); |
Victor Stinner | 764a46d | 2013-07-17 21:50:21 +0200 | [diff] [blame] | 171 | if (PyList_SetSlice(heap, n-1, n, NULL) < 0) { |
| 172 | Py_DECREF(lastelt); |
| 173 | return NULL; |
| 174 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | n--; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | if (!n) |
| 178 | return lastelt; |
| 179 | returnitem = PyList_GET_ITEM(heap, 0); |
| 180 | PyList_SET_ITEM(heap, 0, lastelt); |
| 181 | if (_siftup((PyListObject *)heap, 0) == -1) { |
| 182 | Py_DECREF(returnitem); |
| 183 | return NULL; |
| 184 | } |
| 185 | return returnitem; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | PyDoc_STRVAR(heappop_doc, |
| 189 | "Pop the smallest item off the heap, maintaining the heap invariant."); |
| 190 | |
| 191 | static PyObject * |
| 192 | heapreplace(PyObject *self, PyObject *args) |
| 193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | PyObject *heap, *item, *returnitem; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | if (!PyArg_UnpackTuple(args, "heapreplace", 2, 2, &heap, &item)) |
| 197 | return NULL; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | if (!PyList_Check(heap)) { |
| 200 | PyErr_SetString(PyExc_TypeError, "heap argument must be a list"); |
| 201 | return NULL; |
| 202 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | if (PyList_GET_SIZE(heap) < 1) { |
| 205 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 206 | return NULL; |
| 207 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | returnitem = PyList_GET_ITEM(heap, 0); |
| 210 | Py_INCREF(item); |
| 211 | PyList_SET_ITEM(heap, 0, item); |
| 212 | if (_siftup((PyListObject *)heap, 0) == -1) { |
| 213 | Py_DECREF(returnitem); |
| 214 | return NULL; |
| 215 | } |
| 216 | return returnitem; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | PyDoc_STRVAR(heapreplace_doc, |
Raymond Hettinger | bd8f290 | 2013-01-18 17:35:25 -0800 | [diff] [blame] | 220 | "heapreplace(heap, item) -> value. Pop and return the current smallest value, and add the new item.\n\ |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 221 | \n\ |
| 222 | This is more efficient than heappop() followed by heappush(), and can be\n\ |
| 223 | more appropriate when using a fixed-size heap. Note that the value\n\ |
| 224 | returned may be larger than item! That constrains reasonable uses of\n\ |
Raymond Hettinger | 8158e84 | 2004-09-06 07:04:09 +0000 | [diff] [blame] | 225 | this routine unless written as part of a conditional replacement:\n\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | if item > heap[0]:\n\ |
| 227 | item = heapreplace(heap, item)\n"); |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 228 | |
| 229 | static PyObject * |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 230 | heappushpop(PyObject *self, PyObject *args) |
| 231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | PyObject *heap, *item, *returnitem; |
| 233 | int cmp; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | if (!PyArg_UnpackTuple(args, "heappushpop", 2, 2, &heap, &item)) |
| 236 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | if (!PyList_Check(heap)) { |
| 239 | PyErr_SetString(PyExc_TypeError, "heap argument must be a list"); |
| 240 | return NULL; |
| 241 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | if (PyList_GET_SIZE(heap) < 1) { |
| 244 | Py_INCREF(item); |
| 245 | return item; |
| 246 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 247 | |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 248 | cmp = PyObject_RichCompareBool(PyList_GET_ITEM(heap, 0), item, Py_LT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | if (cmp == -1) |
| 250 | return NULL; |
| 251 | if (cmp == 0) { |
| 252 | Py_INCREF(item); |
| 253 | return item; |
| 254 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | returnitem = PyList_GET_ITEM(heap, 0); |
| 257 | Py_INCREF(item); |
| 258 | PyList_SET_ITEM(heap, 0, item); |
| 259 | if (_siftup((PyListObject *)heap, 0) == -1) { |
| 260 | Py_DECREF(returnitem); |
| 261 | return NULL; |
| 262 | } |
| 263 | return returnitem; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | PyDoc_STRVAR(heappushpop_doc, |
Raymond Hettinger | bd8f290 | 2013-01-18 17:35:25 -0800 | [diff] [blame] | 267 | "heappushpop(heap, item) -> value. Push item on the heap, then pop and return the smallest item\n\ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 268 | from the heap. The combined action runs more efficiently than\n\ |
| 269 | heappush() followed by a separate call to heappop()."); |
| 270 | |
| 271 | static PyObject * |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 272 | heapify(PyObject *self, PyObject *heap) |
| 273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | Py_ssize_t i, n; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | if (!PyList_Check(heap)) { |
| 277 | PyErr_SetString(PyExc_TypeError, "heap argument must be a list"); |
| 278 | return NULL; |
| 279 | } |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | n = PyList_GET_SIZE(heap); |
| 282 | /* Transform bottom-up. The largest index there's any point to |
| 283 | looking at is the largest with a child index in-range, so must |
| 284 | have 2*i + 1 < n, or i < (n-1)/2. If n is even = 2*j, this is |
| 285 | (2*j-1)/2 = j-1/2 so j-1 is the largest, which is n//2 - 1. If |
| 286 | n is odd = 2*j+1, this is (2*j+1-1)/2 = j so j-1 is the largest, |
| 287 | and that's again n//2-1. |
| 288 | */ |
| 289 | for (i=n/2-1 ; i>=0 ; i--) |
| 290 | if(_siftup((PyListObject *)heap, i) == -1) |
| 291 | return NULL; |
| 292 | Py_INCREF(Py_None); |
| 293 | return Py_None; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | PyDoc_STRVAR(heapify_doc, |
| 297 | "Transform list into a heap, in-place, in O(len(heap)) time."); |
| 298 | |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 299 | static PyObject * |
| 300 | nlargest(PyObject *self, PyObject *args) |
| 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem; |
| 303 | Py_ssize_t i, n; |
| 304 | int cmp; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | if (!PyArg_ParseTuple(args, "nO:nlargest", &n, &iterable)) |
| 307 | return NULL; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | it = PyObject_GetIter(iterable); |
| 310 | if (it == NULL) |
| 311 | return NULL; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | heap = PyList_New(0); |
| 314 | if (heap == NULL) |
| 315 | goto fail; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | for (i=0 ; i<n ; i++ ){ |
| 318 | elem = PyIter_Next(it); |
| 319 | if (elem == NULL) { |
| 320 | if (PyErr_Occurred()) |
| 321 | goto fail; |
| 322 | else |
| 323 | goto sortit; |
| 324 | } |
| 325 | if (PyList_Append(heap, elem) == -1) { |
| 326 | Py_DECREF(elem); |
| 327 | goto fail; |
| 328 | } |
| 329 | Py_DECREF(elem); |
| 330 | } |
| 331 | if (PyList_GET_SIZE(heap) == 0) |
| 332 | goto sortit; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | for (i=n/2-1 ; i>=0 ; i--) |
| 335 | if(_siftup((PyListObject *)heap, i) == -1) |
| 336 | goto fail; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | sol = PyList_GET_ITEM(heap, 0); |
| 339 | while (1) { |
| 340 | elem = PyIter_Next(it); |
| 341 | if (elem == NULL) { |
| 342 | if (PyErr_Occurred()) |
| 343 | goto fail; |
| 344 | else |
| 345 | goto sortit; |
| 346 | } |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 347 | cmp = PyObject_RichCompareBool(sol, elem, Py_LT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | if (cmp == -1) { |
| 349 | Py_DECREF(elem); |
| 350 | goto fail; |
| 351 | } |
| 352 | if (cmp == 0) { |
| 353 | Py_DECREF(elem); |
| 354 | continue; |
| 355 | } |
| 356 | oldelem = PyList_GET_ITEM(heap, 0); |
| 357 | PyList_SET_ITEM(heap, 0, elem); |
| 358 | Py_DECREF(oldelem); |
| 359 | if (_siftup((PyListObject *)heap, 0) == -1) |
| 360 | goto fail; |
| 361 | sol = PyList_GET_ITEM(heap, 0); |
| 362 | } |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 363 | sortit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | if (PyList_Sort(heap) == -1) |
| 365 | goto fail; |
| 366 | if (PyList_Reverse(heap) == -1) |
| 367 | goto fail; |
| 368 | Py_DECREF(it); |
| 369 | return heap; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 370 | |
| 371 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | Py_DECREF(it); |
| 373 | Py_XDECREF(heap); |
| 374 | return NULL; |
Raymond Hettinger | c929766 | 2004-06-12 22:48:46 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | PyDoc_STRVAR(nlargest_doc, |
| 378 | "Find the n largest elements in a dataset.\n\ |
| 379 | \n\ |
| 380 | Equivalent to: sorted(iterable, reverse=True)[:n]\n"); |
| 381 | |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 382 | static int |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 383 | _siftdownmax(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos) |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | PyObject *newitem, *parent; |
| 386 | int cmp; |
| 387 | Py_ssize_t parentpos; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | assert(PyList_Check(heap)); |
| 390 | if (pos >= PyList_GET_SIZE(heap)) { |
| 391 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 392 | return -1; |
| 393 | } |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | newitem = PyList_GET_ITEM(heap, pos); |
| 396 | Py_INCREF(newitem); |
| 397 | /* Follow the path to the root, moving parents down until finding |
| 398 | a place newitem fits. */ |
| 399 | while (pos > startpos){ |
| 400 | parentpos = (pos - 1) >> 1; |
| 401 | parent = PyList_GET_ITEM(heap, parentpos); |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 402 | cmp = PyObject_RichCompareBool(parent, newitem, Py_LT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | if (cmp == -1) { |
| 404 | Py_DECREF(newitem); |
| 405 | return -1; |
| 406 | } |
| 407 | if (cmp == 0) |
| 408 | break; |
| 409 | Py_INCREF(parent); |
| 410 | Py_DECREF(PyList_GET_ITEM(heap, pos)); |
| 411 | PyList_SET_ITEM(heap, pos, parent); |
| 412 | pos = parentpos; |
| 413 | } |
| 414 | Py_DECREF(PyList_GET_ITEM(heap, pos)); |
| 415 | PyList_SET_ITEM(heap, pos, newitem); |
| 416 | return 0; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static int |
Martin v. Löwis | ad0a462 | 2006-02-16 14:30:23 +0000 | [diff] [blame] | 420 | _siftupmax(PyListObject *heap, Py_ssize_t pos) |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 421 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | Py_ssize_t startpos, endpos, childpos, rightpos; |
| 423 | int cmp; |
| 424 | PyObject *newitem, *tmp; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | assert(PyList_Check(heap)); |
| 427 | endpos = PyList_GET_SIZE(heap); |
| 428 | startpos = pos; |
| 429 | if (pos >= endpos) { |
| 430 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 431 | return -1; |
| 432 | } |
| 433 | newitem = PyList_GET_ITEM(heap, pos); |
| 434 | Py_INCREF(newitem); |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | /* Bubble up the smaller child until hitting a leaf. */ |
| 437 | childpos = 2*pos + 1; /* leftmost child position */ |
| 438 | while (childpos < endpos) { |
| 439 | /* Set childpos to index of smaller child. */ |
| 440 | rightpos = childpos + 1; |
| 441 | if (rightpos < endpos) { |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 442 | cmp = PyObject_RichCompareBool( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | PyList_GET_ITEM(heap, rightpos), |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 444 | PyList_GET_ITEM(heap, childpos), |
| 445 | Py_LT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | if (cmp == -1) { |
| 447 | Py_DECREF(newitem); |
| 448 | return -1; |
| 449 | } |
| 450 | if (cmp == 0) |
| 451 | childpos = rightpos; |
| 452 | } |
| 453 | /* Move the smaller child up. */ |
| 454 | tmp = PyList_GET_ITEM(heap, childpos); |
| 455 | Py_INCREF(tmp); |
| 456 | Py_DECREF(PyList_GET_ITEM(heap, pos)); |
| 457 | PyList_SET_ITEM(heap, pos, tmp); |
| 458 | pos = childpos; |
| 459 | childpos = 2*pos + 1; |
| 460 | } |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 461 | |
Terry Jan Reedy | 0158af3 | 2013-03-11 17:42:46 -0400 | [diff] [blame] | 462 | /* The leaf at pos is empty now. Put newitem there, and bubble |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | it up to its final resting place (by sifting its parents down). */ |
| 464 | Py_DECREF(PyList_GET_ITEM(heap, pos)); |
| 465 | PyList_SET_ITEM(heap, pos, newitem); |
| 466 | return _siftdownmax(heap, startpos, pos); |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static PyObject * |
| 470 | nsmallest(PyObject *self, PyObject *args) |
| 471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem; |
| 473 | Py_ssize_t i, n; |
| 474 | int cmp; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | if (!PyArg_ParseTuple(args, "nO:nsmallest", &n, &iterable)) |
| 477 | return NULL; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | it = PyObject_GetIter(iterable); |
| 480 | if (it == NULL) |
| 481 | return NULL; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | heap = PyList_New(0); |
| 484 | if (heap == NULL) |
| 485 | goto fail; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | for (i=0 ; i<n ; i++ ){ |
| 488 | elem = PyIter_Next(it); |
| 489 | if (elem == NULL) { |
| 490 | if (PyErr_Occurred()) |
| 491 | goto fail; |
| 492 | else |
| 493 | goto sortit; |
| 494 | } |
| 495 | if (PyList_Append(heap, elem) == -1) { |
| 496 | Py_DECREF(elem); |
| 497 | goto fail; |
| 498 | } |
| 499 | Py_DECREF(elem); |
| 500 | } |
| 501 | n = PyList_GET_SIZE(heap); |
| 502 | if (n == 0) |
| 503 | goto sortit; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | for (i=n/2-1 ; i>=0 ; i--) |
| 506 | if(_siftupmax((PyListObject *)heap, i) == -1) |
| 507 | goto fail; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | los = PyList_GET_ITEM(heap, 0); |
| 510 | while (1) { |
| 511 | elem = PyIter_Next(it); |
| 512 | if (elem == NULL) { |
| 513 | if (PyErr_Occurred()) |
| 514 | goto fail; |
| 515 | else |
| 516 | goto sortit; |
| 517 | } |
Raymond Hettinger | db6b62e | 2010-09-05 05:26:10 +0000 | [diff] [blame] | 518 | cmp = PyObject_RichCompareBool(elem, los, Py_LT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | if (cmp == -1) { |
| 520 | Py_DECREF(elem); |
| 521 | goto fail; |
| 522 | } |
| 523 | if (cmp == 0) { |
| 524 | Py_DECREF(elem); |
| 525 | continue; |
| 526 | } |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | oldelem = PyList_GET_ITEM(heap, 0); |
| 529 | PyList_SET_ITEM(heap, 0, elem); |
| 530 | Py_DECREF(oldelem); |
| 531 | if (_siftupmax((PyListObject *)heap, 0) == -1) |
| 532 | goto fail; |
| 533 | los = PyList_GET_ITEM(heap, 0); |
| 534 | } |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 535 | |
| 536 | sortit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | if (PyList_Sort(heap) == -1) |
| 538 | goto fail; |
| 539 | Py_DECREF(it); |
| 540 | return heap; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 541 | |
| 542 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | Py_DECREF(it); |
| 544 | Py_XDECREF(heap); |
| 545 | return NULL; |
Raymond Hettinger | 2e3dfaf | 2004-06-13 05:26:33 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | PyDoc_STRVAR(nsmallest_doc, |
| 549 | "Find the n smallest elements in a dataset.\n\ |
| 550 | \n\ |
| 551 | Equivalent to: sorted(iterable)[:n]\n"); |
| 552 | |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 553 | static PyMethodDef heapq_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | {"heappush", (PyCFunction)heappush, |
| 555 | METH_VARARGS, heappush_doc}, |
| 556 | {"heappushpop", (PyCFunction)heappushpop, |
| 557 | METH_VARARGS, heappushpop_doc}, |
| 558 | {"heappop", (PyCFunction)heappop, |
| 559 | METH_O, heappop_doc}, |
| 560 | {"heapreplace", (PyCFunction)heapreplace, |
| 561 | METH_VARARGS, heapreplace_doc}, |
| 562 | {"heapify", (PyCFunction)heapify, |
| 563 | METH_O, heapify_doc}, |
| 564 | {"nlargest", (PyCFunction)nlargest, |
| 565 | METH_VARARGS, nlargest_doc}, |
| 566 | {"nsmallest", (PyCFunction)nsmallest, |
| 567 | METH_VARARGS, nsmallest_doc}, |
| 568 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
| 571 | PyDoc_STRVAR(module_doc, |
| 572 | "Heap queue algorithm (a.k.a. priority queue).\n\ |
| 573 | \n\ |
| 574 | Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for\n\ |
| 575 | all k, counting elements from 0. For the sake of comparison,\n\ |
| 576 | non-existing elements are considered to be infinite. The interesting\n\ |
| 577 | property of a heap is that a[0] is always its smallest element.\n\ |
| 578 | \n\ |
| 579 | Usage:\n\ |
| 580 | \n\ |
| 581 | heap = [] # creates an empty heap\n\ |
| 582 | heappush(heap, item) # pushes a new item on the heap\n\ |
| 583 | item = heappop(heap) # pops the smallest item from the heap\n\ |
| 584 | item = heap[0] # smallest item on the heap without popping it\n\ |
| 585 | heapify(x) # transforms list into a heap, in-place, in linear time\n\ |
| 586 | item = heapreplace(heap, item) # pops and returns smallest item, and adds\n\ |
| 587 | # new item; the heap size is unchanged\n\ |
| 588 | \n\ |
| 589 | Our API differs from textbook heap algorithms as follows:\n\ |
| 590 | \n\ |
| 591 | - We use 0-based indexing. This makes the relationship between the\n\ |
| 592 | index for a node and the indexes for its children slightly less\n\ |
| 593 | obvious, but is more suitable since Python uses 0-based indexing.\n\ |
| 594 | \n\ |
| 595 | - Our heappop() method returns the smallest item, not the largest.\n\ |
| 596 | \n\ |
| 597 | These two make it possible to view the heap as a regular Python list\n\ |
| 598 | without surprises: heap[0] is the smallest item, and heap.sort()\n\ |
| 599 | maintains the heap invariant!\n"); |
| 600 | |
| 601 | |
| 602 | PyDoc_STRVAR(__about__, |
| 603 | "Heap queues\n\ |
| 604 | \n\ |
Neal Norwitz | c1786ea | 2007-08-23 23:58:43 +0000 | [diff] [blame] | 605 | [explanation by Fran\xc3\xa7ois Pinard]\n\ |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 606 | \n\ |
| 607 | Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for\n\ |
| 608 | all k, counting elements from 0. For the sake of comparison,\n\ |
| 609 | non-existing elements are considered to be infinite. The interesting\n\ |
| 610 | property of a heap is that a[0] is always its smallest element.\n" |
| 611 | "\n\ |
| 612 | The strange invariant above is meant to be an efficient memory\n\ |
| 613 | representation for a tournament. The numbers below are `k', not a[k]:\n\ |
| 614 | \n\ |
| 615 | 0\n\ |
| 616 | \n\ |
| 617 | 1 2\n\ |
| 618 | \n\ |
| 619 | 3 4 5 6\n\ |
| 620 | \n\ |
| 621 | 7 8 9 10 11 12 13 14\n\ |
| 622 | \n\ |
| 623 | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30\n\ |
| 624 | \n\ |
| 625 | \n\ |
| 626 | In the tree above, each cell `k' is topping `2*k+1' and `2*k+2'. In\n\ |
| 627 | an usual binary tournament we see in sports, each cell is the winner\n\ |
| 628 | over the two cells it tops, and we can trace the winner down the tree\n\ |
| 629 | to see all opponents s/he had. However, in many computer applications\n\ |
| 630 | of such tournaments, we do not need to trace the history of a winner.\n\ |
| 631 | To be more memory efficient, when a winner is promoted, we try to\n\ |
| 632 | replace it by something else at a lower level, and the rule becomes\n\ |
| 633 | that a cell and the two cells it tops contain three different items,\n\ |
| 634 | but the top cell \"wins\" over the two topped cells.\n" |
| 635 | "\n\ |
| 636 | If this heap invariant is protected at all time, index 0 is clearly\n\ |
| 637 | the overall winner. The simplest algorithmic way to remove it and\n\ |
| 638 | find the \"next\" winner is to move some loser (let's say cell 30 in the\n\ |
| 639 | diagram above) into the 0 position, and then percolate this new 0 down\n\ |
| 640 | the tree, exchanging values, until the invariant is re-established.\n\ |
| 641 | This is clearly logarithmic on the total number of items in the tree.\n\ |
| 642 | By iterating over all items, you get an O(n ln n) sort.\n" |
| 643 | "\n\ |
| 644 | A nice feature of this sort is that you can efficiently insert new\n\ |
| 645 | items while the sort is going on, provided that the inserted items are\n\ |
| 646 | not \"better\" than the last 0'th element you extracted. This is\n\ |
| 647 | especially useful in simulation contexts, where the tree holds all\n\ |
| 648 | incoming events, and the \"win\" condition means the smallest scheduled\n\ |
| 649 | time. When an event schedule other events for execution, they are\n\ |
| 650 | scheduled into the future, so they can easily go into the heap. So, a\n\ |
| 651 | heap is a good structure for implementing schedulers (this is what I\n\ |
| 652 | used for my MIDI sequencer :-).\n" |
| 653 | "\n\ |
| 654 | Various structures for implementing schedulers have been extensively\n\ |
| 655 | studied, and heaps are good for this, as they are reasonably speedy,\n\ |
| 656 | the speed is almost constant, and the worst case is not much different\n\ |
| 657 | than the average case. However, there are other representations which\n\ |
| 658 | are more efficient overall, yet the worst cases might be terrible.\n" |
| 659 | "\n\ |
| 660 | Heaps are also very useful in big disk sorts. You most probably all\n\ |
| 661 | know that a big sort implies producing \"runs\" (which are pre-sorted\n\ |
| 662 | sequences, which size is usually related to the amount of CPU memory),\n\ |
| 663 | followed by a merging passes for these runs, which merging is often\n\ |
| 664 | very cleverly organised[1]. It is very important that the initial\n\ |
| 665 | sort produces the longest runs possible. Tournaments are a good way\n\ |
| 666 | to that. If, using all the memory available to hold a tournament, you\n\ |
| 667 | replace and percolate items that happen to fit the current run, you'll\n\ |
| 668 | produce runs which are twice the size of the memory for random input,\n\ |
| 669 | and much better for input fuzzily ordered.\n" |
| 670 | "\n\ |
| 671 | Moreover, if you output the 0'th item on disk and get an input which\n\ |
| 672 | may not fit in the current tournament (because the value \"wins\" over\n\ |
| 673 | the last output value), it cannot fit in the heap, so the size of the\n\ |
| 674 | heap decreases. The freed memory could be cleverly reused immediately\n\ |
| 675 | for progressively building a second heap, which grows at exactly the\n\ |
| 676 | same rate the first heap is melting. When the first heap completely\n\ |
| 677 | vanishes, you switch heaps and start a new run. Clever and quite\n\ |
| 678 | effective!\n\ |
| 679 | \n\ |
| 680 | In a word, heaps are useful memory structures to know. I use them in\n\ |
| 681 | a few applications, and I think it is good to keep a `heap' module\n\ |
| 682 | around. :-)\n" |
| 683 | "\n\ |
| 684 | --------------------\n\ |
| 685 | [1] The disk balancing algorithms which are current, nowadays, are\n\ |
| 686 | more annoying than clever, and this is a consequence of the seeking\n\ |
| 687 | capabilities of the disks. On devices which cannot seek, like big\n\ |
| 688 | tape drives, the story was quite different, and one had to be very\n\ |
| 689 | clever to ensure (far in advance) that each tape movement will be the\n\ |
| 690 | most effective possible (that is, will best participate at\n\ |
| 691 | \"progressing\" the merge). Some tapes were even able to read\n\ |
| 692 | backwards, and this was also used to avoid the rewinding time.\n\ |
| 693 | Believe me, real good tape sorts were quite spectacular to watch!\n\ |
| 694 | From all times, sorting has always been a Great Art! :-)\n"); |
| 695 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 696 | |
| 697 | static struct PyModuleDef _heapqmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | PyModuleDef_HEAD_INIT, |
| 699 | "_heapq", |
| 700 | module_doc, |
| 701 | -1, |
| 702 | heapq_methods, |
| 703 | NULL, |
| 704 | NULL, |
| 705 | NULL, |
| 706 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 707 | }; |
| 708 | |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 709 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 710 | PyInit__heapq(void) |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | PyObject *m, *about; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | m = PyModule_Create(&_heapqmodule); |
| 715 | if (m == NULL) |
| 716 | return NULL; |
| 717 | about = PyUnicode_DecodeUTF8(__about__, strlen(__about__), NULL); |
| 718 | PyModule_AddObject(m, "__about__", about); |
| 719 | return m; |
Raymond Hettinger | c46cb2a | 2004-04-19 19:06:21 +0000 | [diff] [blame] | 720 | } |
| 721 | |