_heapqmodule.c.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  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. #define _HEAPQ_HEAPPUSH_METHODDEF \
  14. {"heappush", _PyCFunction_CAST(_heapq_heappush), METH_FASTCALL, _heapq_heappush__doc__},
  15. static PyObject *
  16. _heapq_heappush_impl(PyObject *module, PyObject *heap, PyObject *item);
  17. static PyObject *
  18. _heapq_heappush(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  19. {
  20. PyObject *return_value = NULL;
  21. PyObject *heap;
  22. PyObject *item;
  23. if (!_PyArg_CheckPositional("heappush", nargs, 2, 2)) {
  24. goto exit;
  25. }
  26. if (!PyList_Check(args[0])) {
  27. _PyArg_BadArgument("heappush", "argument 1", "list", args[0]);
  28. goto exit;
  29. }
  30. heap = args[0];
  31. item = args[1];
  32. return_value = _heapq_heappush_impl(module, heap, item);
  33. exit:
  34. return return_value;
  35. }
  36. PyDoc_STRVAR(_heapq_heappop__doc__,
  37. "heappop($module, heap, /)\n"
  38. "--\n"
  39. "\n"
  40. "Pop the smallest item off the heap, maintaining the heap invariant.");
  41. #define _HEAPQ_HEAPPOP_METHODDEF \
  42. {"heappop", (PyCFunction)_heapq_heappop, METH_O, _heapq_heappop__doc__},
  43. static PyObject *
  44. _heapq_heappop_impl(PyObject *module, PyObject *heap);
  45. static PyObject *
  46. _heapq_heappop(PyObject *module, PyObject *arg)
  47. {
  48. PyObject *return_value = NULL;
  49. PyObject *heap;
  50. if (!PyList_Check(arg)) {
  51. _PyArg_BadArgument("heappop", "argument", "list", arg);
  52. goto exit;
  53. }
  54. heap = arg;
  55. return_value = _heapq_heappop_impl(module, heap);
  56. exit:
  57. return return_value;
  58. }
  59. PyDoc_STRVAR(_heapq_heapreplace__doc__,
  60. "heapreplace($module, heap, item, /)\n"
  61. "--\n"
  62. "\n"
  63. "Pop and return the current smallest value, and add the new item.\n"
  64. "\n"
  65. "This is more efficient than heappop() followed by heappush(), and can be\n"
  66. "more appropriate when using a fixed-size heap. Note that the value\n"
  67. "returned may be larger than item! That constrains reasonable uses of\n"
  68. "this routine unless written as part of a conditional replacement:\n"
  69. "\n"
  70. " if item > heap[0]:\n"
  71. " item = heapreplace(heap, item)");
  72. #define _HEAPQ_HEAPREPLACE_METHODDEF \
  73. {"heapreplace", _PyCFunction_CAST(_heapq_heapreplace), METH_FASTCALL, _heapq_heapreplace__doc__},
  74. static PyObject *
  75. _heapq_heapreplace_impl(PyObject *module, PyObject *heap, PyObject *item);
  76. static PyObject *
  77. _heapq_heapreplace(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  78. {
  79. PyObject *return_value = NULL;
  80. PyObject *heap;
  81. PyObject *item;
  82. if (!_PyArg_CheckPositional("heapreplace", nargs, 2, 2)) {
  83. goto exit;
  84. }
  85. if (!PyList_Check(args[0])) {
  86. _PyArg_BadArgument("heapreplace", "argument 1", "list", args[0]);
  87. goto exit;
  88. }
  89. heap = args[0];
  90. item = args[1];
  91. return_value = _heapq_heapreplace_impl(module, heap, item);
  92. exit:
  93. return return_value;
  94. }
  95. PyDoc_STRVAR(_heapq_heappushpop__doc__,
  96. "heappushpop($module, heap, item, /)\n"
  97. "--\n"
  98. "\n"
  99. "Push item on the heap, then pop and return the smallest item from the heap.\n"
  100. "\n"
  101. "The combined action runs more efficiently than heappush() followed by\n"
  102. "a separate call to heappop().");
  103. #define _HEAPQ_HEAPPUSHPOP_METHODDEF \
  104. {"heappushpop", _PyCFunction_CAST(_heapq_heappushpop), METH_FASTCALL, _heapq_heappushpop__doc__},
  105. static PyObject *
  106. _heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item);
  107. static PyObject *
  108. _heapq_heappushpop(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  109. {
  110. PyObject *return_value = NULL;
  111. PyObject *heap;
  112. PyObject *item;
  113. if (!_PyArg_CheckPositional("heappushpop", nargs, 2, 2)) {
  114. goto exit;
  115. }
  116. if (!PyList_Check(args[0])) {
  117. _PyArg_BadArgument("heappushpop", "argument 1", "list", args[0]);
  118. goto exit;
  119. }
  120. heap = args[0];
  121. item = args[1];
  122. return_value = _heapq_heappushpop_impl(module, heap, item);
  123. exit:
  124. return return_value;
  125. }
  126. PyDoc_STRVAR(_heapq_heapify__doc__,
  127. "heapify($module, heap, /)\n"
  128. "--\n"
  129. "\n"
  130. "Transform list into a heap, in-place, in O(len(heap)) time.");
  131. #define _HEAPQ_HEAPIFY_METHODDEF \
  132. {"heapify", (PyCFunction)_heapq_heapify, METH_O, _heapq_heapify__doc__},
  133. static PyObject *
  134. _heapq_heapify_impl(PyObject *module, PyObject *heap);
  135. static PyObject *
  136. _heapq_heapify(PyObject *module, PyObject *arg)
  137. {
  138. PyObject *return_value = NULL;
  139. PyObject *heap;
  140. if (!PyList_Check(arg)) {
  141. _PyArg_BadArgument("heapify", "argument", "list", arg);
  142. goto exit;
  143. }
  144. heap = arg;
  145. return_value = _heapq_heapify_impl(module, heap);
  146. exit:
  147. return return_value;
  148. }
  149. PyDoc_STRVAR(_heapq__heappop_max__doc__,
  150. "_heappop_max($module, heap, /)\n"
  151. "--\n"
  152. "\n"
  153. "Maxheap variant of heappop.");
  154. #define _HEAPQ__HEAPPOP_MAX_METHODDEF \
  155. {"_heappop_max", (PyCFunction)_heapq__heappop_max, METH_O, _heapq__heappop_max__doc__},
  156. static PyObject *
  157. _heapq__heappop_max_impl(PyObject *module, PyObject *heap);
  158. static PyObject *
  159. _heapq__heappop_max(PyObject *module, PyObject *arg)
  160. {
  161. PyObject *return_value = NULL;
  162. PyObject *heap;
  163. if (!PyList_Check(arg)) {
  164. _PyArg_BadArgument("_heappop_max", "argument", "list", arg);
  165. goto exit;
  166. }
  167. heap = arg;
  168. return_value = _heapq__heappop_max_impl(module, heap);
  169. exit:
  170. return return_value;
  171. }
  172. PyDoc_STRVAR(_heapq__heapreplace_max__doc__,
  173. "_heapreplace_max($module, heap, item, /)\n"
  174. "--\n"
  175. "\n"
  176. "Maxheap variant of heapreplace.");
  177. #define _HEAPQ__HEAPREPLACE_MAX_METHODDEF \
  178. {"_heapreplace_max", _PyCFunction_CAST(_heapq__heapreplace_max), METH_FASTCALL, _heapq__heapreplace_max__doc__},
  179. static PyObject *
  180. _heapq__heapreplace_max_impl(PyObject *module, PyObject *heap,
  181. PyObject *item);
  182. static PyObject *
  183. _heapq__heapreplace_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  184. {
  185. PyObject *return_value = NULL;
  186. PyObject *heap;
  187. PyObject *item;
  188. if (!_PyArg_CheckPositional("_heapreplace_max", nargs, 2, 2)) {
  189. goto exit;
  190. }
  191. if (!PyList_Check(args[0])) {
  192. _PyArg_BadArgument("_heapreplace_max", "argument 1", "list", args[0]);
  193. goto exit;
  194. }
  195. heap = args[0];
  196. item = args[1];
  197. return_value = _heapq__heapreplace_max_impl(module, heap, item);
  198. exit:
  199. return return_value;
  200. }
  201. PyDoc_STRVAR(_heapq__heapify_max__doc__,
  202. "_heapify_max($module, heap, /)\n"
  203. "--\n"
  204. "\n"
  205. "Maxheap variant of heapify.");
  206. #define _HEAPQ__HEAPIFY_MAX_METHODDEF \
  207. {"_heapify_max", (PyCFunction)_heapq__heapify_max, METH_O, _heapq__heapify_max__doc__},
  208. static PyObject *
  209. _heapq__heapify_max_impl(PyObject *module, PyObject *heap);
  210. static PyObject *
  211. _heapq__heapify_max(PyObject *module, PyObject *arg)
  212. {
  213. PyObject *return_value = NULL;
  214. PyObject *heap;
  215. if (!PyList_Check(arg)) {
  216. _PyArg_BadArgument("_heapify_max", "argument", "list", arg);
  217. goto exit;
  218. }
  219. heap = arg;
  220. return_value = _heapq__heapify_max_impl(module, heap);
  221. exit:
  222. return return_value;
  223. }
  224. /*[clinic end generated code: output=29e99a48c57f82bb input=a9049054013a1b77]*/