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