Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Range object implementation */ |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 35 | |
| 36 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 37 | PyObject_HEAD |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 38 | long start; |
| 39 | long step; |
| 40 | long len; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 41 | int reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 42 | } rangeobject; |
| 43 | |
| 44 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | PyObject * |
| 46 | PyRange_New(start, len, step, reps) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 47 | long start, len, step; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 48 | int reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 49 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | rangeobject *obj = PyObject_NEW(rangeobject, &PyRange_Type); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 51 | |
| 52 | obj->start = start; |
| 53 | obj->len = len; |
| 54 | obj->step = step; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 55 | obj->reps = reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 57 | return (PyObject *) obj; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void |
| 61 | range_dealloc(r) |
| 62 | rangeobject *r; |
| 63 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | PyMem_DEL(r); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 68 | range_item(r, i) |
| 69 | rangeobject *r; |
| 70 | int i; |
| 71 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 72 | if (i < 0 || i >= r->len * r->reps) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 73 | PyErr_SetString(PyExc_IndexError, |
| 74 | "range object index out of range"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | return PyInt_FromLong(r->start + (i % r->len) * r->step); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int |
| 82 | range_length(r) |
| 83 | rangeobject *r; |
| 84 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 85 | return r->len * r->reps; |
| 86 | } |
| 87 | |
| 88 | static int |
| 89 | range_print(r, fp, flags) |
| 90 | rangeobject *r; |
| 91 | FILE *fp; |
| 92 | int flags; |
| 93 | { |
| 94 | int i, j; |
| 95 | |
| 96 | fprintf(fp, "("); |
| 97 | for (i = 0; i < r->reps; ++i) |
| 98 | for (j = 0; j < r->len; ++j) { |
| 99 | if (j > 0 || i > 0) |
| 100 | fprintf(fp, ", "); |
| 101 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 102 | fprintf(fp, "%ld", r->start + j * r->step); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (r->len == 1 && r->reps == 1) |
| 106 | fprintf(fp, ","); |
| 107 | fprintf(fp, ")"); |
| 108 | return 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 111 | static PyObject * |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 112 | range_repr(r) |
| 113 | rangeobject *r; |
| 114 | { |
| 115 | char buf[80]; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 116 | sprintf(buf, "(xrange(%ld, %ld, %ld) * %d)", |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 117 | r->start, |
| 118 | r->start + r->len * r->step, |
| 119 | r->step, |
| 120 | r->reps); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 121 | return PyString_FromString(buf); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 124 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 125 | range_concat(r, obj) |
| 126 | rangeobject *r; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 127 | PyObject *obj; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 128 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 129 | PyErr_SetString(PyExc_TypeError, "cannot concatenate range objects"); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 130 | return NULL; |
| 131 | } |
| 132 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 133 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 134 | range_repeat(r, n) |
| 135 | rangeobject *r; |
| 136 | int n; |
| 137 | { |
| 138 | if (n < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 139 | return (PyObject *) PyRange_New(0, 0, 1, 1); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 140 | |
| 141 | else if (n == 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 142 | Py_INCREF(r); |
| 143 | return (PyObject *) r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | else |
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 | r->start, |
| 149 | r->len, |
| 150 | r->step, |
| 151 | r->reps * n); |
| 152 | } |
| 153 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 154 | static int |
| 155 | range_compare(r1, r2) |
| 156 | rangeobject *r1, *r2; |
| 157 | { |
| 158 | if (r1->start != r2->start) |
| 159 | return r1->start - r2->start; |
| 160 | |
| 161 | else if (r1->step != r2->step) |
| 162 | return r1->step - r2->step; |
| 163 | |
| 164 | else if (r1->len != r2->len) |
| 165 | return r1->len - r2->len; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 166 | |
| 167 | else |
| 168 | return r1->reps - r2->reps; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 171 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 172 | range_slice(r, low, high) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 173 | rangeobject *r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 174 | int low, high; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 175 | { |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 176 | if (r->reps != 1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 177 | PyErr_SetString(PyExc_TypeError, |
| 178 | "cannot slice a replicated range"); |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
| 181 | if (low < 0) |
| 182 | low = 0; |
| 183 | else if (low > r->len) |
| 184 | low = r->len; |
| 185 | if (high < 0) |
| 186 | high = 0; |
| 187 | if (high < low) |
| 188 | high = low; |
| 189 | else if (high > r->len) |
| 190 | high = r->len; |
| 191 | |
| 192 | if (low == 0 && high == r->len) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 193 | Py_INCREF(r); |
| 194 | return (PyObject *) r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 197 | return (PyObject *) PyRange_New( |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 198 | low * r->step + r->start, |
| 199 | high - low, |
| 200 | r->step, |
| 201 | 1); |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 202 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 205 | range_tolist(self, args) |
| 206 | rangeobject *self; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 207 | PyObject *args; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 208 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 209 | PyObject *thelist; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 210 | int j; |
| 211 | int len = self->len * self->reps; |
| 212 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 213 | if (! PyArg_Parse(args, "")) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 214 | return NULL; |
| 215 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | if ((thelist = PyList_New(len)) == NULL) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 217 | return NULL; |
| 218 | |
| 219 | for (j = 0; j < len; ++j) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 220 | if ((PyList_SetItem(thelist, j, (PyObject *) PyInt_FromLong( |
| 221 | self->start + (j % self->len) * self->step))) < 0) |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 222 | return NULL; |
| 223 | |
| 224 | return thelist; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 227 | static PyObject * |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 228 | range_getattr(r, name) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 229 | rangeobject *r; |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 230 | char *name; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 231 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 232 | static PyMethodDef range_methods[] = { |
| 233 | {"tolist", (PyCFunction)range_tolist}, |
Guido van Rossum | 7d6aa51 | 1993-12-21 22:50:31 +0000 | [diff] [blame] | 234 | {NULL, NULL} |
| 235 | }; |
| 236 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 237 | return Py_FindMethod(range_methods, (PyObject *) r, name); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | static PySequenceMethods range_as_sequence = { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 241 | (inquiry)range_length, /*sq_length*/ |
| 242 | (binaryfunc)range_concat, /*sq_concat*/ |
| 243 | (intargfunc)range_repeat, /*sq_repeat*/ |
| 244 | (intargfunc)range_item, /*sq_item*/ |
| 245 | (intintargfunc)range_slice, /*sq_slice*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 246 | 0, /*sq_ass_item*/ |
| 247 | 0, /*sq_ass_slice*/ |
| 248 | }; |
| 249 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 250 | PyTypeObject PyRange_Type = { |
| 251 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 252 | 0, /* Number of items for varobject */ |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 253 | "xrange", /* Name of this type */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 254 | sizeof(rangeobject), /* Basic object size */ |
| 255 | 0, /* Item size for varobject */ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 256 | (destructor)range_dealloc, /*tp_dealloc*/ |
| 257 | (printfunc)range_print, /*tp_print*/ |
| 258 | (getattrfunc)range_getattr, /*tp_getattr*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 259 | 0, /*tp_setattr*/ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 260 | (cmpfunc)range_compare, /*tp_compare*/ |
| 261 | (reprfunc)range_repr, /*tp_repr*/ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 262 | 0, /*tp_as_number*/ |
| 263 | &range_as_sequence, /*tp_as_sequence*/ |
| 264 | 0, /*tp_as_mapping*/ |
| 265 | }; |