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