| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 6 |
# include "pycore_gc.h" // PyGC_Head |
| 7 |
# include "pycore_runtime.h" // _Py_ID() |
| 8 |
#endif |
| 9 |
#include "pycore_abstract.h" // _PyNumber_Index() |
| 10 |
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
| 11 |
|
| 12 |
PyDoc_STRVAR(_bisect_bisect_right__doc__, |
| 13 |
"bisect_right($module, /, a, x, lo=0, hi=None, *, key=None)\n" |
| 14 |
"--\n" |
| 15 |
"\n" |
| 16 |
"Return the index where to insert item x in list a, assuming a is sorted.\n" |
| 17 |
"\n" |
| 18 |
"The return value i is such that all e in a[:i] have e <= x, and all e in\n" |
| 19 |
"a[i:] have e > x. So if x already appears in the list, a.insert(i, x)\n" |
| 20 |
"will insert just after the rightmost x already there.\n" |
| 21 |
"\n" |
| 22 |
"Optional args lo (default 0) and hi (default len(a)) bound the\n" |
| 23 |
"slice of a to be searched.\n" |
| 24 |
"\n" |
| 25 |
"A custom key function can be supplied to customize the sort order."); |
| 26 |
|
| 27 |
#define _BISECT_BISECT_RIGHT_METHODDEF \ |
| 28 |
{"bisect_right", _PyCFunction_CAST(_bisect_bisect_right), METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_right__doc__}, |
| 29 |
|
| 30 |
static Py_ssize_t |
| 31 |
_bisect_bisect_right_impl(PyObject *module, PyObject *a, PyObject *x, |
| 32 |
Py_ssize_t lo, Py_ssize_t hi, PyObject *key); |
| 33 |
|
| 34 |
static PyObject * |
| 35 |
_bisect_bisect_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 36 |
{ |
| 37 |
PyObject *return_value = NULL; |
| 38 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 39 |
|
| 40 |
#define NUM_KEYWORDS 5 |
| 41 |
static struct { |
| 42 |
PyGC_Head _this_is_not_used; |
| 43 |
PyObject_VAR_HEAD |
| 44 |
Py_hash_t ob_hash; |
| 45 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 46 |
} _kwtuple = { |
| 47 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 48 |
.ob_hash = -1, |
| 49 |
.ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), }, |
| 50 |
}; |
| 51 |
#undef NUM_KEYWORDS |
| 52 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 53 |
|
| 54 |
#else // !Py_BUILD_CORE |
| 55 |
# define KWTUPLE NULL |
| 56 |
#endif // !Py_BUILD_CORE |
| 57 |
|
| 58 |
static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL}; |
| 59 |
static _PyArg_Parser _parser = { |
| 60 |
.keywords = _keywords, |
| 61 |
.fname = "bisect_right", |
| 62 |
.kwtuple = KWTUPLE, |
| 63 |
}; |
| 64 |
#undef KWTUPLE |
| 65 |
PyObject *argsbuf[5]; |
| 66 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
| 67 |
PyObject *a; |
| 68 |
PyObject *x; |
| 69 |
Py_ssize_t lo = 0; |
| 70 |
Py_ssize_t hi = -1; |
| 71 |
PyObject *key = Py_None; |
| 72 |
Py_ssize_t _return_value; |
| 73 |
|
| 74 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 75 |
/*minpos*/ 2, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 76 |
if (!args) { |
| 77 |
goto exit; |
| 78 |
} |
| 79 |
a = args[0]; |
| 80 |
x = args[1]; |
| 81 |
if (!noptargs) { |
| 82 |
goto skip_optional_pos; |
| 83 |
} |
| 84 |
if (args[2]) { |
| 85 |
{ |
| 86 |
Py_ssize_t ival = -1; |
| 87 |
PyObject *iobj = _PyNumber_Index(args[2]); |
| 88 |
if (iobj != NULL) { |
| 89 |
ival = PyLong_AsSsize_t(iobj); |
| 90 |
Py_DECREF(iobj); |
| 91 |
} |
| 92 |
if (ival == -1 && PyErr_Occurred()) { |
| 93 |
goto exit; |
| 94 |
} |
| 95 |
lo = ival; |
| 96 |
} |
| 97 |
if (!--noptargs) { |
| 98 |
goto skip_optional_pos; |
| 99 |
} |
| 100 |
} |
| 101 |
if (args[3]) { |
| 102 |
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { |
| 103 |
goto exit; |
| 104 |
} |
| 105 |
if (!--noptargs) { |
| 106 |
goto skip_optional_pos; |
| 107 |
} |
| 108 |
} |
| 109 |
skip_optional_pos: |
| 110 |
if (!noptargs) { |
| 111 |
goto skip_optional_kwonly; |
| 112 |
} |
| 113 |
key = args[4]; |
| 114 |
skip_optional_kwonly: |
| 115 |
_return_value = _bisect_bisect_right_impl(module, a, x, lo, hi, key); |
| 116 |
if ((_return_value == -1) && PyErr_Occurred()) { |
| 117 |
goto exit; |
| 118 |
} |
| 119 |
return_value = PyLong_FromSsize_t(_return_value); |
| 120 |
|
| 121 |
exit: |
| 122 |
return return_value; |
| 123 |
} |
| 124 |
|
| 125 |
PyDoc_STRVAR(_bisect_insort_right__doc__, |
| 126 |
"insort_right($module, /, a, x, lo=0, hi=None, *, key=None)\n" |
| 127 |
"--\n" |
| 128 |
"\n" |
| 129 |
"Insert item x in list a, and keep it sorted assuming a is sorted.\n" |
| 130 |
"\n" |
| 131 |
"If x is already in a, insert it to the right of the rightmost x.\n" |
| 132 |
"\n" |
| 133 |
"Optional args lo (default 0) and hi (default len(a)) bound the\n" |
| 134 |
"slice of a to be searched.\n" |
| 135 |
"\n" |
| 136 |
"A custom key function can be supplied to customize the sort order."); |
| 137 |
|
| 138 |
#define _BISECT_INSORT_RIGHT_METHODDEF \ |
| 139 |
{"insort_right", _PyCFunction_CAST(_bisect_insort_right), METH_FASTCALL|METH_KEYWORDS, _bisect_insort_right__doc__}, |
| 140 |
|
| 141 |
static PyObject * |
| 142 |
_bisect_insort_right_impl(PyObject *module, PyObject *a, PyObject *x, |
| 143 |
Py_ssize_t lo, Py_ssize_t hi, PyObject *key); |
| 144 |
|
| 145 |
static PyObject * |
| 146 |
_bisect_insort_right(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 147 |
{ |
| 148 |
PyObject *return_value = NULL; |
| 149 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 150 |
|
| 151 |
#define NUM_KEYWORDS 5 |
| 152 |
static struct { |
| 153 |
PyGC_Head _this_is_not_used; |
| 154 |
PyObject_VAR_HEAD |
| 155 |
Py_hash_t ob_hash; |
| 156 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 157 |
} _kwtuple = { |
| 158 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 159 |
.ob_hash = -1, |
| 160 |
.ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), }, |
| 161 |
}; |
| 162 |
#undef NUM_KEYWORDS |
| 163 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 164 |
|
| 165 |
#else // !Py_BUILD_CORE |
| 166 |
# define KWTUPLE NULL |
| 167 |
#endif // !Py_BUILD_CORE |
| 168 |
|
| 169 |
static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL}; |
| 170 |
static _PyArg_Parser _parser = { |
| 171 |
.keywords = _keywords, |
| 172 |
.fname = "insort_right", |
| 173 |
.kwtuple = KWTUPLE, |
| 174 |
}; |
| 175 |
#undef KWTUPLE |
| 176 |
PyObject *argsbuf[5]; |
| 177 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
| 178 |
PyObject *a; |
| 179 |
PyObject *x; |
| 180 |
Py_ssize_t lo = 0; |
| 181 |
Py_ssize_t hi = -1; |
| 182 |
PyObject *key = Py_None; |
| 183 |
|
| 184 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 185 |
/*minpos*/ 2, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 186 |
if (!args) { |
| 187 |
goto exit; |
| 188 |
} |
| 189 |
a = args[0]; |
| 190 |
x = args[1]; |
| 191 |
if (!noptargs) { |
| 192 |
goto skip_optional_pos; |
| 193 |
} |
| 194 |
if (args[2]) { |
| 195 |
{ |
| 196 |
Py_ssize_t ival = -1; |
| 197 |
PyObject *iobj = _PyNumber_Index(args[2]); |
| 198 |
if (iobj != NULL) { |
| 199 |
ival = PyLong_AsSsize_t(iobj); |
| 200 |
Py_DECREF(iobj); |
| 201 |
} |
| 202 |
if (ival == -1 && PyErr_Occurred()) { |
| 203 |
goto exit; |
| 204 |
} |
| 205 |
lo = ival; |
| 206 |
} |
| 207 |
if (!--noptargs) { |
| 208 |
goto skip_optional_pos; |
| 209 |
} |
| 210 |
} |
| 211 |
if (args[3]) { |
| 212 |
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { |
| 213 |
goto exit; |
| 214 |
} |
| 215 |
if (!--noptargs) { |
| 216 |
goto skip_optional_pos; |
| 217 |
} |
| 218 |
} |
| 219 |
skip_optional_pos: |
| 220 |
if (!noptargs) { |
| 221 |
goto skip_optional_kwonly; |
| 222 |
} |
| 223 |
key = args[4]; |
| 224 |
skip_optional_kwonly: |
| 225 |
return_value = _bisect_insort_right_impl(module, a, x, lo, hi, key); |
| 226 |
|
| 227 |
exit: |
| 228 |
return return_value; |
| 229 |
} |
| 230 |
|
| 231 |
PyDoc_STRVAR(_bisect_bisect_left__doc__, |
| 232 |
"bisect_left($module, /, a, x, lo=0, hi=None, *, key=None)\n" |
| 233 |
"--\n" |
| 234 |
"\n" |
| 235 |
"Return the index where to insert item x in list a, assuming a is sorted.\n" |
| 236 |
"\n" |
| 237 |
"The return value i is such that all e in a[:i] have e < x, and all e in\n" |
| 238 |
"a[i:] have e >= x. So if x already appears in the list, a.insert(i, x)\n" |
| 239 |
"will insert just before the leftmost x already there.\n" |
| 240 |
"\n" |
| 241 |
"Optional args lo (default 0) and hi (default len(a)) bound the\n" |
| 242 |
"slice of a to be searched.\n" |
| 243 |
"\n" |
| 244 |
"A custom key function can be supplied to customize the sort order."); |
| 245 |
|
| 246 |
#define _BISECT_BISECT_LEFT_METHODDEF \ |
| 247 |
{"bisect_left", _PyCFunction_CAST(_bisect_bisect_left), METH_FASTCALL|METH_KEYWORDS, _bisect_bisect_left__doc__}, |
| 248 |
|
| 249 |
static Py_ssize_t |
| 250 |
_bisect_bisect_left_impl(PyObject *module, PyObject *a, PyObject *x, |
| 251 |
Py_ssize_t lo, Py_ssize_t hi, PyObject *key); |
| 252 |
|
| 253 |
static PyObject * |
| 254 |
_bisect_bisect_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 255 |
{ |
| 256 |
PyObject *return_value = NULL; |
| 257 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 258 |
|
| 259 |
#define NUM_KEYWORDS 5 |
| 260 |
static struct { |
| 261 |
PyGC_Head _this_is_not_used; |
| 262 |
PyObject_VAR_HEAD |
| 263 |
Py_hash_t ob_hash; |
| 264 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 265 |
} _kwtuple = { |
| 266 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 267 |
.ob_hash = -1, |
| 268 |
.ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), }, |
| 269 |
}; |
| 270 |
#undef NUM_KEYWORDS |
| 271 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 272 |
|
| 273 |
#else // !Py_BUILD_CORE |
| 274 |
# define KWTUPLE NULL |
| 275 |
#endif // !Py_BUILD_CORE |
| 276 |
|
| 277 |
static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL}; |
| 278 |
static _PyArg_Parser _parser = { |
| 279 |
.keywords = _keywords, |
| 280 |
.fname = "bisect_left", |
| 281 |
.kwtuple = KWTUPLE, |
| 282 |
}; |
| 283 |
#undef KWTUPLE |
| 284 |
PyObject *argsbuf[5]; |
| 285 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
| 286 |
PyObject *a; |
| 287 |
PyObject *x; |
| 288 |
Py_ssize_t lo = 0; |
| 289 |
Py_ssize_t hi = -1; |
| 290 |
PyObject *key = Py_None; |
| 291 |
Py_ssize_t _return_value; |
| 292 |
|
| 293 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 294 |
/*minpos*/ 2, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 295 |
if (!args) { |
| 296 |
goto exit; |
| 297 |
} |
| 298 |
a = args[0]; |
| 299 |
x = args[1]; |
| 300 |
if (!noptargs) { |
| 301 |
goto skip_optional_pos; |
| 302 |
} |
| 303 |
if (args[2]) { |
| 304 |
{ |
| 305 |
Py_ssize_t ival = -1; |
| 306 |
PyObject *iobj = _PyNumber_Index(args[2]); |
| 307 |
if (iobj != NULL) { |
| 308 |
ival = PyLong_AsSsize_t(iobj); |
| 309 |
Py_DECREF(iobj); |
| 310 |
} |
| 311 |
if (ival == -1 && PyErr_Occurred()) { |
| 312 |
goto exit; |
| 313 |
} |
| 314 |
lo = ival; |
| 315 |
} |
| 316 |
if (!--noptargs) { |
| 317 |
goto skip_optional_pos; |
| 318 |
} |
| 319 |
} |
| 320 |
if (args[3]) { |
| 321 |
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { |
| 322 |
goto exit; |
| 323 |
} |
| 324 |
if (!--noptargs) { |
| 325 |
goto skip_optional_pos; |
| 326 |
} |
| 327 |
} |
| 328 |
skip_optional_pos: |
| 329 |
if (!noptargs) { |
| 330 |
goto skip_optional_kwonly; |
| 331 |
} |
| 332 |
key = args[4]; |
| 333 |
skip_optional_kwonly: |
| 334 |
_return_value = _bisect_bisect_left_impl(module, a, x, lo, hi, key); |
| 335 |
if ((_return_value == -1) && PyErr_Occurred()) { |
| 336 |
goto exit; |
| 337 |
} |
| 338 |
return_value = PyLong_FromSsize_t(_return_value); |
| 339 |
|
| 340 |
exit: |
| 341 |
return return_value; |
| 342 |
} |
| 343 |
|
| 344 |
PyDoc_STRVAR(_bisect_insort_left__doc__, |
| 345 |
"insort_left($module, /, a, x, lo=0, hi=None, *, key=None)\n" |
| 346 |
"--\n" |
| 347 |
"\n" |
| 348 |
"Insert item x in list a, and keep it sorted assuming a is sorted.\n" |
| 349 |
"\n" |
| 350 |
"If x is already in a, insert it to the left of the leftmost x.\n" |
| 351 |
"\n" |
| 352 |
"Optional args lo (default 0) and hi (default len(a)) bound the\n" |
| 353 |
"slice of a to be searched.\n" |
| 354 |
"\n" |
| 355 |
"A custom key function can be supplied to customize the sort order."); |
| 356 |
|
| 357 |
#define _BISECT_INSORT_LEFT_METHODDEF \ |
| 358 |
{"insort_left", _PyCFunction_CAST(_bisect_insort_left), METH_FASTCALL|METH_KEYWORDS, _bisect_insort_left__doc__}, |
| 359 |
|
| 360 |
static PyObject * |
| 361 |
_bisect_insort_left_impl(PyObject *module, PyObject *a, PyObject *x, |
| 362 |
Py_ssize_t lo, Py_ssize_t hi, PyObject *key); |
| 363 |
|
| 364 |
static PyObject * |
| 365 |
_bisect_insort_left(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 366 |
{ |
| 367 |
PyObject *return_value = NULL; |
| 368 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 369 |
|
| 370 |
#define NUM_KEYWORDS 5 |
| 371 |
static struct { |
| 372 |
PyGC_Head _this_is_not_used; |
| 373 |
PyObject_VAR_HEAD |
| 374 |
Py_hash_t ob_hash; |
| 375 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 376 |
} _kwtuple = { |
| 377 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 378 |
.ob_hash = -1, |
| 379 |
.ob_item = { _Py_LATIN1_CHR('a'), _Py_LATIN1_CHR('x'), &_Py_ID(lo), &_Py_ID(hi), &_Py_ID(key), }, |
| 380 |
}; |
| 381 |
#undef NUM_KEYWORDS |
| 382 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 383 |
|
| 384 |
#else // !Py_BUILD_CORE |
| 385 |
# define KWTUPLE NULL |
| 386 |
#endif // !Py_BUILD_CORE |
| 387 |
|
| 388 |
static const char * const _keywords[] = {"a", "x", "lo", "hi", "key", NULL}; |
| 389 |
static _PyArg_Parser _parser = { |
| 390 |
.keywords = _keywords, |
| 391 |
.fname = "insort_left", |
| 392 |
.kwtuple = KWTUPLE, |
| 393 |
}; |
| 394 |
#undef KWTUPLE |
| 395 |
PyObject *argsbuf[5]; |
| 396 |
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
| 397 |
PyObject *a; |
| 398 |
PyObject *x; |
| 399 |
Py_ssize_t lo = 0; |
| 400 |
Py_ssize_t hi = -1; |
| 401 |
PyObject *key = Py_None; |
| 402 |
|
| 403 |
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, |
| 404 |
/*minpos*/ 2, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 405 |
if (!args) { |
| 406 |
goto exit; |
| 407 |
} |
| 408 |
a = args[0]; |
| 409 |
x = args[1]; |
| 410 |
if (!noptargs) { |
| 411 |
goto skip_optional_pos; |
| 412 |
} |
| 413 |
if (args[2]) { |
| 414 |
{ |
| 415 |
Py_ssize_t ival = -1; |
| 416 |
PyObject *iobj = _PyNumber_Index(args[2]); |
| 417 |
if (iobj != NULL) { |
| 418 |
ival = PyLong_AsSsize_t(iobj); |
| 419 |
Py_DECREF(iobj); |
| 420 |
} |
| 421 |
if (ival == -1 && PyErr_Occurred()) { |
| 422 |
goto exit; |
| 423 |
} |
| 424 |
lo = ival; |
| 425 |
} |
| 426 |
if (!--noptargs) { |
| 427 |
goto skip_optional_pos; |
| 428 |
} |
| 429 |
} |
| 430 |
if (args[3]) { |
| 431 |
if (!_Py_convert_optional_to_ssize_t(args[3], &hi)) { |
| 432 |
goto exit; |
| 433 |
} |
| 434 |
if (!--noptargs) { |
| 435 |
goto skip_optional_pos; |
| 436 |
} |
| 437 |
} |
| 438 |
skip_optional_pos: |
| 439 |
if (!noptargs) { |
| 440 |
goto skip_optional_kwonly; |
| 441 |
} |
| 442 |
key = args[4]; |
| 443 |
skip_optional_kwonly: |
| 444 |
return_value = _bisect_insort_left_impl(module, a, x, lo, hi, key); |
| 445 |
|
| 446 |
exit: |
| 447 |
return return_value; |
| 448 |
} |
| 449 |
/*[clinic end generated code: output=5e0b8c4e5e697eae input=a9049054013a1b77]*/ |