summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1993-11-01 16:21:44 (GMT)
committerGuido van Rossum <guido@python.org>1993-11-01 16:21:44 (GMT)
commit2586bf0a3434fc0430b6c16a56ad2b950eabc589 (patch)
treee16eef17c5be42833c891b22be5fbb4ef0ef3209
parent0667626694cf1d7232d3142aef645ecb2d8680c0 (diff)
downloadcpython-2586bf0a3434fc0430b6c16a56ad2b950eabc589.zip
cpython-2586bf0a3434fc0430b6c16a56ad2b950eabc589.tar.gz
cpython-2586bf0a3434fc0430b6c16a56ad2b950eabc589.tar.bz2
* rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
object.
-rw-r--r--Include/rangeobject.h2
-rw-r--r--Objects/rangeobject.c108
-rw-r--r--Python/bltinmodule.c53
3 files changed, 42 insertions, 121 deletions
diff --git a/Include/rangeobject.h b/Include/rangeobject.h
index 39d20c6..f2d6788 100644
--- a/Include/rangeobject.h
+++ b/Include/rangeobject.h
@@ -38,4 +38,4 @@ extern typeobject Rangetype;
#define is_rangeobject(op) ((op)->ob_type == &Rangetype)
-extern object *newrangeobject PROTO((long, long, long, int));
+extern object *newrangeobject PROTO((long, long, long));
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index a68d317..0bdbb66 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -31,21 +31,18 @@ typedef struct {
long start;
long step;
long len;
- int reps;
} rangeobject;
object *
-newrangeobject(start, len, step, reps)
+newrangeobject(start, len, step)
long start, len, step;
- int reps;
{
rangeobject *obj = (rangeobject *) newobject(&Rangetype);
obj->start = start;
obj->len = len;
obj->step = step;
- obj->reps = reps;
return (object *) obj;
}
@@ -62,19 +59,19 @@ range_item(r, i)
rangeobject *r;
int i;
{
- if (i < 0 || i >= r->len * r->reps) {
+ if (i < 0 || i >= r->len) {
err_setstr(IndexError, "range object index out of range");
return NULL;
}
- return newintobject(r->start + (i % r->len) * r->step);
+ return newintobject(r->start + i * r->step);
}
static int
range_length(r)
rangeobject *r;
{
- return r->len * r->reps;
+ return r->len;
}
static object *
@@ -82,57 +79,11 @@ range_repr(r)
rangeobject *r;
{
char buf[80];
- if (r->reps != 1)
- sprintf(buf, "(xrange(%ld, %ld, %ld) * %d)",
- r->start,
- r->start + r->len * r->step,
- r->step,
- r->reps);
- else
- sprintf(buf, "xrange(%ld, %ld, %ld)",
- r->start,
- r->start + r->len * r->step,
- r->step);
+ sprintf(buf, "xrange(%ld, %ld, %ld)",
+ r->start, r->start + r->len * r->step, r->step);
return newstringobject(buf);
}
-object *
-range_concat(r, obj)
- rangeobject *r;
- object *obj;
-{
- if (is_rangeobject(obj)) {
- rangeobject *s = (rangeobject *)obj;
- if (r->start == s->start && r->len == s->len &&
- r->step == s->step)
- return newrangeobject(r->start, r->len, r->step,
- r->reps + s->reps);
- }
- err_setstr(TypeError, "cannot concatenate different range objects");
- return NULL;
-}
-
-object *
-range_repeat(r, n)
- rangeobject *r;
- int n;
-{
- if (n < 0)
- return (object *) newrangeobject(0, 0, 1, 1);
-
- else if (n == 1) {
- INCREF(r);
- return (object *) r;
- }
-
- else
- return (object *) newrangeobject(
- r->start,
- r->len,
- r->step,
- r->reps * n);
-}
-
static int
range_compare(r1, r2)
rangeobject *r1, *r2;
@@ -145,36 +96,33 @@ range_compare(r1, r2)
else if (r1->len != r2->len)
return r1->len - r2->len;
+}
- else
- return r1->reps - r2->reps;
+static object *
+range_concat(r, s)
+ rangeobject *r;
+ object *s;
+{
+ err_setstr(TypeError, "concat not supported by xrange object");
+ return NULL;
}
static object *
-range_slice(r, low, high)
+range_repeat(r, n)
rangeobject *r;
- int low, high;
+ int n;
{
- if (r->reps != 1) {
- err_setstr(TypeError, "cannot slice a replicated range");
- return NULL;
- }
- if (low < 0)
- low = 0;
- else if (low > r->len)
- low = r->len;
- if (high < 0)
- high = 0;
- if (high < low)
- high = low;
- else if (high > r->len)
- high = r->len;
-
- return (object *) newrangeobject(
- low * r->step + r->start,
- high - low,
- r->step,
- 1);
+ err_setstr(TypeError, "repeat not supported by xrange object");
+ return NULL;
+}
+
+static object *
+range_slice(r, i, j)
+ rangeobject *r;
+ int i, j;
+{
+ err_setstr(TypeError, "slice not supported by xrange object");
+ return NULL;
}
static sequence_methods range_as_sequence = {
@@ -190,7 +138,7 @@ static sequence_methods range_as_sequence = {
typeobject Rangetype = {
OB_HEAD_INIT(&Typetype)
0, /* Number of items for varobject */
- "range", /* Name of this type */
+ "xrange", /* Name of this type */
sizeof(rangeobject), /* Basic object size */
0, /* Item size for varobject */
range_dealloc, /*tp_dealloc*/
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 6069ae0..5183069 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -922,7 +922,7 @@ builtin_xrange(self, v)
if (len < 0)
len = 0;
- return newrangeobject(start, len, step, 1);
+ return newrangeobject(start, len, step);
}
static object *
@@ -1280,16 +1280,10 @@ filtertuple(func, tuple)
{
object *result;
register int i, j;
- int len = gettuplesize(tuple), shared = 0;
+ int len = gettuplesize(tuple);
- if (tuple->ob_refcnt == 1) {
- result = tuple;
- shared = 1;
- /* defer INCREF (resizetuple wants it to be one) */
- }
- else
- if ((result = newtupleobject(len)) == NULL)
- return NULL;
+ if ((result = newtupleobject(len)) == NULL)
+ return NULL;
for (i = j = 0; i < len; ++i) {
object *item, *good;
@@ -1322,14 +1316,10 @@ filtertuple(func, tuple)
if (resizetuple(&result, j, 0) < 0)
return NULL;
- if (shared)
- INCREF(result);
-
return result;
Fail_1:
- if (!shared)
- DECREF(result);
+ DECREF(result);
return NULL;
}
@@ -1343,28 +1333,15 @@ filterstring(func, strobj)
{
object *result;
register int i, j;
- int len = getstringsize(strobj), shared = 0;
-
- if (strobj->ob_refcnt == 1) {
- result = strobj;
- shared = 1;
- /* defer INCREF (resizestring wants it to be one) */
+ int len = getstringsize(strobj);
- if (func == None) {
- INCREF(result);
- return result;
- }
- }
- else {
- if ((result = newsizedstringobject(NULL, len)) == NULL)
- return NULL;
-
- if (func == None) {
- strcpy(GETSTRINGVALUE((stringobject *)result),
- GETSTRINGVALUE((stringobject *)strobj));
- return result;
- }
+ if (func == None) {
+ /* No character is ever false -- share input string */
+ INCREF(result);
+ return result;
}
+ if ((result = newsizedstringobject(NULL, len)) == NULL)
+ return NULL;
for (i = j = 0; i < len; ++i) {
object *item, *arg, *good;
@@ -1391,13 +1368,9 @@ filterstring(func, strobj)
if (resizestring(&result, j) < 0)
return NULL;
- if (shared)
- INCREF(result);
-
return result;
Fail_1:
- if (!shared)
- DECREF(result);
+ DECREF(result);
return NULL;
}