blob: 5c794fc204f42a940ee30fe274e6fa3d8984a6df [file] [log] [blame]
Guido van Rossum12d12c51993-10-26 17:58:25 +00001
2/* Range object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum12d12c51993-10-26 17:58:25 +00005
6typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00007 PyObject_HEAD
Guido van Rossum12d12c51993-10-26 17:58:25 +00008 long start;
9 long step;
10 long len;
Guido van Rossum7d6aa511993-12-21 22:50:31 +000011 int reps;
Guido van Rossum12d12c51993-10-26 17:58:25 +000012} rangeobject;
13
14
Guido van Rossumc0b618a1997-05-02 03:12:38 +000015PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000016PyRange_New(long start, long len, long step, int reps)
Guido van Rossum12d12c51993-10-26 17:58:25 +000017{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000018 rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type);
Guido van Rossum12d12c51993-10-26 17:58:25 +000019
20 obj->start = start;
21 obj->len = len;
22 obj->step = step;
Guido van Rossum7d6aa511993-12-21 22:50:31 +000023 obj->reps = reps;
Guido van Rossum12d12c51993-10-26 17:58:25 +000024
Guido van Rossumc0b618a1997-05-02 03:12:38 +000025 return (PyObject *) obj;
Guido van Rossum12d12c51993-10-26 17:58:25 +000026}
27
28static void
Fred Drake45cfbcc2000-07-09 06:21:27 +000029range_dealloc(rangeobject *r)
Guido van Rossum12d12c51993-10-26 17:58:25 +000030{
Guido van Rossumb18618d2000-05-03 23:44:39 +000031 PyObject_DEL(r);
Guido van Rossum12d12c51993-10-26 17:58:25 +000032}
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000035range_item(rangeobject *r, int i)
Guido van Rossum12d12c51993-10-26 17:58:25 +000036{
Guido van Rossum7d6aa511993-12-21 22:50:31 +000037 if (i < 0 || i >= r->len * r->reps) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038 PyErr_SetString(PyExc_IndexError,
Guido van Rossum5dadf7e1999-01-09 21:40:35 +000039 "xrange object index out of range");
Guido van Rossum12d12c51993-10-26 17:58:25 +000040 return NULL;
41 }
42
Guido van Rossumc0b618a1997-05-02 03:12:38 +000043 return PyInt_FromLong(r->start + (i % r->len) * r->step);
Guido van Rossum12d12c51993-10-26 17:58:25 +000044}
45
46static int
Fred Drake45cfbcc2000-07-09 06:21:27 +000047range_length(rangeobject *r)
Guido van Rossum12d12c51993-10-26 17:58:25 +000048{
Guido van Rossum7d6aa511993-12-21 22:50:31 +000049 return r->len * r->reps;
50}
51
Guido van Rossumc0b618a1997-05-02 03:12:38 +000052static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000053range_repr(rangeobject *r)
Guido van Rossum12d12c51993-10-26 17:58:25 +000054{
Tim Peters72d421b2000-08-04 03:05:40 +000055 /* buffers must be big enough to hold 3 longs + an int +
56 * a bit of "(xrange(...) * ...)" text.
57 */
58 char buf1[250];
59 char buf2[250];
Fred Drake2b83b462000-08-03 17:43:02 +000060
Tim Peters72d421b2000-08-04 03:05:40 +000061 if (r->start == 0 && r->step == 1)
Fred Drakec76e0e52000-08-04 02:34:41 +000062 sprintf(buf1, "xrange(%ld)", r->start + r->len * r->step);
Tim Peters72d421b2000-08-04 03:05:40 +000063
64 else if (r->step == 1)
65 sprintf(buf1, "xrange(%ld, %ld)",
66 r->start,
67 r->start + r->len * r->step);
68
69 else
70 sprintf(buf1, "xrange(%ld, %ld, %ld)",
Fred Drakec76e0e52000-08-04 02:34:41 +000071 r->start,
72 r->start + r->len * r->step,
73 r->step);
Tim Peters72d421b2000-08-04 03:05:40 +000074
75 if (r->reps != 1)
Fred Drakec76e0e52000-08-04 02:34:41 +000076 sprintf(buf2, "(%s * %d)", buf1, r->reps);
Tim Peters72d421b2000-08-04 03:05:40 +000077
Fred Drake2b83b462000-08-03 17:43:02 +000078 return PyString_FromString(r->reps == 1 ? buf1 : buf2);
Guido van Rossum12d12c51993-10-26 17:58:25 +000079}
80
Guido van Rossumc0b618a1997-05-02 03:12:38 +000081static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000082range_concat(rangeobject *r, PyObject *obj)
Guido van Rossum7d6aa511993-12-21 22:50:31 +000083{
Guido van Rossum5dadf7e1999-01-09 21:40:35 +000084 PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects");
Guido van Rossum7d6aa511993-12-21 22:50:31 +000085 return NULL;
86}
87
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +000089range_repeat(rangeobject *r, int n)
Guido van Rossum7d6aa511993-12-21 22:50:31 +000090{
91 if (n < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092 return (PyObject *) PyRange_New(0, 0, 1, 1);
Guido van Rossum7d6aa511993-12-21 22:50:31 +000093
94 else if (n == 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 Py_INCREF(r);
96 return (PyObject *) r;
Guido van Rossum7d6aa511993-12-21 22:50:31 +000097 }
98
99 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 return (PyObject *) PyRange_New(
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000101 r->start,
102 r->len,
103 r->step,
104 r->reps * n);
105}
106
Guido van Rossum12d12c51993-10-26 17:58:25 +0000107static int
Fred Drake45cfbcc2000-07-09 06:21:27 +0000108range_compare(rangeobject *r1, rangeobject *r2)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000109{
110 if (r1->start != r2->start)
111 return r1->start - r2->start;
112
113 else if (r1->step != r2->step)
114 return r1->step - r2->step;
115
116 else if (r1->len != r2->len)
117 return r1->len - r2->len;
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000118
119 else
120 return r1->reps - r2->reps;
Guido van Rossum12d12c51993-10-26 17:58:25 +0000121}
122
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000123static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000124range_slice(rangeobject *r, int low, int high)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000125{
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000126 if (r->reps != 1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 PyErr_SetString(PyExc_TypeError,
Guido van Rossum5dadf7e1999-01-09 21:40:35 +0000128 "cannot slice a replicated xrange");
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000129 return NULL;
130 }
131 if (low < 0)
132 low = 0;
133 else if (low > r->len)
134 low = r->len;
135 if (high < 0)
136 high = 0;
137 if (high < low)
138 high = low;
139 else if (high > r->len)
140 high = r->len;
141
142 if (low == 0 && high == r->len) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143 Py_INCREF(r);
144 return (PyObject *) r;
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000145 }
146
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 return (PyObject *) PyRange_New(
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000148 low * r->step + r->start,
149 high - low,
150 r->step,
151 1);
Guido van Rossum2586bf01993-11-01 16:21:44 +0000152}
Guido van Rossum12d12c51993-10-26 17:58:25 +0000153
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000155range_tolist(rangeobject *self, PyObject *args)
Guido van Rossum2586bf01993-11-01 16:21:44 +0000156{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 PyObject *thelist;
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000158 int j;
159 int len = self->len * self->reps;
160
Fred Drake2b83b462000-08-03 17:43:02 +0000161 if (! PyArg_ParseTuple(args, ":tolist"))
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000162 return NULL;
163
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 if ((thelist = PyList_New(len)) == NULL)
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000165 return NULL;
166
167 for (j = 0; j < len; ++j)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong(
169 self->start + (j % self->len) * self->step))) < 0)
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000170 return NULL;
171
172 return thelist;
Guido van Rossum2586bf01993-11-01 16:21:44 +0000173}
174
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175static PyObject *
Fred Drake45cfbcc2000-07-09 06:21:27 +0000176range_getattr(rangeobject *r, char *name)
Guido van Rossum2586bf01993-11-01 16:21:44 +0000177{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 static PyMethodDef range_methods[] = {
Fred Drake2b83b462000-08-03 17:43:02 +0000179 {"tolist", (PyCFunction)range_tolist, METH_VARARGS,
180 "tolist() -> list\n"
181 "Return a list object with the same values."},
Guido van Rossum7d6aa511993-12-21 22:50:31 +0000182 {NULL, NULL}
183 };
184
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185 return Py_FindMethod(range_methods, (PyObject *) r, name);
Guido van Rossum12d12c51993-10-26 17:58:25 +0000186}
187
Fred Drake56780252000-06-15 14:50:20 +0000188static int
Fred Drake45cfbcc2000-07-09 06:21:27 +0000189range_contains(rangeobject *r, PyObject *obj)
Fred Drake56780252000-06-15 14:50:20 +0000190{
191 long num = PyInt_AsLong(obj);
Tim Peters72d421b2000-08-04 03:05:40 +0000192
Fred Drake56780252000-06-15 14:50:20 +0000193 if (num < 0 && PyErr_Occurred())
194 return -1;
Tim Peters72d421b2000-08-04 03:05:40 +0000195
Fred Drake56780252000-06-15 14:50:20 +0000196 if (num < r->start || (num - r->start) % r->step)
197 return 0;
198 if (num > (r->start + (r->len * r->step)))
199 return 0;
200 return 1;
201}
202
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203static PySequenceMethods range_as_sequence = {
Guido van Rossum03093a21994-09-28 15:51:32 +0000204 (inquiry)range_length, /*sq_length*/
205 (binaryfunc)range_concat, /*sq_concat*/
206 (intargfunc)range_repeat, /*sq_repeat*/
207 (intargfunc)range_item, /*sq_item*/
208 (intintargfunc)range_slice, /*sq_slice*/
Guido van Rossum12d12c51993-10-26 17:58:25 +0000209 0, /*sq_ass_item*/
210 0, /*sq_ass_slice*/
Fred Drake56780252000-06-15 14:50:20 +0000211 (objobjproc)range_contains, /*sq_contains*/
Guido van Rossum12d12c51993-10-26 17:58:25 +0000212};
213
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214PyTypeObject PyRange_Type = {
215 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum12d12c51993-10-26 17:58:25 +0000216 0, /* Number of items for varobject */
Guido van Rossum2586bf01993-11-01 16:21:44 +0000217 "xrange", /* Name of this type */
Guido van Rossum12d12c51993-10-26 17:58:25 +0000218 sizeof(rangeobject), /* Basic object size */
219 0, /* Item size for varobject */
Guido van Rossum03093a21994-09-28 15:51:32 +0000220 (destructor)range_dealloc, /*tp_dealloc*/
Fred Drake2b83b462000-08-03 17:43:02 +0000221 0, /*tp_print*/
Guido van Rossum03093a21994-09-28 15:51:32 +0000222 (getattrfunc)range_getattr, /*tp_getattr*/
Guido van Rossum12d12c51993-10-26 17:58:25 +0000223 0, /*tp_setattr*/
Guido van Rossum03093a21994-09-28 15:51:32 +0000224 (cmpfunc)range_compare, /*tp_compare*/
Fred Drake2b83b462000-08-03 17:43:02 +0000225 (reprfunc)range_repr, /*tp_repr*/
Guido van Rossum12d12c51993-10-26 17:58:25 +0000226 0, /*tp_as_number*/
227 &range_as_sequence, /*tp_as_sequence*/
228 0, /*tp_as_mapping*/
Fred Drake56780252000-06-15 14:50:20 +0000229 0, /*tp_hash*/
230 0, /*tp_call*/
231 0, /*tp_str*/
232 0, /*tp_getattro*/
233 0, /*tp_setattro*/
234 0, /*tp_as_buffer*/
235 Py_TPFLAGS_DEFAULT, /*tp_flags*/
Guido van Rossum12d12c51993-10-26 17:58:25 +0000236};