blob: a6d1611a155bc84725a97d2c672e540ebf6edb83 [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 {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00006 PyObject_HEAD
Guido van Rossum12d12c51993-10-26 17:58:25 +00007 long start;
8 long step;
9 long len;
Guido van Rossum12d12c51993-10-26 17:58:25 +000010} rangeobject;
11
Thomas Woutersefafcea2001-07-09 12:30:54 +000012PyObject *
13PyRange_New(long start, long len, long step, int reps)
14{
Fred Draked9018322002-05-02 19:56:55 +000015 rangeobject *obj;
Guido van Rossum12d12c51993-10-26 17:58:25 +000016
Fred Draked9018322002-05-02 19:56:55 +000017 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 Rossum9e8f4ea2000-12-14 14:59:53 +000024 if (obj == NULL)
25 return NULL;
26
Fred Draked9018322002-05-02 19:56:55 +000027 if (len == 0) {
Guido van Rossum65e0b992001-01-15 18:58:56 +000028 start = 0;
29 len = 0;
30 step = 1;
Guido van Rossum65e0b992001-01-15 18:58:56 +000031 }
32 else {
33 long last = start + (len - 1) * step;
34 if ((step > 0) ?
Thomas Woutersefafcea2001-07-09 12:30:54 +000035 (last > (PyInt_GetMax() - step)) :
36 (last < (-1 - PyInt_GetMax() - step))) {
Guido van Rossum65e0b992001-01-15 18:58:56 +000037 PyErr_SetString(PyExc_OverflowError,
38 "integer addition");
39 return NULL;
Thomas Woutersefafcea2001-07-09 12:30:54 +000040 }
Guido van Rossum65e0b992001-01-15 18:58:56 +000041 }
Guido van Rossum12d12c51993-10-26 17:58:25 +000042 obj->start = start;
43 obj->len = len;
44 obj->step = step;
Guido van Rossum12d12c51993-10-26 17:58:25 +000045
Guido van Rossumc0b618a1997-05-02 03:12:38 +000046 return (PyObject *) obj;
Guido van Rossum12d12c51993-10-26 17:58:25 +000047}
48
Raymond Hettingerc4c453f2002-06-05 23:12:45 +000049/* 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 */
53static long
54get_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
78static PyObject *
79range_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öwis14f8b4c2002-06-13 20:33:02 +0000112PyDoc_STRVAR(range_doc,
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000113"xrange([start,] stop[, step]) -> xrange object\n\
114\n\
115Like range(), but instead of returning a list, returns an object that\n\
Raymond Hettingerd2bef822002-12-11 07:14:03 +0000116generates the numbers in the range on demand. For looping, this is \n\
117slightly faster than range() and more memory efficient.");
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000118
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000120range_item(rangeobject *r, int i)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000121{
Fred Draked9018322002-05-02 19:56:55 +0000122 if (i < 0 || i >= r->len) {
123 PyErr_SetString(PyExc_IndexError,
Guido van Rossum5dadf7e1999-01-09 21:40:35 +0000124 "xrange object index out of range");
Fred Draked9018322002-05-02 19:56:55 +0000125 return NULL;
126 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 return PyInt_FromLong(r->start + (i % r->len) * r->step);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000128}
129
130static int
Fred Drake45cfbcc2000-07-09 06:21:27 +0000131range_length(rangeobject *r)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000132{
Guido van Rossumd4774fb2002-09-11 15:55:48 +0000133#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 Rossum7d6aa511993-12-21 22:50:31 +0000141}
142
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000144range_repr(rangeobject *r)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000145{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000146 PyObject *rtn;
147
Tim Peters72d421b2000-08-04 03:05:40 +0000148 if (r->start == 0 && r->step == 1)
Barry Warsaw7ce36942001-08-24 18:34:26 +0000149 rtn = PyString_FromFormat("xrange(%ld)",
150 r->start + r->len * r->step);
Tim Peters72d421b2000-08-04 03:05:40 +0000151
152 else if (r->step == 1)
Barry Warsaw7ce36942001-08-24 18:34:26 +0000153 rtn = PyString_FromFormat("xrange(%ld, %ld)",
154 r->start,
155 r->start + r->len * r->step);
Tim Peters72d421b2000-08-04 03:05:40 +0000156
157 else
Barry Warsaw7ce36942001-08-24 18:34:26 +0000158 rtn = PyString_FromFormat("xrange(%ld, %ld, %ld)",
159 r->start,
160 r->start + r->len * r->step,
161 r->step);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000162 return rtn;
Thomas Woutersefafcea2001-07-09 12:30:54 +0000163}
164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165static PySequenceMethods range_as_sequence = {
Fred Draked9018322002-05-02 19:56:55 +0000166 (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 Rossum12d12c51993-10-26 17:58:25 +0000171};
172
Jeremy Hylton938ace62002-07-17 16:30:39 +0000173static PyObject * range_iter(PyObject *seq);
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000174static PyObject * range_reverse(PyObject *seq);
175
176PyDoc_STRVAR(reverse_doc,
177"Returns a reverse iterator.");
178
179static PyMethodDef range_methods[] = {
180 {"__reversed__", (PyCFunction)range_reverse, METH_NOARGS, reverse_doc},
181 {NULL, NULL} /* sentinel */
182};
Raymond Hettinger48165d42002-06-05 20:08:48 +0000183
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184PyTypeObject PyRange_Type = {
185 PyObject_HEAD_INIT(&PyType_Type)
Fred Draked9018322002-05-02 19:56:55 +0000186 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 Hettinger5ae8e012002-11-07 16:55:54 +0000202 PyObject_GenericGetAttr, /* tp_getattro */
Fred Draked9018322002-05-02 19:56:55 +0000203 0, /* tp_setattro */
204 0, /* tp_as_buffer */
205 Py_TPFLAGS_DEFAULT, /* tp_flags */
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000206 range_doc, /* tp_doc */
Martin v. Löwise4526592002-05-08 08:49:27 +0000207 0, /* tp_traverse */
208 0, /* tp_clear */
209 0, /* tp_richcompare */
210 0, /* tp_weaklistoffset */
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000211 (getiterfunc)range_iter, /* tp_iter */
212 0, /* tp_iternext */
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000213 range_methods, /* tp_methods */
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000214 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 {
229 PyObject_HEAD
230 long index;
231 long start;
232 long step;
233 long len;
234} rangeiterobject;
235
Jeremy Hylton938ace62002-07-17 16:30:39 +0000236static PyTypeObject Pyrangeiter_Type;
Raymond Hettinger48165d42002-06-05 20:08:48 +0000237
Neal Norwitz56f46f82002-06-06 14:58:21 +0000238static PyObject *
Raymond Hettinger48165d42002-06-05 20:08:48 +0000239range_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
257static PyObject *
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000258range_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
283static PyObject *
Raymond Hettinger48165d42002-06-05 20:08:48 +0000284rangeiter_next(rangeiterobject *r)
285{
286 if (r->index < r->len)
287 return PyInt_FromLong(r->start + (r->index++) * r->step);
Raymond Hettinger48165d42002-06-05 20:08:48 +0000288 return NULL;
289}
290
Raymond Hettingeref9bf402004-03-10 10:10:42 +0000291static int
292rangeiter_len(rangeiterobject *r)
293{
294 return r->len - r->index;
295}
296
297static PySequenceMethods rangeiter_as_sequence = {
298 (inquiry)rangeiter_len, /* sq_length */
299 0, /* sq_concat */
300};
301
302
Neal Norwitz56f46f82002-06-06 14:58:21 +0000303static PyTypeObject Pyrangeiter_Type = {
Raymond Hettinger48165d42002-06-05 20:08:48 +0000304 PyObject_HEAD_INIT(&PyType_Type)
305 0, /* ob_size */
Neal Norwitz56f46f82002-06-06 14:58:21 +0000306 "rangeiterator", /* tp_name */
307 sizeof(rangeiterobject), /* tp_basicsize */
Raymond Hettinger48165d42002-06-05 20:08:48 +0000308 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 Hettingeref9bf402004-03-10 10:10:42 +0000317 &rangeiter_as_sequence, /* tp_as_sequence */
Raymond Hettinger48165d42002-06-05 20:08:48 +0000318 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 Hettinger1da1dbf2003-03-17 19:46:11 +0000331 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger48165d42002-06-05 20:08:48 +0000332 (iternextfunc)rangeiter_next, /* tp_iternext */
Guido van Rossum86d593e2002-07-16 20:47:50 +0000333 0, /* tp_methods */
Raymond Hettinger48165d42002-06-05 20:08:48 +0000334};