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