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