Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | |
| 2 | #include "Python.h" |
Fred Drake | 08ebfec | 2004-10-17 19:36:57 +0000 | [diff] [blame] | 3 | #include "structmember.h" |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5 | /* Itertools module written and maintained |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 6 | by Raymond D. Hettinger <python@rcn.com> |
| 7 | Copyright (c) 2003 Python Software Foundation. |
| 8 | All rights reserved. |
| 9 | */ |
| 10 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 11 | |
| 12 | /* groupby object ***********************************************************/ |
| 13 | |
| 14 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | PyObject_HEAD |
| 16 | PyObject *it; |
| 17 | PyObject *keyfunc; |
| 18 | PyObject *tgtkey; |
| 19 | PyObject *currkey; |
| 20 | PyObject *currvalue; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 21 | } groupbyobject; |
| 22 | |
| 23 | static PyTypeObject groupby_type; |
| 24 | static PyObject *_grouper_create(groupbyobject *, PyObject *); |
| 25 | |
| 26 | static PyObject * |
| 27 | groupby_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 28 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | static char *kwargs[] = {"iterable", "key", NULL}; |
| 30 | groupbyobject *gbo; |
| 31 | PyObject *it, *keyfunc = Py_None; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 32 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:groupby", kwargs, |
| 34 | &it, &keyfunc)) |
| 35 | return NULL; |
| 36 | |
| 37 | gbo = (groupbyobject *)type->tp_alloc(type, 0); |
| 38 | if (gbo == NULL) |
| 39 | return NULL; |
| 40 | gbo->tgtkey = NULL; |
| 41 | gbo->currkey = NULL; |
| 42 | gbo->currvalue = NULL; |
| 43 | gbo->keyfunc = keyfunc; |
| 44 | Py_INCREF(keyfunc); |
| 45 | gbo->it = PyObject_GetIter(it); |
| 46 | if (gbo->it == NULL) { |
| 47 | Py_DECREF(gbo); |
| 48 | return NULL; |
| 49 | } |
| 50 | return (PyObject *)gbo; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void |
| 54 | groupby_dealloc(groupbyobject *gbo) |
| 55 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | PyObject_GC_UnTrack(gbo); |
| 57 | Py_XDECREF(gbo->it); |
| 58 | Py_XDECREF(gbo->keyfunc); |
| 59 | Py_XDECREF(gbo->tgtkey); |
| 60 | Py_XDECREF(gbo->currkey); |
| 61 | Py_XDECREF(gbo->currvalue); |
| 62 | Py_TYPE(gbo)->tp_free(gbo); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int |
| 66 | groupby_traverse(groupbyobject *gbo, visitproc visit, void *arg) |
| 67 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | Py_VISIT(gbo->it); |
| 69 | Py_VISIT(gbo->keyfunc); |
| 70 | Py_VISIT(gbo->tgtkey); |
| 71 | Py_VISIT(gbo->currkey); |
| 72 | Py_VISIT(gbo->currvalue); |
| 73 | return 0; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static PyObject * |
| 77 | groupby_next(groupbyobject *gbo) |
| 78 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | PyObject *newvalue, *newkey, *r, *grouper, *tmp; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | /* skip to next iteration group */ |
| 82 | for (;;) { |
| 83 | if (gbo->currkey == NULL) |
| 84 | /* pass */; |
| 85 | else if (gbo->tgtkey == NULL) |
| 86 | break; |
| 87 | else { |
| 88 | int rcmp; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 89 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | rcmp = PyObject_RichCompareBool(gbo->tgtkey, |
| 91 | gbo->currkey, Py_EQ); |
| 92 | if (rcmp == -1) |
| 93 | return NULL; |
| 94 | else if (rcmp == 0) |
| 95 | break; |
| 96 | } |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | newvalue = PyIter_Next(gbo->it); |
| 99 | if (newvalue == NULL) |
| 100 | return NULL; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | if (gbo->keyfunc == Py_None) { |
| 103 | newkey = newvalue; |
| 104 | Py_INCREF(newvalue); |
| 105 | } else { |
| 106 | newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
| 107 | newvalue, NULL); |
| 108 | if (newkey == NULL) { |
| 109 | Py_DECREF(newvalue); |
| 110 | return NULL; |
| 111 | } |
| 112 | } |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | tmp = gbo->currkey; |
| 115 | gbo->currkey = newkey; |
| 116 | Py_XDECREF(tmp); |
Raymond Hettinger | 4cda01e | 2004-09-28 04:45:28 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | tmp = gbo->currvalue; |
| 119 | gbo->currvalue = newvalue; |
| 120 | Py_XDECREF(tmp); |
| 121 | } |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | Py_INCREF(gbo->currkey); |
| 124 | tmp = gbo->tgtkey; |
| 125 | gbo->tgtkey = gbo->currkey; |
| 126 | Py_XDECREF(tmp); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | grouper = _grouper_create(gbo, gbo->tgtkey); |
| 129 | if (grouper == NULL) |
| 130 | return NULL; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | r = PyTuple_Pack(2, gbo->currkey, grouper); |
| 133 | Py_DECREF(grouper); |
| 134 | return r; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 137 | static PyObject * |
| 138 | groupby_reduce(groupbyobject *lz) |
| 139 | { |
| 140 | /* reduce as a 'new' call with an optional 'setstate' if groupby |
| 141 | * has started |
| 142 | */ |
| 143 | PyObject *value; |
| 144 | if (lz->tgtkey && lz->currkey && lz->currvalue) |
| 145 | value = Py_BuildValue("O(OO)(OOO)", Py_TYPE(lz), |
| 146 | lz->it, lz->keyfunc, lz->currkey, lz->currvalue, lz->tgtkey); |
| 147 | else |
| 148 | value = Py_BuildValue("O(OO)", Py_TYPE(lz), |
| 149 | lz->it, lz->keyfunc); |
| 150 | |
| 151 | return value; |
| 152 | } |
| 153 | |
| 154 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 155 | |
| 156 | static PyObject * |
| 157 | groupby_setstate(groupbyobject *lz, PyObject *state) |
| 158 | { |
| 159 | PyObject *currkey, *currvalue, *tgtkey; |
| 160 | if (!PyArg_ParseTuple(state, "OOO", &currkey, &currvalue, &tgtkey)) |
| 161 | return NULL; |
| 162 | Py_CLEAR(lz->currkey); |
| 163 | lz->currkey = currkey; |
| 164 | Py_INCREF(lz->currkey); |
| 165 | Py_CLEAR(lz->currvalue); |
| 166 | lz->currvalue = currvalue; |
| 167 | Py_INCREF(lz->currvalue); |
| 168 | Py_CLEAR(lz->tgtkey); |
| 169 | lz->tgtkey = tgtkey; |
| 170 | Py_INCREF(lz->tgtkey); |
| 171 | Py_RETURN_NONE; |
| 172 | } |
| 173 | |
| 174 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 175 | |
| 176 | static PyMethodDef groupby_methods[] = { |
| 177 | {"__reduce__", (PyCFunction)groupby_reduce, METH_NOARGS, |
| 178 | reduce_doc}, |
| 179 | {"__setstate__", (PyCFunction)groupby_setstate, METH_O, |
| 180 | setstate_doc}, |
| 181 | {NULL, NULL} /* sentinel */ |
| 182 | }; |
| 183 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 184 | PyDoc_STRVAR(groupby_doc, |
| 185 | "groupby(iterable[, keyfunc]) -> create an iterator which returns\n\ |
| 186 | (key, sub-iterator) grouped by each value of key(value).\n"); |
| 187 | |
| 188 | static PyTypeObject groupby_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | PyVarObject_HEAD_INIT(NULL, 0) |
| 190 | "itertools.groupby", /* tp_name */ |
| 191 | sizeof(groupbyobject), /* tp_basicsize */ |
| 192 | 0, /* tp_itemsize */ |
| 193 | /* methods */ |
| 194 | (destructor)groupby_dealloc, /* tp_dealloc */ |
| 195 | 0, /* tp_print */ |
| 196 | 0, /* tp_getattr */ |
| 197 | 0, /* tp_setattr */ |
| 198 | 0, /* tp_reserved */ |
| 199 | 0, /* tp_repr */ |
| 200 | 0, /* tp_as_number */ |
| 201 | 0, /* tp_as_sequence */ |
| 202 | 0, /* tp_as_mapping */ |
| 203 | 0, /* tp_hash */ |
| 204 | 0, /* tp_call */ |
| 205 | 0, /* tp_str */ |
| 206 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 207 | 0, /* tp_setattro */ |
| 208 | 0, /* tp_as_buffer */ |
| 209 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 210 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 211 | groupby_doc, /* tp_doc */ |
| 212 | (traverseproc)groupby_traverse, /* tp_traverse */ |
| 213 | 0, /* tp_clear */ |
| 214 | 0, /* tp_richcompare */ |
| 215 | 0, /* tp_weaklistoffset */ |
| 216 | PyObject_SelfIter, /* tp_iter */ |
| 217 | (iternextfunc)groupby_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 218 | groupby_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | 0, /* tp_members */ |
| 220 | 0, /* tp_getset */ |
| 221 | 0, /* tp_base */ |
| 222 | 0, /* tp_dict */ |
| 223 | 0, /* tp_descr_get */ |
| 224 | 0, /* tp_descr_set */ |
| 225 | 0, /* tp_dictoffset */ |
| 226 | 0, /* tp_init */ |
| 227 | 0, /* tp_alloc */ |
| 228 | groupby_new, /* tp_new */ |
| 229 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | |
| 233 | /* _grouper object (internal) ************************************************/ |
| 234 | |
| 235 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | PyObject_HEAD |
| 237 | PyObject *parent; |
| 238 | PyObject *tgtkey; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 239 | } _grouperobject; |
| 240 | |
| 241 | static PyTypeObject _grouper_type; |
| 242 | |
| 243 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 244 | _grouper_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 245 | { |
| 246 | PyObject *parent, *tgtkey; |
| 247 | |
| 248 | if (!PyArg_ParseTuple(args, "O!O", &groupby_type, &parent, &tgtkey)) |
| 249 | return NULL; |
| 250 | |
| 251 | return _grouper_create((groupbyobject*) parent, tgtkey); |
| 252 | } |
| 253 | |
| 254 | static PyObject * |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 255 | _grouper_create(groupbyobject *parent, PyObject *tgtkey) |
| 256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | _grouperobject *igo; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | igo = PyObject_GC_New(_grouperobject, &_grouper_type); |
| 260 | if (igo == NULL) |
| 261 | return NULL; |
| 262 | igo->parent = (PyObject *)parent; |
| 263 | Py_INCREF(parent); |
| 264 | igo->tgtkey = tgtkey; |
| 265 | Py_INCREF(tgtkey); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | PyObject_GC_Track(igo); |
| 268 | return (PyObject *)igo; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static void |
| 272 | _grouper_dealloc(_grouperobject *igo) |
| 273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | PyObject_GC_UnTrack(igo); |
| 275 | Py_DECREF(igo->parent); |
| 276 | Py_DECREF(igo->tgtkey); |
| 277 | PyObject_GC_Del(igo); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static int |
| 281 | _grouper_traverse(_grouperobject *igo, visitproc visit, void *arg) |
| 282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | Py_VISIT(igo->parent); |
| 284 | Py_VISIT(igo->tgtkey); |
| 285 | return 0; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static PyObject * |
| 289 | _grouper_next(_grouperobject *igo) |
| 290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | groupbyobject *gbo = (groupbyobject *)igo->parent; |
| 292 | PyObject *newvalue, *newkey, *r; |
| 293 | int rcmp; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | if (gbo->currvalue == NULL) { |
| 296 | newvalue = PyIter_Next(gbo->it); |
| 297 | if (newvalue == NULL) |
| 298 | return NULL; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | if (gbo->keyfunc == Py_None) { |
| 301 | newkey = newvalue; |
| 302 | Py_INCREF(newvalue); |
| 303 | } else { |
| 304 | newkey = PyObject_CallFunctionObjArgs(gbo->keyfunc, |
| 305 | newvalue, NULL); |
| 306 | if (newkey == NULL) { |
| 307 | Py_DECREF(newvalue); |
| 308 | return NULL; |
| 309 | } |
| 310 | } |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | assert(gbo->currkey == NULL); |
| 313 | gbo->currkey = newkey; |
| 314 | gbo->currvalue = newvalue; |
| 315 | } |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | assert(gbo->currkey != NULL); |
| 318 | rcmp = PyObject_RichCompareBool(igo->tgtkey, gbo->currkey, Py_EQ); |
| 319 | if (rcmp <= 0) |
| 320 | /* got any error or current group is end */ |
| 321 | return NULL; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | r = gbo->currvalue; |
| 324 | gbo->currvalue = NULL; |
| 325 | Py_CLEAR(gbo->currkey); |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | return r; |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 330 | static PyObject * |
| 331 | _grouper_reduce(_grouperobject *lz) |
| 332 | { |
| 333 | return Py_BuildValue("O(OO)", Py_TYPE(lz), |
| 334 | lz->parent, lz->tgtkey); |
| 335 | } |
| 336 | |
| 337 | static PyMethodDef _grouper_methods[] = { |
| 338 | {"__reduce__", (PyCFunction)_grouper_reduce, METH_NOARGS, |
| 339 | reduce_doc}, |
| 340 | {NULL, NULL} /* sentinel */ |
| 341 | }; |
| 342 | |
| 343 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 344 | static PyTypeObject _grouper_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | PyVarObject_HEAD_INIT(NULL, 0) |
| 346 | "itertools._grouper", /* tp_name */ |
| 347 | sizeof(_grouperobject), /* tp_basicsize */ |
| 348 | 0, /* tp_itemsize */ |
| 349 | /* methods */ |
| 350 | (destructor)_grouper_dealloc, /* tp_dealloc */ |
| 351 | 0, /* tp_print */ |
| 352 | 0, /* tp_getattr */ |
| 353 | 0, /* tp_setattr */ |
| 354 | 0, /* tp_reserved */ |
| 355 | 0, /* tp_repr */ |
| 356 | 0, /* tp_as_number */ |
| 357 | 0, /* tp_as_sequence */ |
| 358 | 0, /* tp_as_mapping */ |
| 359 | 0, /* tp_hash */ |
| 360 | 0, /* tp_call */ |
| 361 | 0, /* tp_str */ |
| 362 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 363 | 0, /* tp_setattro */ |
| 364 | 0, /* tp_as_buffer */ |
| 365 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 366 | 0, /* tp_doc */ |
| 367 | (traverseproc)_grouper_traverse,/* tp_traverse */ |
| 368 | 0, /* tp_clear */ |
| 369 | 0, /* tp_richcompare */ |
| 370 | 0, /* tp_weaklistoffset */ |
| 371 | PyObject_SelfIter, /* tp_iter */ |
| 372 | (iternextfunc)_grouper_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 373 | _grouper_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | 0, /* tp_members */ |
| 375 | 0, /* tp_getset */ |
| 376 | 0, /* tp_base */ |
| 377 | 0, /* tp_dict */ |
| 378 | 0, /* tp_descr_get */ |
| 379 | 0, /* tp_descr_set */ |
| 380 | 0, /* tp_dictoffset */ |
| 381 | 0, /* tp_init */ |
| 382 | 0, /* tp_alloc */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 383 | _grouper_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | |
Raymond Hettinger | d25c1c6 | 2003-12-06 16:23:06 +0000 | [diff] [blame] | 388 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 389 | /* tee object and with supporting function and objects ***************/ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 390 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 391 | /* The teedataobject pre-allocates space for LINKCELLS number of objects. |
| 392 | To help the object fit neatly inside cache lines (space for 16 to 32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 393 | pointers), the value should be a multiple of 16 minus space for |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 394 | the other structure members including PyHEAD overhead. The larger the |
| 395 | value, the less memory overhead per object and the less time spent |
| 396 | allocating/deallocating new links. The smaller the number, the less |
| 397 | wasted space and the more rapid freeing of older data. |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 398 | */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 399 | #define LINKCELLS 57 |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 400 | |
| 401 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | PyObject_HEAD |
| 403 | PyObject *it; |
| 404 | int numread; |
| 405 | PyObject *nextlink; |
| 406 | PyObject *(values[LINKCELLS]); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 407 | } teedataobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 408 | |
| 409 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | PyObject_HEAD |
| 411 | teedataobject *dataobj; |
| 412 | int index; |
| 413 | PyObject *weakreflist; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 414 | } teeobject; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 415 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 416 | static PyTypeObject teedataobject_type; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 417 | |
| 418 | static PyObject * |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 419 | teedataobject_newinternal(PyObject *it) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | teedataobject *tdo; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | tdo = PyObject_GC_New(teedataobject, &teedataobject_type); |
| 424 | if (tdo == NULL) |
| 425 | return NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 427 | tdo->numread = 0; |
| 428 | tdo->nextlink = NULL; |
| 429 | Py_INCREF(it); |
| 430 | tdo->it = it; |
| 431 | PyObject_GC_Track(tdo); |
| 432 | return (PyObject *)tdo; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static PyObject * |
| 436 | teedataobject_jumplink(teedataobject *tdo) |
| 437 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | if (tdo->nextlink == NULL) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 439 | tdo->nextlink = teedataobject_newinternal(tdo->it); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | Py_XINCREF(tdo->nextlink); |
| 441 | return tdo->nextlink; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static PyObject * |
| 445 | teedataobject_getitem(teedataobject *tdo, int i) |
| 446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | PyObject *value; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | assert(i < LINKCELLS); |
| 450 | if (i < tdo->numread) |
| 451 | value = tdo->values[i]; |
| 452 | else { |
| 453 | /* this is the lead iterator, so fetch more data */ |
| 454 | assert(i == tdo->numread); |
| 455 | value = PyIter_Next(tdo->it); |
| 456 | if (value == NULL) |
| 457 | return NULL; |
| 458 | tdo->numread++; |
| 459 | tdo->values[i] = value; |
| 460 | } |
| 461 | Py_INCREF(value); |
| 462 | return value; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 465 | static int |
| 466 | teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg) |
| 467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | int i; |
| 469 | Py_VISIT(tdo->it); |
| 470 | for (i = 0; i < tdo->numread; i++) |
| 471 | Py_VISIT(tdo->values[i]); |
| 472 | Py_VISIT(tdo->nextlink); |
| 473 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static int |
| 477 | teedataobject_clear(teedataobject *tdo) |
| 478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | int i; |
| 480 | Py_CLEAR(tdo->it); |
| 481 | for (i=0 ; i<tdo->numread ; i++) |
| 482 | Py_CLEAR(tdo->values[i]); |
| 483 | Py_CLEAR(tdo->nextlink); |
| 484 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 487 | static void |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 488 | teedataobject_dealloc(teedataobject *tdo) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | PyObject_GC_UnTrack(tdo); |
| 491 | teedataobject_clear(tdo); |
| 492 | PyObject_GC_Del(tdo); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 495 | static PyObject * |
| 496 | teedataobject_reduce(teedataobject *tdo) |
| 497 | { |
| 498 | int i; |
| 499 | /* create a temporary list of already iterated values */ |
| 500 | PyObject *values = PyList_New(tdo->numread); |
| 501 | if (!values) |
| 502 | return NULL; |
| 503 | for (i=0 ; i<tdo->numread ; i++) { |
| 504 | Py_INCREF(tdo->values[i]); |
| 505 | PyList_SET_ITEM(values, i, tdo->values[i]); |
| 506 | } |
| 507 | return Py_BuildValue("O(ONO)", Py_TYPE(tdo), tdo->it, |
| 508 | values, |
| 509 | tdo->nextlink ? tdo->nextlink : Py_None); |
| 510 | } |
| 511 | |
| 512 | static PyTypeObject teedataobject_type; |
| 513 | |
| 514 | static PyObject * |
| 515 | teedataobject_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 516 | { |
| 517 | teedataobject *tdo; |
| 518 | PyObject *it, *values, *next; |
| 519 | Py_ssize_t i, len; |
| 520 | |
| 521 | assert(type == &teedataobject_type); |
| 522 | if (!PyArg_ParseTuple(args, "OO!O", &it, &PyList_Type, &values, &next)) |
| 523 | return NULL; |
| 524 | |
| 525 | tdo = (teedataobject *)teedataobject_newinternal(it); |
| 526 | if (!tdo) |
| 527 | return NULL; |
| 528 | |
| 529 | len = PyList_GET_SIZE(values); |
| 530 | if (len > LINKCELLS) |
| 531 | goto err; |
| 532 | for (i=0; i<len; i++) { |
| 533 | tdo->values[i] = PyList_GET_ITEM(values, i); |
| 534 | Py_INCREF(tdo->values[i]); |
| 535 | } |
| 536 | tdo->numread = len; |
| 537 | |
| 538 | if (len == LINKCELLS) { |
| 539 | if (next != Py_None) { |
| 540 | if (Py_TYPE(next) != &teedataobject_type) |
| 541 | goto err; |
| 542 | assert(tdo->nextlink == NULL); |
| 543 | Py_INCREF(next); |
| 544 | tdo->nextlink = next; |
| 545 | } |
| 546 | } else { |
| 547 | if (next != Py_None) |
| 548 | goto err; /* shouldn't have a next if we are not full */ |
| 549 | } |
| 550 | return (PyObject*)tdo; |
| 551 | |
| 552 | err: |
| 553 | Py_XDECREF(tdo); |
| 554 | PyErr_SetString(PyExc_ValueError, "Invalid arguments"); |
| 555 | return NULL; |
| 556 | } |
| 557 | |
| 558 | static PyMethodDef teedataobject_methods[] = { |
| 559 | {"__reduce__", (PyCFunction)teedataobject_reduce, METH_NOARGS, |
| 560 | reduce_doc}, |
| 561 | {NULL, NULL} /* sentinel */ |
| 562 | }; |
| 563 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 564 | PyDoc_STRVAR(teedataobject_doc, "Data container common to multiple tee objects."); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 565 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 566 | static PyTypeObject teedataobject_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | PyVarObject_HEAD_INIT(0, 0) /* Must fill in type value later */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 568 | "itertools._tee_dataobject", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | sizeof(teedataobject), /* tp_basicsize */ |
| 570 | 0, /* tp_itemsize */ |
| 571 | /* methods */ |
| 572 | (destructor)teedataobject_dealloc, /* tp_dealloc */ |
| 573 | 0, /* tp_print */ |
| 574 | 0, /* tp_getattr */ |
| 575 | 0, /* tp_setattr */ |
| 576 | 0, /* tp_reserved */ |
| 577 | 0, /* tp_repr */ |
| 578 | 0, /* tp_as_number */ |
| 579 | 0, /* tp_as_sequence */ |
| 580 | 0, /* tp_as_mapping */ |
| 581 | 0, /* tp_hash */ |
| 582 | 0, /* tp_call */ |
| 583 | 0, /* tp_str */ |
| 584 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 585 | 0, /* tp_setattro */ |
| 586 | 0, /* tp_as_buffer */ |
| 587 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 588 | teedataobject_doc, /* tp_doc */ |
| 589 | (traverseproc)teedataobject_traverse, /* tp_traverse */ |
| 590 | (inquiry)teedataobject_clear, /* tp_clear */ |
| 591 | 0, /* tp_richcompare */ |
| 592 | 0, /* tp_weaklistoffset */ |
| 593 | 0, /* tp_iter */ |
| 594 | 0, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 595 | teedataobject_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | 0, /* tp_members */ |
| 597 | 0, /* tp_getset */ |
| 598 | 0, /* tp_base */ |
| 599 | 0, /* tp_dict */ |
| 600 | 0, /* tp_descr_get */ |
| 601 | 0, /* tp_descr_set */ |
| 602 | 0, /* tp_dictoffset */ |
| 603 | 0, /* tp_init */ |
| 604 | 0, /* tp_alloc */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 605 | teedataobject_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 607 | }; |
| 608 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 609 | |
| 610 | static PyTypeObject tee_type; |
| 611 | |
| 612 | static PyObject * |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 613 | tee_next(teeobject *to) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 614 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | PyObject *value, *link; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | if (to->index >= LINKCELLS) { |
| 618 | link = teedataobject_jumplink(to->dataobj); |
| 619 | Py_DECREF(to->dataobj); |
| 620 | to->dataobj = (teedataobject *)link; |
| 621 | to->index = 0; |
| 622 | } |
| 623 | value = teedataobject_getitem(to->dataobj, to->index); |
| 624 | if (value == NULL) |
| 625 | return NULL; |
| 626 | to->index++; |
| 627 | return value; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 630 | static int |
| 631 | tee_traverse(teeobject *to, visitproc visit, void *arg) |
| 632 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | Py_VISIT((PyObject *)to->dataobj); |
| 634 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 637 | static PyObject * |
| 638 | tee_copy(teeobject *to) |
| 639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | teeobject *newto; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | newto = PyObject_GC_New(teeobject, &tee_type); |
| 643 | if (newto == NULL) |
| 644 | return NULL; |
| 645 | Py_INCREF(to->dataobj); |
| 646 | newto->dataobj = to->dataobj; |
| 647 | newto->index = to->index; |
| 648 | newto->weakreflist = NULL; |
| 649 | PyObject_GC_Track(newto); |
| 650 | return (PyObject *)newto; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | PyDoc_STRVAR(teecopy_doc, "Returns an independent iterator."); |
| 654 | |
| 655 | static PyObject * |
| 656 | tee_fromiterable(PyObject *iterable) |
| 657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | teeobject *to; |
| 659 | PyObject *it = NULL; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | it = PyObject_GetIter(iterable); |
| 662 | if (it == NULL) |
| 663 | return NULL; |
| 664 | if (PyObject_TypeCheck(it, &tee_type)) { |
| 665 | to = (teeobject *)tee_copy((teeobject *)it); |
| 666 | goto done; |
| 667 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | to = PyObject_GC_New(teeobject, &tee_type); |
| 670 | if (to == NULL) |
| 671 | goto done; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 672 | to->dataobj = (teedataobject *)teedataobject_newinternal(it); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | if (!to->dataobj) { |
| 674 | PyObject_GC_Del(to); |
| 675 | to = NULL; |
| 676 | goto done; |
| 677 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | to->index = 0; |
| 680 | to->weakreflist = NULL; |
| 681 | PyObject_GC_Track(to); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 682 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | Py_XDECREF(it); |
| 684 | return (PyObject *)to; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static PyObject * |
| 688 | tee_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 690 | PyObject *iterable; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 691 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 692 | if (!PyArg_UnpackTuple(args, "_tee", 1, 1, &iterable)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | return NULL; |
| 694 | return tee_fromiterable(iterable); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 697 | static int |
| 698 | tee_clear(teeobject *to) |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | if (to->weakreflist != NULL) |
| 701 | PyObject_ClearWeakRefs((PyObject *) to); |
| 702 | Py_CLEAR(to->dataobj); |
| 703 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static void |
| 707 | tee_dealloc(teeobject *to) |
| 708 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | PyObject_GC_UnTrack(to); |
| 710 | tee_clear(to); |
| 711 | PyObject_GC_Del(to); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 714 | static PyObject * |
| 715 | tee_reduce(teeobject *to) |
| 716 | { |
| 717 | return Py_BuildValue("O(())(Oi)", Py_TYPE(to), to->dataobj, to->index); |
| 718 | } |
| 719 | |
| 720 | static PyObject * |
| 721 | tee_setstate(teeobject *to, PyObject *state) |
| 722 | { |
| 723 | teedataobject *tdo; |
| 724 | int index; |
| 725 | if (!PyArg_ParseTuple(state, "O!i", &teedataobject_type, &tdo, &index)) |
| 726 | return NULL; |
| 727 | if (index < 0 || index > LINKCELLS) { |
| 728 | PyErr_SetString(PyExc_ValueError, "Index out of range"); |
| 729 | return NULL; |
| 730 | } |
| 731 | Py_CLEAR(to->dataobj); |
| 732 | to->dataobj = tdo; |
| 733 | Py_INCREF(to->dataobj); |
| 734 | to->index = index; |
| 735 | Py_RETURN_NONE; |
| 736 | } |
| 737 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 738 | PyDoc_STRVAR(teeobject_doc, |
| 739 | "Iterator wrapped to make it copyable"); |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 740 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 741 | static PyMethodDef tee_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | {"__copy__", (PyCFunction)tee_copy, METH_NOARGS, teecopy_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 743 | {"__reduce__", (PyCFunction)tee_reduce, METH_NOARGS, reduce_doc}, |
| 744 | {"__setstate__", (PyCFunction)tee_setstate, METH_O, setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 746 | }; |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 747 | |
| 748 | static PyTypeObject tee_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 750 | "itertools._tee", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | sizeof(teeobject), /* tp_basicsize */ |
| 752 | 0, /* tp_itemsize */ |
| 753 | /* methods */ |
| 754 | (destructor)tee_dealloc, /* tp_dealloc */ |
| 755 | 0, /* tp_print */ |
| 756 | 0, /* tp_getattr */ |
| 757 | 0, /* tp_setattr */ |
| 758 | 0, /* tp_reserved */ |
| 759 | 0, /* tp_repr */ |
| 760 | 0, /* tp_as_number */ |
| 761 | 0, /* tp_as_sequence */ |
| 762 | 0, /* tp_as_mapping */ |
| 763 | 0, /* tp_hash */ |
| 764 | 0, /* tp_call */ |
| 765 | 0, /* tp_str */ |
| 766 | 0, /* tp_getattro */ |
| 767 | 0, /* tp_setattro */ |
| 768 | 0, /* tp_as_buffer */ |
| 769 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 770 | teeobject_doc, /* tp_doc */ |
| 771 | (traverseproc)tee_traverse, /* tp_traverse */ |
| 772 | (inquiry)tee_clear, /* tp_clear */ |
| 773 | 0, /* tp_richcompare */ |
| 774 | offsetof(teeobject, weakreflist), /* tp_weaklistoffset */ |
| 775 | PyObject_SelfIter, /* tp_iter */ |
| 776 | (iternextfunc)tee_next, /* tp_iternext */ |
| 777 | tee_methods, /* tp_methods */ |
| 778 | 0, /* tp_members */ |
| 779 | 0, /* tp_getset */ |
| 780 | 0, /* tp_base */ |
| 781 | 0, /* tp_dict */ |
| 782 | 0, /* tp_descr_get */ |
| 783 | 0, /* tp_descr_set */ |
| 784 | 0, /* tp_dictoffset */ |
| 785 | 0, /* tp_init */ |
| 786 | 0, /* tp_alloc */ |
| 787 | tee_new, /* tp_new */ |
| 788 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 789 | }; |
| 790 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 791 | static PyObject * |
| 792 | tee(PyObject *self, PyObject *args) |
| 793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | Py_ssize_t i, n=2; |
| 795 | PyObject *it, *iterable, *copyable, *result; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 796 | _Py_IDENTIFIER(__copy__); |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | if (!PyArg_ParseTuple(args, "O|n", &iterable, &n)) |
| 799 | return NULL; |
| 800 | if (n < 0) { |
| 801 | PyErr_SetString(PyExc_ValueError, "n must be >= 0"); |
| 802 | return NULL; |
| 803 | } |
| 804 | result = PyTuple_New(n); |
| 805 | if (result == NULL) |
| 806 | return NULL; |
| 807 | if (n == 0) |
| 808 | return result; |
| 809 | it = PyObject_GetIter(iterable); |
| 810 | if (it == NULL) { |
| 811 | Py_DECREF(result); |
| 812 | return NULL; |
| 813 | } |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 814 | if (!_PyObject_HasAttrId(it, &PyId___copy__)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | copyable = tee_fromiterable(it); |
| 816 | Py_DECREF(it); |
| 817 | if (copyable == NULL) { |
| 818 | Py_DECREF(result); |
| 819 | return NULL; |
| 820 | } |
| 821 | } else |
| 822 | copyable = it; |
| 823 | PyTuple_SET_ITEM(result, 0, copyable); |
| 824 | for (i=1 ; i<n ; i++) { |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 825 | |
| 826 | copyable = _PyObject_CallMethodId(copyable, &PyId___copy__, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | if (copyable == NULL) { |
| 828 | Py_DECREF(result); |
| 829 | return NULL; |
| 830 | } |
| 831 | PyTuple_SET_ITEM(result, i, copyable); |
| 832 | } |
| 833 | return result; |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | PyDoc_STRVAR(tee_doc, |
| 837 | "tee(iterable, n=2) --> tuple of n independent iterators."); |
| 838 | |
| 839 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 840 | /* cycle object **********************************************************/ |
| 841 | |
| 842 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | PyObject_HEAD |
| 844 | PyObject *it; |
| 845 | PyObject *saved; |
| 846 | int firstpass; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 847 | } cycleobject; |
| 848 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 849 | static PyTypeObject cycle_type; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 850 | |
| 851 | static PyObject * |
| 852 | cycle_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | PyObject *it; |
| 855 | PyObject *iterable; |
| 856 | PyObject *saved; |
| 857 | cycleobject *lz; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | if (type == &cycle_type && !_PyArg_NoKeywords("cycle()", kwds)) |
| 860 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | if (!PyArg_UnpackTuple(args, "cycle", 1, 1, &iterable)) |
| 863 | return NULL; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | /* Get iterator. */ |
| 866 | it = PyObject_GetIter(iterable); |
| 867 | if (it == NULL) |
| 868 | return NULL; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | saved = PyList_New(0); |
| 871 | if (saved == NULL) { |
| 872 | Py_DECREF(it); |
| 873 | return NULL; |
| 874 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | /* create cycleobject structure */ |
| 877 | lz = (cycleobject *)type->tp_alloc(type, 0); |
| 878 | if (lz == NULL) { |
| 879 | Py_DECREF(it); |
| 880 | Py_DECREF(saved); |
| 881 | return NULL; |
| 882 | } |
| 883 | lz->it = it; |
| 884 | lz->saved = saved; |
| 885 | lz->firstpass = 0; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | return (PyObject *)lz; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static void |
| 891 | cycle_dealloc(cycleobject *lz) |
| 892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | PyObject_GC_UnTrack(lz); |
| 894 | Py_XDECREF(lz->saved); |
| 895 | Py_XDECREF(lz->it); |
| 896 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | static int |
| 900 | cycle_traverse(cycleobject *lz, visitproc visit, void *arg) |
| 901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | Py_VISIT(lz->it); |
| 903 | Py_VISIT(lz->saved); |
| 904 | return 0; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | static PyObject * |
| 908 | cycle_next(cycleobject *lz) |
| 909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | PyObject *item; |
| 911 | PyObject *it; |
| 912 | PyObject *tmp; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | while (1) { |
| 915 | item = PyIter_Next(lz->it); |
| 916 | if (item != NULL) { |
| 917 | if (!lz->firstpass && PyList_Append(lz->saved, item)) { |
| 918 | Py_DECREF(item); |
| 919 | return NULL; |
| 920 | } |
| 921 | return item; |
| 922 | } |
| 923 | if (PyErr_Occurred()) { |
| 924 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 925 | PyErr_Clear(); |
| 926 | else |
| 927 | return NULL; |
| 928 | } |
| 929 | if (PyList_Size(lz->saved) == 0) |
| 930 | return NULL; |
| 931 | it = PyObject_GetIter(lz->saved); |
| 932 | if (it == NULL) |
| 933 | return NULL; |
| 934 | tmp = lz->it; |
| 935 | lz->it = it; |
| 936 | lz->firstpass = 1; |
| 937 | Py_DECREF(tmp); |
| 938 | } |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 941 | static PyObject * |
| 942 | cycle_reduce(cycleobject *lz) |
| 943 | { |
| 944 | /* Create a new cycle with the iterator tuple, then set |
| 945 | * the saved state on it. |
| 946 | */ |
| 947 | return Py_BuildValue("O(O)(Oi)", Py_TYPE(lz), |
| 948 | lz->it, lz->saved, lz->firstpass); |
| 949 | } |
| 950 | |
| 951 | static PyObject * |
| 952 | cycle_setstate(cycleobject *lz, PyObject *state) |
| 953 | { |
| 954 | PyObject *saved=NULL; |
| 955 | int firstpass; |
| 956 | if (!PyArg_ParseTuple(state, "Oi", &saved, &firstpass)) |
| 957 | return NULL; |
| 958 | Py_CLEAR(lz->saved); |
| 959 | lz->saved = saved; |
| 960 | Py_XINCREF(lz->saved); |
| 961 | lz->firstpass = firstpass != 0; |
| 962 | Py_RETURN_NONE; |
| 963 | } |
| 964 | |
| 965 | static PyMethodDef cycle_methods[] = { |
| 966 | {"__reduce__", (PyCFunction)cycle_reduce, METH_NOARGS, |
| 967 | reduce_doc}, |
| 968 | {"__setstate__", (PyCFunction)cycle_setstate, METH_O, |
| 969 | setstate_doc}, |
| 970 | {NULL, NULL} /* sentinel */ |
| 971 | }; |
| 972 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 973 | PyDoc_STRVAR(cycle_doc, |
| 974 | "cycle(iterable) --> cycle object\n\ |
| 975 | \n\ |
| 976 | Return elements from the iterable until it is exhausted.\n\ |
| 977 | Then repeat the sequence indefinitely."); |
| 978 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 979 | static PyTypeObject cycle_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | PyVarObject_HEAD_INIT(NULL, 0) |
| 981 | "itertools.cycle", /* tp_name */ |
| 982 | sizeof(cycleobject), /* tp_basicsize */ |
| 983 | 0, /* tp_itemsize */ |
| 984 | /* methods */ |
| 985 | (destructor)cycle_dealloc, /* tp_dealloc */ |
| 986 | 0, /* tp_print */ |
| 987 | 0, /* tp_getattr */ |
| 988 | 0, /* tp_setattr */ |
| 989 | 0, /* tp_reserved */ |
| 990 | 0, /* tp_repr */ |
| 991 | 0, /* tp_as_number */ |
| 992 | 0, /* tp_as_sequence */ |
| 993 | 0, /* tp_as_mapping */ |
| 994 | 0, /* tp_hash */ |
| 995 | 0, /* tp_call */ |
| 996 | 0, /* tp_str */ |
| 997 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 998 | 0, /* tp_setattro */ |
| 999 | 0, /* tp_as_buffer */ |
| 1000 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1001 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1002 | cycle_doc, /* tp_doc */ |
| 1003 | (traverseproc)cycle_traverse, /* tp_traverse */ |
| 1004 | 0, /* tp_clear */ |
| 1005 | 0, /* tp_richcompare */ |
| 1006 | 0, /* tp_weaklistoffset */ |
| 1007 | PyObject_SelfIter, /* tp_iter */ |
| 1008 | (iternextfunc)cycle_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1009 | cycle_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | 0, /* tp_members */ |
| 1011 | 0, /* tp_getset */ |
| 1012 | 0, /* tp_base */ |
| 1013 | 0, /* tp_dict */ |
| 1014 | 0, /* tp_descr_get */ |
| 1015 | 0, /* tp_descr_set */ |
| 1016 | 0, /* tp_dictoffset */ |
| 1017 | 0, /* tp_init */ |
| 1018 | 0, /* tp_alloc */ |
| 1019 | cycle_new, /* tp_new */ |
| 1020 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1021 | }; |
| 1022 | |
| 1023 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1024 | /* dropwhile object **********************************************************/ |
| 1025 | |
| 1026 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | PyObject_HEAD |
| 1028 | PyObject *func; |
| 1029 | PyObject *it; |
| 1030 | long start; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1031 | } dropwhileobject; |
| 1032 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1033 | static PyTypeObject dropwhile_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1034 | |
| 1035 | static PyObject * |
| 1036 | dropwhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1037 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | PyObject *func, *seq; |
| 1039 | PyObject *it; |
| 1040 | dropwhileobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | if (type == &dropwhile_type && !_PyArg_NoKeywords("dropwhile()", kwds)) |
| 1043 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | if (!PyArg_UnpackTuple(args, "dropwhile", 2, 2, &func, &seq)) |
| 1046 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | /* Get iterator. */ |
| 1049 | it = PyObject_GetIter(seq); |
| 1050 | if (it == NULL) |
| 1051 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | /* create dropwhileobject structure */ |
| 1054 | lz = (dropwhileobject *)type->tp_alloc(type, 0); |
| 1055 | if (lz == NULL) { |
| 1056 | Py_DECREF(it); |
| 1057 | return NULL; |
| 1058 | } |
| 1059 | Py_INCREF(func); |
| 1060 | lz->func = func; |
| 1061 | lz->it = it; |
| 1062 | lz->start = 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static void |
| 1068 | dropwhile_dealloc(dropwhileobject *lz) |
| 1069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | PyObject_GC_UnTrack(lz); |
| 1071 | Py_XDECREF(lz->func); |
| 1072 | Py_XDECREF(lz->it); |
| 1073 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static int |
| 1077 | dropwhile_traverse(dropwhileobject *lz, visitproc visit, void *arg) |
| 1078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | Py_VISIT(lz->it); |
| 1080 | Py_VISIT(lz->func); |
| 1081 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | static PyObject * |
| 1085 | dropwhile_next(dropwhileobject *lz) |
| 1086 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | PyObject *item, *good; |
| 1088 | PyObject *it = lz->it; |
| 1089 | long ok; |
| 1090 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | iternext = *Py_TYPE(it)->tp_iternext; |
| 1093 | for (;;) { |
| 1094 | item = iternext(it); |
| 1095 | if (item == NULL) |
| 1096 | return NULL; |
| 1097 | if (lz->start == 1) |
| 1098 | return item; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | good = PyObject_CallFunctionObjArgs(lz->func, item, NULL); |
| 1101 | if (good == NULL) { |
| 1102 | Py_DECREF(item); |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | ok = PyObject_IsTrue(good); |
| 1106 | Py_DECREF(good); |
| 1107 | if (!ok) { |
| 1108 | lz->start = 1; |
| 1109 | return item; |
| 1110 | } |
| 1111 | Py_DECREF(item); |
| 1112 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1115 | static PyObject * |
| 1116 | dropwhile_reduce(dropwhileobject *lz) |
| 1117 | { |
| 1118 | return Py_BuildValue("O(OO)l", Py_TYPE(lz), |
| 1119 | lz->func, lz->it, lz->start); |
| 1120 | } |
| 1121 | |
| 1122 | static PyObject * |
| 1123 | dropwhile_setstate(dropwhileobject *lz, PyObject *state) |
| 1124 | { |
| 1125 | int start = PyObject_IsTrue(state); |
| 1126 | if (start == -1) |
| 1127 | return NULL; |
| 1128 | lz->start = start; |
| 1129 | Py_RETURN_NONE; |
| 1130 | } |
| 1131 | |
| 1132 | static PyMethodDef dropwhile_methods[] = { |
| 1133 | {"__reduce__", (PyCFunction)dropwhile_reduce, METH_NOARGS, |
| 1134 | reduce_doc}, |
| 1135 | {"__setstate__", (PyCFunction)dropwhile_setstate, METH_O, |
| 1136 | setstate_doc}, |
| 1137 | {NULL, NULL} /* sentinel */ |
| 1138 | }; |
| 1139 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1140 | PyDoc_STRVAR(dropwhile_doc, |
| 1141 | "dropwhile(predicate, iterable) --> dropwhile object\n\ |
| 1142 | \n\ |
| 1143 | Drop items from the iterable while predicate(item) is true.\n\ |
| 1144 | Afterwards, return every element until the iterable is exhausted."); |
| 1145 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1146 | static PyTypeObject dropwhile_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1148 | "itertools.dropwhile", /* tp_name */ |
| 1149 | sizeof(dropwhileobject), /* tp_basicsize */ |
| 1150 | 0, /* tp_itemsize */ |
| 1151 | /* methods */ |
| 1152 | (destructor)dropwhile_dealloc, /* tp_dealloc */ |
| 1153 | 0, /* tp_print */ |
| 1154 | 0, /* tp_getattr */ |
| 1155 | 0, /* tp_setattr */ |
| 1156 | 0, /* tp_reserved */ |
| 1157 | 0, /* tp_repr */ |
| 1158 | 0, /* tp_as_number */ |
| 1159 | 0, /* tp_as_sequence */ |
| 1160 | 0, /* tp_as_mapping */ |
| 1161 | 0, /* tp_hash */ |
| 1162 | 0, /* tp_call */ |
| 1163 | 0, /* tp_str */ |
| 1164 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1165 | 0, /* tp_setattro */ |
| 1166 | 0, /* tp_as_buffer */ |
| 1167 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1168 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1169 | dropwhile_doc, /* tp_doc */ |
| 1170 | (traverseproc)dropwhile_traverse, /* tp_traverse */ |
| 1171 | 0, /* tp_clear */ |
| 1172 | 0, /* tp_richcompare */ |
| 1173 | 0, /* tp_weaklistoffset */ |
| 1174 | PyObject_SelfIter, /* tp_iter */ |
| 1175 | (iternextfunc)dropwhile_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1176 | dropwhile_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | 0, /* tp_members */ |
| 1178 | 0, /* tp_getset */ |
| 1179 | 0, /* tp_base */ |
| 1180 | 0, /* tp_dict */ |
| 1181 | 0, /* tp_descr_get */ |
| 1182 | 0, /* tp_descr_set */ |
| 1183 | 0, /* tp_dictoffset */ |
| 1184 | 0, /* tp_init */ |
| 1185 | 0, /* tp_alloc */ |
| 1186 | dropwhile_new, /* tp_new */ |
| 1187 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1188 | }; |
| 1189 | |
| 1190 | |
| 1191 | /* takewhile object **********************************************************/ |
| 1192 | |
| 1193 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | PyObject_HEAD |
| 1195 | PyObject *func; |
| 1196 | PyObject *it; |
| 1197 | long stop; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1198 | } takewhileobject; |
| 1199 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1200 | static PyTypeObject takewhile_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1201 | |
| 1202 | static PyObject * |
| 1203 | takewhile_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | PyObject *func, *seq; |
| 1206 | PyObject *it; |
| 1207 | takewhileobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | if (type == &takewhile_type && !_PyArg_NoKeywords("takewhile()", kwds)) |
| 1210 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | if (!PyArg_UnpackTuple(args, "takewhile", 2, 2, &func, &seq)) |
| 1213 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | /* Get iterator. */ |
| 1216 | it = PyObject_GetIter(seq); |
| 1217 | if (it == NULL) |
| 1218 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | /* create takewhileobject structure */ |
| 1221 | lz = (takewhileobject *)type->tp_alloc(type, 0); |
| 1222 | if (lz == NULL) { |
| 1223 | Py_DECREF(it); |
| 1224 | return NULL; |
| 1225 | } |
| 1226 | Py_INCREF(func); |
| 1227 | lz->func = func; |
| 1228 | lz->it = it; |
| 1229 | lz->stop = 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1231 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | static void |
| 1235 | takewhile_dealloc(takewhileobject *lz) |
| 1236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | PyObject_GC_UnTrack(lz); |
| 1238 | Py_XDECREF(lz->func); |
| 1239 | Py_XDECREF(lz->it); |
| 1240 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | static int |
| 1244 | takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg) |
| 1245 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | Py_VISIT(lz->it); |
| 1247 | Py_VISIT(lz->func); |
| 1248 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | static PyObject * |
| 1252 | takewhile_next(takewhileobject *lz) |
| 1253 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | PyObject *item, *good; |
| 1255 | PyObject *it = lz->it; |
| 1256 | long ok; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1257 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | if (lz->stop == 1) |
| 1259 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | item = (*Py_TYPE(it)->tp_iternext)(it); |
| 1262 | if (item == NULL) |
| 1263 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | good = PyObject_CallFunctionObjArgs(lz->func, item, NULL); |
| 1266 | if (good == NULL) { |
| 1267 | Py_DECREF(item); |
| 1268 | return NULL; |
| 1269 | } |
| 1270 | ok = PyObject_IsTrue(good); |
| 1271 | Py_DECREF(good); |
| 1272 | if (ok) |
| 1273 | return item; |
| 1274 | Py_DECREF(item); |
| 1275 | lz->stop = 1; |
| 1276 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1279 | static PyObject * |
| 1280 | takewhile_reduce(takewhileobject *lz) |
| 1281 | { |
| 1282 | return Py_BuildValue("O(OO)l", Py_TYPE(lz), |
| 1283 | lz->func, lz->it, lz->stop); |
| 1284 | } |
| 1285 | |
| 1286 | static PyObject * |
| 1287 | takewhile_reduce_setstate(takewhileobject *lz, PyObject *state) |
| 1288 | { |
| 1289 | int stop = PyObject_IsTrue(state); |
| 1290 | if (stop == -1) |
| 1291 | return NULL; |
| 1292 | lz->stop = stop; |
| 1293 | Py_RETURN_NONE; |
| 1294 | } |
| 1295 | |
| 1296 | static PyMethodDef takewhile_reduce_methods[] = { |
| 1297 | {"__reduce__", (PyCFunction)takewhile_reduce, METH_NOARGS, |
| 1298 | reduce_doc}, |
| 1299 | {"__setstate__", (PyCFunction)takewhile_reduce_setstate, METH_O, |
| 1300 | setstate_doc}, |
| 1301 | {NULL, NULL} /* sentinel */ |
| 1302 | }; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1303 | PyDoc_STRVAR(takewhile_doc, |
| 1304 | "takewhile(predicate, iterable) --> takewhile object\n\ |
| 1305 | \n\ |
| 1306 | Return successive entries from an iterable as long as the \n\ |
| 1307 | predicate evaluates to true for each entry."); |
| 1308 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1309 | static PyTypeObject takewhile_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1311 | "itertools.takewhile", /* tp_name */ |
| 1312 | sizeof(takewhileobject), /* tp_basicsize */ |
| 1313 | 0, /* tp_itemsize */ |
| 1314 | /* methods */ |
| 1315 | (destructor)takewhile_dealloc, /* tp_dealloc */ |
| 1316 | 0, /* tp_print */ |
| 1317 | 0, /* tp_getattr */ |
| 1318 | 0, /* tp_setattr */ |
| 1319 | 0, /* tp_reserved */ |
| 1320 | 0, /* tp_repr */ |
| 1321 | 0, /* tp_as_number */ |
| 1322 | 0, /* tp_as_sequence */ |
| 1323 | 0, /* tp_as_mapping */ |
| 1324 | 0, /* tp_hash */ |
| 1325 | 0, /* tp_call */ |
| 1326 | 0, /* tp_str */ |
| 1327 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1328 | 0, /* tp_setattro */ |
| 1329 | 0, /* tp_as_buffer */ |
| 1330 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1331 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1332 | takewhile_doc, /* tp_doc */ |
| 1333 | (traverseproc)takewhile_traverse, /* tp_traverse */ |
| 1334 | 0, /* tp_clear */ |
| 1335 | 0, /* tp_richcompare */ |
| 1336 | 0, /* tp_weaklistoffset */ |
| 1337 | PyObject_SelfIter, /* tp_iter */ |
| 1338 | (iternextfunc)takewhile_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1339 | takewhile_reduce_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | 0, /* tp_members */ |
| 1341 | 0, /* tp_getset */ |
| 1342 | 0, /* tp_base */ |
| 1343 | 0, /* tp_dict */ |
| 1344 | 0, /* tp_descr_get */ |
| 1345 | 0, /* tp_descr_set */ |
| 1346 | 0, /* tp_dictoffset */ |
| 1347 | 0, /* tp_init */ |
| 1348 | 0, /* tp_alloc */ |
| 1349 | takewhile_new, /* tp_new */ |
| 1350 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1351 | }; |
| 1352 | |
| 1353 | |
| 1354 | /* islice object ************************************************************/ |
| 1355 | |
| 1356 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | PyObject_HEAD |
| 1358 | PyObject *it; |
| 1359 | Py_ssize_t next; |
| 1360 | Py_ssize_t stop; |
| 1361 | Py_ssize_t step; |
| 1362 | Py_ssize_t cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1363 | } isliceobject; |
| 1364 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1365 | static PyTypeObject islice_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1366 | |
| 1367 | static PyObject * |
| 1368 | islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | PyObject *seq; |
| 1371 | Py_ssize_t start=0, stop=-1, step=1; |
| 1372 | PyObject *it, *a1=NULL, *a2=NULL, *a3=NULL; |
| 1373 | Py_ssize_t numargs; |
| 1374 | isliceobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | if (type == &islice_type && !_PyArg_NoKeywords("islice()", kwds)) |
| 1377 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3)) |
| 1380 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | numargs = PyTuple_Size(args); |
| 1383 | if (numargs == 2) { |
| 1384 | if (a1 != Py_None) { |
| 1385 | stop = PyLong_AsSsize_t(a1); |
| 1386 | if (stop == -1) { |
| 1387 | if (PyErr_Occurred()) |
| 1388 | PyErr_Clear(); |
| 1389 | PyErr_SetString(PyExc_ValueError, |
| 1390 | "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize."); |
| 1391 | return NULL; |
| 1392 | } |
| 1393 | } |
| 1394 | } else { |
| 1395 | if (a1 != Py_None) |
| 1396 | start = PyLong_AsSsize_t(a1); |
| 1397 | if (start == -1 && PyErr_Occurred()) |
| 1398 | PyErr_Clear(); |
| 1399 | if (a2 != Py_None) { |
| 1400 | stop = PyLong_AsSsize_t(a2); |
| 1401 | if (stop == -1) { |
| 1402 | if (PyErr_Occurred()) |
| 1403 | PyErr_Clear(); |
| 1404 | PyErr_SetString(PyExc_ValueError, |
| 1405 | "Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize."); |
| 1406 | return NULL; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | if (start<0 || stop<-1) { |
| 1411 | PyErr_SetString(PyExc_ValueError, |
| 1412 | "Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize."); |
| 1413 | return NULL; |
| 1414 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | if (a3 != NULL) { |
| 1417 | if (a3 != Py_None) |
| 1418 | step = PyLong_AsSsize_t(a3); |
| 1419 | if (step == -1 && PyErr_Occurred()) |
| 1420 | PyErr_Clear(); |
| 1421 | } |
| 1422 | if (step<1) { |
| 1423 | PyErr_SetString(PyExc_ValueError, |
| 1424 | "Step for islice() must be a positive integer or None."); |
| 1425 | return NULL; |
| 1426 | } |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | /* Get iterator. */ |
| 1429 | it = PyObject_GetIter(seq); |
| 1430 | if (it == NULL) |
| 1431 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | /* create isliceobject structure */ |
| 1434 | lz = (isliceobject *)type->tp_alloc(type, 0); |
| 1435 | if (lz == NULL) { |
| 1436 | Py_DECREF(it); |
| 1437 | return NULL; |
| 1438 | } |
| 1439 | lz->it = it; |
| 1440 | lz->next = start; |
| 1441 | lz->stop = stop; |
| 1442 | lz->step = step; |
| 1443 | lz->cnt = 0L; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | static void |
| 1449 | islice_dealloc(isliceobject *lz) |
| 1450 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | PyObject_GC_UnTrack(lz); |
| 1452 | Py_XDECREF(lz->it); |
| 1453 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | static int |
| 1457 | islice_traverse(isliceobject *lz, visitproc visit, void *arg) |
| 1458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | Py_VISIT(lz->it); |
| 1460 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | static PyObject * |
| 1464 | islice_next(isliceobject *lz) |
| 1465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | PyObject *item; |
| 1467 | PyObject *it = lz->it; |
Raymond Hettinger | 69b34bf | 2010-11-30 02:49:29 +0000 | [diff] [blame] | 1468 | Py_ssize_t stop = lz->stop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | Py_ssize_t oldnext; |
| 1470 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | iternext = *Py_TYPE(it)->tp_iternext; |
| 1473 | while (lz->cnt < lz->next) { |
| 1474 | item = iternext(it); |
| 1475 | if (item == NULL) |
| 1476 | return NULL; |
| 1477 | Py_DECREF(item); |
| 1478 | lz->cnt++; |
| 1479 | } |
Raymond Hettinger | 69b34bf | 2010-11-30 02:49:29 +0000 | [diff] [blame] | 1480 | if (stop != -1 && lz->cnt >= stop) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | return NULL; |
| 1482 | item = iternext(it); |
| 1483 | if (item == NULL) |
| 1484 | return NULL; |
| 1485 | lz->cnt++; |
| 1486 | oldnext = lz->next; |
Mark Dickinson | b2f6bc7 | 2011-09-24 08:56:09 +0100 | [diff] [blame] | 1487 | /* The (size_t) cast below avoids the danger of undefined |
| 1488 | behaviour from signed integer overflow. */ |
| 1489 | lz->next += (size_t)lz->step; |
Raymond Hettinger | 69b34bf | 2010-11-30 02:49:29 +0000 | [diff] [blame] | 1490 | if (lz->next < oldnext || (stop != -1 && lz->next > stop)) |
| 1491 | lz->next = stop; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | return item; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1495 | static PyObject * |
| 1496 | islice_reduce(isliceobject *lz) |
| 1497 | { |
| 1498 | /* When unpickled, generate a new object with the same bounds, |
| 1499 | * then 'setstate' with the next and count |
| 1500 | */ |
| 1501 | PyObject *stop; |
| 1502 | if (lz->stop == -1) { |
| 1503 | stop = Py_None; |
| 1504 | Py_INCREF(stop); |
| 1505 | } else { |
| 1506 | stop = PyLong_FromSsize_t(lz->stop); |
| 1507 | if (stop == NULL) |
| 1508 | return NULL; |
| 1509 | } |
| 1510 | return Py_BuildValue("O(OnNn)n", Py_TYPE(lz), |
| 1511 | lz->it, lz->next, stop, lz->step, |
| 1512 | lz->cnt); |
| 1513 | } |
| 1514 | |
| 1515 | static PyObject * |
| 1516 | islice_setstate(isliceobject *lz, PyObject *state) |
| 1517 | { |
| 1518 | Py_ssize_t cnt = PyLong_AsSsize_t(state); |
| 1519 | if (cnt == -1 && PyErr_Occurred()) |
| 1520 | return NULL; |
| 1521 | lz->cnt = cnt; |
| 1522 | Py_RETURN_NONE; |
| 1523 | } |
| 1524 | |
| 1525 | static PyMethodDef islice_methods[] = { |
| 1526 | {"__reduce__", (PyCFunction)islice_reduce, METH_NOARGS, |
| 1527 | reduce_doc}, |
| 1528 | {"__setstate__", (PyCFunction)islice_setstate, METH_O, |
| 1529 | setstate_doc}, |
| 1530 | {NULL, NULL} /* sentinel */ |
| 1531 | }; |
| 1532 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1533 | PyDoc_STRVAR(islice_doc, |
| 1534 | "islice(iterable, [start,] stop [, step]) --> islice object\n\ |
| 1535 | \n\ |
| 1536 | Return an iterator whose next() method returns selected values from an\n\ |
| 1537 | iterable. If start is specified, will skip all preceding elements;\n\ |
| 1538 | otherwise, start defaults to zero. Step defaults to one. If\n\ |
| 1539 | specified as another value, step determines how many values are \n\ |
| 1540 | skipped between successive calls. Works like a slice() on a list\n\ |
| 1541 | but returns an iterator."); |
| 1542 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1543 | static PyTypeObject islice_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1545 | "itertools.islice", /* tp_name */ |
| 1546 | sizeof(isliceobject), /* tp_basicsize */ |
| 1547 | 0, /* tp_itemsize */ |
| 1548 | /* methods */ |
| 1549 | (destructor)islice_dealloc, /* tp_dealloc */ |
| 1550 | 0, /* tp_print */ |
| 1551 | 0, /* tp_getattr */ |
| 1552 | 0, /* tp_setattr */ |
| 1553 | 0, /* tp_reserved */ |
| 1554 | 0, /* tp_repr */ |
| 1555 | 0, /* tp_as_number */ |
| 1556 | 0, /* tp_as_sequence */ |
| 1557 | 0, /* tp_as_mapping */ |
| 1558 | 0, /* tp_hash */ |
| 1559 | 0, /* tp_call */ |
| 1560 | 0, /* tp_str */ |
| 1561 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1562 | 0, /* tp_setattro */ |
| 1563 | 0, /* tp_as_buffer */ |
| 1564 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1565 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1566 | islice_doc, /* tp_doc */ |
| 1567 | (traverseproc)islice_traverse, /* tp_traverse */ |
| 1568 | 0, /* tp_clear */ |
| 1569 | 0, /* tp_richcompare */ |
| 1570 | 0, /* tp_weaklistoffset */ |
| 1571 | PyObject_SelfIter, /* tp_iter */ |
| 1572 | (iternextfunc)islice_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1573 | islice_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | 0, /* tp_members */ |
| 1575 | 0, /* tp_getset */ |
| 1576 | 0, /* tp_base */ |
| 1577 | 0, /* tp_dict */ |
| 1578 | 0, /* tp_descr_get */ |
| 1579 | 0, /* tp_descr_set */ |
| 1580 | 0, /* tp_dictoffset */ |
| 1581 | 0, /* tp_init */ |
| 1582 | 0, /* tp_alloc */ |
| 1583 | islice_new, /* tp_new */ |
| 1584 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1585 | }; |
| 1586 | |
| 1587 | |
| 1588 | /* starmap object ************************************************************/ |
| 1589 | |
| 1590 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | PyObject_HEAD |
| 1592 | PyObject *func; |
| 1593 | PyObject *it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1594 | } starmapobject; |
| 1595 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1596 | static PyTypeObject starmap_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1597 | |
| 1598 | static PyObject * |
| 1599 | starmap_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1600 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | PyObject *func, *seq; |
| 1602 | PyObject *it; |
| 1603 | starmapobject *lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | if (type == &starmap_type && !_PyArg_NoKeywords("starmap()", kwds)) |
| 1606 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | if (!PyArg_UnpackTuple(args, "starmap", 2, 2, &func, &seq)) |
| 1609 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | /* Get iterator. */ |
| 1612 | it = PyObject_GetIter(seq); |
| 1613 | if (it == NULL) |
| 1614 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1616 | /* create starmapobject structure */ |
| 1617 | lz = (starmapobject *)type->tp_alloc(type, 0); |
| 1618 | if (lz == NULL) { |
| 1619 | Py_DECREF(it); |
| 1620 | return NULL; |
| 1621 | } |
| 1622 | Py_INCREF(func); |
| 1623 | lz->func = func; |
| 1624 | lz->it = it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | static void |
| 1630 | starmap_dealloc(starmapobject *lz) |
| 1631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1632 | PyObject_GC_UnTrack(lz); |
| 1633 | Py_XDECREF(lz->func); |
| 1634 | Py_XDECREF(lz->it); |
| 1635 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | static int |
| 1639 | starmap_traverse(starmapobject *lz, visitproc visit, void *arg) |
| 1640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | Py_VISIT(lz->it); |
| 1642 | Py_VISIT(lz->func); |
| 1643 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | static PyObject * |
| 1647 | starmap_next(starmapobject *lz) |
| 1648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | PyObject *args; |
| 1650 | PyObject *result; |
| 1651 | PyObject *it = lz->it; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | args = (*Py_TYPE(it)->tp_iternext)(it); |
| 1654 | if (args == NULL) |
| 1655 | return NULL; |
| 1656 | if (!PyTuple_CheckExact(args)) { |
| 1657 | PyObject *newargs = PySequence_Tuple(args); |
| 1658 | Py_DECREF(args); |
| 1659 | if (newargs == NULL) |
| 1660 | return NULL; |
| 1661 | args = newargs; |
| 1662 | } |
| 1663 | result = PyObject_Call(lz->func, args, NULL); |
| 1664 | Py_DECREF(args); |
| 1665 | return result; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1668 | static PyObject * |
| 1669 | starmap_reduce(starmapobject *lz) |
| 1670 | { |
| 1671 | /* Just pickle the iterator */ |
| 1672 | return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it); |
| 1673 | } |
| 1674 | |
| 1675 | static PyMethodDef starmap_methods[] = { |
| 1676 | {"__reduce__", (PyCFunction)starmap_reduce, METH_NOARGS, |
| 1677 | reduce_doc}, |
| 1678 | {NULL, NULL} /* sentinel */ |
| 1679 | }; |
| 1680 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1681 | PyDoc_STRVAR(starmap_doc, |
| 1682 | "starmap(function, sequence) --> starmap object\n\ |
| 1683 | \n\ |
| 1684 | Return an iterator whose values are returned from the function evaluated\n\ |
| 1685 | with a argument tuple taken from the given sequence."); |
| 1686 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1687 | static PyTypeObject starmap_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1689 | "itertools.starmap", /* tp_name */ |
| 1690 | sizeof(starmapobject), /* tp_basicsize */ |
| 1691 | 0, /* tp_itemsize */ |
| 1692 | /* methods */ |
| 1693 | (destructor)starmap_dealloc, /* tp_dealloc */ |
| 1694 | 0, /* tp_print */ |
| 1695 | 0, /* tp_getattr */ |
| 1696 | 0, /* tp_setattr */ |
| 1697 | 0, /* tp_reserved */ |
| 1698 | 0, /* tp_repr */ |
| 1699 | 0, /* tp_as_number */ |
| 1700 | 0, /* tp_as_sequence */ |
| 1701 | 0, /* tp_as_mapping */ |
| 1702 | 0, /* tp_hash */ |
| 1703 | 0, /* tp_call */ |
| 1704 | 0, /* tp_str */ |
| 1705 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1706 | 0, /* tp_setattro */ |
| 1707 | 0, /* tp_as_buffer */ |
| 1708 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1709 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1710 | starmap_doc, /* tp_doc */ |
| 1711 | (traverseproc)starmap_traverse, /* tp_traverse */ |
| 1712 | 0, /* tp_clear */ |
| 1713 | 0, /* tp_richcompare */ |
| 1714 | 0, /* tp_weaklistoffset */ |
| 1715 | PyObject_SelfIter, /* tp_iter */ |
| 1716 | (iternextfunc)starmap_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1717 | starmap_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | 0, /* tp_members */ |
| 1719 | 0, /* tp_getset */ |
| 1720 | 0, /* tp_base */ |
| 1721 | 0, /* tp_dict */ |
| 1722 | 0, /* tp_descr_get */ |
| 1723 | 0, /* tp_descr_set */ |
| 1724 | 0, /* tp_dictoffset */ |
| 1725 | 0, /* tp_init */ |
| 1726 | 0, /* tp_alloc */ |
| 1727 | starmap_new, /* tp_new */ |
| 1728 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1729 | }; |
| 1730 | |
| 1731 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1732 | /* chain object ************************************************************/ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1733 | |
| 1734 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | PyObject_HEAD |
| 1736 | PyObject *source; /* Iterator over input iterables */ |
| 1737 | PyObject *active; /* Currently running input iterator */ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1738 | } chainobject; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1739 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1740 | static PyTypeObject chain_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | static PyObject * |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 1743 | chain_new_internal(PyTypeObject *type, PyObject *source) |
| 1744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | chainobject *lz; |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 1746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | lz = (chainobject *)type->tp_alloc(type, 0); |
| 1748 | if (lz == NULL) { |
| 1749 | Py_DECREF(source); |
| 1750 | return NULL; |
| 1751 | } |
| 1752 | |
| 1753 | lz->source = source; |
| 1754 | lz->active = NULL; |
| 1755 | return (PyObject *)lz; |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1758 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1759 | chain_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | PyObject *source; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | if (type == &chain_type && !_PyArg_NoKeywords("chain()", kwds)) |
| 1764 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | source = PyObject_GetIter(args); |
| 1767 | if (source == NULL) |
| 1768 | return NULL; |
| 1769 | |
| 1770 | return chain_new_internal(type, source); |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | static PyObject * |
| 1774 | chain_new_from_iterable(PyTypeObject *type, PyObject *arg) |
| 1775 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | PyObject *source; |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | source = PyObject_GetIter(arg); |
| 1779 | if (source == NULL) |
| 1780 | return NULL; |
| 1781 | |
| 1782 | return chain_new_internal(type, source); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | static void |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1786 | chain_dealloc(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | PyObject_GC_UnTrack(lz); |
| 1789 | Py_XDECREF(lz->active); |
| 1790 | Py_XDECREF(lz->source); |
| 1791 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1794 | static int |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1795 | chain_traverse(chainobject *lz, visitproc visit, void *arg) |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | Py_VISIT(lz->source); |
| 1798 | Py_VISIT(lz->active); |
| 1799 | return 0; |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1802 | static PyObject * |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1803 | chain_next(chainobject *lz) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1804 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | PyObject *item; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1807 | if (lz->source == NULL) |
| 1808 | return NULL; /* already stopped */ |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 1809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | if (lz->active == NULL) { |
| 1811 | PyObject *iterable = PyIter_Next(lz->source); |
| 1812 | if (iterable == NULL) { |
| 1813 | Py_CLEAR(lz->source); |
| 1814 | return NULL; /* no more input sources */ |
| 1815 | } |
| 1816 | lz->active = PyObject_GetIter(iterable); |
| 1817 | Py_DECREF(iterable); |
| 1818 | if (lz->active == NULL) { |
| 1819 | Py_CLEAR(lz->source); |
| 1820 | return NULL; /* input not iterable */ |
| 1821 | } |
| 1822 | } |
| 1823 | item = PyIter_Next(lz->active); |
| 1824 | if (item != NULL) |
| 1825 | return item; |
| 1826 | if (PyErr_Occurred()) { |
| 1827 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 1828 | PyErr_Clear(); |
| 1829 | else |
| 1830 | return NULL; /* input raised an exception */ |
| 1831 | } |
| 1832 | Py_CLEAR(lz->active); |
| 1833 | return chain_next(lz); /* recurse and use next active */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1836 | static PyObject * |
| 1837 | chain_reduce(chainobject *lz) |
| 1838 | { |
| 1839 | if (lz->source) { |
| 1840 | /* we can't pickle function objects (itertools.from_iterable) so |
| 1841 | * we must use setstate to replace the iterable. One day we |
| 1842 | * will fix pickling of functions |
| 1843 | */ |
| 1844 | if (lz->active) { |
| 1845 | return Py_BuildValue("O()(OO)", Py_TYPE(lz), lz->source, lz->active); |
| 1846 | } else { |
| 1847 | return Py_BuildValue("O()(O)", Py_TYPE(lz), lz->source); |
| 1848 | } |
| 1849 | } else { |
| 1850 | return Py_BuildValue("O()", Py_TYPE(lz)); /* exhausted */ |
| 1851 | } |
| 1852 | return NULL; |
| 1853 | } |
| 1854 | |
| 1855 | static PyObject * |
| 1856 | chain_setstate(chainobject *lz, PyObject *state) |
| 1857 | { |
| 1858 | PyObject *source, *active=NULL; |
| 1859 | if (! PyArg_ParseTuple(state, "O|O", &source, &active)) |
| 1860 | return NULL; |
| 1861 | |
| 1862 | Py_CLEAR(lz->source); |
| 1863 | lz->source = source; |
| 1864 | Py_INCREF(lz->source); |
| 1865 | Py_CLEAR(lz->active); |
| 1866 | lz->active = active; |
| 1867 | Py_XINCREF(lz->active); |
| 1868 | Py_RETURN_NONE; |
| 1869 | } |
| 1870 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1871 | PyDoc_STRVAR(chain_doc, |
| 1872 | "chain(*iterables) --> chain object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1873 | \n\ |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1874 | Return a chain object whose .__next__() method returns elements from the\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 1875 | first iterable until it is exhausted, then elements from the next\n\ |
| 1876 | iterable, until all of the iterables are exhausted."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1877 | |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 1878 | PyDoc_STRVAR(chain_from_iterable_doc, |
| 1879 | "chain.from_iterable(iterable) --> chain object\n\ |
| 1880 | \n\ |
| 1881 | Alternate chain() contructor taking a single iterable argument\n\ |
| 1882 | that evaluates lazily."); |
| 1883 | |
| 1884 | static PyMethodDef chain_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | {"from_iterable", (PyCFunction) chain_new_from_iterable, METH_O | METH_CLASS, |
| 1886 | chain_from_iterable_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 1887 | {"__reduce__", (PyCFunction)chain_reduce, METH_NOARGS, |
| 1888 | reduce_doc}, |
| 1889 | {"__setstate__", (PyCFunction)chain_setstate, METH_O, |
| 1890 | setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | {NULL, NULL} /* sentinel */ |
Christian Heimes | f16baeb | 2008-02-29 14:57:44 +0000 | [diff] [blame] | 1892 | }; |
| 1893 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 1894 | static PyTypeObject chain_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1895 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1896 | "itertools.chain", /* tp_name */ |
| 1897 | sizeof(chainobject), /* tp_basicsize */ |
| 1898 | 0, /* tp_itemsize */ |
| 1899 | /* methods */ |
| 1900 | (destructor)chain_dealloc, /* tp_dealloc */ |
| 1901 | 0, /* tp_print */ |
| 1902 | 0, /* tp_getattr */ |
| 1903 | 0, /* tp_setattr */ |
| 1904 | 0, /* tp_reserved */ |
| 1905 | 0, /* tp_repr */ |
| 1906 | 0, /* tp_as_number */ |
| 1907 | 0, /* tp_as_sequence */ |
| 1908 | 0, /* tp_as_mapping */ |
| 1909 | 0, /* tp_hash */ |
| 1910 | 0, /* tp_call */ |
| 1911 | 0, /* tp_str */ |
| 1912 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1913 | 0, /* tp_setattro */ |
| 1914 | 0, /* tp_as_buffer */ |
| 1915 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1916 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1917 | chain_doc, /* tp_doc */ |
| 1918 | (traverseproc)chain_traverse, /* tp_traverse */ |
| 1919 | 0, /* tp_clear */ |
| 1920 | 0, /* tp_richcompare */ |
| 1921 | 0, /* tp_weaklistoffset */ |
| 1922 | PyObject_SelfIter, /* tp_iter */ |
| 1923 | (iternextfunc)chain_next, /* tp_iternext */ |
| 1924 | chain_methods, /* tp_methods */ |
| 1925 | 0, /* tp_members */ |
| 1926 | 0, /* tp_getset */ |
| 1927 | 0, /* tp_base */ |
| 1928 | 0, /* tp_dict */ |
| 1929 | 0, /* tp_descr_get */ |
| 1930 | 0, /* tp_descr_set */ |
| 1931 | 0, /* tp_dictoffset */ |
| 1932 | 0, /* tp_init */ |
| 1933 | 0, /* tp_alloc */ |
| 1934 | chain_new, /* tp_new */ |
| 1935 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1936 | }; |
| 1937 | |
| 1938 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1939 | /* product object ************************************************************/ |
| 1940 | |
| 1941 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1942 | PyObject_HEAD |
| 1943 | PyObject *pools; /* tuple of pool tuples */ |
| 1944 | Py_ssize_t *indices; /* one index per pool */ |
| 1945 | PyObject *result; /* most recently returned result tuple */ |
| 1946 | int stopped; /* set to 1 when the product iterator is exhausted */ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1947 | } productobject; |
| 1948 | |
| 1949 | static PyTypeObject product_type; |
| 1950 | |
| 1951 | static PyObject * |
| 1952 | product_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | productobject *lz; |
| 1955 | Py_ssize_t nargs, npools, repeat=1; |
| 1956 | PyObject *pools = NULL; |
| 1957 | Py_ssize_t *indices = NULL; |
| 1958 | Py_ssize_t i; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | if (kwds != NULL) { |
| 1961 | char *kwlist[] = {"repeat", 0}; |
| 1962 | PyObject *tmpargs = PyTuple_New(0); |
| 1963 | if (tmpargs == NULL) |
| 1964 | return NULL; |
| 1965 | if (!PyArg_ParseTupleAndKeywords(tmpargs, kwds, "|n:product", kwlist, &repeat)) { |
| 1966 | Py_DECREF(tmpargs); |
| 1967 | return NULL; |
| 1968 | } |
| 1969 | Py_DECREF(tmpargs); |
| 1970 | if (repeat < 0) { |
| 1971 | PyErr_SetString(PyExc_ValueError, |
| 1972 | "repeat argument cannot be negative"); |
| 1973 | return NULL; |
| 1974 | } |
| 1975 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1977 | assert(PyTuple_Check(args)); |
| 1978 | nargs = (repeat == 0) ? 0 : PyTuple_GET_SIZE(args); |
| 1979 | npools = nargs * repeat; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | indices = PyMem_Malloc(npools * sizeof(Py_ssize_t)); |
| 1982 | if (indices == NULL) { |
| 1983 | PyErr_NoMemory(); |
| 1984 | goto error; |
| 1985 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | pools = PyTuple_New(npools); |
| 1988 | if (pools == NULL) |
| 1989 | goto error; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 1990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | for (i=0; i < nargs ; ++i) { |
| 1992 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 1993 | PyObject *pool = PySequence_Tuple(item); |
| 1994 | if (pool == NULL) |
| 1995 | goto error; |
| 1996 | PyTuple_SET_ITEM(pools, i, pool); |
| 1997 | indices[i] = 0; |
| 1998 | } |
| 1999 | for ( ; i < npools; ++i) { |
| 2000 | PyObject *pool = PyTuple_GET_ITEM(pools, i - nargs); |
| 2001 | Py_INCREF(pool); |
| 2002 | PyTuple_SET_ITEM(pools, i, pool); |
| 2003 | indices[i] = 0; |
| 2004 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2005 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | /* create productobject structure */ |
| 2007 | lz = (productobject *)type->tp_alloc(type, 0); |
| 2008 | if (lz == NULL) |
| 2009 | goto error; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2011 | lz->pools = pools; |
| 2012 | lz->indices = indices; |
| 2013 | lz->result = NULL; |
| 2014 | lz->stopped = 0; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | return (PyObject *)lz; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2017 | |
| 2018 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | if (indices != NULL) |
| 2020 | PyMem_Free(indices); |
| 2021 | Py_XDECREF(pools); |
| 2022 | return NULL; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | static void |
| 2026 | product_dealloc(productobject *lz) |
| 2027 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | PyObject_GC_UnTrack(lz); |
| 2029 | Py_XDECREF(lz->pools); |
| 2030 | Py_XDECREF(lz->result); |
| 2031 | if (lz->indices != NULL) |
| 2032 | PyMem_Free(lz->indices); |
| 2033 | Py_TYPE(lz)->tp_free(lz); |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | static int |
| 2037 | product_traverse(productobject *lz, visitproc visit, void *arg) |
| 2038 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | Py_VISIT(lz->pools); |
| 2040 | Py_VISIT(lz->result); |
| 2041 | return 0; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | static PyObject * |
| 2045 | product_next(productobject *lz) |
| 2046 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | PyObject *pool; |
| 2048 | PyObject *elem; |
| 2049 | PyObject *oldelem; |
| 2050 | PyObject *pools = lz->pools; |
| 2051 | PyObject *result = lz->result; |
| 2052 | Py_ssize_t npools = PyTuple_GET_SIZE(pools); |
| 2053 | Py_ssize_t i; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | if (lz->stopped) |
| 2056 | return NULL; |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | if (result == NULL) { |
| 2059 | /* On the first pass, return an initial tuple filled with the |
| 2060 | first element from each pool. */ |
| 2061 | result = PyTuple_New(npools); |
| 2062 | if (result == NULL) |
| 2063 | goto empty; |
| 2064 | lz->result = result; |
| 2065 | for (i=0; i < npools; i++) { |
| 2066 | pool = PyTuple_GET_ITEM(pools, i); |
| 2067 | if (PyTuple_GET_SIZE(pool) == 0) |
| 2068 | goto empty; |
| 2069 | elem = PyTuple_GET_ITEM(pool, 0); |
| 2070 | Py_INCREF(elem); |
| 2071 | PyTuple_SET_ITEM(result, i, elem); |
| 2072 | } |
| 2073 | } else { |
| 2074 | Py_ssize_t *indices = lz->indices; |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | /* Copy the previous result tuple or re-use it if available */ |
| 2077 | if (Py_REFCNT(result) > 1) { |
| 2078 | PyObject *old_result = result; |
| 2079 | result = PyTuple_New(npools); |
| 2080 | if (result == NULL) |
| 2081 | goto empty; |
| 2082 | lz->result = result; |
| 2083 | for (i=0; i < npools; i++) { |
| 2084 | elem = PyTuple_GET_ITEM(old_result, i); |
| 2085 | Py_INCREF(elem); |
| 2086 | PyTuple_SET_ITEM(result, i, elem); |
| 2087 | } |
| 2088 | Py_DECREF(old_result); |
| 2089 | } |
| 2090 | /* Now, we've got the only copy so we can update it in-place */ |
| 2091 | assert (npools==0 || Py_REFCNT(result) == 1); |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | /* Update the pool indices right-to-left. Only advance to the |
| 2094 | next pool when the previous one rolls-over */ |
| 2095 | for (i=npools-1 ; i >= 0 ; i--) { |
| 2096 | pool = PyTuple_GET_ITEM(pools, i); |
| 2097 | indices[i]++; |
| 2098 | if (indices[i] == PyTuple_GET_SIZE(pool)) { |
| 2099 | /* Roll-over and advance to next pool */ |
| 2100 | indices[i] = 0; |
| 2101 | elem = PyTuple_GET_ITEM(pool, 0); |
| 2102 | Py_INCREF(elem); |
| 2103 | oldelem = PyTuple_GET_ITEM(result, i); |
| 2104 | PyTuple_SET_ITEM(result, i, elem); |
| 2105 | Py_DECREF(oldelem); |
| 2106 | } else { |
| 2107 | /* No rollover. Just increment and stop here. */ |
| 2108 | elem = PyTuple_GET_ITEM(pool, indices[i]); |
| 2109 | Py_INCREF(elem); |
| 2110 | oldelem = PyTuple_GET_ITEM(result, i); |
| 2111 | PyTuple_SET_ITEM(result, i, elem); |
| 2112 | Py_DECREF(oldelem); |
| 2113 | break; |
| 2114 | } |
| 2115 | } |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | /* If i is negative, then the indices have all rolled-over |
| 2118 | and we're done. */ |
| 2119 | if (i < 0) |
| 2120 | goto empty; |
| 2121 | } |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | Py_INCREF(result); |
| 2124 | return result; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2125 | |
| 2126 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | lz->stopped = 1; |
| 2128 | return NULL; |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2131 | static PyObject * |
| 2132 | product_reduce(productobject *lz) |
| 2133 | { |
| 2134 | if (lz->stopped) { |
| 2135 | return Py_BuildValue("O(())", Py_TYPE(lz)); |
| 2136 | } else if (lz->result == NULL) { |
| 2137 | return Py_BuildValue("OO", Py_TYPE(lz), lz->pools); |
| 2138 | } else { |
| 2139 | PyObject *indices; |
| 2140 | Py_ssize_t n, i; |
| 2141 | |
| 2142 | /* we must pickle the indices use them for setstate, and |
| 2143 | * additionally indicate that the iterator has started |
| 2144 | */ |
| 2145 | n = PyTuple_GET_SIZE(lz->pools); |
| 2146 | indices = PyTuple_New(n); |
| 2147 | if (indices == NULL) |
| 2148 | return NULL; |
| 2149 | for (i=0; i<n; i++){ |
| 2150 | PyObject* index = PyLong_FromSsize_t(lz->indices[i]); |
| 2151 | if (!index) { |
| 2152 | Py_DECREF(indices); |
| 2153 | return NULL; |
| 2154 | } |
| 2155 | PyTuple_SET_ITEM(indices, i, index); |
| 2156 | } |
| 2157 | return Py_BuildValue("OON", Py_TYPE(lz), lz->pools, indices); |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | static PyObject * |
| 2162 | product_setstate(productobject *lz, PyObject *state) |
| 2163 | { |
| 2164 | PyObject *result; |
| 2165 | Py_ssize_t n, i; |
| 2166 | |
| 2167 | n = PyTuple_GET_SIZE(lz->pools); |
| 2168 | if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != n) { |
| 2169 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 2170 | return NULL; |
| 2171 | } |
| 2172 | for (i=0; i<n; i++) |
| 2173 | { |
| 2174 | PyObject* indexObject = PyTuple_GET_ITEM(state, i); |
| 2175 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
| 2176 | if (index < 0 && PyErr_Occurred()) |
| 2177 | return NULL; /* not an integer */ |
| 2178 | /* clamp the index */ |
| 2179 | if (index < 0) |
| 2180 | index = 0; |
| 2181 | else if (index > n-1) |
| 2182 | index = n-1; |
| 2183 | lz->indices[i] = index; |
| 2184 | } |
| 2185 | |
| 2186 | result = PyTuple_New(n); |
| 2187 | if (!result) |
| 2188 | return NULL; |
| 2189 | for (i=0; i<n; i++) { |
| 2190 | PyObject *pool = PyTuple_GET_ITEM(lz->pools, i); |
| 2191 | PyObject *element = PyTuple_GET_ITEM(pool, lz->indices[i]); |
| 2192 | Py_INCREF(element); |
| 2193 | PyTuple_SET_ITEM(result, i, element); |
| 2194 | } |
| 2195 | Py_CLEAR(lz->result); |
| 2196 | lz->result = result; |
| 2197 | Py_RETURN_NONE; |
| 2198 | } |
| 2199 | |
| 2200 | static PyMethodDef product_methods[] = { |
| 2201 | {"__reduce__", (PyCFunction)product_reduce, METH_NOARGS, |
| 2202 | reduce_doc}, |
| 2203 | {"__setstate__", (PyCFunction)product_setstate, METH_O, |
| 2204 | setstate_doc}, |
| 2205 | {NULL, NULL} /* sentinel */ |
| 2206 | }; |
| 2207 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2208 | PyDoc_STRVAR(product_doc, |
| 2209 | "product(*iterables) --> product object\n\ |
| 2210 | \n\ |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 2211 | Cartesian product of input iterables. Equivalent to nested for-loops.\n\n\ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2212 | For example, product(A, B) returns the same as: ((x,y) for x in A for y in B).\n\ |
| 2213 | The leftmost iterators are in the outermost for-loop, so the output tuples\n\ |
| 2214 | cycle in a manner similar to an odometer (with the rightmost element changing\n\ |
| 2215 | on every iteration).\n\n\ |
Benjamin Peterson | a86f2c0 | 2009-02-10 02:41:10 +0000 | [diff] [blame] | 2216 | To compute the product of an iterable with itself, specify the number\n\ |
| 2217 | of repetitions with the optional repeat keyword argument. For example,\n\ |
| 2218 | product(A, repeat=4) means the same as product(A, A, A, A).\n\n\ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2219 | product('ab', range(3)) --> ('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)\n\ |
| 2220 | product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ..."); |
| 2221 | |
| 2222 | static PyTypeObject product_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2224 | "itertools.product", /* tp_name */ |
| 2225 | sizeof(productobject), /* tp_basicsize */ |
| 2226 | 0, /* tp_itemsize */ |
| 2227 | /* methods */ |
| 2228 | (destructor)product_dealloc, /* tp_dealloc */ |
| 2229 | 0, /* tp_print */ |
| 2230 | 0, /* tp_getattr */ |
| 2231 | 0, /* tp_setattr */ |
| 2232 | 0, /* tp_reserved */ |
| 2233 | 0, /* tp_repr */ |
| 2234 | 0, /* tp_as_number */ |
| 2235 | 0, /* tp_as_sequence */ |
| 2236 | 0, /* tp_as_mapping */ |
| 2237 | 0, /* tp_hash */ |
| 2238 | 0, /* tp_call */ |
| 2239 | 0, /* tp_str */ |
| 2240 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2241 | 0, /* tp_setattro */ |
| 2242 | 0, /* tp_as_buffer */ |
| 2243 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2244 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2245 | product_doc, /* tp_doc */ |
| 2246 | (traverseproc)product_traverse, /* tp_traverse */ |
| 2247 | 0, /* tp_clear */ |
| 2248 | 0, /* tp_richcompare */ |
| 2249 | 0, /* tp_weaklistoffset */ |
| 2250 | PyObject_SelfIter, /* tp_iter */ |
| 2251 | (iternextfunc)product_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2252 | product_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | 0, /* tp_members */ |
| 2254 | 0, /* tp_getset */ |
| 2255 | 0, /* tp_base */ |
| 2256 | 0, /* tp_dict */ |
| 2257 | 0, /* tp_descr_get */ |
| 2258 | 0, /* tp_descr_set */ |
| 2259 | 0, /* tp_dictoffset */ |
| 2260 | 0, /* tp_init */ |
| 2261 | 0, /* tp_alloc */ |
| 2262 | product_new, /* tp_new */ |
| 2263 | PyObject_GC_Del, /* tp_free */ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 2264 | }; |
| 2265 | |
| 2266 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2267 | /* combinations object ************************************************************/ |
| 2268 | |
| 2269 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | PyObject_HEAD |
| 2271 | PyObject *pool; /* input converted to a tuple */ |
| 2272 | Py_ssize_t *indices; /* one index per result element */ |
| 2273 | PyObject *result; /* most recently returned result tuple */ |
| 2274 | Py_ssize_t r; /* size of result tuple */ |
| 2275 | int stopped; /* set to 1 when the combinations iterator is exhausted */ |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2276 | } combinationsobject; |
| 2277 | |
| 2278 | static PyTypeObject combinations_type; |
| 2279 | |
| 2280 | static PyObject * |
| 2281 | combinations_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | combinationsobject *co; |
| 2284 | Py_ssize_t n; |
| 2285 | Py_ssize_t r; |
| 2286 | PyObject *pool = NULL; |
| 2287 | PyObject *iterable = NULL; |
| 2288 | Py_ssize_t *indices = NULL; |
| 2289 | Py_ssize_t i; |
| 2290 | static char *kwargs[] = {"iterable", "r", NULL}; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations", kwargs, |
| 2293 | &iterable, &r)) |
| 2294 | return NULL; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | pool = PySequence_Tuple(iterable); |
| 2297 | if (pool == NULL) |
| 2298 | goto error; |
| 2299 | n = PyTuple_GET_SIZE(pool); |
| 2300 | if (r < 0) { |
| 2301 | PyErr_SetString(PyExc_ValueError, "r must be non-negative"); |
| 2302 | goto error; |
| 2303 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | indices = PyMem_Malloc(r * sizeof(Py_ssize_t)); |
| 2306 | if (indices == NULL) { |
| 2307 | PyErr_NoMemory(); |
| 2308 | goto error; |
| 2309 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | for (i=0 ; i<r ; i++) |
| 2312 | indices[i] = i; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | /* create combinationsobject structure */ |
| 2315 | co = (combinationsobject *)type->tp_alloc(type, 0); |
| 2316 | if (co == NULL) |
| 2317 | goto error; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | co->pool = pool; |
| 2320 | co->indices = indices; |
| 2321 | co->result = NULL; |
| 2322 | co->r = r; |
| 2323 | co->stopped = r > n ? 1 : 0; |
| 2324 | |
| 2325 | return (PyObject *)co; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2326 | |
| 2327 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | if (indices != NULL) |
| 2329 | PyMem_Free(indices); |
| 2330 | Py_XDECREF(pool); |
| 2331 | return NULL; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | static void |
| 2335 | combinations_dealloc(combinationsobject *co) |
| 2336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | PyObject_GC_UnTrack(co); |
| 2338 | Py_XDECREF(co->pool); |
| 2339 | Py_XDECREF(co->result); |
| 2340 | if (co->indices != NULL) |
| 2341 | PyMem_Free(co->indices); |
| 2342 | Py_TYPE(co)->tp_free(co); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | static int |
| 2346 | combinations_traverse(combinationsobject *co, visitproc visit, void *arg) |
| 2347 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | Py_VISIT(co->pool); |
| 2349 | Py_VISIT(co->result); |
| 2350 | return 0; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | static PyObject * |
| 2354 | combinations_next(combinationsobject *co) |
| 2355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2356 | PyObject *elem; |
| 2357 | PyObject *oldelem; |
| 2358 | PyObject *pool = co->pool; |
| 2359 | Py_ssize_t *indices = co->indices; |
| 2360 | PyObject *result = co->result; |
| 2361 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
| 2362 | Py_ssize_t r = co->r; |
| 2363 | Py_ssize_t i, j, index; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | if (co->stopped) |
| 2366 | return NULL; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | if (result == NULL) { |
| 2369 | /* On the first pass, initialize result tuple using the indices */ |
| 2370 | result = PyTuple_New(r); |
| 2371 | if (result == NULL) |
| 2372 | goto empty; |
| 2373 | co->result = result; |
| 2374 | for (i=0; i<r ; i++) { |
| 2375 | index = indices[i]; |
| 2376 | elem = PyTuple_GET_ITEM(pool, index); |
| 2377 | Py_INCREF(elem); |
| 2378 | PyTuple_SET_ITEM(result, i, elem); |
| 2379 | } |
| 2380 | } else { |
| 2381 | /* Copy the previous result tuple or re-use it if available */ |
| 2382 | if (Py_REFCNT(result) > 1) { |
| 2383 | PyObject *old_result = result; |
| 2384 | result = PyTuple_New(r); |
| 2385 | if (result == NULL) |
| 2386 | goto empty; |
| 2387 | co->result = result; |
| 2388 | for (i=0; i<r ; i++) { |
| 2389 | elem = PyTuple_GET_ITEM(old_result, i); |
| 2390 | Py_INCREF(elem); |
| 2391 | PyTuple_SET_ITEM(result, i, elem); |
| 2392 | } |
| 2393 | Py_DECREF(old_result); |
| 2394 | } |
| 2395 | /* Now, we've got the only copy so we can update it in-place |
| 2396 | * CPython's empty tuple is a singleton and cached in |
| 2397 | * PyTuple's freelist. |
| 2398 | */ |
| 2399 | assert(r == 0 || Py_REFCNT(result) == 1); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | /* Scan indices right-to-left until finding one that is not |
| 2402 | at its maximum (i + n - r). */ |
| 2403 | for (i=r-1 ; i >= 0 && indices[i] == i+n-r ; i--) |
| 2404 | ; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | /* If i is negative, then the indices are all at |
| 2407 | their maximum value and we're done. */ |
| 2408 | if (i < 0) |
| 2409 | goto empty; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | /* Increment the current index which we know is not at its |
| 2412 | maximum. Then move back to the right setting each index |
| 2413 | to its lowest possible value (one higher than the index |
| 2414 | to its left -- this maintains the sort order invariant). */ |
| 2415 | indices[i]++; |
| 2416 | for (j=i+1 ; j<r ; j++) |
| 2417 | indices[j] = indices[j-1] + 1; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | /* Update the result tuple for the new indices |
| 2420 | starting with i, the leftmost index that changed */ |
| 2421 | for ( ; i<r ; i++) { |
| 2422 | index = indices[i]; |
| 2423 | elem = PyTuple_GET_ITEM(pool, index); |
| 2424 | Py_INCREF(elem); |
| 2425 | oldelem = PyTuple_GET_ITEM(result, i); |
| 2426 | PyTuple_SET_ITEM(result, i, elem); |
| 2427 | Py_DECREF(oldelem); |
| 2428 | } |
| 2429 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2431 | Py_INCREF(result); |
| 2432 | return result; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2433 | |
| 2434 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | co->stopped = 1; |
| 2436 | return NULL; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2439 | static PyObject * |
| 2440 | combinations_reduce(combinationsobject *lz) |
| 2441 | { |
| 2442 | if (lz->result == NULL) { |
| 2443 | return Py_BuildValue("O(On)", Py_TYPE(lz), lz->pool, lz->r); |
| 2444 | } else if (lz->stopped) { |
| 2445 | return Py_BuildValue("O(()n)", Py_TYPE(lz), lz->r); |
| 2446 | } else { |
| 2447 | PyObject *indices; |
| 2448 | Py_ssize_t i; |
| 2449 | |
| 2450 | /* we must pickle the indices and use them for setstate */ |
| 2451 | indices = PyTuple_New(lz->r); |
| 2452 | if (!indices) |
| 2453 | return NULL; |
| 2454 | for (i=0; i<lz->r; i++) |
| 2455 | { |
| 2456 | PyObject* index = PyLong_FromSsize_t(lz->indices[i]); |
| 2457 | if (!index) { |
| 2458 | Py_DECREF(indices); |
| 2459 | return NULL; |
| 2460 | } |
| 2461 | PyTuple_SET_ITEM(indices, i, index); |
| 2462 | } |
| 2463 | |
| 2464 | return Py_BuildValue("O(On)N", Py_TYPE(lz), lz->pool, lz->r, indices); |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | static PyObject * |
| 2469 | combinations_setstate(combinationsobject *lz, PyObject *state) |
| 2470 | { |
| 2471 | PyObject *result; |
| 2472 | Py_ssize_t i; |
| 2473 | Py_ssize_t n = PyTuple_GET_SIZE(lz->pool); |
| 2474 | |
| 2475 | if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != lz->r) |
| 2476 | { |
| 2477 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 2478 | return NULL; |
| 2479 | } |
| 2480 | |
| 2481 | for (i=0; i<lz->r; i++) |
| 2482 | { |
| 2483 | Py_ssize_t max; |
| 2484 | PyObject* indexObject = PyTuple_GET_ITEM(state, i); |
| 2485 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
| 2486 | if (index == -1 && PyErr_Occurred()) |
| 2487 | return NULL; /* not an integer */ |
| 2488 | max = i + n - lz->r; |
| 2489 | /* clamp the index (beware of negative max) */ |
| 2490 | if (index > max) |
| 2491 | index = max; |
| 2492 | if (index < 0) |
| 2493 | index = 0; |
| 2494 | lz->indices[i] = index; |
| 2495 | } |
| 2496 | |
| 2497 | result = PyTuple_New(lz->r); |
| 2498 | if (result == NULL) |
| 2499 | return NULL; |
| 2500 | for (i=0; i<lz->r; i++) { |
| 2501 | PyObject *element = PyTuple_GET_ITEM(lz->pool, lz->indices[i]); |
| 2502 | Py_INCREF(element); |
| 2503 | PyTuple_SET_ITEM(result, i, element); |
| 2504 | } |
| 2505 | |
| 2506 | Py_CLEAR(lz->result); |
| 2507 | lz->result = result; |
| 2508 | Py_RETURN_NONE; |
| 2509 | } |
| 2510 | |
| 2511 | static PyMethodDef combinations_methods[] = { |
| 2512 | {"__reduce__", (PyCFunction)combinations_reduce, METH_NOARGS, |
| 2513 | reduce_doc}, |
| 2514 | {"__setstate__", (PyCFunction)combinations_setstate, METH_O, |
| 2515 | setstate_doc}, |
| 2516 | {NULL, NULL} /* sentinel */ |
| 2517 | }; |
| 2518 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2519 | PyDoc_STRVAR(combinations_doc, |
Raymond Hettinger | 36c3c02 | 2009-11-19 01:20:07 +0000 | [diff] [blame] | 2520 | "combinations(iterable, r) --> combinations object\n\ |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2521 | \n\ |
| 2522 | Return successive r-length combinations of elements in the iterable.\n\n\ |
| 2523 | combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)"); |
| 2524 | |
| 2525 | static PyTypeObject combinations_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2527 | "itertools.combinations", /* tp_name */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | sizeof(combinationsobject), /* tp_basicsize */ |
| 2529 | 0, /* tp_itemsize */ |
| 2530 | /* methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2531 | (destructor)combinations_dealloc, /* tp_dealloc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2532 | 0, /* tp_print */ |
| 2533 | 0, /* tp_getattr */ |
| 2534 | 0, /* tp_setattr */ |
| 2535 | 0, /* tp_reserved */ |
| 2536 | 0, /* tp_repr */ |
| 2537 | 0, /* tp_as_number */ |
| 2538 | 0, /* tp_as_sequence */ |
| 2539 | 0, /* tp_as_mapping */ |
| 2540 | 0, /* tp_hash */ |
| 2541 | 0, /* tp_call */ |
| 2542 | 0, /* tp_str */ |
| 2543 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2544 | 0, /* tp_setattro */ |
| 2545 | 0, /* tp_as_buffer */ |
| 2546 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2547 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2548 | combinations_doc, /* tp_doc */ |
| 2549 | (traverseproc)combinations_traverse,/* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | 0, /* tp_clear */ |
| 2551 | 0, /* tp_richcompare */ |
| 2552 | 0, /* tp_weaklistoffset */ |
| 2553 | PyObject_SelfIter, /* tp_iter */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2554 | (iternextfunc)combinations_next, /* tp_iternext */ |
| 2555 | combinations_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2556 | 0, /* tp_members */ |
| 2557 | 0, /* tp_getset */ |
| 2558 | 0, /* tp_base */ |
| 2559 | 0, /* tp_dict */ |
| 2560 | 0, /* tp_descr_get */ |
| 2561 | 0, /* tp_descr_set */ |
| 2562 | 0, /* tp_dictoffset */ |
| 2563 | 0, /* tp_init */ |
| 2564 | 0, /* tp_alloc */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2565 | combinations_new, /* tp_new */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | PyObject_GC_Del, /* tp_free */ |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2567 | }; |
| 2568 | |
| 2569 | |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2570 | /* combinations with replacement object *******************************************/ |
| 2571 | |
| 2572 | /* Equivalent to: |
| 2573 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | def combinations_with_replacement(iterable, r): |
| 2575 | "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC" |
| 2576 | # number items returned: (n+r-1)! / r! / (n-1)! |
| 2577 | pool = tuple(iterable) |
| 2578 | n = len(pool) |
| 2579 | indices = [0] * r |
| 2580 | yield tuple(pool[i] for i in indices) |
| 2581 | while 1: |
| 2582 | for i in reversed(range(r)): |
| 2583 | if indices[i] != n - 1: |
| 2584 | break |
| 2585 | else: |
| 2586 | return |
| 2587 | indices[i:] = [indices[i] + 1] * (r - i) |
| 2588 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | def combinations_with_replacement2(iterable, r): |
| 2591 | 'Alternate version that filters from product()' |
| 2592 | pool = tuple(iterable) |
| 2593 | n = len(pool) |
| 2594 | for indices in product(range(n), repeat=r): |
| 2595 | if sorted(indices) == list(indices): |
| 2596 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2597 | */ |
| 2598 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2599 | PyObject_HEAD |
| 2600 | PyObject *pool; /* input converted to a tuple */ |
| 2601 | Py_ssize_t *indices; /* one index per result element */ |
| 2602 | PyObject *result; /* most recently returned result tuple */ |
| 2603 | Py_ssize_t r; /* size of result tuple */ |
| 2604 | int stopped; /* set to 1 when the cwr iterator is exhausted */ |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2605 | } cwrobject; |
| 2606 | |
| 2607 | static PyTypeObject cwr_type; |
| 2608 | |
| 2609 | static PyObject * |
| 2610 | cwr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | cwrobject *co; |
| 2613 | Py_ssize_t n; |
| 2614 | Py_ssize_t r; |
| 2615 | PyObject *pool = NULL; |
| 2616 | PyObject *iterable = NULL; |
| 2617 | Py_ssize_t *indices = NULL; |
| 2618 | Py_ssize_t i; |
| 2619 | static char *kwargs[] = {"iterable", "r", NULL}; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "On:combinations_with_replacement", kwargs, |
| 2622 | &iterable, &r)) |
| 2623 | return NULL; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | pool = PySequence_Tuple(iterable); |
| 2626 | if (pool == NULL) |
| 2627 | goto error; |
| 2628 | n = PyTuple_GET_SIZE(pool); |
| 2629 | if (r < 0) { |
| 2630 | PyErr_SetString(PyExc_ValueError, "r must be non-negative"); |
| 2631 | goto error; |
| 2632 | } |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | indices = PyMem_Malloc(r * sizeof(Py_ssize_t)); |
| 2635 | if (indices == NULL) { |
| 2636 | PyErr_NoMemory(); |
| 2637 | goto error; |
| 2638 | } |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | for (i=0 ; i<r ; i++) |
| 2641 | indices[i] = 0; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | /* create cwrobject structure */ |
| 2644 | co = (cwrobject *)type->tp_alloc(type, 0); |
| 2645 | if (co == NULL) |
| 2646 | goto error; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2648 | co->pool = pool; |
| 2649 | co->indices = indices; |
| 2650 | co->result = NULL; |
| 2651 | co->r = r; |
| 2652 | co->stopped = !n && r; |
| 2653 | |
| 2654 | return (PyObject *)co; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2655 | |
| 2656 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | if (indices != NULL) |
| 2658 | PyMem_Free(indices); |
| 2659 | Py_XDECREF(pool); |
| 2660 | return NULL; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | static void |
| 2664 | cwr_dealloc(cwrobject *co) |
| 2665 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | PyObject_GC_UnTrack(co); |
| 2667 | Py_XDECREF(co->pool); |
| 2668 | Py_XDECREF(co->result); |
| 2669 | if (co->indices != NULL) |
| 2670 | PyMem_Free(co->indices); |
| 2671 | Py_TYPE(co)->tp_free(co); |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | static int |
| 2675 | cwr_traverse(cwrobject *co, visitproc visit, void *arg) |
| 2676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | Py_VISIT(co->pool); |
| 2678 | Py_VISIT(co->result); |
| 2679 | return 0; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | static PyObject * |
| 2683 | cwr_next(cwrobject *co) |
| 2684 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2685 | PyObject *elem; |
| 2686 | PyObject *oldelem; |
| 2687 | PyObject *pool = co->pool; |
| 2688 | Py_ssize_t *indices = co->indices; |
| 2689 | PyObject *result = co->result; |
| 2690 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
| 2691 | Py_ssize_t r = co->r; |
| 2692 | Py_ssize_t i, j, index; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | if (co->stopped) |
| 2695 | return NULL; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | if (result == NULL) { |
| 2698 | /* On the first pass, initialize result tuple using the indices */ |
| 2699 | result = PyTuple_New(r); |
| 2700 | if (result == NULL) |
| 2701 | goto empty; |
| 2702 | co->result = result; |
| 2703 | for (i=0; i<r ; i++) { |
| 2704 | index = indices[i]; |
| 2705 | elem = PyTuple_GET_ITEM(pool, index); |
| 2706 | Py_INCREF(elem); |
| 2707 | PyTuple_SET_ITEM(result, i, elem); |
| 2708 | } |
| 2709 | } else { |
| 2710 | /* Copy the previous result tuple or re-use it if available */ |
| 2711 | if (Py_REFCNT(result) > 1) { |
| 2712 | PyObject *old_result = result; |
| 2713 | result = PyTuple_New(r); |
| 2714 | if (result == NULL) |
| 2715 | goto empty; |
| 2716 | co->result = result; |
| 2717 | for (i=0; i<r ; i++) { |
| 2718 | elem = PyTuple_GET_ITEM(old_result, i); |
| 2719 | Py_INCREF(elem); |
| 2720 | PyTuple_SET_ITEM(result, i, elem); |
| 2721 | } |
| 2722 | Py_DECREF(old_result); |
| 2723 | } |
| 2724 | /* Now, we've got the only copy so we can update it in-place CPython's |
| 2725 | empty tuple is a singleton and cached in PyTuple's freelist. */ |
| 2726 | assert(r == 0 || Py_REFCNT(result) == 1); |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2727 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | /* Scan indices right-to-left until finding one that is not |
| 2729 | * at its maximum (n-1). */ |
| 2730 | for (i=r-1 ; i >= 0 && indices[i] == n-1; i--) |
| 2731 | ; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | /* If i is negative, then the indices are all at |
| 2734 | their maximum value and we're done. */ |
| 2735 | if (i < 0) |
| 2736 | goto empty; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | /* Increment the current index which we know is not at its |
| 2739 | maximum. Then set all to the right to the same value. */ |
| 2740 | indices[i]++; |
| 2741 | for (j=i+1 ; j<r ; j++) |
| 2742 | indices[j] = indices[j-1]; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | /* Update the result tuple for the new indices |
| 2745 | starting with i, the leftmost index that changed */ |
| 2746 | for ( ; i<r ; i++) { |
| 2747 | index = indices[i]; |
| 2748 | elem = PyTuple_GET_ITEM(pool, index); |
| 2749 | Py_INCREF(elem); |
| 2750 | oldelem = PyTuple_GET_ITEM(result, i); |
| 2751 | PyTuple_SET_ITEM(result, i, elem); |
| 2752 | Py_DECREF(oldelem); |
| 2753 | } |
| 2754 | } |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2756 | Py_INCREF(result); |
| 2757 | return result; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2758 | |
| 2759 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | co->stopped = 1; |
| 2761 | return NULL; |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2764 | static PyObject * |
| 2765 | cwr_reduce(cwrobject *lz) |
| 2766 | { |
| 2767 | if (lz->result == NULL) { |
| 2768 | return Py_BuildValue("O(On)", Py_TYPE(lz), lz->pool, lz->r); |
| 2769 | } else if (lz->stopped) { |
| 2770 | return Py_BuildValue("O(()n)", Py_TYPE(lz), lz->r); |
| 2771 | } else { |
| 2772 | PyObject *indices; |
| 2773 | Py_ssize_t i; |
| 2774 | |
| 2775 | /* we must pickle the indices and use them for setstate */ |
| 2776 | indices = PyTuple_New(lz->r); |
| 2777 | if (!indices) |
| 2778 | return NULL; |
| 2779 | for (i=0; i<lz->r; i++) |
| 2780 | { |
| 2781 | PyObject* index = PyLong_FromSsize_t(lz->indices[i]); |
| 2782 | if (!index) { |
| 2783 | Py_DECREF(indices); |
| 2784 | return NULL; |
| 2785 | } |
| 2786 | PyTuple_SET_ITEM(indices, i, index); |
| 2787 | } |
| 2788 | |
| 2789 | return Py_BuildValue("O(On)N", Py_TYPE(lz), lz->pool, lz->r, indices); |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | static PyObject * |
| 2794 | cwr_setstate(cwrobject *lz, PyObject *state) |
| 2795 | { |
| 2796 | PyObject *result; |
| 2797 | Py_ssize_t n, i; |
| 2798 | |
| 2799 | if (!PyTuple_Check(state) || PyTuple_GET_SIZE(state) != lz->r) |
| 2800 | { |
| 2801 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 2802 | return NULL; |
| 2803 | } |
| 2804 | |
| 2805 | n = PyTuple_GET_SIZE(lz->pool); |
| 2806 | for (i=0; i<lz->r; i++) |
| 2807 | { |
| 2808 | PyObject* indexObject = PyTuple_GET_ITEM(state, i); |
| 2809 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
| 2810 | if (index < 0 && PyErr_Occurred()) |
| 2811 | return NULL; /* not an integer */ |
| 2812 | /* clamp the index */ |
| 2813 | if (index < 0) |
| 2814 | index = 0; |
| 2815 | else if (index > n-1) |
| 2816 | index = n-1; |
| 2817 | lz->indices[i] = index; |
| 2818 | } |
| 2819 | result = PyTuple_New(lz->r); |
| 2820 | if (result == NULL) |
| 2821 | return NULL; |
| 2822 | for (i=0; i<lz->r; i++) { |
| 2823 | PyObject *element = PyTuple_GET_ITEM(lz->pool, lz->indices[i]); |
| 2824 | Py_INCREF(element); |
| 2825 | PyTuple_SET_ITEM(result, i, element); |
| 2826 | } |
| 2827 | Py_CLEAR(lz->result); |
| 2828 | lz->result = result; |
| 2829 | Py_RETURN_NONE; |
| 2830 | } |
| 2831 | |
| 2832 | static PyMethodDef cwr_methods[] = { |
| 2833 | {"__reduce__", (PyCFunction)cwr_reduce, METH_NOARGS, |
| 2834 | reduce_doc}, |
| 2835 | {"__setstate__", (PyCFunction)cwr_setstate, METH_O, |
| 2836 | setstate_doc}, |
| 2837 | {NULL, NULL} /* sentinel */ |
| 2838 | }; |
| 2839 | |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2840 | PyDoc_STRVAR(cwr_doc, |
Raymond Hettinger | 36c3c02 | 2009-11-19 01:20:07 +0000 | [diff] [blame] | 2841 | "combinations_with_replacement(iterable, r) --> combinations_with_replacement object\n\ |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2842 | \n\ |
| 2843 | Return successive r-length combinations of elements in the iterable\n\ |
| 2844 | allowing individual elements to have successive repeats.\n\ |
| 2845 | combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"); |
| 2846 | |
| 2847 | static PyTypeObject cwr_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | PyVarObject_HEAD_INIT(NULL, 0) |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2849 | "itertools.combinations_with_replacement", /* tp_name */ |
| 2850 | sizeof(cwrobject), /* tp_basicsize */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | 0, /* tp_itemsize */ |
| 2852 | /* methods */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2853 | (destructor)cwr_dealloc, /* tp_dealloc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | 0, /* tp_print */ |
| 2855 | 0, /* tp_getattr */ |
| 2856 | 0, /* tp_setattr */ |
| 2857 | 0, /* tp_reserved */ |
| 2858 | 0, /* tp_repr */ |
| 2859 | 0, /* tp_as_number */ |
| 2860 | 0, /* tp_as_sequence */ |
| 2861 | 0, /* tp_as_mapping */ |
| 2862 | 0, /* tp_hash */ |
| 2863 | 0, /* tp_call */ |
| 2864 | 0, /* tp_str */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2865 | PyObject_GenericGetAttr, /* tp_getattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | 0, /* tp_setattro */ |
| 2867 | 0, /* tp_as_buffer */ |
| 2868 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2869 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2870 | cwr_doc, /* tp_doc */ |
| 2871 | (traverseproc)cwr_traverse, /* tp_traverse */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | 0, /* tp_clear */ |
| 2873 | 0, /* tp_richcompare */ |
| 2874 | 0, /* tp_weaklistoffset */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2875 | PyObject_SelfIter, /* tp_iter */ |
| 2876 | (iternextfunc)cwr_next, /* tp_iternext */ |
| 2877 | cwr_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | 0, /* tp_members */ |
| 2879 | 0, /* tp_getset */ |
| 2880 | 0, /* tp_base */ |
| 2881 | 0, /* tp_dict */ |
| 2882 | 0, /* tp_descr_get */ |
| 2883 | 0, /* tp_descr_set */ |
| 2884 | 0, /* tp_dictoffset */ |
| 2885 | 0, /* tp_init */ |
| 2886 | 0, /* tp_alloc */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2887 | cwr_new, /* tp_new */ |
| 2888 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | d07d939 | 2009-01-27 04:20:44 +0000 | [diff] [blame] | 2889 | }; |
| 2890 | |
| 2891 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2892 | /* permutations object ************************************************************ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2894 | def permutations(iterable, r=None): |
| 2895 | 'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)' |
| 2896 | pool = tuple(iterable) |
| 2897 | n = len(pool) |
| 2898 | r = n if r is None else r |
| 2899 | indices = range(n) |
| 2900 | cycles = range(n-r+1, n+1)[::-1] |
| 2901 | yield tuple(pool[i] for i in indices[:r]) |
| 2902 | while n: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 | for i in reversed(range(r)): |
| 2904 | cycles[i] -= 1 |
| 2905 | if cycles[i] == 0: |
| 2906 | indices[i:] = indices[i+1:] + indices[i:i+1] |
| 2907 | cycles[i] = n - i |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2908 | else: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | j = cycles[i] |
| 2910 | indices[i], indices[-j] = indices[-j], indices[i] |
| 2911 | yield tuple(pool[i] for i in indices[:r]) |
| 2912 | break |
| 2913 | else: |
| 2914 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2915 | */ |
| 2916 | |
| 2917 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2918 | PyObject_HEAD |
| 2919 | PyObject *pool; /* input converted to a tuple */ |
| 2920 | Py_ssize_t *indices; /* one index per element in the pool */ |
| 2921 | Py_ssize_t *cycles; /* one rollover counter per element in the result */ |
| 2922 | PyObject *result; /* most recently returned result tuple */ |
| 2923 | Py_ssize_t r; /* size of result tuple */ |
| 2924 | int stopped; /* set to 1 when the permutations iterator is exhausted */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2925 | } permutationsobject; |
| 2926 | |
| 2927 | static PyTypeObject permutations_type; |
| 2928 | |
| 2929 | static PyObject * |
| 2930 | permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2931 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | permutationsobject *po; |
| 2933 | Py_ssize_t n; |
| 2934 | Py_ssize_t r; |
| 2935 | PyObject *robj = Py_None; |
| 2936 | PyObject *pool = NULL; |
| 2937 | PyObject *iterable = NULL; |
| 2938 | Py_ssize_t *indices = NULL; |
| 2939 | Py_ssize_t *cycles = NULL; |
| 2940 | Py_ssize_t i; |
| 2941 | static char *kwargs[] = {"iterable", "r", NULL}; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:permutations", kwargs, |
| 2944 | &iterable, &robj)) |
| 2945 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | pool = PySequence_Tuple(iterable); |
| 2948 | if (pool == NULL) |
| 2949 | goto error; |
| 2950 | n = PyTuple_GET_SIZE(pool); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | r = n; |
| 2953 | if (robj != Py_None) { |
| 2954 | if (!PyLong_Check(robj)) { |
| 2955 | PyErr_SetString(PyExc_TypeError, "Expected int as r"); |
| 2956 | goto error; |
| 2957 | } |
| 2958 | r = PyLong_AsSsize_t(robj); |
| 2959 | if (r == -1 && PyErr_Occurred()) |
| 2960 | goto error; |
| 2961 | } |
| 2962 | if (r < 0) { |
| 2963 | PyErr_SetString(PyExc_ValueError, "r must be non-negative"); |
| 2964 | goto error; |
| 2965 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | indices = PyMem_Malloc(n * sizeof(Py_ssize_t)); |
| 2968 | cycles = PyMem_Malloc(r * sizeof(Py_ssize_t)); |
| 2969 | if (indices == NULL || cycles == NULL) { |
| 2970 | PyErr_NoMemory(); |
| 2971 | goto error; |
| 2972 | } |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | for (i=0 ; i<n ; i++) |
| 2975 | indices[i] = i; |
| 2976 | for (i=0 ; i<r ; i++) |
| 2977 | cycles[i] = n - i; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | /* create permutationsobject structure */ |
| 2980 | po = (permutationsobject *)type->tp_alloc(type, 0); |
| 2981 | if (po == NULL) |
| 2982 | goto error; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | po->pool = pool; |
| 2985 | po->indices = indices; |
| 2986 | po->cycles = cycles; |
| 2987 | po->result = NULL; |
| 2988 | po->r = r; |
| 2989 | po->stopped = r > n ? 1 : 0; |
| 2990 | |
| 2991 | return (PyObject *)po; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2992 | |
| 2993 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | if (indices != NULL) |
| 2995 | PyMem_Free(indices); |
| 2996 | if (cycles != NULL) |
| 2997 | PyMem_Free(cycles); |
| 2998 | Py_XDECREF(pool); |
| 2999 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | static void |
| 3003 | permutations_dealloc(permutationsobject *po) |
| 3004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | PyObject_GC_UnTrack(po); |
| 3006 | Py_XDECREF(po->pool); |
| 3007 | Py_XDECREF(po->result); |
| 3008 | PyMem_Free(po->indices); |
| 3009 | PyMem_Free(po->cycles); |
| 3010 | Py_TYPE(po)->tp_free(po); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | static int |
| 3014 | permutations_traverse(permutationsobject *po, visitproc visit, void *arg) |
| 3015 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | Py_VISIT(po->pool); |
| 3017 | Py_VISIT(po->result); |
| 3018 | return 0; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| 3021 | static PyObject * |
| 3022 | permutations_next(permutationsobject *po) |
| 3023 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | PyObject *elem; |
| 3025 | PyObject *oldelem; |
| 3026 | PyObject *pool = po->pool; |
| 3027 | Py_ssize_t *indices = po->indices; |
| 3028 | Py_ssize_t *cycles = po->cycles; |
| 3029 | PyObject *result = po->result; |
| 3030 | Py_ssize_t n = PyTuple_GET_SIZE(pool); |
| 3031 | Py_ssize_t r = po->r; |
| 3032 | Py_ssize_t i, j, k, index; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3034 | if (po->stopped) |
| 3035 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | if (result == NULL) { |
| 3038 | /* On the first pass, initialize result tuple using the indices */ |
| 3039 | result = PyTuple_New(r); |
| 3040 | if (result == NULL) |
| 3041 | goto empty; |
| 3042 | po->result = result; |
| 3043 | for (i=0; i<r ; i++) { |
| 3044 | index = indices[i]; |
| 3045 | elem = PyTuple_GET_ITEM(pool, index); |
| 3046 | Py_INCREF(elem); |
| 3047 | PyTuple_SET_ITEM(result, i, elem); |
| 3048 | } |
| 3049 | } else { |
| 3050 | if (n == 0) |
| 3051 | goto empty; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3053 | /* Copy the previous result tuple or re-use it if available */ |
| 3054 | if (Py_REFCNT(result) > 1) { |
| 3055 | PyObject *old_result = result; |
| 3056 | result = PyTuple_New(r); |
| 3057 | if (result == NULL) |
| 3058 | goto empty; |
| 3059 | po->result = result; |
| 3060 | for (i=0; i<r ; i++) { |
| 3061 | elem = PyTuple_GET_ITEM(old_result, i); |
| 3062 | Py_INCREF(elem); |
| 3063 | PyTuple_SET_ITEM(result, i, elem); |
| 3064 | } |
| 3065 | Py_DECREF(old_result); |
| 3066 | } |
| 3067 | /* Now, we've got the only copy so we can update it in-place */ |
| 3068 | assert(r == 0 || Py_REFCNT(result) == 1); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | /* Decrement rightmost cycle, moving leftward upon zero rollover */ |
| 3071 | for (i=r-1 ; i>=0 ; i--) { |
| 3072 | cycles[i] -= 1; |
| 3073 | if (cycles[i] == 0) { |
| 3074 | /* rotatation: indices[i:] = indices[i+1:] + indices[i:i+1] */ |
| 3075 | index = indices[i]; |
| 3076 | for (j=i ; j<n-1 ; j++) |
| 3077 | indices[j] = indices[j+1]; |
| 3078 | indices[n-1] = index; |
| 3079 | cycles[i] = n - i; |
| 3080 | } else { |
| 3081 | j = cycles[i]; |
| 3082 | index = indices[i]; |
| 3083 | indices[i] = indices[n-j]; |
| 3084 | indices[n-j] = index; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | for (k=i; k<r ; k++) { |
| 3087 | /* start with i, the leftmost element that changed */ |
| 3088 | /* yield tuple(pool[k] for k in indices[:r]) */ |
| 3089 | index = indices[k]; |
| 3090 | elem = PyTuple_GET_ITEM(pool, index); |
| 3091 | Py_INCREF(elem); |
| 3092 | oldelem = PyTuple_GET_ITEM(result, k); |
| 3093 | PyTuple_SET_ITEM(result, k, elem); |
| 3094 | Py_DECREF(oldelem); |
| 3095 | } |
| 3096 | break; |
| 3097 | } |
| 3098 | } |
| 3099 | /* If i is negative, then the cycles have all |
| 3100 | rolled-over and we're done. */ |
| 3101 | if (i < 0) |
| 3102 | goto empty; |
| 3103 | } |
| 3104 | Py_INCREF(result); |
| 3105 | return result; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3106 | |
| 3107 | empty: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | po->stopped = 1; |
| 3109 | return NULL; |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3112 | static PyObject * |
| 3113 | permutations_reduce(permutationsobject *po) |
| 3114 | { |
| 3115 | if (po->result == NULL) { |
| 3116 | return Py_BuildValue("O(On)", Py_TYPE(po), po->pool, po->r); |
| 3117 | } else if (po->stopped) { |
| 3118 | return Py_BuildValue("O(()n)", Py_TYPE(po), po->r); |
| 3119 | } else { |
| 3120 | PyObject *indices=NULL, *cycles=NULL; |
| 3121 | Py_ssize_t n, i; |
| 3122 | |
| 3123 | /* we must pickle the indices and cycles and use them for setstate */ |
| 3124 | n = PyTuple_GET_SIZE(po->pool); |
| 3125 | indices = PyTuple_New(n); |
| 3126 | if (indices == NULL) |
| 3127 | goto err; |
| 3128 | for (i=0; i<n; i++){ |
| 3129 | PyObject* index = PyLong_FromSsize_t(po->indices[i]); |
| 3130 | if (!index) |
| 3131 | goto err; |
| 3132 | PyTuple_SET_ITEM(indices, i, index); |
| 3133 | } |
| 3134 | |
| 3135 | cycles = PyTuple_New(po->r); |
| 3136 | if (cycles == NULL) |
| 3137 | goto err; |
| 3138 | for (i=0; i<po->r; i++) |
| 3139 | { |
| 3140 | PyObject* index = PyLong_FromSsize_t(po->cycles[i]); |
| 3141 | if (!index) |
| 3142 | goto err; |
| 3143 | PyTuple_SET_ITEM(cycles, i, index); |
| 3144 | } |
| 3145 | return Py_BuildValue("O(On)(NN)", Py_TYPE(po), |
| 3146 | po->pool, po->r, |
| 3147 | indices, cycles); |
| 3148 | err: |
| 3149 | Py_XDECREF(indices); |
| 3150 | Py_XDECREF(cycles); |
| 3151 | return NULL; |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | static PyObject * |
| 3156 | permutations_setstate(permutationsobject *po, PyObject *state) |
| 3157 | { |
| 3158 | PyObject *indices, *cycles, *result; |
| 3159 | Py_ssize_t n, i; |
| 3160 | |
| 3161 | if (!PyArg_ParseTuple(state, "O!O!", |
| 3162 | &PyTuple_Type, &indices, |
| 3163 | &PyTuple_Type, &cycles)) |
| 3164 | return NULL; |
| 3165 | |
| 3166 | n = PyTuple_GET_SIZE(po->pool); |
| 3167 | if (PyTuple_GET_SIZE(indices) != n || |
| 3168 | PyTuple_GET_SIZE(cycles) != po->r) |
| 3169 | { |
| 3170 | PyErr_SetString(PyExc_ValueError, "invalid arguments"); |
| 3171 | return NULL; |
| 3172 | } |
| 3173 | |
| 3174 | for (i=0; i<n; i++) |
| 3175 | { |
| 3176 | PyObject* indexObject = PyTuple_GET_ITEM(indices, i); |
| 3177 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
| 3178 | if (index < 0 && PyErr_Occurred()) |
| 3179 | return NULL; /* not an integer */ |
| 3180 | /* clamp the index */ |
| 3181 | if (index < 0) |
| 3182 | index = 0; |
| 3183 | else if (index > n-1) |
| 3184 | index = n-1; |
| 3185 | po->indices[i] = index; |
| 3186 | } |
| 3187 | |
| 3188 | for (i=0; i<po->r; i++) |
| 3189 | { |
| 3190 | PyObject* indexObject = PyTuple_GET_ITEM(cycles, i); |
| 3191 | Py_ssize_t index = PyLong_AsSsize_t(indexObject); |
| 3192 | if (index < 0 && PyErr_Occurred()) |
| 3193 | return NULL; /* not an integer */ |
| 3194 | if (index < 1) |
| 3195 | index = 1; |
| 3196 | else if (index > n-i) |
| 3197 | index = n-i; |
| 3198 | po->cycles[i] = index; |
| 3199 | } |
| 3200 | result = PyTuple_New(po->r); |
| 3201 | if (result == NULL) |
| 3202 | return NULL; |
| 3203 | for (i=0; i<po->r; i++) { |
| 3204 | PyObject *element = PyTuple_GET_ITEM(po->pool, po->indices[i]); |
| 3205 | Py_INCREF(element); |
| 3206 | PyTuple_SET_ITEM(result, i, element); |
| 3207 | } |
| 3208 | Py_CLEAR(po->result); |
| 3209 | po->result = result; |
| 3210 | Py_RETURN_NONE; |
| 3211 | } |
| 3212 | |
| 3213 | static PyMethodDef permuations_methods[] = { |
| 3214 | {"__reduce__", (PyCFunction)permutations_reduce, METH_NOARGS, |
| 3215 | reduce_doc}, |
| 3216 | {"__setstate__", (PyCFunction)permutations_setstate, METH_O, |
| 3217 | setstate_doc}, |
| 3218 | {NULL, NULL} /* sentinel */ |
| 3219 | }; |
| 3220 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3221 | PyDoc_STRVAR(permutations_doc, |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 3222 | "permutations(iterable[, r]) --> permutations object\n\ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3223 | \n\ |
| 3224 | Return successive r-length permutations of elements in the iterable.\n\n\ |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 3225 | permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)"); |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3226 | |
| 3227 | static PyTypeObject permutations_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3228 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3229 | "itertools.permutations", /* tp_name */ |
| 3230 | sizeof(permutationsobject), /* tp_basicsize */ |
| 3231 | 0, /* tp_itemsize */ |
| 3232 | /* methods */ |
| 3233 | (destructor)permutations_dealloc, /* tp_dealloc */ |
| 3234 | 0, /* tp_print */ |
| 3235 | 0, /* tp_getattr */ |
| 3236 | 0, /* tp_setattr */ |
| 3237 | 0, /* tp_reserved */ |
| 3238 | 0, /* tp_repr */ |
| 3239 | 0, /* tp_as_number */ |
| 3240 | 0, /* tp_as_sequence */ |
| 3241 | 0, /* tp_as_mapping */ |
| 3242 | 0, /* tp_hash */ |
| 3243 | 0, /* tp_call */ |
| 3244 | 0, /* tp_str */ |
| 3245 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3246 | 0, /* tp_setattro */ |
| 3247 | 0, /* tp_as_buffer */ |
| 3248 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3249 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3250 | permutations_doc, /* tp_doc */ |
| 3251 | (traverseproc)permutations_traverse, /* tp_traverse */ |
| 3252 | 0, /* tp_clear */ |
| 3253 | 0, /* tp_richcompare */ |
| 3254 | 0, /* tp_weaklistoffset */ |
| 3255 | PyObject_SelfIter, /* tp_iter */ |
| 3256 | (iternextfunc)permutations_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3257 | permuations_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3258 | 0, /* tp_members */ |
| 3259 | 0, /* tp_getset */ |
| 3260 | 0, /* tp_base */ |
| 3261 | 0, /* tp_dict */ |
| 3262 | 0, /* tp_descr_get */ |
| 3263 | 0, /* tp_descr_set */ |
| 3264 | 0, /* tp_dictoffset */ |
| 3265 | 0, /* tp_init */ |
| 3266 | 0, /* tp_alloc */ |
| 3267 | permutations_new, /* tp_new */ |
| 3268 | PyObject_GC_Del, /* tp_free */ |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3269 | }; |
| 3270 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3271 | /* accumulate object ************************************************************/ |
| 3272 | |
| 3273 | typedef struct { |
| 3274 | PyObject_HEAD |
| 3275 | PyObject *total; |
| 3276 | PyObject *it; |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3277 | PyObject *binop; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3278 | } accumulateobject; |
| 3279 | |
| 3280 | static PyTypeObject accumulate_type; |
| 3281 | |
| 3282 | static PyObject * |
| 3283 | accumulate_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3284 | { |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3285 | static char *kwargs[] = {"iterable", "func", NULL}; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3286 | PyObject *iterable; |
| 3287 | PyObject *it; |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3288 | PyObject *binop = Py_None; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3289 | accumulateobject *lz; |
| 3290 | |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3291 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:accumulate", |
| 3292 | kwargs, &iterable, &binop)) |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3293 | return NULL; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3294 | |
| 3295 | /* Get iterator. */ |
| 3296 | it = PyObject_GetIter(iterable); |
| 3297 | if (it == NULL) |
| 3298 | return NULL; |
| 3299 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3300 | /* create accumulateobject structure */ |
| 3301 | lz = (accumulateobject *)type->tp_alloc(type, 0); |
| 3302 | if (lz == NULL) { |
| 3303 | Py_DECREF(it); |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3304 | return NULL; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3307 | if (binop != Py_None) { |
| 3308 | Py_XINCREF(binop); |
| 3309 | lz->binop = binop; |
| 3310 | } |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3311 | lz->total = NULL; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3312 | lz->it = it; |
| 3313 | return (PyObject *)lz; |
| 3314 | } |
| 3315 | |
| 3316 | static void |
| 3317 | accumulate_dealloc(accumulateobject *lz) |
| 3318 | { |
| 3319 | PyObject_GC_UnTrack(lz); |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3320 | Py_XDECREF(lz->binop); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3321 | Py_XDECREF(lz->total); |
| 3322 | Py_XDECREF(lz->it); |
| 3323 | Py_TYPE(lz)->tp_free(lz); |
| 3324 | } |
| 3325 | |
| 3326 | static int |
| 3327 | accumulate_traverse(accumulateobject *lz, visitproc visit, void *arg) |
| 3328 | { |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3329 | Py_VISIT(lz->binop); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3330 | Py_VISIT(lz->it); |
| 3331 | Py_VISIT(lz->total); |
| 3332 | return 0; |
| 3333 | } |
| 3334 | |
| 3335 | static PyObject * |
| 3336 | accumulate_next(accumulateobject *lz) |
| 3337 | { |
| 3338 | PyObject *val, *oldtotal, *newtotal; |
| 3339 | |
| 3340 | val = PyIter_Next(lz->it); |
| 3341 | if (val == NULL) |
| 3342 | return NULL; |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3343 | |
| 3344 | if (lz->total == NULL) { |
| 3345 | Py_INCREF(val); |
| 3346 | lz->total = val; |
| 3347 | return lz->total; |
| 3348 | } |
Raymond Hettinger | 5d44613 | 2011-03-27 18:52:10 -0700 | [diff] [blame] | 3349 | |
| 3350 | if (lz->binop == NULL) |
| 3351 | newtotal = PyNumber_Add(lz->total, val); |
| 3352 | else |
| 3353 | newtotal = PyObject_CallFunctionObjArgs(lz->binop, lz->total, val, NULL); |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3354 | Py_DECREF(val); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3355 | if (newtotal == NULL) |
Raymond Hettinger | d8ff465 | 2010-12-03 02:09:34 +0000 | [diff] [blame] | 3356 | return NULL; |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3357 | |
| 3358 | oldtotal = lz->total; |
| 3359 | lz->total = newtotal; |
| 3360 | Py_DECREF(oldtotal); |
| 3361 | |
| 3362 | Py_INCREF(newtotal); |
| 3363 | return newtotal; |
| 3364 | } |
| 3365 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3366 | static PyObject * |
| 3367 | accumulate_reduce(accumulateobject *lz) |
| 3368 | { |
| 3369 | return Py_BuildValue("O(OO)O", Py_TYPE(lz), |
| 3370 | lz->it, lz->binop?lz->binop:Py_None, |
| 3371 | lz->total?lz->total:Py_None); |
| 3372 | } |
| 3373 | |
| 3374 | static PyObject * |
| 3375 | accumulate_setstate(accumulateobject *lz, PyObject *state) |
| 3376 | { |
| 3377 | Py_CLEAR(lz->total); |
| 3378 | lz->total = state; |
| 3379 | Py_INCREF(lz->total); |
| 3380 | Py_RETURN_NONE; |
| 3381 | } |
| 3382 | |
| 3383 | static PyMethodDef accumulate_methods[] = { |
| 3384 | {"__reduce__", (PyCFunction)accumulate_reduce, METH_NOARGS, |
| 3385 | reduce_doc}, |
| 3386 | {"__setstate__", (PyCFunction)accumulate_setstate, METH_O, |
| 3387 | setstate_doc}, |
| 3388 | {NULL, NULL} /* sentinel */ |
| 3389 | }; |
| 3390 | |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3391 | PyDoc_STRVAR(accumulate_doc, |
Raymond Hettinger | 5bf7091 | 2011-03-27 18:59:51 -0700 | [diff] [blame] | 3392 | "accumulate(iterable[, func]) --> accumulate object\n\ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3393 | \n\ |
Raymond Hettinger | 5bf7091 | 2011-03-27 18:59:51 -0700 | [diff] [blame] | 3394 | Return series of accumulated sums (or other binary function results)."); |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3395 | |
| 3396 | static PyTypeObject accumulate_type = { |
| 3397 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3398 | "itertools.accumulate", /* tp_name */ |
| 3399 | sizeof(accumulateobject), /* tp_basicsize */ |
| 3400 | 0, /* tp_itemsize */ |
| 3401 | /* methods */ |
| 3402 | (destructor)accumulate_dealloc, /* tp_dealloc */ |
| 3403 | 0, /* tp_print */ |
| 3404 | 0, /* tp_getattr */ |
| 3405 | 0, /* tp_setattr */ |
| 3406 | 0, /* tp_reserved */ |
| 3407 | 0, /* tp_repr */ |
| 3408 | 0, /* tp_as_number */ |
| 3409 | 0, /* tp_as_sequence */ |
| 3410 | 0, /* tp_as_mapping */ |
| 3411 | 0, /* tp_hash */ |
| 3412 | 0, /* tp_call */ |
| 3413 | 0, /* tp_str */ |
| 3414 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3415 | 0, /* tp_setattro */ |
| 3416 | 0, /* tp_as_buffer */ |
| 3417 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3418 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3419 | accumulate_doc, /* tp_doc */ |
| 3420 | (traverseproc)accumulate_traverse, /* tp_traverse */ |
| 3421 | 0, /* tp_clear */ |
| 3422 | 0, /* tp_richcompare */ |
| 3423 | 0, /* tp_weaklistoffset */ |
| 3424 | PyObject_SelfIter, /* tp_iter */ |
| 3425 | (iternextfunc)accumulate_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3426 | accumulate_methods, /* tp_methods */ |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 3427 | 0, /* tp_members */ |
| 3428 | 0, /* tp_getset */ |
| 3429 | 0, /* tp_base */ |
| 3430 | 0, /* tp_dict */ |
| 3431 | 0, /* tp_descr_get */ |
| 3432 | 0, /* tp_descr_set */ |
| 3433 | 0, /* tp_dictoffset */ |
| 3434 | 0, /* tp_init */ |
| 3435 | 0, /* tp_alloc */ |
| 3436 | accumulate_new, /* tp_new */ |
| 3437 | PyObject_GC_Del, /* tp_free */ |
| 3438 | }; |
| 3439 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3440 | |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3441 | /* compress object ************************************************************/ |
| 3442 | |
| 3443 | /* Equivalent to: |
| 3444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | def compress(data, selectors): |
| 3446 | "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F" |
| 3447 | return (d for d, s in zip(data, selectors) if s) |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3448 | */ |
| 3449 | |
| 3450 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | PyObject_HEAD |
| 3452 | PyObject *data; |
| 3453 | PyObject *selectors; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3454 | } compressobject; |
| 3455 | |
| 3456 | static PyTypeObject compress_type; |
| 3457 | |
| 3458 | static PyObject * |
| 3459 | compress_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3460 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | PyObject *seq1, *seq2; |
| 3462 | PyObject *data=NULL, *selectors=NULL; |
| 3463 | compressobject *lz; |
| 3464 | static char *kwargs[] = {"data", "selectors", NULL}; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:compress", kwargs, &seq1, &seq2)) |
| 3467 | return NULL; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | data = PyObject_GetIter(seq1); |
| 3470 | if (data == NULL) |
| 3471 | goto fail; |
| 3472 | selectors = PyObject_GetIter(seq2); |
| 3473 | if (selectors == NULL) |
| 3474 | goto fail; |
| 3475 | |
| 3476 | /* create compressobject structure */ |
| 3477 | lz = (compressobject *)type->tp_alloc(type, 0); |
| 3478 | if (lz == NULL) |
| 3479 | goto fail; |
| 3480 | lz->data = data; |
| 3481 | lz->selectors = selectors; |
| 3482 | return (PyObject *)lz; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3483 | |
| 3484 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | Py_XDECREF(data); |
| 3486 | Py_XDECREF(selectors); |
| 3487 | return NULL; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | static void |
| 3491 | compress_dealloc(compressobject *lz) |
| 3492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3493 | PyObject_GC_UnTrack(lz); |
| 3494 | Py_XDECREF(lz->data); |
| 3495 | Py_XDECREF(lz->selectors); |
| 3496 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | static int |
| 3500 | compress_traverse(compressobject *lz, visitproc visit, void *arg) |
| 3501 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3502 | Py_VISIT(lz->data); |
| 3503 | Py_VISIT(lz->selectors); |
| 3504 | return 0; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | static PyObject * |
| 3508 | compress_next(compressobject *lz) |
| 3509 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3510 | PyObject *data = lz->data, *selectors = lz->selectors; |
| 3511 | PyObject *datum, *selector; |
| 3512 | PyObject *(*datanext)(PyObject *) = *Py_TYPE(data)->tp_iternext; |
| 3513 | PyObject *(*selectornext)(PyObject *) = *Py_TYPE(selectors)->tp_iternext; |
| 3514 | int ok; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | while (1) { |
| 3517 | /* Steps: get datum, get selector, evaluate selector. |
| 3518 | Order is important (to match the pure python version |
| 3519 | in terms of which input gets a chance to raise an |
| 3520 | exception first). |
| 3521 | */ |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | datum = datanext(data); |
| 3524 | if (datum == NULL) |
| 3525 | return NULL; |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3527 | selector = selectornext(selectors); |
| 3528 | if (selector == NULL) { |
| 3529 | Py_DECREF(datum); |
| 3530 | return NULL; |
| 3531 | } |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | ok = PyObject_IsTrue(selector); |
| 3534 | Py_DECREF(selector); |
| 3535 | if (ok == 1) |
| 3536 | return datum; |
| 3537 | Py_DECREF(datum); |
| 3538 | if (ok == -1) |
| 3539 | return NULL; |
| 3540 | } |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3543 | static PyObject * |
| 3544 | compress_reduce(compressobject *lz) |
| 3545 | { |
| 3546 | return Py_BuildValue("O(OO)", Py_TYPE(lz), |
| 3547 | lz->data, lz->selectors); |
| 3548 | } |
| 3549 | |
| 3550 | static PyMethodDef compress_methods[] = { |
| 3551 | {"__reduce__", (PyCFunction)compress_reduce, METH_NOARGS, |
| 3552 | reduce_doc}, |
| 3553 | {NULL, NULL} /* sentinel */ |
| 3554 | }; |
| 3555 | |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3556 | PyDoc_STRVAR(compress_doc, |
Raymond Hettinger | 15a4950 | 2009-02-19 02:17:09 +0000 | [diff] [blame] | 3557 | "compress(data, selectors) --> iterator over selected data\n\ |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3558 | \n\ |
| 3559 | Return data elements corresponding to true selector elements.\n\ |
| 3560 | Forms a shorter iterator from selected data elements using the\n\ |
| 3561 | selectors to choose the data elements."); |
| 3562 | |
| 3563 | static PyTypeObject compress_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3565 | "itertools.compress", /* tp_name */ |
| 3566 | sizeof(compressobject), /* tp_basicsize */ |
| 3567 | 0, /* tp_itemsize */ |
| 3568 | /* methods */ |
| 3569 | (destructor)compress_dealloc, /* tp_dealloc */ |
| 3570 | 0, /* tp_print */ |
| 3571 | 0, /* tp_getattr */ |
| 3572 | 0, /* tp_setattr */ |
| 3573 | 0, /* tp_reserved */ |
| 3574 | 0, /* tp_repr */ |
| 3575 | 0, /* tp_as_number */ |
| 3576 | 0, /* tp_as_sequence */ |
| 3577 | 0, /* tp_as_mapping */ |
| 3578 | 0, /* tp_hash */ |
| 3579 | 0, /* tp_call */ |
| 3580 | 0, /* tp_str */ |
| 3581 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3582 | 0, /* tp_setattro */ |
| 3583 | 0, /* tp_as_buffer */ |
| 3584 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3585 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3586 | compress_doc, /* tp_doc */ |
| 3587 | (traverseproc)compress_traverse, /* tp_traverse */ |
| 3588 | 0, /* tp_clear */ |
| 3589 | 0, /* tp_richcompare */ |
| 3590 | 0, /* tp_weaklistoffset */ |
| 3591 | PyObject_SelfIter, /* tp_iter */ |
| 3592 | (iternextfunc)compress_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3593 | compress_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3594 | 0, /* tp_members */ |
| 3595 | 0, /* tp_getset */ |
| 3596 | 0, /* tp_base */ |
| 3597 | 0, /* tp_dict */ |
| 3598 | 0, /* tp_descr_get */ |
| 3599 | 0, /* tp_descr_set */ |
| 3600 | 0, /* tp_dictoffset */ |
| 3601 | 0, /* tp_init */ |
| 3602 | 0, /* tp_alloc */ |
| 3603 | compress_new, /* tp_new */ |
| 3604 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame] | 3605 | }; |
| 3606 | |
| 3607 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3608 | /* filterfalse object ************************************************************/ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3609 | |
| 3610 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3611 | PyObject_HEAD |
| 3612 | PyObject *func; |
| 3613 | PyObject *it; |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3614 | } filterfalseobject; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3615 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3616 | static PyTypeObject filterfalse_type; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3617 | |
| 3618 | static PyObject * |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3619 | filterfalse_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3620 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | PyObject *func, *seq; |
| 3622 | PyObject *it; |
| 3623 | filterfalseobject *lz; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | if (type == &filterfalse_type && |
| 3626 | !_PyArg_NoKeywords("filterfalse()", kwds)) |
| 3627 | return NULL; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 3628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | if (!PyArg_UnpackTuple(args, "filterfalse", 2, 2, &func, &seq)) |
| 3630 | return NULL; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | /* Get iterator. */ |
| 3633 | it = PyObject_GetIter(seq); |
| 3634 | if (it == NULL) |
| 3635 | return NULL; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | /* create filterfalseobject structure */ |
| 3638 | lz = (filterfalseobject *)type->tp_alloc(type, 0); |
| 3639 | if (lz == NULL) { |
| 3640 | Py_DECREF(it); |
| 3641 | return NULL; |
| 3642 | } |
| 3643 | Py_INCREF(func); |
| 3644 | lz->func = func; |
| 3645 | lz->it = it; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3647 | return (PyObject *)lz; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | static void |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3651 | filterfalse_dealloc(filterfalseobject *lz) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | PyObject_GC_UnTrack(lz); |
| 3654 | Py_XDECREF(lz->func); |
| 3655 | Py_XDECREF(lz->it); |
| 3656 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | static int |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3660 | filterfalse_traverse(filterfalseobject *lz, visitproc visit, void *arg) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3661 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3662 | Py_VISIT(lz->it); |
| 3663 | Py_VISIT(lz->func); |
| 3664 | return 0; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | static PyObject * |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3668 | filterfalse_next(filterfalseobject *lz) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3670 | PyObject *item; |
| 3671 | PyObject *it = lz->it; |
| 3672 | long ok; |
| 3673 | PyObject *(*iternext)(PyObject *); |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3675 | iternext = *Py_TYPE(it)->tp_iternext; |
| 3676 | for (;;) { |
| 3677 | item = iternext(it); |
| 3678 | if (item == NULL) |
| 3679 | return NULL; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | if (lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type) { |
| 3682 | ok = PyObject_IsTrue(item); |
| 3683 | } else { |
| 3684 | PyObject *good; |
| 3685 | good = PyObject_CallFunctionObjArgs(lz->func, |
| 3686 | item, NULL); |
| 3687 | if (good == NULL) { |
| 3688 | Py_DECREF(item); |
| 3689 | return NULL; |
| 3690 | } |
| 3691 | ok = PyObject_IsTrue(good); |
| 3692 | Py_DECREF(good); |
| 3693 | } |
| 3694 | if (!ok) |
| 3695 | return item; |
| 3696 | Py_DECREF(item); |
| 3697 | } |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3700 | static PyObject * |
| 3701 | filterfalse_reduce(filterfalseobject *lz) |
| 3702 | { |
| 3703 | return Py_BuildValue("O(OO)", Py_TYPE(lz), |
| 3704 | lz->func, lz->it); |
| 3705 | } |
| 3706 | |
| 3707 | static PyMethodDef filterfalse_methods[] = { |
| 3708 | {"__reduce__", (PyCFunction)filterfalse_reduce, METH_NOARGS, |
| 3709 | reduce_doc}, |
| 3710 | {NULL, NULL} /* sentinel */ |
| 3711 | }; |
| 3712 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3713 | PyDoc_STRVAR(filterfalse_doc, |
| 3714 | "filterfalse(function or None, sequence) --> filterfalse object\n\ |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 3715 | \n\ |
| 3716 | Return those items of sequence for which function(item) is false.\n\ |
| 3717 | If function is None, return the items that are false."); |
| 3718 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 3719 | static PyTypeObject filterfalse_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3721 | "itertools.filterfalse", /* tp_name */ |
| 3722 | sizeof(filterfalseobject), /* tp_basicsize */ |
| 3723 | 0, /* tp_itemsize */ |
| 3724 | /* methods */ |
| 3725 | (destructor)filterfalse_dealloc, /* tp_dealloc */ |
| 3726 | 0, /* tp_print */ |
| 3727 | 0, /* tp_getattr */ |
| 3728 | 0, /* tp_setattr */ |
| 3729 | 0, /* tp_reserved */ |
| 3730 | 0, /* tp_repr */ |
| 3731 | 0, /* tp_as_number */ |
| 3732 | 0, /* tp_as_sequence */ |
| 3733 | 0, /* tp_as_mapping */ |
| 3734 | 0, /* tp_hash */ |
| 3735 | 0, /* tp_call */ |
| 3736 | 0, /* tp_str */ |
| 3737 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3738 | 0, /* tp_setattro */ |
| 3739 | 0, /* tp_as_buffer */ |
| 3740 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3741 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3742 | filterfalse_doc, /* tp_doc */ |
| 3743 | (traverseproc)filterfalse_traverse, /* tp_traverse */ |
| 3744 | 0, /* tp_clear */ |
| 3745 | 0, /* tp_richcompare */ |
| 3746 | 0, /* tp_weaklistoffset */ |
| 3747 | PyObject_SelfIter, /* tp_iter */ |
| 3748 | (iternextfunc)filterfalse_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3749 | filterfalse_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3750 | 0, /* tp_members */ |
| 3751 | 0, /* tp_getset */ |
| 3752 | 0, /* tp_base */ |
| 3753 | 0, /* tp_dict */ |
| 3754 | 0, /* tp_descr_get */ |
| 3755 | 0, /* tp_descr_set */ |
| 3756 | 0, /* tp_dictoffset */ |
| 3757 | 0, /* tp_init */ |
| 3758 | 0, /* tp_alloc */ |
| 3759 | filterfalse_new, /* tp_new */ |
| 3760 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3761 | }; |
| 3762 | |
| 3763 | |
| 3764 | /* count object ************************************************************/ |
| 3765 | |
| 3766 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | PyObject_HEAD |
| 3768 | Py_ssize_t cnt; |
| 3769 | PyObject *long_cnt; |
| 3770 | PyObject *long_step; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3771 | } countobject; |
| 3772 | |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3773 | /* Counting logic and invariants: |
| 3774 | |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3775 | fast_mode: when cnt an integer < PY_SSIZE_T_MAX and no step is specified. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3777 | assert(cnt != PY_SSIZE_T_MAX && long_cnt == NULL && long_step==PyInt(1)); |
| 3778 | Advances with: cnt += 1 |
| 3779 | When count hits Y_SSIZE_T_MAX, switch to slow_mode. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3780 | |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3781 | slow_mode: when cnt == PY_SSIZE_T_MAX, step is not int(1), or cnt is a float. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3782 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3783 | assert(cnt == PY_SSIZE_T_MAX && long_cnt != NULL && long_step != NULL); |
| 3784 | All counting is done with python objects (no overflows or underflows). |
| 3785 | Advances with: long_cnt += long_step |
| 3786 | Step may be zero -- effectively a slow version of repeat(cnt). |
| 3787 | Either long_cnt or long_step may be a float, Fraction, or Decimal. |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3788 | */ |
| 3789 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 3790 | static PyTypeObject count_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3791 | |
| 3792 | static PyObject * |
| 3793 | count_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3795 | countobject *lz; |
| 3796 | int slow_mode = 0; |
| 3797 | Py_ssize_t cnt = 0; |
| 3798 | PyObject *long_cnt = NULL; |
| 3799 | PyObject *long_step = NULL; |
Kristjan Valur Jonsson | 35722a9 | 2011-03-30 11:04:28 +0000 | [diff] [blame] | 3800 | long step; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3801 | static char *kwlist[] = {"start", "step", 0}; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:count", |
| 3804 | kwlist, &long_cnt, &long_step)) |
| 3805 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3807 | if ((long_cnt != NULL && !PyNumber_Check(long_cnt)) || |
| 3808 | (long_step != NULL && !PyNumber_Check(long_step))) { |
| 3809 | PyErr_SetString(PyExc_TypeError, "a number is required"); |
| 3810 | return NULL; |
| 3811 | } |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | if (long_cnt != NULL) { |
| 3814 | cnt = PyLong_AsSsize_t(long_cnt); |
| 3815 | if ((cnt == -1 && PyErr_Occurred()) || !PyLong_Check(long_cnt)) { |
| 3816 | PyErr_Clear(); |
| 3817 | slow_mode = 1; |
| 3818 | } |
| 3819 | Py_INCREF(long_cnt); |
| 3820 | } else { |
| 3821 | cnt = 0; |
| 3822 | long_cnt = PyLong_FromLong(0); |
| 3823 | } |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3825 | /* If not specified, step defaults to 1 */ |
| 3826 | if (long_step == NULL) { |
| 3827 | long_step = PyLong_FromLong(1); |
| 3828 | if (long_step == NULL) { |
| 3829 | Py_DECREF(long_cnt); |
| 3830 | return NULL; |
| 3831 | } |
| 3832 | } else |
| 3833 | Py_INCREF(long_step); |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | assert(long_cnt != NULL && long_step != NULL); |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3836 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3837 | /* Fast mode only works when the step is 1 */ |
Kristjan Valur Jonsson | 35722a9 | 2011-03-30 11:04:28 +0000 | [diff] [blame] | 3838 | step = PyLong_AsLong(long_step); |
| 3839 | if (step != 1) { |
| 3840 | slow_mode = 1; |
| 3841 | if (step == -1 && PyErr_Occurred()) |
| 3842 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3843 | } |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3845 | if (slow_mode) |
| 3846 | cnt = PY_SSIZE_T_MAX; |
| 3847 | else |
| 3848 | Py_CLEAR(long_cnt); |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3850 | assert((cnt != PY_SSIZE_T_MAX && long_cnt == NULL && !slow_mode) || |
| 3851 | (cnt == PY_SSIZE_T_MAX && long_cnt != NULL && slow_mode)); |
| 3852 | assert(slow_mode || |
| 3853 | (PyLong_Check(long_step) && PyLong_AS_LONG(long_step) == 1)); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3855 | /* create countobject structure */ |
| 3856 | lz = (countobject *)type->tp_alloc(type, 0); |
| 3857 | if (lz == NULL) { |
| 3858 | Py_XDECREF(long_cnt); |
| 3859 | return NULL; |
| 3860 | } |
| 3861 | lz->cnt = cnt; |
| 3862 | lz->long_cnt = long_cnt; |
| 3863 | lz->long_step = long_step; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | return (PyObject *)lz; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3866 | } |
| 3867 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3868 | static void |
| 3869 | count_dealloc(countobject *lz) |
| 3870 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3871 | PyObject_GC_UnTrack(lz); |
| 3872 | Py_XDECREF(lz->long_cnt); |
| 3873 | Py_XDECREF(lz->long_step); |
| 3874 | Py_TYPE(lz)->tp_free(lz); |
Raymond Hettinger | d6280f4 | 2009-02-16 20:50:56 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Benjamin Peterson | 25e26a0 | 2009-02-16 21:28:29 +0000 | [diff] [blame] | 3877 | static int |
Raymond Hettinger | d6280f4 | 2009-02-16 20:50:56 +0000 | [diff] [blame] | 3878 | count_traverse(countobject *lz, visitproc visit, void *arg) |
| 3879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3880 | Py_VISIT(lz->long_cnt); |
| 3881 | Py_VISIT(lz->long_step); |
| 3882 | return 0; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | static PyObject * |
| 3886 | count_nextlong(countobject *lz) |
| 3887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3888 | PyObject *long_cnt; |
| 3889 | PyObject *stepped_up; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 | long_cnt = lz->long_cnt; |
| 3892 | if (long_cnt == NULL) { |
| 3893 | /* Switch to slow_mode */ |
| 3894 | long_cnt = PyLong_FromSsize_t(PY_SSIZE_T_MAX); |
| 3895 | if (long_cnt == NULL) |
| 3896 | return NULL; |
| 3897 | } |
| 3898 | assert(lz->cnt == PY_SSIZE_T_MAX && long_cnt != NULL); |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | stepped_up = PyNumber_Add(long_cnt, lz->long_step); |
| 3901 | if (stepped_up == NULL) |
| 3902 | return NULL; |
| 3903 | lz->long_cnt = stepped_up; |
| 3904 | return long_cnt; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3907 | static PyObject * |
| 3908 | count_next(countobject *lz) |
| 3909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3910 | if (lz->cnt == PY_SSIZE_T_MAX) |
| 3911 | return count_nextlong(lz); |
| 3912 | return PyLong_FromSsize_t(lz->cnt++); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 3915 | static PyObject * |
| 3916 | count_repr(countobject *lz) |
| 3917 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3918 | if (lz->cnt != PY_SSIZE_T_MAX) |
| 3919 | return PyUnicode_FromFormat("count(%zd)", lz->cnt); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 3920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3921 | if (PyLong_Check(lz->long_step)) { |
| 3922 | long step = PyLong_AsLong(lz->long_step); |
| 3923 | if (step == -1 && PyErr_Occurred()) { |
| 3924 | PyErr_Clear(); |
| 3925 | } |
| 3926 | if (step == 1) { |
| 3927 | /* Don't display step when it is an integer equal to 1 */ |
| 3928 | return PyUnicode_FromFormat("count(%R)", lz->long_cnt); |
| 3929 | } |
| 3930 | } |
| 3931 | return PyUnicode_FromFormat("count(%R, %R)", |
| 3932 | lz->long_cnt, lz->long_step); |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 3935 | static PyObject * |
| 3936 | count_reduce(countobject *lz) |
| 3937 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3938 | if (lz->cnt == PY_SSIZE_T_MAX) |
| 3939 | return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->long_cnt, lz->long_step); |
| 3940 | return Py_BuildValue("O(n)", Py_TYPE(lz), lz->cnt); |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 3943 | static PyMethodDef count_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3944 | {"__reduce__", (PyCFunction)count_reduce, METH_NOARGS, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3945 | reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3946 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | a3e1ad2 | 2009-11-30 22:02:31 +0000 | [diff] [blame] | 3947 | }; |
| 3948 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3949 | PyDoc_STRVAR(count_doc, |
Ezio Melotti | bfd73fa | 2010-06-11 02:26:42 +0000 | [diff] [blame] | 3950 | "count(start=0, step=1) --> count object\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3951 | \n\ |
Raymond Hettinger | eb13fdd | 2009-02-21 22:30:12 +0000 | [diff] [blame] | 3952 | Return a count object whose .__next__() method returns consecutive values.\n\ |
| 3953 | Equivalent to:\n\n\ |
Raymond Hettinger | 3072921 | 2009-02-12 06:28:27 +0000 | [diff] [blame] | 3954 | def count(firstval=0, step=1):\n\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3955 | x = firstval\n\ |
| 3956 | while 1:\n\ |
| 3957 | yield x\n\ |
| 3958 | x += step\n"); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 3959 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 3960 | static PyTypeObject count_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3961 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3962 | "itertools.count", /* tp_name */ |
| 3963 | sizeof(countobject), /* tp_basicsize */ |
| 3964 | 0, /* tp_itemsize */ |
| 3965 | /* methods */ |
| 3966 | (destructor)count_dealloc, /* tp_dealloc */ |
| 3967 | 0, /* tp_print */ |
| 3968 | 0, /* tp_getattr */ |
| 3969 | 0, /* tp_setattr */ |
| 3970 | 0, /* tp_reserved */ |
| 3971 | (reprfunc)count_repr, /* tp_repr */ |
| 3972 | 0, /* tp_as_number */ |
| 3973 | 0, /* tp_as_sequence */ |
| 3974 | 0, /* tp_as_mapping */ |
| 3975 | 0, /* tp_hash */ |
| 3976 | 0, /* tp_call */ |
| 3977 | 0, /* tp_str */ |
| 3978 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3979 | 0, /* tp_setattro */ |
| 3980 | 0, /* tp_as_buffer */ |
| 3981 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3982 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3983 | count_doc, /* tp_doc */ |
| 3984 | (traverseproc)count_traverse, /* tp_traverse */ |
| 3985 | 0, /* tp_clear */ |
| 3986 | 0, /* tp_richcompare */ |
| 3987 | 0, /* tp_weaklistoffset */ |
| 3988 | PyObject_SelfIter, /* tp_iter */ |
| 3989 | (iternextfunc)count_next, /* tp_iternext */ |
| 3990 | count_methods, /* tp_methods */ |
| 3991 | 0, /* tp_members */ |
| 3992 | 0, /* tp_getset */ |
| 3993 | 0, /* tp_base */ |
| 3994 | 0, /* tp_dict */ |
| 3995 | 0, /* tp_descr_get */ |
| 3996 | 0, /* tp_descr_set */ |
| 3997 | 0, /* tp_dictoffset */ |
| 3998 | 0, /* tp_init */ |
| 3999 | 0, /* tp_alloc */ |
| 4000 | count_new, /* tp_new */ |
| 4001 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4002 | }; |
| 4003 | |
| 4004 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4005 | /* repeat object ************************************************************/ |
| 4006 | |
| 4007 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4008 | PyObject_HEAD |
| 4009 | PyObject *element; |
| 4010 | Py_ssize_t cnt; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4011 | } repeatobject; |
| 4012 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 4013 | static PyTypeObject repeat_type; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4014 | |
| 4015 | static PyObject * |
| 4016 | repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4017 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4018 | repeatobject *ro; |
| 4019 | PyObject *element; |
| 4020 | Py_ssize_t cnt = -1; |
| 4021 | static char *kwargs[] = {"object", "times", NULL}; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4023 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:repeat", kwargs, |
| 4024 | &element, &cnt)) |
| 4025 | return NULL; |
Raymond Hettinger | 7c2bb5b | 2003-05-03 05:59:48 +0000 | [diff] [blame] | 4026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4027 | if (PyTuple_Size(args) == 2 && cnt < 0) |
| 4028 | cnt = 0; |
| 4029 | |
| 4030 | ro = (repeatobject *)type->tp_alloc(type, 0); |
| 4031 | if (ro == NULL) |
| 4032 | return NULL; |
| 4033 | Py_INCREF(element); |
| 4034 | ro->element = element; |
| 4035 | ro->cnt = cnt; |
| 4036 | return (PyObject *)ro; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | static void |
| 4040 | repeat_dealloc(repeatobject *ro) |
| 4041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4042 | PyObject_GC_UnTrack(ro); |
| 4043 | Py_XDECREF(ro->element); |
| 4044 | Py_TYPE(ro)->tp_free(ro); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | static int |
| 4048 | repeat_traverse(repeatobject *ro, visitproc visit, void *arg) |
| 4049 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4050 | Py_VISIT(ro->element); |
| 4051 | return 0; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | static PyObject * |
| 4055 | repeat_next(repeatobject *ro) |
| 4056 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4057 | if (ro->cnt == 0) |
| 4058 | return NULL; |
| 4059 | if (ro->cnt > 0) |
| 4060 | ro->cnt--; |
| 4061 | Py_INCREF(ro->element); |
| 4062 | return ro->element; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 4065 | static PyObject * |
| 4066 | repeat_repr(repeatobject *ro) |
| 4067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4068 | if (ro->cnt == -1) |
| 4069 | return PyUnicode_FromFormat("repeat(%R)", ro->element); |
| 4070 | else |
| 4071 | return PyUnicode_FromFormat("repeat(%R, %zd)", ro->element, ro->cnt); |
| 4072 | } |
Raymond Hettinger | 7dacda2 | 2004-04-08 21:54:00 +0000 | [diff] [blame] | 4073 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 4074 | static PyObject * |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 4075 | repeat_len(repeatobject *ro) |
| 4076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4077 | if (ro->cnt == -1) { |
| 4078 | PyErr_SetString(PyExc_TypeError, "len() of unsized object"); |
| 4079 | return NULL; |
| 4080 | } |
| 4081 | return PyLong_FromSize_t(ro->cnt); |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 4084 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 4085 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4086 | static PyObject * |
| 4087 | repeat_reduce(repeatobject *ro) |
| 4088 | { |
| 4089 | /* unpickle this so that a new repeat iterator is constructed with an |
| 4090 | * object, then call __setstate__ on it to set cnt |
| 4091 | */ |
| 4092 | if (ro->cnt >= 0) |
| 4093 | return Py_BuildValue("O(On)", Py_TYPE(ro), ro->element, ro->cnt); |
| 4094 | else |
| 4095 | return Py_BuildValue("O(O)", Py_TYPE(ro), ro->element); |
| 4096 | } |
| 4097 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 4098 | static PyMethodDef repeat_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4099 | {"__length_hint__", (PyCFunction)repeat_len, METH_NOARGS, length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4100 | {"__reduce__", (PyCFunction)repeat_reduce, METH_NOARGS, reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4101 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 5cab2e3 | 2004-02-10 09:25:40 +0000 | [diff] [blame] | 4102 | }; |
| 4103 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4104 | PyDoc_STRVAR(repeat_doc, |
Raymond Hettinger | f4bb7f2 | 2009-02-19 02:44:01 +0000 | [diff] [blame] | 4105 | "repeat(object [,times]) -> create an iterator which returns the object\n\ |
| 4106 | for the specified number of times. If not specified, returns the object\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 4107 | endlessly."); |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4108 | |
Raymond Hettinger | 1d7a348 | 2003-07-14 07:07:12 +0000 | [diff] [blame] | 4109 | static PyTypeObject repeat_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4110 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4111 | "itertools.repeat", /* tp_name */ |
| 4112 | sizeof(repeatobject), /* tp_basicsize */ |
| 4113 | 0, /* tp_itemsize */ |
| 4114 | /* methods */ |
| 4115 | (destructor)repeat_dealloc, /* tp_dealloc */ |
| 4116 | 0, /* tp_print */ |
| 4117 | 0, /* tp_getattr */ |
| 4118 | 0, /* tp_setattr */ |
| 4119 | 0, /* tp_reserved */ |
| 4120 | (reprfunc)repeat_repr, /* tp_repr */ |
| 4121 | 0, /* tp_as_number */ |
| 4122 | 0, /* tp_as_sequence */ |
| 4123 | 0, /* tp_as_mapping */ |
| 4124 | 0, /* tp_hash */ |
| 4125 | 0, /* tp_call */ |
| 4126 | 0, /* tp_str */ |
| 4127 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4128 | 0, /* tp_setattro */ |
| 4129 | 0, /* tp_as_buffer */ |
| 4130 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 4131 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4132 | repeat_doc, /* tp_doc */ |
| 4133 | (traverseproc)repeat_traverse, /* tp_traverse */ |
| 4134 | 0, /* tp_clear */ |
| 4135 | 0, /* tp_richcompare */ |
| 4136 | 0, /* tp_weaklistoffset */ |
| 4137 | PyObject_SelfIter, /* tp_iter */ |
| 4138 | (iternextfunc)repeat_next, /* tp_iternext */ |
| 4139 | repeat_methods, /* tp_methods */ |
| 4140 | 0, /* tp_members */ |
| 4141 | 0, /* tp_getset */ |
| 4142 | 0, /* tp_base */ |
| 4143 | 0, /* tp_dict */ |
| 4144 | 0, /* tp_descr_get */ |
| 4145 | 0, /* tp_descr_set */ |
| 4146 | 0, /* tp_dictoffset */ |
| 4147 | 0, /* tp_init */ |
| 4148 | 0, /* tp_alloc */ |
| 4149 | repeat_new, /* tp_new */ |
| 4150 | PyObject_GC_Del, /* tp_free */ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4151 | }; |
| 4152 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4153 | /* ziplongest object ************************************************************/ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4154 | |
| 4155 | #include "Python.h" |
| 4156 | |
| 4157 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | PyObject_HEAD |
| 4159 | Py_ssize_t tuplesize; |
| 4160 | Py_ssize_t numactive; |
| 4161 | PyObject *ittuple; /* tuple of iterators */ |
| 4162 | PyObject *result; |
| 4163 | PyObject *fillvalue; |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4164 | } ziplongestobject; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4165 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4166 | static PyTypeObject ziplongest_type; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4167 | |
| 4168 | static PyObject * |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4169 | zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4170 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | ziplongestobject *lz; |
| 4172 | Py_ssize_t i; |
| 4173 | PyObject *ittuple; /* tuple of iterators */ |
| 4174 | PyObject *result; |
| 4175 | PyObject *fillvalue = Py_None; |
| 4176 | Py_ssize_t tuplesize = PySequence_Length(args); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4178 | if (kwds != NULL && PyDict_CheckExact(kwds) && PyDict_Size(kwds) > 0) { |
| 4179 | fillvalue = PyDict_GetItemString(kwds, "fillvalue"); |
| 4180 | if (fillvalue == NULL || PyDict_Size(kwds) > 1) { |
| 4181 | PyErr_SetString(PyExc_TypeError, |
| 4182 | "zip_longest() got an unexpected keyword argument"); |
| 4183 | return NULL; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4184 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4185 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4187 | /* args must be a tuple */ |
| 4188 | assert(PyTuple_Check(args)); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4190 | /* obtain iterators */ |
| 4191 | ittuple = PyTuple_New(tuplesize); |
| 4192 | if (ittuple == NULL) |
| 4193 | return NULL; |
| 4194 | for (i=0; i < tuplesize; ++i) { |
| 4195 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 4196 | PyObject *it = PyObject_GetIter(item); |
| 4197 | if (it == NULL) { |
| 4198 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 4199 | PyErr_Format(PyExc_TypeError, |
| 4200 | "zip_longest argument #%zd must support iteration", |
| 4201 | i+1); |
| 4202 | Py_DECREF(ittuple); |
| 4203 | return NULL; |
| 4204 | } |
| 4205 | PyTuple_SET_ITEM(ittuple, i, it); |
| 4206 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | /* create a result holder */ |
| 4209 | result = PyTuple_New(tuplesize); |
| 4210 | if (result == NULL) { |
| 4211 | Py_DECREF(ittuple); |
| 4212 | return NULL; |
| 4213 | } |
| 4214 | for (i=0 ; i < tuplesize ; i++) { |
| 4215 | Py_INCREF(Py_None); |
| 4216 | PyTuple_SET_ITEM(result, i, Py_None); |
| 4217 | } |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | /* create ziplongestobject structure */ |
| 4220 | lz = (ziplongestobject *)type->tp_alloc(type, 0); |
| 4221 | if (lz == NULL) { |
| 4222 | Py_DECREF(ittuple); |
| 4223 | Py_DECREF(result); |
| 4224 | return NULL; |
| 4225 | } |
| 4226 | lz->ittuple = ittuple; |
| 4227 | lz->tuplesize = tuplesize; |
| 4228 | lz->numactive = tuplesize; |
| 4229 | lz->result = result; |
| 4230 | Py_INCREF(fillvalue); |
| 4231 | lz->fillvalue = fillvalue; |
| 4232 | return (PyObject *)lz; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | static void |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4236 | zip_longest_dealloc(ziplongestobject *lz) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4238 | PyObject_GC_UnTrack(lz); |
| 4239 | Py_XDECREF(lz->ittuple); |
| 4240 | Py_XDECREF(lz->result); |
| 4241 | Py_XDECREF(lz->fillvalue); |
| 4242 | Py_TYPE(lz)->tp_free(lz); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | static int |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4246 | zip_longest_traverse(ziplongestobject *lz, visitproc visit, void *arg) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | Py_VISIT(lz->ittuple); |
| 4249 | Py_VISIT(lz->result); |
| 4250 | Py_VISIT(lz->fillvalue); |
| 4251 | return 0; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | static PyObject * |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4255 | zip_longest_next(ziplongestobject *lz) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4257 | Py_ssize_t i; |
| 4258 | Py_ssize_t tuplesize = lz->tuplesize; |
| 4259 | PyObject *result = lz->result; |
| 4260 | PyObject *it; |
| 4261 | PyObject *item; |
| 4262 | PyObject *olditem; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4264 | if (tuplesize == 0) |
| 4265 | return NULL; |
| 4266 | if (lz->numactive == 0) |
| 4267 | return NULL; |
| 4268 | if (Py_REFCNT(result) == 1) { |
| 4269 | Py_INCREF(result); |
| 4270 | for (i=0 ; i < tuplesize ; i++) { |
| 4271 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
| 4272 | if (it == NULL) { |
| 4273 | Py_INCREF(lz->fillvalue); |
| 4274 | item = lz->fillvalue; |
| 4275 | } else { |
| 4276 | item = PyIter_Next(it); |
| 4277 | if (item == NULL) { |
| 4278 | lz->numactive -= 1; |
| 4279 | if (lz->numactive == 0 || PyErr_Occurred()) { |
| 4280 | lz->numactive = 0; |
| 4281 | Py_DECREF(result); |
Raymond Hettinger | fc43851 | 2009-11-01 20:55:33 +0000 | [diff] [blame] | 4282 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4283 | } else { |
| 4284 | Py_INCREF(lz->fillvalue); |
| 4285 | item = lz->fillvalue; |
| 4286 | PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
| 4287 | Py_DECREF(it); |
| 4288 | } |
Raymond Hettinger | fc43851 | 2009-11-01 20:55:33 +0000 | [diff] [blame] | 4289 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4290 | } |
| 4291 | olditem = PyTuple_GET_ITEM(result, i); |
| 4292 | PyTuple_SET_ITEM(result, i, item); |
| 4293 | Py_DECREF(olditem); |
Raymond Hettinger | fc43851 | 2009-11-01 20:55:33 +0000 | [diff] [blame] | 4294 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4295 | } else { |
| 4296 | result = PyTuple_New(tuplesize); |
| 4297 | if (result == NULL) |
| 4298 | return NULL; |
| 4299 | for (i=0 ; i < tuplesize ; i++) { |
| 4300 | it = PyTuple_GET_ITEM(lz->ittuple, i); |
| 4301 | if (it == NULL) { |
| 4302 | Py_INCREF(lz->fillvalue); |
| 4303 | item = lz->fillvalue; |
| 4304 | } else { |
| 4305 | item = PyIter_Next(it); |
| 4306 | if (item == NULL) { |
| 4307 | lz->numactive -= 1; |
| 4308 | if (lz->numactive == 0 || PyErr_Occurred()) { |
| 4309 | lz->numactive = 0; |
| 4310 | Py_DECREF(result); |
| 4311 | return NULL; |
| 4312 | } else { |
| 4313 | Py_INCREF(lz->fillvalue); |
| 4314 | item = lz->fillvalue; |
| 4315 | PyTuple_SET_ITEM(lz->ittuple, i, NULL); |
| 4316 | Py_DECREF(it); |
| 4317 | } |
| 4318 | } |
| 4319 | } |
| 4320 | PyTuple_SET_ITEM(result, i, item); |
| 4321 | } |
| 4322 | } |
| 4323 | return result; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4326 | static PyObject * |
| 4327 | zip_longest_reduce(ziplongestobject *lz) |
| 4328 | { |
| 4329 | |
| 4330 | /* Create a new tuple with empty sequences where appropriate to pickle. |
| 4331 | * Then use setstate to set the fillvalue |
| 4332 | */ |
| 4333 | int i; |
| 4334 | PyObject *args = PyTuple_New(PyTuple_GET_SIZE(lz->ittuple)); |
| 4335 | if (args == NULL) |
| 4336 | return NULL; |
| 4337 | for (i=0; i<PyTuple_GET_SIZE(lz->ittuple); i++) { |
| 4338 | PyObject *elem = PyTuple_GET_ITEM(lz->ittuple, i); |
| 4339 | if (elem == NULL) { |
| 4340 | elem = PyTuple_New(0); |
| 4341 | if (elem == NULL) { |
| 4342 | Py_DECREF(args); |
| 4343 | return NULL; |
| 4344 | } |
| 4345 | } else |
| 4346 | Py_INCREF(elem); |
| 4347 | PyTuple_SET_ITEM(args, i, elem); |
| 4348 | } |
| 4349 | return Py_BuildValue("ONO", Py_TYPE(lz), args, lz->fillvalue); |
| 4350 | } |
| 4351 | |
| 4352 | static PyObject * |
| 4353 | zip_longest_setstate(ziplongestobject *lz, PyObject *state) |
| 4354 | { |
| 4355 | Py_CLEAR(lz->fillvalue); |
| 4356 | lz->fillvalue = state; |
| 4357 | Py_INCREF(lz->fillvalue); |
| 4358 | Py_RETURN_NONE; |
| 4359 | } |
| 4360 | |
| 4361 | static PyMethodDef zip_longest_methods[] = { |
| 4362 | {"__reduce__", (PyCFunction)zip_longest_reduce, METH_NOARGS, |
| 4363 | reduce_doc}, |
| 4364 | {"__setstate__", (PyCFunction)zip_longest_setstate, METH_O, |
| 4365 | setstate_doc}, |
| 4366 | {NULL, NULL} /* sentinel */ |
| 4367 | }; |
| 4368 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4369 | PyDoc_STRVAR(zip_longest_doc, |
| 4370 | "zip_longest(iter1 [,iter2 [...]], [fillvalue=None]) --> zip_longest object\n\ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4371 | \n\ |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4372 | Return an zip_longest object whose .__next__() method returns a tuple where\n\ |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 4373 | the i-th element comes from the i-th iterable argument. The .__next__()\n\ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4374 | method continues until the longest iterable in the argument sequence\n\ |
| 4375 | is exhausted and then it raises StopIteration. When the shorter iterables\n\ |
| 4376 | are exhausted, the fillvalue is substituted in their place. The fillvalue\n\ |
| 4377 | defaults to None or can be specified by a keyword argument.\n\ |
| 4378 | "); |
| 4379 | |
Raymond Hettinger | 736c0ab | 2008-03-13 02:09:15 +0000 | [diff] [blame] | 4380 | static PyTypeObject ziplongest_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4381 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4382 | "itertools.zip_longest", /* tp_name */ |
| 4383 | sizeof(ziplongestobject), /* tp_basicsize */ |
| 4384 | 0, /* tp_itemsize */ |
| 4385 | /* methods */ |
| 4386 | (destructor)zip_longest_dealloc, /* tp_dealloc */ |
| 4387 | 0, /* tp_print */ |
| 4388 | 0, /* tp_getattr */ |
| 4389 | 0, /* tp_setattr */ |
| 4390 | 0, /* tp_reserved */ |
| 4391 | 0, /* tp_repr */ |
| 4392 | 0, /* tp_as_number */ |
| 4393 | 0, /* tp_as_sequence */ |
| 4394 | 0, /* tp_as_mapping */ |
| 4395 | 0, /* tp_hash */ |
| 4396 | 0, /* tp_call */ |
| 4397 | 0, /* tp_str */ |
| 4398 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4399 | 0, /* tp_setattro */ |
| 4400 | 0, /* tp_as_buffer */ |
| 4401 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 4402 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4403 | zip_longest_doc, /* tp_doc */ |
| 4404 | (traverseproc)zip_longest_traverse, /* tp_traverse */ |
| 4405 | 0, /* tp_clear */ |
| 4406 | 0, /* tp_richcompare */ |
| 4407 | 0, /* tp_weaklistoffset */ |
| 4408 | PyObject_SelfIter, /* tp_iter */ |
| 4409 | (iternextfunc)zip_longest_next, /* tp_iternext */ |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4410 | zip_longest_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | 0, /* tp_members */ |
| 4412 | 0, /* tp_getset */ |
| 4413 | 0, /* tp_base */ |
| 4414 | 0, /* tp_dict */ |
| 4415 | 0, /* tp_descr_get */ |
| 4416 | 0, /* tp_descr_set */ |
| 4417 | 0, /* tp_dictoffset */ |
| 4418 | 0, /* tp_init */ |
| 4419 | 0, /* tp_alloc */ |
| 4420 | zip_longest_new, /* tp_new */ |
| 4421 | PyObject_GC_Del, /* tp_free */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 4422 | }; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4423 | |
| 4424 | /* module level code ********************************************************/ |
| 4425 | |
| 4426 | PyDoc_STRVAR(module_doc, |
| 4427 | "Functional tools for creating and using iterators.\n\ |
| 4428 | \n\ |
| 4429 | Infinite iterators:\n\ |
| 4430 | count([n]) --> n, n+1, n+2, ...\n\ |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 4431 | cycle(p) --> p0, p1, ... plast, p0, p1, ...\n\ |
Andrew M. Kuchling | dff694b | 2003-04-14 15:31:27 +0000 | [diff] [blame] | 4432 | 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] | 4433 | \n\ |
| 4434 | Iterators terminating on the shortest input sequence:\n\ |
Raymond Hettinger | 5bf7091 | 2011-03-27 18:59:51 -0700 | [diff] [blame] | 4435 | accumulate(p[, func]) --> p0, p0+p1, p0+p1+p2\n\ |
Raymond Hettinger | 3522a58 | 2009-01-29 03:41:55 +0000 | [diff] [blame] | 4436 | chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... \n\ |
| 4437 | compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...\n\ |
| 4438 | dropwhile(pred, seq) --> seq[n], seq[n+1], starting when pred fails\n\ |
| 4439 | groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)\n\ |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 4440 | filterfalse(pred, seq) --> elements of seq where pred(elem) is False\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4441 | islice(seq, [start,] stop [, step]) --> elements from\n\ |
| 4442 | seq[start:stop:step]\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4443 | starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...\n\ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 4444 | tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4445 | takewhile(pred, seq) --> seq[0], seq[1], until pred fails\n\ |
Raymond Hettinger | 3522a58 | 2009-01-29 03:41:55 +0000 | [diff] [blame] | 4446 | zip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... \n\ |
Raymond Hettinger | 811f3dc | 2009-01-29 03:48:02 +0000 | [diff] [blame] | 4447 | \n\ |
| 4448 | Combinatoric generators:\n\ |
| 4449 | product(p, q, ... [repeat=1]) --> cartesian product\n\ |
| 4450 | permutations(p[, r])\n\ |
Raymond Hettinger | 36c3c02 | 2009-11-19 01:20:07 +0000 | [diff] [blame] | 4451 | combinations(p, r)\n\ |
| 4452 | combinations_with_replacement(p, r)\n\ |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4453 | "); |
| 4454 | |
| 4455 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 4456 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | {"tee", (PyCFunction)tee, METH_VARARGS, tee_doc}, |
| 4458 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 4459 | }; |
| 4460 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4461 | |
| 4462 | static struct PyModuleDef itertoolsmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | PyModuleDef_HEAD_INIT, |
| 4464 | "itertools", |
| 4465 | module_doc, |
| 4466 | -1, |
| 4467 | module_methods, |
| 4468 | NULL, |
| 4469 | NULL, |
| 4470 | NULL, |
| 4471 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4472 | }; |
| 4473 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4474 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 4475 | PyInit_itertools(void) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4476 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | int i; |
| 4478 | PyObject *m; |
| 4479 | char *name; |
| 4480 | PyTypeObject *typelist[] = { |
Raymond Hettinger | 482ba77 | 2010-12-01 22:48:00 +0000 | [diff] [blame] | 4481 | &accumulate_type, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | &combinations_type, |
| 4483 | &cwr_type, |
| 4484 | &cycle_type, |
| 4485 | &dropwhile_type, |
| 4486 | &takewhile_type, |
| 4487 | &islice_type, |
| 4488 | &starmap_type, |
| 4489 | &chain_type, |
| 4490 | &compress_type, |
| 4491 | &filterfalse_type, |
| 4492 | &count_type, |
| 4493 | &ziplongest_type, |
| 4494 | &permutations_type, |
| 4495 | &product_type, |
| 4496 | &repeat_type, |
| 4497 | &groupby_type, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4498 | &_grouper_type, |
| 4499 | &tee_type, |
| 4500 | &teedataobject_type, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | NULL |
| 4502 | }; |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 4503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | Py_TYPE(&teedataobject_type) = &PyType_Type; |
| 4505 | m = PyModule_Create(&itertoolsmodule); |
| 4506 | if (m == NULL) |
| 4507 | return NULL; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4509 | for (i=0 ; typelist[i] != NULL ; i++) { |
| 4510 | if (PyType_Ready(typelist[i]) < 0) |
| 4511 | return NULL; |
| 4512 | name = strchr(typelist[i]->tp_name, '.'); |
| 4513 | assert (name != NULL); |
| 4514 | Py_INCREF(typelist[i]); |
| 4515 | PyModule_AddObject(m, name+1, (PyObject *)typelist[i]); |
| 4516 | } |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 4517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | return m; |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 4519 | } |