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.

280 lines
8.0 KiB

Merged revisions 65209-65216,65225-65226,65233,65239,65246-65247,65255-65256 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r65209 | raymond.hettinger | 2008-07-23 19:08:18 -0500 (Wed, 23 Jul 2008) | 1 line Finish-up the partial conversion from int to Py_ssize_t for deque indices and length. ........ r65210 | raymond.hettinger | 2008-07-23 19:53:49 -0500 (Wed, 23 Jul 2008) | 1 line Parse to the correct datatype. ........ r65211 | benjamin.peterson | 2008-07-23 21:27:46 -0500 (Wed, 23 Jul 2008) | 1 line fix spacing ........ r65212 | benjamin.peterson | 2008-07-23 21:31:28 -0500 (Wed, 23 Jul 2008) | 1 line fix markup ........ r65213 | benjamin.peterson | 2008-07-23 21:45:37 -0500 (Wed, 23 Jul 2008) | 1 line add some documentation for 2to3 ........ r65214 | raymond.hettinger | 2008-07-24 00:38:48 -0500 (Thu, 24 Jul 2008) | 1 line Finish conversion from int to Py_ssize_t. ........ r65215 | raymond.hettinger | 2008-07-24 02:04:55 -0500 (Thu, 24 Jul 2008) | 1 line Convert from long to Py_ssize_t. ........ r65216 | georg.brandl | 2008-07-24 02:09:21 -0500 (Thu, 24 Jul 2008) | 2 lines Fix indentation. ........ r65225 | benjamin.peterson | 2008-07-25 11:55:37 -0500 (Fri, 25 Jul 2008) | 1 line teach .bzrignore about doc tools ........ r65226 | benjamin.peterson | 2008-07-25 12:02:11 -0500 (Fri, 25 Jul 2008) | 1 line document default value for fillvalue ........ r65233 | raymond.hettinger | 2008-07-25 13:43:33 -0500 (Fri, 25 Jul 2008) | 1 line Issue 1592: Better error reporting for operations on closed shelves. ........ r65239 | benjamin.peterson | 2008-07-25 16:59:53 -0500 (Fri, 25 Jul 2008) | 1 line fix indentation ........ r65246 | andrew.kuchling | 2008-07-26 08:08:19 -0500 (Sat, 26 Jul 2008) | 1 line This sentence continues to bug me; rewrite it for the second time ........ r65247 | andrew.kuchling | 2008-07-26 08:09:06 -0500 (Sat, 26 Jul 2008) | 1 line Remove extra words ........ r65255 | skip.montanaro | 2008-07-26 19:49:02 -0500 (Sat, 26 Jul 2008) | 3 lines Close issue 3437 - missing state change when Allow lines are processed. Adds test cases which use Allow: as well. ........ r65256 | skip.montanaro | 2008-07-26 19:50:41 -0500 (Sat, 26 Jul 2008) | 2 lines note robotparser bug fix. ........
18 years ago
  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. _Py_IDENTIFIER(insert);
  7. static inline Py_ssize_t
  8. internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
  9. {
  10. PyObject *litem;
  11. Py_ssize_t mid;
  12. int res;
  13. if (lo < 0) {
  14. PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
  15. return -1;
  16. }
  17. if (hi == -1) {
  18. hi = PySequence_Size(list);
  19. if (hi < 0)
  20. return -1;
  21. }
  22. while (lo < hi) {
  23. /* The (size_t)cast ensures that the addition and subsequent division
  24. are performed as unsigned operations, avoiding difficulties from
  25. signed overflow. (See issue 13496.) */
  26. mid = ((size_t)lo + hi) / 2;
  27. litem = PySequence_GetItem(list, mid);
  28. if (litem == NULL)
  29. return -1;
  30. res = PyObject_RichCompareBool(item, litem, Py_LT);
  31. Py_DECREF(litem);
  32. if (res < 0)
  33. return -1;
  34. if (res)
  35. hi = mid;
  36. else
  37. lo = mid + 1;
  38. }
  39. return lo;
  40. }
  41. static PyObject *
  42. bisect_right(PyObject *self, PyObject *args, PyObject *kw)
  43. {
  44. PyObject *list, *item;
  45. Py_ssize_t lo = 0;
  46. Py_ssize_t hi = -1;
  47. Py_ssize_t index;
  48. static char *keywords[] = {"a", "x", "lo", "hi", NULL};
  49. if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
  50. list = PyTuple_GET_ITEM(args, 0);
  51. item = PyTuple_GET_ITEM(args, 1);
  52. }
  53. else {
  54. if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right",
  55. keywords, &list, &item, &lo, &hi))
  56. return NULL;
  57. }
  58. index = internal_bisect_right(list, item, lo, hi);
  59. if (index < 0)
  60. return NULL;
  61. return PyLong_FromSsize_t(index);
  62. }
  63. PyDoc_STRVAR(bisect_right_doc,
  64. "bisect_right(a, x[, lo[, hi]]) -> index\n\
  65. \n\
  66. Return the index where to insert item x in list a, assuming a is sorted.\n\
  67. \n\
  68. The return value i is such that all e in a[:i] have e <= x, and all e in\n\
  69. a[i:] have e > x. So if x already appears in the list, i points just\n\
  70. beyond the rightmost x already there\n\
  71. \n\
  72. Optional args lo (default 0) and hi (default len(a)) bound the\n\
  73. slice of a to be searched.\n");
  74. static PyObject *
  75. insort_right(PyObject *self, PyObject *args, PyObject *kw)
  76. {
  77. PyObject *list, *item, *result;
  78. Py_ssize_t lo = 0;
  79. Py_ssize_t hi = -1;
  80. Py_ssize_t index;
  81. static char *keywords[] = {"a", "x", "lo", "hi", NULL};
  82. if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
  83. list = PyTuple_GET_ITEM(args, 0);
  84. item = PyTuple_GET_ITEM(args, 1);
  85. }
  86. else {
  87. if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right",
  88. keywords, &list, &item, &lo, &hi))
  89. return NULL;
  90. }
  91. index = internal_bisect_right(list, item, lo, hi);
  92. if (index < 0)
  93. return NULL;
  94. if (PyList_CheckExact(list)) {
  95. if (PyList_Insert(list, index, item) < 0)
  96. return NULL;
  97. }
  98. else {
  99. result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item);
  100. if (result == NULL)
  101. return NULL;
  102. Py_DECREF(result);
  103. }
  104. Py_RETURN_NONE;
  105. }
  106. PyDoc_STRVAR(insort_right_doc,
  107. "insort_right(a, x[, lo[, hi]])\n\
  108. \n\
  109. Insert item x in list a, and keep it sorted assuming a is sorted.\n\
  110. \n\
  111. If x is already in a, insert it to the right of the rightmost x.\n\
  112. \n\
  113. Optional args lo (default 0) and hi (default len(a)) bound the\n\
  114. slice of a to be searched.\n");
  115. static inline Py_ssize_t
  116. internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
  117. {
  118. PyObject *litem;
  119. Py_ssize_t mid;
  120. int res;
  121. if (lo < 0) {
  122. PyErr_SetString(PyExc_ValueError, "lo must be non-negative");
  123. return -1;
  124. }
  125. if (hi == -1) {
  126. hi = PySequence_Size(list);
  127. if (hi < 0)
  128. return -1;
  129. }
  130. while (lo < hi) {
  131. /* The (size_t)cast ensures that the addition and subsequent division
  132. are performed as unsigned operations, avoiding difficulties from
  133. signed overflow. (See issue 13496.) */
  134. mid = ((size_t)lo + hi) / 2;
  135. litem = PySequence_GetItem(list, mid);
  136. if (litem == NULL)
  137. return -1;
  138. res = PyObject_RichCompareBool(litem, item, Py_LT);
  139. Py_DECREF(litem);
  140. if (res < 0)
  141. return -1;
  142. if (res)
  143. lo = mid + 1;
  144. else
  145. hi = mid;
  146. }
  147. return lo;
  148. }
  149. static PyObject *
  150. bisect_left(PyObject *self, PyObject *args, PyObject *kw)
  151. {
  152. PyObject *list, *item;
  153. Py_ssize_t lo = 0;
  154. Py_ssize_t hi = -1;
  155. Py_ssize_t index;
  156. static char *keywords[] = {"a", "x", "lo", "hi", NULL};
  157. if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
  158. list = PyTuple_GET_ITEM(args, 0);
  159. item = PyTuple_GET_ITEM(args, 1);
  160. }
  161. else {
  162. if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left",
  163. keywords, &list, &item, &lo, &hi))
  164. return NULL;
  165. }
  166. index = internal_bisect_left(list, item, lo, hi);
  167. if (index < 0)
  168. return NULL;
  169. return PyLong_FromSsize_t(index);
  170. }
  171. PyDoc_STRVAR(bisect_left_doc,
  172. "bisect_left(a, x[, lo[, hi]]) -> index\n\
  173. \n\
  174. Return the index where to insert item x in list a, assuming a is sorted.\n\
  175. \n\
  176. The return value i is such that all e in a[:i] have e < x, and all e in\n\
  177. a[i:] have e >= x. So if x already appears in the list, i points just\n\
  178. before the leftmost x already there.\n\
  179. \n\
  180. Optional args lo (default 0) and hi (default len(a)) bound the\n\
  181. slice of a to be searched.\n");
  182. static PyObject *
  183. insort_left(PyObject *self, PyObject *args, PyObject *kw)
  184. {
  185. PyObject *list, *item, *result;
  186. Py_ssize_t lo = 0;
  187. Py_ssize_t hi = -1;
  188. Py_ssize_t index;
  189. static char *keywords[] = {"a", "x", "lo", "hi", NULL};
  190. if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
  191. list = PyTuple_GET_ITEM(args, 0);
  192. item = PyTuple_GET_ITEM(args, 1);
  193. } else {
  194. if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left",
  195. keywords, &list, &item, &lo, &hi))
  196. return NULL;
  197. }
  198. index = internal_bisect_left(list, item, lo, hi);
  199. if (index < 0)
  200. return NULL;
  201. if (PyList_CheckExact(list)) {
  202. if (PyList_Insert(list, index, item) < 0)
  203. return NULL;
  204. } else {
  205. result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item);
  206. if (result == NULL)
  207. return NULL;
  208. Py_DECREF(result);
  209. }
  210. Py_RETURN_NONE;
  211. }
  212. PyDoc_STRVAR(insort_left_doc,
  213. "insort_left(a, x[, lo[, hi]])\n\
  214. \n\
  215. Insert item x in list a, and keep it sorted assuming a is sorted.\n\
  216. \n\
  217. If x is already in a, insert it to the left of the leftmost x.\n\
  218. \n\
  219. Optional args lo (default 0) and hi (default len(a)) bound the\n\
  220. slice of a to be searched.\n");
  221. static PyMethodDef bisect_methods[] = {
  222. {"bisect_right", (PyCFunction)(void(*)(void))bisect_right,
  223. METH_VARARGS|METH_KEYWORDS, bisect_right_doc},
  224. {"insort_right", (PyCFunction)(void(*)(void))insort_right,
  225. METH_VARARGS|METH_KEYWORDS, insort_right_doc},
  226. {"bisect_left", (PyCFunction)(void(*)(void))bisect_left,
  227. METH_VARARGS|METH_KEYWORDS, bisect_left_doc},
  228. {"insort_left", (PyCFunction)(void(*)(void))insort_left,
  229. METH_VARARGS|METH_KEYWORDS, insort_left_doc},
  230. {NULL, NULL} /* sentinel */
  231. };
  232. PyDoc_STRVAR(module_doc,
  233. "Bisection algorithms.\n\
  234. \n\
  235. This module provides support for maintaining a list in sorted order without\n\
  236. having to sort the list after each insertion. For long lists of items with\n\
  237. expensive comparison operations, this can be an improvement over the more\n\
  238. common approach.\n");
  239. static struct PyModuleDef _bisectmodule = {
  240. PyModuleDef_HEAD_INIT,
  241. "_bisect",
  242. module_doc,
  243. -1,
  244. bisect_methods,
  245. NULL,
  246. NULL,
  247. NULL,
  248. NULL
  249. };
  250. PyMODINIT_FUNC
  251. PyInit__bisect(void)
  252. {
  253. return PyModule_Create(&_bisectmodule);
  254. }