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