diff -r 45713818fd81 -r ba5cde025849 Modules/_bisectmodule.c --- a/Modules/_bisectmodule.c Thu Nov 24 23:31:59 2016 +0100 +++ b/Modules/_bisectmodule.c Fri Nov 25 00:29:05 2016 +0100 @@ -6,6 +6,13 @@ #define PY_SSIZE_T_CLEAN #include "Python.h" +#include "clinic/_bisectmodule.c.h" + +/*[clinic input] +module bisect +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d0e256c42a9e4c13]*/ + _Py_IDENTIFIER(insert); static Py_ssize_t @@ -44,56 +51,76 @@ return lo; } +/*[clinic input] +bisect.bisect_right + + a: 'O' + The list in which x is to be inserted. + x: 'O' + The value to be inserted. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning len(a)). + +Return the index where to insert item x in list a, assuming a is sorted. + +The return value i is such that all e in a[:i] have e <= x, and all e in +a[i:] have e > x. So if x already appears in the list, i points just +beyond the rightmost x already there. + +Optional args lo and hi bound the slice of a to be searched. +[clinic start generated code]*/ + static PyObject * -bisect_right(PyObject *self, PyObject *args, PyObject *kw) +bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=a2fb3e3261e46954 input=d18ab313426f78d2]*/ { - PyObject *list, *item; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right", - keywords, &list, &item, &lo, &hi)) - return NULL; - index = internal_bisect_right(list, item, lo, hi); + index = internal_bisect_right(a, x, lo, hi); if (index < 0) return NULL; return PyLong_FromSsize_t(index); } -PyDoc_STRVAR(bisect_right_doc, -"bisect_right(a, x[, lo[, hi]]) -> index\n\ -\n\ -Return the index where to insert item x in list a, assuming a is sorted.\n\ -\n\ -The return value i is such that all e in a[:i] have e <= x, and all e in\n\ -a[i:] have e > x. So if x already appears in the list, i points just\n\ -beyond the rightmost x already there\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); +/*[clinic input] +bisect.insort_right + + a: 'O' + The list in which x will be inserted. + x: 'O' + The value to insert. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning len(a)). + +Insert item x in list a, and keep it sorted assuming a is sorted. + +If x is already in a, insert it to the right of the rightmost x. + +Optional args lo and hi bound the slice of a to be searched. + +[clinic start generated code]*/ static PyObject * -insort_right(PyObject *self, PyObject *args, PyObject *kw) +bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=6e0b99c731a11c1a input=f84559a13742ef89]*/ { - PyObject *list, *item, *result; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; + PyObject *result; Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right", - keywords, &list, &item, &lo, &hi)) - return NULL; - index = internal_bisect_right(list, item, lo, hi); + index = internal_bisect_right(a, x, lo, hi); if (index < 0) return NULL; - if (PyList_CheckExact(list)) { - if (PyList_Insert(list, index, item) < 0) + if (PyList_CheckExact(a)) { + if (PyList_Insert(a, index, x) < 0) return NULL; } else { - result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item); + result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x); if (result == NULL) return NULL; Py_DECREF(result); @@ -102,16 +129,6 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(insort_right_doc, -"insort_right(a, x[, lo[, hi]])\n\ -\n\ -Insert item x in list a, and keep it sorted assuming a is sorted.\n\ -\n\ -If x is already in a, insert it to the right of the rightmost x.\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); - static Py_ssize_t internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi) { @@ -148,56 +165,77 @@ return lo; } +/*[clinic input] +bisect.bisect_left + + a: 'O' + The list in which x is to be inserted. + x: 'O' + The value to be inserted. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning len(a)). + +Return the index where to insert item x in list a, assuming a is sorted. + +The return value i is such that all e in a[:i] have e < x, and all e in +a[i:] have e >= x. So if x already appears in the list, i points just +before the leftmost x already there. + +Optional args lo and hi bound the slice of a to be searched. + +[clinic start generated code]*/ + static PyObject * -bisect_left(PyObject *self, PyObject *args, PyObject *kw) +bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=27a0228c4a0a5fa2 input=c792433b9d6859f3]*/ { - PyObject *list, *item; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left", - keywords, &list, &item, &lo, &hi)) - return NULL; - index = internal_bisect_left(list, item, lo, hi); + index = internal_bisect_left(a, x, lo, hi); if (index < 0) return NULL; return PyLong_FromSsize_t(index); } -PyDoc_STRVAR(bisect_left_doc, -"bisect_left(a, x[, lo[, hi]]) -> index\n\ -\n\ -Return the index where to insert item x in list a, assuming a is sorted.\n\ -\n\ -The return value i is such that all e in a[:i] have e < x, and all e in\n\ -a[i:] have e >= x. So if x already appears in the list, i points just\n\ -before the leftmost x already there.\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); +/*[clinic input] +bisect.insort_left + + a: 'O' + The list in which x will be inserted. + x: 'O' + The value to insert. + lo: 'n' = 0 + Lower bound, defaults to 0. + hi: 'n' = -1 + Upper bound, defaults to -1 (meaning len(a)). + +Insert item x in list a, and keep it sorted assuming a is sorted. + +If x is already in a, insert it to the left of the leftmost x. + +Optional args lo and hi bound the slice of a to be searched. + +[clinic start generated code]*/ static PyObject * -insort_left(PyObject *self, PyObject *args, PyObject *kw) +bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi) +/*[clinic end generated code: output=aa0228af6970ec52 input=8d43307f38becedb]*/ { - PyObject *list, *item, *result; - Py_ssize_t lo = 0; - Py_ssize_t hi = -1; + PyObject *result; Py_ssize_t index; - static char *keywords[] = {"a", "x", "lo", "hi", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left", - keywords, &list, &item, &lo, &hi)) - return NULL; - index = internal_bisect_left(list, item, lo, hi); + index = internal_bisect_left(a, x, lo, hi); if (index < 0) return NULL; - if (PyList_CheckExact(list)) { - if (PyList_Insert(list, index, item) < 0) + if (PyList_CheckExact(a)) { + if (PyList_Insert(a, index, x) < 0) return NULL; } else { - result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item); + result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x); if (result == NULL) return NULL; Py_DECREF(result); @@ -206,25 +244,11 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(insort_left_doc, -"insort_left(a, x[, lo[, hi]])\n\ -\n\ -Insert item x in list a, and keep it sorted assuming a is sorted.\n\ -\n\ -If x is already in a, insert it to the left of the leftmost x.\n\ -\n\ -Optional args lo (default 0) and hi (default len(a)) bound the\n\ -slice of a to be searched.\n"); - static PyMethodDef bisect_methods[] = { - {"bisect_right", (PyCFunction)bisect_right, - METH_VARARGS|METH_KEYWORDS, bisect_right_doc}, - {"insort_right", (PyCFunction)insort_right, - METH_VARARGS|METH_KEYWORDS, insort_right_doc}, - {"bisect_left", (PyCFunction)bisect_left, - METH_VARARGS|METH_KEYWORDS, bisect_left_doc}, - {"insort_left", (PyCFunction)insort_left, - METH_VARARGS|METH_KEYWORDS, insort_left_doc}, + BISECT_BISECT_RIGHT_METHODDEF + BISECT_INSORT_RIGHT_METHODDEF + BISECT_BISECT_LEFT_METHODDEF + BISECT_INSORT_LEFT_METHODDEF {NULL, NULL} /* sentinel */ }; diff -r 45713818fd81 -r ba5cde025849 Modules/clinic/_bisectmodule.c.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Modules/clinic/_bisectmodule.c.h Fri Nov 25 00:29:05 2016 +0100 @@ -0,0 +1,196 @@ +/*[clinic input] +preserve +[clinic start generated code]*/ + +PyDoc_STRVAR(bisect_bisect_right__doc__, +"bisect_right($module, /, a, x, lo=0, hi=-1)\n" +"--\n" +"\n" +"Return the index where to insert item x in list a, assuming a is sorted.\n" +"\n" +" a\n" +" The list in which x is to be inserted.\n" +" x\n" +" The value to be inserted.\n" +" lo\n" +" Lower bound, defaults to 0.\n" +" hi\n" +" Upper bound, defaults to -1 (meaning len(a)).\n" +"\n" +"The return value i is such that all e in a[:i] have e <= x, and all e in\n" +"a[i:] have e > x. So if x already appears in the list, i points just\n" +"beyond the rightmost x already there.\n" +"\n" +"Optional args lo and hi bound the slice of a to be searched."); + +#define BISECT_BISECT_RIGHT_METHODDEF \ + {"bisect_right", (PyCFunction)bisect_bisect_right, METH_FASTCALL, bisect_bisect_right__doc__}, + +static PyObject * +bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +bisect_bisect_right(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {"OO|nn:bisect_right", _keywords, 0}; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + + if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser, + &a, &x, &lo, &hi)) { + goto exit; + } + return_value = bisect_bisect_right_impl(module, a, x, lo, hi); + +exit: + return return_value; +} + +PyDoc_STRVAR(bisect_insort_right__doc__, +"insort_right($module, /, a, x, lo=0, hi=-1)\n" +"--\n" +"\n" +"Insert item x in list a, and keep it sorted assuming a is sorted.\n" +"\n" +" a\n" +" The list in which x will be inserted.\n" +" x\n" +" The value to insert.\n" +" lo\n" +" Lower bound, defaults to 0.\n" +" hi\n" +" Upper bound, defaults to -1 (meaning len(a)).\n" +"\n" +"If x is already in a, insert it to the right of the rightmost x.\n" +"\n" +"Optional args lo and hi bound the slice of a to be searched."); + +#define BISECT_INSORT_RIGHT_METHODDEF \ + {"insort_right", (PyCFunction)bisect_insort_right, METH_FASTCALL, bisect_insort_right__doc__}, + +static PyObject * +bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +bisect_insort_right(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {"OO|nn:insort_right", _keywords, 0}; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + + if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser, + &a, &x, &lo, &hi)) { + goto exit; + } + return_value = bisect_insort_right_impl(module, a, x, lo, hi); + +exit: + return return_value; +} + +PyDoc_STRVAR(bisect_bisect_left__doc__, +"bisect_left($module, /, a, x, lo=0, hi=-1)\n" +"--\n" +"\n" +"Return the index where to insert item x in list a, assuming a is sorted.\n" +"\n" +" a\n" +" The list in which x is to be inserted.\n" +" x\n" +" The value to be inserted.\n" +" lo\n" +" Lower bound, defaults to 0.\n" +" hi\n" +" Upper bound, defaults to -1 (meaning len(a)).\n" +"\n" +"The return value i is such that all e in a[:i] have e < x, and all e in\n" +"a[i:] have e >= x. So if x already appears in the list, i points just\n" +"before the leftmost x already there.\n" +"\n" +"Optional args lo and hi bound the slice of a to be searched."); + +#define BISECT_BISECT_LEFT_METHODDEF \ + {"bisect_left", (PyCFunction)bisect_bisect_left, METH_FASTCALL, bisect_bisect_left__doc__}, + +static PyObject * +bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +bisect_bisect_left(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {"OO|nn:bisect_left", _keywords, 0}; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + + if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser, + &a, &x, &lo, &hi)) { + goto exit; + } + return_value = bisect_bisect_left_impl(module, a, x, lo, hi); + +exit: + return return_value; +} + +PyDoc_STRVAR(bisect_insort_left__doc__, +"insort_left($module, /, a, x, lo=0, hi=-1)\n" +"--\n" +"\n" +"Insert item x in list a, and keep it sorted assuming a is sorted.\n" +"\n" +" a\n" +" The list in which x will be inserted.\n" +" x\n" +" The value to insert.\n" +" lo\n" +" Lower bound, defaults to 0.\n" +" hi\n" +" Upper bound, defaults to -1 (meaning len(a)).\n" +"\n" +"If x is already in a, insert it to the left of the leftmost x.\n" +"\n" +"Optional args lo and hi bound the slice of a to be searched."); + +#define BISECT_INSORT_LEFT_METHODDEF \ + {"insort_left", (PyCFunction)bisect_insort_left, METH_FASTCALL, bisect_insort_left__doc__}, + +static PyObject * +bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x, + Py_ssize_t lo, Py_ssize_t hi); + +static PyObject * +bisect_insort_left(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) +{ + PyObject *return_value = NULL; + static const char * const _keywords[] = {"a", "x", "lo", "hi", NULL}; + static _PyArg_Parser _parser = {"OO|nn:insort_left", _keywords, 0}; + PyObject *a; + PyObject *x; + Py_ssize_t lo = 0; + Py_ssize_t hi = -1; + + if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser, + &a, &x, &lo, &hi)) { + goto exit; + } + return_value = bisect_insort_left_impl(module, a, x, lo, hi); + +exit: + return return_value; +} +/*[clinic end generated code: output=a0f188de312e0db0 input=a9049054013a1b77]*/