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
|
|
|
|
1995-01-26 18:59:43 -04:00
|
|
|
static int
|
2000-07-09 12:16:51 -03:00
|
|
|
roundupsize(int n)
|
1995-01-26 18:59:43 -04:00
|
|
|
{
|
2001-05-26 02:28:40 -03:00
|
|
|
unsigned int nbits = 0;
|
|
|
|
unsigned int n2 = (unsigned int)n >> 5;
|
|
|
|
|
2002-07-18 23:35:45 -03:00
|
|
|
/* Round up:
|
2001-05-26 02:28:40 -03:00
|
|
|
* If n < 256, to a multiple of 8.
|
|
|
|
* If n < 2048, to a multiple of 64.
|
|
|
|
* If n < 16384, to a multiple of 512.
|
|
|
|
* If n < 131072, to a multiple of 4096.
|
|
|
|
* If n < 1048576, to a multiple of 32768.
|
|
|
|
* If n < 8388608, to a multiple of 262144.
|
|
|
|
* If n < 67108864, to a multiple of 2097152.
|
|
|
|
* If n < 536870912, to a multiple of 16777216.
|
|
|
|
* ...
|
|
|
|
* If n < 2**(5+3*i), to a multiple of 2**(3*i).
|
|
|
|
*
|
|
|
|
* This over-allocates proportional to the list size, making room
|
|
|
|
* for additional growth. The over-allocation is mild, but is
|
|
|
|
* enough to give linear-time amortized behavior over a long
|
|
|
|
* sequence of appends() in the presence of a poorly-performing
|
|
|
|
* system realloc() (which is a reality, e.g., across all flavors
|
|
|
|
* of Windows, with Win9x behavior being particularly bad -- and
|
|
|
|
* we've still got address space fragmentation problems on Win9x
|
|
|
|
* even with this scheme, although it requires much longer lists to
|
|
|
|
* provoke them than it used to).
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
n2 >>= 3;
|
|
|
|
nbits += 3;
|
|
|
|
} while (n2);
|
|
|
|
return ((n >> nbits) + 1) << nbits;
|
|
|
|
}
|
1995-01-26 18:59:43 -04:00
|
|
|
|
2002-05-22 20:19:17 -03:00
|
|
|
#define NRESIZE(var, type, nitems) \
|
|
|
|
do { \
|
|
|
|
size_t _new_size = roundupsize(nitems); \
|
|
|
|
if (_new_size <= ((~(size_t)0) / sizeof(type))) \
|
|
|
|
PyMem_RESIZE(var, type, _new_size); \
|
|
|
|
else \
|
|
|
|
var = NULL; \
|
|
|
|
} while (0)
|
1995-01-26 18:59:43 -04:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_New(int size)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
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
|
|
|
}
|
2001-08-29 20:54:21 -03:00
|
|
|
op = PyObject_GC_New(PyListObject, &PyList_Type);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op == NULL) {
|
2001-08-29 20:54:21 -03:00
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
if (size <= 0) {
|
|
|
|
op->ob_item = NULL;
|
|
|
|
}
|
|
|
|
else {
|
2000-05-03 20:44:39 -03:00
|
|
|
op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (op->ob_item == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyErr_NoMemory();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2002-06-13 18:11:11 -03:00
|
|
|
memset(op->ob_item, 0, sizeof(*op->ob_item) * size);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2001-08-29 20:54:21 -03:00
|
|
|
op->ob_size = size;
|
|
|
|
_PyObject_GC_TRACK(op);
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Size(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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_GetItem(PyObject *op, int i)
|
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 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
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_SetItem(register PyObject *op, register int i,
|
|
|
|
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
|
2000-07-09 12:16:51 -03:00
|
|
|
ins1(PyListObject *self, int where, 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;
|
|
|
|
}
|
2000-08-13 19:47:45 -03:00
|
|
|
if (self->ob_size == INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"cannot add more objects to list");
|
|
|
|
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;
|
|
|
|
}
|
2003-04-14 17:58:14 -03:00
|
|
|
if (where < 0) {
|
|
|
|
where += self->ob_size;
|
|
|
|
if (where < 0)
|
|
|
|
where = 0;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
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
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Insert(PyObject *op, int where, 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
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Append(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
|
2000-07-09 12:16:51 -03:00
|
|
|
list_dealloc(PyListObject *op)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int i;
|
2002-03-28 16:34:59 -04:00
|
|
|
PyObject_GC_UnTrack(op);
|
2000-03-13 12:01:29 -04:00
|
|
|
Py_TRASHCAN_SAFE_BEGIN(op)
|
1995-01-19 08:09:27 -04:00
|
|
|
if (op->ob_item != NULL) {
|
1999-06-09 12:19:34 -03:00
|
|
|
/* Do it backwards, for Christian Tismer.
|
|
|
|
There's a simple test case where somehow this reduces
|
|
|
|
thrashing when a *very* large list is created and
|
|
|
|
immediately deleted. */
|
|
|
|
i = op->ob_size;
|
|
|
|
while (--i >= 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(op->ob_item[i]);
|
1995-01-19 08:09:27 -04:00
|
|
|
}
|
2000-05-03 20:44:39 -03:00
|
|
|
PyMem_FREE(op->ob_item);
|
1995-01-19 08:09:27 -04:00
|
|
|
}
|
2001-10-05 17:51:39 -03:00
|
|
|
op->ob_type->tp_free((PyObject *)op);
|
2000-03-13 12:01:29 -04:00
|
|
|
Py_TRASHCAN_SAFE_END(op)
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-06-07 13:10:43 -03:00
|
|
|
static int
|
2000-07-09 12:16:51 -03:00
|
|
|
list_print(PyListObject *op, FILE *fp, int flags)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
list_repr(PyListObject *v)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int i;
|
2001-06-16 02:11:17 -03:00
|
|
|
PyObject *s, *temp;
|
|
|
|
PyObject *pieces = NULL, *result = NULL;
|
1998-04-10 19:47:27 -03:00
|
|
|
|
|
|
|
i = Py_ReprEnter((PyObject*)v);
|
|
|
|
if (i != 0) {
|
2001-06-16 02:11:17 -03:00
|
|
|
return i > 0 ? PyString_FromString("[...]") : NULL;
|
1998-04-10 19:47:27 -03:00
|
|
|
}
|
2001-06-16 02:11:17 -03:00
|
|
|
|
|
|
|
if (v->ob_size == 0) {
|
|
|
|
result = PyString_FromString("[]");
|
|
|
|
goto Done;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2001-06-16 02:11:17 -03:00
|
|
|
|
|
|
|
pieces = PyList_New(0);
|
|
|
|
if (pieces == NULL)
|
|
|
|
goto Done;
|
|
|
|
|
|
|
|
/* Do repr() on each element. Note that this may mutate the list,
|
|
|
|
so must refetch the list size on each iteration. */
|
|
|
|
for (i = 0; i < v->ob_size; ++i) {
|
|
|
|
int status;
|
|
|
|
s = PyObject_Repr(v->ob_item[i]);
|
|
|
|
if (s == NULL)
|
|
|
|
goto Done;
|
|
|
|
status = PyList_Append(pieces, s);
|
|
|
|
Py_DECREF(s); /* append created a new ref */
|
|
|
|
if (status < 0)
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add "[]" decorations to the first and last items. */
|
|
|
|
assert(PyList_GET_SIZE(pieces) > 0);
|
|
|
|
s = PyString_FromString("[");
|
|
|
|
if (s == NULL)
|
|
|
|
goto Done;
|
|
|
|
temp = PyList_GET_ITEM(pieces, 0);
|
|
|
|
PyString_ConcatAndDel(&s, temp);
|
|
|
|
PyList_SET_ITEM(pieces, 0, s);
|
|
|
|
if (s == NULL)
|
|
|
|
goto Done;
|
|
|
|
|
|
|
|
s = PyString_FromString("]");
|
|
|
|
if (s == NULL)
|
|
|
|
goto Done;
|
|
|
|
temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
|
|
|
|
PyString_ConcatAndDel(&temp, s);
|
|
|
|
PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
|
|
|
|
if (temp == NULL)
|
|
|
|
goto Done;
|
|
|
|
|
|
|
|
/* Paste them all together with ", " between. */
|
|
|
|
s = PyString_FromString(", ");
|
|
|
|
if (s == NULL)
|
|
|
|
goto Done;
|
|
|
|
result = _PyString_Join(s, pieces);
|
2002-07-18 23:35:45 -03:00
|
|
|
Py_DECREF(s);
|
2001-06-16 02:11:17 -03:00
|
|
|
|
|
|
|
Done:
|
|
|
|
Py_XDECREF(pieces);
|
1998-04-10 19:47:27 -03:00
|
|
|
Py_ReprLeave((PyObject *)v);
|
2001-06-16 02:11:17 -03:00
|
|
|
return result;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-07-09 12:16:51 -03:00
|
|
|
list_length(PyListObject *a)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
return a->ob_size;
|
|
|
|
}
|
|
|
|
|
2000-04-27 18:41:03 -03:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2000-07-09 12:16:51 -03:00
|
|
|
list_contains(PyListObject *a, PyObject *el)
|
2000-04-27 18:41:03 -03:00
|
|
|
{
|
2002-09-05 11:23:49 -03:00
|
|
|
int i, cmp;
|
2000-04-27 18:41:03 -03:00
|
|
|
|
2002-09-05 11:23:49 -03:00
|
|
|
for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i)
|
|
|
|
cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
|
2001-01-17 18:11:59 -04:00
|
|
|
Py_EQ);
|
2002-09-05 18:32:55 -03:00
|
|
|
return cmp;
|
2000-04-27 18:41:03 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 12:16:51 -03:00
|
|
|
list_item(PyListObject *a, int i)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
list_slice(PyListObject *a, int ilow, int ihigh)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
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 < 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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_GetSlice(PyObject *a, int ilow, int ihigh)
|
1993-06-17 09:35:49 -03:00
|
|
|
{
|
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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
list_concat(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)) {
|
2000-06-01 00:12:13 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
2000-06-01 11:31:03 -03:00
|
|
|
"can only concatenate list (not \"%.200s\") to list",
|
|
|
|
bb->ob_type->tp_name);
|
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;
|
2002-10-11 18:05:56 -03:00
|
|
|
if (size < 0)
|
|
|
|
return PyErr_NoMemory();
|
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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
list_repeat(PyListObject *a, int n)
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
int size;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyListObject *np;
|
|
|
|
PyObject **p;
|
2003-05-21 02:58:46 -03:00
|
|
|
PyObject *elem;
|
1991-03-06 09:07:53 -04:00
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
size = a->ob_size * n;
|
2003-05-21 02:58:46 -03:00
|
|
|
if (size == 0)
|
|
|
|
return PyList_New(0);
|
2002-10-11 20:39:35 -03:00
|
|
|
if (n && size/n != a->ob_size)
|
2002-10-11 18:05:56 -03:00
|
|
|
return PyErr_NoMemory();
|
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;
|
2003-05-21 02:58:46 -03:00
|
|
|
|
|
|
|
if (a->ob_size == 1) {
|
|
|
|
elem = a->ob_item[0];
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
np->ob_item[i] = elem;
|
|
|
|
Py_INCREF(elem);
|
|
|
|
}
|
|
|
|
return (PyObject *) np;
|
|
|
|
}
|
1991-03-06 09:07:53 -04:00
|
|
|
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
|
2000-07-09 12:16:51 -03:00
|
|
|
list_ass_slice(PyListObject *a, int ilow, int ihigh, 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;
|
2002-11-05 13:38:05 -04:00
|
|
|
PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
|
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;
|
2002-11-05 13:38:05 -04:00
|
|
|
else {
|
|
|
|
char msg[256];
|
2002-11-05 18:41:37 -04:00
|
|
|
PyOS_snprintf(msg, sizeof(msg),
|
|
|
|
"must assign sequence"
|
|
|
|
" (not \"%.200s\") to slice",
|
|
|
|
v->ob_type->tp_name);
|
2002-11-05 13:38:05 -04:00
|
|
|
v_as_SF = PySequence_Fast(v, msg);
|
|
|
|
if(v_as_SF == NULL)
|
|
|
|
return -1;
|
|
|
|
n = PySequence_Fast_GET_SIZE(v_as_SF);
|
|
|
|
|
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);
|
2003-05-03 07:53:08 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
1991-12-24 09:27:34 -04:00
|
|
|
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-14 09:07:46 -03:00
|
|
|
if (ilow < 0)
|
|
|
|
ilow = 0;
|
|
|
|
else if (ilow > a->ob_size)
|
|
|
|
ilow = a->ob_size;
|
|
|
|
if (ihigh < ilow)
|
|
|
|
ihigh = ilow;
|
|
|
|
else if (ihigh > a->ob_size)
|
|
|
|
ihigh = a->ob_size;
|
|
|
|
item = a->ob_item;
|
|
|
|
d = n - (ihigh-ilow);
|
2003-05-03 07:53:08 -03:00
|
|
|
if (ihigh > ilow) {
|
1997-05-02 00:12:38 -03:00
|
|
|
p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
|
2003-05-03 07:53:08 -03:00
|
|
|
if (recycle == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2000-05-03 20:44:39 -03:00
|
|
|
if (recycle != NULL)
|
|
|
|
PyMem_DEL(recycle);
|
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
|
|
|
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++) {
|
2002-11-05 13:38:05 -04:00
|
|
|
PyObject *w = PySequence_Fast_GET_ITEM(v_as_SF, k);
|
1997-05-02 00:12:38 -03:00
|
|
|
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
|
|
|
}
|
2001-08-02 01:15:00 -03:00
|
|
|
if (a->ob_size == 0 && a->ob_item != NULL) {
|
|
|
|
PyMem_FREE(a->ob_item);
|
|
|
|
a->ob_item = NULL;
|
|
|
|
}
|
2002-11-05 13:38:05 -04:00
|
|
|
Py_XDECREF(v_as_SF);
|
1990-10-14 09:07:46 -03:00
|
|
|
return 0;
|
|
|
|
#undef b
|
|
|
|
}
|
|
|
|
|
1993-06-17 09:35:49 -03:00
|
|
|
int
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_SetSlice(PyObject *a, int ilow, int ihigh, 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
|
|
|
}
|
|
|
|
|
2000-08-24 17:08:19 -03:00
|
|
|
static PyObject *
|
|
|
|
list_inplace_repeat(PyListObject *self, int n)
|
|
|
|
{
|
|
|
|
PyObject **items;
|
|
|
|
int size, i, j;
|
|
|
|
|
|
|
|
|
|
|
|
size = PyList_GET_SIZE(self);
|
|
|
|
if (size == 0) {
|
|
|
|
Py_INCREF(self);
|
|
|
|
return (PyObject *)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
items = self->ob_item;
|
|
|
|
|
|
|
|
if (n < 1) {
|
|
|
|
self->ob_item = NULL;
|
|
|
|
self->ob_size = 0;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
Py_XDECREF(items[i]);
|
|
|
|
PyMem_DEL(items);
|
|
|
|
Py_INCREF(self);
|
|
|
|
return (PyObject *)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
NRESIZE(items, PyObject*, size*n);
|
|
|
|
if (items == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
goto finally;
|
|
|
|
}
|
|
|
|
self->ob_item = items;
|
|
|
|
for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
|
|
|
|
for (j = 0; j < size; j++) {
|
|
|
|
PyObject *o = PyList_GET_ITEM(self, j);
|
|
|
|
Py_INCREF(o);
|
|
|
|
PyList_SET_ITEM(self, self->ob_size++, o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Py_INCREF(self);
|
|
|
|
return (PyObject *)self;
|
|
|
|
finally:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1991-04-03 15:05:18 -04:00
|
|
|
static int
|
2000-07-09 12:16:51 -03:00
|
|
|
list_ass_item(PyListObject *a, int i, 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;
|
2002-07-18 23:35:45 -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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
ins(PyListObject *self, int where, 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 *
|
2000-07-09 12:16:51 -03:00
|
|
|
listinsert(PyListObject *self, PyObject *args)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
2000-02-24 17:48:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "iO:insert", &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 *
|
2001-08-16 10:15:00 -03:00
|
|
|
listappend(PyListObject *self, PyObject *v)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1993-02-08 11:49:17 -04:00
|
|
|
return ins(self, (int) self->ob_size, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2000-08-24 17:08:19 -03:00
|
|
|
static int
|
|
|
|
listextend_internal(PyListObject *self, PyObject *b)
|
1998-10-09 13:37:25 -03:00
|
|
|
{
|
|
|
|
PyObject **items;
|
|
|
|
int selflen = PyList_GET_SIZE(self);
|
|
|
|
int blen;
|
|
|
|
register int i;
|
|
|
|
|
2001-01-03 18:32:16 -04:00
|
|
|
if (PyObject_Size(b) == 0) {
|
1998-10-09 13:37:25 -03:00
|
|
|
/* short circuit when b is empty */
|
2001-01-03 18:32:16 -04:00
|
|
|
Py_DECREF(b);
|
2000-08-24 17:08:19 -03:00
|
|
|
return 0;
|
2001-01-03 18:32:16 -04:00
|
|
|
}
|
2000-06-18 15:43:14 -03:00
|
|
|
|
1998-10-09 13:37:25 -03:00
|
|
|
if (self == (PyListObject*)b) {
|
|
|
|
/* as in list_ass_slice() we must special case the
|
|
|
|
* situation: a.extend(a)
|
|
|
|
*
|
|
|
|
* XXX: I think this way ought to be faster than using
|
|
|
|
* list_slice() the way list_ass_slice() does.
|
|
|
|
*/
|
2000-06-18 15:43:14 -03:00
|
|
|
Py_DECREF(b);
|
1998-10-09 13:37:25 -03:00
|
|
|
b = PyList_New(selflen);
|
|
|
|
if (!b)
|
2000-08-24 17:08:19 -03:00
|
|
|
return -1;
|
1998-10-09 13:37:25 -03:00
|
|
|
for (i = 0; i < selflen; i++) {
|
|
|
|
PyObject *o = PyList_GET_ITEM(self, i);
|
|
|
|
Py_INCREF(o);
|
|
|
|
PyList_SET_ITEM(b, i, o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-12 10:05:33 -03:00
|
|
|
blen = PyObject_Size(b);
|
2000-06-18 15:43:14 -03:00
|
|
|
|
1998-10-09 13:37:25 -03:00
|
|
|
/* resize a using idiom */
|
|
|
|
items = self->ob_item;
|
|
|
|
NRESIZE(items, PyObject*, selflen + blen);
|
2000-06-18 15:43:14 -03:00
|
|
|
if (items == NULL) {
|
1998-10-09 13:37:25 -03:00
|
|
|
PyErr_NoMemory();
|
2000-08-24 17:08:19 -03:00
|
|
|
Py_DECREF(b);
|
|
|
|
return -1;
|
1998-10-09 13:37:25 -03:00
|
|
|
}
|
2000-08-24 17:08:19 -03:00
|
|
|
|
1998-10-09 13:37:25 -03:00
|
|
|
self->ob_item = items;
|
|
|
|
|
2000-06-18 15:43:14 -03:00
|
|
|
/* populate the end of self with b's items */
|
1998-10-09 13:37:25 -03:00
|
|
|
for (i = 0; i < blen; i++) {
|
2000-06-18 15:43:14 -03:00
|
|
|
PyObject *o = PySequence_Fast_GET_ITEM(b, i);
|
1998-10-09 13:37:25 -03:00
|
|
|
Py_INCREF(o);
|
|
|
|
PyList_SET_ITEM(self, self->ob_size++, o);
|
|
|
|
}
|
2000-06-18 15:43:14 -03:00
|
|
|
Py_DECREF(b);
|
2000-08-24 17:08:19 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
list_inplace_concat(PyListObject *self, PyObject *other)
|
|
|
|
{
|
2001-05-26 16:37:54 -03:00
|
|
|
other = PySequence_Fast(other, "argument to += must be iterable");
|
2000-08-24 17:08:19 -03:00
|
|
|
if (!other)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (listextend_internal(self, other) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(self);
|
|
|
|
return (PyObject *)self;
|
1998-10-09 13:37:25 -03:00
|
|
|
}
|
|
|
|
|
2000-08-24 17:08:19 -03:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
listextend(PyListObject *self, PyObject *b)
|
2000-08-24 17:08:19 -03:00
|
|
|
{
|
|
|
|
|
2001-05-26 16:37:54 -03:00
|
|
|
b = PySequence_Fast(b, "list.extend() argument must be iterable");
|
2000-08-24 17:08:19 -03:00
|
|
|
if (!b)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (listextend_internal(self, b) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
1998-10-09 13:37:25 -03:00
|
|
|
|
1998-06-30 12:36:32 -03:00
|
|
|
static PyObject *
|
2000-07-09 12:16:51 -03:00
|
|
|
listpop(PyListObject *self, PyObject *args)
|
1998-06-30 12:36:32 -03:00
|
|
|
{
|
|
|
|
int i = -1;
|
|
|
|
PyObject *v;
|
2000-02-24 17:48:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "|i:pop", &i))
|
1998-06-30 12:36:32 -03:00
|
|
|
return NULL;
|
|
|
|
if (self->ob_size == 0) {
|
|
|
|
/* Special-case most common failure cause */
|
|
|
|
PyErr_SetString(PyExc_IndexError, "pop from empty list");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (i < 0)
|
|
|
|
i += self->ob_size;
|
|
|
|
if (i < 0 || i >= self->ob_size) {
|
|
|
|
PyErr_SetString(PyExc_IndexError, "pop index out of range");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
v = self->ob_item[i];
|
|
|
|
Py_INCREF(v);
|
|
|
|
if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
|
|
|
|
Py_DECREF(v);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:33:08 -03:00
|
|
|
/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
|
|
|
|
static void
|
|
|
|
reverse_slice(PyObject **lo, PyObject **hi)
|
|
|
|
{
|
|
|
|
assert(lo && hi);
|
|
|
|
|
|
|
|
--hi;
|
|
|
|
while (lo < hi) {
|
|
|
|
PyObject *t = *lo;
|
|
|
|
*lo = *hi;
|
|
|
|
*hi = t;
|
|
|
|
++lo;
|
|
|
|
--hi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Lots of code for an adaptive, stable, natural mergesort. There are many
|
|
|
|
* pieces to this algorithm; read listsort.txt for overviews and details.
|
|
|
|
*/
|
1996-12-10 19:55:39 -04:00
|
|
|
|
|
|
|
/* Comparison function. Takes care of calling a user-supplied
|
2002-08-04 14:47:26 -03:00
|
|
|
* comparison function (any callable Python object), which must not be
|
|
|
|
* NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool
|
|
|
|
* with Py_LT if you know it's NULL).
|
2002-07-31 23:13:36 -03:00
|
|
|
* Returns -1 on error, 1 if x < y, 0 if x >= y.
|
|
|
|
*/
|
1996-12-10 19:55:39 -04:00
|
|
|
static int
|
2002-07-19 00:30:57 -03:00
|
|
|
islt(PyObject *x, PyObject *y, PyObject *compare)
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
2002-07-11 18:46:16 -03:00
|
|
|
PyObject *res;
|
|
|
|
PyObject *args;
|
1996-12-10 19:55:39 -04:00
|
|
|
int i;
|
|
|
|
|
2002-08-04 14:47:26 -03:00
|
|
|
assert(compare != NULL);
|
2002-07-19 00:30:57 -03:00
|
|
|
/* Call the user's comparison function and translate the 3-way
|
|
|
|
* result into true or false (or error).
|
|
|
|
*/
|
2002-07-11 18:46:16 -03:00
|
|
|
args = PyTuple_New(2);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (args == NULL)
|
2002-07-19 00:30:57 -03:00
|
|
|
return -1;
|
2002-07-11 18:46:16 -03:00
|
|
|
Py_INCREF(x);
|
|
|
|
Py_INCREF(y);
|
|
|
|
PyTuple_SET_ITEM(args, 0, x);
|
|
|
|
PyTuple_SET_ITEM(args, 1, y);
|
2002-07-15 02:16:13 -03:00
|
|
|
res = PyObject_Call(compare, args, NULL);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(args);
|
1996-12-10 19:55:39 -04:00
|
|
|
if (res == NULL)
|
2002-07-19 00:30:57 -03:00
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInt_Check(res)) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1999-04-19 14:44:39 -03:00
|
|
|
"comparison function must return int");
|
2002-07-19 00:30:57 -03:00
|
|
|
return -1;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
i = PyInt_AsLong(res);
|
|
|
|
Py_DECREF(res);
|
2002-07-19 00:30:57 -03:00
|
|
|
return i < 0;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
|
|
|
|
2002-08-04 14:47:26 -03:00
|
|
|
/* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls
|
|
|
|
* islt. This avoids a layer of function call in the usual case, and
|
|
|
|
* sorting does many comparisons.
|
|
|
|
* Returns -1 on error, 1 if x < y, 0 if x >= y.
|
|
|
|
*/
|
|
|
|
#define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \
|
|
|
|
PyObject_RichCompareBool(X, Y, Py_LT) : \
|
|
|
|
islt(X, Y, COMPARE))
|
|
|
|
|
|
|
|
/* Compare X to Y via "<". Goto "fail" if the comparison raises an
|
2002-07-19 00:30:57 -03:00
|
|
|
error. Else "k" is set to true iff X<Y, and an "if (k)" block is
|
|
|
|
started. It makes more sense in context <wink>. X and Y are PyObject*s.
|
|
|
|
*/
|
2002-08-04 14:47:26 -03:00
|
|
|
#define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \
|
2002-07-19 00:30:57 -03:00
|
|
|
if (k)
|
1998-06-16 12:18:28 -03:00
|
|
|
|
|
|
|
/* binarysort is the best method for sorting small arrays: it does
|
|
|
|
few compares, but can do data movement quadratic in the number of
|
|
|
|
elements.
|
1998-06-17 11:15:44 -03:00
|
|
|
[lo, hi) is a contiguous slice of a list, and is sorted via
|
2002-07-19 00:30:57 -03:00
|
|
|
binary insertion. This sort is stable.
|
1998-06-16 12:18:28 -03:00
|
|
|
On entry, must have lo <= start <= hi, and that [lo, start) is already
|
|
|
|
sorted (pass start == lo if you don't know!).
|
2002-07-19 00:30:57 -03:00
|
|
|
If islt() complains return -1, else 0.
|
1998-06-16 12:18:28 -03:00
|
|
|
Even in case of error, the output slice will be some permutation of
|
|
|
|
the input (nothing is lost or duplicated).
|
|
|
|
*/
|
|
|
|
static int
|
2000-07-09 12:16:51 -03:00
|
|
|
binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
|
|
|
|
/* compare -- comparison function object, or NULL for default */
|
1998-06-16 12:18:28 -03:00
|
|
|
{
|
|
|
|
register int k;
|
|
|
|
register PyObject **l, **p, **r;
|
|
|
|
register PyObject *pivot;
|
|
|
|
|
2002-07-19 00:30:57 -03:00
|
|
|
assert(lo <= start && start <= hi);
|
|
|
|
/* assert [lo, start) is sorted */
|
1998-06-16 12:18:28 -03:00
|
|
|
if (lo == start)
|
|
|
|
++start;
|
|
|
|
for (; start < hi; ++start) {
|
|
|
|
/* set l to where *start belongs */
|
|
|
|
l = lo;
|
|
|
|
r = start;
|
|
|
|
pivot = *r;
|
2002-07-19 03:12:32 -03:00
|
|
|
/* Invariants:
|
|
|
|
* pivot >= all in [lo, l).
|
|
|
|
* pivot < all in [r, start).
|
|
|
|
* The second is vacuously true at the start.
|
|
|
|
*/
|
|
|
|
assert(l < r);
|
1998-06-16 12:18:28 -03:00
|
|
|
do {
|
|
|
|
p = l + ((r - l) >> 1);
|
2002-07-19 00:30:57 -03:00
|
|
|
IFLT(pivot, *p)
|
1998-06-16 12:18:28 -03:00
|
|
|
r = p;
|
|
|
|
else
|
2002-07-19 03:12:32 -03:00
|
|
|
l = p+1;
|
1998-06-16 12:18:28 -03:00
|
|
|
} while (l < r);
|
2002-07-19 03:12:32 -03:00
|
|
|
assert(l == r);
|
|
|
|
/* The invariants still hold, so pivot >= all in [lo, l) and
|
|
|
|
pivot < all in [l, start), so pivot belongs at l. Note
|
|
|
|
that if there are elements equal to pivot, l points to the
|
|
|
|
first slot after them -- that's why this sort is stable.
|
|
|
|
Slide over to make room.
|
1998-06-16 12:18:28 -03:00
|
|
|
Caution: using memmove is much slower under MSVC 5;
|
|
|
|
we're not usually moving many slots. */
|
|
|
|
for (p = start; p > l; --p)
|
|
|
|
*p = *(p-1);
|
|
|
|
*l = pivot;
|
|
|
|
}
|
|
|
|
return 0;
|
1996-12-10 19:55:39 -04:00
|
|
|
|
1998-06-16 12:18:28 -03:00
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/*
|
|
|
|
Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
|
|
|
|
is required on entry. "A run" is the longest ascending sequence, with
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
lo[0] <= lo[1] <= lo[2] <= ...
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
or the longest descending sequence, with
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
lo[0] > lo[1] > lo[2] > ...
|
|
|
|
|
|
|
|
Boolean *descending is set to 0 in the former case, or to 1 in the latter.
|
|
|
|
For its intended use in a stable mergesort, the strictness of the defn of
|
|
|
|
"descending" is needed so that the caller can safely reverse a descending
|
|
|
|
sequence without violating stability (strict > ensures there are no equal
|
|
|
|
elements to get out of order).
|
1996-12-10 19:55:39 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
Returns -1 in case of error.
|
|
|
|
*/
|
1996-12-10 19:55:39 -04:00
|
|
|
static int
|
2002-07-31 23:13:36 -03:00
|
|
|
count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
2002-07-31 23:13:36 -03:00
|
|
|
int k;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
assert(lo < hi);
|
|
|
|
*descending = 0;
|
|
|
|
++lo;
|
|
|
|
if (lo == hi)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
n = 2;
|
|
|
|
IFLT(*lo, *(lo-1)) {
|
|
|
|
*descending = 1;
|
|
|
|
for (lo = lo+1; lo < hi; ++lo, ++n) {
|
|
|
|
IFLT(*lo, *(lo-1))
|
|
|
|
;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (lo = lo+1; lo < hi; ++lo, ++n) {
|
|
|
|
IFLT(*lo, *(lo-1))
|
|
|
|
break;
|
1998-06-16 12:18:28 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
return n;
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/*
|
|
|
|
Locate the proper position of key in a sorted vector; if the vector contains
|
|
|
|
an element equal to key, return the position immediately to the left of
|
|
|
|
the leftmost equal element. [gallop_right() does the same except returns
|
|
|
|
the position to the right of the rightmost equal element (if any).]
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
"a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
|
1998-05-26 12:06:32 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
"hint" is an index at which to begin the search, 0 <= hint < n. The closer
|
|
|
|
hint is to the final result, the faster this runs.
|
|
|
|
|
|
|
|
The return value is the int k in 0..n such that
|
|
|
|
|
|
|
|
a[k-1] < key <= a[k]
|
|
|
|
|
|
|
|
pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
|
|
|
|
key belongs at index k; or, IOW, the first k elements of a should precede
|
|
|
|
key, and the last n-k should follow key.
|
|
|
|
|
|
|
|
Returns -1 on error. See listsort.txt for info on the method.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
|
|
|
|
{
|
|
|
|
int ofs;
|
|
|
|
int lastofs;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
assert(key && a && n > 0 && hint >= 0 && hint < n);
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
a += hint;
|
|
|
|
lastofs = 0;
|
|
|
|
ofs = 1;
|
|
|
|
IFLT(*a, key) {
|
|
|
|
/* a[hint] < key -- gallop right, until
|
|
|
|
* a[hint + lastofs] < key <= a[hint + ofs]
|
|
|
|
*/
|
|
|
|
const int maxofs = n - hint; /* &a[n-1] is highest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(a[ofs], key) {
|
|
|
|
lastofs = ofs;
|
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
if (ofs <= 0) /* int overflow */
|
|
|
|
ofs = maxofs;
|
1998-05-26 12:06:32 -03:00
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
else /* key <= a[hint + ofs] */
|
|
|
|
break;
|
1998-05-26 12:06:32 -03:00
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to offsets relative to &a[0]. */
|
|
|
|
lastofs += hint;
|
|
|
|
ofs += hint;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* key <= a[hint] -- gallop left, until
|
|
|
|
* a[hint - ofs] < key <= a[hint - lastofs]
|
|
|
|
*/
|
|
|
|
const int maxofs = hint + 1; /* &a[0] is lowest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(*(a-ofs), key)
|
|
|
|
break;
|
|
|
|
/* key <= a[hint - ofs] */
|
|
|
|
lastofs = ofs;
|
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
if (ofs <= 0) /* int overflow */
|
|
|
|
ofs = maxofs;
|
|
|
|
}
|
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to positive offsets relative to &a[0]. */
|
|
|
|
k = lastofs;
|
|
|
|
lastofs = hint - ofs;
|
|
|
|
ofs = hint - k;
|
|
|
|
}
|
|
|
|
a -= hint;
|
|
|
|
|
|
|
|
assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
|
|
|
|
/* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
|
|
|
|
* right of lastofs but no farther right than ofs. Do a binary
|
|
|
|
* search, with invariant a[lastofs-1] < key <= a[ofs].
|
|
|
|
*/
|
|
|
|
++lastofs;
|
|
|
|
while (lastofs < ofs) {
|
|
|
|
int m = lastofs + ((ofs - lastofs) >> 1);
|
|
|
|
|
|
|
|
IFLT(a[m], key)
|
|
|
|
lastofs = m+1; /* a[m] < key */
|
|
|
|
else
|
|
|
|
ofs = m; /* key <= a[m] */
|
|
|
|
}
|
|
|
|
assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
|
|
|
|
return ofs;
|
1996-12-10 19:55:39 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Exactly like gallop_left(), except that if key already exists in a[0:n],
|
|
|
|
finds the position immediately to the right of the rightmost equal value.
|
|
|
|
|
|
|
|
The return value is the int k in 0..n such that
|
|
|
|
|
|
|
|
a[k-1] <= key < a[k]
|
|
|
|
|
|
|
|
or -1 if error.
|
|
|
|
|
|
|
|
The code duplication is massive, but this is enough different given that
|
|
|
|
we're sticking to "<" comparisons that it's much harder to follow if
|
|
|
|
written as one routine with yet another "left or right?" flag.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare)
|
|
|
|
{
|
|
|
|
int ofs;
|
|
|
|
int lastofs;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
assert(key && a && n > 0 && hint >= 0 && hint < n);
|
|
|
|
|
|
|
|
a += hint;
|
|
|
|
lastofs = 0;
|
|
|
|
ofs = 1;
|
|
|
|
IFLT(key, *a) {
|
|
|
|
/* key < a[hint] -- gallop left, until
|
|
|
|
* a[hint - ofs] <= key < a[hint - lastofs]
|
|
|
|
*/
|
|
|
|
const int maxofs = hint + 1; /* &a[0] is lowest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(key, *(a-ofs)) {
|
|
|
|
lastofs = ofs;
|
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
if (ofs <= 0) /* int overflow */
|
|
|
|
ofs = maxofs;
|
|
|
|
}
|
|
|
|
else /* a[hint - ofs] <= key */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to positive offsets relative to &a[0]. */
|
|
|
|
k = lastofs;
|
|
|
|
lastofs = hint - ofs;
|
|
|
|
ofs = hint - k;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* a[hint] <= key -- gallop right, until
|
|
|
|
* a[hint + lastofs] <= key < a[hint + ofs]
|
1998-06-16 12:18:28 -03:00
|
|
|
*/
|
2002-07-31 23:13:36 -03:00
|
|
|
const int maxofs = n - hint; /* &a[n-1] is highest */
|
|
|
|
while (ofs < maxofs) {
|
|
|
|
IFLT(key, a[ofs])
|
|
|
|
break;
|
|
|
|
/* a[hint + ofs] <= key */
|
|
|
|
lastofs = ofs;
|
|
|
|
ofs = (ofs << 1) + 1;
|
|
|
|
if (ofs <= 0) /* int overflow */
|
|
|
|
ofs = maxofs;
|
1998-05-29 14:56:32 -03:00
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
if (ofs > maxofs)
|
|
|
|
ofs = maxofs;
|
|
|
|
/* Translate back to offsets relative to &a[0]. */
|
|
|
|
lastofs += hint;
|
|
|
|
ofs += hint;
|
|
|
|
}
|
|
|
|
a -= hint;
|
|
|
|
|
|
|
|
assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
|
|
|
|
/* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
|
|
|
|
* right of lastofs but no farther right than ofs. Do a binary
|
|
|
|
* search, with invariant a[lastofs-1] <= key < a[ofs].
|
|
|
|
*/
|
|
|
|
++lastofs;
|
|
|
|
while (lastofs < ofs) {
|
|
|
|
int m = lastofs + ((ofs - lastofs) >> 1);
|
|
|
|
|
|
|
|
IFLT(key, a[m])
|
|
|
|
ofs = m; /* key < a[m] */
|
|
|
|
else
|
|
|
|
lastofs = m+1; /* a[m] <= key */
|
|
|
|
}
|
|
|
|
assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
|
|
|
|
return ofs;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The maximum number of entries in a MergeState's pending-runs stack.
|
|
|
|
* This is enough to sort arrays of size up to about
|
|
|
|
* 32 * phi ** MAX_MERGE_PENDING
|
|
|
|
* where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
|
|
|
|
* with 2**64 elements.
|
|
|
|
*/
|
|
|
|
#define MAX_MERGE_PENDING 85
|
|
|
|
|
2002-08-10 02:21:15 -03:00
|
|
|
/* When we get into galloping mode, we stay there until both runs win less
|
|
|
|
* often than MIN_GALLOP consecutive times. See listsort.txt for more info.
|
2002-07-31 23:13:36 -03:00
|
|
|
*/
|
2002-08-10 02:21:15 -03:00
|
|
|
#define MIN_GALLOP 7
|
2002-07-31 23:13:36 -03:00
|
|
|
|
|
|
|
/* Avoid malloc for small temp arrays. */
|
|
|
|
#define MERGESTATE_TEMP_SIZE 256
|
|
|
|
|
|
|
|
/* One MergeState exists on the stack per invocation of mergesort. It's just
|
|
|
|
* a convenient way to pass state around among the helper functions.
|
|
|
|
*/
|
2002-08-10 02:21:15 -03:00
|
|
|
struct s_slice {
|
|
|
|
PyObject **base;
|
|
|
|
int len;
|
|
|
|
};
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
typedef struct s_MergeState {
|
|
|
|
/* The user-supplied comparison function. or NULL if none given. */
|
|
|
|
PyObject *compare;
|
|
|
|
|
2002-08-10 02:21:15 -03:00
|
|
|
/* This controls when we get *into* galloping mode. It's initialized
|
|
|
|
* to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
|
|
|
|
* random data, and lower for highly structured data.
|
|
|
|
*/
|
|
|
|
int min_gallop;
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* 'a' is temp storage to help with merges. It contains room for
|
|
|
|
* alloced entries.
|
|
|
|
*/
|
|
|
|
PyObject **a; /* may point to temparray below */
|
|
|
|
int alloced;
|
|
|
|
|
|
|
|
/* A stack of n pending runs yet to be merged. Run #i starts at
|
|
|
|
* address base[i] and extends for len[i] elements. It's always
|
|
|
|
* true (so long as the indices are in bounds) that
|
|
|
|
*
|
2002-08-10 02:21:15 -03:00
|
|
|
* pending[i].base + pending[i].len == pending[i+1].base
|
2002-07-31 23:13:36 -03:00
|
|
|
*
|
|
|
|
* so we could cut the storage for this, but it's a minor amount,
|
|
|
|
* and keeping all the info explicit simplifies the code.
|
|
|
|
*/
|
|
|
|
int n;
|
2002-08-10 02:21:15 -03:00
|
|
|
struct s_slice pending[MAX_MERGE_PENDING];
|
2002-07-31 23:13:36 -03:00
|
|
|
|
|
|
|
/* 'a' points to this when possible, rather than muck with malloc. */
|
|
|
|
PyObject *temparray[MERGESTATE_TEMP_SIZE];
|
|
|
|
} MergeState;
|
|
|
|
|
|
|
|
/* Conceptually a MergeState's constructor. */
|
|
|
|
static void
|
|
|
|
merge_init(MergeState *ms, PyObject *compare)
|
|
|
|
{
|
|
|
|
assert(ms != NULL);
|
|
|
|
ms->compare = compare;
|
|
|
|
ms->a = ms->temparray;
|
|
|
|
ms->alloced = MERGESTATE_TEMP_SIZE;
|
|
|
|
ms->n = 0;
|
2002-08-10 02:21:15 -03:00
|
|
|
ms->min_gallop = MIN_GALLOP;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free all the temp memory owned by the MergeState. This must be called
|
|
|
|
* when you're done with a MergeState, and may be called before then if
|
|
|
|
* you want to free the temp memory early.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
merge_freemem(MergeState *ms)
|
|
|
|
{
|
|
|
|
assert(ms != NULL);
|
|
|
|
if (ms->a != ms->temparray)
|
|
|
|
PyMem_Free(ms->a);
|
|
|
|
ms->a = ms->temparray;
|
|
|
|
ms->alloced = MERGESTATE_TEMP_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure enough temp memory for 'need' array slots is available.
|
|
|
|
* Returns 0 on success and -1 if the memory can't be gotten.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_getmem(MergeState *ms, int need)
|
|
|
|
{
|
|
|
|
assert(ms != NULL);
|
|
|
|
if (need <= ms->alloced)
|
|
|
|
return 0;
|
|
|
|
/* Don't realloc! That can cost cycles to copy the old data, but
|
|
|
|
* we don't care what's in the block.
|
|
|
|
*/
|
|
|
|
merge_freemem(ms);
|
|
|
|
ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
|
|
|
|
if (ms->a) {
|
|
|
|
ms->alloced = need;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
PyErr_NoMemory();
|
|
|
|
merge_freemem(ms); /* reset to sane state */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
|
|
|
|
merge_getmem(MS, NEED))
|
|
|
|
|
|
|
|
/* Merge the na elements starting at pa with the nb elements starting at pb
|
|
|
|
* in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
|
|
|
|
* Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
|
|
|
|
* merge, and should have na <= nb. See listsort.txt for more info.
|
|
|
|
* Return 0 if successful, -1 if error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
PyObject *compare;
|
|
|
|
PyObject **dest;
|
|
|
|
int result = -1; /* guilty until proved innocent */
|
2002-08-10 02:21:15 -03:00
|
|
|
int min_gallop = ms->min_gallop;
|
2002-07-31 23:13:36 -03:00
|
|
|
|
|
|
|
assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
|
|
|
|
if (MERGE_GETMEM(ms, na) < 0)
|
|
|
|
return -1;
|
|
|
|
memcpy(ms->a, pa, na * sizeof(PyObject*));
|
|
|
|
dest = pa;
|
|
|
|
pa = ms->a;
|
|
|
|
|
|
|
|
*dest++ = *pb++;
|
|
|
|
--nb;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
|
|
|
|
|
|
|
compare = ms->compare;
|
|
|
|
for (;;) {
|
|
|
|
int acount = 0; /* # of times A won in a row */
|
|
|
|
int bcount = 0; /* # of times B won in a row */
|
|
|
|
|
|
|
|
/* Do the straightforward thing until (if ever) one run
|
|
|
|
* appears to win consistently.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
2002-08-10 02:21:15 -03:00
|
|
|
assert(na > 1 && nb > 0);
|
2002-08-04 14:47:26 -03:00
|
|
|
k = ISLT(*pb, *pa, compare);
|
2002-07-31 23:13:36 -03:00
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
*dest++ = *pb++;
|
|
|
|
++bcount;
|
|
|
|
acount = 0;
|
|
|
|
--nb;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
2002-08-10 02:21:15 -03:00
|
|
|
if (bcount >= min_gallop)
|
2002-07-31 23:13:36 -03:00
|
|
|
break;
|
1998-05-29 14:56:32 -03:00
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
else {
|
|
|
|
*dest++ = *pa++;
|
|
|
|
++acount;
|
|
|
|
bcount = 0;
|
|
|
|
--na;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
2002-08-10 02:21:15 -03:00
|
|
|
if (acount >= min_gallop)
|
2002-07-31 23:13:36 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* One run is winning so consistently that galloping may
|
|
|
|
* be a huge win. So try that, and continue galloping until
|
|
|
|
* (if ever) neither run appears to be winning consistently
|
|
|
|
* anymore.
|
|
|
|
*/
|
2002-08-10 02:21:15 -03:00
|
|
|
++min_gallop;
|
1998-06-16 12:18:28 -03:00
|
|
|
do {
|
2002-08-10 02:21:15 -03:00
|
|
|
assert(na > 1 && nb > 0);
|
|
|
|
min_gallop -= min_gallop > 1;
|
|
|
|
ms->min_gallop = min_gallop;
|
2002-07-31 23:13:36 -03:00
|
|
|
k = gallop_right(*pb, pa, na, 0, compare);
|
|
|
|
acount = k;
|
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
memcpy(dest, pa, k * sizeof(PyObject *));
|
|
|
|
dest += k;
|
|
|
|
pa += k;
|
|
|
|
na -= k;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
|
|
|
/* na==0 is impossible now if the comparison
|
|
|
|
* function is consistent, but we can't assume
|
|
|
|
* that it is.
|
|
|
|
*/
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
|
|
|
*dest++ = *pb++;
|
|
|
|
--nb;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
|
|
|
|
k = gallop_left(*pa, pb, nb, 0, compare);
|
|
|
|
bcount = k;
|
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
memmove(dest, pb, k * sizeof(PyObject *));
|
|
|
|
dest += k;
|
|
|
|
pb += k;
|
|
|
|
nb -= k;
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
|
|
|
*dest++ = *pa++;
|
|
|
|
--na;
|
|
|
|
if (na == 1)
|
|
|
|
goto CopyB;
|
|
|
|
} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
|
2002-08-10 02:21:15 -03:00
|
|
|
++min_gallop; /* penalize it for leaving galloping mode */
|
|
|
|
ms->min_gallop = min_gallop;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
Succeed:
|
|
|
|
result = 0;
|
|
|
|
Fail:
|
|
|
|
if (na)
|
|
|
|
memcpy(dest, pa, na * sizeof(PyObject*));
|
|
|
|
return result;
|
|
|
|
CopyB:
|
|
|
|
assert(na == 1 && nb > 0);
|
|
|
|
/* The last element of pa belongs at the end of the merge. */
|
|
|
|
memmove(dest, pb, nb * sizeof(PyObject *));
|
|
|
|
dest[nb] = *pa;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge the na elements starting at pa with the nb elements starting at pb
|
|
|
|
* in a stable way, in-place. na and nb must be > 0, and pa + na == pb.
|
|
|
|
* Must also have that *pb < *pa, that pa[na-1] belongs at the end of the
|
|
|
|
* merge, and should have na >= nb. See listsort.txt for more info.
|
|
|
|
* Return 0 if successful, -1 if error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb)
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
PyObject *compare;
|
|
|
|
PyObject **dest;
|
|
|
|
int result = -1; /* guilty until proved innocent */
|
|
|
|
PyObject **basea;
|
|
|
|
PyObject **baseb;
|
2002-08-10 02:21:15 -03:00
|
|
|
int min_gallop = ms->min_gallop;
|
2002-07-31 23:13:36 -03:00
|
|
|
|
|
|
|
assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
|
|
|
|
if (MERGE_GETMEM(ms, nb) < 0)
|
|
|
|
return -1;
|
|
|
|
dest = pb + nb - 1;
|
|
|
|
memcpy(ms->a, pb, nb * sizeof(PyObject*));
|
|
|
|
basea = pa;
|
|
|
|
baseb = ms->a;
|
|
|
|
pb = ms->a + nb - 1;
|
|
|
|
pa += na - 1;
|
|
|
|
|
|
|
|
*dest-- = *pa--;
|
|
|
|
--na;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
|
|
|
|
|
|
|
compare = ms->compare;
|
|
|
|
for (;;) {
|
|
|
|
int acount = 0; /* # of times A won in a row */
|
|
|
|
int bcount = 0; /* # of times B won in a row */
|
|
|
|
|
|
|
|
/* Do the straightforward thing until (if ever) one run
|
|
|
|
* appears to win consistently.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
2002-08-10 02:21:15 -03:00
|
|
|
assert(na > 0 && nb > 1);
|
2002-08-04 14:47:26 -03:00
|
|
|
k = ISLT(*pb, *pa, compare);
|
2002-07-31 23:13:36 -03:00
|
|
|
if (k) {
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
*dest-- = *pa--;
|
|
|
|
++acount;
|
|
|
|
bcount = 0;
|
|
|
|
--na;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
2002-08-10 02:21:15 -03:00
|
|
|
if (acount >= min_gallop)
|
1996-12-10 19:55:39 -04:00
|
|
|
break;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*dest-- = *pb--;
|
|
|
|
++bcount;
|
|
|
|
acount = 0;
|
|
|
|
--nb;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
2002-08-10 02:21:15 -03:00
|
|
|
if (bcount >= min_gallop)
|
1996-12-10 19:55:39 -04:00
|
|
|
break;
|
1997-12-10 11:14:24 -04:00
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
1997-12-10 11:14:24 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* One run is winning so consistently that galloping may
|
|
|
|
* be a huge win. So try that, and continue galloping until
|
|
|
|
* (if ever) neither run appears to be winning consistently
|
|
|
|
* anymore.
|
|
|
|
*/
|
2002-08-10 02:21:15 -03:00
|
|
|
++min_gallop;
|
2002-07-31 23:13:36 -03:00
|
|
|
do {
|
2002-08-10 02:21:15 -03:00
|
|
|
assert(na > 0 && nb > 1);
|
|
|
|
min_gallop -= min_gallop > 1;
|
|
|
|
ms->min_gallop = min_gallop;
|
2002-07-31 23:13:36 -03:00
|
|
|
k = gallop_right(*pb, basea, na, na-1, compare);
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
k = na - k;
|
|
|
|
acount = k;
|
|
|
|
if (k) {
|
|
|
|
dest -= k;
|
|
|
|
pa -= k;
|
|
|
|
memmove(dest+1, pa+1, k * sizeof(PyObject *));
|
|
|
|
na -= k;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
|
|
|
*dest-- = *pb--;
|
|
|
|
--nb;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
|
|
|
|
|
|
|
k = gallop_left(*pa, baseb, nb, nb-1, compare);
|
|
|
|
if (k < 0)
|
|
|
|
goto Fail;
|
|
|
|
k = nb - k;
|
|
|
|
bcount = k;
|
|
|
|
if (k) {
|
|
|
|
dest -= k;
|
|
|
|
pb -= k;
|
|
|
|
memcpy(dest+1, pb+1, k * sizeof(PyObject *));
|
|
|
|
nb -= k;
|
|
|
|
if (nb == 1)
|
|
|
|
goto CopyA;
|
|
|
|
/* nb==0 is impossible now if the comparison
|
|
|
|
* function is consistent, but we can't assume
|
|
|
|
* that it is.
|
|
|
|
*/
|
|
|
|
if (nb == 0)
|
|
|
|
goto Succeed;
|
|
|
|
}
|
|
|
|
*dest-- = *pa--;
|
|
|
|
--na;
|
|
|
|
if (na == 0)
|
|
|
|
goto Succeed;
|
|
|
|
} while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
|
2002-08-10 02:21:15 -03:00
|
|
|
++min_gallop; /* penalize it for leaving galloping mode */
|
|
|
|
ms->min_gallop = min_gallop;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
|
|
|
Succeed:
|
|
|
|
result = 0;
|
|
|
|
Fail:
|
|
|
|
if (nb)
|
|
|
|
memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
|
|
|
|
return result;
|
|
|
|
CopyA:
|
|
|
|
assert(nb == 1 && na > 0);
|
|
|
|
/* The first element of pb belongs at the front of the merge. */
|
|
|
|
dest -= na;
|
|
|
|
pa -= na;
|
|
|
|
memmove(dest+1, pa+1, na * sizeof(PyObject *));
|
|
|
|
*dest = *pb;
|
|
|
|
return 0;
|
|
|
|
}
|
1998-05-13 18:20:49 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Merge the two runs at stack indices i and i+1.
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_at(MergeState *ms, int i)
|
|
|
|
{
|
|
|
|
PyObject **pa, **pb;
|
|
|
|
int na, nb;
|
|
|
|
int k;
|
|
|
|
PyObject *compare;
|
|
|
|
|
|
|
|
assert(ms != NULL);
|
|
|
|
assert(ms->n >= 2);
|
|
|
|
assert(i >= 0);
|
|
|
|
assert(i == ms->n - 2 || i == ms->n - 3);
|
|
|
|
|
2002-08-10 02:21:15 -03:00
|
|
|
pa = ms->pending[i].base;
|
|
|
|
na = ms->pending[i].len;
|
|
|
|
pb = ms->pending[i+1].base;
|
|
|
|
nb = ms->pending[i+1].len;
|
2002-07-31 23:13:36 -03:00
|
|
|
assert(na > 0 && nb > 0);
|
|
|
|
assert(pa + na == pb);
|
|
|
|
|
|
|
|
/* Record the length of the combined runs; if i is the 3rd-last
|
|
|
|
* run now, also slide over the last run (which isn't involved
|
|
|
|
* in this merge). The current run i+1 goes away in any case.
|
|
|
|
*/
|
2002-08-10 02:21:15 -03:00
|
|
|
ms->pending[i].len = na + nb;
|
|
|
|
if (i == ms->n - 3)
|
|
|
|
ms->pending[i+1] = ms->pending[i+2];
|
2002-07-31 23:13:36 -03:00
|
|
|
--ms->n;
|
1998-05-13 18:20:49 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Where does b start in a? Elements in a before that can be
|
|
|
|
* ignored (already in place).
|
|
|
|
*/
|
|
|
|
compare = ms->compare;
|
|
|
|
k = gallop_right(*pb, pa, na, 0, compare);
|
|
|
|
if (k < 0)
|
|
|
|
return -1;
|
|
|
|
pa += k;
|
|
|
|
na -= k;
|
|
|
|
if (na == 0)
|
|
|
|
return 0;
|
1998-05-26 12:06:32 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Where does a end in b? Elements in b after that can be
|
|
|
|
* ignored (already in place).
|
|
|
|
*/
|
|
|
|
nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
|
|
|
|
if (nb <= 0)
|
|
|
|
return nb;
|
|
|
|
|
|
|
|
/* Merge what remains of the runs, using a temp array with
|
|
|
|
* min(na, nb) elements.
|
|
|
|
*/
|
|
|
|
if (na <= nb)
|
|
|
|
return merge_lo(ms, pa, na, pb, nb);
|
|
|
|
else
|
|
|
|
return merge_hi(ms, pa, na, pb, nb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Examine the stack of runs waiting to be merged, merging adjacent runs
|
|
|
|
* until the stack invariants are re-established:
|
|
|
|
*
|
|
|
|
* 1. len[-3] > len[-2] + len[-1]
|
|
|
|
* 2. len[-2] > len[-1]
|
|
|
|
*
|
|
|
|
* See listsort.txt for more info.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_collapse(MergeState *ms)
|
|
|
|
{
|
2002-08-10 02:21:15 -03:00
|
|
|
struct s_slice *p = ms->pending;
|
2002-07-31 23:13:36 -03:00
|
|
|
|
|
|
|
assert(ms);
|
|
|
|
while (ms->n > 1) {
|
|
|
|
int n = ms->n - 2;
|
2002-08-10 02:21:15 -03:00
|
|
|
if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
|
|
|
|
if (p[n-1].len < p[n+1].len)
|
2002-07-31 23:13:36 -03:00
|
|
|
--n;
|
|
|
|
if (merge_at(ms, n) < 0)
|
|
|
|
return -1;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
2002-08-10 02:21:15 -03:00
|
|
|
else if (p[n].len <= p[n+1].len) {
|
2002-07-31 23:13:36 -03:00
|
|
|
if (merge_at(ms, n) < 0)
|
|
|
|
return -1;
|
1998-06-16 12:18:28 -03:00
|
|
|
}
|
2002-07-31 23:13:36 -03:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1997-12-10 11:14:24 -04:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Regardless of invariants, merge all runs on the stack until only one
|
|
|
|
* remains. This is used at the end of the mergesort.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_force_collapse(MergeState *ms)
|
|
|
|
{
|
2002-08-10 02:21:15 -03:00
|
|
|
struct s_slice *p = ms->pending;
|
2002-07-31 23:13:36 -03:00
|
|
|
|
|
|
|
assert(ms);
|
|
|
|
while (ms->n > 1) {
|
|
|
|
int n = ms->n - 2;
|
2002-08-10 02:21:15 -03:00
|
|
|
if (n > 0 && p[n-1].len < p[n+1].len)
|
2002-07-31 23:13:36 -03:00
|
|
|
--n;
|
|
|
|
if (merge_at(ms, n) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
1996-12-10 19:55:39 -04:00
|
|
|
return 0;
|
2002-07-31 23:13:36 -03:00
|
|
|
}
|
1998-05-29 14:56:32 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Compute a good value for the minimum run length; natural runs shorter
|
|
|
|
* than this are boosted artificially via binary insertion.
|
|
|
|
*
|
|
|
|
* If n < 64, return n (it's too small to bother with fancy stuff).
|
|
|
|
* Else if n is an exact power of 2, return 32.
|
|
|
|
* Else return an int k, 32 <= k <= 64, such that n/k is close to, but
|
|
|
|
* strictly less than, an exact power of 2.
|
|
|
|
*
|
|
|
|
* See listsort.txt for more info.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
merge_compute_minrun(int n)
|
|
|
|
{
|
|
|
|
int r = 0; /* becomes 1 if any 1 bits are shifted off */
|
|
|
|
|
|
|
|
assert(n >= 0);
|
|
|
|
while (n >= 64) {
|
|
|
|
r |= n & 1;
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
return n + r;
|
1998-06-16 12:18:28 -03:00
|
|
|
}
|
1998-05-29 14:56:32 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* An adaptive, stable, natural mergesort. See listsort.txt.
|
|
|
|
* Returns Py_None on success, NULL on error. Even in case of error, the
|
|
|
|
* list will be some permutation of its input state (nothing is lost or
|
|
|
|
* duplicated).
|
|
|
|
*/
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
2000-07-09 12:16:51 -03:00
|
|
|
listsort(PyListObject *self, PyObject *args)
|
1996-12-10 19:55:39 -04:00
|
|
|
{
|
2002-07-31 23:13:36 -03:00
|
|
|
MergeState ms;
|
|
|
|
PyObject **lo, **hi;
|
|
|
|
int nremaining;
|
|
|
|
int minrun;
|
2002-11-12 18:08:10 -04:00
|
|
|
int saved_ob_size;
|
|
|
|
PyObject **saved_ob_item;
|
|
|
|
PyObject **empty_ob_item;
|
2002-07-31 23:13:36 -03:00
|
|
|
PyObject *compare = NULL;
|
|
|
|
PyObject *result = NULL; /* guilty until proved innocent */
|
1998-06-16 12:18:28 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
assert(self != NULL);
|
2000-02-24 11:23:03 -04:00
|
|
|
if (args != NULL) {
|
2002-12-29 12:33:45 -04:00
|
|
|
if (!PyArg_UnpackTuple(args, "sort", 0, 1, &compare))
|
2000-02-24 11:23:03 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-01-02 16:51:08 -04:00
|
|
|
if (compare == Py_None)
|
|
|
|
compare = NULL;
|
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
merge_init(&ms, compare);
|
2002-07-19 04:05:44 -03:00
|
|
|
|
2002-11-12 18:08:10 -04:00
|
|
|
/* The list is temporarily made empty, so that mutations performed
|
|
|
|
* by comparison functions can't affect the slice of memory we're
|
|
|
|
* sorting (allowing mutations during sorting is a core-dump
|
|
|
|
* factory, since ob_item may change).
|
|
|
|
*/
|
|
|
|
saved_ob_size = self->ob_size;
|
|
|
|
saved_ob_item = self->ob_item;
|
|
|
|
self->ob_size = 0;
|
|
|
|
self->ob_item = empty_ob_item = PyMem_NEW(PyObject *, 0);
|
2002-07-19 04:05:44 -03:00
|
|
|
|
2002-11-12 18:08:10 -04:00
|
|
|
nremaining = saved_ob_size;
|
2002-07-31 23:13:36 -03:00
|
|
|
if (nremaining < 2)
|
|
|
|
goto succeed;
|
|
|
|
|
|
|
|
/* March over the array once, left to right, finding natural runs,
|
|
|
|
* and extending short natural runs to minrun elements.
|
|
|
|
*/
|
2002-11-12 18:08:10 -04:00
|
|
|
lo = saved_ob_item;
|
2002-07-31 23:13:36 -03:00
|
|
|
hi = lo + nremaining;
|
|
|
|
minrun = merge_compute_minrun(nremaining);
|
|
|
|
do {
|
|
|
|
int descending;
|
|
|
|
int n;
|
2002-07-19 04:05:44 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
/* Identify next run. */
|
|
|
|
n = count_run(lo, hi, compare, &descending);
|
|
|
|
if (n < 0)
|
|
|
|
goto fail;
|
|
|
|
if (descending)
|
|
|
|
reverse_slice(lo, lo + n);
|
|
|
|
/* If short, extend to min(minrun, nremaining). */
|
|
|
|
if (n < minrun) {
|
|
|
|
const int force = nremaining <= minrun ?
|
|
|
|
nremaining : minrun;
|
|
|
|
if (binarysort(lo, lo + force, lo + n, compare) < 0)
|
|
|
|
goto fail;
|
|
|
|
n = force;
|
|
|
|
}
|
|
|
|
/* Push run onto pending-runs stack, and maybe merge. */
|
|
|
|
assert(ms.n < MAX_MERGE_PENDING);
|
2002-08-10 02:21:15 -03:00
|
|
|
ms.pending[ms.n].base = lo;
|
|
|
|
ms.pending[ms.n].len = n;
|
2002-07-31 23:13:36 -03:00
|
|
|
++ms.n;
|
|
|
|
if (merge_collapse(&ms) < 0)
|
|
|
|
goto fail;
|
|
|
|
/* Advance to find next run. */
|
|
|
|
lo += n;
|
|
|
|
nremaining -= n;
|
|
|
|
} while (nremaining);
|
|
|
|
assert(lo == hi);
|
|
|
|
|
|
|
|
if (merge_force_collapse(&ms) < 0)
|
|
|
|
goto fail;
|
|
|
|
assert(ms.n == 1);
|
2002-11-12 18:08:10 -04:00
|
|
|
assert(ms.pending[0].base == saved_ob_item);
|
|
|
|
assert(ms.pending[0].len == saved_ob_size);
|
2002-07-19 04:05:44 -03:00
|
|
|
|
2002-07-31 23:13:36 -03:00
|
|
|
succeed:
|
|
|
|
result = Py_None;
|
2002-07-19 04:05:44 -03:00
|
|
|
fail:
|
2002-11-12 18:08:10 -04:00
|
|
|
if (self->ob_item != empty_ob_item || self->ob_size) {
|
|
|
|
/* The user mucked with the list during the sort. */
|
|
|
|
(void)list_ass_slice(self, 0, self->ob_size, (PyObject *)NULL);
|
|
|
|
if (result != NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"list modified during sort");
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (self->ob_item == empty_ob_item)
|
|
|
|
PyMem_FREE(empty_ob_item);
|
|
|
|
self->ob_size = saved_ob_size;
|
|
|
|
self->ob_item = saved_ob_item;
|
2002-07-31 23:13:36 -03:00
|
|
|
merge_freemem(&ms);
|
|
|
|
Py_XINCREF(result);
|
|
|
|
return result;
|
1996-12-10 19:55:39 -04:00
|
|
|
}
|
2002-07-19 04:05:44 -03:00
|
|
|
#undef IFLT
|
2002-08-04 14:47:26 -03:00
|
|
|
#undef ISLT
|
2002-07-19 04:05:44 -03:00
|
|
|
|
1998-06-16 12:18:28 -03:00
|
|
|
int
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Sort(PyObject *v)
|
1998-06-16 12:18:28 -03:00
|
|
|
{
|
|
|
|
if (v == NULL || !PyList_Check(v)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v = listsort((PyListObject *)v, (PyObject *)NULL);
|
|
|
|
if (v == NULL)
|
|
|
|
return -1;
|
|
|
|
Py_DECREF(v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-12 18:06:02 -04:00
|
|
|
static PyObject *
|
2001-08-16 10:15:00 -03:00
|
|
|
listreverse(PyListObject *self)
|
2001-02-12 18:06:02 -04:00
|
|
|
{
|
2002-07-19 01:04:16 -03:00
|
|
|
if (self->ob_size > 1)
|
|
|
|
reverse_slice(self->ob_item, self->ob_item + self->ob_size);
|
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
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_Reverse(PyObject *v)
|
1995-01-17 12:34:45 -04:00
|
|
|
{
|
2002-08-07 22:06:39 -03:00
|
|
|
PyListObject *self = (PyListObject *)v;
|
|
|
|
|
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;
|
|
|
|
}
|
2002-08-07 22:06:39 -03:00
|
|
|
if (self->ob_size > 1)
|
|
|
|
reverse_slice(self->ob_item, self->ob_item + self->ob_size);
|
1995-01-17 12:34:45 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
2000-07-09 12:16:51 -03:00
|
|
|
PyList_AsTuple(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;
|
2000-07-25 09:56:38 -03:00
|
|
|
memcpy((void *)p,
|
|
|
|
(void *)((PyListObject *)v)->ob_item,
|
1997-05-02 00:12:38 -03:00
|
|
|
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 *
|
2003-06-17 02:05:49 -03:00
|
|
|
listindex(PyListObject *self, PyObject *args)
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
2003-06-17 02:05:49 -03:00
|
|
|
int i, start=0, stop=self->ob_size;
|
|
|
|
PyObject *v;
|
2000-02-24 11:23:03 -04:00
|
|
|
|
2003-06-17 16:27:39 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
|
|
|
|
_PyEval_SliceIndex, &start,
|
|
|
|
_PyEval_SliceIndex, &stop))
|
2003-06-17 02:05:49 -03:00
|
|
|
return NULL;
|
2003-06-17 11:25:14 -03:00
|
|
|
if (start < 0) {
|
|
|
|
start += self->ob_size;
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
if (stop < 0) {
|
|
|
|
stop += self->ob_size;
|
|
|
|
if (stop < 0)
|
|
|
|
stop = 0;
|
|
|
|
}
|
|
|
|
else if (stop > self->ob_size)
|
|
|
|
stop = self->ob_size;
|
2003-06-17 02:05:49 -03:00
|
|
|
for (i = start; i < stop; i++) {
|
2001-01-17 18:11:59 -04:00
|
|
|
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
|
|
|
|
if (cmp > 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInt_FromLong((long)i);
|
2001-01-17 18:11:59 -04:00
|
|
|
else if (cmp < 0)
|
1997-05-22 21:06:51 -03:00
|
|
|
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 *
|
2001-08-16 10:15:00 -03:00
|
|
|
listcount(PyListObject *self, PyObject *v)
|
1991-10-20 17:20:40 -03:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
int i;
|
2000-02-24 11:23:03 -04:00
|
|
|
|
1991-10-20 17:20:40 -03:00
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
2001-01-17 18:11:59 -04:00
|
|
|
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
|
|
|
|
if (cmp > 0)
|
1991-10-20 17:20:40 -03:00
|
|
|
count++;
|
2001-01-17 18:11:59 -04:00
|
|
|
else if (cmp < 0)
|
1997-05-22 21:06:51 -03:00
|
|
|
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 *
|
2001-08-16 10:15:00 -03:00
|
|
|
listremove(PyListObject *self, PyObject *v)
|
1991-03-06 09:07:53 -04:00
|
|
|
{
|
|
|
|
int i;
|
2000-02-24 11:23:03 -04:00
|
|
|
|
1991-03-06 09:07:53 -04:00
|
|
|
for (i = 0; i < self->ob_size; i++) {
|
2001-01-17 18:11:59 -04:00
|
|
|
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
|
|
|
|
if (cmp > 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
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
|
|
|
}
|
2001-01-17 18:11:59 -04:00
|
|
|
else if (cmp < 0)
|
1997-05-22 21:06:51 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-06-23 11:18:11 -03:00
|
|
|
static int
|
|
|
|
list_traverse(PyListObject *o, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
int i, err;
|
|
|
|
PyObject *x;
|
|
|
|
|
|
|
|
for (i = o->ob_size; --i >= 0; ) {
|
|
|
|
x = o->ob_item[i];
|
|
|
|
if (x != NULL) {
|
|
|
|
err = visit(x, arg);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
list_clear(PyListObject *lp)
|
|
|
|
{
|
|
|
|
(void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-17 18:11:59 -04:00
|
|
|
static PyObject *
|
|
|
|
list_richcompare(PyObject *v, PyObject *w, int op)
|
|
|
|
{
|
|
|
|
PyListObject *vl, *wl;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!PyList_Check(v) || !PyList_Check(w)) {
|
|
|
|
Py_INCREF(Py_NotImplemented);
|
|
|
|
return Py_NotImplemented;
|
|
|
|
}
|
|
|
|
|
|
|
|
vl = (PyListObject *)v;
|
|
|
|
wl = (PyListObject *)w;
|
|
|
|
|
|
|
|
if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
|
|
|
|
/* Shortcut: if the lengths differ, the lists differ */
|
|
|
|
PyObject *res;
|
|
|
|
if (op == Py_EQ)
|
|
|
|
res = Py_False;
|
|
|
|
else
|
|
|
|
res = Py_True;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for the first index where items are different */
|
|
|
|
for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
|
|
|
|
int k = PyObject_RichCompareBool(vl->ob_item[i],
|
|
|
|
wl->ob_item[i], Py_EQ);
|
|
|
|
if (k < 0)
|
|
|
|
return NULL;
|
|
|
|
if (!k)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= vl->ob_size || i >= wl->ob_size) {
|
|
|
|
/* No more items to compare -- compare sizes */
|
|
|
|
int vs = vl->ob_size;
|
|
|
|
int ws = wl->ob_size;
|
|
|
|
int cmp;
|
|
|
|
PyObject *res;
|
|
|
|
switch (op) {
|
|
|
|
case Py_LT: cmp = vs < ws; break;
|
2001-07-06 14:45:43 -03:00
|
|
|
case Py_LE: cmp = vs <= ws; break;
|
2001-01-17 18:11:59 -04:00
|
|
|
case Py_EQ: cmp = vs == ws; break;
|
|
|
|
case Py_NE: cmp = vs != ws; break;
|
|
|
|
case Py_GT: cmp = vs > ws; break;
|
|
|
|
case Py_GE: cmp = vs >= ws; break;
|
|
|
|
default: return NULL; /* cannot happen */
|
|
|
|
}
|
|
|
|
if (cmp)
|
|
|
|
res = Py_True;
|
|
|
|
else
|
|
|
|
res = Py_False;
|
|
|
|
Py_INCREF(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have an item that differs -- shortcuts for EQ/NE */
|
|
|
|
if (op == Py_EQ) {
|
|
|
|
Py_INCREF(Py_False);
|
|
|
|
return Py_False;
|
|
|
|
}
|
|
|
|
if (op == Py_NE) {
|
|
|
|
Py_INCREF(Py_True);
|
|
|
|
return Py_True;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare the final item again using the proper operator */
|
|
|
|
return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
|
|
|
|
}
|
|
|
|
|
2001-08-02 01:15:00 -03:00
|
|
|
/* Adapted from newer code by Tim */
|
|
|
|
static int
|
|
|
|
list_fill(PyListObject *result, PyObject *v)
|
|
|
|
{
|
|
|
|
PyObject *it; /* iter(v) */
|
|
|
|
int n; /* guess for result list size */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
n = result->ob_size;
|
|
|
|
|
|
|
|
/* Special-case list(a_list), for speed. */
|
|
|
|
if (PyList_Check(v)) {
|
|
|
|
if (v == (PyObject *)result)
|
|
|
|
return 0; /* source is destination, we're done */
|
|
|
|
return list_ass_slice(result, 0, n, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Empty previous contents */
|
|
|
|
if (n != 0) {
|
|
|
|
if (list_ass_slice(result, 0, n, (PyObject *)NULL) != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get iterator. There may be some low-level efficiency to be gained
|
|
|
|
* by caching the tp_iternext slot instead of using PyIter_Next()
|
|
|
|
* later, but premature optimization is the root etc.
|
|
|
|
*/
|
|
|
|
it = PyObject_GetIter(v);
|
|
|
|
if (it == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Guess a result list size. */
|
|
|
|
n = -1; /* unknown */
|
|
|
|
if (PySequence_Check(v) &&
|
|
|
|
v->ob_type->tp_as_sequence->sq_length) {
|
|
|
|
n = PySequence_Size(v);
|
|
|
|
if (n < 0)
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
if (n < 0)
|
|
|
|
n = 8; /* arbitrary */
|
|
|
|
NRESIZE(result->ob_item, PyObject*, n);
|
2002-05-22 20:19:17 -03:00
|
|
|
if (result->ob_item == NULL) {
|
|
|
|
PyErr_NoMemory();
|
2001-08-02 01:15:00 -03:00
|
|
|
goto error;
|
2002-05-22 20:19:17 -03:00
|
|
|
}
|
2002-06-13 18:11:11 -03:00
|
|
|
memset(result->ob_item, 0, sizeof(*result->ob_item) * n);
|
2001-08-02 01:15:00 -03:00
|
|
|
result->ob_size = n;
|
|
|
|
|
|
|
|
/* Run iterator to exhaustion. */
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
PyObject *item = PyIter_Next(it);
|
|
|
|
if (item == NULL) {
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i < n)
|
|
|
|
PyList_SET_ITEM(result, i, item); /* steals ref */
|
|
|
|
else {
|
|
|
|
int status = ins1(result, result->ob_size, item);
|
|
|
|
Py_DECREF(item); /* append creates a new ref */
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cut back result list if initial guess was too large. */
|
|
|
|
if (i < n && result != NULL) {
|
|
|
|
if (list_ass_slice(result, i, n, (PyObject *)NULL) != 0)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(it);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(it);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
list_init(PyListObject *self, PyObject *args, PyObject *kw)
|
|
|
|
{
|
|
|
|
PyObject *arg = NULL;
|
|
|
|
static char *kwlist[] = {"sequence", 0};
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
|
|
|
|
return -1;
|
|
|
|
if (arg != NULL)
|
|
|
|
return list_fill(self, arg);
|
|
|
|
if (self->ob_size > 0)
|
|
|
|
return list_ass_slice(self, 0, self->ob_size, (PyObject*)NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-03 12:32:18 -04:00
|
|
|
static long
|
|
|
|
list_nohash(PyObject *self)
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(append_doc,
|
|
|
|
"L.append(object) -- append object to end");
|
|
|
|
PyDoc_STRVAR(extend_doc,
|
2002-12-29 01:49:09 -04:00
|
|
|
"L.extend(iterable) -- extend list by appending elements from the iterable");
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(insert_doc,
|
|
|
|
"L.insert(index, object) -- insert object before index");
|
|
|
|
PyDoc_STRVAR(pop_doc,
|
|
|
|
"L.pop([index]) -> item -- remove and return item at index (default last)");
|
|
|
|
PyDoc_STRVAR(remove_doc,
|
|
|
|
"L.remove(value) -- remove first occurrence of value");
|
|
|
|
PyDoc_STRVAR(index_doc,
|
2003-06-17 02:05:49 -03:00
|
|
|
"L.index(value, [start, [stop]]) -> integer -- return first index of value");
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(count_doc,
|
|
|
|
"L.count(value) -> integer -- return number of occurrences of value");
|
|
|
|
PyDoc_STRVAR(reverse_doc,
|
|
|
|
"L.reverse() -- reverse *IN PLACE*");
|
|
|
|
PyDoc_STRVAR(sort_doc,
|
2003-01-02 16:51:08 -04:00
|
|
|
"L.sort(cmpfunc=None) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1");
|
1998-06-30 12:36:32 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyMethodDef list_methods[] = {
|
2001-08-16 10:15:00 -03:00
|
|
|
{"append", (PyCFunction)listappend, METH_O, append_doc},
|
2001-01-17 18:11:59 -04:00
|
|
|
{"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
|
2001-08-16 10:15:00 -03:00
|
|
|
{"extend", (PyCFunction)listextend, METH_O, extend_doc},
|
2001-01-17 18:11:59 -04:00
|
|
|
{"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
|
2001-08-16 10:15:00 -03:00
|
|
|
{"remove", (PyCFunction)listremove, METH_O, remove_doc},
|
2003-06-17 02:05:49 -03:00
|
|
|
{"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
|
2001-08-16 10:15:00 -03:00
|
|
|
{"count", (PyCFunction)listcount, METH_O, count_doc},
|
|
|
|
{"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
|
2001-01-17 18:11:59 -04:00
|
|
|
{"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
|
2002-07-31 23:13:36 -03:00
|
|
|
{NULL, NULL} /* sentinel */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PySequenceMethods list_as_sequence = {
|
2001-01-17 18:11:59 -04: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 */
|
|
|
|
(objobjproc)list_contains, /* sq_contains */
|
|
|
|
(binaryfunc)list_inplace_concat, /* sq_inplace_concat */
|
|
|
|
(intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
2002-06-13 23:04:18 -03:00
|
|
|
PyDoc_STRVAR(list_doc,
|
2001-08-02 01:15:00 -03:00
|
|
|
"list() -> new list\n"
|
2002-06-13 23:04:18 -03:00
|
|
|
"list(sequence) -> new list initialized from sequence's items");
|
2001-08-02 01:15:00 -03:00
|
|
|
|
2002-07-13 00:51:17 -03:00
|
|
|
static PyObject *list_iter(PyObject *seq);
|
2002-05-31 18:40:38 -03:00
|
|
|
|
2002-07-13 00:51:17 -03:00
|
|
|
static PyObject *
|
2002-06-11 07:55:12 -03:00
|
|
|
list_subscript(PyListObject* self, PyObject* item)
|
|
|
|
{
|
|
|
|
if (PyInt_Check(item)) {
|
|
|
|
long i = PyInt_AS_LONG(item);
|
|
|
|
if (i < 0)
|
|
|
|
i += PyList_GET_SIZE(self);
|
|
|
|
return list_item(self, i);
|
|
|
|
}
|
|
|
|
else if (PyLong_Check(item)) {
|
|
|
|
long i = PyLong_AsLong(item);
|
|
|
|
if (i == -1 && PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
if (i < 0)
|
|
|
|
i += PyList_GET_SIZE(self);
|
|
|
|
return list_item(self, i);
|
|
|
|
}
|
|
|
|
else if (PySlice_Check(item)) {
|
|
|
|
int start, stop, step, slicelength, cur, i;
|
|
|
|
PyObject* result;
|
|
|
|
PyObject* it;
|
|
|
|
|
|
|
|
if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
|
|
|
|
&start, &stop, &step, &slicelength) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slicelength <= 0) {
|
|
|
|
return PyList_New(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PyList_New(slicelength);
|
|
|
|
if (!result) return NULL;
|
|
|
|
|
2002-07-18 23:35:45 -03:00
|
|
|
for (cur = start, i = 0; i < slicelength;
|
2002-06-11 07:55:12 -03:00
|
|
|
cur += step, i++) {
|
|
|
|
it = PyList_GET_ITEM(self, cur);
|
|
|
|
Py_INCREF(it);
|
|
|
|
PyList_SET_ITEM(result, i, it);
|
|
|
|
}
|
2002-07-18 23:35:45 -03:00
|
|
|
|
2002-06-11 07:55:12 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"list indices must be integers");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-18 23:35:45 -03:00
|
|
|
static int
|
2002-06-11 07:55:12 -03:00
|
|
|
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
|
|
|
|
{
|
|
|
|
if (PyInt_Check(item)) {
|
|
|
|
long i = PyInt_AS_LONG(item);
|
|
|
|
if (i < 0)
|
|
|
|
i += PyList_GET_SIZE(self);
|
|
|
|
return list_ass_item(self, i, value);
|
|
|
|
}
|
|
|
|
else if (PyLong_Check(item)) {
|
|
|
|
long i = PyLong_AsLong(item);
|
|
|
|
if (i == -1 && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
if (i < 0)
|
|
|
|
i += PyList_GET_SIZE(self);
|
|
|
|
return list_ass_item(self, i, value);
|
|
|
|
}
|
|
|
|
else if (PySlice_Check(item)) {
|
|
|
|
int start, stop, step, slicelength;
|
|
|
|
|
|
|
|
if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size,
|
|
|
|
&start, &stop, &step, &slicelength) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-06-19 12:44:15 -03:00
|
|
|
/* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */
|
|
|
|
if (step == 1 && ((PySliceObject*)item)->step == Py_None)
|
|
|
|
return list_ass_slice(self, start, stop, value);
|
|
|
|
|
2002-06-11 07:55:12 -03:00
|
|
|
if (value == NULL) {
|
|
|
|
/* delete slice */
|
2002-06-19 12:44:15 -03:00
|
|
|
PyObject **garbage, **it;
|
2002-06-11 07:55:12 -03:00
|
|
|
int cur, i, j;
|
2002-07-18 23:35:45 -03:00
|
|
|
|
2002-06-11 07:55:12 -03:00
|
|
|
if (slicelength <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (step < 0) {
|
|
|
|
stop = start + 1;
|
|
|
|
start = stop + step*(slicelength - 1) - 1;
|
|
|
|
step = -step;
|
|
|
|
}
|
|
|
|
|
|
|
|
garbage = (PyObject**)
|
|
|
|
PyMem_MALLOC(slicelength*sizeof(PyObject*));
|
2002-07-18 23:35:45 -03:00
|
|
|
|
|
|
|
/* drawing pictures might help
|
2002-06-11 07:55:12 -03:00
|
|
|
understand these for loops */
|
2002-06-11 09:22:28 -03:00
|
|
|
for (cur = start, i = 0;
|
|
|
|
cur < stop;
|
2002-07-29 11:35:04 -03:00
|
|
|
cur += step, i++) {
|
|
|
|
int lim = step;
|
|
|
|
|
2002-06-11 07:55:12 -03:00
|
|
|
garbage[i] = PyList_GET_ITEM(self, cur);
|
|
|
|
|
2002-07-29 11:35:04 -03:00
|
|
|
if (cur + step >= self->ob_size) {
|
|
|
|
lim = self->ob_size - cur - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < lim; j++) {
|
2002-07-18 23:35:45 -03:00
|
|
|
PyList_SET_ITEM(self, cur + j - i,
|
2002-06-11 09:22:28 -03:00
|
|
|
PyList_GET_ITEM(self,
|
|
|
|
cur + j + 1));
|
2002-06-11 07:55:12 -03:00
|
|
|
}
|
|
|
|
}
|
2002-06-19 12:44:15 -03:00
|
|
|
for (cur = start + slicelength*step + 1;
|
2002-06-11 07:55:12 -03:00
|
|
|
cur < self->ob_size; cur++) {
|
|
|
|
PyList_SET_ITEM(self, cur - slicelength,
|
|
|
|
PyList_GET_ITEM(self, cur));
|
|
|
|
}
|
|
|
|
self->ob_size -= slicelength;
|
2002-06-19 12:44:15 -03:00
|
|
|
it = self->ob_item;
|
|
|
|
NRESIZE(it, PyObject*, self->ob_size);
|
|
|
|
self->ob_item = it;
|
2002-06-11 07:55:12 -03:00
|
|
|
|
|
|
|
for (i = 0; i < slicelength; i++) {
|
|
|
|
Py_DECREF(garbage[i]);
|
|
|
|
}
|
|
|
|
PyMem_FREE(garbage);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* assign slice */
|
2002-12-05 17:32:32 -04:00
|
|
|
PyObject **garbage, *ins, *seq;
|
2002-06-11 07:55:12 -03:00
|
|
|
int cur, i;
|
|
|
|
|
2002-12-05 17:32:32 -04:00
|
|
|
/* protect against a[::-1] = a */
|
|
|
|
if (self == (PyListObject*)value) {
|
|
|
|
seq = list_slice((PyListObject*)value, 0,
|
|
|
|
PyList_GET_SIZE(value));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char msg[256];
|
|
|
|
PyOS_snprintf(msg, sizeof(msg),
|
|
|
|
"must assign sequence (not \"%.200s\") to extended slice",
|
|
|
|
value->ob_type->tp_name);
|
|
|
|
seq = PySequence_Fast(value, msg);
|
|
|
|
if (!seq)
|
|
|
|
return -1;
|
2002-06-11 07:55:12 -03:00
|
|
|
}
|
|
|
|
|
2002-12-05 17:32:32 -04:00
|
|
|
if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
|
2002-06-11 07:55:12 -03:00
|
|
|
PyErr_Format(PyExc_ValueError,
|
2002-12-05 17:32:32 -04:00
|
|
|
"attempt to assign sequence of size %d to extended slice of size %d",
|
|
|
|
PySequence_Fast_GET_SIZE(seq),
|
|
|
|
slicelength);
|
|
|
|
Py_DECREF(seq);
|
2002-06-11 07:55:12 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-12-05 17:32:32 -04:00
|
|
|
if (!slicelength) {
|
|
|
|
Py_DECREF(seq);
|
2002-06-11 07:55:12 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
garbage = (PyObject**)
|
|
|
|
PyMem_MALLOC(slicelength*sizeof(PyObject*));
|
2002-07-18 23:35:45 -03:00
|
|
|
|
|
|
|
for (cur = start, i = 0; i < slicelength;
|
2002-06-11 07:55:12 -03:00
|
|
|
cur += step, i++) {
|
|
|
|
garbage[i] = PyList_GET_ITEM(self, cur);
|
2002-07-18 23:35:45 -03:00
|
|
|
|
2002-12-05 17:32:32 -04:00
|
|
|
ins = PySequence_Fast_GET_ITEM(seq, i);
|
2002-06-11 07:55:12 -03:00
|
|
|
Py_INCREF(ins);
|
|
|
|
PyList_SET_ITEM(self, cur, ins);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < slicelength; i++) {
|
|
|
|
Py_DECREF(garbage[i]);
|
|
|
|
}
|
2002-07-18 23:35:45 -03:00
|
|
|
|
2002-06-11 07:55:12 -03:00
|
|
|
PyMem_FREE(garbage);
|
2002-12-05 17:32:32 -04:00
|
|
|
Py_DECREF(seq);
|
2002-07-18 23:35:45 -03:00
|
|
|
|
2002-06-11 07:55:12 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2002-07-18 23:35:45 -03:00
|
|
|
}
|
2002-06-11 07:55:12 -03:00
|
|
|
else {
|
2002-07-18 23:35:45 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
2002-06-11 07:55:12 -03:00
|
|
|
"list indices must be integers");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMappingMethods list_as_mapping = {
|
|
|
|
(inquiry)list_length,
|
|
|
|
(binaryfunc)list_subscript,
|
|
|
|
(objobjargproc)list_ass_subscript
|
|
|
|
};
|
|
|
|
|
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",
|
2001-08-29 20:54:21 -03:00
|
|
|
sizeof(PyListObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
2001-01-17 18:11:59 -04:00
|
|
|
(destructor)list_dealloc, /* tp_dealloc */
|
|
|
|
(printfunc)list_print, /* tp_print */
|
2001-08-02 01:15:00 -03:00
|
|
|
0, /* tp_getattr */
|
2001-01-17 18:11:59 -04:00
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
(reprfunc)list_repr, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
&list_as_sequence, /* tp_as_sequence */
|
2002-06-11 07:55:12 -03:00
|
|
|
&list_as_mapping, /* tp_as_mapping */
|
2001-12-03 12:32:18 -04:00
|
|
|
list_nohash, /* tp_hash */
|
2001-01-17 18:11:59 -04:00
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
2001-08-02 01:15:00 -03:00
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
2001-01-17 18:11:59 -04:00
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2001-08-29 20:54:21 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
2001-08-02 01:15:00 -03:00
|
|
|
Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
|
|
list_doc, /* tp_doc */
|
2001-01-17 18:11:59 -04:00
|
|
|
(traverseproc)list_traverse, /* tp_traverse */
|
|
|
|
(inquiry)list_clear, /* tp_clear */
|
|
|
|
list_richcompare, /* tp_richcompare */
|
2001-08-02 01:15:00 -03:00
|
|
|
0, /* tp_weaklistoffset */
|
2002-05-31 18:40:38 -03:00
|
|
|
list_iter, /* tp_iter */
|
2001-08-02 01:15:00 -03:00
|
|
|
0, /* tp_iternext */
|
|
|
|
list_methods, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
(initproc)list_init, /* tp_init */
|
|
|
|
PyType_GenericAlloc, /* tp_alloc */
|
|
|
|
PyType_GenericNew, /* tp_new */
|
2002-07-16 17:10:23 -03:00
|
|
|
PyObject_GC_Del, /* tp_free */
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
1998-06-16 12:18:28 -03:00
|
|
|
|
|
|
|
|
2002-05-31 18:40:38 -03:00
|
|
|
/*********************** List Iterator **************************/
|
|
|
|
|
|
|
|
typedef struct {
|
2002-07-16 17:10:23 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
long it_index;
|
2002-07-16 17:07:32 -03:00
|
|
|
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
|
2002-05-31 18:40:38 -03:00
|
|
|
} listiterobject;
|
|
|
|
|
|
|
|
PyTypeObject PyListIter_Type;
|
|
|
|
|
2002-07-16 12:56:52 -03:00
|
|
|
static PyObject *
|
2002-05-31 18:40:38 -03:00
|
|
|
list_iter(PyObject *seq)
|
|
|
|
{
|
2002-07-16 17:10:23 -03:00
|
|
|
listiterobject *it;
|
|
|
|
|
|
|
|
if (!PyList_Check(seq)) {
|
|
|
|
PyErr_BadInternalCall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
it = PyObject_GC_New(listiterobject, &PyListIter_Type);
|
|
|
|
if (it == NULL)
|
|
|
|
return NULL;
|
|
|
|
it->it_index = 0;
|
|
|
|
Py_INCREF(seq);
|
|
|
|
it->it_seq = (PyListObject *)seq;
|
|
|
|
_PyObject_GC_TRACK(it);
|
|
|
|
return (PyObject *)it;
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
listiter_dealloc(listiterobject *it)
|
|
|
|
{
|
2002-07-16 17:10:23 -03:00
|
|
|
_PyObject_GC_UNTRACK(it);
|
2002-07-16 17:07:32 -03:00
|
|
|
Py_XDECREF(it->it_seq);
|
2002-07-16 17:10:23 -03:00
|
|
|
PyObject_GC_Del(it);
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
listiter_traverse(listiterobject *it, visitproc visit, void *arg)
|
|
|
|
{
|
2002-07-16 17:07:32 -03:00
|
|
|
if (it->it_seq == NULL)
|
|
|
|
return 0;
|
2002-07-16 17:10:23 -03:00
|
|
|
return visit((PyObject *)it->it_seq, arg);
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2002-06-01 02:22:55 -03:00
|
|
|
listiter_next(listiterobject *it)
|
2002-05-31 18:40:38 -03:00
|
|
|
{
|
2002-07-16 17:10:23 -03:00
|
|
|
PyListObject *seq;
|
|
|
|
PyObject *item;
|
2002-05-31 18:40:38 -03:00
|
|
|
|
2002-06-01 02:22:55 -03:00
|
|
|
assert(it != NULL);
|
2002-07-16 17:10:23 -03:00
|
|
|
seq = it->it_seq;
|
2002-07-16 17:07:32 -03:00
|
|
|
if (seq == NULL)
|
|
|
|
return NULL;
|
2002-07-16 17:10:23 -03:00
|
|
|
assert(PyList_Check(seq));
|
2002-05-31 18:40:38 -03:00
|
|
|
|
2002-07-16 17:10:23 -03:00
|
|
|
if (it->it_index < PyList_GET_SIZE(seq)) {
|
2002-06-01 02:22:55 -03:00
|
|
|
item = PyList_GET_ITEM(seq, it->it_index);
|
|
|
|
++it->it_index;
|
2002-07-16 17:10:23 -03:00
|
|
|
Py_INCREF(item);
|
|
|
|
return item;
|
|
|
|
}
|
2002-07-16 17:07:32 -03:00
|
|
|
|
|
|
|
Py_DECREF(seq);
|
|
|
|
it->it_seq = NULL;
|
2002-07-16 17:10:23 -03:00
|
|
|
return NULL;
|
2002-05-31 18:40:38 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
PyTypeObject PyListIter_Type = {
|
2002-07-16 17:10:23 -03:00
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
|
|
|
0, /* ob_size */
|
|
|
|
"listiterator", /* tp_name */
|
|
|
|
sizeof(listiterobject), /* tp_basicsize */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
/* methods */
|
|
|
|
(destructor)listiter_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
PyObject_GenericGetAttr, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
(traverseproc)listiter_traverse, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
2003-03-17 15:46:11 -04:00
|
|
|
PyObject_SelfIter, /* tp_iter */
|
2002-07-16 17:10:23 -03:00
|
|
|
(iternextfunc)listiter_next, /* tp_iternext */
|
2002-07-16 17:07:32 -03:00
|
|
|
0, /* tp_methods */
|
2002-07-16 17:10:23 -03:00
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
|
|
|
0, /* tp_base */
|
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
2002-05-31 18:40:38 -03:00
|
|
|
};
|