* rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range

object.
This commit is contained in:
Guido van Rossum 1993-11-01 16:21:44 +00:00
parent 0667626694
commit 2586bf0a34
3 changed files with 42 additions and 121 deletions

View File

@ -38,4 +38,4 @@ extern typeobject Rangetype;
#define is_rangeobject(op) ((op)->ob_type == &Rangetype) #define is_rangeobject(op) ((op)->ob_type == &Rangetype)
extern object *newrangeobject PROTO((long, long, long, int)); extern object *newrangeobject PROTO((long, long, long));

View File

@ -31,21 +31,18 @@ typedef struct {
long start; long start;
long step; long step;
long len; long len;
int reps;
} rangeobject; } rangeobject;
object * object *
newrangeobject(start, len, step, reps) newrangeobject(start, len, step)
long start, len, step; long start, len, step;
int reps;
{ {
rangeobject *obj = (rangeobject *) newobject(&Rangetype); rangeobject *obj = (rangeobject *) newobject(&Rangetype);
obj->start = start; obj->start = start;
obj->len = len; obj->len = len;
obj->step = step; obj->step = step;
obj->reps = reps;
return (object *) obj; return (object *) obj;
} }
@ -62,19 +59,19 @@ range_item(r, i)
rangeobject *r; rangeobject *r;
int i; 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"); err_setstr(IndexError, "range object index out of range");
return NULL; return NULL;
} }
return newintobject(r->start + (i % r->len) * r->step); return newintobject(r->start + i * r->step);
} }
static int static int
range_length(r) range_length(r)
rangeobject *r; rangeobject *r;
{ {
return r->len * r->reps; return r->len;
} }
static object * static object *
@ -82,57 +79,11 @@ range_repr(r)
rangeobject *r; rangeobject *r;
{ {
char buf[80]; char buf[80];
if (r->reps != 1) sprintf(buf, "xrange(%ld, %ld, %ld)",
sprintf(buf, "(xrange(%ld, %ld, %ld) * %d)", r->start, r->start + r->len * r->step, r->step);
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);
return newstringobject(buf); 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 static int
range_compare(r1, r2) range_compare(r1, r2)
rangeobject *r1, *r2; rangeobject *r1, *r2;
@ -145,36 +96,33 @@ range_compare(r1, r2)
else if (r1->len != r2->len) else if (r1->len != r2->len)
return r1->len - r2->len; return r1->len - r2->len;
else
return r1->reps - r2->reps;
} }
static object * static object *
range_slice(r, low, high) range_concat(r, s)
rangeobject *r; rangeobject *r;
int low, high; object *s;
{ {
if (r->reps != 1) { err_setstr(TypeError, "concat not supported by xrange object");
err_setstr(TypeError, "cannot slice a replicated range"); return NULL;
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( static object *
low * r->step + r->start, range_repeat(r, n)
high - low, rangeobject *r;
r->step, int n;
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 = { static sequence_methods range_as_sequence = {
@ -190,7 +138,7 @@ static sequence_methods range_as_sequence = {
typeobject Rangetype = { typeobject Rangetype = {
OB_HEAD_INIT(&Typetype) OB_HEAD_INIT(&Typetype)
0, /* Number of items for varobject */ 0, /* Number of items for varobject */
"range", /* Name of this type */ "xrange", /* Name of this type */
sizeof(rangeobject), /* Basic object size */ sizeof(rangeobject), /* Basic object size */
0, /* Item size for varobject */ 0, /* Item size for varobject */
range_dealloc, /*tp_dealloc*/ range_dealloc, /*tp_dealloc*/

View File

@ -922,7 +922,7 @@ builtin_xrange(self, v)
if (len < 0) if (len < 0)
len = 0; len = 0;
return newrangeobject(start, len, step, 1); return newrangeobject(start, len, step);
} }
static object * static object *
@ -1280,16 +1280,10 @@ filtertuple(func, tuple)
{ {
object *result; object *result;
register int i, j; register int i, j;
int len = gettuplesize(tuple), shared = 0; int len = gettuplesize(tuple);
if (tuple->ob_refcnt == 1) { if ((result = newtupleobject(len)) == NULL)
result = tuple; return NULL;
shared = 1;
/* defer INCREF (resizetuple wants it to be one) */
}
else
if ((result = newtupleobject(len)) == NULL)
return NULL;
for (i = j = 0; i < len; ++i) { for (i = j = 0; i < len; ++i) {
object *item, *good; object *item, *good;
@ -1322,14 +1316,10 @@ filtertuple(func, tuple)
if (resizetuple(&result, j, 0) < 0) if (resizetuple(&result, j, 0) < 0)
return NULL; return NULL;
if (shared)
INCREF(result);
return result; return result;
Fail_1: Fail_1:
if (!shared) DECREF(result);
DECREF(result);
return NULL; return NULL;
} }
@ -1343,28 +1333,15 @@ filterstring(func, strobj)
{ {
object *result; object *result;
register int i, j; register int i, j;
int len = getstringsize(strobj), shared = 0; int len = getstringsize(strobj);
if (strobj->ob_refcnt == 1) { if (func == None) {
result = strobj; /* No character is ever false -- share input string */
shared = 1; INCREF(result);
/* defer INCREF (resizestring wants it to be one) */ return result;
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 ((result = newsizedstringobject(NULL, len)) == NULL)
return NULL;
for (i = j = 0; i < len; ++i) { for (i = j = 0; i < len; ++i) {
object *item, *arg, *good; object *item, *arg, *good;
@ -1391,13 +1368,9 @@ filterstring(func, strobj)
if (resizestring(&result, j) < 0) if (resizestring(&result, j) < 0)
return NULL; return NULL;
if (shared)
INCREF(result);
return result; return result;
Fail_1: Fail_1:
if (!shared) DECREF(result);
DECREF(result);
return NULL; return NULL;
} }