_bisectmodule.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* Bisection algorithms. Drop in replacement for bisect.py
  2. Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru).
  3. */
  4. #define PY_SSIZE_T_CLEAN
  5. #include "Python.h"
  6. /*[clinic input]
  7. module _bisect
  8. [clinic start generated code]*/
  9. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4d56a2b2033b462b]*/
  10. #include "clinic/_bisectmodule.c.h"
  11. typedef struct {
  12. PyObject *str_insert;
  13. } bisect_state;
  14. static inline bisect_state*
  15. get_bisect_state(PyObject *module)
  16. {
  17. void *state = PyModule_GetState(module);
  18. assert(state != NULL);
  19. return (bisect_state *)state;
  20. }
  21. static ssizeargfunc
  22. get_sq_item(PyObject *s)
  23. {
  24. // The parts of PySequence_GetItem that we only need to do once
  25. PyTypeObject *tp = Py_TYPE(s);
  26. PySequenceMethods *m = tp->tp_as_sequence;
  27. if (m && m->sq_item) {
  28. return m->sq_item;
  29. }
  30. const char *msg;
  31. if (tp->tp_as_mapping && tp->tp_as_mapping->mp_subscript) {
  32. msg = "%.200s is not a sequence";
  33. }
  34. else {
  35. msg = "'%.200s' object does not support indexing";
  36. }
  37. PyErr_Format(PyExc_TypeError, msg, tp->tp_name);
  38. return NULL;
  39. }
  40. static inline Py_ssize_t
  41. internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi,
  42. PyObject* key)
  43. {
  44. PyObject *litem;
  45. Py_ssize_t mid;
  46. int res;
  47. if (lo < 0) {
  48. PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
  49. return -1;
  50. }
  51. if (hi == -1) {
  52. hi = PySequence_Size(list);
  53. if (hi < 0)
  54. return -1;
  55. }
  56. ssizeargfunc sq_item = get_sq_item(list);
  57. if (sq_item == NULL) {
  58. return -1;
  59. }
  60. if (Py_EnterRecursiveCall("in _bisect.bisect_right")) {
  61. return -1;
  62. }
  63. PyTypeObject *tp = Py_TYPE(item);
  64. richcmpfunc compare = tp->tp_richcompare;
  65. while (lo < hi) {
  66. /* The (size_t)cast ensures that the addition and subsequent division
  67. are performed as unsigned operations, avoiding difficulties from
  68. signed overflow. (See issue 13496.) */
  69. mid = ((size_t)lo + hi) / 2;
  70. assert(mid >= 0);
  71. // PySequence_GetItem, but we already checked the types.
  72. litem = sq_item(list, mid);
  73. assert((PyErr_Occurred() == NULL) ^ (litem == NULL));
  74. if (litem == NULL) {
  75. goto error;
  76. }
  77. if (key != Py_None) {
  78. PyObject *newitem = PyObject_CallOneArg(key, litem);
  79. if (newitem == NULL) {
  80. goto error;
  81. }
  82. Py_SETREF(litem, newitem);
  83. }
  84. /* if item < key(list[mid]):
  85. * hi = mid
  86. * else:
  87. * lo = mid + 1
  88. */
  89. if (compare != NULL && Py_IS_TYPE(litem, tp)) {
  90. // A fast path for comparing objects of the same type
  91. PyObject *res_obj = compare(item, litem, Py_LT);
  92. if (res_obj == Py_True) {
  93. Py_DECREF(res_obj);
  94. Py_DECREF(litem);
  95. hi = mid;
  96. continue;
  97. }
  98. if (res_obj == Py_False) {
  99. Py_DECREF(res_obj);
  100. Py_DECREF(litem);
  101. lo = mid + 1;
  102. continue;
  103. }
  104. if (res_obj == NULL) {
  105. goto error;
  106. }
  107. if (res_obj == Py_NotImplemented) {
  108. Py_DECREF(res_obj);
  109. compare = NULL;
  110. res = PyObject_RichCompareBool(item, litem, Py_LT);
  111. }
  112. else {
  113. res = PyObject_IsTrue(res_obj);
  114. Py_DECREF(res_obj);
  115. }
  116. }
  117. else {
  118. // A default path for comparing arbitrary objects
  119. res = PyObject_RichCompareBool(item, litem, Py_LT);
  120. }
  121. if (res < 0) {
  122. goto error;
  123. }
  124. Py_DECREF(litem);
  125. if (res)
  126. hi = mid;
  127. else
  128. lo = mid + 1;
  129. }
  130. Py_LeaveRecursiveCall();
  131. return lo;
  132. error:
  133. Py_LeaveRecursiveCall();
  134. Py_XDECREF(litem);
  135. return -1;
  136. }
  137. /*[clinic input]
  138. _bisect.bisect_right -> Py_ssize_t
  139. a: object
  140. x: object
  141. lo: Py_ssize_t = 0
  142. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  143. *
  144. key: object = None
  145. Return the index where to insert item x in list a, assuming a is sorted.
  146. The return value i is such that all e in a[:i] have e <= x, and all e in
  147. a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will
  148. insert just after the rightmost x already there.
  149. Optional args lo (default 0) and hi (default len(a)) bound the
  150. slice of a to be searched.
  151. A custom key function can be supplied to customize the sort order.
  152. [clinic start generated code]*/
  153. static Py_ssize_t
  154. _bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x,
  155. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  156. /*[clinic end generated code: output=3a4bc09cc7c8a73d input=43071869772dd53a]*/
  157. {
  158. return internal_bisect_right(a, x, lo, hi, key);
  159. }
  160. /*[clinic input]
  161. _bisect.insort_right
  162. a: object
  163. x: object
  164. lo: Py_ssize_t = 0
  165. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  166. *
  167. key: object = None
  168. Insert item x in list a, and keep it sorted assuming a is sorted.
  169. If x is already in a, insert it to the right of the rightmost x.
  170. Optional args lo (default 0) and hi (default len(a)) bound the
  171. slice of a to be searched.
  172. A custom key function can be supplied to customize the sort order.
  173. [clinic start generated code]*/
  174. static PyObject *
  175. _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x,
  176. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  177. /*[clinic end generated code: output=ac3bf26d07aedda2 input=f60777d2b6ddb239]*/
  178. {
  179. PyObject *result, *key_x;
  180. Py_ssize_t index;
  181. if (key == Py_None) {
  182. index = internal_bisect_right(a, x, lo, hi, key);
  183. } else {
  184. key_x = PyObject_CallOneArg(key, x);
  185. if (key_x == NULL) {
  186. return NULL;
  187. }
  188. index = internal_bisect_right(a, key_x, lo, hi, key);
  189. Py_DECREF(key_x);
  190. }
  191. if (index < 0)
  192. return NULL;
  193. if (PyList_CheckExact(a)) {
  194. if (PyList_Insert(a, index, x) < 0)
  195. return NULL;
  196. }
  197. else {
  198. bisect_state *state = get_bisect_state(module);
  199. result = _PyObject_CallMethod(a, state->str_insert, "nO", index, x);
  200. if (result == NULL)
  201. return NULL;
  202. Py_DECREF(result);
  203. }
  204. Py_RETURN_NONE;
  205. }
  206. static inline Py_ssize_t
  207. internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi,
  208. PyObject *key)
  209. {
  210. PyObject *litem;
  211. Py_ssize_t mid;
  212. int res;
  213. if (lo < 0) {
  214. PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
  215. return -1;
  216. }
  217. if (hi == -1) {
  218. hi = PySequence_Size(list);
  219. if (hi < 0)
  220. return -1;
  221. }
  222. ssizeargfunc sq_item = get_sq_item(list);
  223. if (sq_item == NULL) {
  224. return -1;
  225. }
  226. if (Py_EnterRecursiveCall("in _bisect.bisect_left")) {
  227. return -1;
  228. }
  229. PyTypeObject *tp = Py_TYPE(item);
  230. richcmpfunc compare = tp->tp_richcompare;
  231. while (lo < hi) {
  232. /* The (size_t)cast ensures that the addition and subsequent division
  233. are performed as unsigned operations, avoiding difficulties from
  234. signed overflow. (See issue 13496.) */
  235. mid = ((size_t)lo + hi) / 2;
  236. assert(mid >= 0);
  237. // PySequence_GetItem, but we already checked the types.
  238. litem = sq_item(list, mid);
  239. assert((PyErr_Occurred() == NULL) ^ (litem == NULL));
  240. if (litem == NULL) {
  241. goto error;
  242. }
  243. if (key != Py_None) {
  244. PyObject *newitem = PyObject_CallOneArg(key, litem);
  245. if (newitem == NULL) {
  246. goto error;
  247. }
  248. Py_SETREF(litem, newitem);
  249. }
  250. /* if key(list[mid]) < item:
  251. * lo = mid + 1
  252. * else:
  253. * hi = mid
  254. */
  255. if (compare != NULL && Py_IS_TYPE(litem, tp)) {
  256. // A fast path for comparing objects of the same type
  257. PyObject *res_obj = compare(litem, item, Py_LT);
  258. if (res_obj == Py_True) {
  259. Py_DECREF(res_obj);
  260. Py_DECREF(litem);
  261. lo = mid + 1;
  262. continue;
  263. }
  264. if (res_obj == Py_False) {
  265. Py_DECREF(res_obj);
  266. Py_DECREF(litem);
  267. hi = mid;
  268. continue;
  269. }
  270. if (res_obj == NULL) {
  271. goto error;
  272. }
  273. if (res_obj == Py_NotImplemented) {
  274. Py_DECREF(res_obj);
  275. compare = NULL;
  276. res = PyObject_RichCompareBool(litem, item, Py_LT);
  277. }
  278. else {
  279. res = PyObject_IsTrue(res_obj);
  280. Py_DECREF(res_obj);
  281. }
  282. }
  283. else {
  284. // A default path for comparing arbitrary objects
  285. res = PyObject_RichCompareBool(litem, item, Py_LT);
  286. }
  287. if (res < 0) {
  288. goto error;
  289. }
  290. Py_DECREF(litem);
  291. if (res)
  292. lo = mid + 1;
  293. else
  294. hi = mid;
  295. }
  296. Py_LeaveRecursiveCall();
  297. return lo;
  298. error:
  299. Py_LeaveRecursiveCall();
  300. Py_XDECREF(litem);
  301. return -1;
  302. }
  303. /*[clinic input]
  304. _bisect.bisect_left -> Py_ssize_t
  305. a: object
  306. x: object
  307. lo: Py_ssize_t = 0
  308. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  309. *
  310. key: object = None
  311. Return the index where to insert item x in list a, assuming a is sorted.
  312. The return value i is such that all e in a[:i] have e < x, and all e in
  313. a[i:] have e >= x. So if x already appears in the list, a.insert(i, x) will
  314. insert just before the leftmost x already there.
  315. Optional args lo (default 0) and hi (default len(a)) bound the
  316. slice of a to be searched.
  317. A custom key function can be supplied to customize the sort order.
  318. [clinic start generated code]*/
  319. static Py_ssize_t
  320. _bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x,
  321. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  322. /*[clinic end generated code: output=70749d6e5cae9284 input=f29c4fe7f9b797c7]*/
  323. {
  324. return internal_bisect_left(a, x, lo, hi, key);
  325. }
  326. /*[clinic input]
  327. _bisect.insort_left
  328. a: object
  329. x: object
  330. lo: Py_ssize_t = 0
  331. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  332. *
  333. key: object = None
  334. Insert item x in list a, and keep it sorted assuming a is sorted.
  335. If x is already in a, insert it to the left of the leftmost x.
  336. Optional args lo (default 0) and hi (default len(a)) bound the
  337. slice of a to be searched.
  338. A custom key function can be supplied to customize the sort order.
  339. [clinic start generated code]*/
  340. static PyObject *
  341. _bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x,
  342. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  343. /*[clinic end generated code: output=b1d33e5e7ffff11e input=0a700a82edbd472c]*/
  344. {
  345. PyObject *result, *key_x;
  346. Py_ssize_t index;
  347. if (key == Py_None) {
  348. index = internal_bisect_left(a, x, lo, hi, key);
  349. } else {
  350. key_x = PyObject_CallOneArg(key, x);
  351. if (key_x == NULL) {
  352. return NULL;
  353. }
  354. index = internal_bisect_left(a, key_x, lo, hi, key);
  355. Py_DECREF(key_x);
  356. }
  357. if (index < 0)
  358. return NULL;
  359. if (PyList_CheckExact(a)) {
  360. if (PyList_Insert(a, index, x) < 0)
  361. return NULL;
  362. } else {
  363. bisect_state *state = get_bisect_state(module);
  364. result = _PyObject_CallMethod(a, state->str_insert, "nO", index, x);
  365. if (result == NULL)
  366. return NULL;
  367. Py_DECREF(result);
  368. }
  369. Py_RETURN_NONE;
  370. }
  371. static PyMethodDef bisect_methods[] = {
  372. _BISECT_BISECT_RIGHT_METHODDEF
  373. _BISECT_INSORT_RIGHT_METHODDEF
  374. _BISECT_BISECT_LEFT_METHODDEF
  375. _BISECT_INSORT_LEFT_METHODDEF
  376. {NULL, NULL} /* sentinel */
  377. };
  378. PyDoc_STRVAR(module_doc,
  379. "Bisection algorithms.\n\
  380. \n\
  381. This module provides support for maintaining a list in sorted order without\n\
  382. having to sort the list after each insertion. For long lists of items with\n\
  383. expensive comparison operations, this can be an improvement over the more\n\
  384. common approach.\n");
  385. static int
  386. bisect_clear(PyObject *module)
  387. {
  388. bisect_state *state = get_bisect_state(module);
  389. Py_CLEAR(state->str_insert);
  390. return 0;
  391. }
  392. static void
  393. bisect_free(void *module)
  394. {
  395. bisect_clear((PyObject *)module);
  396. }
  397. static int
  398. bisect_modexec(PyObject *m)
  399. {
  400. bisect_state *state = get_bisect_state(m);
  401. state->str_insert = PyUnicode_InternFromString("insert");
  402. if (state->str_insert == NULL) {
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. static PyModuleDef_Slot bisect_slots[] = {
  408. {Py_mod_exec, bisect_modexec},
  409. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  410. {0, NULL}
  411. };
  412. static struct PyModuleDef _bisectmodule = {
  413. PyModuleDef_HEAD_INIT,
  414. .m_name = "_bisect",
  415. .m_size = sizeof(bisect_state),
  416. .m_doc = module_doc,
  417. .m_methods = bisect_methods,
  418. .m_slots = bisect_slots,
  419. .m_clear = bisect_clear,
  420. .m_free = bisect_free,
  421. };
  422. PyMODINIT_FUNC
  423. PyInit__bisect(void)
  424. {
  425. return PyModuleDef_Init(&_bisectmodule);
  426. }