blob: 08c7c096cad227eedfdd45178dd94455cf08505f [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 */
Guido van Rossum8ce8a782007-11-01 19:42:39 +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;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +000018 PyObject *start = NULL;
19 static char *kwlist[] = {"sequence", "start", 0};
Guido van Rossum7dab2422002-04-26 19:40:56 +000020
Alexandre Vassalottie9f305f2008-05-16 04:39: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;
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +000028 if (start) {
29 start = PyNumber_Index(start);
30 if (start == NULL) {
31 Py_DECREF(en);
32 return NULL;
33 }
34 en->en_index = LONG_MAX;
35 en->en_longindex = start;
36 } else {
37 en->en_index = 0;
38 en->en_longindex = NULL;
39 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000040 en->en_sit = PyObject_GetIter(seq);
41 if (en->en_sit == NULL) {
42 Py_DECREF(en);
43 return NULL;
44 }
Raymond Hettinger54a831b2003-11-02 05:37:44 +000045 en->en_result = PyTuple_Pack(2, Py_None, Py_None);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000046 if (en->en_result == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +000047 Py_DECREF(en);
48 return NULL;
49 }
Guido van Rossum7dab2422002-04-26 19:40:56 +000050 return (PyObject *)en;
51}
52
53static void
54enum_dealloc(enumobject *en)
55{
56 PyObject_GC_UnTrack(en);
57 Py_XDECREF(en->en_sit);
Raymond Hettingere8b0f042003-05-28 14:05:34 +000058 Py_XDECREF(en->en_result);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000059 Py_XDECREF(en->en_longindex);
Christian Heimes90aa7642007-12-19 02:45:37 +000060 Py_TYPE(en)->tp_free(en);
Guido van Rossum7dab2422002-04-26 19:40:56 +000061}
62
63static int
64enum_traverse(enumobject *en, visitproc visit, void *arg)
65{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066 Py_VISIT(en->en_sit);
67 Py_VISIT(en->en_result);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000068 Py_VISIT(en->en_longindex);
Guido van Rossum7dab2422002-04-26 19:40:56 +000069 return 0;
70}
71
72static PyObject *
Guido van Rossum8ce8a782007-11-01 19:42:39 +000073enum_next_long(enumobject *en, PyObject* next_item)
74{
75 static PyObject *one = NULL;
76 PyObject *result = en->en_result;
77 PyObject *next_index;
78 PyObject *stepped_up;
79
80 if (en->en_longindex == NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +000081 en->en_longindex = PyLong_FromLong(LONG_MAX);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000082 if (en->en_longindex == NULL)
83 return NULL;
84 }
85 if (one == NULL) {
Christian Heimes217cfd12007-12-02 14:31:20 +000086 one = PyLong_FromLong(1);
Guido van Rossum8ce8a782007-11-01 19:42:39 +000087 if (one == NULL)
88 return NULL;
89 }
90 next_index = en->en_longindex;
91 assert(next_index != NULL);
92 stepped_up = PyNumber_Add(next_index, one);
93 if (stepped_up == NULL)
94 return NULL;
95 en->en_longindex = stepped_up;
96
97 if (result->ob_refcnt == 1) {
98 Py_INCREF(result);
99 Py_DECREF(PyTuple_GET_ITEM(result, 0));
100 Py_DECREF(PyTuple_GET_ITEM(result, 1));
101 } else {
102 result = PyTuple_New(2);
103 if (result == NULL) {
104 Py_DECREF(next_index);
105 Py_DECREF(next_item);
106 return NULL;
107 }
108 }
109 PyTuple_SET_ITEM(result, 0, next_index);
110 PyTuple_SET_ITEM(result, 1, next_item);
111 return result;
112}
113
114static PyObject *
Guido van Rossum7dab2422002-04-26 19:40:56 +0000115enum_next(enumobject *en)
116{
Guido van Rossum7dab2422002-04-26 19:40:56 +0000117 PyObject *next_index;
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000118 PyObject *next_item;
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000119 PyObject *result = en->en_result;
120 PyObject *it = en->en_sit;
Guido van Rossum7dab2422002-04-26 19:40:56 +0000121
Christian Heimes90aa7642007-12-19 02:45:37 +0000122 next_item = (*Py_TYPE(it)->tp_iternext)(it);
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000123 if (next_item == NULL)
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000124 return NULL;
125
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000126 if (en->en_index == LONG_MAX)
127 return enum_next_long(en, next_item);
128
Christian Heimes217cfd12007-12-02 14:31:20 +0000129 next_index = PyLong_FromLong(en->en_index);
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000130 if (next_index == NULL) {
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000131 Py_DECREF(next_item);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000132 return NULL;
133 }
Raymond Hettingere8b0f042003-05-28 14:05:34 +0000134 en->en_index++;
135
136 if (result->ob_refcnt == 1) {
137 Py_INCREF(result);
138 Py_DECREF(PyTuple_GET_ITEM(result, 0));
139 Py_DECREF(PyTuple_GET_ITEM(result, 1));
140 } else {
141 result = PyTuple_New(2);
142 if (result == NULL) {
143 Py_DECREF(next_index);
144 Py_DECREF(next_item);
145 return NULL;
146 }
147 }
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000148 PyTuple_SET_ITEM(result, 0, next_index);
Guido van Rossum7dab2422002-04-26 19:40:56 +0000149 PyTuple_SET_ITEM(result, 1, next_item);
150 return result;
151}
152
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000153PyDoc_STRVAR(enum_doc,
Jeremy Hyltonfbbe3472003-04-21 20:26:25 +0000154"enumerate(iterable) -> iterator for index, value of iterable\n"
155"\n"
156"Return an enumerate object. iterable must be an other object that supports\n"
157"iteration. The enumerate object yields pairs containing a count (from\n"
158"zero) and a value yielded by the iterable argument. enumerate is useful\n"
159"for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ...");
Guido van Rossum7dab2422002-04-26 19:40:56 +0000160
161PyTypeObject PyEnum_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000162 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum7dab2422002-04-26 19:40:56 +0000163 "enumerate", /* tp_name */
164 sizeof(enumobject), /* tp_basicsize */
165 0, /* tp_itemsize */
166 /* methods */
167 (destructor)enum_dealloc, /* tp_dealloc */
168 0, /* tp_print */
169 0, /* tp_getattr */
170 0, /* tp_setattr */
171 0, /* tp_compare */
172 0, /* tp_repr */
173 0, /* tp_as_number */
174 0, /* tp_as_sequence */
175 0, /* tp_as_mapping */
176 0, /* tp_hash */
177 0, /* tp_call */
178 0, /* tp_str */
179 PyObject_GenericGetAttr, /* tp_getattro */
180 0, /* tp_setattro */
181 0, /* tp_as_buffer */
182 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
183 Py_TPFLAGS_BASETYPE, /* tp_flags */
184 enum_doc, /* tp_doc */
185 (traverseproc)enum_traverse, /* tp_traverse */
186 0, /* tp_clear */
187 0, /* tp_richcompare */
188 0, /* tp_weaklistoffset */
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000189 PyObject_SelfIter, /* tp_iter */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000190 (iternextfunc)enum_next, /* tp_iternext */
Guido van Rossumca5ed5b2002-07-16 21:02:42 +0000191 0, /* tp_methods */
Guido van Rossum7dab2422002-04-26 19:40:56 +0000192 0, /* tp_members */
193 0, /* tp_getset */
194 0, /* tp_base */
195 0, /* tp_dict */
196 0, /* tp_descr_get */
197 0, /* tp_descr_set */
198 0, /* tp_dictoffset */
199 0, /* tp_init */
200 PyType_GenericAlloc, /* tp_alloc */
201 enum_new, /* tp_new */
202 PyObject_GC_Del, /* tp_free */
203};
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000204
205/* Reversed Object ***************************************************************/
206
207typedef struct {
208 PyObject_HEAD
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000209 Py_ssize_t index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000210 PyObject* seq;
211} reversedobject;
212
213static PyObject *
214reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
215{
Martin v. Löwiseb079f12006-02-16 14:32:27 +0000216 Py_ssize_t n;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000217 PyObject *seq;
218 reversedobject *ro;
219
220 if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq))
221 return NULL;
222
Raymond Hettingerc058fd12004-02-07 02:45:22 +0000223 if (PyObject_HasAttrString(seq, "__reversed__"))
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000224 return PyObject_CallMethod(seq, "__reversed__", NULL);
225
226 if (!PySequence_Check(seq)) {
227 PyErr_SetString(PyExc_TypeError,
228 "argument to reversed() must be a sequence");
229 return NULL;
230 }
231
232 n = PySequence_Size(seq);
233 if (n == -1)
234 return NULL;
235
236 ro = (reversedobject *)type->tp_alloc(type, 0);
237 if (ro == NULL)
238 return NULL;
239
240 ro->index = n-1;
241 Py_INCREF(seq);
242 ro->seq = seq;
243 return (PyObject *)ro;
244}
245
246static void
247reversed_dealloc(reversedobject *ro)
248{
249 PyObject_GC_UnTrack(ro);
250 Py_XDECREF(ro->seq);
Christian Heimes90aa7642007-12-19 02:45:37 +0000251 Py_TYPE(ro)->tp_free(ro);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000252}
253
254static int
255reversed_traverse(reversedobject *ro, visitproc visit, void *arg)
256{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 Py_VISIT(ro->seq);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000258 return 0;
259}
260
261static PyObject *
262reversed_next(reversedobject *ro)
263{
264 PyObject *item;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000265 Py_ssize_t index = ro->index;
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000266
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000267 if (index >= 0) {
268 item = PySequence_GetItem(ro->seq, index);
269 if (item != NULL) {
270 ro->index--;
271 return item;
272 }
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000273 if (PyErr_ExceptionMatches(PyExc_IndexError) ||
274 PyErr_ExceptionMatches(PyExc_StopIteration))
275 PyErr_Clear();
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000276 }
277 ro->index = -1;
Raymond Hettinger75ccea32004-09-01 07:02:44 +0000278 Py_CLEAR(ro->seq);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000279 return NULL;
Raymond Hettinger029dba52004-02-10 09:33:39 +0000280}
281
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000282PyDoc_STRVAR(reversed_doc,
Raymond Hettingerd2afee42004-08-25 19:42:12 +0000283"reversed(sequence) -> reverse iterator over values of the sequence\n"
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000284"\n"
285"Return a reverse iterator");
286
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000287static PyObject *
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000288reversed_len(reversedobject *ro)
289{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000290 Py_ssize_t position, seqsize;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000291
292 if (ro->seq == NULL)
Christian Heimes217cfd12007-12-02 14:31:20 +0000293 return PyLong_FromLong(0);
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000294 seqsize = PySequence_Size(ro->seq);
295 if (seqsize == -1)
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000296 return NULL;
Raymond Hettinger7892b1c2004-04-12 18:10:01 +0000297 position = ro->index + 1;
Christian Heimes217cfd12007-12-02 14:31:20 +0000298 return PyLong_FromSsize_t((seqsize < position) ? 0 : position);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000299}
300
Armin Rigof5b3e362006-02-11 21:32:43 +0000301PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000302
303static PyMethodDef reversediter_methods[] = {
Armin Rigof5b3e362006-02-11 21:32:43 +0000304 {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc},
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000305 {NULL, NULL} /* sentinel */
Raymond Hettinger06353f72004-02-08 10:49:42 +0000306};
307
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000308PyTypeObject PyReversed_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000309 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000310 "reversed", /* tp_name */
311 sizeof(reversedobject), /* tp_basicsize */
312 0, /* tp_itemsize */
313 /* methods */
314 (destructor)reversed_dealloc, /* tp_dealloc */
315 0, /* tp_print */
316 0, /* tp_getattr */
317 0, /* tp_setattr */
318 0, /* tp_compare */
319 0, /* tp_repr */
320 0, /* tp_as_number */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000321 0, /* tp_as_sequence */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000322 0, /* tp_as_mapping */
323 0, /* tp_hash */
324 0, /* tp_call */
325 0, /* tp_str */
326 PyObject_GenericGetAttr, /* tp_getattro */
327 0, /* tp_setattro */
328 0, /* tp_as_buffer */
329 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
330 Py_TPFLAGS_BASETYPE, /* tp_flags */
331 reversed_doc, /* tp_doc */
332 (traverseproc)reversed_traverse,/* tp_traverse */
333 0, /* tp_clear */
334 0, /* tp_richcompare */
335 0, /* tp_weaklistoffset */
336 PyObject_SelfIter, /* tp_iter */
337 (iternextfunc)reversed_next, /* tp_iternext */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000338 reversediter_methods, /* tp_methods */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000339 0, /* tp_members */
340 0, /* tp_getset */
341 0, /* tp_base */
342 0, /* tp_dict */
343 0, /* tp_descr_get */
344 0, /* tp_descr_set */
345 0, /* tp_dictoffset */
346 0, /* tp_init */
347 PyType_GenericAlloc, /* tp_alloc */
348 reversed_new, /* tp_new */
349 PyObject_GC_Del, /* tp_free */
350};