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" |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 5 | |
| 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 | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 11 | int reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 12 | } rangeobject; |
| 13 | |
| 14 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 15 | PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 16 | PyRange_New(long start, long len, long step, int reps) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 17 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 18 | rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 19 | |
| 20 | obj->start = start; |
| 21 | obj->len = len; |
| 22 | obj->step = step; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 23 | obj->reps = reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 25 | return (PyObject *) obj; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static void |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 29 | range_dealloc(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 30 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 31 | PyObject_DEL(r); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 35 | range_item(rangeobject *r, int i) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 36 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 37 | if (i < 0 || i >= r->len * r->reps) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 5dadf7e | 1999-01-09 21:40:35 +0000 | [diff] [blame] | 39 | "xrange object index out of range"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 40 | return NULL; |
| 41 | } |
| 42 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 43 | return PyInt_FromLong(r->start + (i % r->len) * r->step); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static int |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 47 | range_length(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 48 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 49 | return r->len * r->reps; |
| 50 | } |
| 51 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 53 | range_repr(rangeobject *r) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 54 | { |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 55 | /* 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 Drake | 2b83b46 | 2000-08-03 17:43:02 +0000 | [diff] [blame] | 60 | |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 61 | if (r->start == 0 && r->step == 1) |
Fred Drake | c76e0e5 | 2000-08-04 02:34:41 +0000 | [diff] [blame] | 62 | sprintf(buf1, "xrange(%ld)", r->start + r->len * r->step); |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 63 | |
| 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 Drake | c76e0e5 | 2000-08-04 02:34:41 +0000 | [diff] [blame] | 71 | r->start, |
| 72 | r->start + r->len * r->step, |
| 73 | r->step); |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 74 | |
| 75 | if (r->reps != 1) |
Fred Drake | c76e0e5 | 2000-08-04 02:34:41 +0000 | [diff] [blame] | 76 | sprintf(buf2, "(%s * %d)", buf1, r->reps); |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 77 | |
Fred Drake | 2b83b46 | 2000-08-03 17:43:02 +0000 | [diff] [blame] | 78 | return PyString_FromString(r->reps == 1 ? buf1 : buf2); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 82 | range_concat(rangeobject *r, PyObject *obj) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 83 | { |
Guido van Rossum | 5dadf7e | 1999-01-09 21:40:35 +0000 | [diff] [blame] | 84 | PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects"); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 85 | return NULL; |
| 86 | } |
| 87 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 89 | range_repeat(rangeobject *r, int n) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 90 | { |
| 91 | if (n < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 92 | return (PyObject *) PyRange_New(0, 0, 1, 1); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 93 | |
| 94 | else if (n == 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 95 | Py_INCREF(r); |
| 96 | return (PyObject *) r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | return (PyObject *) PyRange_New( |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 101 | r->start, |
| 102 | r->len, |
| 103 | r->step, |
| 104 | r->reps * n); |
| 105 | } |
| 106 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 107 | static int |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 108 | range_compare(rangeobject *r1, rangeobject *r2) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 109 | { |
| 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 Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 118 | |
| 119 | else |
| 120 | return r1->reps - r2->reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 123 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 124 | range_slice(rangeobject *r, int low, int high) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 125 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 126 | if (r->reps != 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 127 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5dadf7e | 1999-01-09 21:40:35 +0000 | [diff] [blame] | 128 | "cannot slice a replicated xrange"); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 129 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | Py_INCREF(r); |
| 144 | return (PyObject *) r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 147 | return (PyObject *) PyRange_New( |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 148 | low * r->step + r->start, |
| 149 | high - low, |
| 150 | r->step, |
| 151 | 1); |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 152 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 154 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 155 | range_tolist(rangeobject *self, PyObject *args) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 156 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | PyObject *thelist; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 158 | int j; |
| 159 | int len = self->len * self->reps; |
| 160 | |
Fred Drake | 2b83b46 | 2000-08-03 17:43:02 +0000 | [diff] [blame] | 161 | if (! PyArg_ParseTuple(args, ":tolist")) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 162 | return NULL; |
| 163 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 164 | if ((thelist = PyList_New(len)) == NULL) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 165 | return NULL; |
| 166 | |
| 167 | for (j = 0; j < len; ++j) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 168 | if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong( |
| 169 | self->start + (j % self->len) * self->step))) < 0) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 170 | return NULL; |
| 171 | |
| 172 | return thelist; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 175 | static PyObject * |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 176 | range_getattr(rangeobject *r, char *name) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 177 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 178 | static PyMethodDef range_methods[] = { |
Fred Drake | 2b83b46 | 2000-08-03 17:43:02 +0000 | [diff] [blame] | 179 | {"tolist", (PyCFunction)range_tolist, METH_VARARGS, |
| 180 | "tolist() -> list\n" |
| 181 | "Return a list object with the same values."}, |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 182 | {NULL, NULL} |
| 183 | }; |
| 184 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 185 | return Py_FindMethod(range_methods, (PyObject *) r, name); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 188 | static int |
Fred Drake | 45cfbcc | 2000-07-09 06:21:27 +0000 | [diff] [blame] | 189 | range_contains(rangeobject *r, PyObject *obj) |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 190 | { |
| 191 | long num = PyInt_AsLong(obj); |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 192 | |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 193 | if (num < 0 && PyErr_Occurred()) |
| 194 | return -1; |
Tim Peters | 72d421b | 2000-08-04 03:05:40 +0000 | [diff] [blame] | 195 | |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 196 | 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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 203 | static PySequenceMethods range_as_sequence = { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 204 | (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 Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 209 | 0, /*sq_ass_item*/ |
| 210 | 0, /*sq_ass_slice*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 211 | (objobjproc)range_contains, /*sq_contains*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | PyTypeObject PyRange_Type = { |
| 215 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 216 | 0, /* Number of items for varobject */ |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 217 | "xrange", /* Name of this type */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 218 | sizeof(rangeobject), /* Basic object size */ |
| 219 | 0, /* Item size for varobject */ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 220 | (destructor)range_dealloc, /*tp_dealloc*/ |
Fred Drake | 2b83b46 | 2000-08-03 17:43:02 +0000 | [diff] [blame] | 221 | 0, /*tp_print*/ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 222 | (getattrfunc)range_getattr, /*tp_getattr*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 223 | 0, /*tp_setattr*/ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 224 | (cmpfunc)range_compare, /*tp_compare*/ |
Fred Drake | 2b83b46 | 2000-08-03 17:43:02 +0000 | [diff] [blame] | 225 | (reprfunc)range_repr, /*tp_repr*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 226 | 0, /*tp_as_number*/ |
| 227 | &range_as_sequence, /*tp_as_sequence*/ |
| 228 | 0, /*tp_as_mapping*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 229 | 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 Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 236 | }; |