blob: 299f4a651f9fb779534343cd39eed92935a3a5c7 [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 Hettinger48165d42002-06-05 20:08:48 +0000174
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175PyTypeObject PyRange_Type = {
176 PyObject_HEAD_INIT(&PyType_Type)
Fred Draked9018322002-05-02 19:56:55 +0000177 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 Hettinger5ae8e012002-11-07 16:55:54 +0000193 PyObject_GenericGetAttr, /* tp_getattro */
Fred Draked9018322002-05-02 19:56:55 +0000194 0, /* tp_setattro */
195 0, /* tp_as_buffer */
196 Py_TPFLAGS_DEFAULT, /* tp_flags */
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000197 range_doc, /* tp_doc */
Martin v. Löwise4526592002-05-08 08:49:27 +0000198 0, /* tp_traverse */
199 0, /* tp_clear */
200 0, /* tp_richcompare */
201 0, /* tp_weaklistoffset */
Raymond Hettingerc4c453f2002-06-05 23:12:45 +0000202 (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 Rossum12d12c51993-10-26 17:58:25 +0000215};
Raymond Hettinger48165d42002-06-05 20:08:48 +0000216
217/*********************** Xrange Iterator **************************/
218
219typedef struct {
220 PyObject_HEAD
221 long index;
222 long start;
223 long step;
224 long len;
225} rangeiterobject;
226
Jeremy Hylton938ace62002-07-17 16:30:39 +0000227static PyTypeObject Pyrangeiter_Type;
Raymond Hettinger48165d42002-06-05 20:08:48 +0000228
Neal Norwitz56f46f82002-06-06 14:58:21 +0000229static PyObject *
Raymond Hettinger48165d42002-06-05 20:08:48 +0000230range_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
248static PyObject *
Raymond Hettinger48165d42002-06-05 20:08:48 +0000249rangeiter_next(rangeiterobject *r)
250{
251 if (r->index < r->len)
252 return PyInt_FromLong(r->start + (r->index++) * r->step);
Raymond Hettinger48165d42002-06-05 20:08:48 +0000253 return NULL;
254}
255
Neal Norwitz56f46f82002-06-06 14:58:21 +0000256static PyTypeObject Pyrangeiter_Type = {
Raymond Hettinger48165d42002-06-05 20:08:48 +0000257 PyObject_HEAD_INIT(&PyType_Type)
258 0, /* ob_size */
Neal Norwitz56f46f82002-06-06 14:58:21 +0000259 "rangeiterator", /* tp_name */
260 sizeof(rangeiterobject), /* tp_basicsize */
Raymond Hettinger48165d42002-06-05 20:08:48 +0000261 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 Hettinger1da1dbf2003-03-17 19:46:11 +0000284 PyObject_SelfIter, /* tp_iter */
Raymond Hettinger48165d42002-06-05 20:08:48 +0000285 (iternextfunc)rangeiter_next, /* tp_iternext */
Guido van Rossum86d593e2002-07-16 20:47:50 +0000286 0, /* tp_methods */
Raymond Hettinger48165d42002-06-05 20:08:48 +0000287};