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