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