Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
| 3 | |
| 4 | /* Itertools module written and maintained |
| 5 | by Raymond D. Hettinger <python@rcn.com> |
| 6 | Copyright (c) 2003 Python Software Foundation. |
| 7 | All rights reserved. |
| 8 | */ |
| 9 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 10 | |
| 11 | /* groupby object ***********************************************************/ |
| 12 | |
| 13 | typedef struct { |
| 14 | PyObject_HEAD |
| 15 | PyObject *it; |
| 16 | PyObject *keyfunc; |
| 17 | PyObject *tgtkey; |
| 18 | PyObject *currkey; |
| 19 | PyObject *currvalue; |
| 20 | } groupbyobject; |
| 21 | |
| 22 | static PyTypeObject groupby_type; |
| 23 | static PyObject *_grouper_create(groupbyobject *, PyObject *); |
| 24 | |
| 25 | static PyObject * |
| 26 | groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 27 | { |
| 28 | static char *kwargs[] = {"iterable", "key", NULL}; |
| 29 | groupbyobject *gbo; |
| 30 | PyObject *it, *keyfunc = Py_None; |
| 31 | |
| 32 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs, |
| 33 | &it, &keyfunc)) |
| 34 | return NULL; |
| 35 | |
| 36 | gbo = (groupbyobject *)type->tp_alloc(type, 0); |
| 37 | if (gbo == NULL) |
| 38 | return NULL; |
| 39 | gbo->tgtkey = NULL; |
| 40 | gbo->currkey = NULL; |
| 41 | gbo->currvalue = NULL; |
| 42 | gbo->keyfunc = keyfunc; |
| 43 | Py_INCREF(keyfunc); |
| 44 | gbo->it = PyObject_GetIter(it); |
| 45 | if (gbo->it == NULL) { |
| 46 | Py_DECREF(gbo); |
| 47 | return NULL; |
| 48 | } |
| 49 | return (PyObject *)gbo; |
| 50 | } |
| 51 | |
| 52 | static void |
| 53 | groupby_dealloc(groupbyobject *gbo) |
| 54 | { |
| 55 | PyObject_GC_UnTrack(gbo); |
| 56 | Py_XDECREF(gbo->it); |
| 57 | Py_XDECREF(gbo->keyfunc); |
| 58 | Py_XDECREF(gbo->tgtkey); |
| 59 | Py_XDECREF(gbo->currkey); |
| 60 | Py_XDECREF(gbo->currvalue); |
| 61 | gbo->ob_type->tp_free(gbo); |
| 62 | } |
| 63 | |
| 64 | static int |
| 65 | groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg) |
| 66 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 67 | Py_VISIT(gbo->it); |
| 68 | Py_VISIT(gbo->keyfunc); |
| 69 | Py_VISIT(gbo->tgtkey); |
| 70 | Py_VISIT(gbo->currkey); |
| 71 | Py_VISIT(gbo->currvalue); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static PyObject * |
| 76 | groupby_next(groupbyobject *gbo) |
| 77 | { |
| 78 | PyObject *newvalue, *newkey, *r, *grouper; |
| 79 | |
| 80 | /* skip to next iteration group */ |
| 81 | for (;;) { |
| 82 | if (gbo->currkey == NULL) |
| 83 | /* pass */; |
| 84 | else if (gbo->tgtkey == NULL) |
| 85 | break; |
| 86 | else { |
| 87 | int rcmp; |
| 88 | |
| 89 | rcmp = PyObject_RichCompareBool(gbo->tgtkey, |
| 90 | gbo->currkey, Py_EQ); |
| 91 | if (rcmp == -1) |
| 92 | return NULL; |
| 93 | else if (rcmp == 0) |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | newvalue = PyIter_Next(gbo->it); |
| 98 | if (newvalue == NULL) |
| 99 | return NULL; |
| 100 | |
| 101 | if (gbo->keyfunc == Py_None) { |
| 102 | newkey = newvalue; |
| 103 | Py_INCREF(newvalue); |
| 104 | } else { |
| 105 | newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
| 106 | newvalue, NULL); |
| 107 | if (newkey == NULL) { |
| 108 | Py_DECREF(newvalue); |
| 109 | return NULL; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | Py_XDECREF(gbo->currkey); |
| 114 | gbo->currkey = newkey; |
| 115 | Py_XDECREF(gbo->currvalue); |
| 116 | gbo->currvalue = newvalue; |
| 117 | } |
| 118 | |
| 119 | Py_XDECREF(gbo->tgtkey); |
| 120 | gbo->tgtkey = gbo->currkey; |
| 121 | Py_INCREF(gbo->currkey); |
| 122 | |
| 123 | grouper = _grouper_create(gbo, gbo->tgtkey); |
| 124 | if (grouper == NULL) |
| 125 | return NULL; |
| 126 | |
| 127 | r = PyTuple_Pack(2, gbo->currkey, grouper); |
| 128 | Py_DECREF(grouper); |
| 129 | return r; |
| 130 | } |
| 131 | |
| 132 | PyDoc_STRVAR(groupby_doc, |
| 133 | "groupby(iterable[, keyfunc]) -> create an iterator which returns\n\ |
| 134 | (key, sub-iterator) grouped by each value of key(value).\n"); |
| 135 | |
| 136 | static PyTypeObject groupby_type = { |
| 137 | PyObject_HEAD_INIT(NULL) |
| 138 | 0, /* ob_size */ |
| 139 | "itertools.groupby", /* tp_name */ |
| 140 | sizeof(groupbyobject), /* tp_basicsize */ |
| 141 | 0, /* tp_itemsize */ |
| 142 | /* methods */ |
| 143 | (destructor)groupby_dealloc, /* tp_dealloc */ |
| 144 | 0, /* tp_print */ |
| 145 | 0, /* tp_getattr */ |
| 146 | 0, /* tp_setattr */ |
| 147 | 0, /* tp_compare */ |
| 148 | 0, /* tp_repr */ |
| 149 | 0, /* tp_as_number */ |
| 150 | 0, /* tp_as_sequence */ |
| 151 | 0, /* tp_as_mapping */ |
| 152 | 0, /* tp_hash */ |
| 153 | 0, /* tp_call */ |
| 154 | 0, /* tp_str */ |
| 155 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 156 | 0, /* tp_setattro */ |
| 157 | 0, /* tp_as_buffer */ |
| 158 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 159 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 160 | groupby_doc, /* tp_doc */ |
| 161 | (traverseproc)groupby_traverse, /* tp_traverse */ |
| 162 | 0, /* tp_clear */ |
| 163 | 0, /* tp_richcompare */ |
| 164 | 0, /* tp_weaklistoffset */ |
| 165 | PyObject_SelfIter, /* tp_iter */ |
| 166 | (iternextfunc)groupby_next, /* tp_iternext */ |
| 167 | 0, /* tp_methods */ |
| 168 | 0, /* tp_members */ |
| 169 | 0, /* tp_getset */ |
| 170 | 0, /* tp_base */ |
| 171 | 0, /* tp_dict */ |
| 172 | 0, /* tp_descr_get */ |
| 173 | 0, /* tp_descr_set */ |
| 174 | 0, /* tp_dictoffset */ |
| 175 | 0, /* tp_init */ |
| 176 | 0, /* tp_alloc */ |
| 177 | groupby_new, /* tp_new */ |
| 178 | PyObject_GC_Del, /* tp_free */ |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | /* _grouper object (internal) ************************************************/ |
| 183 | |
| 184 | typedef struct { |
| 185 | PyObject_HEAD |
| 186 | PyObject *parent; |
| 187 | PyObject *tgtkey; |
| 188 | } _grouperobject; |
| 189 | |
| 190 | static PyTypeObject _grouper_type; |
| 191 | |
| 192 | static PyObject * |
| 193 | _grouper_create(groupbyobject *parent, PyObject *tgtkey) |
| 194 | { |
| 195 | _grouperobject *igo; |
| 196 | |
| 197 | igo = PyObject_New(_grouperobject, &_grouper_type); |
| 198 | if (igo == NULL) |
| 199 | return NULL; |
| 200 | igo->parent = (PyObject *)parent; |
| 201 | Py_INCREF(parent); |
| 202 | igo->tgtkey = tgtkey; |
| 203 | Py_INCREF(tgtkey); |
| 204 | |
| 205 | return (PyObject *)igo; |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | _grouper_dealloc(_grouperobject *igo) |
| 210 | { |
| 211 | Py_DECREF(igo->parent); |
| 212 | Py_DECREF(igo->tgtkey); |
| 213 | PyObject_Del(igo); |
| 214 | } |
| 215 | |
| 216 | static PyObject * |
| 217 | _grouper_next(_grouperobject *igo) |
| 218 | { |
| 219 | groupbyobject *gbo = (groupbyobject *)igo->parent; |
| 220 | PyObject *newvalue, *newkey, *r; |
| 221 | int rcmp; |
| 222 | |
| 223 | if (gbo->currvalue == NULL) { |
| 224 | newvalue = PyIter_Next(gbo->it); |
| 225 | if (newvalue == NULL) |
| 226 | return NULL; |
| 227 | |
| 228 | if (gbo->keyfunc == Py_None) { |
| 229 | newkey = newvalue; |
| 230 | Py_INCREF(newvalue); |
| 231 | } else { |
| 232 | newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
| 233 | newvalue, NULL); |
| 234 | if (newkey == NULL) { |
| 235 | Py_DECREF(newvalue); |
| 236 | return NULL; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | assert(gbo->currkey == NULL); |
| 241 | gbo->currkey = newkey; |
| 242 | gbo->currvalue = newvalue; |
| 243 | } |
| 244 | |
| 245 | assert(gbo->currkey != NULL); |
| 246 | rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ); |
| 247 | if (rcmp <= 0) |
| 248 | /* got any error or current group is end */ |
| 249 | return NULL; |
| 250 | |
| 251 | r = gbo->currvalue; |
| 252 | gbo->currvalue = NULL; |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame^] | 253 | Py_CLEAR(gbo->currkey); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 254 | |
| 255 | return r; |
| 256 | } |
| 257 | |
| 258 | static PyTypeObject _grouper_type = { |
| 259 | PyObject_HEAD_INIT(NULL) |
| 260 | 0, /* ob_size */ |
| 261 | "itertools._grouper", /* tp_name */ |
| 262 | sizeof(_grouperobject), /* tp_basicsize */ |
| 263 | 0, /* tp_itemsize */ |
| 264 | /* methods */ |
| 265 | (destructor)_grouper_dealloc, /* tp_dealloc */ |
| 266 | 0, /* tp_print */ |
| 267 | 0, /* tp_getattr */ |
| 268 | 0, /* tp_setattr */ |
| 269 | 0, /* tp_compare */ |
| 270 | 0, /* tp_repr */ |
| 271 | 0, /* tp_as_number */ |
| 272 | 0, /* tp_as_sequence */ |
| 273 | 0, /* tp_as_mapping */ |
| 274 | 0, /* tp_hash */ |
| 275 | 0, /* tp_call */ |
| 276 | 0, /* tp_str */ |
| 277 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 278 | 0, /* tp_setattro */ |
| 279 | 0, /* tp_as_buffer */ |
| 280 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 281 | 0, /* tp_doc */ |
| 282 | 0, /* tp_traverse */ |
| 283 | 0, /* tp_clear */ |
| 284 | 0, /* tp_richcompare */ |
| 285 | 0, /* tp_weaklistoffset */ |
| 286 | PyObject_SelfIter, /* tp_iter */ |
| 287 | (iternextfunc)_grouper_next, /* tp_iternext */ |
| 288 | 0, /* tp_methods */ |
| 289 | 0, /* tp_members */ |
| 290 | 0, /* tp_getset */ |
| 291 | 0, /* tp_base */ |
| 292 | 0, /* tp_dict */ |
| 293 | 0, /* tp_descr_get */ |
| 294 | 0, /* tp_descr_set */ |
| 295 | 0, /* tp_dictoffset */ |
| 296 | 0, /* tp_init */ |
| 297 | 0, /* tp_alloc */ |
| 298 | 0, /* tp_new */ |
| 299 | PyObject_Del, /* tp_free */ |
| 300 | }; |
| 301 | |
| 302 | |
| 303 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 304 | /* tee object and with supporting function and objects ***************/ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 305 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 306 | /* The teedataobject pre-allocates space for LINKCELLS number of objects. |
| 307 | To help the object fit neatly inside cache lines (space for 16 to 32 |
| 308 | pointers), the value should be a multiple of 16 minus space for |
| 309 | the other structure members including PyHEAD overhead. The larger the |
| 310 | value, the less memory overhead per object and the less time spent |
| 311 | allocating/deallocating new links. The smaller the number, the less |
| 312 | wasted space and the more rapid freeing of older data. |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 313 | */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 314 | #define LINKCELLS 57 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 315 | |
| 316 | typedef struct { |
| 317 | PyObject_HEAD |
| 318 | PyObject *it; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 319 | int numread; |
| 320 | PyObject *nextlink; |
| 321 | PyObject *(values[LINKCELLS]); |
| 322 | } teedataobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 323 | |
| 324 | typedef struct { |
| 325 | PyObject_HEAD |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 326 | teedataobject *dataobj; |
| 327 | int index; |
| 328 | } teeobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 329 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 330 | static PyTypeObject teedataobject_type; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 331 | |
| 332 | static PyObject * |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 333 | teedataobject_new(PyObject *it) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 334 | { |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 335 | teedataobject *tdo; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 336 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 337 | tdo = PyObject_New(teedataobject, &teedataobject_type); |
| 338 | if (tdo == NULL) |
Raymond Hettinger | 4514369 | 2003-10-25 06:37:47 +0000 | [diff] [blame] | 339 | return NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 340 | |
| 341 | tdo->numread = 0; |
| 342 | tdo->nextlink = NULL; |
| 343 | Py_INCREF(it); |
| 344 | tdo->it = it; |
| 345 | return (PyObject *)tdo; |
| 346 | } |
| 347 | |
| 348 | static PyObject * |
| 349 | teedataobject_jumplink(teedataobject *tdo) |
| 350 | { |
| 351 | if (tdo->nextlink == NULL) |
| 352 | tdo->nextlink = teedataobject_new(tdo->it); |
| 353 | Py_INCREF(tdo->nextlink); |
| 354 | return tdo->nextlink; |
| 355 | } |
| 356 | |
| 357 | static PyObject * |
| 358 | teedataobject_getitem(teedataobject *tdo, int i) |
| 359 | { |
| 360 | PyObject *value; |
| 361 | |
| 362 | assert(i < LINKCELLS); |
| 363 | if (i < tdo->numread) |
| 364 | value = tdo->values[i]; |
| 365 | else { |
| 366 | /* this is the lead iterator, so fetch more data */ |
| 367 | assert(i == tdo->numread); |
| 368 | value = PyIter_Next(tdo->it); |
| 369 | if (value == NULL) |
| 370 | return NULL; |
| 371 | tdo->numread++; |
| 372 | tdo->values[i] = value; |
Raymond Hettinger | 4514369 | 2003-10-25 06:37:47 +0000 | [diff] [blame] | 373 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 374 | Py_INCREF(value); |
| 375 | return value; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 379 | teedataobject_dealloc(teedataobject *tdo) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 380 | { |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 381 | int i; |
Raymond Hettinger | 4514369 | 2003-10-25 06:37:47 +0000 | [diff] [blame] | 382 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 383 | for (i=0 ; i<tdo->numread ; i++) |
| 384 | Py_DECREF(tdo->values[i]); |
| 385 | Py_XDECREF(tdo->it); |
| 386 | Py_XDECREF(tdo->nextlink); |
| 387 | PyObject_Del(tdo); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 390 | PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects."); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 391 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 392 | static PyTypeObject teedataobject_type = { |
Skip Montanaro | f3938fd | 2004-02-10 20:27:40 +0000 | [diff] [blame] | 393 | PyObject_HEAD_INIT(0) /* Must fill in type value later */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 394 | 0, /* ob_size */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 395 | "itertools.tee_dataobject", /* tp_name */ |
| 396 | sizeof(teedataobject), /* tp_basicsize */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 397 | 0, /* tp_itemsize */ |
| 398 | /* methods */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 399 | (destructor)teedataobject_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 400 | 0, /* tp_print */ |
| 401 | 0, /* tp_getattr */ |
| 402 | 0, /* tp_setattr */ |
| 403 | 0, /* tp_compare */ |
| 404 | 0, /* tp_repr */ |
| 405 | 0, /* tp_as_number */ |
| 406 | 0, /* tp_as_sequence */ |
| 407 | 0, /* tp_as_mapping */ |
| 408 | 0, /* tp_hash */ |
| 409 | 0, /* tp_call */ |
| 410 | 0, /* tp_str */ |
| 411 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 412 | 0, /* tp_setattro */ |
| 413 | 0, /* tp_as_buffer */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 414 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 415 | teedataobject_doc, /* tp_doc */ |
| 416 | 0, /* tp_traverse */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 419 | |
| 420 | static PyTypeObject tee_type; |
| 421 | |
| 422 | static PyObject * |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 423 | tee_next(teeobject *to) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 424 | { |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 425 | PyObject *value, *link; |
| 426 | |
| 427 | if (to->index >= LINKCELLS) { |
| 428 | link = teedataobject_jumplink(to->dataobj); |
| 429 | Py_XDECREF(to->dataobj); |
| 430 | to->dataobj = (teedataobject *)link; |
| 431 | to->index = 0; |
| 432 | } |
| 433 | value = teedataobject_getitem(to->dataobj, to->index); |
| 434 | if (value == NULL) |
| 435 | return NULL; |
| 436 | to->index++; |
| 437 | return value; |
| 438 | } |
| 439 | |
| 440 | static PyObject * |
| 441 | tee_copy(teeobject *to) |
| 442 | { |
| 443 | teeobject *newto; |
| 444 | |
| 445 | newto = PyObject_New(teeobject, &tee_type); |
| 446 | if (newto == NULL) |
| 447 | return NULL; |
| 448 | Py_INCREF(to->dataobj); |
| 449 | newto->dataobj = to->dataobj; |
| 450 | newto->index = to->index; |
| 451 | return (PyObject *)newto; |
| 452 | } |
| 453 | |
| 454 | PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); |
| 455 | |
| 456 | static PyObject * |
| 457 | tee_fromiterable(PyObject *iterable) |
| 458 | { |
| 459 | teeobject *to; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 460 | PyObject *it = NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 461 | |
| 462 | it = PyObject_GetIter(iterable); |
| 463 | if (it == NULL) |
| 464 | return NULL; |
| 465 | if (PyObject_TypeCheck(it, &tee_type)) { |
| 466 | to = (teeobject *)tee_copy((teeobject *)it); |
| 467 | goto done; |
| 468 | } |
| 469 | |
| 470 | to = PyObject_New(teeobject, &tee_type); |
| 471 | if (to == NULL) |
| 472 | goto done; |
| 473 | to->dataobj = (teedataobject *)teedataobject_new(it); |
| 474 | to->index = 0; |
| 475 | done: |
| 476 | Py_XDECREF(it); |
| 477 | return (PyObject *)to; |
| 478 | } |
| 479 | |
| 480 | static PyObject * |
| 481 | tee_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 482 | { |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 483 | PyObject *iterable; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 484 | |
| 485 | if (!PyArg_UnpackTuple(args, "tee", 1, 1, &iterable)) |
| 486 | return NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 487 | return tee_fromiterable(iterable); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void |
| 491 | tee_dealloc(teeobject *to) |
| 492 | { |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 493 | Py_XDECREF(to->dataobj); |
| 494 | PyObject_Del(to); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 497 | PyDoc_STRVAR(teeobject_doc, |
| 498 | "Iterator wrapped to make it copyable"); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 499 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 500 | static PyMethodDef tee_methods[] = { |
| 501 | {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc}, |
| 502 | {NULL, NULL} /* sentinel */ |
| 503 | }; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 504 | |
| 505 | static PyTypeObject tee_type = { |
| 506 | PyObject_HEAD_INIT(NULL) |
| 507 | 0, /* ob_size */ |
| 508 | "itertools.tee", /* tp_name */ |
| 509 | sizeof(teeobject), /* tp_basicsize */ |
| 510 | 0, /* tp_itemsize */ |
| 511 | /* methods */ |
| 512 | (destructor)tee_dealloc, /* tp_dealloc */ |
| 513 | 0, /* tp_print */ |
| 514 | 0, /* tp_getattr */ |
| 515 | 0, /* tp_setattr */ |
| 516 | 0, /* tp_compare */ |
| 517 | 0, /* tp_repr */ |
| 518 | 0, /* tp_as_number */ |
| 519 | 0, /* tp_as_sequence */ |
| 520 | 0, /* tp_as_mapping */ |
| 521 | 0, /* tp_hash */ |
| 522 | 0, /* tp_call */ |
| 523 | 0, /* tp_str */ |
Raymond Hettinger | f0c5aec | 2003-10-26 14:25:56 +0000 | [diff] [blame] | 524 | 0, /* tp_getattro */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 525 | 0, /* tp_setattro */ |
| 526 | 0, /* tp_as_buffer */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 527 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 528 | teeobject_doc, /* tp_doc */ |
| 529 | 0, /* tp_traverse */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 530 | 0, /* tp_clear */ |
| 531 | 0, /* tp_richcompare */ |
| 532 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 533 | PyObject_SelfIter, /* tp_iter */ |
| 534 | (iternextfunc)tee_next, /* tp_iternext */ |
| 535 | tee_methods, /* tp_methods */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 536 | 0, /* tp_members */ |
| 537 | 0, /* tp_getset */ |
| 538 | 0, /* tp_base */ |
| 539 | 0, /* tp_dict */ |
| 540 | 0, /* tp_descr_get */ |
| 541 | 0, /* tp_descr_set */ |
| 542 | 0, /* tp_dictoffset */ |
| 543 | 0, /* tp_init */ |
| 544 | 0, /* tp_alloc */ |
| 545 | tee_new, /* tp_new */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 546 | PyObject_Del, /* tp_free */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 547 | }; |
| 548 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 549 | static PyObject * |
| 550 | tee(PyObject *self, PyObject *args) |
| 551 | { |
| 552 | int i, n=2; |
| 553 | PyObject *it, *iterable, *copyable, *result; |
| 554 | |
| 555 | if (!PyArg_ParseTuple(args, "O|i", &iterable, &n)) |
| 556 | return NULL; |
| 557 | result = PyTuple_New(n); |
| 558 | if (result == NULL) |
| 559 | return NULL; |
| 560 | if (n == 0) |
| 561 | return result; |
| 562 | it = PyObject_GetIter(iterable); |
| 563 | if (it == NULL) { |
| 564 | Py_DECREF(result); |
| 565 | return NULL; |
| 566 | } |
| 567 | if (!PyObject_HasAttrString(it, "__copy__")) { |
| 568 | copyable = tee_fromiterable(it); |
| 569 | Py_DECREF(it); |
| 570 | if (copyable == NULL) { |
| 571 | Py_DECREF(result); |
| 572 | return NULL; |
| 573 | } |
| 574 | } else |
| 575 | copyable = it; |
| 576 | PyTuple_SET_ITEM(result, 0, copyable); |
| 577 | for (i=1 ; i<n ; i++) { |
| 578 | copyable = PyObject_CallMethod(copyable, "__copy__", NULL); |
| 579 | if (copyable == NULL) { |
| 580 | Py_DECREF(result); |
| 581 | return NULL; |
| 582 | } |
| 583 | PyTuple_SET_ITEM(result, i, copyable); |
| 584 | } |
| 585 | return result; |
| 586 | } |
| 587 | |
| 588 | PyDoc_STRVAR(tee_doc, |
| 589 | "tee(iterable, n=2) --> tuple of n independent iterators."); |
| 590 | |
| 591 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 592 | /* cycle object **********************************************************/ |
| 593 | |
| 594 | typedef struct { |
| 595 | PyObject_HEAD |
| 596 | PyObject *it; |
| 597 | PyObject *saved; |
| 598 | int firstpass; |
| 599 | } cycleobject; |
| 600 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 601 | static PyTypeObject cycle_type; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 602 | |
| 603 | static PyObject * |
| 604 | cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 605 | { |
| 606 | PyObject *it; |
| 607 | PyObject *iterable; |
| 608 | PyObject *saved; |
| 609 | cycleobject *lz; |
| 610 | |
| 611 | if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable)) |
| 612 | return NULL; |
| 613 | |
| 614 | /* Get iterator. */ |
| 615 | it = PyObject_GetIter(iterable); |
| 616 | if (it == NULL) |
| 617 | return NULL; |
| 618 | |
| 619 | saved = PyList_New(0); |
| 620 | if (saved == NULL) { |
| 621 | Py_DECREF(it); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | /* create cycleobject structure */ |
| 626 | lz = (cycleobject *)type->tp_alloc(type, 0); |
| 627 | if (lz == NULL) { |
| 628 | Py_DECREF(it); |
| 629 | Py_DECREF(saved); |
| 630 | return NULL; |
| 631 | } |
| 632 | lz->it = it; |
| 633 | lz->saved = saved; |
| 634 | lz->firstpass = 0; |
| 635 | |
| 636 | return (PyObject *)lz; |
| 637 | } |
| 638 | |
| 639 | static void |
| 640 | cycle_dealloc(cycleobject *lz) |
| 641 | { |
| 642 | PyObject_GC_UnTrack(lz); |
| 643 | Py_XDECREF(lz->saved); |
| 644 | Py_XDECREF(lz->it); |
| 645 | lz->ob_type->tp_free(lz); |
| 646 | } |
| 647 | |
| 648 | static int |
| 649 | cycle_traverse(cycleobject *lz, visitproc visit, void *arg) |
| 650 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 651 | Py_VISIT(lz->it); |
| 652 | Py_VISIT(lz->saved); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static PyObject * |
| 657 | cycle_next(cycleobject *lz) |
| 658 | { |
| 659 | PyObject *item; |
| 660 | PyObject *it; |
| 661 | |
| 662 | while (1) { |
| 663 | item = PyIter_Next(lz->it); |
| 664 | if (item != NULL) { |
| 665 | if (!lz->firstpass) |
| 666 | PyList_Append(lz->saved, item); |
| 667 | return item; |
| 668 | } |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 669 | if (PyErr_Occurred()) { |
| 670 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 671 | PyErr_Clear(); |
| 672 | else |
| 673 | return NULL; |
| 674 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 675 | if (PyList_Size(lz->saved) == 0) |
| 676 | return NULL; |
| 677 | it = PyObject_GetIter(lz->saved); |
| 678 | if (it == NULL) |
| 679 | return NULL; |
| 680 | Py_DECREF(lz->it); |
| 681 | lz->it = it; |
| 682 | lz->firstpass = 1; |
| 683 | } |
| 684 | } |
| 685 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 686 | PyDoc_STRVAR(cycle_doc, |
| 687 | "cycle(iterable) --> cycle object\n\ |
| 688 | \n\ |
| 689 | Return elements from the iterable until it is exhausted.\n\ |
| 690 | Then repeat the sequence indefinitely."); |
| 691 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 692 | static PyTypeObject cycle_type = { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 693 | PyObject_HEAD_INIT(NULL) |
| 694 | 0, /* ob_size */ |
| 695 | "itertools.cycle", /* tp_name */ |
| 696 | sizeof(cycleobject), /* tp_basicsize */ |
| 697 | 0, /* tp_itemsize */ |
| 698 | /* methods */ |
| 699 | (destructor)cycle_dealloc, /* tp_dealloc */ |
| 700 | 0, /* tp_print */ |
| 701 | 0, /* tp_getattr */ |
| 702 | 0, /* tp_setattr */ |
| 703 | 0, /* tp_compare */ |
| 704 | 0, /* tp_repr */ |
| 705 | 0, /* tp_as_number */ |
| 706 | 0, /* tp_as_sequence */ |
| 707 | 0, /* tp_as_mapping */ |
| 708 | 0, /* tp_hash */ |
| 709 | 0, /* tp_call */ |
| 710 | 0, /* tp_str */ |
| 711 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 712 | 0, /* tp_setattro */ |
| 713 | 0, /* tp_as_buffer */ |
| 714 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 715 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 716 | cycle_doc, /* tp_doc */ |
| 717 | (traverseproc)cycle_traverse, /* tp_traverse */ |
| 718 | 0, /* tp_clear */ |
| 719 | 0, /* tp_richcompare */ |
| 720 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 721 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 722 | (iternextfunc)cycle_next, /* tp_iternext */ |
| 723 | 0, /* tp_methods */ |
| 724 | 0, /* tp_members */ |
| 725 | 0, /* tp_getset */ |
| 726 | 0, /* tp_base */ |
| 727 | 0, /* tp_dict */ |
| 728 | 0, /* tp_descr_get */ |
| 729 | 0, /* tp_descr_set */ |
| 730 | 0, /* tp_dictoffset */ |
| 731 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 732 | 0, /* tp_alloc */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 733 | cycle_new, /* tp_new */ |
| 734 | PyObject_GC_Del, /* tp_free */ |
| 735 | }; |
| 736 | |
| 737 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 738 | /* dropwhile object **********************************************************/ |
| 739 | |
| 740 | typedef struct { |
| 741 | PyObject_HEAD |
| 742 | PyObject *func; |
| 743 | PyObject *it; |
| 744 | long start; |
| 745 | } dropwhileobject; |
| 746 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 747 | static PyTypeObject dropwhile_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 748 | |
| 749 | static PyObject * |
| 750 | dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 751 | { |
| 752 | PyObject *func, *seq; |
| 753 | PyObject *it; |
| 754 | dropwhileobject *lz; |
| 755 | |
| 756 | if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq)) |
| 757 | return NULL; |
| 758 | |
| 759 | /* Get iterator. */ |
| 760 | it = PyObject_GetIter(seq); |
| 761 | if (it == NULL) |
| 762 | return NULL; |
| 763 | |
| 764 | /* create dropwhileobject structure */ |
| 765 | lz = (dropwhileobject *)type->tp_alloc(type, 0); |
| 766 | if (lz == NULL) { |
| 767 | Py_DECREF(it); |
| 768 | return NULL; |
| 769 | } |
| 770 | Py_INCREF(func); |
| 771 | lz->func = func; |
| 772 | lz->it = it; |
| 773 | lz->start = 0; |
| 774 | |
| 775 | return (PyObject *)lz; |
| 776 | } |
| 777 | |
| 778 | static void |
| 779 | dropwhile_dealloc(dropwhileobject *lz) |
| 780 | { |
| 781 | PyObject_GC_UnTrack(lz); |
| 782 | Py_XDECREF(lz->func); |
| 783 | Py_XDECREF(lz->it); |
| 784 | lz->ob_type->tp_free(lz); |
| 785 | } |
| 786 | |
| 787 | static int |
| 788 | dropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg) |
| 789 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 790 | Py_VISIT(lz->it); |
| 791 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static PyObject * |
| 796 | dropwhile_next(dropwhileobject *lz) |
| 797 | { |
| 798 | PyObject *item, *good; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 799 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 800 | long ok; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 801 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 802 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 803 | assert(PyIter_Check(it)); |
| 804 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 805 | for (;;) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 806 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 807 | if (item == NULL) |
| 808 | return NULL; |
| 809 | if (lz->start == 1) |
| 810 | return item; |
| 811 | |
| 812 | good = PyObject_CallFunctionObjArgs(lz->func, item, NULL); |
| 813 | if (good == NULL) { |
| 814 | Py_DECREF(item); |
| 815 | return NULL; |
| 816 | } |
| 817 | ok = PyObject_IsTrue(good); |
| 818 | Py_DECREF(good); |
| 819 | if (!ok) { |
| 820 | lz->start = 1; |
| 821 | return item; |
| 822 | } |
| 823 | Py_DECREF(item); |
| 824 | } |
| 825 | } |
| 826 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 827 | PyDoc_STRVAR(dropwhile_doc, |
| 828 | "dropwhile(predicate, iterable) --> dropwhile object\n\ |
| 829 | \n\ |
| 830 | Drop items from the iterable while predicate(item) is true.\n\ |
| 831 | Afterwards, return every element until the iterable is exhausted."); |
| 832 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 833 | static PyTypeObject dropwhile_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 834 | PyObject_HEAD_INIT(NULL) |
| 835 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 836 | "itertools.dropwhile", /* tp_name */ |
| 837 | sizeof(dropwhileobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 838 | 0, /* tp_itemsize */ |
| 839 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 840 | (destructor)dropwhile_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 841 | 0, /* tp_print */ |
| 842 | 0, /* tp_getattr */ |
| 843 | 0, /* tp_setattr */ |
| 844 | 0, /* tp_compare */ |
| 845 | 0, /* tp_repr */ |
| 846 | 0, /* tp_as_number */ |
| 847 | 0, /* tp_as_sequence */ |
| 848 | 0, /* tp_as_mapping */ |
| 849 | 0, /* tp_hash */ |
| 850 | 0, /* tp_call */ |
| 851 | 0, /* tp_str */ |
| 852 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 853 | 0, /* tp_setattro */ |
| 854 | 0, /* tp_as_buffer */ |
| 855 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 856 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 857 | dropwhile_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 858 | (traverseproc)dropwhile_traverse, /* tp_traverse */ |
| 859 | 0, /* tp_clear */ |
| 860 | 0, /* tp_richcompare */ |
| 861 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 862 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 863 | (iternextfunc)dropwhile_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 864 | 0, /* tp_methods */ |
| 865 | 0, /* tp_members */ |
| 866 | 0, /* tp_getset */ |
| 867 | 0, /* tp_base */ |
| 868 | 0, /* tp_dict */ |
| 869 | 0, /* tp_descr_get */ |
| 870 | 0, /* tp_descr_set */ |
| 871 | 0, /* tp_dictoffset */ |
| 872 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 873 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 874 | dropwhile_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 875 | PyObject_GC_Del, /* tp_free */ |
| 876 | }; |
| 877 | |
| 878 | |
| 879 | /* takewhile object **********************************************************/ |
| 880 | |
| 881 | typedef struct { |
| 882 | PyObject_HEAD |
| 883 | PyObject *func; |
| 884 | PyObject *it; |
| 885 | long stop; |
| 886 | } takewhileobject; |
| 887 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 888 | static PyTypeObject takewhile_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 889 | |
| 890 | static PyObject * |
| 891 | takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 892 | { |
| 893 | PyObject *func, *seq; |
| 894 | PyObject *it; |
| 895 | takewhileobject *lz; |
| 896 | |
| 897 | if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq)) |
| 898 | return NULL; |
| 899 | |
| 900 | /* Get iterator. */ |
| 901 | it = PyObject_GetIter(seq); |
| 902 | if (it == NULL) |
| 903 | return NULL; |
| 904 | |
| 905 | /* create takewhileobject structure */ |
| 906 | lz = (takewhileobject *)type->tp_alloc(type, 0); |
| 907 | if (lz == NULL) { |
| 908 | Py_DECREF(it); |
| 909 | return NULL; |
| 910 | } |
| 911 | Py_INCREF(func); |
| 912 | lz->func = func; |
| 913 | lz->it = it; |
| 914 | lz->stop = 0; |
| 915 | |
| 916 | return (PyObject *)lz; |
| 917 | } |
| 918 | |
| 919 | static void |
| 920 | takewhile_dealloc(takewhileobject *lz) |
| 921 | { |
| 922 | PyObject_GC_UnTrack(lz); |
| 923 | Py_XDECREF(lz->func); |
| 924 | Py_XDECREF(lz->it); |
| 925 | lz->ob_type->tp_free(lz); |
| 926 | } |
| 927 | |
| 928 | static int |
| 929 | takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg) |
| 930 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 931 | Py_VISIT(lz->it); |
| 932 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static PyObject * |
| 937 | takewhile_next(takewhileobject *lz) |
| 938 | { |
| 939 | PyObject *item, *good; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 940 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 941 | long ok; |
| 942 | |
| 943 | if (lz->stop == 1) |
| 944 | return NULL; |
| 945 | |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 946 | assert(PyIter_Check(it)); |
| 947 | item = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 948 | if (item == NULL) |
| 949 | return NULL; |
| 950 | |
| 951 | good = PyObject_CallFunctionObjArgs(lz->func, item, NULL); |
| 952 | if (good == NULL) { |
| 953 | Py_DECREF(item); |
| 954 | return NULL; |
| 955 | } |
| 956 | ok = PyObject_IsTrue(good); |
| 957 | Py_DECREF(good); |
| 958 | if (ok) |
| 959 | return item; |
| 960 | Py_DECREF(item); |
| 961 | lz->stop = 1; |
| 962 | return NULL; |
| 963 | } |
| 964 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 965 | PyDoc_STRVAR(takewhile_doc, |
| 966 | "takewhile(predicate, iterable) --> takewhile object\n\ |
| 967 | \n\ |
| 968 | Return successive entries from an iterable as long as the \n\ |
| 969 | predicate evaluates to true for each entry."); |
| 970 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 971 | static PyTypeObject takewhile_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 972 | PyObject_HEAD_INIT(NULL) |
| 973 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 974 | "itertools.takewhile", /* tp_name */ |
| 975 | sizeof(takewhileobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 976 | 0, /* tp_itemsize */ |
| 977 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 978 | (destructor)takewhile_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 979 | 0, /* tp_print */ |
| 980 | 0, /* tp_getattr */ |
| 981 | 0, /* tp_setattr */ |
| 982 | 0, /* tp_compare */ |
| 983 | 0, /* tp_repr */ |
| 984 | 0, /* tp_as_number */ |
| 985 | 0, /* tp_as_sequence */ |
| 986 | 0, /* tp_as_mapping */ |
| 987 | 0, /* tp_hash */ |
| 988 | 0, /* tp_call */ |
| 989 | 0, /* tp_str */ |
| 990 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 991 | 0, /* tp_setattro */ |
| 992 | 0, /* tp_as_buffer */ |
| 993 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 994 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 995 | takewhile_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 996 | (traverseproc)takewhile_traverse, /* tp_traverse */ |
| 997 | 0, /* tp_clear */ |
| 998 | 0, /* tp_richcompare */ |
| 999 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1000 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1001 | (iternextfunc)takewhile_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1002 | 0, /* tp_methods */ |
| 1003 | 0, /* tp_members */ |
| 1004 | 0, /* tp_getset */ |
| 1005 | 0, /* tp_base */ |
| 1006 | 0, /* tp_dict */ |
| 1007 | 0, /* tp_descr_get */ |
| 1008 | 0, /* tp_descr_set */ |
| 1009 | 0, /* tp_dictoffset */ |
| 1010 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1011 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1012 | takewhile_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1013 | PyObject_GC_Del, /* tp_free */ |
| 1014 | }; |
| 1015 | |
| 1016 | |
| 1017 | /* islice object ************************************************************/ |
| 1018 | |
| 1019 | typedef struct { |
| 1020 | PyObject_HEAD |
| 1021 | PyObject *it; |
| 1022 | long next; |
| 1023 | long stop; |
| 1024 | long step; |
| 1025 | long cnt; |
| 1026 | } isliceobject; |
| 1027 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1028 | static PyTypeObject islice_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1029 | |
| 1030 | static PyObject * |
| 1031 | islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1032 | { |
| 1033 | PyObject *seq; |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1034 | long start=0, stop=-1, step=1; |
| 1035 | PyObject *it, *a1=NULL, *a2=NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1036 | int numargs; |
| 1037 | isliceobject *lz; |
| 1038 | |
| 1039 | numargs = PyTuple_Size(args); |
Raymond Hettinger | 341deb7 | 2003-05-02 19:44:20 +0000 | [diff] [blame] | 1040 | if (!PyArg_ParseTuple(args, "OO|Ol:islice", &seq, &a1, &a2, &step)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1041 | return NULL; |
| 1042 | |
| 1043 | if (numargs == 2) { |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1044 | if (a1 != Py_None) { |
| 1045 | stop = PyInt_AsLong(a1); |
| 1046 | if (stop == -1) { |
| 1047 | if (PyErr_Occurred()) |
| 1048 | PyErr_Clear(); |
| 1049 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 0e4f764 | 2003-10-28 07:32:28 +0000 | [diff] [blame] | 1050 | "Stop argument must be a non-negative integer or None."); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1051 | return NULL; |
| 1052 | } |
| 1053 | } |
Raymond Hettinger | 341deb7 | 2003-05-02 19:44:20 +0000 | [diff] [blame] | 1054 | } else { |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1055 | start = PyInt_AsLong(a1); |
| 1056 | if (start == -1 && PyErr_Occurred()) { |
| 1057 | PyErr_Clear(); |
| 1058 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 0e4f764 | 2003-10-28 07:32:28 +0000 | [diff] [blame] | 1059 | "Start argument must be a non-negative integer."); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1060 | return NULL; |
| 1061 | } |
| 1062 | if (a2 != Py_None) { |
| 1063 | stop = PyInt_AsLong(a2); |
| 1064 | if (stop == -1) { |
| 1065 | if (PyErr_Occurred()) |
| 1066 | PyErr_Clear(); |
| 1067 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 0e4f764 | 2003-10-28 07:32:28 +0000 | [diff] [blame] | 1068 | "Stop argument must be a non-negative integer or None."); |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1069 | return NULL; |
| 1070 | } |
| 1071 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1074 | if (start<0 || stop<-1) { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1075 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 0e4f764 | 2003-10-28 07:32:28 +0000 | [diff] [blame] | 1076 | "Indices for islice() must be non-negative integers."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1077 | return NULL; |
| 1078 | } |
| 1079 | |
| 1080 | if (step<1) { |
| 1081 | PyErr_SetString(PyExc_ValueError, |
| 1082 | "Step must be one or larger for islice()."); |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | |
| 1086 | /* Get iterator. */ |
| 1087 | it = PyObject_GetIter(seq); |
| 1088 | if (it == NULL) |
| 1089 | return NULL; |
| 1090 | |
| 1091 | /* create isliceobject structure */ |
| 1092 | lz = (isliceobject *)type->tp_alloc(type, 0); |
| 1093 | if (lz == NULL) { |
| 1094 | Py_DECREF(it); |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | lz->it = it; |
| 1098 | lz->next = start; |
| 1099 | lz->stop = stop; |
| 1100 | lz->step = step; |
| 1101 | lz->cnt = 0L; |
| 1102 | |
| 1103 | return (PyObject *)lz; |
| 1104 | } |
| 1105 | |
| 1106 | static void |
| 1107 | islice_dealloc(isliceobject *lz) |
| 1108 | { |
| 1109 | PyObject_GC_UnTrack(lz); |
| 1110 | Py_XDECREF(lz->it); |
| 1111 | lz->ob_type->tp_free(lz); |
| 1112 | } |
| 1113 | |
| 1114 | static int |
| 1115 | islice_traverse(isliceobject *lz, visitproc visit, void *arg) |
| 1116 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1117 | Py_VISIT(lz->it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | static PyObject * |
| 1122 | islice_next(isliceobject *lz) |
| 1123 | { |
| 1124 | PyObject *item; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1125 | PyObject *it = lz->it; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1126 | long oldnext; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1127 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1128 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1129 | assert(PyIter_Check(it)); |
| 1130 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1131 | while (lz->cnt < lz->next) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1132 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1133 | if (item == NULL) |
| 1134 | return NULL; |
| 1135 | Py_DECREF(item); |
| 1136 | lz->cnt++; |
| 1137 | } |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 1138 | if (lz->stop != -1 && lz->cnt >= lz->stop) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1139 | return NULL; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1140 | assert(PyIter_Check(it)); |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1141 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1142 | if (item == NULL) |
| 1143 | return NULL; |
| 1144 | lz->cnt++; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1145 | oldnext = lz->next; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1146 | lz->next += lz->step; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1147 | if (lz->next < oldnext) /* Check for overflow */ |
| 1148 | lz->next = lz->stop; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1149 | return item; |
| 1150 | } |
| 1151 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1152 | PyDoc_STRVAR(islice_doc, |
| 1153 | "islice(iterable, [start,] stop [, step]) --> islice object\n\ |
| 1154 | \n\ |
| 1155 | Return an iterator whose next() method returns selected values from an\n\ |
| 1156 | iterable. If start is specified, will skip all preceding elements;\n\ |
| 1157 | otherwise, start defaults to zero. Step defaults to one. If\n\ |
| 1158 | specified as another value, step determines how many values are \n\ |
| 1159 | skipped between successive calls. Works like a slice() on a list\n\ |
| 1160 | but returns an iterator."); |
| 1161 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1162 | static PyTypeObject islice_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1163 | PyObject_HEAD_INIT(NULL) |
| 1164 | 0, /* ob_size */ |
| 1165 | "itertools.islice", /* tp_name */ |
| 1166 | sizeof(isliceobject), /* tp_basicsize */ |
| 1167 | 0, /* tp_itemsize */ |
| 1168 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1169 | (destructor)islice_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1170 | 0, /* tp_print */ |
| 1171 | 0, /* tp_getattr */ |
| 1172 | 0, /* tp_setattr */ |
| 1173 | 0, /* tp_compare */ |
| 1174 | 0, /* tp_repr */ |
| 1175 | 0, /* tp_as_number */ |
| 1176 | 0, /* tp_as_sequence */ |
| 1177 | 0, /* tp_as_mapping */ |
| 1178 | 0, /* tp_hash */ |
| 1179 | 0, /* tp_call */ |
| 1180 | 0, /* tp_str */ |
| 1181 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1182 | 0, /* tp_setattro */ |
| 1183 | 0, /* tp_as_buffer */ |
| 1184 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1185 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1186 | islice_doc, /* tp_doc */ |
| 1187 | (traverseproc)islice_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1188 | 0, /* tp_clear */ |
| 1189 | 0, /* tp_richcompare */ |
| 1190 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1191 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1192 | (iternextfunc)islice_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1193 | 0, /* tp_methods */ |
| 1194 | 0, /* tp_members */ |
| 1195 | 0, /* tp_getset */ |
| 1196 | 0, /* tp_base */ |
| 1197 | 0, /* tp_dict */ |
| 1198 | 0, /* tp_descr_get */ |
| 1199 | 0, /* tp_descr_set */ |
| 1200 | 0, /* tp_dictoffset */ |
| 1201 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1202 | 0, /* tp_alloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1203 | islice_new, /* tp_new */ |
| 1204 | PyObject_GC_Del, /* tp_free */ |
| 1205 | }; |
| 1206 | |
| 1207 | |
| 1208 | /* starmap object ************************************************************/ |
| 1209 | |
| 1210 | typedef struct { |
| 1211 | PyObject_HEAD |
| 1212 | PyObject *func; |
| 1213 | PyObject *it; |
| 1214 | } starmapobject; |
| 1215 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1216 | static PyTypeObject starmap_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1217 | |
| 1218 | static PyObject * |
| 1219 | starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1220 | { |
| 1221 | PyObject *func, *seq; |
| 1222 | PyObject *it; |
| 1223 | starmapobject *lz; |
| 1224 | |
| 1225 | if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq)) |
| 1226 | return NULL; |
| 1227 | |
| 1228 | /* Get iterator. */ |
| 1229 | it = PyObject_GetIter(seq); |
| 1230 | if (it == NULL) |
| 1231 | return NULL; |
| 1232 | |
| 1233 | /* create starmapobject structure */ |
| 1234 | lz = (starmapobject *)type->tp_alloc(type, 0); |
| 1235 | if (lz == NULL) { |
| 1236 | Py_DECREF(it); |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | Py_INCREF(func); |
| 1240 | lz->func = func; |
| 1241 | lz->it = it; |
| 1242 | |
| 1243 | return (PyObject *)lz; |
| 1244 | } |
| 1245 | |
| 1246 | static void |
| 1247 | starmap_dealloc(starmapobject *lz) |
| 1248 | { |
| 1249 | PyObject_GC_UnTrack(lz); |
| 1250 | Py_XDECREF(lz->func); |
| 1251 | Py_XDECREF(lz->it); |
| 1252 | lz->ob_type->tp_free(lz); |
| 1253 | } |
| 1254 | |
| 1255 | static int |
| 1256 | starmap_traverse(starmapobject *lz, visitproc visit, void *arg) |
| 1257 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1258 | Py_VISIT(lz->it); |
| 1259 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | static PyObject * |
| 1264 | starmap_next(starmapobject *lz) |
| 1265 | { |
| 1266 | PyObject *args; |
| 1267 | PyObject *result; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1268 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1269 | |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1270 | assert(PyIter_Check(it)); |
| 1271 | args = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1272 | if (args == NULL) |
| 1273 | return NULL; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1274 | if (!PyTuple_CheckExact(args)) { |
| 1275 | Py_DECREF(args); |
| 1276 | PyErr_SetString(PyExc_TypeError, |
| 1277 | "iterator must return a tuple"); |
| 1278 | return NULL; |
| 1279 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1280 | result = PyObject_Call(lz->func, args, NULL); |
| 1281 | Py_DECREF(args); |
| 1282 | return result; |
| 1283 | } |
| 1284 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1285 | PyDoc_STRVAR(starmap_doc, |
| 1286 | "starmap(function, sequence) --> starmap object\n\ |
| 1287 | \n\ |
| 1288 | Return an iterator whose values are returned from the function evaluated\n\ |
| 1289 | with a argument tuple taken from the given sequence."); |
| 1290 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1291 | static PyTypeObject starmap_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1292 | PyObject_HEAD_INIT(NULL) |
| 1293 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1294 | "itertools.starmap", /* tp_name */ |
| 1295 | sizeof(starmapobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1296 | 0, /* tp_itemsize */ |
| 1297 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1298 | (destructor)starmap_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1299 | 0, /* tp_print */ |
| 1300 | 0, /* tp_getattr */ |
| 1301 | 0, /* tp_setattr */ |
| 1302 | 0, /* tp_compare */ |
| 1303 | 0, /* tp_repr */ |
| 1304 | 0, /* tp_as_number */ |
| 1305 | 0, /* tp_as_sequence */ |
| 1306 | 0, /* tp_as_mapping */ |
| 1307 | 0, /* tp_hash */ |
| 1308 | 0, /* tp_call */ |
| 1309 | 0, /* tp_str */ |
| 1310 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1311 | 0, /* tp_setattro */ |
| 1312 | 0, /* tp_as_buffer */ |
| 1313 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1314 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1315 | starmap_doc, /* tp_doc */ |
| 1316 | (traverseproc)starmap_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1317 | 0, /* tp_clear */ |
| 1318 | 0, /* tp_richcompare */ |
| 1319 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1320 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1321 | (iternextfunc)starmap_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1322 | 0, /* tp_methods */ |
| 1323 | 0, /* tp_members */ |
| 1324 | 0, /* tp_getset */ |
| 1325 | 0, /* tp_base */ |
| 1326 | 0, /* tp_dict */ |
| 1327 | 0, /* tp_descr_get */ |
| 1328 | 0, /* tp_descr_set */ |
| 1329 | 0, /* tp_dictoffset */ |
| 1330 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1331 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1332 | starmap_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1333 | PyObject_GC_Del, /* tp_free */ |
| 1334 | }; |
| 1335 | |
| 1336 | |
| 1337 | /* imap object ************************************************************/ |
| 1338 | |
| 1339 | typedef struct { |
| 1340 | PyObject_HEAD |
| 1341 | PyObject *iters; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1342 | PyObject *func; |
| 1343 | } imapobject; |
| 1344 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1345 | static PyTypeObject imap_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1346 | |
| 1347 | static PyObject * |
| 1348 | imap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1349 | { |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1350 | PyObject *it, *iters, *func; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1351 | imapobject *lz; |
| 1352 | int numargs, i; |
| 1353 | |
| 1354 | numargs = PyTuple_Size(args); |
| 1355 | if (numargs < 2) { |
| 1356 | PyErr_SetString(PyExc_TypeError, |
| 1357 | "imap() must have at least two arguments."); |
| 1358 | return NULL; |
| 1359 | } |
| 1360 | |
| 1361 | iters = PyTuple_New(numargs-1); |
| 1362 | if (iters == NULL) |
| 1363 | return NULL; |
| 1364 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1365 | for (i=1 ; i<numargs ; i++) { |
| 1366 | /* Get iterator. */ |
| 1367 | it = PyObject_GetIter(PyTuple_GET_ITEM(args, i)); |
| 1368 | if (it == NULL) { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1369 | Py_DECREF(iters); |
| 1370 | return NULL; |
| 1371 | } |
| 1372 | PyTuple_SET_ITEM(iters, i-1, it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /* create imapobject structure */ |
| 1376 | lz = (imapobject *)type->tp_alloc(type, 0); |
| 1377 | if (lz == NULL) { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1378 | Py_DECREF(iters); |
| 1379 | return NULL; |
| 1380 | } |
| 1381 | lz->iters = iters; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1382 | func = PyTuple_GET_ITEM(args, 0); |
| 1383 | Py_INCREF(func); |
| 1384 | lz->func = func; |
| 1385 | |
| 1386 | return (PyObject *)lz; |
| 1387 | } |
| 1388 | |
| 1389 | static void |
| 1390 | imap_dealloc(imapobject *lz) |
| 1391 | { |
| 1392 | PyObject_GC_UnTrack(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1393 | Py_XDECREF(lz->iters); |
| 1394 | Py_XDECREF(lz->func); |
| 1395 | lz->ob_type->tp_free(lz); |
| 1396 | } |
| 1397 | |
| 1398 | static int |
| 1399 | imap_traverse(imapobject *lz, visitproc visit, void *arg) |
| 1400 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1401 | Py_VISIT(lz->iters); |
| 1402 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1403 | return 0; |
| 1404 | } |
| 1405 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1406 | /* |
| 1407 | imap() is an iterator version of __builtins__.map() except that it does |
| 1408 | not have the None fill-in feature. That was intentionally left out for |
| 1409 | the following reasons: |
| 1410 | |
| 1411 | 1) Itertools are designed to be easily combined and chained together. |
| 1412 | Having all tools stop with the shortest input is a unifying principle |
| 1413 | that makes it easier to combine finite iterators (supplying data) with |
| 1414 | infinite iterators like count() and repeat() (for supplying sequential |
| 1415 | or constant arguments to a function). |
| 1416 | |
| 1417 | 2) In typical use cases for combining itertools, having one finite data |
| 1418 | supplier run out before another is likely to be an error condition which |
| 1419 | should not pass silently by automatically supplying None. |
| 1420 | |
| 1421 | 3) The use cases for automatic None fill-in are rare -- not many functions |
| 1422 | do something useful when a parameter suddenly switches type and becomes |
| 1423 | None. |
| 1424 | |
| 1425 | 4) If a need does arise, it can be met by __builtins__.map() or by |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 1426 | writing: chain(iterable, repeat(None)). |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1427 | |
| 1428 | 5) Similar toolsets in Haskell and SML do not have automatic None fill-in. |
| 1429 | */ |
| 1430 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1431 | static PyObject * |
| 1432 | imap_next(imapobject *lz) |
| 1433 | { |
| 1434 | PyObject *val; |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1435 | PyObject *argtuple; |
| 1436 | PyObject *result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1437 | int numargs, i; |
| 1438 | |
| 1439 | numargs = PyTuple_Size(lz->iters); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1440 | argtuple = PyTuple_New(numargs); |
| 1441 | if (argtuple == NULL) |
| 1442 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1443 | |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1444 | for (i=0 ; i<numargs ; i++) { |
| 1445 | val = PyIter_Next(PyTuple_GET_ITEM(lz->iters, i)); |
| 1446 | if (val == NULL) { |
| 1447 | Py_DECREF(argtuple); |
| 1448 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1449 | } |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1450 | PyTuple_SET_ITEM(argtuple, i, val); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1451 | } |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 1452 | if (lz->func == Py_None) |
| 1453 | return argtuple; |
| 1454 | result = PyObject_Call(lz->func, argtuple, NULL); |
| 1455 | Py_DECREF(argtuple); |
| 1456 | return result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1459 | PyDoc_STRVAR(imap_doc, |
| 1460 | "imap(func, *iterables) --> imap object\n\ |
| 1461 | \n\ |
| 1462 | Make an iterator that computes the function using arguments from\n\ |
| 1463 | each of the iterables. Like map() except that it returns\n\ |
| 1464 | an iterator instead of a list and that it stops when the shortest\n\ |
| 1465 | iterable is exhausted instead of filling in None for shorter\n\ |
| 1466 | iterables."); |
| 1467 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1468 | static PyTypeObject imap_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1469 | PyObject_HEAD_INIT(NULL) |
| 1470 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1471 | "itertools.imap", /* tp_name */ |
| 1472 | sizeof(imapobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1473 | 0, /* tp_itemsize */ |
| 1474 | /* methods */ |
| 1475 | (destructor)imap_dealloc, /* tp_dealloc */ |
| 1476 | 0, /* tp_print */ |
| 1477 | 0, /* tp_getattr */ |
| 1478 | 0, /* tp_setattr */ |
| 1479 | 0, /* tp_compare */ |
| 1480 | 0, /* tp_repr */ |
| 1481 | 0, /* tp_as_number */ |
| 1482 | 0, /* tp_as_sequence */ |
| 1483 | 0, /* tp_as_mapping */ |
| 1484 | 0, /* tp_hash */ |
| 1485 | 0, /* tp_call */ |
| 1486 | 0, /* tp_str */ |
| 1487 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1488 | 0, /* tp_setattro */ |
| 1489 | 0, /* tp_as_buffer */ |
| 1490 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1491 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1492 | imap_doc, /* tp_doc */ |
| 1493 | (traverseproc)imap_traverse, /* tp_traverse */ |
| 1494 | 0, /* tp_clear */ |
| 1495 | 0, /* tp_richcompare */ |
| 1496 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1497 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1498 | (iternextfunc)imap_next, /* tp_iternext */ |
| 1499 | 0, /* tp_methods */ |
| 1500 | 0, /* tp_members */ |
| 1501 | 0, /* tp_getset */ |
| 1502 | 0, /* tp_base */ |
| 1503 | 0, /* tp_dict */ |
| 1504 | 0, /* tp_descr_get */ |
| 1505 | 0, /* tp_descr_set */ |
| 1506 | 0, /* tp_dictoffset */ |
| 1507 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1508 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1509 | imap_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1510 | PyObject_GC_Del, /* tp_free */ |
| 1511 | }; |
| 1512 | |
| 1513 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1514 | /* chain object ************************************************************/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1515 | |
| 1516 | typedef struct { |
| 1517 | PyObject_HEAD |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1518 | long tuplesize; |
| 1519 | long iternum; /* which iterator is active */ |
| 1520 | PyObject *ittuple; /* tuple of iterators */ |
| 1521 | } chainobject; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1522 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1523 | static PyTypeObject chain_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1524 | |
| 1525 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1526 | chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1527 | { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1528 | chainobject *lz; |
| 1529 | int tuplesize = PySequence_Length(args); |
| 1530 | int i; |
| 1531 | PyObject *ittuple; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1532 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1533 | /* obtain iterators */ |
| 1534 | assert(PyTuple_Check(args)); |
| 1535 | ittuple = PyTuple_New(tuplesize); |
| 1536 | if(ittuple == NULL) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1537 | return NULL; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1538 | for (i=0; i < tuplesize; ++i) { |
| 1539 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 1540 | PyObject *it = PyObject_GetIter(item); |
| 1541 | if (it == NULL) { |
| 1542 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1543 | PyErr_Format(PyExc_TypeError, |
| 1544 | "chain argument #%d must support iteration", |
| 1545 | i+1); |
| 1546 | Py_DECREF(ittuple); |
| 1547 | return NULL; |
| 1548 | } |
| 1549 | PyTuple_SET_ITEM(ittuple, i, it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1552 | /* create chainobject structure */ |
| 1553 | lz = (chainobject *)type->tp_alloc(type, 0); |
Raymond Hettinger | 7d98fb9 | 2003-06-17 23:14:40 +0000 | [diff] [blame] | 1554 | if (lz == NULL) { |
| 1555 | Py_DECREF(ittuple); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1556 | return NULL; |
Raymond Hettinger | 7d98fb9 | 2003-06-17 23:14:40 +0000 | [diff] [blame] | 1557 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1558 | |
| 1559 | lz->ittuple = ittuple; |
| 1560 | lz->iternum = 0; |
| 1561 | lz->tuplesize = tuplesize; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1562 | |
| 1563 | return (PyObject *)lz; |
| 1564 | } |
| 1565 | |
| 1566 | static void |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1567 | chain_dealloc(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1568 | { |
| 1569 | PyObject_GC_UnTrack(lz); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1570 | Py_XDECREF(lz->ittuple); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1571 | lz->ob_type->tp_free(lz); |
| 1572 | } |
| 1573 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1574 | static int |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1575 | chain_traverse(chainobject *lz, visitproc visit, void *arg) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1576 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1577 | Py_VISIT(lz->ittuple); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1578 | return 0; |
| 1579 | } |
| 1580 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1581 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1582 | chain_next(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1583 | { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1584 | PyObject *it; |
| 1585 | PyObject *item; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1586 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1587 | while (lz->iternum < lz->tuplesize) { |
| 1588 | it = PyTuple_GET_ITEM(lz->ittuple, lz->iternum); |
| 1589 | item = PyIter_Next(it); |
| 1590 | if (item != NULL) |
| 1591 | return item; |
Raymond Hettinger | 9d7c870 | 2004-05-08 19:49:42 +0000 | [diff] [blame] | 1592 | if (PyErr_Occurred()) { |
| 1593 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 1594 | PyErr_Clear(); |
| 1595 | else |
| 1596 | return NULL; |
| 1597 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1598 | lz->iternum++; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1599 | } |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1603 | PyDoc_STRVAR(chain_doc, |
| 1604 | "chain(*iterables) --> chain object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1605 | \n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1606 | Return a chain object whose .next() method returns elements from the\n\ |
| 1607 | first iterable until it is exhausted, then elements from the next\n\ |
| 1608 | iterable, until all of the iterables are exhausted."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1609 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1610 | static PyTypeObject chain_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1611 | PyObject_HEAD_INIT(NULL) |
| 1612 | 0, /* ob_size */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1613 | "itertools.chain", /* tp_name */ |
| 1614 | sizeof(chainobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1615 | 0, /* tp_itemsize */ |
| 1616 | /* methods */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1617 | (destructor)chain_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1618 | 0, /* tp_print */ |
| 1619 | 0, /* tp_getattr */ |
| 1620 | 0, /* tp_setattr */ |
| 1621 | 0, /* tp_compare */ |
| 1622 | 0, /* tp_repr */ |
| 1623 | 0, /* tp_as_number */ |
| 1624 | 0, /* tp_as_sequence */ |
| 1625 | 0, /* tp_as_mapping */ |
| 1626 | 0, /* tp_hash */ |
| 1627 | 0, /* tp_call */ |
| 1628 | 0, /* tp_str */ |
| 1629 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1630 | 0, /* tp_setattro */ |
| 1631 | 0, /* tp_as_buffer */ |
| 1632 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1633 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1634 | chain_doc, /* tp_doc */ |
| 1635 | (traverseproc)chain_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1636 | 0, /* tp_clear */ |
| 1637 | 0, /* tp_richcompare */ |
| 1638 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1639 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1640 | (iternextfunc)chain_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1641 | 0, /* tp_methods */ |
| 1642 | 0, /* tp_members */ |
| 1643 | 0, /* tp_getset */ |
| 1644 | 0, /* tp_base */ |
| 1645 | 0, /* tp_dict */ |
| 1646 | 0, /* tp_descr_get */ |
| 1647 | 0, /* tp_descr_set */ |
| 1648 | 0, /* tp_dictoffset */ |
| 1649 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1650 | 0, /* tp_alloc */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1651 | chain_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1652 | PyObject_GC_Del, /* tp_free */ |
| 1653 | }; |
| 1654 | |
| 1655 | |
| 1656 | /* ifilter object ************************************************************/ |
| 1657 | |
| 1658 | typedef struct { |
| 1659 | PyObject_HEAD |
| 1660 | PyObject *func; |
| 1661 | PyObject *it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1662 | } ifilterobject; |
| 1663 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1664 | static PyTypeObject ifilter_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1665 | |
| 1666 | static PyObject * |
| 1667 | ifilter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1668 | { |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1669 | PyObject *func, *seq; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1670 | PyObject *it; |
| 1671 | ifilterobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1672 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1673 | if (!PyArg_UnpackTuple(args, "ifilter", 2, 2, &func, &seq)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1674 | return NULL; |
| 1675 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1676 | /* Get iterator. */ |
| 1677 | it = PyObject_GetIter(seq); |
| 1678 | if (it == NULL) |
| 1679 | return NULL; |
| 1680 | |
| 1681 | /* create ifilterobject structure */ |
| 1682 | lz = (ifilterobject *)type->tp_alloc(type, 0); |
| 1683 | if (lz == NULL) { |
| 1684 | Py_DECREF(it); |
| 1685 | return NULL; |
| 1686 | } |
| 1687 | Py_INCREF(func); |
| 1688 | lz->func = func; |
| 1689 | lz->it = it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1690 | |
| 1691 | return (PyObject *)lz; |
| 1692 | } |
| 1693 | |
| 1694 | static void |
| 1695 | ifilter_dealloc(ifilterobject *lz) |
| 1696 | { |
| 1697 | PyObject_GC_UnTrack(lz); |
| 1698 | Py_XDECREF(lz->func); |
| 1699 | Py_XDECREF(lz->it); |
| 1700 | lz->ob_type->tp_free(lz); |
| 1701 | } |
| 1702 | |
| 1703 | static int |
| 1704 | ifilter_traverse(ifilterobject *lz, visitproc visit, void *arg) |
| 1705 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1706 | Py_VISIT(lz->it); |
| 1707 | Py_VISIT(lz->func); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static PyObject * |
| 1712 | ifilter_next(ifilterobject *lz) |
| 1713 | { |
| 1714 | PyObject *item; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1715 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1716 | long ok; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1717 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1718 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1719 | assert(PyIter_Check(it)); |
| 1720 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1721 | for (;;) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1722 | item = iternext(it); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1723 | if (item == NULL) |
| 1724 | return NULL; |
| 1725 | |
| 1726 | if (lz->func == Py_None) { |
| 1727 | ok = PyObject_IsTrue(item); |
| 1728 | } else { |
| 1729 | PyObject *good; |
| 1730 | good = PyObject_CallFunctionObjArgs(lz->func, |
| 1731 | item, NULL); |
| 1732 | if (good == NULL) { |
| 1733 | Py_DECREF(item); |
| 1734 | return NULL; |
| 1735 | } |
| 1736 | ok = PyObject_IsTrue(good); |
| 1737 | Py_DECREF(good); |
| 1738 | } |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1739 | if (ok) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1740 | return item; |
| 1741 | Py_DECREF(item); |
| 1742 | } |
| 1743 | } |
| 1744 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1745 | PyDoc_STRVAR(ifilter_doc, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1746 | "ifilter(function or None, sequence) --> ifilter object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1747 | \n\ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1748 | Return those items of sequence for which function(item) is true.\n\ |
| 1749 | If function is None, return the items that are true."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1750 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1751 | static PyTypeObject ifilter_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1752 | PyObject_HEAD_INIT(NULL) |
| 1753 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1754 | "itertools.ifilter", /* tp_name */ |
| 1755 | sizeof(ifilterobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1756 | 0, /* tp_itemsize */ |
| 1757 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1758 | (destructor)ifilter_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1759 | 0, /* tp_print */ |
| 1760 | 0, /* tp_getattr */ |
| 1761 | 0, /* tp_setattr */ |
| 1762 | 0, /* tp_compare */ |
| 1763 | 0, /* tp_repr */ |
| 1764 | 0, /* tp_as_number */ |
| 1765 | 0, /* tp_as_sequence */ |
| 1766 | 0, /* tp_as_mapping */ |
| 1767 | 0, /* tp_hash */ |
| 1768 | 0, /* tp_call */ |
| 1769 | 0, /* tp_str */ |
| 1770 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1771 | 0, /* tp_setattro */ |
| 1772 | 0, /* tp_as_buffer */ |
| 1773 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1774 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1775 | ifilter_doc, /* tp_doc */ |
| 1776 | (traverseproc)ifilter_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1777 | 0, /* tp_clear */ |
| 1778 | 0, /* tp_richcompare */ |
| 1779 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1780 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1781 | (iternextfunc)ifilter_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1782 | 0, /* tp_methods */ |
| 1783 | 0, /* tp_members */ |
| 1784 | 0, /* tp_getset */ |
| 1785 | 0, /* tp_base */ |
| 1786 | 0, /* tp_dict */ |
| 1787 | 0, /* tp_descr_get */ |
| 1788 | 0, /* tp_descr_set */ |
| 1789 | 0, /* tp_dictoffset */ |
| 1790 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1791 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1792 | ifilter_new, /* tp_new */ |
| 1793 | PyObject_GC_Del, /* tp_free */ |
| 1794 | }; |
| 1795 | |
| 1796 | |
| 1797 | /* ifilterfalse object ************************************************************/ |
| 1798 | |
| 1799 | typedef struct { |
| 1800 | PyObject_HEAD |
| 1801 | PyObject *func; |
| 1802 | PyObject *it; |
| 1803 | } ifilterfalseobject; |
| 1804 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1805 | static PyTypeObject ifilterfalse_type; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1806 | |
| 1807 | static PyObject * |
| 1808 | ifilterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1809 | { |
Guido van Rossum | d58f3fc | 2003-02-09 17:19:18 +0000 | [diff] [blame] | 1810 | PyObject *func, *seq; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1811 | PyObject *it; |
| 1812 | ifilterfalseobject *lz; |
| 1813 | |
| 1814 | if (!PyArg_UnpackTuple(args, "ifilterfalse", 2, 2, &func, &seq)) |
| 1815 | return NULL; |
| 1816 | |
| 1817 | /* Get iterator. */ |
| 1818 | it = PyObject_GetIter(seq); |
| 1819 | if (it == NULL) |
| 1820 | return NULL; |
| 1821 | |
| 1822 | /* create ifilterfalseobject structure */ |
| 1823 | lz = (ifilterfalseobject *)type->tp_alloc(type, 0); |
| 1824 | if (lz == NULL) { |
| 1825 | Py_DECREF(it); |
| 1826 | return NULL; |
| 1827 | } |
| 1828 | Py_INCREF(func); |
| 1829 | lz->func = func; |
| 1830 | lz->it = it; |
| 1831 | |
| 1832 | return (PyObject *)lz; |
| 1833 | } |
| 1834 | |
| 1835 | static void |
| 1836 | ifilterfalse_dealloc(ifilterfalseobject *lz) |
| 1837 | { |
| 1838 | PyObject_GC_UnTrack(lz); |
| 1839 | Py_XDECREF(lz->func); |
| 1840 | Py_XDECREF(lz->it); |
| 1841 | lz->ob_type->tp_free(lz); |
| 1842 | } |
| 1843 | |
| 1844 | static int |
| 1845 | ifilterfalse_traverse(ifilterfalseobject *lz, visitproc visit, void *arg) |
| 1846 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 1847 | Py_VISIT(lz->it); |
| 1848 | Py_VISIT(lz->func); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | static PyObject * |
| 1853 | ifilterfalse_next(ifilterfalseobject *lz) |
| 1854 | { |
| 1855 | PyObject *item; |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 1856 | PyObject *it = lz->it; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1857 | long ok; |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1858 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1859 | |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1860 | assert(PyIter_Check(it)); |
| 1861 | iternext = *it->ob_type->tp_iternext; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1862 | for (;;) { |
Raymond Hettinger | 0faa1ca | 2004-03-17 04:27:44 +0000 | [diff] [blame] | 1863 | item = iternext(it); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1864 | if (item == NULL) |
| 1865 | return NULL; |
| 1866 | |
| 1867 | if (lz->func == Py_None) { |
| 1868 | ok = PyObject_IsTrue(item); |
| 1869 | } else { |
| 1870 | PyObject *good; |
| 1871 | good = PyObject_CallFunctionObjArgs(lz->func, |
| 1872 | item, NULL); |
| 1873 | if (good == NULL) { |
| 1874 | Py_DECREF(item); |
| 1875 | return NULL; |
| 1876 | } |
| 1877 | ok = PyObject_IsTrue(good); |
| 1878 | Py_DECREF(good); |
| 1879 | } |
| 1880 | if (!ok) |
| 1881 | return item; |
| 1882 | Py_DECREF(item); |
| 1883 | } |
| 1884 | } |
| 1885 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1886 | PyDoc_STRVAR(ifilterfalse_doc, |
| 1887 | "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\ |
| 1888 | \n\ |
| 1889 | Return those items of sequence for which function(item) is false.\n\ |
| 1890 | If function is None, return the items that are false."); |
| 1891 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1892 | static PyTypeObject ifilterfalse_type = { |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1893 | PyObject_HEAD_INIT(NULL) |
| 1894 | 0, /* ob_size */ |
| 1895 | "itertools.ifilterfalse", /* tp_name */ |
| 1896 | sizeof(ifilterfalseobject), /* tp_basicsize */ |
| 1897 | 0, /* tp_itemsize */ |
| 1898 | /* methods */ |
| 1899 | (destructor)ifilterfalse_dealloc, /* tp_dealloc */ |
| 1900 | 0, /* tp_print */ |
| 1901 | 0, /* tp_getattr */ |
| 1902 | 0, /* tp_setattr */ |
| 1903 | 0, /* tp_compare */ |
| 1904 | 0, /* tp_repr */ |
| 1905 | 0, /* tp_as_number */ |
| 1906 | 0, /* tp_as_sequence */ |
| 1907 | 0, /* tp_as_mapping */ |
| 1908 | 0, /* tp_hash */ |
| 1909 | 0, /* tp_call */ |
| 1910 | 0, /* tp_str */ |
| 1911 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1912 | 0, /* tp_setattro */ |
| 1913 | 0, /* tp_as_buffer */ |
| 1914 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1915 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1916 | ifilterfalse_doc, /* tp_doc */ |
| 1917 | (traverseproc)ifilterfalse_traverse, /* tp_traverse */ |
| 1918 | 0, /* tp_clear */ |
| 1919 | 0, /* tp_richcompare */ |
| 1920 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 1921 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1922 | (iternextfunc)ifilterfalse_next, /* tp_iternext */ |
| 1923 | 0, /* tp_methods */ |
| 1924 | 0, /* tp_members */ |
| 1925 | 0, /* tp_getset */ |
| 1926 | 0, /* tp_base */ |
| 1927 | 0, /* tp_dict */ |
| 1928 | 0, /* tp_descr_get */ |
| 1929 | 0, /* tp_descr_set */ |
| 1930 | 0, /* tp_dictoffset */ |
| 1931 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 1932 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1933 | ifilterfalse_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1934 | PyObject_GC_Del, /* tp_free */ |
| 1935 | }; |
| 1936 | |
| 1937 | |
| 1938 | /* count object ************************************************************/ |
| 1939 | |
| 1940 | typedef struct { |
| 1941 | PyObject_HEAD |
| 1942 | long cnt; |
| 1943 | } countobject; |
| 1944 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1945 | static PyTypeObject count_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1946 | |
| 1947 | static PyObject * |
| 1948 | count_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1949 | { |
| 1950 | countobject *lz; |
| 1951 | long cnt = 0; |
| 1952 | |
| 1953 | if (!PyArg_ParseTuple(args, "|l:count", &cnt)) |
| 1954 | return NULL; |
| 1955 | |
| 1956 | /* create countobject structure */ |
| 1957 | lz = (countobject *)PyObject_New(countobject, &count_type); |
| 1958 | if (lz == NULL) |
| 1959 | return NULL; |
| 1960 | lz->cnt = cnt; |
| 1961 | |
| 1962 | return (PyObject *)lz; |
| 1963 | } |
| 1964 | |
| 1965 | static PyObject * |
| 1966 | count_next(countobject *lz) |
| 1967 | { |
| 1968 | return PyInt_FromLong(lz->cnt++); |
| 1969 | } |
| 1970 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 1971 | static PyObject * |
| 1972 | count_repr(countobject *lz) |
| 1973 | { |
Brett Cannon | 0046839 | 2004-04-13 02:43:53 +0000 | [diff] [blame] | 1974 | return PyString_FromFormat("count(%ld)", lz->cnt); |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1977 | PyDoc_STRVAR(count_doc, |
| 1978 | "count([firstval]) --> count object\n\ |
| 1979 | \n\ |
| 1980 | Return a count object whose .next() method returns consecutive\n\ |
| 1981 | integers starting from zero or, if specified, from firstval."); |
| 1982 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1983 | static PyTypeObject count_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1984 | PyObject_HEAD_INIT(NULL) |
| 1985 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 1986 | "itertools.count", /* tp_name */ |
| 1987 | sizeof(countobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1988 | 0, /* tp_itemsize */ |
| 1989 | /* methods */ |
| 1990 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 1991 | 0, /* tp_print */ |
| 1992 | 0, /* tp_getattr */ |
| 1993 | 0, /* tp_setattr */ |
| 1994 | 0, /* tp_compare */ |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 1995 | (reprfunc)count_repr, /* tp_repr */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1996 | 0, /* tp_as_number */ |
| 1997 | 0, /* tp_as_sequence */ |
| 1998 | 0, /* tp_as_mapping */ |
| 1999 | 0, /* tp_hash */ |
| 2000 | 0, /* tp_call */ |
| 2001 | 0, /* tp_str */ |
| 2002 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2003 | 0, /* tp_setattro */ |
| 2004 | 0, /* tp_as_buffer */ |
| 2005 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2006 | count_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2007 | 0, /* tp_traverse */ |
| 2008 | 0, /* tp_clear */ |
| 2009 | 0, /* tp_richcompare */ |
| 2010 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2011 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2012 | (iternextfunc)count_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2013 | 0, /* tp_methods */ |
| 2014 | 0, /* tp_members */ |
| 2015 | 0, /* tp_getset */ |
| 2016 | 0, /* tp_base */ |
| 2017 | 0, /* tp_dict */ |
| 2018 | 0, /* tp_descr_get */ |
| 2019 | 0, /* tp_descr_set */ |
| 2020 | 0, /* tp_dictoffset */ |
| 2021 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 2022 | 0, /* tp_alloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2023 | count_new, /* tp_new */ |
| 2024 | }; |
| 2025 | |
| 2026 | |
| 2027 | /* izip object ************************************************************/ |
| 2028 | |
| 2029 | #include "Python.h" |
| 2030 | |
| 2031 | typedef struct { |
| 2032 | PyObject_HEAD |
| 2033 | long tuplesize; |
| 2034 | PyObject *ittuple; /* tuple of iterators */ |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2035 | PyObject *result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2036 | } izipobject; |
| 2037 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2038 | static PyTypeObject izip_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2039 | |
| 2040 | static PyObject * |
| 2041 | izip_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2042 | { |
| 2043 | izipobject *lz; |
| 2044 | int i; |
| 2045 | PyObject *ittuple; /* tuple of iterators */ |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2046 | PyObject *result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2047 | int tuplesize = PySequence_Length(args); |
| 2048 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2049 | /* args must be a tuple */ |
| 2050 | assert(PyTuple_Check(args)); |
| 2051 | |
| 2052 | /* obtain iterators */ |
| 2053 | ittuple = PyTuple_New(tuplesize); |
| 2054 | if(ittuple == NULL) |
| 2055 | return NULL; |
| 2056 | for (i=0; i < tuplesize; ++i) { |
| 2057 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 2058 | PyObject *it = PyObject_GetIter(item); |
| 2059 | if (it == NULL) { |
| 2060 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2061 | PyErr_Format(PyExc_TypeError, |
| 2062 | "izip argument #%d must support iteration", |
| 2063 | i+1); |
| 2064 | Py_DECREF(ittuple); |
| 2065 | return NULL; |
| 2066 | } |
| 2067 | PyTuple_SET_ITEM(ittuple, i, it); |
| 2068 | } |
| 2069 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2070 | /* create a result holder */ |
| 2071 | result = PyTuple_New(tuplesize); |
| 2072 | if (result == NULL) { |
| 2073 | Py_DECREF(ittuple); |
| 2074 | return NULL; |
| 2075 | } |
| 2076 | for (i=0 ; i < tuplesize ; i++) { |
| 2077 | Py_INCREF(Py_None); |
| 2078 | PyTuple_SET_ITEM(result, i, Py_None); |
| 2079 | } |
| 2080 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2081 | /* create izipobject structure */ |
| 2082 | lz = (izipobject *)type->tp_alloc(type, 0); |
| 2083 | if (lz == NULL) { |
| 2084 | Py_DECREF(ittuple); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2085 | Py_DECREF(result); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2086 | return NULL; |
| 2087 | } |
| 2088 | lz->ittuple = ittuple; |
| 2089 | lz->tuplesize = tuplesize; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2090 | lz->result = result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2091 | |
| 2092 | return (PyObject *)lz; |
| 2093 | } |
| 2094 | |
| 2095 | static void |
| 2096 | izip_dealloc(izipobject *lz) |
| 2097 | { |
| 2098 | PyObject_GC_UnTrack(lz); |
| 2099 | Py_XDECREF(lz->ittuple); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2100 | Py_XDECREF(lz->result); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2101 | lz->ob_type->tp_free(lz); |
| 2102 | } |
| 2103 | |
| 2104 | static int |
| 2105 | izip_traverse(izipobject *lz, visitproc visit, void *arg) |
| 2106 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 2107 | Py_VISIT(lz->ittuple); |
| 2108 | Py_VISIT(lz->result); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2109 | return 0; |
| 2110 | } |
| 2111 | |
| 2112 | static PyObject * |
| 2113 | izip_next(izipobject *lz) |
| 2114 | { |
| 2115 | int i; |
| 2116 | long tuplesize = lz->tuplesize; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2117 | PyObject *result = lz->result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2118 | PyObject *it; |
| 2119 | PyObject *item; |
Raymond Hettinger | 4f01f89 | 2003-08-30 00:10:06 +0000 | [diff] [blame] | 2120 | PyObject *olditem; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2121 | |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 2122 | if (tuplesize == 0) |
| 2123 | return NULL; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2124 | if (result->ob_refcnt == 1) { |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 2125 | Py_INCREF(result); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2126 | for (i=0 ; i < tuplesize ; i++) { |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2127 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 2128 | assert(PyIter_Check(it)); |
| 2129 | item = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 2130 | if (item == NULL) { |
| 2131 | Py_DECREF(result); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2132 | return NULL; |
Raymond Hettinger | a56f6b6 | 2003-08-29 23:09:58 +0000 | [diff] [blame] | 2133 | } |
Raymond Hettinger | 4f01f89 | 2003-08-30 00:10:06 +0000 | [diff] [blame] | 2134 | olditem = PyTuple_GET_ITEM(result, i); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2135 | PyTuple_SET_ITEM(result, i, item); |
Raymond Hettinger | 4f01f89 | 2003-08-30 00:10:06 +0000 | [diff] [blame] | 2136 | Py_DECREF(olditem); |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2137 | } |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2138 | } else { |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 2139 | result = PyTuple_New(tuplesize); |
| 2140 | if (result == NULL) |
| 2141 | return NULL; |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2142 | for (i=0 ; i < tuplesize ; i++) { |
| 2143 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
Raymond Hettinger | d1a283b | 2003-03-01 01:48:24 +0000 | [diff] [blame] | 2144 | assert(PyIter_Check(it)); |
| 2145 | item = (*it->ob_type->tp_iternext)(it); |
Raymond Hettinger | f0c0024 | 2003-02-07 07:26:25 +0000 | [diff] [blame] | 2146 | if (item == NULL) { |
| 2147 | Py_DECREF(result); |
| 2148 | return NULL; |
| 2149 | } |
| 2150 | PyTuple_SET_ITEM(result, i, item); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2151 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2152 | } |
| 2153 | return result; |
| 2154 | } |
| 2155 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2156 | PyDoc_STRVAR(izip_doc, |
| 2157 | "izip(iter1 [,iter2 [...]]) --> izip object\n\ |
| 2158 | \n\ |
| 2159 | Return a izip object whose .next() method returns a tuple where\n\ |
| 2160 | the i-th element comes from the i-th iterable argument. The .next()\n\ |
| 2161 | method continues until the shortest iterable in the argument sequence\n\ |
| 2162 | is exhausted and then it raises StopIteration. Works like the zip()\n\ |
| 2163 | function but consumes less memory by returning an iterator instead of\n\ |
| 2164 | a list."); |
| 2165 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2166 | static PyTypeObject izip_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2167 | PyObject_HEAD_INIT(NULL) |
| 2168 | 0, /* ob_size */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2169 | "itertools.izip", /* tp_name */ |
| 2170 | sizeof(izipobject), /* tp_basicsize */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2171 | 0, /* tp_itemsize */ |
| 2172 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2173 | (destructor)izip_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2174 | 0, /* tp_print */ |
| 2175 | 0, /* tp_getattr */ |
| 2176 | 0, /* tp_setattr */ |
| 2177 | 0, /* tp_compare */ |
| 2178 | 0, /* tp_repr */ |
| 2179 | 0, /* tp_as_number */ |
| 2180 | 0, /* tp_as_sequence */ |
| 2181 | 0, /* tp_as_mapping */ |
| 2182 | 0, /* tp_hash */ |
| 2183 | 0, /* tp_call */ |
| 2184 | 0, /* tp_str */ |
| 2185 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2186 | 0, /* tp_setattro */ |
| 2187 | 0, /* tp_as_buffer */ |
| 2188 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2189 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2190 | izip_doc, /* tp_doc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2191 | (traverseproc)izip_traverse, /* tp_traverse */ |
| 2192 | 0, /* tp_clear */ |
| 2193 | 0, /* tp_richcompare */ |
| 2194 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2195 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2196 | (iternextfunc)izip_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2197 | 0, /* tp_methods */ |
| 2198 | 0, /* tp_members */ |
| 2199 | 0, /* tp_getset */ |
| 2200 | 0, /* tp_base */ |
| 2201 | 0, /* tp_dict */ |
| 2202 | 0, /* tp_descr_get */ |
| 2203 | 0, /* tp_descr_set */ |
| 2204 | 0, /* tp_dictoffset */ |
| 2205 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 2206 | 0, /* tp_alloc */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2207 | izip_new, /* tp_new */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2208 | PyObject_GC_Del, /* tp_free */ |
| 2209 | }; |
| 2210 | |
| 2211 | |
| 2212 | /* repeat object ************************************************************/ |
| 2213 | |
| 2214 | typedef struct { |
| 2215 | PyObject_HEAD |
| 2216 | PyObject *element; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2217 | long cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2218 | } repeatobject; |
| 2219 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2220 | static PyTypeObject repeat_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2221 | |
| 2222 | static PyObject * |
| 2223 | repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2224 | { |
| 2225 | repeatobject *ro; |
| 2226 | PyObject *element; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2227 | long cnt = -1; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2228 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2229 | if (!PyArg_ParseTuple(args, "O|l:repeat", &element, &cnt)) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2230 | return NULL; |
| 2231 | |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 2232 | if (PyTuple_Size(args) == 2 && cnt < 0) |
| 2233 | cnt = 0; |
| 2234 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2235 | ro = (repeatobject *)type->tp_alloc(type, 0); |
| 2236 | if (ro == NULL) |
| 2237 | return NULL; |
| 2238 | Py_INCREF(element); |
| 2239 | ro->element = element; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2240 | ro->cnt = cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2241 | return (PyObject *)ro; |
| 2242 | } |
| 2243 | |
| 2244 | static void |
| 2245 | repeat_dealloc(repeatobject *ro) |
| 2246 | { |
| 2247 | PyObject_GC_UnTrack(ro); |
| 2248 | Py_XDECREF(ro->element); |
| 2249 | ro->ob_type->tp_free(ro); |
| 2250 | } |
| 2251 | |
| 2252 | static int |
| 2253 | repeat_traverse(repeatobject *ro, visitproc visit, void *arg) |
| 2254 | { |
Raymond Hettinger | 58ed69b | 2004-07-15 05:32:47 +0000 | [diff] [blame] | 2255 | Py_VISIT(ro->element); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | static PyObject * |
| 2260 | repeat_next(repeatobject *ro) |
| 2261 | { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2262 | if (ro->cnt == 0) |
| 2263 | return NULL; |
| 2264 | if (ro->cnt > 0) |
| 2265 | ro->cnt--; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2266 | Py_INCREF(ro->element); |
| 2267 | return ro->element; |
| 2268 | } |
| 2269 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2270 | static PyObject * |
| 2271 | repeat_repr(repeatobject *ro) |
| 2272 | { |
| 2273 | PyObject *result, *objrepr; |
| 2274 | |
| 2275 | objrepr = PyObject_Repr(ro->element); |
| 2276 | if (objrepr == NULL) |
| 2277 | return NULL; |
| 2278 | |
| 2279 | if (ro->cnt == -1) |
| 2280 | result = PyString_FromFormat("repeat(%s)", |
| 2281 | PyString_AS_STRING(objrepr)); |
| 2282 | else |
Brett Cannon | 0046839 | 2004-04-13 02:43:53 +0000 | [diff] [blame] | 2283 | result = PyString_FromFormat("repeat(%s, %ld)", |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2284 | PyString_AS_STRING(objrepr), ro->cnt); |
| 2285 | Py_DECREF(objrepr); |
| 2286 | return result; |
| 2287 | } |
| 2288 | |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 2289 | static int |
| 2290 | repeat_len(repeatobject *ro) |
| 2291 | { |
| 2292 | if (ro->cnt == -1) |
| 2293 | PyErr_SetString(PyExc_TypeError, "len() of unsized object"); |
| 2294 | return (int)(ro->cnt); |
| 2295 | } |
| 2296 | |
| 2297 | static PySequenceMethods repeat_as_sequence = { |
| 2298 | (inquiry)repeat_len, /* sq_length */ |
| 2299 | 0, /* sq_concat */ |
| 2300 | }; |
| 2301 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2302 | PyDoc_STRVAR(repeat_doc, |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2303 | "repeat(element [,times]) -> create an iterator which returns the element\n\ |
| 2304 | for the specified number of times. If not specified, returns the element\n\ |
| 2305 | endlessly."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2306 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 2307 | static PyTypeObject repeat_type = { |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2308 | PyObject_HEAD_INIT(NULL) |
| 2309 | 0, /* ob_size */ |
| 2310 | "itertools.repeat", /* tp_name */ |
| 2311 | sizeof(repeatobject), /* tp_basicsize */ |
| 2312 | 0, /* tp_itemsize */ |
| 2313 | /* methods */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2314 | (destructor)repeat_dealloc, /* tp_dealloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2315 | 0, /* tp_print */ |
| 2316 | 0, /* tp_getattr */ |
| 2317 | 0, /* tp_setattr */ |
| 2318 | 0, /* tp_compare */ |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 2319 | (reprfunc)repeat_repr, /* tp_repr */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2320 | 0, /* tp_as_number */ |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 2321 | &repeat_as_sequence, /* tp_as_sequence */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2322 | 0, /* tp_as_mapping */ |
| 2323 | 0, /* tp_hash */ |
| 2324 | 0, /* tp_call */ |
| 2325 | 0, /* tp_str */ |
| 2326 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2327 | 0, /* tp_setattro */ |
| 2328 | 0, /* tp_as_buffer */ |
| 2329 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2330 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2331 | repeat_doc, /* tp_doc */ |
| 2332 | (traverseproc)repeat_traverse, /* tp_traverse */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2333 | 0, /* tp_clear */ |
| 2334 | 0, /* tp_richcompare */ |
| 2335 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2336 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2337 | (iternextfunc)repeat_next, /* tp_iternext */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2338 | 0, /* tp_methods */ |
| 2339 | 0, /* tp_members */ |
| 2340 | 0, /* tp_getset */ |
| 2341 | 0, /* tp_base */ |
| 2342 | 0, /* tp_dict */ |
| 2343 | 0, /* tp_descr_get */ |
| 2344 | 0, /* tp_descr_set */ |
| 2345 | 0, /* tp_dictoffset */ |
| 2346 | 0, /* tp_init */ |
Raymond Hettinger | bfef18c | 2003-05-23 03:55:42 +0000 | [diff] [blame] | 2347 | 0, /* tp_alloc */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2348 | repeat_new, /* tp_new */ |
| 2349 | PyObject_GC_Del, /* tp_free */ |
| 2350 | }; |
| 2351 | |
| 2352 | |
| 2353 | /* module level code ********************************************************/ |
| 2354 | |
| 2355 | PyDoc_STRVAR(module_doc, |
| 2356 | "Functional tools for creating and using iterators.\n\ |
| 2357 | \n\ |
| 2358 | Infinite iterators:\n\ |
| 2359 | count([n]) --> n, n+1, n+2, ...\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2360 | cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\ |
Andrew M. Kuchling | dff694b | 2003-04-14 15:31:27 +0000 | [diff] [blame] | 2361 | repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2362 | \n\ |
| 2363 | Iterators terminating on the shortest input sequence:\n\ |
| 2364 | izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2365 | ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\ |
| 2366 | ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2367 | islice(seq, [start,] stop [, step]) --> elements from\n\ |
| 2368 | seq[start:stop:step]\n\ |
| 2369 | imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\ |
| 2370 | starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2371 | tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2372 | chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2373 | takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\ |
| 2374 | dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 2375 | groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2376 | "); |
| 2377 | |
| 2378 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2379 | static PyMethodDef module_methods[] = { |
| 2380 | {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc}, |
| 2381 | {NULL, NULL} /* sentinel */ |
| 2382 | }; |
| 2383 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2384 | PyMODINIT_FUNC |
| 2385 | inititertools(void) |
| 2386 | { |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2387 | int i; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2388 | PyObject *m; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2389 | char *name; |
| 2390 | PyTypeObject *typelist[] = { |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2391 | &cycle_type, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2392 | &dropwhile_type, |
| 2393 | &takewhile_type, |
| 2394 | &islice_type, |
| 2395 | &starmap_type, |
| 2396 | &imap_type, |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2397 | &chain_type, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2398 | &ifilter_type, |
| 2399 | &ifilterfalse_type, |
| 2400 | &count_type, |
| 2401 | &izip_type, |
| 2402 | &repeat_type, |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 2403 | &groupby_type, |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2404 | NULL |
| 2405 | }; |
| 2406 | |
Skip Montanaro | f3938fd | 2004-02-10 20:27:40 +0000 | [diff] [blame] | 2407 | teedataobject_type.ob_type = &PyType_Type; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2408 | m = Py_InitModule3("itertools", module_methods, module_doc); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2409 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2410 | for (i=0 ; typelist[i] != NULL ; i++) { |
| 2411 | if (PyType_Ready(typelist[i]) < 0) |
| 2412 | return; |
Raymond Hettinger | d449eab | 2003-05-22 16:32:58 +0000 | [diff] [blame] | 2413 | name = strchr(typelist[i]->tp_name, '.'); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 2414 | assert (name != NULL); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2415 | Py_INCREF(typelist[i]); |
Raymond Hettinger | d449eab | 2003-05-22 16:32:58 +0000 | [diff] [blame] | 2416 | PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 2417 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 2418 | |
| 2419 | if (PyType_Ready(&teedataobject_type) < 0) |
| 2420 | return; |
| 2421 | if (PyType_Ready(&tee_type) < 0) |
| 2422 | return; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 2423 | if (PyType_Ready(&_grouper_type) < 0) |
| 2424 | return; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 2425 | } |