bpo-40665: Use Argument Clinic for the bisect module (GH-20163)
This commit is contained in:
parent
e527ec8abe
commit
3a855b26ae
@ -0,0 +1 @@
|
|||||||
|
Convert :mod:`bisect` to use Argument Clinic.
|
@ -6,6 +6,13 @@ Converted to C by Dmitry Vasiliev (dima at hlabs.spb.ru).
|
|||||||
#define PY_SSIZE_T_CLEAN
|
#define PY_SSIZE_T_CLEAN
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
module _bisect
|
||||||
|
[clinic start generated code]*/
|
||||||
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4d56a2b2033b462b]*/
|
||||||
|
|
||||||
|
#include "clinic/_bisectmodule.c.h"
|
||||||
|
|
||||||
_Py_IDENTIFIER(insert);
|
_Py_IDENTIFIER(insert);
|
||||||
|
|
||||||
static inline Py_ssize_t
|
static inline Py_ssize_t
|
||||||
@ -44,69 +51,63 @@ internal_bisect_right(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t
|
|||||||
return lo;
|
return lo;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
/*[clinic input]
|
||||||
bisect_right(PyObject *self, PyObject *args, PyObject *kw)
|
_bisect.bisect_right -> Py_ssize_t
|
||||||
{
|
|
||||||
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 (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
|
a: object
|
||||||
list = PyTuple_GET_ITEM(args, 0);
|
x: object
|
||||||
item = PyTuple_GET_ITEM(args, 1);
|
lo: Py_ssize_t = 0
|
||||||
}
|
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
|
||||||
else {
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_right",
|
Return the index where to insert item x in list a, assuming a is sorted.
|
||||||
keywords, &list, &item, &lo, &hi))
|
|
||||||
return NULL;
|
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
|
||||||
index = internal_bisect_right(list, item, lo, hi);
|
beyond the rightmost x already there
|
||||||
if (index < 0)
|
|
||||||
return NULL;
|
Optional args lo (default 0) and hi (default len(a)) bound the
|
||||||
return PyLong_FromSsize_t(index);
|
slice of a to be searched.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
static Py_ssize_t
|
||||||
|
_bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x,
|
||||||
|
Py_ssize_t lo, Py_ssize_t hi)
|
||||||
|
/*[clinic end generated code: output=419e150cf1d2a235 input=e72212b282c83375]*/
|
||||||
|
{
|
||||||
|
return internal_bisect_right(a, x, lo, hi);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(bisect_right_doc,
|
/*[clinic input]
|
||||||
"bisect_right(a, x[, lo[, hi]]) -> index\n\
|
_bisect.insort_right
|
||||||
\n\
|
|
||||||
Return the index where to insert item x in list a, assuming a is sorted.\n\
|
a: object
|
||||||
\n\
|
x: object
|
||||||
The return value i is such that all e in a[:i] have e <= x, and all e in\n\
|
lo: Py_ssize_t = 0
|
||||||
a[i:] have e > x. So if x already appears in the list, i points just\n\
|
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
|
||||||
beyond the rightmost x already there\n\
|
|
||||||
\n\
|
Insert item x in list a, and keep it sorted assuming a is sorted.
|
||||||
Optional args lo (default 0) and hi (default len(a)) bound the\n\
|
|
||||||
slice of a to be searched.\n");
|
If x is already in a, insert it to the right of the rightmost x.
|
||||||
|
|
||||||
|
Optional args lo (default 0) and hi (default len(a)) bound the
|
||||||
|
slice of a to be searched.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
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=c2caa3d4cd02035a input=d1c45bfa68182669]*/
|
||||||
{
|
{
|
||||||
PyObject *list, *item, *result;
|
PyObject *result;
|
||||||
Py_ssize_t lo = 0;
|
Py_ssize_t index = internal_bisect_right(a, x, lo, hi);
|
||||||
Py_ssize_t hi = -1;
|
|
||||||
Py_ssize_t index;
|
|
||||||
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
|
|
||||||
|
|
||||||
if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
|
|
||||||
list = PyTuple_GET_ITEM(args, 0);
|
|
||||||
item = PyTuple_GET_ITEM(args, 1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_right",
|
|
||||||
keywords, &list, &item, &lo, &hi))
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
index = internal_bisect_right(list, item, lo, hi);
|
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (PyList_CheckExact(list)) {
|
if (PyList_CheckExact(a)) {
|
||||||
if (PyList_Insert(list, index, item) < 0)
|
if (PyList_Insert(a, index, x) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item);
|
result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
@ -115,16 +116,6 @@ insort_right(PyObject *self, PyObject *args, PyObject *kw)
|
|||||||
Py_RETURN_NONE;
|
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 inline Py_ssize_t
|
static inline Py_ssize_t
|
||||||
internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
|
internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t hi)
|
||||||
{
|
{
|
||||||
@ -161,67 +152,64 @@ internal_bisect_left(PyObject *list, PyObject *item, Py_ssize_t lo, Py_ssize_t h
|
|||||||
return lo;
|
return lo;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
|
||||||
bisect_left(PyObject *self, PyObject *args, PyObject *kw)
|
|
||||||
{
|
|
||||||
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 (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
|
/*[clinic input]
|
||||||
list = PyTuple_GET_ITEM(args, 0);
|
_bisect.bisect_left -> Py_ssize_t
|
||||||
item = PyTuple_GET_ITEM(args, 1);
|
|
||||||
}
|
a: object
|
||||||
else {
|
x: object
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:bisect_left",
|
lo: Py_ssize_t = 0
|
||||||
keywords, &list, &item, &lo, &hi))
|
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
|
||||||
return NULL;
|
|
||||||
}
|
Return the index where to insert item x in list a, assuming a is sorted.
|
||||||
index = internal_bisect_left(list, item, lo, hi);
|
|
||||||
if (index < 0)
|
The return value i is such that all e in a[:i] have e < x, and all e in
|
||||||
return NULL;
|
a[i:] have e >= x. So if x already appears in the list, i points just
|
||||||
return PyLong_FromSsize_t(index);
|
before the leftmost x already there.
|
||||||
|
|
||||||
|
Optional args lo (default 0) and hi (default len(a)) bound the
|
||||||
|
slice of a to be searched.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
static Py_ssize_t
|
||||||
|
_bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x,
|
||||||
|
Py_ssize_t lo, Py_ssize_t hi)
|
||||||
|
/*[clinic end generated code: output=af82168bc2856f24 input=2bd90f34afe5609f]*/
|
||||||
|
{
|
||||||
|
return internal_bisect_left(a, x, lo, hi);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(bisect_left_doc,
|
|
||||||
"bisect_left(a, x[, lo[, hi]]) -> index\n\
|
/*[clinic input]
|
||||||
\n\
|
_bisect.insort_left
|
||||||
Return the index where to insert item x in list a, assuming a is sorted.\n\
|
|
||||||
\n\
|
a: object
|
||||||
The return value i is such that all e in a[:i] have e < x, and all e in\n\
|
x: object
|
||||||
a[i:] have e >= x. So if x already appears in the list, i points just\n\
|
lo: Py_ssize_t = 0
|
||||||
before the leftmost x already there.\n\
|
hi: Py_ssize_t(c_default='-1', accept={int, NoneType}) = None
|
||||||
\n\
|
|
||||||
Optional args lo (default 0) and hi (default len(a)) bound the\n\
|
Insert item x in list a, and keep it sorted assuming a is sorted.
|
||||||
slice of a to be searched.\n");
|
|
||||||
|
If x is already in a, insert it to the left of the leftmost x.
|
||||||
|
|
||||||
|
Optional args lo (default 0) and hi (default len(a)) bound the
|
||||||
|
slice of a to be searched.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
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=9e8356c0844a182b input=bc4583308bce00cc]*/
|
||||||
{
|
{
|
||||||
PyObject *list, *item, *result;
|
PyObject *result;
|
||||||
Py_ssize_t lo = 0;
|
Py_ssize_t index = internal_bisect_left(a, x, lo, hi);
|
||||||
Py_ssize_t hi = -1;
|
|
||||||
Py_ssize_t index;
|
|
||||||
static char *keywords[] = {"a", "x", "lo", "hi", NULL};
|
|
||||||
|
|
||||||
if (kw == NULL && PyTuple_GET_SIZE(args) == 2) {
|
|
||||||
list = PyTuple_GET_ITEM(args, 0);
|
|
||||||
item = PyTuple_GET_ITEM(args, 1);
|
|
||||||
} else {
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|nn:insort_left",
|
|
||||||
keywords, &list, &item, &lo, &hi))
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
index = internal_bisect_left(list, item, lo, hi);
|
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (PyList_CheckExact(list)) {
|
if (PyList_CheckExact(a)) {
|
||||||
if (PyList_Insert(list, index, item) < 0)
|
if (PyList_Insert(a, index, x) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
result = _PyObject_CallMethodId(list, &PyId_insert, "nO", index, item);
|
result = _PyObject_CallMethodId(a, &PyId_insert, "nO", index, x);
|
||||||
if (result == NULL)
|
if (result == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
@ -230,25 +218,11 @@ insort_left(PyObject *self, PyObject *args, PyObject *kw)
|
|||||||
Py_RETURN_NONE;
|
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[] = {
|
static PyMethodDef bisect_methods[] = {
|
||||||
{"bisect_right", (PyCFunction)(void(*)(void))bisect_right,
|
_BISECT_BISECT_RIGHT_METHODDEF
|
||||||
METH_VARARGS|METH_KEYWORDS, bisect_right_doc},
|
_BISECT_INSORT_RIGHT_METHODDEF
|
||||||
{"insort_right", (PyCFunction)(void(*)(void))insort_right,
|
_BISECT_BISECT_LEFT_METHODDEF
|
||||||
METH_VARARGS|METH_KEYWORDS, insort_right_doc},
|
_BISECT_INSORT_LEFT_METHODDEF
|
||||||
{"bisect_left", (PyCFunction)(void(*)(void))bisect_left,
|
|
||||||
METH_VARARGS|METH_KEYWORDS, bisect_left_doc},
|
|
||||||
{"insort_left", (PyCFunction)(void(*)(void))insort_left,
|
|
||||||
METH_VARARGS|METH_KEYWORDS, insort_left_doc},
|
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
306
Modules/clinic/_bisectmodule.c.h
generated
Normal file
306
Modules/clinic/_bisectmodule.c.h
generated
Normal file
@ -0,0 +1,306 @@
|
|||||||
|
/*[clinic input]
|
||||||
|
preserve
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
PyDoc_STRVAR(_bisect_bisect_right__doc__,
|
||||||
|
"bisect_right($module, /, a, x, lo=0, hi=None)\n"
|
||||||
|
"--\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.");
|
||||||
|
|
||||||
|
#define _BISECT_BISECT_RIGHT_METHODDEF \
|
||||||
|
{"bisect_right", (PyCFunction)(void(*)(void))_bisect_bisect_right, METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_right__doc__},
|
||||||
|
|
||||||
|
static Py_ssize_t
|
||||||
|
_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 *const *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 = {NULL, _keywords, "bisect_right", 0};
|
||||||
|
PyObject *argsbuf[4];
|
||||||
|
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
|
||||||
|
PyObject *a;
|
||||||
|
PyObject *x;
|
||||||
|
Py_ssize_t lo = 0;
|
||||||
|
Py_ssize_t hi = -1;
|
||||||
|
Py_ssize_t _return_value;
|
||||||
|
|
||||||
|
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
|
||||||
|
if (!args) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
a = args[0];
|
||||||
|
x = args[1];
|
||||||
|
if (!noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
if (args[2]) {
|
||||||
|
if (PyFloat_Check(args[2])) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"integer argument expected, got float" );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Py_ssize_t ival = -1;
|
||||||
|
PyObject *iobj = PyNumber_Index(args[2]);
|
||||||
|
if (iobj != NULL) {
|
||||||
|
ival = PyLong_AsSsize_t(iobj);
|
||||||
|
Py_DECREF(iobj);
|
||||||
|
}
|
||||||
|
if (ival == -1 && PyErr_Occurred()) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
lo = ival;
|
||||||
|
}
|
||||||
|
if (!--noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
skip_optional_pos:
|
||||||
|
_return_value = _bisect_bisect_right_impl(module, a, x, lo, hi);
|
||||||
|
if ((_return_value == -1) && PyErr_Occurred()) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = PyLong_FromSsize_t(_return_value);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(_bisect_insort_right__doc__,
|
||||||
|
"insort_right($module, /, a, x, lo=0, hi=None)\n"
|
||||||
|
"--\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.");
|
||||||
|
|
||||||
|
#define _BISECT_INSORT_RIGHT_METHODDEF \
|
||||||
|
{"insort_right", (PyCFunction)(void(*)(void))_bisect_insort_right, METH_FASTCALL|METH_KEYWORDS, _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 *const *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 = {NULL, _keywords, "insort_right", 0};
|
||||||
|
PyObject *argsbuf[4];
|
||||||
|
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
|
||||||
|
PyObject *a;
|
||||||
|
PyObject *x;
|
||||||
|
Py_ssize_t lo = 0;
|
||||||
|
Py_ssize_t hi = -1;
|
||||||
|
|
||||||
|
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
|
||||||
|
if (!args) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
a = args[0];
|
||||||
|
x = args[1];
|
||||||
|
if (!noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
if (args[2]) {
|
||||||
|
if (PyFloat_Check(args[2])) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"integer argument expected, got float" );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Py_ssize_t ival = -1;
|
||||||
|
PyObject *iobj = PyNumber_Index(args[2]);
|
||||||
|
if (iobj != NULL) {
|
||||||
|
ival = PyLong_AsSsize_t(iobj);
|
||||||
|
Py_DECREF(iobj);
|
||||||
|
}
|
||||||
|
if (ival == -1 && PyErr_Occurred()) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
lo = ival;
|
||||||
|
}
|
||||||
|
if (!--noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
skip_optional_pos:
|
||||||
|
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=None)\n"
|
||||||
|
"--\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.");
|
||||||
|
|
||||||
|
#define _BISECT_BISECT_LEFT_METHODDEF \
|
||||||
|
{"bisect_left", (PyCFunction)(void(*)(void))_bisect_bisect_left, METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_left__doc__},
|
||||||
|
|
||||||
|
static Py_ssize_t
|
||||||
|
_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 *const *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 = {NULL, _keywords, "bisect_left", 0};
|
||||||
|
PyObject *argsbuf[4];
|
||||||
|
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
|
||||||
|
PyObject *a;
|
||||||
|
PyObject *x;
|
||||||
|
Py_ssize_t lo = 0;
|
||||||
|
Py_ssize_t hi = -1;
|
||||||
|
Py_ssize_t _return_value;
|
||||||
|
|
||||||
|
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
|
||||||
|
if (!args) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
a = args[0];
|
||||||
|
x = args[1];
|
||||||
|
if (!noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
if (args[2]) {
|
||||||
|
if (PyFloat_Check(args[2])) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"integer argument expected, got float" );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Py_ssize_t ival = -1;
|
||||||
|
PyObject *iobj = PyNumber_Index(args[2]);
|
||||||
|
if (iobj != NULL) {
|
||||||
|
ival = PyLong_AsSsize_t(iobj);
|
||||||
|
Py_DECREF(iobj);
|
||||||
|
}
|
||||||
|
if (ival == -1 && PyErr_Occurred()) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
lo = ival;
|
||||||
|
}
|
||||||
|
if (!--noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
skip_optional_pos:
|
||||||
|
_return_value = _bisect_bisect_left_impl(module, a, x, lo, hi);
|
||||||
|
if ((_return_value == -1) && PyErr_Occurred()) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = PyLong_FromSsize_t(_return_value);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(_bisect_insort_left__doc__,
|
||||||
|
"insort_left($module, /, a, x, lo=0, hi=None)\n"
|
||||||
|
"--\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.");
|
||||||
|
|
||||||
|
#define _BISECT_INSORT_LEFT_METHODDEF \
|
||||||
|
{"insort_left", (PyCFunction)(void(*)(void))_bisect_insort_left, METH_FASTCALL|METH_KEYWORDS, _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 *const *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 = {NULL, _keywords, "insort_left", 0};
|
||||||
|
PyObject *argsbuf[4];
|
||||||
|
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
|
||||||
|
PyObject *a;
|
||||||
|
PyObject *x;
|
||||||
|
Py_ssize_t lo = 0;
|
||||||
|
Py_ssize_t hi = -1;
|
||||||
|
|
||||||
|
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 4, 0, argsbuf);
|
||||||
|
if (!args) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
a = args[0];
|
||||||
|
x = args[1];
|
||||||
|
if (!noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
if (args[2]) {
|
||||||
|
if (PyFloat_Check(args[2])) {
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"integer argument expected, got float" );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
Py_ssize_t ival = -1;
|
||||||
|
PyObject *iobj = PyNumber_Index(args[2]);
|
||||||
|
if (iobj != NULL) {
|
||||||
|
ival = PyLong_AsSsize_t(iobj);
|
||||||
|
Py_DECREF(iobj);
|
||||||
|
}
|
||||||
|
if (ival == -1 && PyErr_Occurred()) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
lo = ival;
|
||||||
|
}
|
||||||
|
if (!--noptargs) {
|
||||||
|
goto skip_optional_pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
skip_optional_pos:
|
||||||
|
return_value = _bisect_insort_left_impl(module, a, x, lo, hi);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
/*[clinic end generated code: output=bcbd6c77331a08f0 input=a9049054013a1b77]*/
|
Loading…
x
Reference in New Issue
Block a user