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