You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
8.0 KiB

  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. _Py_IDENTIFIER(insert);
  12. static inline Py_ssize_t
  13. internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi,
  14. PyObject* key)
  15. {
  16. PyObject *litem;
  17. Py_ssize_t mid;
  18. int res;
  19. if (lo < 0) {
  20. PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
  21. return -1;
  22. }
  23. if (hi == -1) {
  24. hi = PySequence_Size(list);
  25. if (hi < 0)
  26. return -1;
  27. }
  28. while (lo < hi) {
  29. /* The (size_t)cast ensures that the addition and subsequent division
  30. are performed as unsigned operations, avoiding difficulties from
  31. signed overflow. (See issue 13496.) */
  32. mid = ((size_t)lo + hi) / 2;
  33. litem = PySequence_GetItem(list, mid);
  34. if (litem == NULL)
  35. return -1;
  36. if (key != Py_None) {
  37. PyObject *newitem = PyObject_CallOneArg(key, litem);
  38. if (newitem == NULL) {
  39. Py_DECREF(litem);
  40. return -1;
  41. }
  42. Py_SETREF(litem, newitem);
  43. }
  44. res = PyObject_RichCompareBool(item, litem, Py_LT);
  45. Py_DECREF(litem);
  46. if (res < 0)
  47. return -1;
  48. if (res)
  49. hi = mid;
  50. else
  51. lo = mid + 1;
  52. }
  53. return lo;
  54. }
  55. /*[clinic input]
  56. _bisect.bisect_right -> Py_ssize_t
  57. a: object
  58. x: object
  59. lo: Py_ssize_t = 0
  60. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  61. *
  62. key: object = None
  63. Return the index where to insert item x in list a, assuming a is sorted.
  64. The return value i is such that all e in a[:i] have e <= x, and all e in
  65. a[i:] have e > x. So if x already appears in the list, i points just
  66. beyond the rightmost x already there
  67. Optional args lo (default 0) and hi (default len(a)) bound the
  68. slice of a to be searched.
  69. [clinic start generated code]*/
  70. static Py_ssize_t
  71. _bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x,
  72. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  73. /*[clinic end generated code: output=3a4bc09cc7c8a73d input=1313e9ca20c8bc3c]*/
  74. {
  75. return internal_bisect_right(a, x, lo, hi, key);
  76. }
  77. /*[clinic input]
  78. _bisect.insort_right
  79. a: object
  80. x: object
  81. lo: Py_ssize_t = 0
  82. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  83. *
  84. key: object = None
  85. Insert item x in list a, and keep it sorted assuming a is sorted.
  86. If x is already in a, insert it to the right of the rightmost x.
  87. Optional args lo (default 0) and hi (default len(a)) bound the
  88. slice of a to be searched.
  89. [clinic start generated code]*/
  90. static PyObject *
  91. _bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x,
  92. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  93. /*[clinic end generated code: output=ac3bf26d07aedda2 input=44e1708e26b7b802]*/
  94. {
  95. PyObject *result, *key_x;
  96. Py_ssize_t index;
  97. if (key == Py_None) {
  98. index = internal_bisect_right(a, x, lo, hi, key);
  99. } else {
  100. key_x = PyObject_CallOneArg(key, x);
  101. if (x == NULL) {
  102. return NULL;
  103. }
  104. index = internal_bisect_right(a, key_x, lo, hi, key);
  105. Py_DECREF(key_x);
  106. }
  107. if (index < 0)
  108. return NULL;
  109. if (PyList_CheckExact(a)) {
  110. if (PyList_Insert(a, index, x) < 0)
  111. return NULL;
  112. }
  113. else {
  114. result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x);
  115. if (result == NULL)
  116. return NULL;
  117. Py_DECREF(result);
  118. }
  119. Py_RETURN_NONE;
  120. }
  121. static inline Py_ssize_t
  122. internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi,
  123. PyObject *key)
  124. {
  125. PyObject *litem;
  126. Py_ssize_t mid;
  127. int res;
  128. if (lo < 0) {
  129. PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
  130. return -1;
  131. }
  132. if (hi == -1) {
  133. hi = PySequence_Size(list);
  134. if (hi < 0)
  135. return -1;
  136. }
  137. while (lo < hi) {
  138. /* The (size_t)cast ensures that the addition and subsequent division
  139. are performed as unsigned operations, avoiding difficulties from
  140. signed overflow. (See issue 13496.) */
  141. mid = ((size_t)lo + hi) / 2;
  142. litem = PySequence_GetItem(list, mid);
  143. if (litem == NULL)
  144. return -1;
  145. if (key != Py_None) {
  146. PyObject *newitem = PyObject_CallOneArg(key, litem);
  147. if (newitem == NULL) {
  148. Py_DECREF(litem);
  149. return -1;
  150. }
  151. Py_SETREF(litem, newitem);
  152. }
  153. res = PyObject_RichCompareBool(litem, item, Py_LT);
  154. Py_DECREF(litem);
  155. if (res < 0)
  156. return -1;
  157. if (res)
  158. lo = mid + 1;
  159. else
  160. hi = mid;
  161. }
  162. return lo;
  163. }
  164. /*[clinic input]
  165. _bisect.bisect_left -> Py_ssize_t
  166. a: object
  167. x: object
  168. lo: Py_ssize_t = 0
  169. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  170. *
  171. key: object = None
  172. Return the index where to insert item x in list a, assuming a is sorted.
  173. The return value i is such that all e in a[:i] have e < x, and all e in
  174. a[i:] have e >= x. So if x already appears in the list, i points just
  175. before the leftmost x already there.
  176. Optional args lo (default 0) and hi (default len(a)) bound the
  177. slice of a to be searched.
  178. [clinic start generated code]*/
  179. static Py_ssize_t
  180. _bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x,
  181. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  182. /*[clinic end generated code: output=70749d6e5cae9284 input=3cbeec690f2f6c6e]*/
  183. {
  184. return internal_bisect_left(a, x, lo, hi, key);
  185. }
  186. /*[clinic input]
  187. _bisect.insort_left
  188. a: object
  189. x: object
  190. lo: Py_ssize_t = 0
  191. hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
  192. *
  193. key: object = None
  194. Insert item x in list a, and keep it sorted assuming a is sorted.
  195. If x is already in a, insert it to the left of the leftmost x.
  196. Optional args lo (default 0) and hi (default len(a)) bound the
  197. slice of a to be searched.
  198. [clinic start generated code]*/
  199. static PyObject *
  200. _bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x,
  201. Py_ssize_t lo, Py_ssize_t hi, PyObject *key)
  202. /*[clinic end generated code: output=b1d33e5e7ffff11e input=3ab65d8784f585b1]*/
  203. {
  204. PyObject *result, *key_x;
  205. Py_ssize_t index;
  206. if (key == Py_None) {
  207. index = internal_bisect_left(a, x, lo, hi, key);
  208. } else {
  209. key_x = PyObject_CallOneArg(key, x);
  210. if (x == NULL) {
  211. return NULL;
  212. }
  213. index = internal_bisect_left(a, key_x, lo, hi, key);
  214. Py_DECREF(key_x);
  215. }
  216. if (index < 0)
  217. return NULL;
  218. if (PyList_CheckExact(a)) {
  219. if (PyList_Insert(a, index, x) < 0)
  220. return NULL;
  221. } else {
  222. result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x);
  223. if (result == NULL)
  224. return NULL;
  225. Py_DECREF(result);
  226. }
  227. Py_RETURN_NONE;
  228. }
  229. static PyMethodDef bisect_methods[] = {
  230. _BISECT_BISECT_RIGHT_METHODDEF
  231. _BISECT_INSORT_RIGHT_METHODDEF
  232. _BISECT_BISECT_LEFT_METHODDEF
  233. _BISECT_INSORT_LEFT_METHODDEF
  234. {NULL, NULL} /* sentinel */
  235. };
  236. PyDoc_STRVAR(module_doc,
  237. "Bisection algorithms.\n\
  238. \n\
  239. This module provides support for maintaining a list in sorted order without\n\
  240. having to sort the list after each insertion. For long lists of items with\n\
  241. expensive comparison operations, this can be an improvement over the more\n\
  242. common approach.\n");
  243. static struct PyModuleDef _bisectmodule = {
  244. PyModuleDef_HEAD_INIT,
  245. .m_name = "_bisect",
  246. .m_doc = module_doc,
  247. .m_methods = bisect_methods,
  248. .m_size = 0
  249. };
  250. PyMODINIT_FUNC
  251. PyInit__bisect(void)
  252. {
  253. return PyModuleDef_Init(&_bisectmodule);
  254. }