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