Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
Raymond Hettinger | cc061d0 | 2020-11-30 20:42:54 -0800 | [diff] [blame] | 5 | PyDoc_STRVAR(pairwise_new__doc__, |
| 6 | "pairwise(iterable, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Return an iterator of overlapping pairs taken from the input iterator.\n" |
| 10 | "\n" |
| 11 | " s -> (s0,s1), (s1,s2), (s2, s3), ..."); |
| 12 | |
| 13 | static PyObject * |
| 14 | pairwise_new_impl(PyTypeObject *type, PyObject *iterable); |
| 15 | |
| 16 | static PyObject * |
| 17 | pairwise_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 18 | { |
| 19 | PyObject *return_value = NULL; |
| 20 | PyObject *iterable; |
| 21 | |
| 22 | if ((type == &pairwise_type) && |
| 23 | !_PyArg_NoKeywords("pairwise", kwargs)) { |
| 24 | goto exit; |
| 25 | } |
| 26 | if (!_PyArg_CheckPositional("pairwise", PyTuple_GET_SIZE(args), 1, 1)) { |
| 27 | goto exit; |
| 28 | } |
| 29 | iterable = PyTuple_GET_ITEM(args, 0); |
| 30 | return_value = pairwise_new_impl(type, iterable); |
| 31 | |
| 32 | exit: |
| 33 | return return_value; |
| 34 | } |
| 35 | |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 36 | PyDoc_STRVAR(itertools_groupby__doc__, |
| 37 | "groupby(iterable, key=None)\n" |
| 38 | "--\n" |
| 39 | "\n" |
| 40 | "make an iterator that returns consecutive keys and groups from the iterable\n" |
| 41 | "\n" |
| 42 | " iterable\n" |
| 43 | " Elements to divide into groups according to the key function.\n" |
| 44 | " key\n" |
| 45 | " A function for computing the group category for each element.\n" |
| 46 | " If the key function is not specified or is None, the element itself\n" |
| 47 | " is used for grouping."); |
| 48 | |
| 49 | static PyObject * |
| 50 | itertools_groupby_impl(PyTypeObject *type, PyObject *it, PyObject *keyfunc); |
| 51 | |
| 52 | static PyObject * |
| 53 | itertools_groupby(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 54 | { |
| 55 | PyObject *return_value = NULL; |
| 56 | static const char * const _keywords[] = {"iterable", "key", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 57 | static _PyArg_Parser _parser = {NULL, _keywords, "groupby", 0}; |
| 58 | PyObject *argsbuf[2]; |
| 59 | PyObject * const *fastargs; |
| 60 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 61 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 62 | PyObject *it; |
| 63 | PyObject *keyfunc = Py_None; |
| 64 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 65 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 66 | if (!fastargs) { |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 67 | goto exit; |
| 68 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 69 | it = fastargs[0]; |
| 70 | if (!noptargs) { |
| 71 | goto skip_optional_pos; |
| 72 | } |
| 73 | keyfunc = fastargs[1]; |
| 74 | skip_optional_pos: |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 75 | return_value = itertools_groupby_impl(type, it, keyfunc); |
| 76 | |
| 77 | exit: |
| 78 | return return_value; |
| 79 | } |
| 80 | |
| 81 | static PyObject * |
| 82 | itertools__grouper_impl(PyTypeObject *type, PyObject *parent, |
| 83 | PyObject *tgtkey); |
| 84 | |
| 85 | static PyObject * |
| 86 | itertools__grouper(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 87 | { |
| 88 | PyObject *return_value = NULL; |
| 89 | PyObject *parent; |
| 90 | PyObject *tgtkey; |
| 91 | |
| 92 | if ((type == &_grouper_type) && |
| 93 | !_PyArg_NoKeywords("_grouper", kwargs)) { |
| 94 | goto exit; |
| 95 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 96 | if (!_PyArg_CheckPositional("_grouper", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 97 | goto exit; |
| 98 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 99 | if (!PyObject_TypeCheck(PyTuple_GET_ITEM(args, 0), &groupby_type)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 100 | _PyArg_BadArgument("_grouper", "argument 1", (&groupby_type)->tp_name, PyTuple_GET_ITEM(args, 0)); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 101 | goto exit; |
| 102 | } |
| 103 | parent = PyTuple_GET_ITEM(args, 0); |
| 104 | tgtkey = PyTuple_GET_ITEM(args, 1); |
Tal Einat | 3286ce4 | 2018-09-10 21:33:08 +0300 | [diff] [blame] | 105 | return_value = itertools__grouper_impl(type, parent, tgtkey); |
| 106 | |
| 107 | exit: |
| 108 | return return_value; |
| 109 | } |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 110 | |
| 111 | PyDoc_STRVAR(itertools_teedataobject__doc__, |
| 112 | "teedataobject(iterable, values, next, /)\n" |
| 113 | "--\n" |
| 114 | "\n" |
| 115 | "Data container common to multiple tee objects."); |
| 116 | |
| 117 | static PyObject * |
| 118 | itertools_teedataobject_impl(PyTypeObject *type, PyObject *it, |
| 119 | PyObject *values, PyObject *next); |
| 120 | |
| 121 | static PyObject * |
| 122 | itertools_teedataobject(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 123 | { |
| 124 | PyObject *return_value = NULL; |
| 125 | PyObject *it; |
| 126 | PyObject *values; |
| 127 | PyObject *next; |
| 128 | |
| 129 | if ((type == &teedataobject_type) && |
| 130 | !_PyArg_NoKeywords("teedataobject", kwargs)) { |
| 131 | goto exit; |
| 132 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 133 | if (!_PyArg_CheckPositional("teedataobject", PyTuple_GET_SIZE(args), 3, 3)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 134 | goto exit; |
| 135 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 136 | it = PyTuple_GET_ITEM(args, 0); |
| 137 | if (!PyList_Check(PyTuple_GET_ITEM(args, 1))) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 138 | _PyArg_BadArgument("teedataobject", "argument 2", "list", PyTuple_GET_ITEM(args, 1)); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 139 | goto exit; |
| 140 | } |
| 141 | values = PyTuple_GET_ITEM(args, 1); |
| 142 | next = PyTuple_GET_ITEM(args, 2); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 143 | return_value = itertools_teedataobject_impl(type, it, values, next); |
| 144 | |
| 145 | exit: |
| 146 | return return_value; |
| 147 | } |
| 148 | |
| 149 | PyDoc_STRVAR(itertools__tee__doc__, |
| 150 | "_tee(iterable, /)\n" |
| 151 | "--\n" |
| 152 | "\n" |
| 153 | "Iterator wrapped to make it copyable."); |
| 154 | |
| 155 | static PyObject * |
| 156 | itertools__tee_impl(PyTypeObject *type, PyObject *iterable); |
| 157 | |
| 158 | static PyObject * |
| 159 | itertools__tee(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 160 | { |
| 161 | PyObject *return_value = NULL; |
| 162 | PyObject *iterable; |
| 163 | |
| 164 | if ((type == &tee_type) && |
| 165 | !_PyArg_NoKeywords("_tee", kwargs)) { |
| 166 | goto exit; |
| 167 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 168 | if (!_PyArg_CheckPositional("_tee", PyTuple_GET_SIZE(args), 1, 1)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 169 | goto exit; |
| 170 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 171 | iterable = PyTuple_GET_ITEM(args, 0); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 172 | return_value = itertools__tee_impl(type, iterable); |
| 173 | |
| 174 | exit: |
| 175 | return return_value; |
| 176 | } |
| 177 | |
| 178 | PyDoc_STRVAR(itertools_tee__doc__, |
| 179 | "tee($module, iterable, n=2, /)\n" |
| 180 | "--\n" |
| 181 | "\n" |
| 182 | "Returns a tuple of n independent iterators."); |
| 183 | |
| 184 | #define ITERTOOLS_TEE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 185 | {"tee", (PyCFunction)(void(*)(void))itertools_tee, METH_FASTCALL, itertools_tee__doc__}, |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 186 | |
| 187 | static PyObject * |
| 188 | itertools_tee_impl(PyObject *module, PyObject *iterable, Py_ssize_t n); |
| 189 | |
| 190 | static PyObject * |
| 191 | itertools_tee(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
| 192 | { |
| 193 | PyObject *return_value = NULL; |
| 194 | PyObject *iterable; |
| 195 | Py_ssize_t n = 2; |
| 196 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 197 | if (!_PyArg_CheckPositional("tee", nargs, 1, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 198 | goto exit; |
| 199 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 200 | iterable = args[0]; |
| 201 | if (nargs < 2) { |
| 202 | goto skip_optional; |
| 203 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 204 | { |
| 205 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 206 | PyObject *iobj = _PyNumber_Index(args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 207 | if (iobj != NULL) { |
| 208 | ival = PyLong_AsSsize_t(iobj); |
| 209 | Py_DECREF(iobj); |
| 210 | } |
| 211 | if (ival == -1 && PyErr_Occurred()) { |
| 212 | goto exit; |
| 213 | } |
| 214 | n = ival; |
| 215 | } |
| 216 | skip_optional: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 217 | return_value = itertools_tee_impl(module, iterable, n); |
| 218 | |
| 219 | exit: |
| 220 | return return_value; |
| 221 | } |
| 222 | |
| 223 | PyDoc_STRVAR(itertools_cycle__doc__, |
| 224 | "cycle(iterable, /)\n" |
| 225 | "--\n" |
| 226 | "\n" |
| 227 | "Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely."); |
| 228 | |
| 229 | static PyObject * |
| 230 | itertools_cycle_impl(PyTypeObject *type, PyObject *iterable); |
| 231 | |
| 232 | static PyObject * |
| 233 | itertools_cycle(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 234 | { |
| 235 | PyObject *return_value = NULL; |
| 236 | PyObject *iterable; |
| 237 | |
| 238 | if ((type == &cycle_type) && |
| 239 | !_PyArg_NoKeywords("cycle", kwargs)) { |
| 240 | goto exit; |
| 241 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 242 | if (!_PyArg_CheckPositional("cycle", PyTuple_GET_SIZE(args), 1, 1)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 243 | goto exit; |
| 244 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 245 | iterable = PyTuple_GET_ITEM(args, 0); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 246 | return_value = itertools_cycle_impl(type, iterable); |
| 247 | |
| 248 | exit: |
| 249 | return return_value; |
| 250 | } |
| 251 | |
| 252 | PyDoc_STRVAR(itertools_dropwhile__doc__, |
| 253 | "dropwhile(predicate, iterable, /)\n" |
| 254 | "--\n" |
| 255 | "\n" |
| 256 | "Drop items from the iterable while predicate(item) is true.\n" |
| 257 | "\n" |
| 258 | "Afterwards, return every element until the iterable is exhausted."); |
| 259 | |
| 260 | static PyObject * |
| 261 | itertools_dropwhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 262 | |
| 263 | static PyObject * |
| 264 | itertools_dropwhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 265 | { |
| 266 | PyObject *return_value = NULL; |
| 267 | PyObject *func; |
| 268 | PyObject *seq; |
| 269 | |
| 270 | if ((type == &dropwhile_type) && |
| 271 | !_PyArg_NoKeywords("dropwhile", kwargs)) { |
| 272 | goto exit; |
| 273 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 274 | if (!_PyArg_CheckPositional("dropwhile", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 275 | goto exit; |
| 276 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 277 | func = PyTuple_GET_ITEM(args, 0); |
| 278 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 279 | return_value = itertools_dropwhile_impl(type, func, seq); |
| 280 | |
| 281 | exit: |
| 282 | return return_value; |
| 283 | } |
| 284 | |
| 285 | PyDoc_STRVAR(itertools_takewhile__doc__, |
| 286 | "takewhile(predicate, iterable, /)\n" |
| 287 | "--\n" |
| 288 | "\n" |
| 289 | "Return successive entries from an iterable as long as the predicate evaluates to true for each entry."); |
| 290 | |
| 291 | static PyObject * |
| 292 | itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 293 | |
| 294 | static PyObject * |
| 295 | itertools_takewhile(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 296 | { |
| 297 | PyObject *return_value = NULL; |
| 298 | PyObject *func; |
| 299 | PyObject *seq; |
| 300 | |
| 301 | if ((type == &takewhile_type) && |
| 302 | !_PyArg_NoKeywords("takewhile", kwargs)) { |
| 303 | goto exit; |
| 304 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 305 | if (!_PyArg_CheckPositional("takewhile", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 306 | goto exit; |
| 307 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 308 | func = PyTuple_GET_ITEM(args, 0); |
| 309 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 310 | return_value = itertools_takewhile_impl(type, func, seq); |
| 311 | |
| 312 | exit: |
| 313 | return return_value; |
| 314 | } |
| 315 | |
| 316 | PyDoc_STRVAR(itertools_starmap__doc__, |
| 317 | "starmap(function, iterable, /)\n" |
| 318 | "--\n" |
| 319 | "\n" |
| 320 | "Return an iterator whose values are returned from the function evaluated with an argument tuple taken from the given sequence."); |
| 321 | |
| 322 | static PyObject * |
| 323 | itertools_starmap_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 324 | |
| 325 | static PyObject * |
| 326 | itertools_starmap(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 327 | { |
| 328 | PyObject *return_value = NULL; |
| 329 | PyObject *func; |
| 330 | PyObject *seq; |
| 331 | |
| 332 | if ((type == &starmap_type) && |
| 333 | !_PyArg_NoKeywords("starmap", kwargs)) { |
| 334 | goto exit; |
| 335 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 336 | if (!_PyArg_CheckPositional("starmap", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 337 | goto exit; |
| 338 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 339 | func = PyTuple_GET_ITEM(args, 0); |
| 340 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 341 | return_value = itertools_starmap_impl(type, func, seq); |
| 342 | |
| 343 | exit: |
| 344 | return return_value; |
| 345 | } |
| 346 | |
| 347 | PyDoc_STRVAR(itertools_chain_from_iterable__doc__, |
| 348 | "from_iterable($type, iterable, /)\n" |
| 349 | "--\n" |
| 350 | "\n" |
| 351 | "Alternative chain() constructor taking a single iterable argument that evaluates lazily."); |
| 352 | |
| 353 | #define ITERTOOLS_CHAIN_FROM_ITERABLE_METHODDEF \ |
| 354 | {"from_iterable", (PyCFunction)itertools_chain_from_iterable, METH_O|METH_CLASS, itertools_chain_from_iterable__doc__}, |
| 355 | |
| 356 | PyDoc_STRVAR(itertools_combinations__doc__, |
| 357 | "combinations(iterable, r)\n" |
| 358 | "--\n" |
| 359 | "\n" |
| 360 | "Return successive r-length combinations of elements in the iterable.\n" |
| 361 | "\n" |
| 362 | "combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)"); |
| 363 | |
| 364 | static PyObject * |
| 365 | itertools_combinations_impl(PyTypeObject *type, PyObject *iterable, |
| 366 | Py_ssize_t r); |
| 367 | |
| 368 | static PyObject * |
| 369 | itertools_combinations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 370 | { |
| 371 | PyObject *return_value = NULL; |
| 372 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 373 | static _PyArg_Parser _parser = {NULL, _keywords, "combinations", 0}; |
| 374 | PyObject *argsbuf[2]; |
| 375 | PyObject * const *fastargs; |
| 376 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 377 | PyObject *iterable; |
| 378 | Py_ssize_t r; |
| 379 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 380 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 381 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 382 | goto exit; |
| 383 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 384 | iterable = fastargs[0]; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 385 | { |
| 386 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 387 | PyObject *iobj = _PyNumber_Index(fastargs[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 388 | if (iobj != NULL) { |
| 389 | ival = PyLong_AsSsize_t(iobj); |
| 390 | Py_DECREF(iobj); |
| 391 | } |
| 392 | if (ival == -1 && PyErr_Occurred()) { |
| 393 | goto exit; |
| 394 | } |
| 395 | r = ival; |
| 396 | } |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 397 | return_value = itertools_combinations_impl(type, iterable, r); |
| 398 | |
| 399 | exit: |
| 400 | return return_value; |
| 401 | } |
| 402 | |
| 403 | PyDoc_STRVAR(itertools_combinations_with_replacement__doc__, |
| 404 | "combinations_with_replacement(iterable, r)\n" |
| 405 | "--\n" |
| 406 | "\n" |
| 407 | "Return successive r-length combinations of elements in the iterable allowing individual elements to have successive repeats.\n" |
| 408 | "\n" |
DongGeon Lee | ac55cea | 2022-02-15 19:24:04 +0900 | [diff] [blame^] | 409 | "combinations_with_replacement(\'ABC\', 2) --> (\'A\',\'A\'), (\'A\',\'B\'), (\'A\',\'C\'), (\'B\',\'B\'), (\'B\',\'C\'), (\'C\',\'C\')"); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 410 | |
| 411 | static PyObject * |
| 412 | itertools_combinations_with_replacement_impl(PyTypeObject *type, |
| 413 | PyObject *iterable, |
| 414 | Py_ssize_t r); |
| 415 | |
| 416 | static PyObject * |
| 417 | itertools_combinations_with_replacement(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 418 | { |
| 419 | PyObject *return_value = NULL; |
| 420 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 421 | static _PyArg_Parser _parser = {NULL, _keywords, "combinations_with_replacement", 0}; |
| 422 | PyObject *argsbuf[2]; |
| 423 | PyObject * const *fastargs; |
| 424 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 425 | PyObject *iterable; |
| 426 | Py_ssize_t r; |
| 427 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 428 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 429 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 430 | goto exit; |
| 431 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 432 | iterable = fastargs[0]; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 433 | { |
| 434 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame] | 435 | PyObject *iobj = _PyNumber_Index(fastargs[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 436 | if (iobj != NULL) { |
| 437 | ival = PyLong_AsSsize_t(iobj); |
| 438 | Py_DECREF(iobj); |
| 439 | } |
| 440 | if (ival == -1 && PyErr_Occurred()) { |
| 441 | goto exit; |
| 442 | } |
| 443 | r = ival; |
| 444 | } |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 445 | return_value = itertools_combinations_with_replacement_impl(type, iterable, r); |
| 446 | |
| 447 | exit: |
| 448 | return return_value; |
| 449 | } |
| 450 | |
| 451 | PyDoc_STRVAR(itertools_permutations__doc__, |
| 452 | "permutations(iterable, r=None)\n" |
| 453 | "--\n" |
| 454 | "\n" |
| 455 | "Return successive r-length permutations of elements in the iterable.\n" |
| 456 | "\n" |
| 457 | "permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)"); |
| 458 | |
| 459 | static PyObject * |
| 460 | itertools_permutations_impl(PyTypeObject *type, PyObject *iterable, |
| 461 | PyObject *robj); |
| 462 | |
| 463 | static PyObject * |
| 464 | itertools_permutations(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 465 | { |
| 466 | PyObject *return_value = NULL; |
| 467 | static const char * const _keywords[] = {"iterable", "r", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 468 | static _PyArg_Parser _parser = {NULL, _keywords, "permutations", 0}; |
| 469 | PyObject *argsbuf[2]; |
| 470 | PyObject * const *fastargs; |
| 471 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 472 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 473 | PyObject *iterable; |
| 474 | PyObject *robj = Py_None; |
| 475 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 476 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 477 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 478 | goto exit; |
| 479 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 480 | iterable = fastargs[0]; |
| 481 | if (!noptargs) { |
| 482 | goto skip_optional_pos; |
| 483 | } |
| 484 | robj = fastargs[1]; |
| 485 | skip_optional_pos: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 486 | return_value = itertools_permutations_impl(type, iterable, robj); |
| 487 | |
| 488 | exit: |
| 489 | return return_value; |
| 490 | } |
| 491 | |
| 492 | PyDoc_STRVAR(itertools_accumulate__doc__, |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 493 | "accumulate(iterable, func=None, *, initial=None)\n" |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 494 | "--\n" |
| 495 | "\n" |
| 496 | "Return series of accumulated sums (or other binary function results)."); |
| 497 | |
| 498 | static PyObject * |
| 499 | itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable, |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 500 | PyObject *binop, PyObject *initial); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 501 | |
| 502 | static PyObject * |
| 503 | itertools_accumulate(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 504 | { |
| 505 | PyObject *return_value = NULL; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 506 | static const char * const _keywords[] = {"iterable", "func", "initial", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 507 | static _PyArg_Parser _parser = {NULL, _keywords, "accumulate", 0}; |
| 508 | PyObject *argsbuf[3]; |
| 509 | PyObject * const *fastargs; |
| 510 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 511 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 512 | PyObject *iterable; |
| 513 | PyObject *binop = Py_None; |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 514 | PyObject *initial = Py_None; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 515 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 516 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf); |
| 517 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 518 | goto exit; |
| 519 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 520 | iterable = fastargs[0]; |
| 521 | if (!noptargs) { |
| 522 | goto skip_optional_pos; |
| 523 | } |
| 524 | if (fastargs[1]) { |
| 525 | binop = fastargs[1]; |
| 526 | if (!--noptargs) { |
| 527 | goto skip_optional_pos; |
| 528 | } |
| 529 | } |
| 530 | skip_optional_pos: |
| 531 | if (!noptargs) { |
| 532 | goto skip_optional_kwonly; |
| 533 | } |
| 534 | initial = fastargs[2]; |
| 535 | skip_optional_kwonly: |
Lisa Roach | 9718b59 | 2018-09-23 17:34:59 -0700 | [diff] [blame] | 536 | return_value = itertools_accumulate_impl(type, iterable, binop, initial); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 537 | |
| 538 | exit: |
| 539 | return return_value; |
| 540 | } |
| 541 | |
| 542 | PyDoc_STRVAR(itertools_compress__doc__, |
| 543 | "compress(data, selectors)\n" |
| 544 | "--\n" |
| 545 | "\n" |
| 546 | "Return data elements corresponding to true selector elements.\n" |
| 547 | "\n" |
| 548 | "Forms a shorter iterator from selected data elements using the selectors to\n" |
| 549 | "choose the data elements."); |
| 550 | |
| 551 | static PyObject * |
| 552 | itertools_compress_impl(PyTypeObject *type, PyObject *seq1, PyObject *seq2); |
| 553 | |
| 554 | static PyObject * |
| 555 | itertools_compress(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 556 | { |
| 557 | PyObject *return_value = NULL; |
| 558 | static const char * const _keywords[] = {"data", "selectors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 559 | static _PyArg_Parser _parser = {NULL, _keywords, "compress", 0}; |
| 560 | PyObject *argsbuf[2]; |
| 561 | PyObject * const *fastargs; |
| 562 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 563 | PyObject *seq1; |
| 564 | PyObject *seq2; |
| 565 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 566 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 2, 0, argsbuf); |
| 567 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 568 | goto exit; |
| 569 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 570 | seq1 = fastargs[0]; |
| 571 | seq2 = fastargs[1]; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 572 | return_value = itertools_compress_impl(type, seq1, seq2); |
| 573 | |
| 574 | exit: |
| 575 | return return_value; |
| 576 | } |
| 577 | |
| 578 | PyDoc_STRVAR(itertools_filterfalse__doc__, |
| 579 | "filterfalse(function, iterable, /)\n" |
| 580 | "--\n" |
| 581 | "\n" |
| 582 | "Return those items of iterable for which function(item) is false.\n" |
| 583 | "\n" |
| 584 | "If function is None, return the items that are false."); |
| 585 | |
| 586 | static PyObject * |
| 587 | itertools_filterfalse_impl(PyTypeObject *type, PyObject *func, PyObject *seq); |
| 588 | |
| 589 | static PyObject * |
| 590 | itertools_filterfalse(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 591 | { |
| 592 | PyObject *return_value = NULL; |
| 593 | PyObject *func; |
| 594 | PyObject *seq; |
| 595 | |
| 596 | if ((type == &filterfalse_type) && |
| 597 | !_PyArg_NoKeywords("filterfalse", kwargs)) { |
| 598 | goto exit; |
| 599 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 600 | if (!_PyArg_CheckPositional("filterfalse", PyTuple_GET_SIZE(args), 2, 2)) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 601 | goto exit; |
| 602 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 603 | func = PyTuple_GET_ITEM(args, 0); |
| 604 | seq = PyTuple_GET_ITEM(args, 1); |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 605 | return_value = itertools_filterfalse_impl(type, func, seq); |
| 606 | |
| 607 | exit: |
| 608 | return return_value; |
| 609 | } |
| 610 | |
| 611 | PyDoc_STRVAR(itertools_count__doc__, |
| 612 | "count(start=0, step=1)\n" |
| 613 | "--\n" |
| 614 | "\n" |
| 615 | "Return a count object whose .__next__() method returns consecutive values.\n" |
| 616 | "\n" |
| 617 | "Equivalent to:\n" |
| 618 | " def count(firstval=0, step=1):\n" |
| 619 | " x = firstval\n" |
| 620 | " while 1:\n" |
| 621 | " yield x\n" |
| 622 | " x += step"); |
| 623 | |
| 624 | static PyObject * |
| 625 | itertools_count_impl(PyTypeObject *type, PyObject *long_cnt, |
| 626 | PyObject *long_step); |
| 627 | |
| 628 | static PyObject * |
| 629 | itertools_count(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 630 | { |
| 631 | PyObject *return_value = NULL; |
| 632 | static const char * const _keywords[] = {"start", "step", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 633 | static _PyArg_Parser _parser = {NULL, _keywords, "count", 0}; |
| 634 | PyObject *argsbuf[2]; |
| 635 | PyObject * const *fastargs; |
| 636 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 637 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 638 | PyObject *long_cnt = NULL; |
| 639 | PyObject *long_step = NULL; |
| 640 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 641 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 2, 0, argsbuf); |
| 642 | if (!fastargs) { |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 643 | goto exit; |
| 644 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 645 | if (!noptargs) { |
| 646 | goto skip_optional_pos; |
| 647 | } |
| 648 | if (fastargs[0]) { |
| 649 | long_cnt = fastargs[0]; |
| 650 | if (!--noptargs) { |
| 651 | goto skip_optional_pos; |
| 652 | } |
| 653 | } |
| 654 | long_step = fastargs[1]; |
| 655 | skip_optional_pos: |
Tal Einat | c4bccd3 | 2018-09-12 00:49:13 +0300 | [diff] [blame] | 656 | return_value = itertools_count_impl(type, long_cnt, long_step); |
| 657 | |
| 658 | exit: |
| 659 | return return_value; |
| 660 | } |
DongGeon Lee | ac55cea | 2022-02-15 19:24:04 +0900 | [diff] [blame^] | 661 | /*[clinic end generated code: output=c91f57481a2461d3 input=a9049054013a1b77]*/ |