blob: c602e7dcfd1db0411a0bf9c427805206c6cb88c7 [file] [log] [blame]
Guido van Rossum12d12c51993-10-26 17:58:25 +00001/* Range object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Thomas Woutersefafcea2001-07-09 12:30:54 +00004
Guido van Rossum12d12c51993-10-26 17:58:25 +00005typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00006 PyObject_HEAD
7 long start;
8 long step;
9 long len;
Guido van Rossum12d12c51993-10-26 17:58:25 +000010} rangeobject;
11
Mark Dickinson009ae862009-11-15 12:31:13 +000012/* Return number of items in range (lo, hi, step). step != 0
13 * required. The result always fits in an unsigned long.
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000014 */
Mark Dickinson009ae862009-11-15 12:31:13 +000015static unsigned long
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000016get_len_of_range(long lo, long hi, long step)
17{
Mark Dickinson009ae862009-11-15 12:31:13 +000018 /* -------------------------------------------------------------
19 If step > 0 and lo >= hi, or step < 0 and lo <= hi, the range is empty.
20 Else for step > 0, if n values are in the range, the last one is
21 lo + (n-1)*step, which must be <= hi-1. Rearranging,
22 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
23 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
24 the RHS is non-negative and so truncation is the same as the
25 floor. Letting M be the largest positive long, the worst case
26 for the RHS numerator is hi=M, lo=-M-1, and then
27 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
28 precision to compute the RHS exactly. The analysis for step < 0
29 is similar.
30 ---------------------------------------------------------------*/
31 assert(step != 0);
32 if (step > 0 && lo < hi)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000033 return 1UL + (hi - 1UL - lo) / step;
Mark Dickinson009ae862009-11-15 12:31:13 +000034 else if (step < 0 && lo > hi)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000035 return 1UL + (lo - 1UL - hi) / (0UL - step);
Mark Dickinson009ae862009-11-15 12:31:13 +000036 else
Antoine Pitrouc83ea132010-05-09 14:46:46 +000037 return 0UL;
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000038}
39
Mark Dickinson218a8ab2012-09-28 20:36:36 +010040/* Return a stop value suitable for reconstructing the xrange from
41 * a (start, stop, step) triple. Used in range_repr and range_reduce.
42 * Computes start + len * step, clipped to the range [LONG_MIN, LONG_MAX].
43 */
44static long
45get_stop_for_range(rangeobject *r)
46{
47 long last;
48
49 if (r->len == 0)
50 return r->start;
51
52 /* The tricky bit is avoiding overflow. We first compute the last entry in
53 the xrange, start + (len - 1) * step, which is guaranteed to lie within
54 the range of a long, and then add step to it. See the range_reverse
55 comments for an explanation of the casts below.
56 */
57 last = (long)(r->start + (unsigned long)(r->len - 1) * r->step);
58 if (r->step > 0)
59 return last > LONG_MAX - r->step ? LONG_MAX : last + r->step;
60 else
61 return last < LONG_MIN - r->step ? LONG_MIN : last + r->step;
62}
63
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000064static PyObject *
65range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
66{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000067 rangeobject *obj;
68 long ilow = 0, ihigh = 0, istep = 1;
69 unsigned long n;
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000070
Antoine Pitrouc83ea132010-05-09 14:46:46 +000071 if (!_PyArg_NoKeywords("xrange()", kw))
72 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +000073
Antoine Pitrouc83ea132010-05-09 14:46:46 +000074 if (PyTuple_Size(args) <= 1) {
75 if (!PyArg_ParseTuple(args,
76 "l;xrange() requires 1-3 int arguments",
77 &ihigh))
78 return NULL;
79 }
80 else {
81 if (!PyArg_ParseTuple(args,
82 "ll|l;xrange() requires 1-3 int arguments",
83 &ilow, &ihigh, &istep))
84 return NULL;
85 }
86 if (istep == 0) {
87 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
88 return NULL;
89 }
90 n = get_len_of_range(ilow, ihigh, istep);
91 if (n > (unsigned long)LONG_MAX || (long)n > PY_SSIZE_T_MAX) {
92 PyErr_SetString(PyExc_OverflowError,
93 "xrange() result has too many items");
94 return NULL;
95 }
Tim Petersfeec4532004-08-08 07:17:39 +000096
Antoine Pitrouc83ea132010-05-09 14:46:46 +000097 obj = PyObject_New(rangeobject, &PyRange_Type);
98 if (obj == NULL)
99 return NULL;
100 obj->start = ilow;
101 obj->len = (long)n;
102 obj->step = istep;
103 return (PyObject *) obj;
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000104}
105
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000106PyDoc_STRVAR(range_doc,
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000107"xrange([start,] stop[, step]) -> xrange object\n\
108\n\
109Like range(), but instead of returning a list, returns an object that\n\
Raymond Hettingerd2bef822002-12-11 07:14:03 +0000110generates the numbers in the range on demand. For looping, this is \n\
111slightly faster than range() and more memory efficient.");
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000114range_item(rangeobject *r, Py_ssize_t i)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000115{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000116 if (i < 0 || i >= r->len) {
117 PyErr_SetString(PyExc_IndexError,
118 "xrange object index out of range");
119 return NULL;
120 }
121 /* do calculation entirely using unsigned longs, to avoid
122 undefined behaviour due to signed overflow. */
123 return PyInt_FromLong((long)(r->start + (unsigned long)i * r->step));
Guido van Rossum12d12c51993-10-26 17:58:25 +0000124}
125
Martin v. Löwis18e16552006-02-15 17:27:45 +0000126static Py_ssize_t
Fred Drake45cfbcc2000-07-09 06:21:27 +0000127range_length(rangeobject *r)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000128{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000129 return (Py_ssize_t)(r->len);
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000130}
131
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000132static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000133range_repr(rangeobject *r)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000134{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000135 PyObject *rtn;
Tim Petersd976ab72004-08-08 06:29:10 +0000136
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000137 if (r->start == 0 && r->step == 1)
138 rtn = PyString_FromFormat("xrange(%ld)",
Mark Dickinson218a8ab2012-09-28 20:36:36 +0100139 get_stop_for_range(r));
Tim Peters72d421b2000-08-04 03:05:40 +0000140
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 else if (r->step == 1)
142 rtn = PyString_FromFormat("xrange(%ld, %ld)",
143 r->start,
Mark Dickinson218a8ab2012-09-28 20:36:36 +0100144 get_stop_for_range(r));
Tim Peters72d421b2000-08-04 03:05:40 +0000145
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000146 else
147 rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)",
148 r->start,
Mark Dickinson218a8ab2012-09-28 20:36:36 +0100149 get_stop_for_range(r),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000150 r->step);
151 return rtn;
Thomas Woutersefafcea2001-07-09 12:30:54 +0000152}
153
Alexandre Vassalotti1f2f61a2008-06-10 03:34:53 +0000154/* Pickling support */
155static PyObject *
Alexandre Vassalotti602d8db2008-06-10 04:01:23 +0000156range_reduce(rangeobject *r, PyObject *args)
Alexandre Vassalotti1f2f61a2008-06-10 03:34:53 +0000157{
Mark Dickinson218a8ab2012-09-28 20:36:36 +0100158 return Py_BuildValue("(O(lll))", Py_TYPE(r),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 r->start,
Mark Dickinson218a8ab2012-09-28 20:36:36 +0100160 get_stop_for_range(r),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 r->step);
Alexandre Vassalotti1f2f61a2008-06-10 03:34:53 +0000162}
163
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164static PySequenceMethods range_as_sequence = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000165 (lenfunc)range_length, /* sq_length */
166 0, /* sq_concat */
167 0, /* sq_repeat */
168 (ssizeargfunc)range_item, /* sq_item */
169 0, /* sq_slice */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000170};
171
Jeremy Hylton938ace62002-07-17 16:30:39 +0000172static PyObject * range_iter(PyObject *seq);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000173static PyObject * range_reverse(PyObject *seq);
174
175PyDoc_STRVAR(reverse_doc,
176"Returns a reverse iterator.");
177
178static PyMethodDef range_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000179 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
180 {"__reduce__", (PyCFunction)range_reduce, METH_VARARGS},
181 {NULL, NULL} /* sentinel */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000182};
Raymond Hettinger48165d42002-06-05 20:08:48 +0000183
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184PyTypeObject PyRange_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000185 PyObject_HEAD_INIT(&PyType_Type)
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 */
202 PyObject_GenericGetAttr, /* tp_getattro */
203 0, /* tp_setattro */
204 0, /* tp_as_buffer */
205 Py_TPFLAGS_DEFAULT, /* tp_flags */
206 range_doc, /* tp_doc */
207 0, /* tp_traverse */
208 0, /* tp_clear */
209 0, /* tp_richcompare */
210 0, /* tp_weaklistoffset */
211 range_iter, /* tp_iter */
212 0, /* tp_iternext */
213 range_methods, /* tp_methods */
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 Rossum12d12c51993-10-26 17:58:25 +0000224};
Raymond Hettinger48165d42002-06-05 20:08:48 +0000225
226/*********************** Xrange Iterator **************************/
227
228typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000229 PyObject_HEAD
230 long index;
231 long start;
232 long step;
233 long len;
Raymond Hettinger48165d42002-06-05 20:08:48 +0000234} rangeiterobject;
235
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000236static PyObject *
Raymond Hettinger48165d42002-06-05 20:08:48 +0000237rangeiter_next(rangeiterobject *r)
238{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 if (r->index < r->len)
240 return PyInt_FromLong(r->start + (r->index++) * r->step);
241 return NULL;
Raymond Hettinger48165d42002-06-05 20:08:48 +0000242}
243
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000244static PyObject *
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000245rangeiter_len(rangeiterobject *r)
246{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 return PyInt_FromLong(r->len - r->index);
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000248}
249
Armin Rigof5b3e362006-02-11 21:32:43 +0000250PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000251
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000252static PyMethodDef rangeiter_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000253 {"__length_hint__", (PyCFunction)rangeiter_len, METH_NOARGS, length_hint_doc},
254 {NULL, NULL} /* sentinel */
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000255};
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000256
Neal Norwitz56f46f82002-06-06 14:58:21 +0000257static PyTypeObject Pyrangeiter_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000258 PyObject_HEAD_INIT(&PyType_Type)
259 0, /* ob_size */
260 "rangeiterator", /* tp_name */
261 sizeof(rangeiterobject), /* tp_basicsize */
262 0, /* tp_itemsize */
263 /* methods */
264 (destructor)PyObject_Del, /* tp_dealloc */
265 0, /* tp_print */
266 0, /* tp_getattr */
267 0, /* tp_setattr */
268 0, /* tp_compare */
269 0, /* tp_repr */
270 0, /* tp_as_number */
271 0, /* tp_as_sequence */
272 0, /* tp_as_mapping */
273 0, /* tp_hash */
274 0, /* tp_call */
275 0, /* tp_str */
276 PyObject_GenericGetAttr, /* tp_getattro */
277 0, /* tp_setattro */
278 0, /* tp_as_buffer */
279 Py_TPFLAGS_DEFAULT, /* tp_flags */
280 0, /* tp_doc */
281 0, /* tp_traverse */
282 0, /* tp_clear */
283 0, /* tp_richcompare */
284 0, /* tp_weaklistoffset */
285 PyObject_SelfIter, /* tp_iter */
286 (iternextfunc)rangeiter_next, /* tp_iternext */
287 rangeiter_methods, /* tp_methods */
288 0,
Raymond Hettinger48165d42002-06-05 20:08:48 +0000289};
Martin v. Löwis72d20672006-04-11 09:04:12 +0000290
291static PyObject *
292range_iter(PyObject *seq)
293{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000294 rangeiterobject *it;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000295
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000296 if (!PyRange_Check(seq)) {
297 PyErr_BadInternalCall();
298 return NULL;
299 }
300 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type);
301 if (it == NULL)
302 return NULL;
303 it->index = 0;
304 it->start = ((rangeobject *)seq)->start;
305 it->step = ((rangeobject *)seq)->step;
306 it->len = ((rangeobject *)seq)->len;
307 return (PyObject *)it;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000308}
309
310static PyObject *
311range_reverse(PyObject *seq)
312{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 rangeiterobject *it;
314 long start, step, len;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000315
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000316 if (!PyRange_Check(seq)) {
317 PyErr_BadInternalCall();
318 return NULL;
319 }
320 it = PyObject_New(rangeiterobject, &Pyrangeiter_Type);
321 if (it == NULL)
322 return NULL;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000323
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000324 start = ((rangeobject *)seq)->start;
325 step = ((rangeobject *)seq)->step;
326 len = ((rangeobject *)seq)->len;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000327
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000328 it->index = 0;
329 it->len = len;
330 /* the casts below guard against signed overflow by turning it
331 into unsigned overflow instead. The correctness of this
332 code still depends on conversion from unsigned long to long
333 wrapping modulo ULONG_MAX+1, which isn't guaranteed (see
334 C99 6.3.1.3p3) but seems to hold in practice for all
335 platforms we're likely to meet.
Mark Dickinson009ae862009-11-15 12:31:13 +0000336
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337 If step == LONG_MIN then we still end up with LONG_MIN
338 after negation; but this works out, since we've still got
339 the correct value modulo ULONG_MAX+1, and the range_item
340 calculation is also done modulo ULONG_MAX+1.
341 */
342 it->start = (long)(start + (unsigned long)(len-1) * step);
343 it->step = (long)(0UL-step);
Martin v. Löwis72d20672006-04-11 09:04:12 +0000344
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000345 return (PyObject *)it;
Martin v. Löwis72d20672006-04-11 09:04:12 +0000346}