_bisectmodule.c.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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(_bisect_bisect_right__doc__,
  9. "bisect_right($module, /, a, x, lo=0, hi=None, *, key=None)\n"
  10. "--\n"
  11. "\n"
  12. "Return the index where to insert item x in list a, assuming a is sorted.\n"
  13. "\n"
  14. "The return value i is such that all e in a[:i] have e <= x, and all e in\n"
  15. "a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will\n"
  16. "insert just after the rightmost x already there.\n"
  17. "\n"
  18. "Optional args lo (default 0) and hi (default len(a)) bound the\n"
  19. "slice of a to be searched.\n"
  20. "\n"
  21. "A custom key function can be supplied to customize the sort order.");
  22. #define _BISECT_BISECT_RIGHT_METHODDEF \
  23. {"bisect_right", _PyCFunction_CAST(_bisect_bisect_right), METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_right__doc__},
  24. static Py_ssize_t
  25. _bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x,
  26. Py_ssize_t lo, Py_ssize_t hi, PyObject *key);
  27. static PyObject *
  28. _bisect_bisect_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  29. {
  30. PyObject *return_value = NULL;
  31. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  32. #define NUM_KEYWORDS 5
  33. static struct {
  34. PyGC_Head _this_is_not_used;
  35. PyObject_VAR_HEAD
  36. PyObject *ob_item[NUM_KEYWORDS];
  37. } _kwtuple = {
  38. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  39. .ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), },
  40. };
  41. #undef NUM_KEYWORDS
  42. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  43. #else // !Py_BUILD_CORE
  44. # define KWTUPLE NULL
  45. #endif // !Py_BUILD_CORE
  46. static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL};
  47. static _PyArg_Parser _parser = {
  48. .keywords = _keywords,
  49. .fname = "bisect_right",
  50. .kwtuple = KWTUPLE,
  51. };
  52. #undef KWTUPLE
  53. PyObject *argsbuf[5];
  54. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  55. PyObject *a;
  56. PyObject *x;
  57. Py_ssize_t lo = 0;
  58. Py_ssize_t hi = -1;
  59. PyObject *key = Py_None;
  60. Py_ssize_t _return_value;
  61. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
  62. if (!args) {
  63. goto exit;
  64. }
  65. a = args[0];
  66. x = args[1];
  67. if (!noptargs) {
  68. goto skip_optional_pos;
  69. }
  70. if (args[2]) {
  71. {
  72. Py_ssize_t ival = -1;
  73. PyObject *iobj = _PyNumber_Index(args[2]);
  74. if (iobj != NULL) {
  75. ival = PyLong_AsSsize_t(iobj);
  76. Py_DECREF(iobj);
  77. }
  78. if (ival == -1 && PyErr_Occurred()) {
  79. goto exit;
  80. }
  81. lo = ival;
  82. }
  83. if (!--noptargs) {
  84. goto skip_optional_pos;
  85. }
  86. }
  87. if (args[3]) {
  88. if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
  89. goto exit;
  90. }
  91. if (!--noptargs) {
  92. goto skip_optional_pos;
  93. }
  94. }
  95. skip_optional_pos:
  96. if (!noptargs) {
  97. goto skip_optional_kwonly;
  98. }
  99. key = args[4];
  100. skip_optional_kwonly:
  101. _return_value = _bisect_bisect_right_impl(module, a, x, lo, hi, key);
  102. if ((_return_value == -1) && PyErr_Occurred()) {
  103. goto exit;
  104. }
  105. return_value = PyLong_FromSsize_t(_return_value);
  106. exit:
  107. return return_value;
  108. }
  109. PyDoc_STRVAR(_bisect_insort_right__doc__,
  110. "insort_right($module, /, a, x, lo=0, hi=None, *, key=None)\n"
  111. "--\n"
  112. "\n"
  113. "Insert item x in list a, and keep it sorted assuming a is sorted.\n"
  114. "\n"
  115. "If x is already in a, insert it to the right of the rightmost x.\n"
  116. "\n"
  117. "Optional args lo (default 0) and hi (default len(a)) bound the\n"
  118. "slice of a to be searched.\n"
  119. "\n"
  120. "A custom key function can be supplied to customize the sort order.");
  121. #define _BISECT_INSORT_RIGHT_METHODDEF \
  122. {"insort_right", _PyCFunction_CAST(_bisect_insort_right), METH_FASTCALL|METH_KEYWORDS, _bisect_insort_right__doc__},
  123. static PyObject *
  124. _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x,
  125. Py_ssize_t lo, Py_ssize_t hi, PyObject *key);
  126. static PyObject *
  127. _bisect_insort_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  128. {
  129. PyObject *return_value = NULL;
  130. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  131. #define NUM_KEYWORDS 5
  132. static struct {
  133. PyGC_Head _this_is_not_used;
  134. PyObject_VAR_HEAD
  135. PyObject *ob_item[NUM_KEYWORDS];
  136. } _kwtuple = {
  137. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  138. .ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), },
  139. };
  140. #undef NUM_KEYWORDS
  141. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  142. #else // !Py_BUILD_CORE
  143. # define KWTUPLE NULL
  144. #endif // !Py_BUILD_CORE
  145. static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL};
  146. static _PyArg_Parser _parser = {
  147. .keywords = _keywords,
  148. .fname = "insort_right",
  149. .kwtuple = KWTUPLE,
  150. };
  151. #undef KWTUPLE
  152. PyObject *argsbuf[5];
  153. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  154. PyObject *a;
  155. PyObject *x;
  156. Py_ssize_t lo = 0;
  157. Py_ssize_t hi = -1;
  158. PyObject *key = Py_None;
  159. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
  160. if (!args) {
  161. goto exit;
  162. }
  163. a = args[0];
  164. x = args[1];
  165. if (!noptargs) {
  166. goto skip_optional_pos;
  167. }
  168. if (args[2]) {
  169. {
  170. Py_ssize_t ival = -1;
  171. PyObject *iobj = _PyNumber_Index(args[2]);
  172. if (iobj != NULL) {
  173. ival = PyLong_AsSsize_t(iobj);
  174. Py_DECREF(iobj);
  175. }
  176. if (ival == -1 && PyErr_Occurred()) {
  177. goto exit;
  178. }
  179. lo = ival;
  180. }
  181. if (!--noptargs) {
  182. goto skip_optional_pos;
  183. }
  184. }
  185. if (args[3]) {
  186. if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
  187. goto exit;
  188. }
  189. if (!--noptargs) {
  190. goto skip_optional_pos;
  191. }
  192. }
  193. skip_optional_pos:
  194. if (!noptargs) {
  195. goto skip_optional_kwonly;
  196. }
  197. key = args[4];
  198. skip_optional_kwonly:
  199. return_value = _bisect_insort_right_impl(module, a, x, lo, hi, key);
  200. exit:
  201. return return_value;
  202. }
  203. PyDoc_STRVAR(_bisect_bisect_left__doc__,
  204. "bisect_left($module, /, a, x, lo=0, hi=None, *, key=None)\n"
  205. "--\n"
  206. "\n"
  207. "Return the index where to insert item x in list a, assuming a is sorted.\n"
  208. "\n"
  209. "The return value i is such that all e in a[:i] have e < x, and all e in\n"
  210. "a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will\n"
  211. "insert just before the leftmost x already there.\n"
  212. "\n"
  213. "Optional args lo (default 0) and hi (default len(a)) bound the\n"
  214. "slice of a to be searched.\n"
  215. "\n"
  216. "A custom key function can be supplied to customize the sort order.");
  217. #define _BISECT_BISECT_LEFT_METHODDEF \
  218. {"bisect_left", _PyCFunction_CAST(_bisect_bisect_left), METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_left__doc__},
  219. static Py_ssize_t
  220. _bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x,
  221. Py_ssize_t lo, Py_ssize_t hi, PyObject *key);
  222. static PyObject *
  223. _bisect_bisect_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  224. {
  225. PyObject *return_value = NULL;
  226. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  227. #define NUM_KEYWORDS 5
  228. static struct {
  229. PyGC_Head _this_is_not_used;
  230. PyObject_VAR_HEAD
  231. PyObject *ob_item[NUM_KEYWORDS];
  232. } _kwtuple = {
  233. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  234. .ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), },
  235. };
  236. #undef NUM_KEYWORDS
  237. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  238. #else // !Py_BUILD_CORE
  239. # define KWTUPLE NULL
  240. #endif // !Py_BUILD_CORE
  241. static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL};
  242. static _PyArg_Parser _parser = {
  243. .keywords = _keywords,
  244. .fname = "bisect_left",
  245. .kwtuple = KWTUPLE,
  246. };
  247. #undef KWTUPLE
  248. PyObject *argsbuf[5];
  249. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  250. PyObject *a;
  251. PyObject *x;
  252. Py_ssize_t lo = 0;
  253. Py_ssize_t hi = -1;
  254. PyObject *key = Py_None;
  255. Py_ssize_t _return_value;
  256. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
  257. if (!args) {
  258. goto exit;
  259. }
  260. a = args[0];
  261. x = args[1];
  262. if (!noptargs) {
  263. goto skip_optional_pos;
  264. }
  265. if (args[2]) {
  266. {
  267. Py_ssize_t ival = -1;
  268. PyObject *iobj = _PyNumber_Index(args[2]);
  269. if (iobj != NULL) {
  270. ival = PyLong_AsSsize_t(iobj);
  271. Py_DECREF(iobj);
  272. }
  273. if (ival == -1 && PyErr_Occurred()) {
  274. goto exit;
  275. }
  276. lo = ival;
  277. }
  278. if (!--noptargs) {
  279. goto skip_optional_pos;
  280. }
  281. }
  282. if (args[3]) {
  283. if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
  284. goto exit;
  285. }
  286. if (!--noptargs) {
  287. goto skip_optional_pos;
  288. }
  289. }
  290. skip_optional_pos:
  291. if (!noptargs) {
  292. goto skip_optional_kwonly;
  293. }
  294. key = args[4];
  295. skip_optional_kwonly:
  296. _return_value = _bisect_bisect_left_impl(module, a, x, lo, hi, key);
  297. if ((_return_value == -1) && PyErr_Occurred()) {
  298. goto exit;
  299. }
  300. return_value = PyLong_FromSsize_t(_return_value);
  301. exit:
  302. return return_value;
  303. }
  304. PyDoc_STRVAR(_bisect_insort_left__doc__,
  305. "insort_left($module, /, a, x, lo=0, hi=None, *, key=None)\n"
  306. "--\n"
  307. "\n"
  308. "Insert item x in list a, and keep it sorted assuming a is sorted.\n"
  309. "\n"
  310. "If x is already in a, insert it to the left of the leftmost x.\n"
  311. "\n"
  312. "Optional args lo (default 0) and hi (default len(a)) bound the\n"
  313. "slice of a to be searched.\n"
  314. "\n"
  315. "A custom key function can be supplied to customize the sort order.");
  316. #define _BISECT_INSORT_LEFT_METHODDEF \
  317. {"insort_left", _PyCFunction_CAST(_bisect_insort_left), METH_FASTCALL|METH_KEYWORDS, _bisect_insort_left__doc__},
  318. static PyObject *
  319. _bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x,
  320. Py_ssize_t lo, Py_ssize_t hi, PyObject *key);
  321. static PyObject *
  322. _bisect_insort_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  323. {
  324. PyObject *return_value = NULL;
  325. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  326. #define NUM_KEYWORDS 5
  327. static struct {
  328. PyGC_Head _this_is_not_used;
  329. PyObject_VAR_HEAD
  330. PyObject *ob_item[NUM_KEYWORDS];
  331. } _kwtuple = {
  332. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  333. .ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), },
  334. };
  335. #undef NUM_KEYWORDS
  336. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  337. #else // !Py_BUILD_CORE
  338. # define KWTUPLE NULL
  339. #endif // !Py_BUILD_CORE
  340. static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL};
  341. static _PyArg_Parser _parser = {
  342. .keywords = _keywords,
  343. .fname = "insort_left",
  344. .kwtuple = KWTUPLE,
  345. };
  346. #undef KWTUPLE
  347. PyObject *argsbuf[5];
  348. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
  349. PyObject *a;
  350. PyObject *x;
  351. Py_ssize_t lo = 0;
  352. Py_ssize_t hi = -1;
  353. PyObject *key = Py_None;
  354. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
  355. if (!args) {
  356. goto exit;
  357. }
  358. a = args[0];
  359. x = args[1];
  360. if (!noptargs) {
  361. goto skip_optional_pos;
  362. }
  363. if (args[2]) {
  364. {
  365. Py_ssize_t ival = -1;
  366. PyObject *iobj = _PyNumber_Index(args[2]);
  367. if (iobj != NULL) {
  368. ival = PyLong_AsSsize_t(iobj);
  369. Py_DECREF(iobj);
  370. }
  371. if (ival == -1 && PyErr_Occurred()) {
  372. goto exit;
  373. }
  374. lo = ival;
  375. }
  376. if (!--noptargs) {
  377. goto skip_optional_pos;
  378. }
  379. }
  380. if (args[3]) {
  381. if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
  382. goto exit;
  383. }
  384. if (!--noptargs) {
  385. goto skip_optional_pos;
  386. }
  387. }
  388. skip_optional_pos:
  389. if (!noptargs) {
  390. goto skip_optional_kwonly;
  391. }
  392. key = args[4];
  393. skip_optional_kwonly:
  394. return_value = _bisect_insort_left_impl(module, a, x, lo, hi, key);
  395. exit:
  396. return return_value;
  397. }
  398. /*[clinic end generated code: output=57335e39ce2bf80e input=a9049054013a1b77]*/