1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1995-01-04 15:07:38 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 08:39:46 -04:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 08:39:46 -04:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI 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.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* List object implementation */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
|
|
|
|
1994-08-29 09:45:32 -03:00
|
|
|
#ifdef STDC_HEADERS
|
|
|
|
#include <stddef.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h> /* For size_t */
|
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#define ROUNDUP(n, PyTryBlock) \
|
|
|
|
((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
|
1995-01-26 18:59:43 -04:00
|
|
|
|
|
|
|
static int
|
1997-08-25 15:36:23 -03:00
|
|
|
roundupsize(n)
|
1995-01-26 18:59:43 -04:00
|
|
|
int n;
|
|
|
|
{
|
|
|
|
if (n < 500)
|
|
|
|
return ROUNDUP(n, 10);
|
|
|
|
else
|
|
|
|
return ROUNDUP(n, 100);
|
|
|
|
}
|
|
|
|
|
1997-08-25 15:36:23 -03:00
|
|
|
#define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
|
1995-01-26 18:59:43 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyList_New(size)
|
1990-10-14 09:07:46 -03:00
|
|
|
int size;
|
|
|
|
{
|
|
|
|
int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *op;
|
1994-08-29 09:45:32 -03:00
|
|
|
size_t nbytes;
|
1990-10-14 09:07:46 -03:00
|
|
|
if (size < 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
nbytes = size * sizeof(PyObject *);
|
1992-08-19 13:46:30 -03:00
|
|
|
/* Check for overflow */
|
1997-05-02 00:12:38 -03:00
|
|
|
if (nbytes / sizeof(PyObject *) != (size_t)size) {
|
|
|
|
return PyErr_NoMemory();
|
1992-08-19 13:46:30 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
op = (PyListObject *) malloc(sizeof(PyListObject));
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyErr_NoMemory();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
if (size <= 0) {
|
|
|
|
op->ob_item = NULL;
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
op->ob_item = (PyObject **) malloc(nbytes);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op->ob_item == NULL) {
|
|
|
|
free((ANY *)op);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyErr_NoMemory();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
op->ob_type = &PyList_Type;
|
1990-10-14 09:07:46 -03:00
|
|
|
op->ob_size = size;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
op->ob_item[i] = NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_NewReference(op);
|
|
|
|
return (PyObject *) op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyList_Size(op)
|
|
|
|
PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyListObject *)op) -> ob_size;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *indexerr;
|
1996-08-09 17:51:27 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyList_GetItem(op, i)
|
|
|
|
PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
int i;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
|
1996-08-09 17:51:27 -03:00
|
|
|
if (indexerr == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
indexerr = PyString_FromString(
|
|
|
|
"list index out of range");
|
|
|
|
PyErr_SetObject(PyExc_IndexError, indexerr);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyListObject *)op) -> ob_item[i];
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyList_SetItem(op, i, newitem)
|
|
|
|
register PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
register int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *newitem;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *olditem;
|
|
|
|
register PyObject **p;
|
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
Py_XDECREF(newitem);
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-21 19:15:08 -03:00
|
|
|
return -1;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
|
|
|
|
Py_XDECREF(newitem);
|
|
|
|
PyErr_SetString(PyExc_IndexError,
|
|
|
|
"list assignment index out of range");
|
1990-10-21 19:15:08 -03:00
|
|
|
return -1;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
p = ((PyListObject *)op) -> ob_item + i;
|
1995-03-09 08:12:50 -04:00
|
|
|
olditem = *p;
|
|
|
|
*p = newitem;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(olditem);
|
1990-10-14 09:07:46 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ins1(self, where, v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
1990-10-14 09:07:46 -03:00
|
|
|
int where;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **items;
|
1990-10-21 19:15:08 -03:00
|
|
|
if (v == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadInternalCall();
|
1990-10-21 19:15:08 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
items = self->ob_item;
|
1997-05-02 00:12:38 -03:00
|
|
|
NRESIZE(items, PyObject *, self->ob_size+1);
|
1990-10-21 19:15:08 -03:00
|
|
|
if (items == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_NoMemory();
|
1990-10-21 19:15:08 -03:00
|
|
|
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];
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1990-10-14 09:07:46 -03:00
|
|
|
items[where] = v;
|
|
|
|
self->ob_item = items;
|
|
|
|
self->ob_size++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyList_Insert(op, where, newitem)
|
|
|
|
PyObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
int where;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *newitem;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-21 19:15:08 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return ins1((PyListObject *)op, where, newitem);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyList_Append(op, newitem)
|
|
|
|
PyObject *op;
|
|
|
|
PyObject *newitem;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyList_Check(op)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-21 19:15:08 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return ins1((PyListObject *)op,
|
|
|
|
(int) ((PyListObject *)op)->ob_size, newitem);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
static void
|
|
|
|
list_dealloc(op)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int i;
|
1995-01-19 08:09:27 -04:00
|
|
|
if (op->ob_item != NULL) {
|
|
|
|
for (i = 0; i < op->ob_size; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(op->ob_item[i]);
|
1995-01-19 08:09:27 -04:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
free((ANY *)op->ob_item);
|
1995-01-19 08:09:27 -04:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
FILE *fp;
|
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
int i;
|
1998-04-10 19:47:27 -03:00
|
|
|
|
|
|
|
i = Py_ReprEnter((PyObject*)op);
|
|
|
|
if (i != 0) {
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
fprintf(fp, "[...]");
|
|
|
|
return 0;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
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, ", ");
|
1998-04-10 19:47:27 -03:00
|
|
|
if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
|
|
|
|
Py_ReprLeave((PyObject *)op);
|
1991-06-07 13:10:43 -03:00
|
|
|
return -1;
|
1998-04-10 19:47:27 -03:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
fprintf(fp, "]");
|
1998-04-10 19:47:27 -03:00
|
|
|
Py_ReprLeave((PyObject *)op);
|
1991-06-07 13:10:43 -03:00
|
|
|
return 0;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
list_repr(v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *s, *comma;
|
1990-10-14 09:07:46 -03:00
|
|
|
int i;
|
1998-04-10 19:47:27 -03:00
|
|
|
|
|
|
|
i = Py_ReprEnter((PyObject*)v);
|
|
|
|
if (i != 0) {
|
|
|
|
if (i > 0)
|
|
|
|
return PyString_FromString("[...]");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
s = PyString_FromString("[");
|
|
|
|
comma = PyString_FromString(", ");
|
1990-10-14 09:07:46 -03:00
|
|
|
for (i = 0; i < v->ob_size && s != NULL; i++) {
|
|
|
|
if (i > 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyString_Concat(&s, comma);
|
|
|
|
PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(comma);
|
|
|
|
PyString_ConcatAndDel(&s, PyString_FromString("]"));
|
1998-04-10 19:47:27 -03:00
|
|
|
Py_ReprLeave((PyObject *)v);
|
1990-10-14 09:07:46 -03:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
list_compare(v, w)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *v, *w;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
int cmp = PyObject_Compare(v->ob_item[i], w->ob_item[i]);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
return v->ob_size - w->ob_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
list_length(a)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *a;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
return a->ob_size;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
list_item(a, i)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *a;
|
1990-10-14 09:07:46 -03:00
|
|
|
int i;
|
|
|
|
{
|
|
|
|
if (i < 0 || i >= a->ob_size) {
|
1996-08-09 17:51:27 -03:00
|
|
|
if (indexerr == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
indexerr = PyString_FromString(
|
|
|
|
"list index out of range");
|
|
|
|
PyErr_SetObject(PyExc_IndexError, indexerr);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(a->ob_item[i]);
|
1990-10-14 09:07:46 -03:00
|
|
|
return a->ob_item[i];
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
list_slice(a, ilow, ihigh)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *a;
|
1990-10-14 09:07:46 -03:00
|
|
|
int ilow, ihigh;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *np;
|
1990-10-14 09:07:46 -03:00
|
|
|
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;
|
1997-05-02 00:12:38 -03:00
|
|
|
np = (PyListObject *) PyList_New(ihigh - ilow);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (np == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = ilow; i < ihigh; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v = a->ob_item[i];
|
|
|
|
Py_INCREF(v);
|
1990-10-14 09:07:46 -03:00
|
|
|
np->ob_item[i - ilow] = v;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)np;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyList_GetSlice(a, ilow, ihigh)
|
|
|
|
PyObject *a;
|
1993-06-17 09:35:49 -03:00
|
|
|
int ilow, ihigh;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyList_Check(a)) {
|
|
|
|
PyErr_BadInternalCall();
|
1993-06-17 09:35:49 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return list_slice((PyListObject *)a, ilow, ihigh);
|
1993-06-17 09:35:49 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
list_concat(a, bb)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *a;
|
|
|
|
PyObject *bb;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *np;
|
|
|
|
if (!PyList_Check(bb)) {
|
|
|
|
PyErr_BadArgument();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
#define b ((PyListObject *)bb)
|
1990-10-14 09:07:46 -03:00
|
|
|
size = a->ob_size + b->ob_size;
|
1997-05-02 00:12:38 -03:00
|
|
|
np = (PyListObject *) PyList_New(size);
|
1990-10-14 09:07:46 -03:00
|
|
|
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++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v = a->ob_item[i];
|
|
|
|
Py_INCREF(v);
|
1990-10-14 09:07:46 -03:00
|
|
|
np->ob_item[i] = v;
|
|
|
|
}
|
|
|
|
for (i = 0; i < b->ob_size; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v = b->ob_item[i];
|
|
|
|
Py_INCREF(v);
|
1990-10-14 09:07:46 -03:00
|
|
|
np->ob_item[i + a->ob_size] = v;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)np;
|
1990-10-14 09:07:46 -03:00
|
|
|
#undef b
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-03-06 09:07:53 -04:00
|
|
|
list_repeat(a, n)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *a;
|
1991-03-06 09:07:53 -04:00
|
|
|
int n;
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int size;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *np;
|
|
|
|
PyObject **p;
|
1991-03-06 09:07:53 -04:00
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
size = a->ob_size * n;
|
1997-05-02 00:12:38 -03:00
|
|
|
np = (PyListObject *) PyList_New(size);
|
1991-03-06 09:07:53 -04:00
|
|
|
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];
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(*p);
|
1991-03-06 09:07:53 -04:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) np;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static int
|
|
|
|
list_ass_slice(a, ilow, ihigh, v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *a;
|
1990-10-14 09:07:46 -03:00
|
|
|
int ilow, ihigh;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1995-01-17 06:21:11 -04:00
|
|
|
/* Because [X]DECREF can recursively invoke list operations on
|
|
|
|
this list, we must postpone all [X]DECREF activity until
|
|
|
|
after the list is back in its canonical shape. Therefore
|
|
|
|
we must allocate an additional array, 'recycle', into which
|
|
|
|
we temporarily copy the items that are deleted from the
|
|
|
|
list. :-( */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **recycle, **p;
|
|
|
|
PyObject **item;
|
1990-10-14 09:07:46 -03:00
|
|
|
int n; /* Size of replacement list */
|
|
|
|
int d; /* Change in size */
|
|
|
|
int k; /* Loop index */
|
1997-05-02 00:12:38 -03:00
|
|
|
#define b ((PyListObject *)v)
|
1990-10-14 09:07:46 -03:00
|
|
|
if (v == NULL)
|
|
|
|
n = 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (PyList_Check(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);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(v);
|
1991-12-24 09:27:34 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
1990-10-21 19:15:08 -03:00
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadArgument();
|
1990-10-21 19:15:08 -03:00
|
|
|
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);
|
1995-01-17 06:21:11 -04:00
|
|
|
if (ihigh > ilow)
|
1997-05-02 00:12:38 -03:00
|
|
|
p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
|
1995-01-17 06:21:11 -04:00
|
|
|
else
|
|
|
|
p = recycle = NULL;
|
|
|
|
if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
|
1990-10-14 09:07:46 -03:00
|
|
|
for (k = ilow; k < ihigh; k++)
|
1995-01-17 06:21:11 -04:00
|
|
|
*p++ = item[k];
|
1990-10-14 09:07:46 -03:00
|
|
|
if (d < 0) {
|
|
|
|
for (/*k = ihigh*/; k < a->ob_size; k++)
|
|
|
|
item[k+d] = item[k];
|
|
|
|
a->ob_size += d;
|
1997-05-02 00:12:38 -03:00
|
|
|
NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
|
1990-10-14 09:07:46 -03:00
|
|
|
a->ob_item = item;
|
|
|
|
}
|
|
|
|
}
|
1995-01-17 06:21:11 -04:00
|
|
|
else { /* Insert d items; recycle ihigh-ilow items */
|
1997-05-02 00:12:38 -03:00
|
|
|
NRESIZE(item, PyObject *, a->ob_size + d);
|
1990-10-21 19:15:08 -03:00
|
|
|
if (item == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyMem_XDEL(recycle);
|
|
|
|
PyErr_NoMemory();
|
1990-10-21 19:15:08 -03:00
|
|
|
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)
|
1995-01-17 06:21:11 -04:00
|
|
|
*p++ = item[k];
|
1990-10-14 09:07:46 -03:00
|
|
|
a->ob_item = item;
|
|
|
|
a->ob_size += d;
|
|
|
|
}
|
|
|
|
for (k = 0; k < n; k++, ilow++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *w = b->ob_item[k];
|
|
|
|
Py_XINCREF(w);
|
1990-10-14 09:07:46 -03:00
|
|
|
item[ilow] = w;
|
|
|
|
}
|
1995-01-17 06:21:11 -04:00
|
|
|
if (recycle) {
|
|
|
|
while (--p >= recycle)
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(*p);
|
|
|
|
PyMem_DEL(recycle);
|
1995-01-17 06:21:11 -04:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
return 0;
|
|
|
|
#undef b
|
|
|
|
}
|
|
|
|
|
1993-06-17 09:35:49 -03:00
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyList_SetSlice(a, ilow, ihigh, v)
|
|
|
|
PyObject *a;
|
1993-06-17 09:35:49 -03:00
|
|
|
int ilow, ihigh;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
1993-06-17 09:35:49 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyList_Check(a)) {
|
|
|
|
PyErr_BadInternalCall();
|
1993-07-29 05:25:09 -03:00
|
|
|
return -1;
|
1993-06-17 09:35:49 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
|
1993-06-17 09:35:49 -03:00
|
|
|
}
|
|
|
|
|
1991-04-03 15:05:18 -04:00
|
|
|
static int
|
|
|
|
list_ass_item(a, i, v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *a;
|
1991-04-03 15:05:18 -04:00
|
|
|
int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
1991-04-03 15:05:18 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *old_value;
|
1991-04-03 15:05:18 -04:00
|
|
|
if (i < 0 || i >= a->ob_size) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_IndexError,
|
|
|
|
"list assignment index out of range");
|
1991-04-03 15:05:18 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (v == NULL)
|
|
|
|
return list_ass_slice(a, i, i+1, v);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1995-01-02 15:07:15 -04:00
|
|
|
old_value = a->ob_item[i];
|
1991-04-03 15:05:18 -04:00
|
|
|
a->ob_item[i] = v;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(old_value);
|
1991-04-03 15:05:18 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
ins(self, where, v)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
1990-10-14 09:07:46 -03:00
|
|
|
int where;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
if (ins1(self, where, v) != 0)
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
listinsert(self, args)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *args;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
|
|
|
if (!PyArg_Parse(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
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
listappend(self, args)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *args;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
|
|
|
if (!PyArg_Parse(args, "O", &v))
|
1993-02-08 11:49:17 -04:00
|
|
|
return NULL;
|
|
|
|
return ins(self, (int) self->ob_size, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1996-12-10 19:55:39 -04:00
|
|
|
#define NEWSORT
|
|
|
|
|
|
|
|
#ifdef NEWSORT
|
|
|
|
|
|
|
|
/* New quicksort implementation for arrays of object pointers.
|
|
|
|
Thanks to discussions with Tim Peters. */
|
|
|
|
|
|
|
|
/* CMPERROR is returned by our comparison function when an error
|
|
|
|
occurred. This is the largest negative integer (0x80000000 on a
|
|
|
|
32-bit system). */
|
1997-03-04 20:45:43 -04:00
|
|
|
#define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
|
1996-12-10 19:55:39 -04:00
|
|
|
|
|
|
|
/* Comparison function. Takes care of calling a user-supplied
|
|
|
|
comparison function (any callable Python object). Calls the
|
|
|
|
standard comparison function, cmpobject(), if the user-supplied
|
|
|
|
function is NULL. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
docompare(x, y, compare)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *x;
|
|
|
|
PyObject *y;
|
|
|
|
PyObject *compare;
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *args, *res;
|
1996-12-10 19:55:39 -04:00
|
|
|
int i;
|
|
|
|
|
1997-05-22 21:06:51 -03:00
|
|
|
if (compare == NULL) {
|
|
|
|
i = PyObject_Compare(x, y);
|
|
|
|
if (i && PyErr_Occurred())
|
|
|
|
i = CMPERROR;
|
|
|
|
return i;
|
|
|
|
}
|
1996-12-10 19:55:39 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(OO)", x, y);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (args == NULL)
|
|
|
|
return CMPERROR;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(compare, args);
|
|
|
|
Py_DECREF(args);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return CMPERROR;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInt_Check(res)) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"comparison function should return int");
|
1996-12-10 19:55:39 -04:00
|
|
|
return CMPERROR;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
i = PyInt_AsLong(res);
|
|
|
|
Py_DECREF(res);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (i < 0)
|
|
|
|
return -1;
|
|
|
|
if (i > 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Straight insertion sort. More efficient for sorting small arrays. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
insertionsort(array, size, compare)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **array; /* Start of array to sort */
|
1996-12-10 19:55:39 -04:00
|
|
|
int size; /* Number of elements to sort */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *compare;/* Comparison function object, or NULL for default */
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject **a = array;
|
|
|
|
register PyObject **end = array+size;
|
|
|
|
register PyObject **p;
|
1996-12-10 19:55:39 -04:00
|
|
|
|
1996-12-11 19:57:39 -04:00
|
|
|
for (p = a+1; p < end; p++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *key = *p;
|
|
|
|
register PyObject **q = p;
|
1996-12-11 19:57:39 -04:00
|
|
|
while (--q >= a) {
|
|
|
|
register int k = docompare(*q, key, compare);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (k == CMPERROR)
|
|
|
|
return -1;
|
|
|
|
if (k <= 0)
|
|
|
|
break;
|
1996-12-11 19:57:39 -04:00
|
|
|
*(q+1) = *q;
|
|
|
|
*q = key; /* For consistency */
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MINSIZE is the smallest array we care to partition; smaller arrays
|
1996-12-15 23:32:39 -04:00
|
|
|
are sorted using a straight insertion sort (above). It must be at
|
|
|
|
least 2 for the quicksort implementation to work. Assuming that
|
|
|
|
comparisons are more expensive than everything else (and this is a
|
|
|
|
good assumption for Python), it should be 10, which is the cutoff
|
|
|
|
point: quicksort requires more comparisons than insertion sort for
|
|
|
|
smaller arrays. */
|
1996-12-10 19:55:39 -04:00
|
|
|
#define MINSIZE 10
|
|
|
|
|
|
|
|
/* STACKSIZE is the size of our work stack. A rough estimate is that
|
|
|
|
this allows us to sort arrays of MINSIZE * 2**STACKSIZE, or large
|
|
|
|
enough. (Because of the way we push the biggest partition first,
|
|
|
|
the worst case occurs when all subarrays are always partitioned
|
|
|
|
exactly in two.) */
|
|
|
|
#define STACKSIZE 64
|
|
|
|
|
|
|
|
/* Quicksort algorithm. Return -1 if an exception occurred; in this
|
|
|
|
case we leave the array partly sorted but otherwise in good health
|
|
|
|
(i.e. no items have been removed or duplicated). */
|
|
|
|
|
|
|
|
static int
|
|
|
|
quicksort(array, size, compare)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **array; /* Start of array to sort */
|
1996-12-10 19:55:39 -04:00
|
|
|
int size; /* Number of elements to sort */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *compare;/* Comparison function object, or NULL for default */
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *tmp, *pivot;
|
|
|
|
register PyObject **lo, **hi, **l, **r;
|
1996-12-11 19:57:39 -04:00
|
|
|
int top, k, n, n2;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **lostack[STACKSIZE];
|
|
|
|
PyObject **histack[STACKSIZE];
|
1996-12-10 19:55:39 -04:00
|
|
|
|
|
|
|
/* Start out with the whole array on the work stack */
|
1996-12-11 19:57:39 -04:00
|
|
|
lostack[0] = array;
|
|
|
|
histack[0] = array+size;
|
1996-12-10 19:55:39 -04:00
|
|
|
top = 1;
|
|
|
|
|
|
|
|
/* Repeat until the work stack is empty */
|
|
|
|
while (--top >= 0) {
|
1996-12-11 19:57:39 -04:00
|
|
|
lo = lostack[top];
|
|
|
|
hi = histack[top];
|
1996-12-10 19:55:39 -04:00
|
|
|
|
|
|
|
/* If it's a small one, use straight insertion sort */
|
1996-12-11 19:57:39 -04:00
|
|
|
n = hi - lo;
|
1996-12-10 19:55:39 -04:00
|
|
|
if (n < MINSIZE) {
|
1997-12-10 11:14:24 -04:00
|
|
|
/*
|
|
|
|
* skip it. The insertion sort at the end will
|
|
|
|
* catch these
|
|
|
|
*/
|
1996-12-10 19:55:39 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1996-12-11 19:57:39 -04:00
|
|
|
/* Choose median of first, middle and last item as pivot */
|
|
|
|
|
|
|
|
l = lo + (n>>1); /* Middle */
|
|
|
|
r = hi - 1; /* Last */
|
1997-12-10 11:14:24 -04:00
|
|
|
|
|
|
|
k = docompare(*l, *lo, compare);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (k == CMPERROR)
|
|
|
|
return -1;
|
|
|
|
if (k < 0)
|
1996-12-11 19:57:39 -04:00
|
|
|
{ tmp = *lo; *lo = *l; *l = tmp; }
|
1997-12-10 11:14:24 -04:00
|
|
|
|
1996-12-11 19:57:39 -04:00
|
|
|
k = docompare(*r, *l, compare);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (k == CMPERROR)
|
|
|
|
return -1;
|
|
|
|
if (k < 0)
|
1996-12-11 19:57:39 -04:00
|
|
|
{ tmp = *r; *r = *l; *l = tmp; }
|
1997-12-10 11:14:24 -04:00
|
|
|
|
|
|
|
k = docompare(*l, *lo, compare);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (k == CMPERROR)
|
|
|
|
return -1;
|
|
|
|
if (k < 0)
|
1997-12-10 11:14:24 -04:00
|
|
|
{ tmp = *l; *l = *lo; *lo = tmp; }
|
|
|
|
pivot = *l;
|
1996-12-10 19:55:39 -04:00
|
|
|
|
|
|
|
/* Partition the array */
|
1997-12-10 11:14:24 -04:00
|
|
|
l = lo+1;
|
|
|
|
r = hi-2;
|
1996-12-10 19:55:39 -04:00
|
|
|
for (;;) {
|
|
|
|
/* Move left index to element > pivot */
|
1998-02-25 13:50:03 -04:00
|
|
|
while (l < hi) {
|
1997-12-10 11:14:24 -04:00
|
|
|
k = docompare(*l, pivot, compare);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (k == CMPERROR)
|
|
|
|
return -1;
|
1997-12-10 11:14:24 -04:00
|
|
|
if (k >= 0)
|
1996-12-10 19:55:39 -04:00
|
|
|
break;
|
1997-12-10 11:14:24 -04:00
|
|
|
l++;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
|
|
|
/* Move right index to element < pivot */
|
1998-02-25 13:50:03 -04:00
|
|
|
while (r >= lo) {
|
1997-12-10 11:14:24 -04:00
|
|
|
k = docompare(pivot, *r, compare);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (k == CMPERROR)
|
|
|
|
return -1;
|
1997-12-10 11:14:24 -04:00
|
|
|
if (k >= 0)
|
1996-12-10 19:55:39 -04:00
|
|
|
break;
|
1997-12-10 11:14:24 -04:00
|
|
|
r--;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
1997-12-10 11:14:24 -04:00
|
|
|
|
1996-12-10 19:55:39 -04:00
|
|
|
/* If they met, we're through */
|
1997-12-10 11:14:24 -04:00
|
|
|
if (l < r) {
|
|
|
|
/* Swap elements and continue */
|
|
|
|
tmp = *l; *l = *r; *r = tmp;
|
|
|
|
l++; r--;
|
|
|
|
}
|
|
|
|
else if (l == r) {
|
|
|
|
l++; r--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l > r)
|
1996-12-10 19:55:39 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* We have now reached the following conditions:
|
1996-12-11 19:57:39 -04:00
|
|
|
lo <= r < l <= hi
|
|
|
|
all x in [lo,r) are <= pivot
|
|
|
|
all x in [r,l) are == pivot
|
|
|
|
all x in [l,hi) are >= pivot
|
|
|
|
The partitions are [lo,r) and [l,hi)
|
1996-12-10 19:55:39 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Push biggest partition first */
|
1996-12-11 19:57:39 -04:00
|
|
|
n = r - lo;
|
|
|
|
n2 = hi - l;
|
|
|
|
if (n > n2) {
|
1996-12-10 19:55:39 -04:00
|
|
|
/* First one is bigger */
|
1997-12-10 11:14:24 -04:00
|
|
|
if (n > MINSIZE) {
|
1996-12-11 19:57:39 -04:00
|
|
|
lostack[top] = lo;
|
|
|
|
histack[top++] = r;
|
1997-12-10 11:14:24 -04:00
|
|
|
if (n2 > MINSIZE) {
|
1996-12-11 19:57:39 -04:00
|
|
|
lostack[top] = l;
|
|
|
|
histack[top++] = hi;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Second one is bigger */
|
1997-12-10 11:14:24 -04:00
|
|
|
if (n2 > MINSIZE) {
|
1996-12-11 19:57:39 -04:00
|
|
|
lostack[top] = l;
|
|
|
|
histack[top++] = hi;
|
1997-12-10 11:14:24 -04:00
|
|
|
if (n > MINSIZE) {
|
1996-12-11 19:57:39 -04:00
|
|
|
lostack[top] = lo;
|
|
|
|
histack[top++] = r;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should assert top < STACKSIZE-1 */
|
|
|
|
}
|
1997-12-10 11:14:24 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ouch - even if I screwed up the quicksort above, the
|
|
|
|
* insertionsort below will cover up the problem - just a
|
|
|
|
* performance hit would be noticable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* insertionsort is pretty fast on the partially sorted list */
|
|
|
|
|
|
|
|
if (insertionsort(array, size, compare) < 0)
|
|
|
|
return -1;
|
1996-12-10 19:55:39 -04:00
|
|
|
|
|
|
|
/* Succes */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1996-12-10 19:55:39 -04:00
|
|
|
listsort(self, compare)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *compare;
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
|
|
|
/* XXX Don't you *dare* changing the list's length in compare()! */
|
|
|
|
if (quicksort(self->ob_item, self->ob_size, compare) < 0)
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !NEWSORT */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *comparefunc;
|
1992-08-03 16:05:37 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static int
|
|
|
|
cmp(v, w)
|
1994-08-29 09:45:32 -03:00
|
|
|
const ANY *v, *w;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *t, *res;
|
1992-08-03 16:05:37 -03:00
|
|
|
long i;
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyErr_Occurred())
|
1992-08-03 16:05:37 -03:00
|
|
|
return 0;
|
|
|
|
|
1994-08-29 09:45:32 -03:00
|
|
|
if (comparefunc == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyObject_Compare(* (PyObject **) v, * (PyObject **) w);
|
1992-08-03 16:05:37 -03:00
|
|
|
|
|
|
|
/* Call the user-supplied comparison function */
|
1997-05-02 00:12:38 -03:00
|
|
|
t = Py_BuildValue("(OO)", * (PyObject **) v, * (PyObject **) w);
|
1992-08-03 16:05:37 -03:00
|
|
|
if (t == NULL)
|
|
|
|
return 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(comparefunc, t);
|
|
|
|
Py_DECREF(t);
|
1992-08-03 16:05:37 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInt_Check(res)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"comparison function should return int");
|
1992-08-03 16:05:37 -03:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
i = PyInt_AsLong(res);
|
1992-08-03 16:05:37 -03:00
|
|
|
if (i < 0)
|
|
|
|
i = -1;
|
|
|
|
else if (i > 0)
|
|
|
|
i = 1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-03 16:05:37 -03:00
|
|
|
return (int) i;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
listsort(self, args)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *args;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *save_comparefunc;
|
1992-08-03 16:05:37 -03:00
|
|
|
if (self->ob_size <= 1) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-08-03 16:05:37 -03:00
|
|
|
}
|
1994-08-29 09:45:32 -03:00
|
|
|
save_comparefunc = comparefunc;
|
|
|
|
comparefunc = args;
|
|
|
|
if (comparefunc != NULL) {
|
1992-08-03 16:05:37 -03:00
|
|
|
/* Test the comparison function for obvious errors */
|
1994-08-29 09:45:32 -03:00
|
|
|
(void) cmp((ANY *)&self->ob_item[0], (ANY *)&self->ob_item[1]);
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyErr_Occurred()) {
|
1994-08-29 09:45:32 -03:00
|
|
|
comparefunc = save_comparefunc;
|
1992-08-03 16:05:37 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1992-08-03 16:05:37 -03:00
|
|
|
qsort((char *)self->ob_item,
|
1997-05-02 00:12:38 -03:00
|
|
|
(int) self->ob_size, sizeof(PyObject *), cmp);
|
1994-08-29 09:45:32 -03:00
|
|
|
comparefunc = save_comparefunc;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyErr_Occurred())
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1996-12-10 19:55:39 -04:00
|
|
|
#endif
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-03-06 09:07:53 -04:00
|
|
|
listreverse(self, args)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *args;
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject **p, **q;
|
|
|
|
register PyObject *tmp;
|
1991-03-06 09:07:53 -04:00
|
|
|
|
|
|
|
if (args != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadArgument();
|
1991-03-06 09:07:53 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
|
|
|
|
1995-01-17 12:34:45 -04:00
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyList_Reverse(v)
|
|
|
|
PyObject *v;
|
1995-01-17 12:34:45 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (v == NULL || !PyList_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
1995-01-17 12:34:45 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
v = listreverse((PyListObject *)v, (PyObject *)NULL);
|
1995-01-17 12:34:45 -04:00
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(v);
|
1995-01-17 12:34:45 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-30 09:32:20 -04:00
|
|
|
int
|
1997-05-02 00:12:38 -03:00
|
|
|
PyList_Sort(v)
|
|
|
|
PyObject *v;
|
1990-10-30 09:32:20 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (v == NULL || !PyList_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-30 09:32:20 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
v = listsort((PyListObject *)v, (PyObject *)NULL);
|
1990-10-30 09:32:20 -04:00
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(v);
|
1990-10-30 09:32:20 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyList_AsTuple(v)
|
|
|
|
PyObject *v;
|
1994-08-29 09:45:32 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *w;
|
|
|
|
PyObject **p;
|
1994-08-29 09:45:32 -03:00
|
|
|
int n;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (v == NULL || !PyList_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
1994-08-29 09:45:32 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
n = ((PyListObject *)v)->ob_size;
|
|
|
|
w = PyTuple_New(n);
|
1994-08-29 09:45:32 -03:00
|
|
|
if (w == NULL)
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
p = ((PyTupleObject *)w)->ob_item;
|
1994-08-29 09:45:32 -03:00
|
|
|
memcpy((ANY *)p,
|
1997-05-02 00:12:38 -03:00
|
|
|
(ANY *)((PyListObject *)v)->ob_item,
|
|
|
|
n*sizeof(PyObject *));
|
1994-08-29 09:45:32 -03:00
|
|
|
while (--n >= 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(*p);
|
1994-08-29 09:45:32 -03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-03-06 09:07:53 -04:00
|
|
|
listindex(self, args)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *args;
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadArgument();
|
1991-03-06 09:07:53 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyObject_Compare(self->ob_item[i], args) == 0)
|
|
|
|
return PyInt_FromLong((long)i);
|
1997-05-22 21:06:51 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
|
1991-03-06 09:07:53 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-10-20 17:20:40 -03:00
|
|
|
listcount(self, args)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *args;
|
1991-10-20 17:20:40 -03:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadArgument();
|
1991-10-20 17:20:40 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyObject_Compare(self->ob_item[i], args) == 0)
|
1991-10-20 17:20:40 -03:00
|
|
|
count++;
|
1997-05-22 21:06:51 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
1991-10-20 17:20:40 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong((long)count);
|
1991-10-20 17:20:40 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-03-06 09:07:53 -04:00
|
|
|
listremove(self, args)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *self;
|
|
|
|
PyObject *args;
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_BadArgument();
|
1991-03-06 09:07:53 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyObject_Compare(self->ob_item[i], args) == 0) {
|
|
|
|
if (list_ass_slice(self, i, i+1,
|
|
|
|
(PyObject *)NULL) != 0)
|
1991-03-06 09:07:53 -04:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
1997-05-22 21:06:51 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
1991-03-06 09:07:53 -04:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
|
1991-03-06 09:07:53 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyMethodDef list_methods[] = {
|
|
|
|
{"append", (PyCFunction)listappend},
|
|
|
|
{"insert", (PyCFunction)listinsert},
|
|
|
|
{"remove", (PyCFunction)listremove},
|
1997-07-13 00:58:01 -03:00
|
|
|
{"index", (PyCFunction)listindex},
|
|
|
|
{"count", (PyCFunction)listcount},
|
1997-05-02 00:12:38 -03:00
|
|
|
{"reverse", (PyCFunction)listreverse},
|
1997-07-13 00:58:01 -03:00
|
|
|
{"sort", (PyCFunction)listsort, 0},
|
1990-10-14 09:07:46 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
list_getattr(f, name)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *f;
|
1990-10-14 09:07:46 -03:00
|
|
|
char *name;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
return Py_FindMethod(list_methods, (PyObject *)f, name);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PySequenceMethods list_as_sequence = {
|
1994-08-29 09:45:32 -03:00
|
|
|
(inquiry)list_length, /*sq_length*/
|
|
|
|
(binaryfunc)list_concat, /*sq_concat*/
|
|
|
|
(intargfunc)list_repeat, /*sq_repeat*/
|
|
|
|
(intargfunc)list_item, /*sq_item*/
|
|
|
|
(intintargfunc)list_slice, /*sq_slice*/
|
|
|
|
(intobjargproc)list_ass_item, /*sq_ass_item*/
|
|
|
|
(intintobjargproc)list_ass_slice, /*sq_ass_slice*/
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyList_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
"list",
|
1997-05-02 00:12:38 -03:00
|
|
|
sizeof(PyListObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1994-08-29 09:45:32 -03:00
|
|
|
(destructor)list_dealloc, /*tp_dealloc*/
|
|
|
|
(printfunc)list_print, /*tp_print*/
|
|
|
|
(getattrfunc)list_getattr, /*tp_getattr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_setattr*/
|
1994-08-29 09:45:32 -03:00
|
|
|
(cmpfunc)list_compare, /*tp_compare*/
|
|
|
|
(reprfunc)list_repr, /*tp_repr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
&list_as_sequence, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
};
|