annotate Objects/listobject.c @ 17322:bd110eadc19a legacy-trunk

One-liner critical fix from Jim Fulton: Eric's string-method crusade got the order backwards in a line (for .find()).
author Tim Peters <tim.peters@gmail.com>
date Thu, 05 Apr 2001 22:26:23 +0000
parents c0cdf82599e0
children fc1680fc9d23 ed8b79c6cb4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* List object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
4 #include "Python.h"
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
5
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
6 #ifdef STDC_HEADERS
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
7 #include <stddef.h>
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
8 #else
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
9 #include <sys/types.h> /* For size_t */
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
10 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
12 #define ROUNDUP(n, PyTryBlock) \
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
13 ((((n)+(PyTryBlock)-1)/(PyTryBlock))*(PyTryBlock))
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
14
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
15 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
16 roundupsize(int n)
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
17 {
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
18 if (n < 500)
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
19 return ROUNDUP(n, 10);
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
20 else
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
21 return ROUNDUP(n, 100);
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
22 }
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
23
6154
2ee9811c92fd Rename roundup() to roundupsize(), as there's a macro roundup() in the
Guido van Rossum <guido@python.org>
parents: 5784
diff changeset
24 #define NRESIZE(var, type, nitems) PyMem_RESIZE(var, type, roundupsize(nitems))
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
25
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
26 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
27 PyList_New(int size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
30 PyListObject *op;
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
31 size_t nbytes;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 if (size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
33 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
36 nbytes = size * sizeof(PyObject *);
978
13c99eaf32a8 * renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
37 /* Check for overflow */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
38 if (nbytes / sizeof(PyObject *) != (size_t)size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
39 return PyErr_NoMemory();
978
13c99eaf32a8 * renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
40 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
41 /* PyObject_NewVar is inlined */
12993
8f79c5b69e0a part 2 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12990
diff changeset
42 op = (PyListObject *) PyObject_MALLOC(sizeof(PyListObject)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
43 + PyGC_HEAD_SIZE);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 if (op == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
45 return PyErr_NoMemory();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 }
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
47 op = (PyListObject *) PyObject_FROM_GC(op);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 if (size <= 0) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 op->ob_item = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 else {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
52 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 if (op->ob_item == NULL) {
13611
88b4f3b508da Fix in PyList_New(). With GC enabled and when out of memory,
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13576
diff changeset
54 PyObject_FREE(PyObject_AS_GC(op));
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
55 return PyErr_NoMemory();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
58 PyObject_INIT_VAR(op, &PyList_Type, size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 for (i = 0; i < size; i++)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 op->ob_item[i] = NULL;
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
61 PyObject_GC_Init(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
62 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
66 PyList_Size(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
68 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
69 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
73 return ((PyListObject *)op) -> ob_size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
76 static PyObject *indexerr;
3985
011cfe7490eb Use pre-created string objects for most common exceptions
Guido van Rossum <guido@python.org>
parents: 2827
diff changeset
77
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
78 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
79 PyList_GetItem(PyObject *op, int i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
81 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
82 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
85 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
3985
011cfe7490eb Use pre-created string objects for most common exceptions
Guido van Rossum <guido@python.org>
parents: 2827
diff changeset
86 if (indexerr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
87 indexerr = PyString_FromString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
88 "list index out of range");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
89 PyErr_SetObject(PyExc_IndexError, indexerr);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
92 return ((PyListObject *)op) -> ob_item[i];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
96 PyList_SetItem(register PyObject *op, register int i,
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
97 register PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
99 register PyObject *olditem;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
100 register PyObject **p;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
101 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
102 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
103 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
104 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
106 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
107 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
108 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
109 "list assignment index out of range");
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
110 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
112 p = ((PyListObject *)op) -> ob_item + i;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
113 olditem = *p;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
114 *p = newitem;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
115 Py_XDECREF(olditem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
120 ins1(PyListObject *self, int where, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
123 PyObject **items;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
124 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
125 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
126 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
127 }
13957
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
128 if (self->ob_size == INT_MAX) {
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
129 PyErr_SetString(PyExc_OverflowError,
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
130 "cannot add more objects to list");
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
131 return -1;
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
132 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 items = self->ob_item;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
134 NRESIZE(items, PyObject *, self->ob_size+1);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
135 if (items == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
136 PyErr_NoMemory();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
137 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
138 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if (where < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 where = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 if (where > self->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 where = self->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 for (i = self->ob_size; --i >= where; )
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 items[i+1] = items[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
145 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 items[where] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 self->ob_item = items;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 self->ob_size++;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
153 PyList_Insert(PyObject *op, int where, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
155 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
156 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
157 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
158 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
159 return ins1((PyListObject *)op, where, newitem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
163 PyList_Append(PyObject *op, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
165 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
166 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
167 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
168 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
169 return ins1((PyListObject *)op,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
170 (int) ((PyListObject *)op)->ob_size, newitem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 static void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
176 list_dealloc(PyListObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 int i;
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11996
diff changeset
179 Py_TRASHCAN_SAFE_BEGIN(op)
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
180 PyObject_GC_Fini(op);
2188
6461ff0c4bee Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents: 2159
diff changeset
181 if (op->ob_item != NULL) {
10945
bf6a0983951d When deallocating a list, DECREF the items from the end back to the start.
Guido van Rossum <guido@python.org>
parents: 10620
diff changeset
182 /* Do it backwards, for Christian Tismer.
bf6a0983951d When deallocating a list, DECREF the items from the end back to the start.
Guido van Rossum <guido@python.org>
parents: 10620
diff changeset
183 There's a simple test case where somehow this reduces
bf6a0983951d When deallocating a list, DECREF the items from the end back to the start.
Guido van Rossum <guido@python.org>
parents: 10620
diff changeset
184 thrashing when a *very* large list is created and
bf6a0983951d When deallocating a list, DECREF the items from the end back to the start.
Guido van Rossum <guido@python.org>
parents: 10620
diff changeset
185 immediately deleted. */
bf6a0983951d When deallocating a list, DECREF the items from the end back to the start.
Guido van Rossum <guido@python.org>
parents: 10620
diff changeset
186 i = op->ob_size;
bf6a0983951d When deallocating a list, DECREF the items from the end back to the start.
Guido van Rossum <guido@python.org>
parents: 10620
diff changeset
187 while (--i >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
188 Py_XDECREF(op->ob_item[i]);
2188
6461ff0c4bee Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents: 2159
diff changeset
189 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
190 PyMem_FREE(op->ob_item);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 }
13265
e8bb0d536633 Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
192 op = (PyListObject *) PyObject_AS_GC(op);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
193 PyObject_DEL(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11996
diff changeset
194 Py_TRASHCAN_SAFE_END(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
197 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
198 list_print(PyListObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 int i;
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
201
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
202 i = Py_ReprEnter((PyObject*)op);
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
203 if (i != 0) {
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
204 if (i < 0)
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
205 return i;
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
206 fprintf(fp, "[...]");
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
207 return 0;
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
208 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 fprintf(fp, "[");
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
210 for (i = 0; i < op->ob_size; i++) {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
211 if (i > 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 fprintf(fp, ", ");
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
213 if (PyObject_Print(op->ob_item[i], fp, 0) != 0) {
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
214 Py_ReprLeave((PyObject *)op);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
215 return -1;
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
216 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 fprintf(fp, "]");
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
219 Py_ReprLeave((PyObject *)op);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
220 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
223 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
224 list_repr(PyListObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
226 PyObject *s, *comma;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 int i;
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
228
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
229 i = Py_ReprEnter((PyObject*)v);
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
230 if (i != 0) {
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
231 if (i > 0)
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
232 return PyString_FromString("[...]");
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
233 return NULL;
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
234 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
235 s = PyString_FromString("[");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
236 comma = PyString_FromString(", ");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 for (i = 0; i < v->ob_size && s != NULL; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 if (i > 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
239 PyString_Concat(&s, comma);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
240 PyString_ConcatAndDel(&s, PyObject_Repr(v->ob_item[i]));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
242 Py_XDECREF(comma);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
243 PyString_ConcatAndDel(&s, PyString_FromString("]"));
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
244 Py_ReprLeave((PyObject *)v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 return s;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
249 list_length(PyListObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
254
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
255
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
256 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
257 list_contains(PyListObject *a, PyObject *el)
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
258 {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
259 int i;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
260
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
261 for (i = 0; i < a->ob_size; ++i) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
262 int cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
263 Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
264 if (cmp > 0)
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
265 return 1;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
266 else if (cmp < 0)
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
267 return -1;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
268 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
269 return 0;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
270 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
271
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
272
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
273 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
274 list_item(PyListObject *a, int i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 if (i < 0 || i >= a->ob_size) {
3985
011cfe7490eb Use pre-created string objects for most common exceptions
Guido van Rossum <guido@python.org>
parents: 2827
diff changeset
277 if (indexerr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
278 indexerr = PyString_FromString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
279 "list index out of range");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
280 PyErr_SetObject(PyExc_IndexError, indexerr);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
283 Py_INCREF(a->ob_item[i]);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 return a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
287 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
288 list_slice(PyListObject *a, int ilow, int ihigh)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
290 PyListObject *np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 else if (ilow > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 ilow = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 else if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 ihigh = a->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
300 np = (PyListObject *) PyList_New(ihigh - ilow);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 if (np == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 for (i = ilow; i < ihigh; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
304 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
305 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 np->ob_item[i - ilow] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
308 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
311 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
312 PyList_GetSlice(PyObject *a, int ilow, int ihigh)
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
313 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
314 if (!PyList_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
315 PyErr_BadInternalCall();
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
316 return NULL;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
317 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
318 return list_slice((PyListObject *)a, ilow, ihigh);
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
319 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
320
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
321 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
322 list_concat(PyListObject *a, PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
326 PyListObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
327 if (!PyList_Check(bb)) {
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
328 PyErr_Format(PyExc_TypeError,
12817
5f1f8ddc2a0c Improve TypeError exception message for list catenation.
Fred Drake <fdrake@acm.org>
parents: 12816
diff changeset
329 "can only concatenate list (not \"%.200s\") to list",
5f1f8ddc2a0c Improve TypeError exception message for list catenation.
Fred Drake <fdrake@acm.org>
parents: 12816
diff changeset
330 bb->ob_type->tp_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
333 #define b ((PyListObject *)bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 size = a->ob_size + b->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
335 np = (PyListObject *) PyList_New(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 if (np == NULL) {
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
337 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 for (i = 0; i < a->ob_size; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
340 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
341 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 np->ob_item[i] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 for (i = 0; i < b->ob_size; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
345 PyObject *v = b->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
346 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 np->ob_item[i + a->ob_size] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
349 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
353 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
354 list_repeat(PyListObject *a, int n)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
355 {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
356 int i, j;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
357 int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
358 PyListObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
359 PyObject **p;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
360 if (n < 0)
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
361 n = 0;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
362 size = a->ob_size * n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
363 np = (PyListObject *) PyList_New(size);
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
364 if (np == NULL)
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
365 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
366 p = np->ob_item;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
367 for (i = 0; i < n; i++) {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
368 for (j = 0; j < a->ob_size; j++) {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
369 *p = a->ob_item[j];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
370 Py_INCREF(*p);
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
371 p++;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
372 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
373 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
374 return (PyObject *) np;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
375 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
376
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
378 list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 {
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
380 /* Because [X]DECREF can recursively invoke list operations on
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
381 this list, we must postpone all [X]DECREF activity until
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
382 after the list is back in its canonical shape. Therefore
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
383 we must allocate an additional array, 'recycle', into which
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
384 we temporarily copy the items that are deleted from the
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
385 list. :-( */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
386 PyObject **recycle, **p;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
387 PyObject **item;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 int n; /* Size of replacement list */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 int d; /* Change in size */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 int k; /* Loop index */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
391 #define b ((PyListObject *)v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 if (v == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 n = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
394 else if (PyList_Check(v)) {
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 n = b->ob_size;
511
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
396 if (a == b) {
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
397 /* Special case "a[i:j] = a" -- copy b first */
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
398 int ret;
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
399 v = list_slice(b, 0, n);
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
400 ret = list_ass_slice(a, ilow, ihigh, v);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
401 Py_DECREF(v);
511
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
402 return ret;
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
403 }
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
404 }
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
405 else {
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
406 PyErr_Format(PyExc_TypeError,
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
407 "must assign list (not \"%.200s\") to slice",
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
408 v->ob_type->tp_name);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
409 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
410 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 else if (ilow > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 ilow = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 else if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 ihigh = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 item = a->ob_item;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 d = n - (ihigh-ilow);
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
421 if (ihigh > ilow)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
422 p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow));
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
423 else
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
424 p = recycle = NULL;
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
425 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 for (k = ilow; k < ihigh; k++)
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
427 *p++ = item[k];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 if (d < 0) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 for (/*k = ihigh*/; k < a->ob_size; k++)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 item[k+d] = item[k];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 a->ob_size += d;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
432 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 a->ob_item = item;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 }
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
436 else { /* Insert d items; recycle ihigh-ilow items */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
437 NRESIZE(item, PyObject *, a->ob_size + d);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
438 if (item == NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
439 if (recycle != NULL)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
440 PyMem_DEL(recycle);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
441 PyErr_NoMemory();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
442 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
443 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 for (k = a->ob_size; --k >= ihigh; )
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 item[k+d] = item[k];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 for (/*k = ihigh-1*/; k >= ilow; --k)
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
447 *p++ = item[k];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 a->ob_item = item;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 a->ob_size += d;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 for (k = 0; k < n; k++, ilow++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
452 PyObject *w = b->ob_item[k];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
453 Py_XINCREF(w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 item[ilow] = w;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 }
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
456 if (recycle) {
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
457 while (--p >= recycle)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
458 Py_XDECREF(*p);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
459 PyMem_DEL(recycle);
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
460 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
464
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
465 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
466 PyList_SetSlice(PyObject *a, int ilow, int ihigh, PyObject *v)
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
467 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
468 if (!PyList_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
469 PyErr_BadInternalCall();
1330
409ea74feb21 Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
470 return -1;
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
471 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
472 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
473 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
474
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
475 static PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
476 list_inplace_repeat(PyListObject *self, int n)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
477 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
478 PyObject **items;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
479 int size, i, j;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
480
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
481
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
482 size = PyList_GET_SIZE(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
483 if (size == 0) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
484 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
485 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
486 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
487
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
488 items = self->ob_item;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
489
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
490 if (n < 1) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
491 self->ob_item = NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
492 self->ob_size = 0;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
493 for (i = 0; i < size; i++)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
494 Py_XDECREF(items[i]);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
495 PyMem_DEL(items);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
496 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
497 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
498 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
499
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
500 NRESIZE(items, PyObject*, size*n);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
501 if (items == NULL) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
502 PyErr_NoMemory();
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
503 goto finally;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
504 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
505 self->ob_item = items;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
506 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
507 for (j = 0; j < size; j++) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
508 PyObject *o = PyList_GET_ITEM(self, j);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
509 Py_INCREF(o);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
510 PyList_SET_ITEM(self, self->ob_size++, o);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
511 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
512 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
513 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
514 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
515 finally:
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
516 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
517 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
518
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
519 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
520 list_ass_item(PyListObject *a, int i, PyObject *v)
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
521 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
522 PyObject *old_value;
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
523 if (i < 0 || i >= a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
524 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
525 "list assignment index out of range");
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
526 return -1;
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
527 }
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
528 if (v == NULL)
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
529 return list_ass_slice(a, i, i+1, v);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
530 Py_INCREF(v);
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1836
diff changeset
531 old_value = a->ob_item[i];
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
532 a->ob_item[i] = v;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
533 Py_DECREF(old_value);
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
534 return 0;
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
535 }
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
536
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
537 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
538 ins(PyListObject *self, int where, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 if (ins1(self, where, v) != 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
542 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
543 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
546 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
547 listinsert(PyListObject *self, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
550 PyObject *v;
11848
6f9799a145e5 OKOK, Greg's right, I should've used the :name option in the argument
Guido van Rossum <guido@python.org>
parents: 11846
diff changeset
551 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 return NULL;
1211
406860dfaac7 * stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
553 return ins(self, i, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555
11996
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
556 /* Define NO_STRICT_LIST_APPEND to enable multi-argument append() */
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
557
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
558 #ifndef NO_STRICT_LIST_APPEND
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
559 #define PyArg_ParseTuple_Compat1 PyArg_ParseTuple
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
560 #else
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
561 #define PyArg_ParseTuple_Compat1(args, format, ret) \
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
562 ( \
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
563 PyTuple_GET_SIZE(args) > 1 ? (*ret = args, 1) : \
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
564 PyTuple_GET_SIZE(args) == 1 ? (*ret = PyTuple_GET_ITEM(args, 0), 1) : \
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
565 PyArg_ParseTuple(args, format, ret) \
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
566 )
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
567 #endif
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
568
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
569
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
570 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
571 listappend(PyListObject *self, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
573 PyObject *v;
11996
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
574 if (!PyArg_ParseTuple_Compat1(args, "O:append", &v))
1211
406860dfaac7 * stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
575 return NULL;
406860dfaac7 * stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
576 return ins(self, (int) self->ob_size, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
579 static int
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
580 listextend_internal(PyListObject *self, PyObject *b)
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
581 {
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
582 PyObject **items;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
583 int selflen = PyList_GET_SIZE(self);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
584 int blen;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
585 register int i;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
586
15707
3075a153a083 fix leak
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15558
diff changeset
587 if (PyObject_Size(b) == 0) {
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
588 /* short circuit when b is empty */
15707
3075a153a083 fix leak
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15558
diff changeset
589 Py_DECREF(b);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
590 return 0;
15707
3075a153a083 fix leak
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15558
diff changeset
591 }
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
592
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
593 if (self == (PyListObject*)b) {
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
594 /* as in list_ass_slice() we must special case the
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
595 * situation: a.extend(a)
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
596 *
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
597 * XXX: I think this way ought to be faster than using
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
598 * list_slice() the way list_ass_slice() does.
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
599 */
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
600 Py_DECREF(b);
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
601 b = PyList_New(selflen);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
602 if (!b)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
603 return -1;
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
604 for (i = 0; i < selflen; i++) {
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
605 PyObject *o = PyList_GET_ITEM(self, i);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
606 Py_INCREF(o);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
607 PyList_SET_ITEM(b, i, o);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
608 }
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
609 }
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
610
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13472
diff changeset
611 blen = PyObject_Size(b);
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
612
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
613 /* resize a using idiom */
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
614 items = self->ob_item;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
615 NRESIZE(items, PyObject*, selflen + blen);
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
616 if (items == NULL) {
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
617 PyErr_NoMemory();
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
618 Py_DECREF(b);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
619 return -1;
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
620 }
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
621
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
622 self->ob_item = items;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
623
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
624 /* populate the end of self with b's items */
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
625 for (i = 0; i < blen; i++) {
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
626 PyObject *o = PySequence_Fast_GET_ITEM(b, i);
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
627 Py_INCREF(o);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
628 PyList_SET_ITEM(self, self->ob_size++, o);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
629 }
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
630 Py_DECREF(b);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
631 return 0;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
632 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
633
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
634
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
635 static PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
636 list_inplace_concat(PyListObject *self, PyObject *other)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
637 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
638 other = PySequence_Fast(other, "argument to += must be a sequence");
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
639 if (!other)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
640 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
641
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
642 if (listextend_internal(self, other) < 0)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
643 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
644
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
645 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
646 return (PyObject *)self;
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
647 }
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
648
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
649 static PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
650 listextend(PyListObject *self, PyObject *args)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
651 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
652
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
653 PyObject *b;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
654
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
655 if (!PyArg_ParseTuple(args, "O:extend", &b))
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
656 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
657
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
658 b = PySequence_Fast(b, "list.extend() argument must be a sequence");
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
659 if (!b)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
660 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
661
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
662 if (listextend_internal(self, b) < 0)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
663 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
664
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
665 Py_INCREF(Py_None);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
666 return Py_None;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
667 }
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
668
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
669 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
670 listpop(PyListObject *self, PyObject *args)
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
671 {
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
672 int i = -1;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
673 PyObject *v;
11848
6f9799a145e5 OKOK, Greg's right, I should've used the :name option in the argument
Guido van Rossum <guido@python.org>
parents: 11846
diff changeset
674 if (!PyArg_ParseTuple(args, "|i:pop", &i))
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
675 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
676 if (self->ob_size == 0) {
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
677 /* Special-case most common failure cause */
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
678 PyErr_SetString(PyExc_IndexError, "pop from empty list");
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
679 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
680 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
681 if (i < 0)
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
682 i += self->ob_size;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
683 if (i < 0 || i >= self->ob_size) {
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
684 PyErr_SetString(PyExc_IndexError, "pop index out of range");
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
685 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
686 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
687 v = self->ob_item[i];
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
688 Py_INCREF(v);
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
689 if (list_ass_slice(self, i, i+1, (PyObject *)NULL) != 0) {
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
690 Py_DECREF(v);
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
691 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
692 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
693 return v;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
694 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
695
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
696 /* New quicksort implementation for arrays of object pointers.
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
697 Thanks to discussions with Tim Peters. */
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
698
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
699 /* CMPERROR is returned by our comparison function when an error
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
700 occurred. This is the largest negative integer (0x80000000 on a
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
701 32-bit system). */
5171
b1fc4eb2ac5a Add casts to CMPERROR macro to silence SunPro compiler warnings about
Guido van Rossum <guido@python.org>
parents: 4732
diff changeset
702 #define CMPERROR ( (int) ((unsigned int)1 << (8*sizeof(int) - 1)) )
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
703
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
704 /* Comparison function. Takes care of calling a user-supplied
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
705 comparison function (any callable Python object). Calls the
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
706 standard comparison function, PyObject_Compare(), if the user-
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
707 supplied function is NULL. */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
708
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
709 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
710 docompare(PyObject *x, PyObject *y, PyObject *compare)
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
711 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
712 PyObject *args, *res;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
713 int i;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
714
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
715 if (compare == NULL) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
716 /* NOTE: we rely on the fact here that the sorting algorithm
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
717 only ever checks whether k<0, i.e., whether x<y. So we
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
718 invoke the rich comparison function with Py_LT ('<'), and
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
719 return -1 when it returns true and 0 when it returns
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
720 false. */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
721 i = PyObject_RichCompareBool(x, y, Py_LT);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
722 if (i < 0)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
723 return CMPERROR;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
724 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
725 return -i;
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
726 }
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
727
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
728 args = Py_BuildValue("(OO)", x, y);
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
729 if (args == NULL)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
730 return CMPERROR;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
731 res = PyEval_CallObject(compare, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
732 Py_DECREF(args);
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
733 if (res == NULL)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
734 return CMPERROR;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
735 if (!PyInt_Check(res)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
736 Py_DECREF(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
737 PyErr_SetString(PyExc_TypeError,
10620
891d089aba41 Improve text of some error messages, as suggested by Sean Reifschneider.
Guido van Rossum <guido@python.org>
parents: 9884
diff changeset
738 "comparison function must return int");
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
739 return CMPERROR;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
740 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
741 i = PyInt_AsLong(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
742 Py_DECREF(res);
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
743 if (i < 0)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
744 return -1;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
745 if (i > 0)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
746 return 1;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
747 return 0;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
748 }
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
749
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
750 /* MINSIZE is the smallest array that will get a full-blown samplesort
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
751 treatment; smaller arrays are sorted using binary insertion. It must
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
752 be at least 7 for the samplesort implementation to work. Binary
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
753 insertion does fewer compares, but can suffer O(N**2) data movement.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
754 The more expensive compares, the larger MINSIZE should be. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
755 #define MINSIZE 100
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
756
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
757 /* MINPARTITIONSIZE is the smallest array slice samplesort will bother to
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
758 partition; smaller slices are passed to binarysort. It must be at
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
759 least 2, and no larger than MINSIZE. Setting it higher reduces the #
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
760 of compares slowly, but increases the amount of data movement quickly.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
761 The value here was chosen assuming a compare costs ~25x more than
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
762 swapping a pair of memory-resident pointers -- but under that assumption,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
763 changing the value by a few dozen more or less has aggregate effect
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
764 under 1%. So the value is crucial, but not touchy <wink>. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
765 #define MINPARTITIONSIZE 40
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
766
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
767 /* MAXMERGE is the largest number of elements we'll always merge into
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
768 a known-to-be sorted chunk via binary insertion, regardless of the
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
769 size of that chunk. Given a chunk of N sorted elements, and a group
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
770 of K unknowns, the largest K for which it's better to do insertion
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
771 (than a full-blown sort) is a complicated function of N and K mostly
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
772 involving the expected number of compares and data moves under each
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
773 approach, and the relative cost of those operations on a specific
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
774 architecure. The fixed value here is conservative, and should be a
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
775 clear win regardless of architecture or N. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
776 #define MAXMERGE 15
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
777
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
778 /* STACKSIZE is the size of our work stack. A rough estimate is that
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
779 this allows us to sort arrays of size N where
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
780 N / ln(N) = MINPARTITIONSIZE * 2**STACKSIZE, so 60 is more than enough
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
781 for arrays of size 2**64. Because we push the biggest partition
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
782 first, the worst case occurs when all subarrays are always partitioned
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
783 exactly in two. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
784 #define STACKSIZE 60
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
785
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
786
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
787 #define SETK(X,Y) if ((k = docompare(X,Y,compare))==CMPERROR) goto fail
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
788
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
789 /* binarysort is the best method for sorting small arrays: it does
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
790 few compares, but can do data movement quadratic in the number of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
791 elements.
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
792 [lo, hi) is a contiguous slice of a list, and is sorted via
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
793 binary insertion.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
794 On entry, must have lo <= start <= hi, and that [lo, start) is already
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
795 sorted (pass start == lo if you don't know!).
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
796 If docompare complains (returns CMPERROR) return -1, else 0.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
797 Even in case of error, the output slice will be some permutation of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
798 the input (nothing is lost or duplicated).
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
799 */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
800
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
801 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
802 binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
803 /* compare -- comparison function object, or NULL for default */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
804 {
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
805 /* assert lo <= start <= hi
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
806 assert [lo, start) is sorted */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
807 register int k;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
808 register PyObject **l, **p, **r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
809 register PyObject *pivot;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
810
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
811 if (lo == start)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
812 ++start;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
813 for (; start < hi; ++start) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
814 /* set l to where *start belongs */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
815 l = lo;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
816 r = start;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
817 pivot = *r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
818 do {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
819 p = l + ((r - l) >> 1);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
820 SETK(pivot, *p);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
821 if (k < 0)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
822 r = p;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
823 else
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
824 l = p + 1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
825 } while (l < r);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
826 /* Pivot should go at l -- slide over to make room.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
827 Caution: using memmove is much slower under MSVC 5;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
828 we're not usually moving many slots. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
829 for (p = start; p > l; --p)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
830 *p = *(p-1);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
831 *l = pivot;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
832 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
833 return 0;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
834
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
835 fail:
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
836 return -1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
837 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
838
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
839 /* samplesortslice is the sorting workhorse.
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
840 [lo, hi) is a contiguous slice of a list, to be sorted in place.
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
841 On entry, must have lo <= hi,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
842 If docompare complains (returns CMPERROR) return -1, else 0.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
843 Even in case of error, the output slice will be some permutation of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
844 the input (nothing is lost or duplicated).
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
845
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
846 samplesort is basically quicksort on steroids: a power of 2 close
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
847 to n/ln(n) is computed, and that many elements (less 1) are picked at
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
848 random from the array and sorted. These 2**k - 1 elements are then
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
849 used as preselected pivots for an equal number of quicksort
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
850 partitioning steps, partitioning the slice into 2**k chunks each of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
851 size about ln(n). These small final chunks are then usually handled
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
852 by binarysort. Note that when k=1, this is roughly the same as an
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
853 ordinary quicksort using a random pivot, and when k=2 this is roughly
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
854 a median-of-3 quicksort. From that view, using k ~= lg(n/ln(n)) makes
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
855 this a "median of n/ln(n)" quicksort. You can also view it as a kind
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
856 of bucket sort, where 2**k-1 bucket boundaries are picked dynamically.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
857
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
858 The large number of samples makes a quadratic-time case almost
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
859 impossible, and asymptotically drives the average-case number of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
860 compares from quicksort's 2 N ln N (or 12/7 N ln N for the median-of-
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
861 3 variant) down to N lg N.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
862
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
863 We also play lots of low-level tricks to cut the number of compares.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
864
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
865 Very obscure: To avoid using extra memory, the PPs are stored in the
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
866 array and shuffled around as partitioning proceeds. At the start of a
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
867 partitioning step, we'll have 2**m-1 (for some m) PPs in sorted order,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
868 adjacent (either on the left or the right!) to a chunk of X elements
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
869 that are to be partitioned: P X or X P. In either case we need to
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
870 shuffle things *in place* so that the 2**(m-1) smaller PPs are on the
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
871 left, followed by the PP to be used for this step (that's the middle
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
872 of the PPs), followed by X, followed by the 2**(m-1) larger PPs:
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
873 P X or X P -> Psmall pivot X Plarge
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
874 and the order of the PPs must not be altered. It can take a while
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
875 to realize this isn't trivial! It can take even longer <wink> to
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
876 understand why the simple code below works, using only 2**(m-1) swaps.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
877 The key is that the order of the X elements isn't necessarily
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
878 preserved: X can end up as some cyclic permutation of its original
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
879 order. That's OK, because X is unsorted anyway. If the order of X
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
880 had to be preserved too, the simplest method I know of using O(1)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
881 scratch storage requires len(X) + 2**(m-1) swaps, spread over 2 passes.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
882 Since len(X) is typically several times larger than 2**(m-1), that
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
883 would slow things down.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
884 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
885
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
886 struct SamplesortStackNode {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
887 /* Represents a slice of the array, from (& including) lo up
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
888 to (but excluding) hi. "extra" additional & adjacent elements
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
889 are pre-selected pivots (PPs), spanning [lo-extra, lo) if
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
890 extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
891 already sorted, but nothing is known about the other elements
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
892 in [lo, hi). |extra| is always one less than a power of 2.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
893 When extra is 0, we're out of PPs, and the slice must be
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
894 sorted by some other means. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
895 PyObject **lo;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
896 PyObject **hi;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
897 int extra;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
898 };
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
899
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
900 /* The number of PPs we want is 2**k - 1, where 2**k is as close to
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
901 N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
902 is undesirable, so cutoff values are canned in the "cutoff" table
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
903 below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
904 #define CUTOFFBASE 4
9884
14dcbb34b9dd Jim Ahlstrom patch: cutoff should be a long for machines with 16-bit
Guido van Rossum <guido@python.org>
parents: 9317
diff changeset
905 static long cutoff[] = {
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
906 43, /* smallest N such that k == 4 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
907 106, /* etc */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
908 250,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
909 576,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
910 1298,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
911 2885,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
912 6339,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
913 13805,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
914 29843,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
915 64116,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
916 137030,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
917 291554,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
918 617916,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
919 1305130,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
920 2748295,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
921 5771662,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
922 12091672,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
923 25276798,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
924 52734615,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
925 109820537,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
926 228324027,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
927 473977813,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
928 982548444, /* smallest N such that k == 26 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
929 2034159050 /* largest N that fits in signed 32-bit; k == 27 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
930 };
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
931
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
932 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
933 samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
934 /* compare -- comparison function object, or NULL for default */
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
935 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
936 register PyObject **l, **r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
937 register PyObject *tmp, *pivot;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
938 register int k;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
939 int n, extra, top, extraOnRight;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
940 struct SamplesortStackNode stack[STACKSIZE];
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
941
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
942 /* assert lo <= hi */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
943 n = hi - lo;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
944
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
945 /* ----------------------------------------------------------
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
946 * Special cases
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
947 * --------------------------------------------------------*/
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
948 if (n < 2)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
949 return 0;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
950
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
951 /* Set r to the largest value such that [lo,r) is sorted.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
952 This catches the already-sorted case, the all-the-same
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
953 case, and the appended-a-few-elements-to-a-sorted-list case.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
954 If the array is unsorted, we're very likely to get out of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
955 the loop fast, so the test is cheap if it doesn't pay off.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
956 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
957 /* assert lo < hi */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
958 for (r = lo+1; r < hi; ++r) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
959 SETK(*r, *(r-1));
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
960 if (k < 0)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
961 break;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
962 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
963 /* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
964 few unknowns, or few elements in total. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
965 if (hi - r <= MAXMERGE || n < MINSIZE)
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
966 return binarysort(lo, hi, r, compare);
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
967
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
968 /* Check for the array already being reverse-sorted. Typical
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
969 benchmark-driven silliness <wink>. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
970 /* assert lo < hi */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
971 for (r = lo+1; r < hi; ++r) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
972 SETK(*(r-1), *r);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
973 if (k < 0)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
974 break;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
975 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
976 if (hi - r <= MAXMERGE) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
977 /* Reverse the reversed prefix, then insert the tail */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
978 PyObject **originalr = r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
979 l = lo;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
980 do {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
981 --r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
982 tmp = *l; *l = *r; *r = tmp;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
983 ++l;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
984 } while (l < r);
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
985 return binarysort(lo, hi, originalr, compare);
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
986 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
987
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
988 /* ----------------------------------------------------------
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
989 * Normal case setup: a large array without obvious pattern.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
990 * --------------------------------------------------------*/
8457
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
991
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
992 /* extra := a power of 2 ~= n/ln(n), less 1.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
993 First find the smallest extra s.t. n < cutoff[extra] */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
994 for (extra = 0;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
995 extra < sizeof(cutoff) / sizeof(cutoff[0]);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
996 ++extra) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
997 if (n < cutoff[extra])
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
998 break;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
999 /* note that if we fall out of the loop, the value of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1000 extra still makes *sense*, but may be smaller than
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1001 we would like (but the array has more than ~= 2**31
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1002 elements in this case!) */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1003 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1004 /* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1005 have is CUTOFFBASE-1, so
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1006 assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1007 extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1008 /* assert extra > 0 and n >= extra */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1009
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1010 /* Swap that many values to the start of the array. The
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1011 selection of elements is pseudo-random, but the same on
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1012 every run (this is intentional! timing algorithm changes is
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1013 a pain if timing varies across runs). */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1014 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1015 unsigned int seed = n / extra; /* arbitrary */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1016 unsigned int i;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1017 for (i = 0; i < (unsigned)extra; ++i) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1018 /* j := random int in [i, n) */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1019 unsigned int j;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1020 seed = seed * 69069 + 7;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1021 j = i + seed % (n - i);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1022 tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1023 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1024 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1025
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1026 /* Recursively sort the preselected pivots. */
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1027 if (samplesortslice(lo, lo + extra, compare) < 0)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1028 goto fail;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1029
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1030 top = 0; /* index of available stack slot */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1031 lo += extra; /* point to first unknown */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1032 extraOnRight = 0; /* the PPs are at the left end */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1033
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1034 /* ----------------------------------------------------------
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1035 * Partition [lo, hi), and repeat until out of work.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1036 * --------------------------------------------------------*/
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1037 for (;;) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1038 /* assert lo <= hi, so n >= 0 */
4690
c2b831f923c2 Some more tuning of quicksort: use pointers instead of indexing.
Guido van Rossum <guido@python.org>
parents: 4673
diff changeset
1039 n = hi - lo;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1040
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1041 /* We may not want, or may not be able, to partition:
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1042 If n is small, it's quicker to insert.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1043 If extra is 0, we're out of pivots, and *must* use
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1044 another method.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1045 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1046 if (n < MINPARTITIONSIZE || extra == 0) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1047 if (n >= MINSIZE) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1048 /* assert extra == 0
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1049 This is rare, since the average size
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1050 of a final block is only about
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1051 ln(original n). */
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1052 if (samplesortslice(lo, hi, compare) < 0)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1053 goto fail;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1054 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1055 else {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1056 /* Binary insertion should be quicker,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1057 and we can take advantage of the PPs
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1058 already being sorted. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1059 if (extraOnRight && extra) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1060 /* swap the PPs to the left end */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1061 k = extra;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1062 do {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1063 tmp = *lo;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1064 *lo = *hi;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1065 *hi = tmp;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1066 ++lo; ++hi;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1067 } while (--k);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1068 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1069 if (binarysort(lo - extra, hi, lo,
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1070 compare) < 0)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1071 goto fail;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1072 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1073
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1074 /* Find another slice to work on. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1075 if (--top < 0)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1076 break; /* no more -- done! */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1077 lo = stack[top].lo;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1078 hi = stack[top].hi;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1079 extra = stack[top].extra;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1080 extraOnRight = 0;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1081 if (extra < 0) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1082 extraOnRight = 1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1083 extra = -extra;
8428
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1084 }
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1085 continue;
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1086 }
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1087
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1088 /* Pretend the PPs are indexed 0, 1, ..., extra-1.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1089 Then our preselected pivot is at (extra-1)/2, and we
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1090 want to move the PPs before that to the left end of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1091 the slice, and the PPs after that to the right end.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1092 The following section changes extra, lo, hi, and the
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1093 slice such that:
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1094 [lo-extra, lo) contains the smaller PPs.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1095 *lo == our PP.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1096 (lo, hi) contains the unknown elements.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1097 [hi, hi+extra) contains the larger PPs.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1098 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1099 k = extra >>= 1; /* num PPs to move */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1100 if (extraOnRight) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1101 /* Swap the smaller PPs to the left end.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1102 Note that this loop actually moves k+1 items:
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1103 the last is our PP */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1104 do {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1105 tmp = *lo; *lo = *hi; *hi = tmp;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1106 ++lo; ++hi;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1107 } while (k--);
8457
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1108 }
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1109 else {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1110 /* Swap the larger PPs to the right end. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1111 while (k--) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1112 --lo; --hi;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1113 tmp = *lo; *lo = *hi; *hi = tmp;
8457
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1114 }
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1115 }
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1116 --lo; /* *lo is now our PP */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1117 pivot = *lo;
8164
ea59bb921380 Quicksort retuned by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 7986
diff changeset
1118
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1119 /* Now an almost-ordinary quicksort partition step.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1120 Note that most of the time is spent here!
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1121 Only odd thing is that we partition into < and >=,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1122 instead of the usual <= and >=. This helps when
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1123 there are lots of duplicates of different values,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1124 because it eventually tends to make subfiles
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1125 "pure" (all duplicates), and we special-case for
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1126 duplicates later. */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1127 l = lo + 1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1128 r = hi - 1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1129 /* assert lo < l < r < hi (small n weeded out above) */
8322
e6ef0e39e35c Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents: 8186
diff changeset
1130
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1131 do {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1132 /* slide l right, looking for key >= pivot */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1133 do {
8457
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1134 SETK(*l, pivot);
8322
e6ef0e39e35c Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents: 8186
diff changeset
1135 if (k < 0)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1136 ++l;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1137 else
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1138 break;
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1139 } while (l < r);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1140
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1141 /* slide r left, looking for key < pivot */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1142 while (l < r) {
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1143 register PyObject *rval = *r--;
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1144 SETK(rval, pivot);
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1145 if (k < 0) {
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1146 /* swap and advance */
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1147 r[1] = *l;
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1148 *l++ = rval;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1149 break;
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1150 }
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1151 }
8164
ea59bb921380 Quicksort retuned by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 7986
diff changeset
1152
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1153 } while (l < r);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1154
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1155 /* assert lo < r <= l < hi
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1156 assert r == l or r+1 == l
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1157 everything to the left of l is < pivot, and
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1158 everything to the right of r is >= pivot */
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1159
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1160 if (l == r) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1161 SETK(*r, pivot);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1162 if (k < 0)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1163 ++l;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1164 else
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1165 --r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1166 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1167 /* assert lo <= r and r+1 == l and l <= hi
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1168 assert r == lo or a[r] < pivot
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1169 assert a[lo] is pivot
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1170 assert l == hi or a[l] >= pivot
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1171 Swap the pivot into "the middle", so we can henceforth
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1172 ignore it.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1173 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1174 *lo = *r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1175 *r = pivot;
8322
e6ef0e39e35c Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents: 8186
diff changeset
1176
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1177 /* The following is true now, & will be preserved:
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1178 All in [lo,r) are < pivot
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1179 All in [r,l) == pivot (& so can be ignored)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1180 All in [l,hi) are >= pivot */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1181
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1182 /* Check for duplicates of the pivot. One compare is
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1183 wasted if there are no duplicates, but can win big
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1184 when there are.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1185 Tricky: we're sticking to "<" compares, so deduce
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1186 equality indirectly. We know pivot <= *l, so they're
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1187 equal iff not pivot < *l.
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1188 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1189 while (l < hi) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1190 /* pivot <= *l known */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1191 SETK(pivot, *l);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1192 if (k < 0)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1193 break;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1194 else
8428
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1195 /* <= and not < implies == */
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1196 ++l;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1197 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1198
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1199 /* assert lo <= r < l <= hi
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1200 Partitions are [lo, r) and [l, hi) */
8322
e6ef0e39e35c Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents: 8186
diff changeset
1201
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1202 /* push fattest first; remember we still have extra PPs
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1203 to the left of the left chunk and to the right of
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1204 the right chunk! */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1205 /* assert top < STACKSIZE */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1206 if (r - lo <= hi - l) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1207 /* second is bigger */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1208 stack[top].lo = l;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1209 stack[top].hi = hi;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1210 stack[top].extra = -extra;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1211 hi = r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1212 extraOnRight = 0;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1213 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1214 else {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1215 /* first is bigger */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1216 stack[top].lo = lo;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1217 stack[top].hi = r;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1218 stack[top].extra = extra;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1219 lo = l;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1220 extraOnRight = 1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1221 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1222 ++top;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1223
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1224 } /* end of partitioning loop */
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1225
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1226 return 0;
8457
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1227
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1228 fail:
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1229 return -1;
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1230 }
8457
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1231
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1232 #undef SETK
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1233
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1234 staticforward PyTypeObject immutable_list_type;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1235
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1236 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1237 listsort(PyListObject *self, PyObject *args)
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1238 {
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1239 int err;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1240 PyObject *compare = NULL;
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1241
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1242 if (args != NULL) {
11848
6f9799a145e5 OKOK, Greg's right, I should've used the :name option in the argument
Guido van Rossum <guido@python.org>
parents: 11846
diff changeset
1243 if (!PyArg_ParseTuple(args, "|O:sort", &compare))
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1244 return NULL;
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1245 }
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1246 self->ob_type = &immutable_list_type;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1247 err = samplesortslice(self->ob_item,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1248 self->ob_item + self->ob_size,
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1249 compare);
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1250 self->ob_type = &PyList_Type;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1251 if (err < 0)
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1252 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1253 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1254 return Py_None;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1255 }
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1256
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1257 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1258 PyList_Sort(PyObject *v)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1259 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1260 if (v == NULL || !PyList_Check(v)) {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1261 PyErr_BadInternalCall();
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1262 return -1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1263 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1264 v = listsort((PyListObject *)v, (PyObject *)NULL);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1265 if (v == NULL)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1266 return -1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1267 Py_DECREF(v);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1268 return 0;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1269 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1270
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1271 static void
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1272 _listreverse(PyListObject *self)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1273 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1274 register PyObject **p, **q;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1275 register PyObject *tmp;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1276
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1277 if (self->ob_size > 1) {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1278 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1279 p < q;
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1280 p++, q--)
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1281 {
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1282 tmp = *p;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1283 *p = *q;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1284 *q = tmp;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1285 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1286 }
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1287 }
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1288
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1289 static PyObject *
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1290 listreverse(PyListObject *self, PyObject *args)
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1291 {
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1292 if (!PyArg_ParseTuple(args, ":reverse"))
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1293 return NULL;
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1294 _listreverse(self);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1295 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1296 return Py_None;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1297 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1298
47
05057e3660f8 Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1299 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1300 PyList_Reverse(PyObject *v)
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1301 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1302 if (v == NULL || !PyList_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1303 PyErr_BadInternalCall();
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1304 return -1;
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1305 }
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1306 _listreverse((PyListObject *)v);
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1307 return 0;
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1308 }
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1309
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1310 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1311 PyList_AsTuple(PyObject *v)
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1312 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1313 PyObject *w;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1314 PyObject **p;
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1315 int n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1316 if (v == NULL || !PyList_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1317 PyErr_BadInternalCall();
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1318 return NULL;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1319 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1320 n = ((PyListObject *)v)->ob_size;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1321 w = PyTuple_New(n);
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1322 if (w == NULL)
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1323 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1324 p = ((PyTupleObject *)w)->ob_item;
13709
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13611
diff changeset
1325 memcpy((void *)p,
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13611
diff changeset
1326 (void *)((PyListObject *)v)->ob_item,
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1327 n*sizeof(PyObject *));
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1328 while (--n >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1329 Py_INCREF(*p);
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1330 p++;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1331 }
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1332 return w;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1333 }
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1334
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1335 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1336 listindex(PyListObject *self, PyObject *args)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1337 {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1338 int i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1339 PyObject *v;
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1340
11996
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
1341 if (!PyArg_ParseTuple_Compat1(args, "O:index", &v))
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1342 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1343 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1344 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1345 if (cmp > 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1346 return PyInt_FromLong((long)i);
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1347 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1348 return NULL;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1349 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1350 PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1351 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1352 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1353
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1354 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1355 listcount(PyListObject *self, PyObject *args)
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1356 {
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1357 int count = 0;
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1358 int i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1359 PyObject *v;
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1360
11996
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
1361 if (!PyArg_ParseTuple_Compat1(args, "O:count", &v))
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1362 return NULL;
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1363 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1364 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1365 if (cmp > 0)
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1366 count++;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1367 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1368 return NULL;
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1369 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1370 return PyInt_FromLong((long)count);
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1371 }
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1372
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1373 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1374 listremove(PyListObject *self, PyObject *args)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1375 {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1376 int i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1377 PyObject *v;
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1378
11996
23dc89ad0996 Added Christian Tismer's patch to allow list.append(a,b,c) back --
Guido van Rossum <guido@python.org>
parents: 11848
diff changeset
1379 if (!PyArg_ParseTuple_Compat1(args, "O:remove", &v))
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1380 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1381 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1382 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1383 if (cmp > 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1384 if (list_ass_slice(self, i, i+1,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1385 (PyObject *)NULL) != 0)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1386 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1387 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1388 return Py_None;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1389 }
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1390 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1391 return NULL;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1392 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1393 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1394 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1395 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1396
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1397 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1398 list_traverse(PyListObject *o, visitproc visit, void *arg)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1399 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1400 int i, err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1401 PyObject *x;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1402
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1403 for (i = o->ob_size; --i >= 0; ) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1404 x = o->ob_item[i];
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1405 if (x != NULL) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1406 err = visit(x, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1407 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1408 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1409 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1410 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1411 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1412 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1413
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1414 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1415 list_clear(PyListObject *lp)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1416 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1417 (void) PyList_SetSlice((PyObject *)lp, 0, lp->ob_size, 0);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1418 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1419 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1420
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1421 static PyObject *
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1422 list_richcompare(PyObject *v, PyObject *w, int op)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1423 {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1424 PyListObject *vl, *wl;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1425 int i;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1426
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1427 if (!PyList_Check(v) || !PyList_Check(w)) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1428 Py_INCREF(Py_NotImplemented);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1429 return Py_NotImplemented;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1430 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1431
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1432 vl = (PyListObject *)v;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1433 wl = (PyListObject *)w;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1434
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1435 if (vl->ob_size != wl->ob_size && (op == Py_EQ || op == Py_NE)) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1436 /* Shortcut: if the lengths differ, the lists differ */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1437 PyObject *res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1438 if (op == Py_EQ)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1439 res = Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1440 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1441 res = Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1442 Py_INCREF(res);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1443 return res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1444 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1445
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1446 /* Search for the first index where items are different */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1447 for (i = 0; i < vl->ob_size && i < wl->ob_size; i++) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1448 int k = PyObject_RichCompareBool(vl->ob_item[i],
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1449 wl->ob_item[i], Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1450 if (k < 0)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1451 return NULL;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1452 if (!k)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1453 break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1454 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1455
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1456 if (i >= vl->ob_size || i >= wl->ob_size) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1457 /* No more items to compare -- compare sizes */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1458 int vs = vl->ob_size;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1459 int ws = wl->ob_size;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1460 int cmp;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1461 PyObject *res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1462 switch (op) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1463 case Py_LT: cmp = vs < ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1464 case Py_LE: cmp = ws <= ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1465 case Py_EQ: cmp = vs == ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1466 case Py_NE: cmp = vs != ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1467 case Py_GT: cmp = vs > ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1468 case Py_GE: cmp = vs >= ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1469 default: return NULL; /* cannot happen */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1470 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1471 if (cmp)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1472 res = Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1473 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1474 res = Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1475 Py_INCREF(res);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1476 return res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1477 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1478
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1479 /* We have an item that differs -- shortcuts for EQ/NE */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1480 if (op == Py_EQ) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1481 Py_INCREF(Py_False);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1482 return Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1483 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1484 if (op == Py_NE) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1485 Py_INCREF(Py_True);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1486 return Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1487 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1488
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1489 /* Compare the final item again using the proper operator */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1490 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1491 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1492
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1493 static char append_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1494 "L.append(object) -- append object to end";
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
1495 static char extend_doc[] =
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
1496 "L.extend(list) -- extend list by appending list elements";
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1497 static char insert_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1498 "L.insert(index, object) -- insert object before index";
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1499 static char pop_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1500 "L.pop([index]) -> item -- remove and return item at index (default last)";
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1501 static char remove_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1502 "L.remove(value) -- remove first occurrence of value";
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1503 static char index_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1504 "L.index(value) -> integer -- return index of first occurrence of value";
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1505 static char count_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1506 "L.count(value) -> integer -- return number of occurrences of value";
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1507 static char reverse_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1508 "L.reverse() -- reverse *IN PLACE*";
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1509 static char sort_doc[] =
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1510 "L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) -> -1, 0, 1";
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1511
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1512 static PyMethodDef list_methods[] = {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1513 {"append", (PyCFunction)listappend, METH_VARARGS, append_doc},
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1514 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1515 {"extend", (PyCFunction)listextend, METH_VARARGS, extend_doc},
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1516 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1517 {"remove", (PyCFunction)listremove, METH_VARARGS, remove_doc},
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1518 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1519 {"count", (PyCFunction)listcount, METH_VARARGS, count_doc},
15558
c81bed0168a0 Use METH_VARARGS instead of "1" in list method table.
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
1520 {"reverse", (PyCFunction)listreverse, METH_VARARGS, reverse_doc},
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1521 {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1522 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1523 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1525 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1526 list_getattr(PyListObject *f, char *name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1528 return Py_FindMethod(list_methods, (PyObject *)f, name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1529 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1530
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1531 static PySequenceMethods list_as_sequence = {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1532 (inquiry)list_length, /* sq_length */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1533 (binaryfunc)list_concat, /* sq_concat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1534 (intargfunc)list_repeat, /* sq_repeat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1535 (intargfunc)list_item, /* sq_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1536 (intintargfunc)list_slice, /* sq_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1537 (intobjargproc)list_ass_item, /* sq_ass_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1538 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1539 (objobjproc)list_contains, /* sq_contains */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1540 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1541 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1542 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1543
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1544 PyTypeObject PyList_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1545 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1546 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1547 "list",
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
1548 sizeof(PyListObject) + PyGC_HEAD_SIZE,
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1549 0,
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1550 (destructor)list_dealloc, /* tp_dealloc */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1551 (printfunc)list_print, /* tp_print */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1552 (getattrfunc)list_getattr, /* tp_getattr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1553 0, /* tp_setattr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1554 0, /* tp_compare */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1555 (reprfunc)list_repr, /* tp_repr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1556 0, /* tp_as_number */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1557 &list_as_sequence, /* tp_as_sequence */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1558 0, /* tp_as_mapping */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1559 0, /* tp_hash */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1560 0, /* tp_call */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1561 0, /* tp_str */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1562 0, /* tp_getattro */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1563 0, /* tp_setattro */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1564 0, /* tp_as_buffer */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1565 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1566 0, /* tp_doc */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1567 (traverseproc)list_traverse, /* tp_traverse */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1568 (inquiry)list_clear, /* tp_clear */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1569 list_richcompare, /* tp_richcompare */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1570 };
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1571
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1572
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1573 /* During a sort, we really can't have anyone modifying the list; it could
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1574 cause core dumps. Thus, we substitute a dummy type that raises an
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1575 explanatory exception when a modifying operation is used. Caveat:
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1576 comparisons may behave differently; but I guess it's a bad idea anyway to
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1577 compare a list that's being sorted... */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1578
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1579 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1580 immutable_list_op(void)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1581 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1582 PyErr_SetString(PyExc_TypeError,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1583 "a list cannot be modified while it is being sorted");
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1584 return NULL;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1585 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1586
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1587 static PyMethodDef immutable_list_methods[] = {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1588 {"append", (PyCFunction)immutable_list_op},
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1589 {"insert", (PyCFunction)immutable_list_op},
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1590 {"remove", (PyCFunction)immutable_list_op},
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1591 {"index", (PyCFunction)listindex},
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1592 {"count", (PyCFunction)listcount},
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1593 {"reverse", (PyCFunction)immutable_list_op},
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1594 {"sort", (PyCFunction)immutable_list_op},
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1595 {NULL, NULL} /* sentinel */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1596 };
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1597
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1598 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1599 immutable_list_getattr(PyListObject *f, char *name)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1600 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1601 return Py_FindMethod(immutable_list_methods, (PyObject *)f, name);
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1602 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1603
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1604 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1605 immutable_list_ass(void)
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1606 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1607 immutable_list_op();
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1608 return -1;
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1609 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1610
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1611 static PySequenceMethods immutable_list_as_sequence = {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1612 (inquiry)list_length, /* sq_length */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1613 (binaryfunc)list_concat, /* sq_concat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1614 (intargfunc)list_repeat, /* sq_repeat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1615 (intargfunc)list_item, /* sq_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1616 (intintargfunc)list_slice, /* sq_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1617 (intobjargproc)immutable_list_ass, /* sq_ass_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1618 (intintobjargproc)immutable_list_ass, /* sq_ass_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1619 (objobjproc)list_contains, /* sq_contains */
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1620 };
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1621
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1622 static PyTypeObject immutable_list_type = {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1623 PyObject_HEAD_INIT(&PyType_Type)
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1624 0,
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1625 "list (immutable, during sort)",
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
1626 sizeof(PyListObject) + PyGC_HEAD_SIZE,
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1627 0,
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1628 0, /* Cannot happen */ /* tp_dealloc */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1629 (printfunc)list_print, /* tp_print */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1630 (getattrfunc)immutable_list_getattr, /* tp_getattr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1631 0, /* tp_setattr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1632 0, /* Won't be called */ /* tp_compare */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1633 (reprfunc)list_repr, /* tp_repr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1634 0, /* tp_as_number */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1635 &immutable_list_as_sequence, /* tp_as_sequence */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1636 0, /* tp_as_mapping */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1637 0, /* tp_hash */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1638 0, /* tp_call */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1639 0, /* tp_str */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1640 0, /* tp_getattro */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1641 0, /* tp_setattro */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1642 0, /* tp_as_buffer */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1643 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1644 0, /* tp_doc */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1645 (traverseproc)list_traverse, /* tp_traverse */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1646 0, /* tp_clear */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1647 list_richcompare, /* tp_richcompare */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1648 /* NOTE: This is *not* the standard list_type struct! */
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1649 };