3 #include "structmember.h"
5 /* Itertools module written and maintained
6 by Raymond D. Hettinger <python@rcn.com>
7 Copyright (c) 2003 Python Software Foundation.
12 /* groupby object ***********************************************************/
23 static PyTypeObject groupby_type
;
24 static PyObject
*_grouper_create(groupbyobject
*, PyObject
*);
27 groupby_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
29 static const char *kwargs
[] = {"iterable", "key", NULL
};
31 PyObject
*it
, *keyfunc
= Py_None
;
33 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|O:groupby", kwargs
,
37 gbo
= (groupbyobject
*)type
->tp_alloc(type
, 0);
42 gbo
->currvalue
= NULL
;
43 gbo
->keyfunc
= keyfunc
;
45 gbo
->it
= PyObject_GetIter(it
);
46 if (gbo
->it
== NULL
) {
50 return (PyObject
*)gbo
;
54 groupby_dealloc(groupbyobject
*gbo
)
56 PyObject_GC_UnTrack(gbo
);
58 Py_XDECREF(gbo
->keyfunc
);
59 Py_XDECREF(gbo
->tgtkey
);
60 Py_XDECREF(gbo
->currkey
);
61 Py_XDECREF(gbo
->currvalue
);
62 gbo
->ob_type
->tp_free(gbo
);
66 groupby_traverse(groupbyobject
*gbo
, visitproc visit
, void *arg
)
69 Py_VISIT(gbo
->keyfunc
);
70 Py_VISIT(gbo
->tgtkey
);
71 Py_VISIT(gbo
->currkey
);
72 Py_VISIT(gbo
->currvalue
);
77 groupby_next(groupbyobject
*gbo
)
79 PyObject
*newvalue
, *newkey
, *r
, *grouper
, *tmp
;
81 /* skip to next iteration group */
83 if (gbo
->currkey
== NULL
)
85 else if (gbo
->tgtkey
== NULL
)
90 rcmp
= PyObject_RichCompareBool(gbo
->tgtkey
,
98 newvalue
= PyIter_Next(gbo
->it
);
102 if (gbo
->keyfunc
== Py_None
) {
106 newkey
= PyObject_CallFunctionObjArgs(gbo
->keyfunc
,
108 if (newkey
== NULL
) {
115 gbo
->currkey
= newkey
;
118 tmp
= gbo
->currvalue
;
119 gbo
->currvalue
= newvalue
;
123 Py_INCREF(gbo
->currkey
);
125 gbo
->tgtkey
= gbo
->currkey
;
128 grouper
= _grouper_create(gbo
, gbo
->tgtkey
);
132 r
= PyTuple_Pack(2, gbo
->currkey
, grouper
);
137 PyDoc_STRVAR(groupby_doc
,
138 "groupby(iterable[, keyfunc]) -> create an iterator which returns\n\
139 (key, sub-iterator) grouped by each value of key(value).\n");
141 static PyTypeObject groupby_type
= {
142 PyObject_HEAD_INIT(NULL
)
144 "itertools.groupby", /* tp_name */
145 sizeof(groupbyobject
), /* tp_basicsize */
148 (destructor
)groupby_dealloc
, /* tp_dealloc */
154 0, /* tp_as_number */
155 0, /* tp_as_sequence */
156 0, /* tp_as_mapping */
160 PyObject_GenericGetAttr
, /* tp_getattro */
162 0, /* tp_as_buffer */
163 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
164 Py_TPFLAGS_BASETYPE
, /* tp_flags */
165 groupby_doc
, /* tp_doc */
166 (traverseproc
)groupby_traverse
, /* tp_traverse */
168 0, /* tp_richcompare */
169 0, /* tp_weaklistoffset */
170 PyObject_SelfIter
, /* tp_iter */
171 (iternextfunc
)groupby_next
, /* tp_iternext */
177 0, /* tp_descr_get */
178 0, /* tp_descr_set */
179 0, /* tp_dictoffset */
182 groupby_new
, /* tp_new */
183 PyObject_GC_Del
, /* tp_free */
187 /* _grouper object (internal) ************************************************/
195 static PyTypeObject _grouper_type
;
198 _grouper_create(groupbyobject
*parent
, PyObject
*tgtkey
)
202 igo
= PyObject_New(_grouperobject
, &_grouper_type
);
205 igo
->parent
= (PyObject
*)parent
;
207 igo
->tgtkey
= tgtkey
;
210 return (PyObject
*)igo
;
214 _grouper_dealloc(_grouperobject
*igo
)
216 Py_DECREF(igo
->parent
);
217 Py_DECREF(igo
->tgtkey
);
222 _grouper_next(_grouperobject
*igo
)
224 groupbyobject
*gbo
= (groupbyobject
*)igo
->parent
;
225 PyObject
*newvalue
, *newkey
, *r
;
228 if (gbo
->currvalue
== NULL
) {
229 newvalue
= PyIter_Next(gbo
->it
);
230 if (newvalue
== NULL
)
233 if (gbo
->keyfunc
== Py_None
) {
237 newkey
= PyObject_CallFunctionObjArgs(gbo
->keyfunc
,
239 if (newkey
== NULL
) {
245 assert(gbo
->currkey
== NULL
);
246 gbo
->currkey
= newkey
;
247 gbo
->currvalue
= newvalue
;
250 assert(gbo
->currkey
!= NULL
);
251 rcmp
= PyObject_RichCompareBool(igo
->tgtkey
, gbo
->currkey
, Py_EQ
);
253 /* got any error or current group is end */
257 gbo
->currvalue
= NULL
;
258 Py_CLEAR(gbo
->currkey
);
263 static PyTypeObject _grouper_type
= {
264 PyObject_HEAD_INIT(NULL
)
266 "itertools._grouper", /* tp_name */
267 sizeof(_grouperobject
), /* tp_basicsize */
270 (destructor
)_grouper_dealloc
, /* tp_dealloc */
276 0, /* tp_as_number */
277 0, /* tp_as_sequence */
278 0, /* tp_as_mapping */
282 PyObject_GenericGetAttr
, /* tp_getattro */
284 0, /* tp_as_buffer */
285 Py_TPFLAGS_DEFAULT
, /* tp_flags */
289 0, /* tp_richcompare */
290 0, /* tp_weaklistoffset */
291 PyObject_SelfIter
, /* tp_iter */
292 (iternextfunc
)_grouper_next
, /* tp_iternext */
298 0, /* tp_descr_get */
299 0, /* tp_descr_set */
300 0, /* tp_dictoffset */
304 PyObject_Del
, /* tp_free */
309 /* tee object and with supporting function and objects ***************/
311 /* The teedataobject pre-allocates space for LINKCELLS number of objects.
312 To help the object fit neatly inside cache lines (space for 16 to 32
313 pointers), the value should be a multiple of 16 minus space for
314 the other structure members including PyHEAD overhead. The larger the
315 value, the less memory overhead per object and the less time spent
316 allocating/deallocating new links. The smaller the number, the less
317 wasted space and the more rapid freeing of older data.
326 PyObject
*(values
[LINKCELLS
]);
331 teedataobject
*dataobj
;
333 PyObject
*weakreflist
;
336 static PyTypeObject teedataobject_type
;
339 teedataobject_new(PyObject
*it
)
343 tdo
= PyObject_New(teedataobject
, &teedataobject_type
);
348 tdo
->nextlink
= NULL
;
351 return (PyObject
*)tdo
;
355 teedataobject_jumplink(teedataobject
*tdo
)
357 if (tdo
->nextlink
== NULL
)
358 tdo
->nextlink
= teedataobject_new(tdo
->it
);
359 Py_INCREF(tdo
->nextlink
);
360 return tdo
->nextlink
;
364 teedataobject_getitem(teedataobject
*tdo
, int i
)
368 assert(i
< LINKCELLS
);
369 if (i
< tdo
->numread
)
370 value
= tdo
->values
[i
];
372 /* this is the lead iterator, so fetch more data */
373 assert(i
== tdo
->numread
);
374 value
= PyIter_Next(tdo
->it
);
378 tdo
->values
[i
] = value
;
385 teedataobject_dealloc(teedataobject
*tdo
)
389 for (i
=0 ; i
<tdo
->numread
; i
++)
390 Py_DECREF(tdo
->values
[i
]);
392 Py_XDECREF(tdo
->nextlink
);
396 PyDoc_STRVAR(teedataobject_doc
, "Data container common to multiple tee objects.");
398 static PyTypeObject teedataobject_type
= {
399 PyObject_HEAD_INIT(0) /* Must fill in type value later */
401 "itertools.tee_dataobject", /* tp_name */
402 sizeof(teedataobject
), /* tp_basicsize */
405 (destructor
)teedataobject_dealloc
, /* tp_dealloc */
411 0, /* tp_as_number */
412 0, /* tp_as_sequence */
413 0, /* tp_as_mapping */
417 PyObject_GenericGetAttr
, /* tp_getattro */
419 0, /* tp_as_buffer */
420 Py_TPFLAGS_DEFAULT
, /* tp_flags */
421 teedataobject_doc
, /* tp_doc */
426 static PyTypeObject tee_type
;
429 tee_next(teeobject
*to
)
431 PyObject
*value
, *link
;
433 if (to
->index
>= LINKCELLS
) {
434 link
= teedataobject_jumplink(to
->dataobj
);
435 Py_XDECREF(to
->dataobj
);
436 to
->dataobj
= (teedataobject
*)link
;
439 value
= teedataobject_getitem(to
->dataobj
, to
->index
);
447 tee_copy(teeobject
*to
)
451 newto
= PyObject_New(teeobject
, &tee_type
);
454 Py_INCREF(to
->dataobj
);
455 newto
->dataobj
= to
->dataobj
;
456 newto
->index
= to
->index
;
457 newto
->weakreflist
= NULL
;
458 return (PyObject
*)newto
;
461 PyDoc_STRVAR(teecopy_doc
, "Returns an independent iterator.");
464 tee_fromiterable(PyObject
*iterable
)
469 it
= PyObject_GetIter(iterable
);
472 if (PyObject_TypeCheck(it
, &tee_type
)) {
473 to
= (teeobject
*)tee_copy((teeobject
*)it
);
477 to
= PyObject_New(teeobject
, &tee_type
);
480 to
->dataobj
= (teedataobject
*)teedataobject_new(it
);
482 to
->weakreflist
= NULL
;
485 return (PyObject
*)to
;
489 tee_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kw
)
493 if (!PyArg_UnpackTuple(args
, "tee", 1, 1, &iterable
))
495 return tee_fromiterable(iterable
);
499 tee_dealloc(teeobject
*to
)
501 if (to
->weakreflist
!= NULL
)
502 PyObject_ClearWeakRefs((PyObject
*) to
);
503 Py_XDECREF(to
->dataobj
);
507 PyDoc_STRVAR(teeobject_doc
,
508 "Iterator wrapped to make it copyable");
510 static PyMethodDef tee_methods
[] = {
511 {"__copy__", (PyCFunction
)tee_copy
, METH_NOARGS
, teecopy_doc
},
512 {NULL
, NULL
} /* sentinel */
515 static PyTypeObject tee_type
= {
516 PyObject_HEAD_INIT(NULL
)
518 "itertools.tee", /* tp_name */
519 sizeof(teeobject
), /* tp_basicsize */
522 (destructor
)tee_dealloc
, /* tp_dealloc */
528 0, /* tp_as_number */
529 0, /* tp_as_sequence */
530 0, /* tp_as_mapping */
536 0, /* tp_as_buffer */
537 Py_TPFLAGS_DEFAULT
, /* tp_flags */
538 teeobject_doc
, /* tp_doc */
541 0, /* tp_richcompare */
542 offsetof(teeobject
, weakreflist
), /* tp_weaklistoffset */
543 PyObject_SelfIter
, /* tp_iter */
544 (iternextfunc
)tee_next
, /* tp_iternext */
545 tee_methods
, /* tp_methods */
550 0, /* tp_descr_get */
551 0, /* tp_descr_set */
552 0, /* tp_dictoffset */
555 tee_new
, /* tp_new */
556 PyObject_Del
, /* tp_free */
560 tee(PyObject
*self
, PyObject
*args
)
563 PyObject
*it
, *iterable
, *copyable
, *result
;
565 if (!PyArg_ParseTuple(args
, "O|i", &iterable
, &n
))
567 result
= PyTuple_New(n
);
572 it
= PyObject_GetIter(iterable
);
577 if (!PyObject_HasAttrString(it
, "__copy__")) {
578 copyable
= tee_fromiterable(it
);
580 if (copyable
== NULL
) {
586 PyTuple_SET_ITEM(result
, 0, copyable
);
587 for (i
=1 ; i
<n
; i
++) {
588 copyable
= PyObject_CallMethod(copyable
, "__copy__", NULL
);
589 if (copyable
== NULL
) {
593 PyTuple_SET_ITEM(result
, i
, copyable
);
598 PyDoc_STRVAR(tee_doc
,
599 "tee(iterable, n=2) --> tuple of n independent iterators.");
602 /* cycle object **********************************************************/
611 static PyTypeObject cycle_type
;
614 cycle_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
621 if (!_PyArg_NoKeywords("cycle()", kwds
))
624 if (!PyArg_UnpackTuple(args
, "cycle", 1, 1, &iterable
))
628 it
= PyObject_GetIter(iterable
);
632 saved
= PyList_New(0);
638 /* create cycleobject structure */
639 lz
= (cycleobject
*)type
->tp_alloc(type
, 0);
649 return (PyObject
*)lz
;
653 cycle_dealloc(cycleobject
*lz
)
655 PyObject_GC_UnTrack(lz
);
656 Py_XDECREF(lz
->saved
);
658 lz
->ob_type
->tp_free(lz
);
662 cycle_traverse(cycleobject
*lz
, visitproc visit
, void *arg
)
670 cycle_next(cycleobject
*lz
)
677 item
= PyIter_Next(lz
->it
);
680 PyList_Append(lz
->saved
, item
);
683 if (PyErr_Occurred()) {
684 if (PyErr_ExceptionMatches(PyExc_StopIteration
))
689 if (PyList_Size(lz
->saved
) == 0)
691 it
= PyObject_GetIter(lz
->saved
);
701 PyDoc_STRVAR(cycle_doc
,
702 "cycle(iterable) --> cycle object\n\
704 Return elements from the iterable until it is exhausted.\n\
705 Then repeat the sequence indefinitely.");
707 static PyTypeObject cycle_type
= {
708 PyObject_HEAD_INIT(NULL
)
710 "itertools.cycle", /* tp_name */
711 sizeof(cycleobject
), /* tp_basicsize */
714 (destructor
)cycle_dealloc
, /* tp_dealloc */
720 0, /* tp_as_number */
721 0, /* tp_as_sequence */
722 0, /* tp_as_mapping */
726 PyObject_GenericGetAttr
, /* tp_getattro */
728 0, /* tp_as_buffer */
729 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
730 Py_TPFLAGS_BASETYPE
, /* tp_flags */
731 cycle_doc
, /* tp_doc */
732 (traverseproc
)cycle_traverse
, /* tp_traverse */
734 0, /* tp_richcompare */
735 0, /* tp_weaklistoffset */
736 PyObject_SelfIter
, /* tp_iter */
737 (iternextfunc
)cycle_next
, /* tp_iternext */
743 0, /* tp_descr_get */
744 0, /* tp_descr_set */
745 0, /* tp_dictoffset */
748 cycle_new
, /* tp_new */
749 PyObject_GC_Del
, /* tp_free */
753 /* dropwhile object **********************************************************/
762 static PyTypeObject dropwhile_type
;
765 dropwhile_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
767 PyObject
*func
, *seq
;
771 if (!_PyArg_NoKeywords("dropwhile()", kwds
))
774 if (!PyArg_UnpackTuple(args
, "dropwhile", 2, 2, &func
, &seq
))
778 it
= PyObject_GetIter(seq
);
782 /* create dropwhileobject structure */
783 lz
= (dropwhileobject
*)type
->tp_alloc(type
, 0);
793 return (PyObject
*)lz
;
797 dropwhile_dealloc(dropwhileobject
*lz
)
799 PyObject_GC_UnTrack(lz
);
800 Py_XDECREF(lz
->func
);
802 lz
->ob_type
->tp_free(lz
);
806 dropwhile_traverse(dropwhileobject
*lz
, visitproc visit
, void *arg
)
814 dropwhile_next(dropwhileobject
*lz
)
816 PyObject
*item
, *good
;
817 PyObject
*it
= lz
->it
;
819 PyObject
*(*iternext
)(PyObject
*);
821 assert(PyIter_Check(it
));
822 iternext
= *it
->ob_type
->tp_iternext
;
830 good
= PyObject_CallFunctionObjArgs(lz
->func
, item
, NULL
);
835 ok
= PyObject_IsTrue(good
);
845 PyDoc_STRVAR(dropwhile_doc
,
846 "dropwhile(predicate, iterable) --> dropwhile object\n\
848 Drop items from the iterable while predicate(item) is true.\n\
849 Afterwards, return every element until the iterable is exhausted.");
851 static PyTypeObject dropwhile_type
= {
852 PyObject_HEAD_INIT(NULL
)
854 "itertools.dropwhile", /* tp_name */
855 sizeof(dropwhileobject
), /* tp_basicsize */
858 (destructor
)dropwhile_dealloc
, /* tp_dealloc */
864 0, /* tp_as_number */
865 0, /* tp_as_sequence */
866 0, /* tp_as_mapping */
870 PyObject_GenericGetAttr
, /* tp_getattro */
872 0, /* tp_as_buffer */
873 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
874 Py_TPFLAGS_BASETYPE
, /* tp_flags */
875 dropwhile_doc
, /* tp_doc */
876 (traverseproc
)dropwhile_traverse
, /* tp_traverse */
878 0, /* tp_richcompare */
879 0, /* tp_weaklistoffset */
880 PyObject_SelfIter
, /* tp_iter */
881 (iternextfunc
)dropwhile_next
, /* tp_iternext */
887 0, /* tp_descr_get */
888 0, /* tp_descr_set */
889 0, /* tp_dictoffset */
892 dropwhile_new
, /* tp_new */
893 PyObject_GC_Del
, /* tp_free */
897 /* takewhile object **********************************************************/
906 static PyTypeObject takewhile_type
;
909 takewhile_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
911 PyObject
*func
, *seq
;
915 if (!_PyArg_NoKeywords("takewhile()", kwds
))
918 if (!PyArg_UnpackTuple(args
, "takewhile", 2, 2, &func
, &seq
))
922 it
= PyObject_GetIter(seq
);
926 /* create takewhileobject structure */
927 lz
= (takewhileobject
*)type
->tp_alloc(type
, 0);
937 return (PyObject
*)lz
;
941 takewhile_dealloc(takewhileobject
*lz
)
943 PyObject_GC_UnTrack(lz
);
944 Py_XDECREF(lz
->func
);
946 lz
->ob_type
->tp_free(lz
);
950 takewhile_traverse(takewhileobject
*lz
, visitproc visit
, void *arg
)
958 takewhile_next(takewhileobject
*lz
)
960 PyObject
*item
, *good
;
961 PyObject
*it
= lz
->it
;
967 assert(PyIter_Check(it
));
968 item
= (*it
->ob_type
->tp_iternext
)(it
);
972 good
= PyObject_CallFunctionObjArgs(lz
->func
, item
, NULL
);
977 ok
= PyObject_IsTrue(good
);
986 PyDoc_STRVAR(takewhile_doc
,
987 "takewhile(predicate, iterable) --> takewhile object\n\
989 Return successive entries from an iterable as long as the \n\
990 predicate evaluates to true for each entry.");
992 static PyTypeObject takewhile_type
= {
993 PyObject_HEAD_INIT(NULL
)
995 "itertools.takewhile", /* tp_name */
996 sizeof(takewhileobject
), /* tp_basicsize */
999 (destructor
)takewhile_dealloc
, /* tp_dealloc */
1005 0, /* tp_as_number */
1006 0, /* tp_as_sequence */
1007 0, /* tp_as_mapping */
1011 PyObject_GenericGetAttr
, /* tp_getattro */
1012 0, /* tp_setattro */
1013 0, /* tp_as_buffer */
1014 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1015 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1016 takewhile_doc
, /* tp_doc */
1017 (traverseproc
)takewhile_traverse
, /* tp_traverse */
1019 0, /* tp_richcompare */
1020 0, /* tp_weaklistoffset */
1021 PyObject_SelfIter
, /* tp_iter */
1022 (iternextfunc
)takewhile_next
, /* tp_iternext */
1028 0, /* tp_descr_get */
1029 0, /* tp_descr_set */
1030 0, /* tp_dictoffset */
1033 takewhile_new
, /* tp_new */
1034 PyObject_GC_Del
, /* tp_free */
1038 /* islice object ************************************************************/
1049 static PyTypeObject islice_type
;
1052 islice_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1055 long start
=0, stop
=-1, step
=1;
1056 PyObject
*it
, *a1
=NULL
, *a2
=NULL
, *a3
=NULL
;
1060 if (!_PyArg_NoKeywords("islice()", kwds
))
1063 if (!PyArg_UnpackTuple(args
, "islice", 2, 4, &seq
, &a1
, &a2
, &a3
))
1066 numargs
= PyTuple_Size(args
);
1068 if (a1
!= Py_None
) {
1069 stop
= PyInt_AsLong(a1
);
1071 if (PyErr_Occurred())
1073 PyErr_SetString(PyExc_ValueError
,
1074 "Stop argument for islice() must be a non-negative integer or None.");
1080 start
= PyInt_AsLong(a1
);
1081 if (start
== -1 && PyErr_Occurred())
1083 if (a2
!= Py_None
) {
1084 stop
= PyInt_AsLong(a2
);
1086 if (PyErr_Occurred())
1088 PyErr_SetString(PyExc_ValueError
,
1089 "Stop argument for islice() must be a non-negative integer or None.");
1094 if (start
<0 || stop
<-1) {
1095 PyErr_SetString(PyExc_ValueError
,
1096 "Indices for islice() must be non-negative integers or None.");
1102 step
= PyInt_AsLong(a3
);
1103 if (step
== -1 && PyErr_Occurred())
1107 PyErr_SetString(PyExc_ValueError
,
1108 "Step for islice() must be a positive integer or None.");
1113 it
= PyObject_GetIter(seq
);
1117 /* create isliceobject structure */
1118 lz
= (isliceobject
*)type
->tp_alloc(type
, 0);
1129 return (PyObject
*)lz
;
1133 islice_dealloc(isliceobject
*lz
)
1135 PyObject_GC_UnTrack(lz
);
1137 lz
->ob_type
->tp_free(lz
);
1141 islice_traverse(isliceobject
*lz
, visitproc visit
, void *arg
)
1148 islice_next(isliceobject
*lz
)
1151 PyObject
*it
= lz
->it
;
1153 PyObject
*(*iternext
)(PyObject
*);
1155 assert(PyIter_Check(it
));
1156 iternext
= *it
->ob_type
->tp_iternext
;
1157 while (lz
->cnt
< lz
->next
) {
1158 item
= iternext(it
);
1164 if (lz
->stop
!= -1 && lz
->cnt
>= lz
->stop
)
1166 assert(PyIter_Check(it
));
1167 item
= iternext(it
);
1172 lz
->next
+= lz
->step
;
1173 if (lz
->next
< oldnext
) /* Check for overflow */
1174 lz
->next
= lz
->stop
;
1178 PyDoc_STRVAR(islice_doc
,
1179 "islice(iterable, [start,] stop [, step]) --> islice object\n\
1181 Return an iterator whose next() method returns selected values from an\n\
1182 iterable. If start is specified, will skip all preceding elements;\n\
1183 otherwise, start defaults to zero. Step defaults to one. If\n\
1184 specified as another value, step determines how many values are \n\
1185 skipped between successive calls. Works like a slice() on a list\n\
1186 but returns an iterator.");
1188 static PyTypeObject islice_type
= {
1189 PyObject_HEAD_INIT(NULL
)
1191 "itertools.islice", /* tp_name */
1192 sizeof(isliceobject
), /* tp_basicsize */
1193 0, /* tp_itemsize */
1195 (destructor
)islice_dealloc
, /* tp_dealloc */
1201 0, /* tp_as_number */
1202 0, /* tp_as_sequence */
1203 0, /* tp_as_mapping */
1207 PyObject_GenericGetAttr
, /* tp_getattro */
1208 0, /* tp_setattro */
1209 0, /* tp_as_buffer */
1210 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1211 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1212 islice_doc
, /* tp_doc */
1213 (traverseproc
)islice_traverse
, /* tp_traverse */
1215 0, /* tp_richcompare */
1216 0, /* tp_weaklistoffset */
1217 PyObject_SelfIter
, /* tp_iter */
1218 (iternextfunc
)islice_next
, /* tp_iternext */
1224 0, /* tp_descr_get */
1225 0, /* tp_descr_set */
1226 0, /* tp_dictoffset */
1229 islice_new
, /* tp_new */
1230 PyObject_GC_Del
, /* tp_free */
1234 /* starmap object ************************************************************/
1242 static PyTypeObject starmap_type
;
1245 starmap_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1247 PyObject
*func
, *seq
;
1251 if (!_PyArg_NoKeywords("starmap()", kwds
))
1254 if (!PyArg_UnpackTuple(args
, "starmap", 2, 2, &func
, &seq
))
1258 it
= PyObject_GetIter(seq
);
1262 /* create starmapobject structure */
1263 lz
= (starmapobject
*)type
->tp_alloc(type
, 0);
1272 return (PyObject
*)lz
;
1276 starmap_dealloc(starmapobject
*lz
)
1278 PyObject_GC_UnTrack(lz
);
1279 Py_XDECREF(lz
->func
);
1281 lz
->ob_type
->tp_free(lz
);
1285 starmap_traverse(starmapobject
*lz
, visitproc visit
, void *arg
)
1293 starmap_next(starmapobject
*lz
)
1297 PyObject
*it
= lz
->it
;
1299 assert(PyIter_Check(it
));
1300 args
= (*it
->ob_type
->tp_iternext
)(it
);
1303 if (!PyTuple_CheckExact(args
)) {
1305 PyErr_SetString(PyExc_TypeError
,
1306 "iterator must return a tuple");
1309 result
= PyObject_Call(lz
->func
, args
, NULL
);
1314 PyDoc_STRVAR(starmap_doc
,
1315 "starmap(function, sequence) --> starmap object\n\
1317 Return an iterator whose values are returned from the function evaluated\n\
1318 with a argument tuple taken from the given sequence.");
1320 static PyTypeObject starmap_type
= {
1321 PyObject_HEAD_INIT(NULL
)
1323 "itertools.starmap", /* tp_name */
1324 sizeof(starmapobject
), /* tp_basicsize */
1325 0, /* tp_itemsize */
1327 (destructor
)starmap_dealloc
, /* tp_dealloc */
1333 0, /* tp_as_number */
1334 0, /* tp_as_sequence */
1335 0, /* tp_as_mapping */
1339 PyObject_GenericGetAttr
, /* tp_getattro */
1340 0, /* tp_setattro */
1341 0, /* tp_as_buffer */
1342 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1343 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1344 starmap_doc
, /* tp_doc */
1345 (traverseproc
)starmap_traverse
, /* tp_traverse */
1347 0, /* tp_richcompare */
1348 0, /* tp_weaklistoffset */
1349 PyObject_SelfIter
, /* tp_iter */
1350 (iternextfunc
)starmap_next
, /* tp_iternext */
1356 0, /* tp_descr_get */
1357 0, /* tp_descr_set */
1358 0, /* tp_dictoffset */
1361 starmap_new
, /* tp_new */
1362 PyObject_GC_Del
, /* tp_free */
1366 /* imap object ************************************************************/
1374 static PyTypeObject imap_type
;
1377 imap_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1379 PyObject
*it
, *iters
, *func
;
1383 if (!_PyArg_NoKeywords("imap()", kwds
))
1386 numargs
= PyTuple_Size(args
);
1388 PyErr_SetString(PyExc_TypeError
,
1389 "imap() must have at least two arguments.");
1393 iters
= PyTuple_New(numargs
-1);
1397 for (i
=1 ; i
<numargs
; i
++) {
1399 it
= PyObject_GetIter(PyTuple_GET_ITEM(args
, i
));
1404 PyTuple_SET_ITEM(iters
, i
-1, it
);
1407 /* create imapobject structure */
1408 lz
= (imapobject
*)type
->tp_alloc(type
, 0);
1414 func
= PyTuple_GET_ITEM(args
, 0);
1418 return (PyObject
*)lz
;
1422 imap_dealloc(imapobject
*lz
)
1424 PyObject_GC_UnTrack(lz
);
1425 Py_XDECREF(lz
->iters
);
1426 Py_XDECREF(lz
->func
);
1427 lz
->ob_type
->tp_free(lz
);
1431 imap_traverse(imapobject
*lz
, visitproc visit
, void *arg
)
1433 Py_VISIT(lz
->iters
);
1439 imap() is an iterator version of __builtins__.map() except that it does
1440 not have the None fill-in feature. That was intentionally left out for
1441 the following reasons:
1443 1) Itertools are designed to be easily combined and chained together.
1444 Having all tools stop with the shortest input is a unifying principle
1445 that makes it easier to combine finite iterators (supplying data) with
1446 infinite iterators like count() and repeat() (for supplying sequential
1447 or constant arguments to a function).
1449 2) In typical use cases for combining itertools, having one finite data
1450 supplier run out before another is likely to be an error condition which
1451 should not pass silently by automatically supplying None.
1453 3) The use cases for automatic None fill-in are rare -- not many functions
1454 do something useful when a parameter suddenly switches type and becomes
1457 4) If a need does arise, it can be met by __builtins__.map() or by
1458 writing: chain(iterable, repeat(None)).
1460 5) Similar toolsets in Haskell and SML do not have automatic None fill-in.
1464 imap_next(imapobject
*lz
)
1471 numargs
= PyTuple_Size(lz
->iters
);
1472 argtuple
= PyTuple_New(numargs
);
1473 if (argtuple
== NULL
)
1476 for (i
=0 ; i
<numargs
; i
++) {
1477 val
= PyIter_Next(PyTuple_GET_ITEM(lz
->iters
, i
));
1479 Py_DECREF(argtuple
);
1482 PyTuple_SET_ITEM(argtuple
, i
, val
);
1484 if (lz
->func
== Py_None
)
1486 result
= PyObject_Call(lz
->func
, argtuple
, NULL
);
1487 Py_DECREF(argtuple
);
1491 PyDoc_STRVAR(imap_doc
,
1492 "imap(func, *iterables) --> imap object\n\
1494 Make an iterator that computes the function using arguments from\n\
1495 each of the iterables. Like map() except that it returns\n\
1496 an iterator instead of a list and that it stops when the shortest\n\
1497 iterable is exhausted instead of filling in None for shorter\n\
1500 static PyTypeObject imap_type
= {
1501 PyObject_HEAD_INIT(NULL
)
1503 "itertools.imap", /* tp_name */
1504 sizeof(imapobject
), /* tp_basicsize */
1505 0, /* tp_itemsize */
1507 (destructor
)imap_dealloc
, /* tp_dealloc */
1513 0, /* tp_as_number */
1514 0, /* tp_as_sequence */
1515 0, /* tp_as_mapping */
1519 PyObject_GenericGetAttr
, /* tp_getattro */
1520 0, /* tp_setattro */
1521 0, /* tp_as_buffer */
1522 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1523 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1524 imap_doc
, /* tp_doc */
1525 (traverseproc
)imap_traverse
, /* tp_traverse */
1527 0, /* tp_richcompare */
1528 0, /* tp_weaklistoffset */
1529 PyObject_SelfIter
, /* tp_iter */
1530 (iternextfunc
)imap_next
, /* tp_iternext */
1536 0, /* tp_descr_get */
1537 0, /* tp_descr_set */
1538 0, /* tp_dictoffset */
1541 imap_new
, /* tp_new */
1542 PyObject_GC_Del
, /* tp_free */
1546 /* chain object ************************************************************/
1551 long iternum
; /* which iterator is active */
1552 PyObject
*ittuple
; /* tuple of iterators */
1555 static PyTypeObject chain_type
;
1558 chain_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1561 int tuplesize
= PySequence_Length(args
);
1565 if (!_PyArg_NoKeywords("chain()", kwds
))
1568 /* obtain iterators */
1569 assert(PyTuple_Check(args
));
1570 ittuple
= PyTuple_New(tuplesize
);
1573 for (i
=0; i
< tuplesize
; ++i
) {
1574 PyObject
*item
= PyTuple_GET_ITEM(args
, i
);
1575 PyObject
*it
= PyObject_GetIter(item
);
1577 if (PyErr_ExceptionMatches(PyExc_TypeError
))
1578 PyErr_Format(PyExc_TypeError
,
1579 "chain argument #%d must support iteration",
1584 PyTuple_SET_ITEM(ittuple
, i
, it
);
1587 /* create chainobject structure */
1588 lz
= (chainobject
*)type
->tp_alloc(type
, 0);
1594 lz
->ittuple
= ittuple
;
1596 lz
->tuplesize
= tuplesize
;
1598 return (PyObject
*)lz
;
1602 chain_dealloc(chainobject
*lz
)
1604 PyObject_GC_UnTrack(lz
);
1605 Py_XDECREF(lz
->ittuple
);
1606 lz
->ob_type
->tp_free(lz
);
1610 chain_traverse(chainobject
*lz
, visitproc visit
, void *arg
)
1612 Py_VISIT(lz
->ittuple
);
1617 chain_next(chainobject
*lz
)
1622 while (lz
->iternum
< lz
->tuplesize
) {
1623 it
= PyTuple_GET_ITEM(lz
->ittuple
, lz
->iternum
);
1624 item
= PyIter_Next(it
);
1627 if (PyErr_Occurred()) {
1628 if (PyErr_ExceptionMatches(PyExc_StopIteration
))
1638 PyDoc_STRVAR(chain_doc
,
1639 "chain(*iterables) --> chain object\n\
1641 Return a chain object whose .next() method returns elements from the\n\
1642 first iterable until it is exhausted, then elements from the next\n\
1643 iterable, until all of the iterables are exhausted.");
1645 static PyTypeObject chain_type
= {
1646 PyObject_HEAD_INIT(NULL
)
1648 "itertools.chain", /* tp_name */
1649 sizeof(chainobject
), /* tp_basicsize */
1650 0, /* tp_itemsize */
1652 (destructor
)chain_dealloc
, /* tp_dealloc */
1658 0, /* tp_as_number */
1659 0, /* tp_as_sequence */
1660 0, /* tp_as_mapping */
1664 PyObject_GenericGetAttr
, /* tp_getattro */
1665 0, /* tp_setattro */
1666 0, /* tp_as_buffer */
1667 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1668 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1669 chain_doc
, /* tp_doc */
1670 (traverseproc
)chain_traverse
, /* tp_traverse */
1672 0, /* tp_richcompare */
1673 0, /* tp_weaklistoffset */
1674 PyObject_SelfIter
, /* tp_iter */
1675 (iternextfunc
)chain_next
, /* tp_iternext */
1681 0, /* tp_descr_get */
1682 0, /* tp_descr_set */
1683 0, /* tp_dictoffset */
1686 chain_new
, /* tp_new */
1687 PyObject_GC_Del
, /* tp_free */
1691 /* ifilter object ************************************************************/
1699 static PyTypeObject ifilter_type
;
1702 ifilter_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1704 PyObject
*func
, *seq
;
1708 if (!_PyArg_NoKeywords("ifilter()", kwds
))
1711 if (!PyArg_UnpackTuple(args
, "ifilter", 2, 2, &func
, &seq
))
1715 it
= PyObject_GetIter(seq
);
1719 /* create ifilterobject structure */
1720 lz
= (ifilterobject
*)type
->tp_alloc(type
, 0);
1729 return (PyObject
*)lz
;
1733 ifilter_dealloc(ifilterobject
*lz
)
1735 PyObject_GC_UnTrack(lz
);
1736 Py_XDECREF(lz
->func
);
1738 lz
->ob_type
->tp_free(lz
);
1742 ifilter_traverse(ifilterobject
*lz
, visitproc visit
, void *arg
)
1750 ifilter_next(ifilterobject
*lz
)
1753 PyObject
*it
= lz
->it
;
1755 PyObject
*(*iternext
)(PyObject
*);
1757 assert(PyIter_Check(it
));
1758 iternext
= *it
->ob_type
->tp_iternext
;
1760 item
= iternext(it
);
1764 if (lz
->func
== Py_None
) {
1765 ok
= PyObject_IsTrue(item
);
1768 good
= PyObject_CallFunctionObjArgs(lz
->func
,
1774 ok
= PyObject_IsTrue(good
);
1783 PyDoc_STRVAR(ifilter_doc
,
1784 "ifilter(function or None, sequence) --> ifilter object\n\
1786 Return those items of sequence for which function(item) is true.\n\
1787 If function is None, return the items that are true.");
1789 static PyTypeObject ifilter_type
= {
1790 PyObject_HEAD_INIT(NULL
)
1792 "itertools.ifilter", /* tp_name */
1793 sizeof(ifilterobject
), /* tp_basicsize */
1794 0, /* tp_itemsize */
1796 (destructor
)ifilter_dealloc
, /* tp_dealloc */
1802 0, /* tp_as_number */
1803 0, /* tp_as_sequence */
1804 0, /* tp_as_mapping */
1808 PyObject_GenericGetAttr
, /* tp_getattro */
1809 0, /* tp_setattro */
1810 0, /* tp_as_buffer */
1811 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1812 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1813 ifilter_doc
, /* tp_doc */
1814 (traverseproc
)ifilter_traverse
, /* tp_traverse */
1816 0, /* tp_richcompare */
1817 0, /* tp_weaklistoffset */
1818 PyObject_SelfIter
, /* tp_iter */
1819 (iternextfunc
)ifilter_next
, /* tp_iternext */
1825 0, /* tp_descr_get */
1826 0, /* tp_descr_set */
1827 0, /* tp_dictoffset */
1830 ifilter_new
, /* tp_new */
1831 PyObject_GC_Del
, /* tp_free */
1835 /* ifilterfalse object ************************************************************/
1841 } ifilterfalseobject
;
1843 static PyTypeObject ifilterfalse_type
;
1846 ifilterfalse_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1848 PyObject
*func
, *seq
;
1850 ifilterfalseobject
*lz
;
1852 if (!_PyArg_NoKeywords("ifilterfalse()", kwds
))
1855 if (!PyArg_UnpackTuple(args
, "ifilterfalse", 2, 2, &func
, &seq
))
1859 it
= PyObject_GetIter(seq
);
1863 /* create ifilterfalseobject structure */
1864 lz
= (ifilterfalseobject
*)type
->tp_alloc(type
, 0);
1873 return (PyObject
*)lz
;
1877 ifilterfalse_dealloc(ifilterfalseobject
*lz
)
1879 PyObject_GC_UnTrack(lz
);
1880 Py_XDECREF(lz
->func
);
1882 lz
->ob_type
->tp_free(lz
);
1886 ifilterfalse_traverse(ifilterfalseobject
*lz
, visitproc visit
, void *arg
)
1894 ifilterfalse_next(ifilterfalseobject
*lz
)
1897 PyObject
*it
= lz
->it
;
1899 PyObject
*(*iternext
)(PyObject
*);
1901 assert(PyIter_Check(it
));
1902 iternext
= *it
->ob_type
->tp_iternext
;
1904 item
= iternext(it
);
1908 if (lz
->func
== Py_None
) {
1909 ok
= PyObject_IsTrue(item
);
1912 good
= PyObject_CallFunctionObjArgs(lz
->func
,
1918 ok
= PyObject_IsTrue(good
);
1927 PyDoc_STRVAR(ifilterfalse_doc
,
1928 "ifilterfalse(function or None, sequence) --> ifilterfalse object\n\
1930 Return those items of sequence for which function(item) is false.\n\
1931 If function is None, return the items that are false.");
1933 static PyTypeObject ifilterfalse_type
= {
1934 PyObject_HEAD_INIT(NULL
)
1936 "itertools.ifilterfalse", /* tp_name */
1937 sizeof(ifilterfalseobject
), /* tp_basicsize */
1938 0, /* tp_itemsize */
1940 (destructor
)ifilterfalse_dealloc
, /* tp_dealloc */
1946 0, /* tp_as_number */
1947 0, /* tp_as_sequence */
1948 0, /* tp_as_mapping */
1952 PyObject_GenericGetAttr
, /* tp_getattro */
1953 0, /* tp_setattro */
1954 0, /* tp_as_buffer */
1955 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
1956 Py_TPFLAGS_BASETYPE
, /* tp_flags */
1957 ifilterfalse_doc
, /* tp_doc */
1958 (traverseproc
)ifilterfalse_traverse
, /* tp_traverse */
1960 0, /* tp_richcompare */
1961 0, /* tp_weaklistoffset */
1962 PyObject_SelfIter
, /* tp_iter */
1963 (iternextfunc
)ifilterfalse_next
, /* tp_iternext */
1969 0, /* tp_descr_get */
1970 0, /* tp_descr_set */
1971 0, /* tp_dictoffset */
1974 ifilterfalse_new
, /* tp_new */
1975 PyObject_GC_Del
, /* tp_free */
1979 /* count object ************************************************************/
1986 static PyTypeObject count_type
;
1989 count_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1994 if (!_PyArg_NoKeywords("count()", kwds
))
1997 if (!PyArg_ParseTuple(args
, "|l:count", &cnt
))
2000 /* create countobject structure */
2001 lz
= (countobject
*)PyObject_New(countobject
, &count_type
);
2006 return (PyObject
*)lz
;
2010 count_next(countobject
*lz
)
2012 return PyInt_FromLong(lz
->cnt
++);
2016 count_repr(countobject
*lz
)
2018 return PyString_FromFormat("count(%ld)", lz
->cnt
);
2021 PyDoc_STRVAR(count_doc
,
2022 "count([firstval]) --> count object\n\
2024 Return a count object whose .next() method returns consecutive\n\
2025 integers starting from zero or, if specified, from firstval.");
2027 static PyTypeObject count_type
= {
2028 PyObject_HEAD_INIT(NULL
)
2030 "itertools.count", /* tp_name */
2031 sizeof(countobject
), /* tp_basicsize */
2032 0, /* tp_itemsize */
2034 (destructor
)PyObject_Del
, /* tp_dealloc */
2039 (reprfunc
)count_repr
, /* tp_repr */
2040 0, /* tp_as_number */
2041 0, /* tp_as_sequence */
2042 0, /* tp_as_mapping */
2046 PyObject_GenericGetAttr
, /* tp_getattro */
2047 0, /* tp_setattro */
2048 0, /* tp_as_buffer */
2049 Py_TPFLAGS_DEFAULT
, /* tp_flags */
2050 count_doc
, /* tp_doc */
2051 0, /* tp_traverse */
2053 0, /* tp_richcompare */
2054 0, /* tp_weaklistoffset */
2055 PyObject_SelfIter
, /* tp_iter */
2056 (iternextfunc
)count_next
, /* tp_iternext */
2062 0, /* tp_descr_get */
2063 0, /* tp_descr_set */
2064 0, /* tp_dictoffset */
2067 count_new
, /* tp_new */
2071 /* izip object ************************************************************/
2078 PyObject
*ittuple
; /* tuple of iterators */
2082 static PyTypeObject izip_type
;
2085 izip_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2089 PyObject
*ittuple
; /* tuple of iterators */
2091 int tuplesize
= PySequence_Length(args
);
2093 if (!_PyArg_NoKeywords("izip()", kwds
))
2096 /* args must be a tuple */
2097 assert(PyTuple_Check(args
));
2099 /* obtain iterators */
2100 ittuple
= PyTuple_New(tuplesize
);
2103 for (i
=0; i
< tuplesize
; ++i
) {
2104 PyObject
*item
= PyTuple_GET_ITEM(args
, i
);
2105 PyObject
*it
= PyObject_GetIter(item
);
2107 if (PyErr_ExceptionMatches(PyExc_TypeError
))
2108 PyErr_Format(PyExc_TypeError
,
2109 "izip argument #%d must support iteration",
2114 PyTuple_SET_ITEM(ittuple
, i
, it
);
2117 /* create a result holder */
2118 result
= PyTuple_New(tuplesize
);
2119 if (result
== NULL
) {
2123 for (i
=0 ; i
< tuplesize
; i
++) {
2125 PyTuple_SET_ITEM(result
, i
, Py_None
);
2128 /* create izipobject structure */
2129 lz
= (izipobject
*)type
->tp_alloc(type
, 0);
2135 lz
->ittuple
= ittuple
;
2136 lz
->tuplesize
= tuplesize
;
2137 lz
->result
= result
;
2139 return (PyObject
*)lz
;
2143 izip_dealloc(izipobject
*lz
)
2145 PyObject_GC_UnTrack(lz
);
2146 Py_XDECREF(lz
->ittuple
);
2147 Py_XDECREF(lz
->result
);
2148 lz
->ob_type
->tp_free(lz
);
2152 izip_traverse(izipobject
*lz
, visitproc visit
, void *arg
)
2154 Py_VISIT(lz
->ittuple
);
2155 Py_VISIT(lz
->result
);
2160 izip_next(izipobject
*lz
)
2163 long tuplesize
= lz
->tuplesize
;
2164 PyObject
*result
= lz
->result
;
2171 if (result
->ob_refcnt
== 1) {
2173 for (i
=0 ; i
< tuplesize
; i
++) {
2174 it
= PyTuple_GET_ITEM(lz
->ittuple
, i
);
2175 assert(PyIter_Check(it
));
2176 item
= (*it
->ob_type
->tp_iternext
)(it
);
2181 olditem
= PyTuple_GET_ITEM(result
, i
);
2182 PyTuple_SET_ITEM(result
, i
, item
);
2186 result
= PyTuple_New(tuplesize
);
2189 for (i
=0 ; i
< tuplesize
; i
++) {
2190 it
= PyTuple_GET_ITEM(lz
->ittuple
, i
);
2191 assert(PyIter_Check(it
));
2192 item
= (*it
->ob_type
->tp_iternext
)(it
);
2197 PyTuple_SET_ITEM(result
, i
, item
);
2203 PyDoc_STRVAR(izip_doc
,
2204 "izip(iter1 [,iter2 [...]]) --> izip object\n\
2206 Return a izip object whose .next() method returns a tuple where\n\
2207 the i-th element comes from the i-th iterable argument. The .next()\n\
2208 method continues until the shortest iterable in the argument sequence\n\
2209 is exhausted and then it raises StopIteration. Works like the zip()\n\
2210 function but consumes less memory by returning an iterator instead of\n\
2213 static PyTypeObject izip_type
= {
2214 PyObject_HEAD_INIT(NULL
)
2216 "itertools.izip", /* tp_name */
2217 sizeof(izipobject
), /* tp_basicsize */
2218 0, /* tp_itemsize */
2220 (destructor
)izip_dealloc
, /* tp_dealloc */
2226 0, /* tp_as_number */
2227 0, /* tp_as_sequence */
2228 0, /* tp_as_mapping */
2232 PyObject_GenericGetAttr
, /* tp_getattro */
2233 0, /* tp_setattro */
2234 0, /* tp_as_buffer */
2235 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2236 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2237 izip_doc
, /* tp_doc */
2238 (traverseproc
)izip_traverse
, /* tp_traverse */
2240 0, /* tp_richcompare */
2241 0, /* tp_weaklistoffset */
2242 PyObject_SelfIter
, /* tp_iter */
2243 (iternextfunc
)izip_next
, /* tp_iternext */
2249 0, /* tp_descr_get */
2250 0, /* tp_descr_set */
2251 0, /* tp_dictoffset */
2254 izip_new
, /* tp_new */
2255 PyObject_GC_Del
, /* tp_free */
2259 /* repeat object ************************************************************/
2267 static PyTypeObject repeat_type
;
2270 repeat_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2276 if (!_PyArg_NoKeywords("repeat()", kwds
))
2279 if (!PyArg_ParseTuple(args
, "O|l:repeat", &element
, &cnt
))
2282 if (PyTuple_Size(args
) == 2 && cnt
< 0)
2285 ro
= (repeatobject
*)type
->tp_alloc(type
, 0);
2289 ro
->element
= element
;
2291 return (PyObject
*)ro
;
2295 repeat_dealloc(repeatobject
*ro
)
2297 PyObject_GC_UnTrack(ro
);
2298 Py_XDECREF(ro
->element
);
2299 ro
->ob_type
->tp_free(ro
);
2303 repeat_traverse(repeatobject
*ro
, visitproc visit
, void *arg
)
2305 Py_VISIT(ro
->element
);
2310 repeat_next(repeatobject
*ro
)
2316 Py_INCREF(ro
->element
);
2321 repeat_repr(repeatobject
*ro
)
2323 PyObject
*result
, *objrepr
;
2325 objrepr
= PyObject_Repr(ro
->element
);
2326 if (objrepr
== NULL
)
2330 result
= PyString_FromFormat("repeat(%s)",
2331 PyString_AS_STRING(objrepr
));
2333 result
= PyString_FromFormat("repeat(%s, %ld)",
2334 PyString_AS_STRING(objrepr
), ro
->cnt
);
2340 repeat_len(repeatobject
*ro
)
2342 if (ro
->cnt
== -1) {
2343 PyErr_SetString(PyExc_TypeError
, "len() of unsized object");
2346 return PyInt_FromLong(ro
->cnt
);
2349 PyDoc_STRVAR(length_cue_doc
, "Private method returning an estimate of len(list(it)).");
2351 static PyMethodDef repeat_methods
[] = {
2352 {"_length_cue", (PyCFunction
)repeat_len
, METH_NOARGS
, length_cue_doc
},
2353 {NULL
, NULL
} /* sentinel */
2356 PyDoc_STRVAR(repeat_doc
,
2357 "repeat(element [,times]) -> create an iterator which returns the element\n\
2358 for the specified number of times. If not specified, returns the element\n\
2361 static PyTypeObject repeat_type
= {
2362 PyObject_HEAD_INIT(NULL
)
2364 "itertools.repeat", /* tp_name */
2365 sizeof(repeatobject
), /* tp_basicsize */
2366 0, /* tp_itemsize */
2368 (destructor
)repeat_dealloc
, /* tp_dealloc */
2373 (reprfunc
)repeat_repr
, /* tp_repr */
2374 0, /* tp_as_number */
2375 0, /* tp_as_sequence */
2376 0, /* tp_as_mapping */
2380 PyObject_GenericGetAttr
, /* tp_getattro */
2381 0, /* tp_setattro */
2382 0, /* tp_as_buffer */
2383 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
2384 Py_TPFLAGS_BASETYPE
, /* tp_flags */
2385 repeat_doc
, /* tp_doc */
2386 (traverseproc
)repeat_traverse
, /* tp_traverse */
2388 0, /* tp_richcompare */
2389 0, /* tp_weaklistoffset */
2390 PyObject_SelfIter
, /* tp_iter */
2391 (iternextfunc
)repeat_next
, /* tp_iternext */
2392 repeat_methods
, /* tp_methods */
2397 0, /* tp_descr_get */
2398 0, /* tp_descr_set */
2399 0, /* tp_dictoffset */
2402 repeat_new
, /* tp_new */
2403 PyObject_GC_Del
, /* tp_free */
2407 /* module level code ********************************************************/
2409 PyDoc_STRVAR(module_doc
,
2410 "Functional tools for creating and using iterators.\n\
2412 Infinite iterators:\n\
2413 count([n]) --> n, n+1, n+2, ...\n\
2414 cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\
2415 repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times\n\
2417 Iterators terminating on the shortest input sequence:\n\
2418 izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\
2419 ifilter(pred, seq) --> elements of seq where pred(elem) is True\n\
2420 ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\
2421 islice(seq, [start,] stop [, step]) --> elements from\n\
2422 seq[start:stop:step]\n\
2423 imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...\n\
2424 starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\
2425 tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\
2426 chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\
2427 takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\
2428 dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\
2429 groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\
2433 static PyMethodDef module_methods
[] = {
2434 {"tee", (PyCFunction
)tee
, METH_VARARGS
, tee_doc
},
2435 {NULL
, NULL
} /* sentinel */
2444 PyTypeObject
*typelist
[] = {
2461 teedataobject_type
.ob_type
= &PyType_Type
;
2462 m
= Py_InitModule3("itertools", module_methods
, module_doc
);
2466 for (i
=0 ; typelist
[i
] != NULL
; i
++) {
2467 if (PyType_Ready(typelist
[i
]) < 0)
2469 name
= strchr(typelist
[i
]->tp_name
, '.');
2470 assert (name
!= NULL
);
2471 Py_INCREF(typelist
[i
]);
2472 PyModule_AddObject(m
, name
+1, (PyObject
*)typelist
[i
]);
2475 if (PyType_Ready(&teedataobject_type
) < 0)
2477 if (PyType_Ready(&tee_type
) < 0)
2479 if (PyType_Ready(&_grouper_type
) < 0)