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 | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 175 | PyTypeObject PyRange_Type = { |
| 176 | PyObject_HEAD_INIT(&PyType_Type) |
Fred Drake | d901832 | 2002-05-02 19:56:55 +0000 | [diff] [blame] | 177 | 0, /* Number of items for varobject */ |
| 178 | "xrange", /* Name of this type */ |
| 179 | sizeof(rangeobject), /* Basic object size */ |
| 180 | 0, /* Item size for varobject */ |
| 181 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 182 | 0, /* tp_print */ |
| 183 | 0, /* tp_getattr */ |
| 184 | 0, /* tp_setattr */ |
| 185 | 0, /* tp_compare */ |
| 186 | (reprfunc)range_repr, /* tp_repr */ |
| 187 | 0, /* tp_as_number */ |
| 188 | &range_as_sequence, /* tp_as_sequence */ |
| 189 | 0, /* tp_as_mapping */ |
| 190 | 0, /* tp_hash */ |
| 191 | 0, /* tp_call */ |
| 192 | 0, /* tp_str */ |
Raymond Hettinger | 5ae8e01 | 2002-11-07 16:55:54 +0000 | [diff] [blame] | 193 | PyObject_GenericGetAttr, /* tp_getattro */ |
Fred Drake | d901832 | 2002-05-02 19:56:55 +0000 | [diff] [blame] | 194 | 0, /* tp_setattro */ |
| 195 | 0, /* tp_as_buffer */ |
| 196 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 197 | range_doc, /* tp_doc */ |
Martin v. Löwis | e452659 | 2002-05-08 08:49:27 +0000 | [diff] [blame] | 198 | 0, /* tp_traverse */ |
| 199 | 0, /* tp_clear */ |
| 200 | 0, /* tp_richcompare */ |
| 201 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 202 | (getiterfunc)range_iter, /* tp_iter */ |
| 203 | 0, /* tp_iternext */ |
| 204 | 0, /* tp_methods */ |
| 205 | 0, /* tp_members */ |
| 206 | 0, /* tp_getset */ |
| 207 | 0, /* tp_base */ |
| 208 | 0, /* tp_dict */ |
| 209 | 0, /* tp_descr_get */ |
| 210 | 0, /* tp_descr_set */ |
| 211 | 0, /* tp_dictoffset */ |
| 212 | 0, /* tp_init */ |
| 213 | 0, /* tp_alloc */ |
| 214 | range_new, /* tp_new */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 215 | }; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 216 | |
| 217 | /*********************** Xrange Iterator **************************/ |
| 218 | |
| 219 | typedef struct { |
| 220 | PyObject_HEAD |
| 221 | long index; |
| 222 | long start; |
| 223 | long step; |
| 224 | long len; |
| 225 | } rangeiterobject; |
| 226 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 227 | static PyTypeObject Pyrangeiter_Type; |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 228 | |
Neal Norwitz | 56f46f8 | 2002-06-06 14:58:21 +0000 | [diff] [blame] | 229 | static PyObject * |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 230 | range_iter(PyObject *seq) |
| 231 | { |
| 232 | rangeiterobject *it; |
| 233 | |
| 234 | if (!PyRange_Check(seq)) { |
| 235 | PyErr_BadInternalCall(); |
| 236 | return NULL; |
| 237 | } |
| 238 | it = PyObject_New(rangeiterobject, &Pyrangeiter_Type); |
| 239 | if (it == NULL) |
| 240 | return NULL; |
| 241 | it->index = 0; |
| 242 | it->start = ((rangeobject *)seq)->start; |
| 243 | it->step = ((rangeobject *)seq)->step; |
| 244 | it->len = ((rangeobject *)seq)->len; |
| 245 | return (PyObject *)it; |
| 246 | } |
| 247 | |
| 248 | static PyObject * |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 249 | rangeiter_next(rangeiterobject *r) |
| 250 | { |
| 251 | if (r->index < r->len) |
| 252 | return PyInt_FromLong(r->start + (r->index++) * r->step); |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 253 | return NULL; |
| 254 | } |
| 255 | |
Neal Norwitz | 56f46f8 | 2002-06-06 14:58:21 +0000 | [diff] [blame] | 256 | static PyTypeObject Pyrangeiter_Type = { |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 257 | PyObject_HEAD_INIT(&PyType_Type) |
| 258 | 0, /* ob_size */ |
Neal Norwitz | 56f46f8 | 2002-06-06 14:58:21 +0000 | [diff] [blame] | 259 | "rangeiterator", /* tp_name */ |
| 260 | sizeof(rangeiterobject), /* tp_basicsize */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 261 | 0, /* tp_itemsize */ |
| 262 | /* methods */ |
| 263 | (destructor)PyObject_Del, /* tp_dealloc */ |
| 264 | 0, /* tp_print */ |
| 265 | 0, /* tp_getattr */ |
| 266 | 0, /* tp_setattr */ |
| 267 | 0, /* tp_compare */ |
| 268 | 0, /* tp_repr */ |
| 269 | 0, /* tp_as_number */ |
| 270 | 0, /* tp_as_sequence */ |
| 271 | 0, /* tp_as_mapping */ |
| 272 | 0, /* tp_hash */ |
| 273 | 0, /* tp_call */ |
| 274 | 0, /* tp_str */ |
| 275 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 276 | 0, /* tp_setattro */ |
| 277 | 0, /* tp_as_buffer */ |
| 278 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 279 | 0, /* tp_doc */ |
| 280 | 0, /* tp_traverse */ |
| 281 | 0, /* tp_clear */ |
| 282 | 0, /* tp_richcompare */ |
| 283 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 284 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 285 | (iternextfunc)rangeiter_next, /* tp_iternext */ |
Guido van Rossum | 86d593e | 2002-07-16 20:47:50 +0000 | [diff] [blame] | 286 | 0, /* tp_methods */ |
Raymond Hettinger | 48165d4 | 2002-06-05 20:08:48 +0000 | [diff] [blame] | 287 | }; |