Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Range object implementation */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | |
| 29 | typedef struct { |
| 30 | OB_HEAD |
| 31 | long start; |
| 32 | long step; |
| 33 | long len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 34 | } rangeobject; |
| 35 | |
| 36 | |
| 37 | object * |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 38 | newrangeobject(start, len, step) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 39 | long start, len, step; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 40 | { |
| 41 | rangeobject *obj = (rangeobject *) newobject(&Rangetype); |
| 42 | |
| 43 | obj->start = start; |
| 44 | obj->len = len; |
| 45 | obj->step = step; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 46 | |
| 47 | return (object *) obj; |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | range_dealloc(r) |
| 52 | rangeobject *r; |
| 53 | { |
| 54 | DEL(r); |
| 55 | } |
| 56 | |
| 57 | static object * |
| 58 | range_item(r, i) |
| 59 | rangeobject *r; |
| 60 | int i; |
| 61 | { |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 62 | if (i < 0 || i >= r->len) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 63 | err_setstr(IndexError, "range object index out of range"); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 67 | return newintobject(r->start + i * r->step); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int |
| 71 | range_length(r) |
| 72 | rangeobject *r; |
| 73 | { |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 74 | return r->len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static object * |
| 78 | range_repr(r) |
| 79 | rangeobject *r; |
| 80 | { |
| 81 | char buf[80]; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 82 | sprintf(buf, "xrange(%ld, %ld, %ld)", |
| 83 | r->start, r->start + r->len * r->step, r->step); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 84 | return newstringobject(buf); |
| 85 | } |
| 86 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 87 | static int |
| 88 | range_compare(r1, r2) |
| 89 | rangeobject *r1, *r2; |
| 90 | { |
| 91 | if (r1->start != r2->start) |
| 92 | return r1->start - r2->start; |
| 93 | |
| 94 | else if (r1->step != r2->step) |
| 95 | return r1->step - r2->step; |
| 96 | |
| 97 | else if (r1->len != r2->len) |
| 98 | return r1->len - r2->len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static object * |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 102 | range_concat(r, s) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 103 | rangeobject *r; |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 104 | object *s; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 105 | { |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 106 | err_setstr(TypeError, "concat not supported by xrange object"); |
| 107 | return NULL; |
| 108 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 110 | static object * |
| 111 | range_repeat(r, n) |
| 112 | rangeobject *r; |
| 113 | int n; |
| 114 | { |
| 115 | err_setstr(TypeError, "repeat not supported by xrange object"); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | static object * |
| 120 | range_slice(r, i, j) |
| 121 | rangeobject *r; |
| 122 | int i, j; |
| 123 | { |
| 124 | err_setstr(TypeError, "slice not supported by xrange object"); |
| 125 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static sequence_methods range_as_sequence = { |
| 129 | range_length, /*sq_length*/ |
| 130 | range_concat, /*sq_concat*/ |
| 131 | range_repeat, /*sq_repeat*/ |
| 132 | range_item, /*sq_item*/ |
| 133 | range_slice, /*sq_slice*/ |
| 134 | 0, /*sq_ass_item*/ |
| 135 | 0, /*sq_ass_slice*/ |
| 136 | }; |
| 137 | |
| 138 | typeobject Rangetype = { |
| 139 | OB_HEAD_INIT(&Typetype) |
| 140 | 0, /* Number of items for varobject */ |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 141 | "xrange", /* Name of this type */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 142 | sizeof(rangeobject), /* Basic object size */ |
| 143 | 0, /* Item size for varobject */ |
| 144 | range_dealloc, /*tp_dealloc*/ |
| 145 | 0, /*tp_print*/ |
| 146 | 0, /*tp_getattr*/ |
| 147 | 0, /*tp_setattr*/ |
| 148 | range_compare, /*tp_compare*/ |
| 149 | range_repr, /*tp_repr*/ |
| 150 | 0, /*tp_as_number*/ |
| 151 | &range_as_sequence, /*tp_as_sequence*/ |
| 152 | 0, /*tp_as_mapping*/ |
| 153 | }; |