blob: dfa738d503a8452b55e35fa48ff2c82fbe4837da [file] [log] [blame]
Guido van Rossum7dab2422002-04-26 19:40:56 +00001/* enumerate object */
2
3#include "Python.h"
4
5typedef struct {
6 PyObject_HEAD
7 long en_index; /* current index of enumeration */
8 PyObject* en_sit; /* secondary iterator of enumeration */
Raymond Hettingere8b0f042003-05-28 14:05:34 +00009 PyObject* en_result; /* result tuple */
Raymond Hettinger8f669372007-10-03 21:18:11 +000010 PyObject* en_longindex; /* index for sequences >= LONG_MAX */
Guido van Rossum7dab2422002-04-26 19:40:56 +000011} enumobject;
12
Guido van Rossum7dab2422002-04-26 19:40:56 +000013static PyObject *
14enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
15{
16 enumobject *en;
17 PyObject *seq = NULL;
Georg Brandl91383572008-05-13 19:04:54 +000018 PyObject *start = NULL;
19 static char *kwlist[] = {"sequence", "start", 0};
Guido van Rossum7dab2422002-04-26 19:40:56 +000020
Georg Brandl91383572008-05-13 19:04:54 +000021 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:enumerate", kwlist,
22 &seq, &start))
Guido van Rossum7dab2422002-04-26 19:40:56 +000023 return NULL;
24
25 en = (enumobject *)type->tp_alloc(type, 0);
26 if (en == NULL)
27 return NULL;
Georg Brandl91383572008-05-13 19:04:54 +000028 if (start) {
29 start = PyNumber_Index(start);
30 if (start == NULL) {
31 Py_DECREF(en);
32 return NULL;
33 }
34 if (PyLong_Check(start)) {
35 en->en_index = LONG_MAX;
36 en->en_longindex = start;
37 } else {
38 assert(PyInt_Check(start));
39 en->en_index = PyInt_AsLong(start);
40 en->en_longindex = NULL;
41 Py_DECREF(start);
42 }
43 } else {
44 en->en_index = 0;
45 en->en_longindex = NULL;
46 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000047 en->en_sit = PyObject_GetIter(seq);
48 if (en->en_sit == NULL) {
49 Py_DECREF(en);
50 return NULL;
51 }
Raymond Hettinger54a831b2003-11-02 05:37:44 +000052 en->en_result = PyTuple_Pack(2, Py_None, Py_None);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000053 if (en->en_result == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +000054 Py_DECREF(en);
55 return NULL;
56 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000057 return (PyObject *)en;
58}
59
60static void
61enum_dealloc(enumobject *en)
62{
63 PyObject_GC_UnTrack(en);
64 Py_XDECREF(en->en_sit);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000065 Py_XDECREF(en->en_result);
Raymond Hettinger8f669372007-10-03 21:18:11 +000066 Py_XDECREF(en->en_longindex);
Christian Heimese93237d2007-12-19 02:37:44 +000067 Py_TYPE(en)->tp_free(en);
Guido van Rossum7dab2422002-04-26 19:40:56 +000068}
69
70static int
71enum_traverse(enumobject *en, visitproc visit, void *arg)
72{
Thomas Woutersc6e55062006-04-15 21:47:09 +000073 Py_VISIT(en->en_sit);
74 Py_VISIT(en->en_result);
Raymond Hettinger8f669372007-10-03 21:18:11 +000075 Py_VISIT(en->en_longindex);
Guido van Rossum7dab2422002-04-26 19:40:56 +000076 return 0;
77}
78
79static PyObject *
Raymond Hettinger8f669372007-10-03 21:18:11 +000080enum_next_long(enumobject *en, PyObject* next_item)
81{
82 static PyObject *one = NULL;
83 PyObject *result = en->en_result;
84 PyObject *next_index;
85 PyObject *stepped_up;
86
87 if (en->en_longindex == NULL) {
88 en->en_longindex = PyInt_FromLong(LONG_MAX);
89 if (en->en_longindex == NULL)
90 return NULL;
91 }
92 if (one == NULL) {
93 one = PyInt_FromLong(1);
94 if (one == NULL)
95 return NULL;
96 }
97 next_index = en->en_longindex;
98 assert(next_index != NULL);
99 stepped_up = PyNumber_Add(next_index, one);
100 if (stepped_up == NULL)
101 return NULL;
102 en->en_longindex = stepped_up;
103
104 if (result->ob_refcnt == 1) {
105 Py_INCREF(result);
106 Py_DECREF(PyTuple_GET_ITEM(result, 0));
107 Py_DECREF(PyTuple_GET_ITEM(result, 1));
108 } else {
109 result = PyTuple_New(2);
110 if (result == NULL) {
111 Py_DECREF(next_index);
112 Py_DECREF(next_item);
113 return NULL;
114 }
115 }
116 PyTuple_SET_ITEM(result, 0, next_index);
117 PyTuple_SET_ITEM(result, 1, next_item);
118 return result;
119}
120
121static PyObject *
Guido van Rossum7dab2422002-04-26 19:40:56 +0000122enum_next(enumobject *en)
123{
Guido van Rossum7dab2422002-04-26 19:40:56 +0000124 PyObject *next_index;
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000125 PyObject *next_item;
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000126 PyObject *result = en->en_result;
127 PyObject *it = en->en_sit;
Guido van Rossum7dab2422002-04-26 19:40:56 +0000128
Christian Heimese93237d2007-12-19 02:37:44 +0000129 next_item = (*Py_TYPE(it)->tp_iternext)(it);
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000130 if (next_item == NULL)
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000131 return NULL;
132
Raymond Hettinger8f669372007-10-03 21:18:11 +0000133 if (en->en_index == LONG_MAX)
134 return enum_next_long(en, next_item);
135
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000136 next_index = PyInt_FromLong(en->en_index);
137 if (next_index == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000138 Py_DECREF(next_item);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000139 return NULL;
140 }
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000141 en->en_index++;
142
143 if (result->ob_refcnt == 1) {
144 Py_INCREF(result);
145 Py_DECREF(PyTuple_GET_ITEM(result, 0));
146 Py_DECREF(PyTuple_GET_ITEM(result, 1));
147 } else {
148 result = PyTuple_New(2);
149 if (result == NULL) {
150 Py_DECREF(next_index);
151 Py_DECREF(next_item);
152 return NULL;
153 }
154 }
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000155 PyTuple_SET_ITEM(result, 0, next_index);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000156 PyTuple_SET_ITEM(result, 1, next_item);
157 return result;
158}
159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000160PyDoc_STRVAR(enum_doc,
Jeremy Hyltonfbbe3472003-04-21 20:26:25 +0000161"enumerate(iterable) -> iterator for index, value of iterable\n"
162"\n"
163"Return an enumerate object. iterable must be an other object that supports\n"
164"iteration. The enumerate object yields pairs containing a count (from\n"
165"zero) and a value yielded by the iterable argument. enumerate is useful\n"
166"for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...");
Guido van Rossum7dab2422002-04-26 19:40:56 +0000167
168PyTypeObject PyEnum_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000169 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum7dab2422002-04-26 19:40:56 +0000170 "enumerate", /* tp_name */
171 sizeof(enumobject), /* tp_basicsize */
172 0, /* tp_itemsize */
173 /* methods */
174 (destructor)enum_dealloc, /* tp_dealloc */
175 0, /* tp_print */
176 0, /* tp_getattr */
177 0, /* tp_setattr */
178 0, /* tp_compare */
179 0, /* tp_repr */
180 0, /* tp_as_number */
181 0, /* tp_as_sequence */
182 0, /* tp_as_mapping */
183 0, /* tp_hash */
184 0, /* tp_call */
185 0, /* tp_str */
186 PyObject_GenericGetAttr, /* tp_getattro */
187 0, /* tp_setattro */
188 0, /* tp_as_buffer */
189 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
190 Py_TPFLAGS_BASETYPE, /* tp_flags */
191 enum_doc, /* tp_doc */
192 (traverseproc)enum_traverse, /* tp_traverse */
193 0, /* tp_clear */
194 0, /* tp_richcompare */
195 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000196 PyObject_SelfIter, /* tp_iter */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000197 (iternextfunc)enum_next, /* tp_iternext */
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000198 0, /* tp_methods */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000199 0, /* tp_members */
200 0, /* tp_getset */
201 0, /* tp_base */
202 0, /* tp_dict */
203 0, /* tp_descr_get */
204 0, /* tp_descr_set */
205 0, /* tp_dictoffset */
206 0, /* tp_init */
207 PyType_GenericAlloc, /* tp_alloc */
208 enum_new, /* tp_new */
209 PyObject_GC_Del, /* tp_free */
210};
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000211
212/* Reversed Object ***************************************************************/
213
214typedef struct {
215 PyObject_HEAD
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000216 Py_ssize_t index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000217 PyObject* seq;
218} reversedobject;
219
220static PyObject *
221reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
222{
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000223 Py_ssize_t n;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000224 PyObject *seq;
225 reversedobject *ro;
226
Georg Brandlecf90912008-05-16 13:24:29 +0000227 if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
228 return NULL;
229
230 if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000231 return NULL;
232
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000233 if (PyObject_HasAttrString(seq, "__reversed__"))
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000234 return PyObject_CallMethod(seq, "__reversed__", NULL);
235
236 if (!PySequence_Check(seq)) {
237 PyErr_SetString(PyExc_TypeError,
238 "argument to reversed() must be a sequence");
239 return NULL;
240 }
241
242 n = PySequence_Size(seq);
243 if (n == -1)
244 return NULL;
245
246 ro = (reversedobject *)type->tp_alloc(type, 0);
247 if (ro == NULL)
248 return NULL;
249
250 ro->index = n-1;
251 Py_INCREF(seq);
252 ro->seq = seq;
253 return (PyObject *)ro;
254}
255
256static void
257reversed_dealloc(reversedobject *ro)
258{
259 PyObject_GC_UnTrack(ro);
260 Py_XDECREF(ro->seq);
Christian Heimese93237d2007-12-19 02:37:44 +0000261 Py_TYPE(ro)->tp_free(ro);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000262}
263
264static int
265reversed_traverse(reversedobject *ro, visitproc visit, void *arg)
266{
Thomas Woutersc6e55062006-04-15 21:47:09 +0000267 Py_VISIT(ro->seq);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000268 return 0;
269}
270
271static PyObject *
272reversed_next(reversedobject *ro)
273{
274 PyObject *item;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000275 Py_ssize_t index = ro->index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000276
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000277 if (index >= 0) {
278 item = PySequence_GetItem(ro->seq, index);
279 if (item != NULL) {
280 ro->index--;
281 return item;
282 }
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000283 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
284 PyErr_ExceptionMatches(PyExc_StopIteration))
285 PyErr_Clear();
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000286 }
287 ro->index = -1;
Raymond Hettinger75ccea32004-09-01 07:02:44 +0000288 Py_CLEAR(ro->seq);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000289 return NULL;
Raymond Hettinger029dba52004-02-10 09:33:39 +0000290}
291
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000292PyDoc_STRVAR(reversed_doc,
Raymond Hettingerd2afee42004-08-25 19:42:12 +0000293"reversed(sequence) -> reverse iterator over values of the sequence\n"
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000294"\n"
295"Return a reverse iterator");
296
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000297static PyObject *
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000298reversed_len(reversedobject *ro)
299{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000300 Py_ssize_t position, seqsize;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000301
302 if (ro->seq == NULL)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000303 return PyInt_FromLong(0);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000304 seqsize = PySequence_Size(ro->seq);
305 if (seqsize == -1)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000306 return NULL;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000307 position = ro->index + 1;
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000308 return PyInt_FromSsize_t((seqsize < position) ? 0 : position);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000309}
310
Armin Rigof5b3e362006-02-11 21:32:43 +0000311PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000312
313static PyMethodDef reversediter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000314 {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000315 {NULL, NULL} /* sentinel */
Raymond Hettinger06353f72004-02-08 10:49:42 +0000316};
317
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000318PyTypeObject PyReversed_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000319 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000320 "reversed", /* tp_name */
321 sizeof(reversedobject), /* tp_basicsize */
322 0, /* tp_itemsize */
323 /* methods */
324 (destructor)reversed_dealloc, /* tp_dealloc */
325 0, /* tp_print */
326 0, /* tp_getattr */
327 0, /* tp_setattr */
328 0, /* tp_compare */
329 0, /* tp_repr */
330 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000331 0, /* tp_as_sequence */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000332 0, /* tp_as_mapping */
333 0, /* tp_hash */
334 0, /* tp_call */
335 0, /* tp_str */
336 PyObject_GenericGetAttr, /* tp_getattro */
337 0, /* tp_setattro */
338 0, /* tp_as_buffer */
339 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
340 Py_TPFLAGS_BASETYPE, /* tp_flags */
341 reversed_doc, /* tp_doc */
342 (traverseproc)reversed_traverse,/* tp_traverse */
343 0, /* tp_clear */
344 0, /* tp_richcompare */
345 0, /* tp_weaklistoffset */
346 PyObject_SelfIter, /* tp_iter */
347 (iternextfunc)reversed_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000348 reversediter_methods, /* tp_methods */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000349 0, /* tp_members */
350 0, /* tp_getset */
351 0, /* tp_base */
352 0, /* tp_dict */
353 0, /* tp_descr_get */
354 0, /* tp_descr_set */
355 0, /* tp_dictoffset */
356 0, /* tp_init */
357 PyType_GenericAlloc, /* tp_alloc */
358 reversed_new, /* tp_new */
359 PyObject_GC_Del, /* tp_free */
360};