annotate Objects/rangeobject.c @ 1944:8e8fddcbdfd2 legacy-trunk

Hopefully the last edits for 1.1
author Guido van Rossum <guido@python.org>
date Thu, 06 Oct 1994 16:15:09 +0000
parents 0437738279a8
children 866c81068dbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /***********************************************************
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
3 Amsterdam, The Netherlands.
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 documentation for any purpose and without fee is hereby granted,
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 provided that the above copyright notice appear in all copies and that
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 both that copyright notice and this permission notice appear in
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 distribution of the software without specific, written prior permission.
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 ******************************************************************/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Range object implementation */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #include "allobjects.h"
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 typedef struct {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 OB_HEAD
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 long start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 long step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 long len;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
34 int reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 } rangeobject;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 object *
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
39 newrangeobject(start, len, step, reps)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 long start, len, step;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
41 int reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 rangeobject *obj = (rangeobject *) newobject(&Rangetype);
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 obj->start = start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 obj->len = len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 obj->step = step;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
48 obj->reps = reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 return (object *) obj;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 static void
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 range_dealloc(r)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 rangeobject *r;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 DEL(r);
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 static object *
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 range_item(r, i)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 rangeobject *r;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 int i;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
65 if (i < 0 || i >= r->len * r->reps) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 err_setstr(IndexError, "range object index out of range");
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
70 return newintobject(r->start + (i % r->len) * r->step);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 static int
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 range_length(r)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 rangeobject *r;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
77 return r->len * r->reps;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
78 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
79
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
80 static int
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
81 range_print(r, fp, flags)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
82 rangeobject *r;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
83 FILE *fp;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
84 int flags;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
85 {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
86 int i, j;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
87
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
88 fprintf(fp, "(");
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
89 for (i = 0; i < r->reps; ++i)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
90 for (j = 0; j < r->len; ++j) {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
91 if (j > 0 || i > 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
92 fprintf(fp, ", ");
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
93
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
94 fprintf(fp, "%d", r->start + j * r->step);
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
95 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
96
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
97 if (r->len == 1 && r->reps == 1)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
98 fprintf(fp, ",");
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
99 fprintf(fp, ")");
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
100 return 0;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 static object *
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 range_repr(r)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 rangeobject *r;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 char buf[80];
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
108 sprintf(buf, "(xrange(%ld, %ld, %ld) * %d)",
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
109 r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
110 r->start + r->len * r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
111 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
112 r->reps);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 return newstringobject(buf);
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
116 object *
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
117 range_concat(r, obj)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
118 rangeobject *r;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
119 object *obj;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
120 {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
121 err_setstr(TypeError, "cannot concatenate range objects");
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
122 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
123 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
124
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
125 object *
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
126 range_repeat(r, n)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
127 rangeobject *r;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
128 int n;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
129 {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
130 if (n < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
131 return (object *) newrangeobject(0, 0, 1, 1);
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
132
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
133 else if (n == 1) {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
134 INCREF(r);
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
135 return (object *) r;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
136 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
137
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
138 else
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
139 return (object *) newrangeobject(
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
140 r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
141 r->len,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
142 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
143 r->reps * n);
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
144 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
145
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 static int
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 range_compare(r1, r2)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 rangeobject *r1, *r2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if (r1->start != r2->start)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 return r1->start - r2->start;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 else if (r1->step != r2->step)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 return r1->step - r2->step;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
155
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 else if (r1->len != r2->len)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 return r1->len - r2->len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
159 else
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
160 return r1->reps - r2->reps;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 static object *
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
164 range_slice(r, low, high)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 rangeobject *r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
166 int low, high;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
168 if (r->reps != 1) {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
169 err_setstr(TypeError, "cannot slice a replicated range");
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
170 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
171 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
172 if (low < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
173 low = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
174 else if (low > r->len)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
175 low = r->len;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
176 if (high < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
177 high = 0;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
178 if (high < low)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
179 high = low;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
180 else if (high > r->len)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
181 high = r->len;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
182
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
183 if (low == 0 && high == r->len) {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
184 INCREF(r);
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
185 return (object *) r;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
186 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
187
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
188 return (object *) newrangeobject(
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
189 low * r->step + r->start,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
190 high - low,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
191 r->step,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
192 1);
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
193 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
195 static object *
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
196 range_tolist(self, args)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
197 rangeobject *self;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
198 object *args;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
199 {
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
200 object *thelist;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
201 int j;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
202 int len = self->len * self->reps;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
203
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
204 if (! getargs(args, ""))
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
205 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
206
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
207 if ((thelist = newlistobject(len)) == NULL)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
208 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
209
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
210 for (j = 0; j < len; ++j)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
211 if ((setlistitem(thelist, j,
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
212 (object *) newintobject(
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
213 self->start + (j % self->len) * self->step))) < 0)
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
214 return NULL;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
215
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
216 return thelist;
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
217 }
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
218
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
219 static object *
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
220 range_getattr(r, name)
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
221 rangeobject *r;
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
222 char *name;
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
223 {
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
224 static struct methodlist range_methods[] = {
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
225 {"tolist", (method)range_tolist},
1444
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
226 {NULL, NULL}
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
227 };
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
228
f2d04054c934 * rangeobject.[ch], bltinmodule.c: incorporate new version of range
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
229 return findmethod(range_methods, (object *) r, name);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 static sequence_methods range_as_sequence = {
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
233 (inquiry)range_length, /*sq_length*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
234 (binaryfunc)range_concat, /*sq_concat*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
235 (intargfunc)range_repeat, /*sq_repeat*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
236 (intargfunc)range_item, /*sq_item*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
237 (intintargfunc)range_slice, /*sq_slice*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 0, /*sq_ass_item*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 0, /*sq_ass_slice*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 };
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 typeobject Rangetype = {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 OB_HEAD_INIT(&Typetype)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 0, /* Number of items for varobject */
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
245 "xrange", /* Name of this type */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 sizeof(rangeobject), /* Basic object size */
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 0, /* Item size for varobject */
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
248 (destructor)range_dealloc, /*tp_dealloc*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
249 (printfunc)range_print, /*tp_print*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
250 (getattrfunc)range_getattr, /*tp_getattr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 0, /*tp_setattr*/
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
252 (cmpfunc)range_compare, /*tp_compare*/
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
253 (reprfunc)range_repr, /*tp_repr*/
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 0, /*tp_as_number*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 &range_as_sequence, /*tp_as_sequence*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 0, /*tp_as_mapping*/
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 };