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