| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() |
| 6 |
#include "pycore_modsupport.h" // _PyArg_CheckPositional() |
| 7 |
|
| 8 |
PyDoc_STRVAR(_heapq_heappush__doc__, |
| 9 |
"heappush($module, heap, item, /)\n" |
| 10 |
"--\n" |
| 11 |
"\n" |
| 12 |
"Push item onto heap, maintaining the heap invariant."); |
| 13 |
|
| 14 |
#define _HEAPQ_HEAPPUSH_METHODDEF \ |
| 15 |
{"heappush", _PyCFunction_CAST(_heapq_heappush), METH_FASTCALL, _heapq_heappush__doc__}, |
| 16 |
|
| 17 |
static PyObject * |
| 18 |
_heapq_heappush_impl(PyObject *module, PyObject *heap, PyObject *item); |
| 19 |
|
| 20 |
static PyObject * |
| 21 |
_heapq_heappush(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 22 |
{ |
| 23 |
PyObject *return_value = NULL; |
| 24 |
PyObject *heap; |
| 25 |
PyObject *item; |
| 26 |
|
| 27 |
if (!_PyArg_CheckPositional("heappush", nargs, 2, 2)) { |
| 28 |
goto exit; |
| 29 |
} |
| 30 |
if (!PyList_Check(args[0])) { |
| 31 |
_PyArg_BadArgument("heappush", "argument 1", "list", args[0]); |
| 32 |
goto exit; |
| 33 |
} |
| 34 |
heap = args[0]; |
| 35 |
item = args[1]; |
| 36 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 37 |
return_value = _heapq_heappush_impl(module, heap, item); |
| 38 |
Py_END_CRITICAL_SECTION(); |
| 39 |
|
| 40 |
exit: |
| 41 |
return return_value; |
| 42 |
} |
| 43 |
|
| 44 |
PyDoc_STRVAR(_heapq_heappop__doc__, |
| 45 |
"heappop($module, heap, /)\n" |
| 46 |
"--\n" |
| 47 |
"\n" |
| 48 |
"Pop the smallest item off the heap, maintaining the heap invariant."); |
| 49 |
|
| 50 |
#define _HEAPQ_HEAPPOP_METHODDEF \ |
| 51 |
{"heappop", (PyCFunction)_heapq_heappop, METH_O, _heapq_heappop__doc__}, |
| 52 |
|
| 53 |
static PyObject * |
| 54 |
_heapq_heappop_impl(PyObject *module, PyObject *heap); |
| 55 |
|
| 56 |
static PyObject * |
| 57 |
_heapq_heappop(PyObject *module, PyObject *arg) |
| 58 |
{ |
| 59 |
PyObject *return_value = NULL; |
| 60 |
PyObject *heap; |
| 61 |
|
| 62 |
if (!PyList_Check(arg)) { |
| 63 |
_PyArg_BadArgument("heappop", "argument", "list", arg); |
| 64 |
goto exit; |
| 65 |
} |
| 66 |
heap = arg; |
| 67 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 68 |
return_value = _heapq_heappop_impl(module, heap); |
| 69 |
Py_END_CRITICAL_SECTION(); |
| 70 |
|
| 71 |
exit: |
| 72 |
return return_value; |
| 73 |
} |
| 74 |
|
| 75 |
PyDoc_STRVAR(_heapq_heapreplace__doc__, |
| 76 |
"heapreplace($module, heap, item, /)\n" |
| 77 |
"--\n" |
| 78 |
"\n" |
| 79 |
"Pop and return the current smallest value, and add the new item.\n" |
| 80 |
"\n" |
| 81 |
"This is more efficient than heappop() followed by heappush(), and can be\n" |
| 82 |
"more appropriate when using a fixed-size heap. Note that the value\n" |
| 83 |
"returned may be larger than item! That constrains reasonable uses of\n" |
| 84 |
"this routine unless written as part of a conditional replacement:\n" |
| 85 |
"\n" |
| 86 |
" if item > heap[0]:\n" |
| 87 |
" item = heapreplace(heap, item)"); |
| 88 |
|
| 89 |
#define _HEAPQ_HEAPREPLACE_METHODDEF \ |
| 90 |
{"heapreplace", _PyCFunction_CAST(_heapq_heapreplace), METH_FASTCALL, _heapq_heapreplace__doc__}, |
| 91 |
|
| 92 |
static PyObject * |
| 93 |
_heapq_heapreplace_impl(PyObject *module, PyObject *heap, PyObject *item); |
| 94 |
|
| 95 |
static PyObject * |
| 96 |
_heapq_heapreplace(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 97 |
{ |
| 98 |
PyObject *return_value = NULL; |
| 99 |
PyObject *heap; |
| 100 |
PyObject *item; |
| 101 |
|
| 102 |
if (!_PyArg_CheckPositional("heapreplace", nargs, 2, 2)) { |
| 103 |
goto exit; |
| 104 |
} |
| 105 |
if (!PyList_Check(args[0])) { |
| 106 |
_PyArg_BadArgument("heapreplace", "argument 1", "list", args[0]); |
| 107 |
goto exit; |
| 108 |
} |
| 109 |
heap = args[0]; |
| 110 |
item = args[1]; |
| 111 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 112 |
return_value = _heapq_heapreplace_impl(module, heap, item); |
| 113 |
Py_END_CRITICAL_SECTION(); |
| 114 |
|
| 115 |
exit: |
| 116 |
return return_value; |
| 117 |
} |
| 118 |
|
| 119 |
PyDoc_STRVAR(_heapq_heappushpop__doc__, |
| 120 |
"heappushpop($module, heap, item, /)\n" |
| 121 |
"--\n" |
| 122 |
"\n" |
| 123 |
"Push item on the heap, then pop and return the smallest item from the heap.\n" |
| 124 |
"\n" |
| 125 |
"The combined action runs more efficiently than heappush() followed by\n" |
| 126 |
"a separate call to heappop()."); |
| 127 |
|
| 128 |
#define _HEAPQ_HEAPPUSHPOP_METHODDEF \ |
| 129 |
{"heappushpop", _PyCFunction_CAST(_heapq_heappushpop), METH_FASTCALL, _heapq_heappushpop__doc__}, |
| 130 |
|
| 131 |
static PyObject * |
| 132 |
_heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item); |
| 133 |
|
| 134 |
static PyObject * |
| 135 |
_heapq_heappushpop(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 136 |
{ |
| 137 |
PyObject *return_value = NULL; |
| 138 |
PyObject *heap; |
| 139 |
PyObject *item; |
| 140 |
|
| 141 |
if (!_PyArg_CheckPositional("heappushpop", nargs, 2, 2)) { |
| 142 |
goto exit; |
| 143 |
} |
| 144 |
if (!PyList_Check(args[0])) { |
| 145 |
_PyArg_BadArgument("heappushpop", "argument 1", "list", args[0]); |
| 146 |
goto exit; |
| 147 |
} |
| 148 |
heap = args[0]; |
| 149 |
item = args[1]; |
| 150 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 151 |
return_value = _heapq_heappushpop_impl(module, heap, item); |
| 152 |
Py_END_CRITICAL_SECTION(); |
| 153 |
|
| 154 |
exit: |
| 155 |
return return_value; |
| 156 |
} |
| 157 |
|
| 158 |
PyDoc_STRVAR(_heapq_heapify__doc__, |
| 159 |
"heapify($module, heap, /)\n" |
| 160 |
"--\n" |
| 161 |
"\n" |
| 162 |
"Transform list into a heap, in-place, in O(len(heap)) time."); |
| 163 |
|
| 164 |
#define _HEAPQ_HEAPIFY_METHODDEF \ |
| 165 |
{"heapify", (PyCFunction)_heapq_heapify, METH_O, _heapq_heapify__doc__}, |
| 166 |
|
| 167 |
static PyObject * |
| 168 |
_heapq_heapify_impl(PyObject *module, PyObject *heap); |
| 169 |
|
| 170 |
static PyObject * |
| 171 |
_heapq_heapify(PyObject *module, PyObject *arg) |
| 172 |
{ |
| 173 |
PyObject *return_value = NULL; |
| 174 |
PyObject *heap; |
| 175 |
|
| 176 |
if (!PyList_Check(arg)) { |
| 177 |
_PyArg_BadArgument("heapify", "argument", "list", arg); |
| 178 |
goto exit; |
| 179 |
} |
| 180 |
heap = arg; |
| 181 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 182 |
return_value = _heapq_heapify_impl(module, heap); |
| 183 |
Py_END_CRITICAL_SECTION(); |
| 184 |
|
| 185 |
exit: |
| 186 |
return return_value; |
| 187 |
} |
| 188 |
|
| 189 |
PyDoc_STRVAR(_heapq_heappush_max__doc__, |
| 190 |
"heappush_max($module, heap, item, /)\n" |
| 191 |
"--\n" |
| 192 |
"\n" |
| 193 |
"Push item onto max heap, maintaining the heap invariant."); |
| 194 |
|
| 195 |
#define _HEAPQ_HEAPPUSH_MAX_METHODDEF \ |
| 196 |
{"heappush_max", _PyCFunction_CAST(_heapq_heappush_max), METH_FASTCALL, _heapq_heappush_max__doc__}, |
| 197 |
|
| 198 |
static PyObject * |
| 199 |
_heapq_heappush_max_impl(PyObject *module, PyObject *heap, PyObject *item); |
| 200 |
|
| 201 |
static PyObject * |
| 202 |
_heapq_heappush_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 203 |
{ |
| 204 |
PyObject *return_value = NULL; |
| 205 |
PyObject *heap; |
| 206 |
PyObject *item; |
| 207 |
|
| 208 |
if (!_PyArg_CheckPositional("heappush_max", nargs, 2, 2)) { |
| 209 |
goto exit; |
| 210 |
} |
| 211 |
if (!PyList_Check(args[0])) { |
| 212 |
_PyArg_BadArgument("heappush_max", "argument 1", "list", args[0]); |
| 213 |
goto exit; |
| 214 |
} |
| 215 |
heap = args[0]; |
| 216 |
item = args[1]; |
| 217 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 218 |
return_value = _heapq_heappush_max_impl(module, heap, item); |
| 219 |
Py_END_CRITICAL_SECTION(); |
| 220 |
|
| 221 |
exit: |
| 222 |
return return_value; |
| 223 |
} |
| 224 |
|
| 225 |
PyDoc_STRVAR(_heapq_heappop_max__doc__, |
| 226 |
"heappop_max($module, heap, /)\n" |
| 227 |
"--\n" |
| 228 |
"\n" |
| 229 |
"Maxheap variant of heappop."); |
| 230 |
|
| 231 |
#define _HEAPQ_HEAPPOP_MAX_METHODDEF \ |
| 232 |
{"heappop_max", (PyCFunction)_heapq_heappop_max, METH_O, _heapq_heappop_max__doc__}, |
| 233 |
|
| 234 |
static PyObject * |
| 235 |
_heapq_heappop_max_impl(PyObject *module, PyObject *heap); |
| 236 |
|
| 237 |
static PyObject * |
| 238 |
_heapq_heappop_max(PyObject *module, PyObject *arg) |
| 239 |
{ |
| 240 |
PyObject *return_value = NULL; |
| 241 |
PyObject *heap; |
| 242 |
|
| 243 |
if (!PyList_Check(arg)) { |
| 244 |
_PyArg_BadArgument("heappop_max", "argument", "list", arg); |
| 245 |
goto exit; |
| 246 |
} |
| 247 |
heap = arg; |
| 248 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 249 |
return_value = _heapq_heappop_max_impl(module, heap); |
| 250 |
Py_END_CRITICAL_SECTION(); |
| 251 |
|
| 252 |
exit: |
| 253 |
return return_value; |
| 254 |
} |
| 255 |
|
| 256 |
PyDoc_STRVAR(_heapq_heapreplace_max__doc__, |
| 257 |
"heapreplace_max($module, heap, item, /)\n" |
| 258 |
"--\n" |
| 259 |
"\n" |
| 260 |
"Maxheap variant of heapreplace."); |
| 261 |
|
| 262 |
#define _HEAPQ_HEAPREPLACE_MAX_METHODDEF \ |
| 263 |
{"heapreplace_max", _PyCFunction_CAST(_heapq_heapreplace_max), METH_FASTCALL, _heapq_heapreplace_max__doc__}, |
| 264 |
|
| 265 |
static PyObject * |
| 266 |
_heapq_heapreplace_max_impl(PyObject *module, PyObject *heap, PyObject *item); |
| 267 |
|
| 268 |
static PyObject * |
| 269 |
_heapq_heapreplace_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 270 |
{ |
| 271 |
PyObject *return_value = NULL; |
| 272 |
PyObject *heap; |
| 273 |
PyObject *item; |
| 274 |
|
| 275 |
if (!_PyArg_CheckPositional("heapreplace_max", nargs, 2, 2)) { |
| 276 |
goto exit; |
| 277 |
} |
| 278 |
if (!PyList_Check(args[0])) { |
| 279 |
_PyArg_BadArgument("heapreplace_max", "argument 1", "list", args[0]); |
| 280 |
goto exit; |
| 281 |
} |
| 282 |
heap = args[0]; |
| 283 |
item = args[1]; |
| 284 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 285 |
return_value = _heapq_heapreplace_max_impl(module, heap, item); |
| 286 |
Py_END_CRITICAL_SECTION(); |
| 287 |
|
| 288 |
exit: |
| 289 |
return return_value; |
| 290 |
} |
| 291 |
|
| 292 |
PyDoc_STRVAR(_heapq_heapify_max__doc__, |
| 293 |
"heapify_max($module, heap, /)\n" |
| 294 |
"--\n" |
| 295 |
"\n" |
| 296 |
"Maxheap variant of heapify."); |
| 297 |
|
| 298 |
#define _HEAPQ_HEAPIFY_MAX_METHODDEF \ |
| 299 |
{"heapify_max", (PyCFunction)_heapq_heapify_max, METH_O, _heapq_heapify_max__doc__}, |
| 300 |
|
| 301 |
static PyObject * |
| 302 |
_heapq_heapify_max_impl(PyObject *module, PyObject *heap); |
| 303 |
|
| 304 |
static PyObject * |
| 305 |
_heapq_heapify_max(PyObject *module, PyObject *arg) |
| 306 |
{ |
| 307 |
PyObject *return_value = NULL; |
| 308 |
PyObject *heap; |
| 309 |
|
| 310 |
if (!PyList_Check(arg)) { |
| 311 |
_PyArg_BadArgument("heapify_max", "argument", "list", arg); |
| 312 |
goto exit; |
| 313 |
} |
| 314 |
heap = arg; |
| 315 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 316 |
return_value = _heapq_heapify_max_impl(module, heap); |
| 317 |
Py_END_CRITICAL_SECTION(); |
| 318 |
|
| 319 |
exit: |
| 320 |
return return_value; |
| 321 |
} |
| 322 |
|
| 323 |
PyDoc_STRVAR(_heapq_heappushpop_max__doc__, |
| 324 |
"heappushpop_max($module, heap, item, /)\n" |
| 325 |
"--\n" |
| 326 |
"\n" |
| 327 |
"Maxheap variant of heappushpop.\n" |
| 328 |
"\n" |
| 329 |
"The combined action runs more efficiently than heappush_max() followed by\n" |
| 330 |
"a separate call to heappop_max()."); |
| 331 |
|
| 332 |
#define _HEAPQ_HEAPPUSHPOP_MAX_METHODDEF \ |
| 333 |
{"heappushpop_max", _PyCFunction_CAST(_heapq_heappushpop_max), METH_FASTCALL, _heapq_heappushpop_max__doc__}, |
| 334 |
|
| 335 |
static PyObject * |
| 336 |
_heapq_heappushpop_max_impl(PyObject *module, PyObject *heap, PyObject *item); |
| 337 |
|
| 338 |
static PyObject * |
| 339 |
_heapq_heappushpop_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 340 |
{ |
| 341 |
PyObject *return_value = NULL; |
| 342 |
PyObject *heap; |
| 343 |
PyObject *item; |
| 344 |
|
| 345 |
if (!_PyArg_CheckPositional("heappushpop_max", nargs, 2, 2)) { |
| 346 |
goto exit; |
| 347 |
} |
| 348 |
if (!PyList_Check(args[0])) { |
| 349 |
_PyArg_BadArgument("heappushpop_max", "argument 1", "list", args[0]); |
| 350 |
goto exit; |
| 351 |
} |
| 352 |
heap = args[0]; |
| 353 |
item = args[1]; |
| 354 |
Py_BEGIN_CRITICAL_SECTION(heap); |
| 355 |
return_value = _heapq_heappushpop_max_impl(module, heap, item); |
| 356 |
Py_END_CRITICAL_SECTION(); |
| 357 |
|
| 358 |
exit: |
| 359 |
return return_value; |
| 360 |
} |
| 361 |
/*[clinic end generated code: output=e83d50002c29a96d input=a9049054013a1b77]*/ |