* 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)
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 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);
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_slice(r, low, high)
range_concat(r, s)
rangeobject *r;
int low, high;
object *s;
{
if (r->reps != 1) {
err_setstr(TypeError, "cannot slice a replicated range");
err_setstr(TypeError, "concat not supported by xrange object");
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);
static object *
range_repeat(r, n)
rangeobject *r;
int n;
{
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*/

View File

@ -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,14 +1280,8 @@ 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;
@ -1322,13 +1316,9 @@ filtertuple(func, tuple)
if (resizetuple(&result, j, 0) < 0)
return NULL;
if (shared)
INCREF(result);
return result;
Fail_1:
if (!shared)
DECREF(result);
return NULL;
}
@ -1343,29 +1333,16 @@ 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) {
/* No character is ever false -- share input string */
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;
}
}
for (i = j = 0; i < len; ++i) {
object *item, *arg, *good;
int ok;
@ -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);
return NULL;
}