Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1 | /* Range object implementation */ |
| 2 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Thomas Wouters | efafcea | 2001-07-09 12:30:54 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 5 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 6 | PyObject_HEAD |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 7 | long start; |
| 8 | long step; |
| 9 | long len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 10 | } rangeobject; |
| 11 | |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 12 | /* Return number of items in range/xrange (lo, hi, step). step > 0 |
| 13 | * required. Return a value < 0 if & only if the true value is too |
| 14 | * large to fit in a signed long. |
| 15 | */ |
| 16 | static long |
| 17 | get_len_of_range(long lo, long hi, long step) |
| 18 | { |
| 19 | /* ------------------------------------------------------------- |
| 20 | If lo >= hi, the range is empty. |
| 21 | Else if n values are in the range, the last one is |
| 22 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 23 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 24 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 25 | the RHS is non-negative and so truncation is the same as the |
| 26 | floor. Letting M be the largest positive long, the worst case |
| 27 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 28 | hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
| 29 | precision to compute the RHS exactly. |
| 30 | ---------------------------------------------------------------*/ |
| 31 | long n = 0; |
| 32 | if (lo < hi) { |
| 33 | unsigned long uhi = (unsigned long)hi; |
| 34 | unsigned long ulo = (unsigned long)lo; |
| 35 | unsigned long diff = uhi - ulo - 1; |
| 36 | n = (long)(diff / (unsigned long)step + 1); |
| 37 | } |
| 38 | return n; |
| 39 | } |
| 40 | |
| 41 | static PyObject * |
| 42 | range_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 43 | { |
Tim Peters | feec453 | 2004-08-08 07:17:39 +0000 | [diff] [blame] | 44 | rangeobject *obj; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 45 | long ilow = 0, ihigh = 0, istep = 1; |
| 46 | long n; |
| 47 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 48 | if (!_PyArg_NoKeywords("xrange()", kw)) |
| 49 | return NULL; |
| 50 | |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 51 | if (PyTuple_Size(args) <= 1) { |
| 52 | if (!PyArg_ParseTuple(args, |
| 53 | "l;xrange() requires 1-3 int arguments", |
| 54 | &ihigh)) |
| 55 | return NULL; |
| 56 | } |
| 57 | else { |
| 58 | if (!PyArg_ParseTuple(args, |
| 59 | "ll|l;xrange() requires 1-3 int arguments", |
| 60 | &ilow, &ihigh, &istep)) |
| 61 | return NULL; |
| 62 | } |
| 63 | if (istep == 0) { |
| 64 | PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero"); |
| 65 | return NULL; |
| 66 | } |
| 67 | if (istep > 0) |
| 68 | n = get_len_of_range(ilow, ihigh, istep); |
| 69 | else |
| 70 | n = get_len_of_range(ihigh, ilow, -istep); |
| 71 | if (n < 0) { |
| 72 | PyErr_SetString(PyExc_OverflowError, |
| 73 | "xrange() result has too many items"); |
| 74 | return NULL; |
| 75 | } |
Tim Peters | feec453 | 2004-08-08 07:17:39 +0000 | [diff] [blame] | 76 | |
| 77 | obj = PyObject_New(rangeobject, &PyRange_Type); |
| 78 | if (obj == NULL) |
| 79 | return NULL; |
| 80 | obj->start = ilow; |
| 81 | obj->len = n; |
| 82 | obj->step = istep; |
| 83 | return (PyObject *) obj; |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 86 | PyDoc_STRVAR(range_doc, |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 87 | "xrange([start,] stop[, step]) -> xrange object\n\ |
| 88 | \n\ |
| 89 | Like range(), but instead of returning a list, returns an object that\n\ |
Raymond Hettinger | d2bef82 | 2002-12-11 07:14:03 +0000 | [diff] [blame] | 90 | generates the numbers in the range on demand. For looping, this is \n\ |
| 91 | slightly faster than range() and more memory efficient."); |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 94 | range_item(rangeobject *r, Py_ssize_t i) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 95 | { |
Fred Drake | d901832 | 2002-05-02 19:56:55 +0000 | [diff] [blame] | 96 | if (i < 0 || i >= r->len) { |
| 97 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 5dadf7e | 1999-01-09 21:40:35 +0000 | [diff] [blame] | 98 | "xrange object index out of range"); |
Fred Drake | d901832 | 2002-05-02 19:56:55 +0000 | [diff] [blame] | 99 | return NULL; |
| 100 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 101 | return PyInt_FromSsize_t(r->start + (i % r->len) * r->step); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 104 | static Py_ssize_t |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 105 | range_length(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 106 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 107 | return (Py_ssize_t)(r->len); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 110 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 111 | range_repr(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 112 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 113 | PyObject *rtn; |
Tim Peters | d976ab7 | 2004-08-08 06:29:10 +0000 | [diff] [blame] | 114 | |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 115 | if (r->start == 0 && r->step == 1) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 116 | rtn = PyString_FromFormat("xrange(%ld)", |
| 117 | r->start + r->len * r->step); |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 118 | |
| 119 | else if (r->step == 1) |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 120 | rtn = PyString_FromFormat("xrange(%ld, %ld)", |
| 121 | r->start, |
| 122 | r->start + r->len * r->step); |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 123 | |
| 124 | else |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 125 | rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)", |
| 126 | r->start, |
| 127 | r->start + r->len * r->step, |
| 128 | r->step); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 129 | return rtn; |
Thomas Wouters | efafcea | 2001-07-09 12:30:54 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | static PySequenceMethods range_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 133 | (lenfunc)range_length, /* sq_length */ |
Fred Drake | d901832 | 2002-05-02 19:56:55 +0000 | [diff] [blame] | 134 | 0, /* sq_concat */ |
| 135 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 136 | (ssizeargfunc)range_item, /* sq_item */ |
Fred Drake | d901832 | 2002-05-02 19:56:55 +0000 | [diff] [blame] | 137 | 0, /* sq_slice */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 140 | static PyObject * range_iter(PyObject *seq); |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 141 | static PyObject * range_reverse(PyObject *seq); |
| 142 | |
| 143 | PyDoc_STRVAR(reverse_doc, |
| 144 | "Returns a reverse iterator."); |
| 145 | |
| 146 | static PyMethodDef range_methods[] = { |
| 147 | {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc}, |
| 148 | {NULL, NULL} /* sentinel */ |
| 149 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 151 | PyTypeObject PyRange_Type = { |
| 152 | PyObject_HEAD_INIT(&PyType_Type) |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 153 | 0, /* Number of items for varobject */ |
| 154 | "xrange", /* Name of this type */ |
| 155 | sizeof(rangeobject), /* Basic object size */ |
| 156 | 0, /* Item size for varobject */ |
| 157 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 158 | 0, /* tp_print */ |
| 159 | 0, /* tp_getattr */ |
| 160 | 0, /* tp_setattr */ |
| 161 | 0, /* tp_compare */ |
| 162 | (reprfunc)range_repr, /* tp_repr */ |
| 163 | 0, /* tp_as_number */ |
| 164 | &range_as_sequence, /* tp_as_sequence */ |
| 165 | 0, /* tp_as_mapping */ |
| 166 | 0, /* tp_hash */ |
| 167 | 0, /* tp_call */ |
| 168 | 0, /* tp_str */ |
| 169 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 170 | 0, /* tp_setattro */ |
| 171 | 0, /* tp_as_buffer */ |
| 172 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 173 | range_doc, /* tp_doc */ |
| 174 | 0, /* tp_traverse */ |
| 175 | 0, /* tp_clear */ |
| 176 | 0, /* tp_richcompare */ |
| 177 | 0, /* tp_weaklistoffset */ |
| 178 | range_iter, /* tp_iter */ |
| 179 | 0, /* tp_iternext */ |
| 180 | range_methods, /* tp_methods */ |
| 181 | 0, /* tp_members */ |
| 182 | 0, /* tp_getset */ |
| 183 | 0, /* tp_base */ |
| 184 | 0, /* tp_dict */ |
| 185 | 0, /* tp_descr_get */ |
| 186 | 0, /* tp_descr_set */ |
| 187 | 0, /* tp_dictoffset */ |
| 188 | 0, /* tp_init */ |
| 189 | 0, /* tp_alloc */ |
| 190 | range_new, /* tp_new */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 191 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 192 | |
| 193 | /*********************** Xrange Iterator **************************/ |
| 194 | |
| 195 | typedef struct { |
| 196 | PyObject_HEAD |
| 197 | long index; |
| 198 | long start; |
| 199 | long step; |
| 200 | long len; |
| 201 | } rangeiterobject; |
| 202 | |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 203 | static PyObject * |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 204 | rangeiter_next(rangeiterobject *r) |
| 205 | { |
Tim Peters | d976ab7 | 2004-08-08 06:29:10 +0000 | [diff] [blame] | 206 | if (r->index < r->len) |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 207 | return PyInt_FromLong(r->start + (r->index++) * r->step); |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 208 | return NULL; |
| 209 | } |
| 210 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 211 | static PyObject * |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 212 | rangeiter_len(rangeiterobject *r) |
| 213 | { |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 214 | return PyInt_FromLong(r->len - r->index); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 217 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 218 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 219 | static PyMethodDef rangeiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 220 | {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 221 | {NULL, NULL} /* sentinel */ |
| 222 | }; |
Raymond Hettinger | ef9bf40 | 2004-03-10 10:10:42 +0000 | [diff] [blame] | 223 | |
Neal Norwitz | 56f46f8 | 2002-06-06 14:58:21 +0000 | [diff] [blame] | 224 | static PyTypeObject Pyrangeiter_Type = { |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 225 | PyObject_HEAD_INIT(&PyType_Type) |
| 226 | 0, /* ob_size */ |
Neal Norwitz | 56f46f8 | 2002-06-06 14:58:21 +0000 | [diff] [blame] | 227 | "rangeiterator", /* tp_name */ |
| 228 | sizeof(rangeiterobject), /* tp_basicsize */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 229 | 0, /* tp_itemsize */ |
| 230 | /* methods */ |
| 231 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 232 | 0, /* tp_print */ |
| 233 | 0, /* tp_getattr */ |
| 234 | 0, /* tp_setattr */ |
| 235 | 0, /* tp_compare */ |
| 236 | 0, /* tp_repr */ |
| 237 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 238 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 239 | 0, /* tp_as_mapping */ |
| 240 | 0, /* tp_hash */ |
| 241 | 0, /* tp_call */ |
| 242 | 0, /* tp_str */ |
| 243 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 244 | 0, /* tp_setattro */ |
| 245 | 0, /* tp_as_buffer */ |
| 246 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 247 | 0, /* tp_doc */ |
| 248 | 0, /* tp_traverse */ |
| 249 | 0, /* tp_clear */ |
| 250 | 0, /* tp_richcompare */ |
| 251 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 252 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 253 | (iternextfunc)rangeiter_next, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 254 | rangeiter_methods, /* tp_methods */ |
| 255 | 0, |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 256 | }; |
Martin v. Löwis | 72d2067 | 2006-04-11 09:04:12 +0000 | [diff] [blame] | 257 | |
| 258 | static PyObject * |
| 259 | range_iter(PyObject *seq) |
| 260 | { |
| 261 | rangeiterobject *it; |
| 262 | |
| 263 | if (!PyRange_Check(seq)) { |
| 264 | PyErr_BadInternalCall(); |
| 265 | return NULL; |
| 266 | } |
| 267 | it = PyObject_New(rangeiterobject, &Pyrangeiter_Type); |
| 268 | if (it == NULL) |
| 269 | return NULL; |
| 270 | it->index = 0; |
| 271 | it->start = ((rangeobject *)seq)->start; |
| 272 | it->step = ((rangeobject *)seq)->step; |
| 273 | it->len = ((rangeobject *)seq)->len; |
| 274 | return (PyObject *)it; |
| 275 | } |
| 276 | |
| 277 | static PyObject * |
| 278 | range_reverse(PyObject *seq) |
| 279 | { |
| 280 | rangeiterobject *it; |
| 281 | long start, step, len; |
| 282 | |
| 283 | if (!PyRange_Check(seq)) { |
| 284 | PyErr_BadInternalCall(); |
| 285 | return NULL; |
| 286 | } |
| 287 | it = PyObject_New(rangeiterobject, &Pyrangeiter_Type); |
| 288 | if (it == NULL) |
| 289 | return NULL; |
| 290 | |
| 291 | start = ((rangeobject *)seq)->start; |
| 292 | step = ((rangeobject *)seq)->step; |
| 293 | len = ((rangeobject *)seq)->len; |
| 294 | |
| 295 | it->index = 0; |
| 296 | it->start = start + (len-1) * step; |
| 297 | it->step = -step; |
| 298 | it->len = len; |
| 299 | |
| 300 | return (PyObject *)it; |
| 301 | } |