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