Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 1 | /* enumerate object */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
| 5 | typedef struct { |
| 6 | PyObject_HEAD |
| 7 | long en_index; /* current index of enumeration */ |
| 8 | PyObject* en_sit; /* secondary iterator of enumeration */ |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 9 | PyObject* en_result; /* result tuple */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 10 | } enumobject; |
| 11 | |
| 12 | PyTypeObject PyEnum_Type; |
| 13 | |
| 14 | static PyObject * |
| 15 | enum_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 16 | { |
| 17 | enumobject *en; |
| 18 | PyObject *seq = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 19 | static char *kwlist[] = {"sequence", 0}; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 20 | |
| 21 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:enumerate", kwlist, |
| 22 | &seq)) |
| 23 | return NULL; |
| 24 | |
| 25 | en = (enumobject *)type->tp_alloc(type, 0); |
| 26 | if (en == NULL) |
| 27 | return NULL; |
| 28 | en->en_index = 0; |
| 29 | en->en_sit = PyObject_GetIter(seq); |
| 30 | if (en->en_sit == NULL) { |
| 31 | Py_DECREF(en); |
| 32 | return NULL; |
| 33 | } |
Raymond Hettinger | 54a831b | 2003-11-02 05:37:44 +0000 | [diff] [blame] | 34 | en->en_result = PyTuple_Pack(2, Py_None, Py_None); |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 35 | if (en->en_result == NULL) { |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 36 | Py_DECREF(en); |
| 37 | return NULL; |
| 38 | } |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 39 | return (PyObject *)en; |
| 40 | } |
| 41 | |
| 42 | static void |
| 43 | enum_dealloc(enumobject *en) |
| 44 | { |
| 45 | PyObject_GC_UnTrack(en); |
| 46 | Py_XDECREF(en->en_sit); |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 47 | Py_XDECREF(en->en_result); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 48 | en->ob_type->tp_free(en); |
| 49 | } |
| 50 | |
| 51 | static int |
| 52 | enum_traverse(enumobject *en, visitproc visit, void *arg) |
| 53 | { |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 54 | int err; |
| 55 | |
| 56 | if (en->en_sit) { |
| 57 | err = visit(en->en_sit, arg); |
| 58 | if (err) |
| 59 | return err; |
| 60 | } |
| 61 | if (en->en_result) { |
| 62 | err = visit(en->en_result, arg); |
| 63 | if (err) |
| 64 | return err; |
| 65 | } |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static PyObject * |
| 70 | enum_next(enumobject *en) |
| 71 | { |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 72 | PyObject *next_index; |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 73 | PyObject *next_item; |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 74 | PyObject *result = en->en_result; |
| 75 | PyObject *it = en->en_sit; |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 76 | |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 77 | next_item = (*it->ob_type->tp_iternext)(it); |
| 78 | if (next_item == NULL) |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 79 | return NULL; |
| 80 | |
| 81 | next_index = PyInt_FromLong(en->en_index); |
| 82 | if (next_index == NULL) { |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 83 | Py_DECREF(next_item); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 84 | return NULL; |
| 85 | } |
Raymond Hettinger | e8b0f04 | 2003-05-28 14:05:34 +0000 | [diff] [blame] | 86 | en->en_index++; |
| 87 | |
| 88 | if (result->ob_refcnt == 1) { |
| 89 | Py_INCREF(result); |
| 90 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 91 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 92 | } else { |
| 93 | result = PyTuple_New(2); |
| 94 | if (result == NULL) { |
| 95 | Py_DECREF(next_index); |
| 96 | Py_DECREF(next_item); |
| 97 | return NULL; |
| 98 | } |
| 99 | } |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 100 | PyTuple_SET_ITEM(result, 0, next_index); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 101 | PyTuple_SET_ITEM(result, 1, next_item); |
| 102 | return result; |
| 103 | } |
| 104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 105 | PyDoc_STRVAR(enum_doc, |
Jeremy Hylton | fbbe347 | 2003-04-21 20:26:25 +0000 | [diff] [blame] | 106 | "enumerate(iterable) -> iterator for index, value of iterable\n" |
| 107 | "\n" |
| 108 | "Return an enumerate object. iterable must be an other object that supports\n" |
| 109 | "iteration. The enumerate object yields pairs containing a count (from\n" |
| 110 | "zero) and a value yielded by the iterable argument. enumerate is useful\n" |
| 111 | "for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2, seq[2]), ..."); |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 112 | |
| 113 | PyTypeObject PyEnum_Type = { |
| 114 | PyObject_HEAD_INIT(&PyType_Type) |
| 115 | 0, /* ob_size */ |
| 116 | "enumerate", /* tp_name */ |
| 117 | sizeof(enumobject), /* tp_basicsize */ |
| 118 | 0, /* tp_itemsize */ |
| 119 | /* methods */ |
| 120 | (destructor)enum_dealloc, /* tp_dealloc */ |
| 121 | 0, /* tp_print */ |
| 122 | 0, /* tp_getattr */ |
| 123 | 0, /* tp_setattr */ |
| 124 | 0, /* tp_compare */ |
| 125 | 0, /* tp_repr */ |
| 126 | 0, /* tp_as_number */ |
| 127 | 0, /* tp_as_sequence */ |
| 128 | 0, /* tp_as_mapping */ |
| 129 | 0, /* tp_hash */ |
| 130 | 0, /* tp_call */ |
| 131 | 0, /* tp_str */ |
| 132 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 133 | 0, /* tp_setattro */ |
| 134 | 0, /* tp_as_buffer */ |
| 135 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 136 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 137 | enum_doc, /* tp_doc */ |
| 138 | (traverseproc)enum_traverse, /* tp_traverse */ |
| 139 | 0, /* tp_clear */ |
| 140 | 0, /* tp_richcompare */ |
| 141 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 142 | PyObject_SelfIter, /* tp_iter */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 143 | (iternextfunc)enum_next, /* tp_iternext */ |
Guido van Rossum | ca5ed5b | 2002-07-16 21:02:42 +0000 | [diff] [blame] | 144 | 0, /* tp_methods */ |
Guido van Rossum | 7dab242 | 2002-04-26 19:40:56 +0000 | [diff] [blame] | 145 | 0, /* tp_members */ |
| 146 | 0, /* tp_getset */ |
| 147 | 0, /* tp_base */ |
| 148 | 0, /* tp_dict */ |
| 149 | 0, /* tp_descr_get */ |
| 150 | 0, /* tp_descr_set */ |
| 151 | 0, /* tp_dictoffset */ |
| 152 | 0, /* tp_init */ |
| 153 | PyType_GenericAlloc, /* tp_alloc */ |
| 154 | enum_new, /* tp_new */ |
| 155 | PyObject_GC_Del, /* tp_free */ |
| 156 | }; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 157 | |
| 158 | /* Reversed Object ***************************************************************/ |
| 159 | |
| 160 | typedef struct { |
| 161 | PyObject_HEAD |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 162 | Py_ssize_t index; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 163 | PyObject* seq; |
| 164 | } reversedobject; |
| 165 | |
| 166 | static PyObject * |
| 167 | reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 168 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 169 | Py_ssize_t n; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 170 | PyObject *seq; |
| 171 | reversedobject *ro; |
| 172 | |
| 173 | if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq)) |
| 174 | return NULL; |
| 175 | |
Raymond Hettinger | c058fd1 | 2004-02-07 02:45:22 +0000 | [diff] [blame] | 176 | if (PyObject_HasAttrString(seq, "__reversed__")) |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 177 | return PyObject_CallMethod(seq, "__reversed__", NULL); |
| 178 | |
| 179 | if (!PySequence_Check(seq)) { |
| 180 | PyErr_SetString(PyExc_TypeError, |
| 181 | "argument to reversed() must be a sequence"); |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | n = PySequence_Size(seq); |
| 186 | if (n == -1) |
| 187 | return NULL; |
| 188 | |
| 189 | ro = (reversedobject *)type->tp_alloc(type, 0); |
| 190 | if (ro == NULL) |
| 191 | return NULL; |
| 192 | |
| 193 | ro->index = n-1; |
| 194 | Py_INCREF(seq); |
| 195 | ro->seq = seq; |
| 196 | return (PyObject *)ro; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | reversed_dealloc(reversedobject *ro) |
| 201 | { |
| 202 | PyObject_GC_UnTrack(ro); |
| 203 | Py_XDECREF(ro->seq); |
| 204 | ro->ob_type->tp_free(ro); |
| 205 | } |
| 206 | |
| 207 | static int |
| 208 | reversed_traverse(reversedobject *ro, visitproc visit, void *arg) |
| 209 | { |
| 210 | if (ro->seq) |
| 211 | return visit((PyObject *)(ro->seq), arg); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static PyObject * |
| 216 | reversed_next(reversedobject *ro) |
| 217 | { |
| 218 | PyObject *item; |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 219 | long index = ro->index; |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 220 | |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 221 | if (index >= 0) { |
| 222 | item = PySequence_GetItem(ro->seq, index); |
| 223 | if (item != NULL) { |
| 224 | ro->index--; |
| 225 | return item; |
| 226 | } |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 227 | if (PyErr_ExceptionMatches(PyExc_IndexError) || |
| 228 | PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 229 | PyErr_Clear(); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 230 | } |
| 231 | ro->index = -1; |
Raymond Hettinger | 75ccea3 | 2004-09-01 07:02:44 +0000 | [diff] [blame] | 232 | Py_CLEAR(ro->seq); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 233 | return NULL; |
Raymond Hettinger | 029dba5 | 2004-02-10 09:33:39 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 236 | PyDoc_STRVAR(reversed_doc, |
Raymond Hettinger | d2afee4 | 2004-08-25 19:42:12 +0000 | [diff] [blame] | 237 | "reversed(sequence) -> reverse iterator over values of the sequence\n" |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 238 | "\n" |
| 239 | "Return a reverse iterator"); |
| 240 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 241 | static PyObject * |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 242 | reversed_len(reversedobject *ro) |
| 243 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 244 | Py_ssize_t position, seqsize; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 245 | |
| 246 | if (ro->seq == NULL) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 247 | return PyInt_FromLong(0); |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 248 | seqsize = PySequence_Size(ro->seq); |
| 249 | if (seqsize == -1) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 250 | return NULL; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 251 | position = ro->index + 1; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 252 | return PyInt_FromSsize_t((seqsize < position) ? 0 : position); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 255 | 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] | 256 | |
| 257 | static PyMethodDef reversediter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 258 | {"__length_hint__", (PyCFunction)reversed_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 259 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 06353f7 | 2004-02-08 10:49:42 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 262 | PyTypeObject PyReversed_Type = { |
| 263 | PyObject_HEAD_INIT(&PyType_Type) |
| 264 | 0, /* ob_size */ |
| 265 | "reversed", /* tp_name */ |
| 266 | sizeof(reversedobject), /* tp_basicsize */ |
| 267 | 0, /* tp_itemsize */ |
| 268 | /* methods */ |
| 269 | (destructor)reversed_dealloc, /* tp_dealloc */ |
| 270 | 0, /* tp_print */ |
| 271 | 0, /* tp_getattr */ |
| 272 | 0, /* tp_setattr */ |
| 273 | 0, /* tp_compare */ |
| 274 | 0, /* tp_repr */ |
| 275 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 276 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 277 | 0, /* tp_as_mapping */ |
| 278 | 0, /* tp_hash */ |
| 279 | 0, /* tp_call */ |
| 280 | 0, /* tp_str */ |
| 281 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 282 | 0, /* tp_setattro */ |
| 283 | 0, /* tp_as_buffer */ |
| 284 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 285 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 286 | reversed_doc, /* tp_doc */ |
| 287 | (traverseproc)reversed_traverse,/* tp_traverse */ |
| 288 | 0, /* tp_clear */ |
| 289 | 0, /* tp_richcompare */ |
| 290 | 0, /* tp_weaklistoffset */ |
| 291 | PyObject_SelfIter, /* tp_iter */ |
| 292 | (iternextfunc)reversed_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 293 | reversediter_methods, /* tp_methods */ |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 294 | 0, /* tp_members */ |
| 295 | 0, /* tp_getset */ |
| 296 | 0, /* tp_base */ |
| 297 | 0, /* tp_dict */ |
| 298 | 0, /* tp_descr_get */ |
| 299 | 0, /* tp_descr_set */ |
| 300 | 0, /* tp_dictoffset */ |
| 301 | 0, /* tp_init */ |
| 302 | PyType_GenericAlloc, /* tp_alloc */ |
| 303 | reversed_new, /* tp_new */ |
| 304 | PyObject_GC_Del, /* tp_free */ |
| 305 | }; |