Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Range object implementation */ |
| 12 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 14 | |
| 15 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 16 | PyObject_HEAD |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 17 | long start; |
| 18 | long step; |
| 19 | long len; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 20 | int reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 21 | } rangeobject; |
| 22 | |
| 23 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 24 | PyObject * |
| 25 | PyRange_New(start, len, step, reps) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 26 | long start, len, step; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 27 | int reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 28 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 29 | rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 30 | |
| 31 | obj->start = start; |
| 32 | obj->len = len; |
| 33 | obj->step = step; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 34 | obj->reps = reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 36 | return (PyObject *) obj; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static void |
| 40 | range_dealloc(r) |
| 41 | rangeobject *r; |
| 42 | { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 43 | PyObject_DEL(r); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 46 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 47 | range_item(r, i) |
| 48 | rangeobject *r; |
| 49 | int i; |
| 50 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 51 | if (i < 0 || i >= r->len * r->reps) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | PyErr_SetString(PyExc_IndexError, |
Guido van Rossum | 5dadf7e | 1999-01-09 21:40:35 +0000 | [diff] [blame] | 53 | "xrange object index out of range"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
| 56 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | return PyInt_FromLong(r->start + (i % r->len) * r->step); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int |
| 61 | range_length(r) |
| 62 | rangeobject *r; |
| 63 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 64 | return r->len * r->reps; |
| 65 | } |
| 66 | |
| 67 | static int |
| 68 | range_print(r, fp, flags) |
| 69 | rangeobject *r; |
| 70 | FILE *fp; |
| 71 | int flags; |
| 72 | { |
| 73 | int i, j; |
| 74 | |
| 75 | fprintf(fp, "("); |
| 76 | for (i = 0; i < r->reps; ++i) |
| 77 | for (j = 0; j < r->len; ++j) { |
| 78 | if (j > 0 || i > 0) |
| 79 | fprintf(fp, ", "); |
| 80 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 81 | fprintf(fp, "%ld", r->start + j * r->step); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (r->len == 1 && r->reps == 1) |
| 85 | fprintf(fp, ","); |
| 86 | fprintf(fp, ")"); |
| 87 | return 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 91 | range_repr(r) |
| 92 | rangeobject *r; |
| 93 | { |
| 94 | char buf[80]; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 95 | sprintf(buf, "(xrange(%ld, %ld, %ld) * %d)", |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 96 | r->start, |
| 97 | r->start + r->len * r->step, |
| 98 | r->step, |
| 99 | r->reps); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | return PyString_FromString(buf); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 103 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 104 | range_concat(r, obj) |
| 105 | rangeobject *r; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 106 | PyObject *obj; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 107 | { |
Guido van Rossum | 5dadf7e | 1999-01-09 21:40:35 +0000 | [diff] [blame] | 108 | PyErr_SetString(PyExc_TypeError, "cannot concatenate xrange objects"); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 109 | return NULL; |
| 110 | } |
| 111 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 113 | range_repeat(r, n) |
| 114 | rangeobject *r; |
| 115 | int n; |
| 116 | { |
| 117 | if (n < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | return (PyObject *) PyRange_New(0, 0, 1, 1); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 119 | |
| 120 | else if (n == 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 121 | Py_INCREF(r); |
| 122 | return (PyObject *) r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | return (PyObject *) PyRange_New( |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 127 | r->start, |
| 128 | r->len, |
| 129 | r->step, |
| 130 | r->reps * n); |
| 131 | } |
| 132 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 133 | static int |
| 134 | range_compare(r1, r2) |
| 135 | rangeobject *r1, *r2; |
| 136 | { |
| 137 | if (r1->start != r2->start) |
| 138 | return r1->start - r2->start; |
| 139 | |
| 140 | else if (r1->step != r2->step) |
| 141 | return r1->step - r2->step; |
| 142 | |
| 143 | else if (r1->len != r2->len) |
| 144 | return r1->len - r2->len; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 145 | |
| 146 | else |
| 147 | return r1->reps - r2->reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 151 | range_slice(r, low, high) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 152 | rangeobject *r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 153 | int low, high; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 154 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 155 | if (r->reps != 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 156 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 5dadf7e | 1999-01-09 21:40:35 +0000 | [diff] [blame] | 157 | "cannot slice a replicated xrange"); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
| 160 | if (low < 0) |
| 161 | low = 0; |
| 162 | else if (low > r->len) |
| 163 | low = r->len; |
| 164 | if (high < 0) |
| 165 | high = 0; |
| 166 | if (high < low) |
| 167 | high = low; |
| 168 | else if (high > r->len) |
| 169 | high = r->len; |
| 170 | |
| 171 | if (low == 0 && high == r->len) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 172 | Py_INCREF(r); |
| 173 | return (PyObject *) r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 176 | return (PyObject *) PyRange_New( |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 177 | low * r->step + r->start, |
| 178 | high - low, |
| 179 | r->step, |
| 180 | 1); |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 181 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 183 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 184 | range_tolist(self, args) |
| 185 | rangeobject *self; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 186 | PyObject *args; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 187 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 188 | PyObject *thelist; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 189 | int j; |
| 190 | int len = self->len * self->reps; |
| 191 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 192 | if (! PyArg_Parse(args, "")) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 193 | return NULL; |
| 194 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 195 | if ((thelist = PyList_New(len)) == NULL) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 196 | return NULL; |
| 197 | |
| 198 | for (j = 0; j < len; ++j) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 199 | if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong( |
| 200 | self->start + (j % self->len) * self->step))) < 0) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 201 | return NULL; |
| 202 | |
| 203 | return thelist; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 206 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 207 | range_getattr(r, name) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 208 | rangeobject *r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 209 | char *name; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 210 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 211 | static PyMethodDef range_methods[] = { |
| 212 | {"tolist", (PyCFunction)range_tolist}, |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 213 | {NULL, NULL} |
| 214 | }; |
| 215 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | return Py_FindMethod(range_methods, (PyObject *) r, name); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 219 | static int |
| 220 | range_contains(r, obj) |
| 221 | rangeobject * r; |
| 222 | PyObject * obj; |
| 223 | { |
| 224 | long num = PyInt_AsLong(obj); |
| 225 | |
| 226 | if (num < 0 && PyErr_Occurred()) |
| 227 | return -1; |
| 228 | |
| 229 | if (num < r->start || (num - r->start) % r->step) |
| 230 | return 0; |
| 231 | if (num > (r->start + (r->len * r->step))) |
| 232 | return 0; |
| 233 | return 1; |
| 234 | } |
| 235 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 236 | static PySequenceMethods range_as_sequence = { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 237 | (inquiry)range_length, /*sq_length*/ |
| 238 | (binaryfunc)range_concat, /*sq_concat*/ |
| 239 | (intargfunc)range_repeat, /*sq_repeat*/ |
| 240 | (intargfunc)range_item, /*sq_item*/ |
| 241 | (intintargfunc)range_slice, /*sq_slice*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 242 | 0, /*sq_ass_item*/ |
| 243 | 0, /*sq_ass_slice*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 244 | (objobjproc)range_contains, /*sq_contains*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 247 | PyTypeObject PyRange_Type = { |
| 248 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 249 | 0, /* Number of items for varobject */ |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 250 | "xrange", /* Name of this type */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 251 | sizeof(rangeobject), /* Basic object size */ |
| 252 | 0, /* Item size for varobject */ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 253 | (destructor)range_dealloc, /*tp_dealloc*/ |
| 254 | (printfunc)range_print, /*tp_print*/ |
| 255 | (getattrfunc)range_getattr, /*tp_getattr*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 256 | 0, /*tp_setattr*/ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 257 | (cmpfunc)range_compare, /*tp_compare*/ |
| 258 | (reprfunc)range_repr, /*tp_repr*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 259 | 0, /*tp_as_number*/ |
| 260 | &range_as_sequence, /*tp_as_sequence*/ |
| 261 | 0, /*tp_as_mapping*/ |
Fred Drake | 5678025 | 2000-06-15 14:50:20 +0000 | [diff] [blame] | 262 | 0, /*tp_hash*/ |
| 263 | 0, /*tp_call*/ |
| 264 | 0, /*tp_str*/ |
| 265 | 0, /*tp_getattro*/ |
| 266 | 0, /*tp_setattro*/ |
| 267 | 0, /*tp_as_buffer*/ |
| 268 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 269 | }; |