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