1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1993-01-26 09:33:44 -04:00
|
|
|
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
|
|
|
|
Amsterdam, The Netherlands.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* List object implementation */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "allobjects.h"
|
1992-01-27 12:53:23 -04:00
|
|
|
#include "modsupport.h"
|
1992-08-05 16:58:53 -03:00
|
|
|
#include "ceval.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
object *
|
|
|
|
newlistobject(size)
|
|
|
|
int size;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
listobject *op;
|
1992-08-19 13:46:30 -03:00
|
|
|
MALLARG nbytes;
|
1990-10-14 09:07:46 -03:00
|
|
|
if (size < 0) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-19 13:46:30 -03:00
|
|
|
nbytes = size * sizeof(object *);
|
|
|
|
/* Check for overflow */
|
|
|
|
if (nbytes / sizeof(object *) != size) {
|
|
|
|
return err_nomem();
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
op = (listobject *) malloc(sizeof(listobject));
|
|
|
|
if (op == NULL) {
|
1990-10-21 19:15:08 -03:00
|
|
|
return err_nomem();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
if (size <= 0) {
|
|
|
|
op->ob_item = NULL;
|
|
|
|
}
|
|
|
|
else {
|
1992-08-19 13:46:30 -03:00
|
|
|
op->ob_item = (object **) malloc(nbytes);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op->ob_item == NULL) {
|
|
|
|
free((ANY *)op);
|
1990-10-21 19:15:08 -03:00
|
|
|
return err_nomem();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
NEWREF(op);
|
|
|
|
op->ob_type = &Listtype;
|
|
|
|
op->ob_size = size;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
op->ob_item[i] = NULL;
|
|
|
|
return (object *) op;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
getlistsize(op)
|
|
|
|
object *op;
|
|
|
|
{
|
|
|
|
if (!is_listobject(op)) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ((listobject *)op) -> ob_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
getlistitem(op, i)
|
|
|
|
object *op;
|
|
|
|
int i;
|
|
|
|
{
|
|
|
|
if (!is_listobject(op)) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (i < 0 || i >= ((listobject *)op) -> ob_size) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_setstr(IndexError, "list index out of range");
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((listobject *)op) -> ob_item[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
setlistitem(op, i, newitem)
|
|
|
|
register object *op;
|
|
|
|
register int i;
|
|
|
|
register object *newitem;
|
|
|
|
{
|
|
|
|
register object *olditem;
|
|
|
|
if (!is_listobject(op)) {
|
1991-10-20 17:20:40 -03:00
|
|
|
XDECREF(newitem);
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
|
|
|
return -1;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
if (i < 0 || i >= ((listobject *)op) -> ob_size) {
|
1991-10-20 17:20:40 -03:00
|
|
|
XDECREF(newitem);
|
1990-10-21 19:15:08 -03:00
|
|
|
err_setstr(IndexError, "list assignment index out of range");
|
|
|
|
return -1;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
olditem = ((listobject *)op) -> ob_item[i];
|
|
|
|
((listobject *)op) -> ob_item[i] = newitem;
|
1991-10-20 17:20:40 -03:00
|
|
|
XDECREF(olditem);
|
1990-10-14 09:07:46 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ins1(self, where, v)
|
|
|
|
listobject *self;
|
|
|
|
int where;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
object **items;
|
1990-10-21 19:15:08 -03:00
|
|
|
if (v == NULL) {
|
|
|
|
err_badcall();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
items = self->ob_item;
|
|
|
|
RESIZE(items, object *, self->ob_size+1);
|
1990-10-21 19:15:08 -03:00
|
|
|
if (items == NULL) {
|
|
|
|
err_nomem();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
if (where < 0)
|
|
|
|
where = 0;
|
|
|
|
if (where > self->ob_size)
|
|
|
|
where = self->ob_size;
|
|
|
|
for (i = self->ob_size; --i >= where; )
|
|
|
|
items[i+1] = items[i];
|
|
|
|
INCREF(v);
|
|
|
|
items[where] = v;
|
|
|
|
self->ob_item = items;
|
|
|
|
self->ob_size++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
inslistitem(op, where, newitem)
|
|
|
|
object *op;
|
|
|
|
int where;
|
|
|
|
object *newitem;
|
|
|
|
{
|
1990-10-21 19:15:08 -03:00
|
|
|
if (!is_listobject(op)) {
|
|
|
|
err_badcall();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
return ins1((listobject *)op, where, newitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
addlistitem(op, newitem)
|
|
|
|
object *op;
|
|
|
|
object *newitem;
|
|
|
|
{
|
1990-10-21 19:15:08 -03:00
|
|
|
if (!is_listobject(op)) {
|
|
|
|
err_badcall();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
return ins1((listobject *)op,
|
|
|
|
(int) ((listobject *)op)->ob_size, newitem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
|
|
|
list_dealloc(op)
|
|
|
|
listobject *op;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < op->ob_size; i++) {
|
1991-10-20 17:20:40 -03:00
|
|
|
XDECREF(op->ob_item[i]);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
if (op->ob_item != NULL)
|
|
|
|
free((ANY *)op->ob_item);
|
|
|
|
free((ANY *)op);
|
|
|
|
}
|
|
|
|
|
1991-06-07 13:10:43 -03:00
|
|
|
static int
|
1990-10-14 09:07:46 -03:00
|
|
|
list_print(op, fp, flags)
|
|
|
|
listobject *op;
|
|
|
|
FILE *fp;
|
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
fprintf(fp, "[");
|
1991-06-07 13:10:43 -03:00
|
|
|
for (i = 0; i < op->ob_size; i++) {
|
|
|
|
if (i > 0)
|
1990-10-14 09:07:46 -03:00
|
|
|
fprintf(fp, ", ");
|
1991-06-07 13:10:43 -03:00
|
|
|
if (printobject(op->ob_item[i], fp, flags) != 0)
|
|
|
|
return -1;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
fprintf(fp, "]");
|
1991-06-07 13:10:43 -03:00
|
|
|
return 0;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1993-06-17 09:35:49 -03:00
|
|
|
static object *
|
1990-10-14 09:07:46 -03:00
|
|
|
list_repr(v)
|
|
|
|
listobject *v;
|
|
|
|
{
|
|
|
|
object *s, *t, *comma;
|
|
|
|
int i;
|
|
|
|
s = newstringobject("[");
|
|
|
|
comma = newstringobject(", ");
|
|
|
|
for (i = 0; i < v->ob_size && s != NULL; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
joinstring(&s, comma);
|
|
|
|
t = reprobject(v->ob_item[i]);
|
|
|
|
joinstring(&s, t);
|
|
|
|
DECREF(t);
|
|
|
|
}
|
|
|
|
DECREF(comma);
|
|
|
|
t = newstringobject("]");
|
|
|
|
joinstring(&s, t);
|
|
|
|
DECREF(t);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
list_compare(v, w)
|
|
|
|
listobject *v, *w;
|
|
|
|
{
|
|
|
|
int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
int cmp = cmpobject(v->ob_item[i], w->ob_item[i]);
|
|
|
|
if (cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
return v->ob_size - w->ob_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
list_length(a)
|
|
|
|
listobject *a;
|
|
|
|
{
|
|
|
|
return a->ob_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
list_item(a, i)
|
|
|
|
listobject *a;
|
|
|
|
int i;
|
|
|
|
{
|
|
|
|
if (i < 0 || i >= a->ob_size) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_setstr(IndexError, "list index out of range");
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
INCREF(a->ob_item[i]);
|
|
|
|
return a->ob_item[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
list_slice(a, ilow, ihigh)
|
|
|
|
listobject *a;
|
|
|
|
int ilow, ihigh;
|
|
|
|
{
|
|
|
|
listobject *np;
|
|
|
|
int i;
|
|
|
|
if (ilow < 0)
|
|
|
|
ilow = 0;
|
|
|
|
else if (ilow > a->ob_size)
|
|
|
|
ilow = a->ob_size;
|
|
|
|
if (ihigh < 0)
|
|
|
|
ihigh = 0;
|
|
|
|
if (ihigh < ilow)
|
|
|
|
ihigh = ilow;
|
|
|
|
else if (ihigh > a->ob_size)
|
|
|
|
ihigh = a->ob_size;
|
|
|
|
np = (listobject *) newlistobject(ihigh - ilow);
|
|
|
|
if (np == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = ilow; i < ihigh; i++) {
|
|
|
|
object *v = a->ob_item[i];
|
|
|
|
INCREF(v);
|
|
|
|
np->ob_item[i - ilow] = v;
|
|
|
|
}
|
|
|
|
return (object *)np;
|
|
|
|
}
|
|
|
|
|
1993-06-17 09:35:49 -03:00
|
|
|
object *
|
|
|
|
getlistslice(a, ilow, ihigh)
|
|
|
|
object *a;
|
|
|
|
int ilow, ihigh;
|
|
|
|
{
|
|
|
|
if (!is_listobject(a)) {
|
|
|
|
err_badcall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return list_slice((listobject *)a, ilow, ihigh);
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static object *
|
|
|
|
list_concat(a, bb)
|
|
|
|
listobject *a;
|
|
|
|
object *bb;
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int i;
|
|
|
|
listobject *np;
|
|
|
|
if (!is_listobject(bb)) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badarg();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#define b ((listobject *)bb)
|
|
|
|
size = a->ob_size + b->ob_size;
|
|
|
|
np = (listobject *) newlistobject(size);
|
|
|
|
if (np == NULL) {
|
1991-06-07 13:10:43 -03:00
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
for (i = 0; i < a->ob_size; i++) {
|
|
|
|
object *v = a->ob_item[i];
|
|
|
|
INCREF(v);
|
|
|
|
np->ob_item[i] = v;
|
|
|
|
}
|
|
|
|
for (i = 0; i < b->ob_size; i++) {
|
|
|
|
object *v = b->ob_item[i];
|
|
|
|
INCREF(v);
|
|
|
|
np->ob_item[i + a->ob_size] = v;
|
|
|
|
}
|
|
|
|
return (object *)np;
|
|
|
|
#undef b
|
|
|
|
}
|
|
|
|
|
1991-03-06 09:07:53 -04:00
|
|
|
static object *
|
|
|
|
list_repeat(a, n)
|
|
|
|
listobject *a;
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int size;
|
|
|
|
listobject *np;
|
|
|
|
object **p;
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
size = a->ob_size * n;
|
|
|
|
np = (listobject *) newlistobject(size);
|
|
|
|
if (np == NULL)
|
|
|
|
return NULL;
|
|
|
|
p = np->ob_item;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
for (j = 0; j < a->ob_size; j++) {
|
|
|
|
*p = a->ob_item[j];
|
|
|
|
INCREF(*p);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (object *) np;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static int
|
|
|
|
list_ass_slice(a, ilow, ihigh, v)
|
|
|
|
listobject *a;
|
|
|
|
int ilow, ihigh;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
object **item;
|
|
|
|
int n; /* Size of replacement list */
|
|
|
|
int d; /* Change in size */
|
|
|
|
int k; /* Loop index */
|
|
|
|
#define b ((listobject *)v)
|
|
|
|
if (v == NULL)
|
|
|
|
n = 0;
|
1991-12-24 09:27:34 -04:00
|
|
|
else if (is_listobject(v)) {
|
1990-10-14 09:07:46 -03:00
|
|
|
n = b->ob_size;
|
1991-12-24 09:27:34 -04:00
|
|
|
if (a == b) {
|
|
|
|
/* Special case "a[i:j] = a" -- copy b first */
|
|
|
|
int ret;
|
|
|
|
v = list_slice(b, 0, n);
|
|
|
|
ret = list_ass_slice(a, ilow, ihigh, v);
|
|
|
|
DECREF(v);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
1990-10-21 19:15:08 -03:00
|
|
|
else {
|
|
|
|
err_badarg();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
if (ilow < 0)
|
|
|
|
ilow = 0;
|
|
|
|
else if (ilow > a->ob_size)
|
|
|
|
ilow = a->ob_size;
|
|
|
|
if (ihigh < 0)
|
|
|
|
ihigh = 0;
|
|
|
|
if (ihigh < ilow)
|
|
|
|
ihigh = ilow;
|
|
|
|
else if (ihigh > a->ob_size)
|
|
|
|
ihigh = a->ob_size;
|
|
|
|
item = a->ob_item;
|
|
|
|
d = n - (ihigh-ilow);
|
|
|
|
if (d <= 0) { /* Delete -d items; DECREF ihigh-ilow items */
|
|
|
|
for (k = ilow; k < ihigh; k++)
|
|
|
|
DECREF(item[k]);
|
|
|
|
if (d < 0) {
|
|
|
|
for (/*k = ihigh*/; k < a->ob_size; k++)
|
|
|
|
item[k+d] = item[k];
|
|
|
|
a->ob_size += d;
|
|
|
|
RESIZE(item, object *, a->ob_size); /* Can't fail */
|
|
|
|
a->ob_item = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* Insert d items; DECREF ihigh-ilow items */
|
|
|
|
RESIZE(item, object *, a->ob_size + d);
|
1990-10-21 19:15:08 -03:00
|
|
|
if (item == NULL) {
|
|
|
|
err_nomem();
|
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
for (k = a->ob_size; --k >= ihigh; )
|
|
|
|
item[k+d] = item[k];
|
|
|
|
for (/*k = ihigh-1*/; k >= ilow; --k)
|
|
|
|
DECREF(item[k]);
|
|
|
|
a->ob_item = item;
|
|
|
|
a->ob_size += d;
|
|
|
|
}
|
|
|
|
for (k = 0; k < n; k++, ilow++) {
|
|
|
|
object *w = b->ob_item[k];
|
|
|
|
INCREF(w);
|
|
|
|
item[ilow] = w;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#undef b
|
|
|
|
}
|
|
|
|
|
1993-06-17 09:35:49 -03:00
|
|
|
int
|
|
|
|
setlistslice(a, ilow, ihigh, v)
|
|
|
|
object *a;
|
|
|
|
int ilow, ihigh;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (!is_listobject(a)) {
|
|
|
|
err_badcall();
|
1993-07-29 05:25:09 -03:00
|
|
|
return -1;
|
1993-06-17 09:35:49 -03:00
|
|
|
}
|
|
|
|
return list_ass_slice((listobject *)a, ilow, ihigh, v);
|
|
|
|
}
|
|
|
|
|
1991-04-03 15:05:18 -04:00
|
|
|
static int
|
|
|
|
list_ass_item(a, i, v)
|
|
|
|
listobject *a;
|
|
|
|
int i;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (i < 0 || i >= a->ob_size) {
|
|
|
|
err_setstr(IndexError, "list assignment index out of range");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (v == NULL)
|
|
|
|
return list_ass_slice(a, i, i+1, v);
|
|
|
|
INCREF(v);
|
|
|
|
DECREF(a->ob_item[i]);
|
|
|
|
a->ob_item[i] = v;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static object *
|
|
|
|
ins(self, where, v)
|
|
|
|
listobject *self;
|
|
|
|
int where;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (ins1(self, where, v) != 0)
|
|
|
|
return NULL;
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
listinsert(self, args)
|
|
|
|
listobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int i;
|
1993-02-08 11:49:17 -04:00
|
|
|
object *v;
|
|
|
|
if (!getargs(args, "(iO)", &i, &v))
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1993-02-08 11:49:17 -04:00
|
|
|
return ins(self, i, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
listappend(self, args)
|
|
|
|
listobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1993-02-08 11:49:17 -04:00
|
|
|
object *v;
|
|
|
|
if (!getargs(args, "O", &v))
|
|
|
|
return NULL;
|
|
|
|
return ins(self, (int) self->ob_size, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1992-08-03 16:05:37 -03:00
|
|
|
static object *cmpfunc;
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static int
|
|
|
|
cmp(v, w)
|
1993-01-04 05:09:59 -04:00
|
|
|
#ifdef __STDC__
|
|
|
|
void *v, *w;
|
|
|
|
#else
|
1990-10-14 09:07:46 -03:00
|
|
|
char *v, *w;
|
1993-01-04 05:09:59 -04:00
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1992-08-03 16:05:37 -03:00
|
|
|
object *t, *res;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
if (err_occurred())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cmpfunc == NULL)
|
|
|
|
return cmpobject(* (object **) v, * (object **) w);
|
|
|
|
|
|
|
|
/* Call the user-supplied comparison function */
|
1993-02-08 11:49:17 -04:00
|
|
|
t = mkvalue("OO", v, w);
|
1992-08-03 16:05:37 -03:00
|
|
|
if (t == NULL)
|
|
|
|
return 0;
|
|
|
|
res = call_object(cmpfunc, t);
|
|
|
|
DECREF(t);
|
|
|
|
if (res == NULL)
|
|
|
|
return 0;
|
|
|
|
if (!is_intobject(res)) {
|
|
|
|
err_setstr(TypeError, "comparison function should return int");
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i = getintvalue(res);
|
|
|
|
if (i < 0)
|
|
|
|
i = -1;
|
|
|
|
else if (i > 0)
|
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
DECREF(res);
|
|
|
|
return (int) i;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
listsort(self, args)
|
|
|
|
listobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
1992-08-03 16:05:37 -03:00
|
|
|
object *save_cmpfunc;
|
|
|
|
if (self->ob_size <= 1) {
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
save_cmpfunc = cmpfunc;
|
|
|
|
cmpfunc = args;
|
|
|
|
if (cmpfunc != NULL) {
|
|
|
|
/* Test the comparison function for obvious errors */
|
|
|
|
(void) cmp(&self->ob_item[0], &self->ob_item[1]);
|
|
|
|
if (err_occurred()) {
|
|
|
|
cmpfunc = save_cmpfunc;
|
|
|
|
return NULL;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1992-08-03 16:05:37 -03:00
|
|
|
qsort((char *)self->ob_item,
|
1990-10-14 09:07:46 -03:00
|
|
|
(int) self->ob_size, sizeof(object *), cmp);
|
1992-08-03 16:05:37 -03:00
|
|
|
cmpfunc = save_cmpfunc;
|
1990-10-21 19:15:08 -03:00
|
|
|
if (err_occurred())
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1991-03-06 09:07:53 -04:00
|
|
|
static object *
|
|
|
|
listreverse(self, args)
|
|
|
|
listobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
register object **p, **q;
|
|
|
|
register object *tmp;
|
|
|
|
|
|
|
|
if (args != NULL) {
|
|
|
|
err_badarg();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->ob_size > 1) {
|
|
|
|
for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
|
|
|
|
p < q; p++, q--) {
|
|
|
|
tmp = *p;
|
|
|
|
*p = *q;
|
|
|
|
*q = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
1990-10-30 09:32:20 -04:00
|
|
|
int
|
|
|
|
sortlist(v)
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (v == NULL || !is_listobject(v)) {
|
|
|
|
err_badcall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v = listsort((listobject *)v, (object *)NULL);
|
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
DECREF(v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1991-03-06 09:07:53 -04:00
|
|
|
static object *
|
|
|
|
listindex(self, args)
|
|
|
|
listobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args == NULL) {
|
|
|
|
err_badarg();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
|
|
|
if (cmpobject(self->ob_item[i], args) == 0)
|
1992-09-17 14:54:56 -03:00
|
|
|
return newintobject((long)i);
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
1991-12-10 10:00:03 -04:00
|
|
|
err_setstr(ValueError, "list.index(x): x not in list");
|
1991-03-06 09:07:53 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1991-10-20 17:20:40 -03:00
|
|
|
static object *
|
|
|
|
listcount(self, args)
|
|
|
|
listobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args == NULL) {
|
|
|
|
err_badarg();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
|
|
|
if (cmpobject(self->ob_item[i], args) == 0)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return newintobject((long)count);
|
|
|
|
}
|
|
|
|
|
1991-03-06 09:07:53 -04:00
|
|
|
static object *
|
|
|
|
listremove(self, args)
|
|
|
|
listobject *self;
|
|
|
|
object *args;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args == NULL) {
|
|
|
|
err_badarg();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
|
|
|
if (cmpobject(self->ob_item[i], args) == 0) {
|
|
|
|
if (list_ass_slice(self, i, i+1, (object *)NULL) != 0)
|
|
|
|
return NULL;
|
|
|
|
INCREF(None);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1991-12-10 10:00:03 -04:00
|
|
|
err_setstr(ValueError, "list.remove(x): x not in list");
|
1991-03-06 09:07:53 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static struct methodlist list_methods[] = {
|
|
|
|
{"append", listappend},
|
1991-10-20 17:20:40 -03:00
|
|
|
{"count", listcount},
|
1991-03-06 09:07:53 -04:00
|
|
|
{"index", listindex},
|
1990-10-14 09:07:46 -03:00
|
|
|
{"insert", listinsert},
|
|
|
|
{"sort", listsort},
|
1991-03-06 09:07:53 -04:00
|
|
|
{"remove", listremove},
|
|
|
|
{"reverse", listreverse},
|
1990-10-14 09:07:46 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
|
|
|
list_getattr(f, name)
|
|
|
|
listobject *f;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
return findmethod(list_methods, (object *)f, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sequence_methods list_as_sequence = {
|
|
|
|
list_length, /*sq_length*/
|
|
|
|
list_concat, /*sq_concat*/
|
1991-03-06 09:07:53 -04:00
|
|
|
list_repeat, /*sq_repeat*/
|
1990-10-14 09:07:46 -03:00
|
|
|
list_item, /*sq_item*/
|
|
|
|
list_slice, /*sq_slice*/
|
|
|
|
list_ass_item, /*sq_ass_item*/
|
|
|
|
list_ass_slice, /*sq_ass_slice*/
|
|
|
|
};
|
|
|
|
|
|
|
|
typeobject Listtype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
|
|
|
"list",
|
|
|
|
sizeof(listobject),
|
|
|
|
0,
|
|
|
|
list_dealloc, /*tp_dealloc*/
|
|
|
|
list_print, /*tp_print*/
|
|
|
|
list_getattr, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
list_compare, /*tp_compare*/
|
|
|
|
list_repr, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
&list_as_sequence, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
};
|