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