annotate Objects/listobject.c @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents d43e82b8d79d
children 0c85a947e2c0
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
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
12 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
13 roundupsize(int n)
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
14 {
17969
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
15 unsigned int nbits = 0;
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
16 unsigned int n2 = (unsigned int)n >> 5;
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
17
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
18 /* Round up:
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
19 * If n < 256, to a multiple of 8.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
20 * If n < 2048, to a multiple of 64.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
21 * If n < 16384, to a multiple of 512.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
22 * If n < 131072, to a multiple of 4096.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
23 * If n < 1048576, to a multiple of 32768.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
24 * If n < 8388608, to a multiple of 262144.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
25 * If n < 67108864, to a multiple of 2097152.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
26 * If n < 536870912, to a multiple of 16777216.
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
27 * ...
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
28 * If n < 2**(5+3*i), to a multiple of 2**(3*i).
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
29 *
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
30 * This over-allocates proportional to the list size, making room
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
31 * for additional growth. The over-allocation is mild, but is
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
32 * enough to give linear-time amortized behavior over a long
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
33 * sequence of appends() in the presence of a poorly-performing
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
34 * system realloc() (which is a reality, e.g., across all flavors
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
35 * of Windows, with Win9x behavior being particularly bad -- and
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
36 * we've still got address space fragmentation problems on Win9x
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
37 * even with this scheme, although it requires much longer lists to
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
38 * provoke them than it used to).
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
39 */
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
40 do {
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
41 n2 >>= 3;
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
42 nbits += 3;
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
43 } while (n2);
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
44 return ((n >> nbits) + 1) << nbits;
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
45 }
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
46
23520
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
47 #define NRESIZE(var, type, nitems) \
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
48 do { \
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
49 size_t _new_size = roundupsize(nitems); \
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
50 if (_new_size <= ((~(size_t)0) / sizeof(type))) \
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
51 PyMem_RESIZE(var, type, _new_size); \
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
52 else \
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
53 var = NULL; \
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
54 } while (0)
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
55
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
56 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
57 PyList_New(int size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
60 PyListObject *op;
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
61 size_t nbytes;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if (size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
63 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
66 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
67 /* Check for overflow */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
68 if (nbytes / sizeof(PyObject *) != (size_t)size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
69 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
70 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
71 op = PyObject_GC_New(PyListObject, &PyList_Type);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 if (op == NULL) {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
73 return NULL;
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 if (size <= 0) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 op->ob_item = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 else {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
79 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 if (op->ob_item == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
81 return PyErr_NoMemory();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
84 op->ob_size = size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 for (i = 0; i < size; i++)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 op->ob_item[i] = NULL;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
87 _PyObject_GC_TRACK(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
88 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
92 PyList_Size(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
94 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
95 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
99 return ((PyListObject *)op) -> ob_size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
102 static PyObject *indexerr;
3985
011cfe7490eb Use pre-created string objects for most common exceptions
Guido van Rossum <guido@python.org>
parents: 2827
diff changeset
103
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
104 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
105 PyList_GetItem(PyObject *op, int i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
107 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
108 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
111 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
112 if (indexerr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
113 indexerr = PyString_FromString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
114 "list index out of range");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
115 PyErr_SetObject(PyExc_IndexError, indexerr);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
118 return ((PyListObject *)op) -> ob_item[i];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
122 PyList_SetItem(register PyObject *op, register int i,
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
123 register PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
125 register PyObject *olditem;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
126 register PyObject **p;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
127 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
128 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
129 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
130 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
132 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
133 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
134 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
135 "list assignment index out of range");
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
136 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
138 p = ((PyListObject *)op) -> ob_item + i;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
139 olditem = *p;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
140 *p = newitem;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
141 Py_XDECREF(olditem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
146 ins1(PyListObject *self, int where, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
149 PyObject **items;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
150 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
151 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
152 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
153 }
13957
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
154 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
155 PyErr_SetString(PyExc_OverflowError,
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
156 "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
157 return -1;
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
158 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 items = self->ob_item;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
160 NRESIZE(items, PyObject *, self->ob_size+1);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
161 if (items == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
162 PyErr_NoMemory();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
163 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
164 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 if (where < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 where = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 if (where > self->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 where = self->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 for (i = self->ob_size; --i >= where; )
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 items[i+1] = items[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
171 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 items[where] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 self->ob_item = items;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 self->ob_size++;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 }
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
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
179 PyList_Insert(PyObject *op, int where, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
181 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
182 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
183 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
184 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
185 return ins1((PyListObject *)op, where, newitem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
189 PyList_Append(PyObject *op, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
191 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
192 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
193 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
194 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
195 return ins1((PyListObject *)op,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
196 (int) ((PyListObject *)op)->ob_size, newitem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 static void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
202 list_dealloc(PyListObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 int i;
22626
0446eeb787b7 Backport to 2.2.1:
Guido van Rossum <guido@python.org>
parents: 21155
diff changeset
205 PyObject_GC_UnTrack(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11996
diff changeset
206 Py_TRASHCAN_SAFE_BEGIN(op)
2188
6461ff0c4bee Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents: 2159
diff changeset
207 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
208 /* 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
209 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
210 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
211 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
212 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
213 while (--i >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
214 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
215 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
216 PyMem_FREE(op->ob_item);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 }
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 19597
diff changeset
218 op->ob_type->tp_free((PyObject *)op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11996
diff changeset
219 Py_TRASHCAN_SAFE_END(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
222 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
223 list_print(PyListObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 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
226
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
227 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
228 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
229 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
230 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
231 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
232 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
233 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 fprintf(fp, "[");
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
235 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
236 if (i > 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 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
238 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
239 Py_ReprLeave((PyObject *)op);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
240 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
241 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 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
244 Py_ReprLeave((PyObject *)op);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
245 return 0;
6
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
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
248 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
249 list_repr(PyListObject *v)
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 int i;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
252 PyObject *s, *temp;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
253 PyObject *pieces = NULL, *result = NULL;
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
254
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
255 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
256 if (i != 0) {
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
257 return i > 0 ? PyString_FromString("[...]") : NULL;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
258 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
259
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
260 if (v->ob_size == 0) {
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
261 result = PyString_FromString("[]");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
262 goto Done;
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
263 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
264
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
265 pieces = PyList_New(0);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
266 if (pieces == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
267 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
268
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
269 /* Do repr() on each element. Note that this may mutate the list,
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
270 so must refetch the list size on each iteration. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
271 for (i = 0; i < v->ob_size; ++i) {
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
272 int status;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
273 s = PyObject_Repr(v->ob_item[i]);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
274 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
275 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
276 status = PyList_Append(pieces, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
277 Py_DECREF(s); /* append created a new ref */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
278 if (status < 0)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
279 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
280 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
281
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
282 /* Add "[]" decorations to the first and last items. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
283 assert(PyList_GET_SIZE(pieces) > 0);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
284 s = PyString_FromString("[");
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
285 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
286 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
287 temp = PyList_GET_ITEM(pieces, 0);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
288 PyString_ConcatAndDel(&s, temp);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
289 PyList_SET_ITEM(pieces, 0, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
290 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
291 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
292
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
293 s = PyString_FromString("]");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
294 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
295 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
296 temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
297 PyString_ConcatAndDel(&temp, s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
298 PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
299 if (temp == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
300 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
301
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
302 /* Paste them all together with ", " between. */
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
303 s = PyString_FromString(", ");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
304 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
305 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
306 result = _PyString_Join(s, pieces);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
307 Py_DECREF(s);
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
308
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
309 Done:
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
310 Py_XDECREF(pieces);
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
311 Py_ReprLeave((PyObject *)v);
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
312 return result;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
316 list_length(PyListObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
321
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
322
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
323 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
324 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
325 {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
326 int i;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
327
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
328 for (i = 0; i < a->ob_size; ++i) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
329 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
330 Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
331 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
332 return 1;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
333 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
334 return -1;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
335 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
336 return 0;
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
337 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
338
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
339
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
340 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
341 list_item(PyListObject *a, int i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 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
344 if (indexerr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
345 indexerr = PyString_FromString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
346 "list index out of range");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
347 PyErr_SetObject(PyExc_IndexError, indexerr);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
350 Py_INCREF(a->ob_item[i]);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 return a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
354 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
355 list_slice(PyListObject *a, int ilow, int ihigh)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
357 PyListObject *np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 else if (ilow > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 ilow = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 else if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 ihigh = a->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
367 np = (PyListObject *) PyList_New(ihigh - ilow);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 if (np == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 for (i = ilow; i < ihigh; i++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
371 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
372 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 np->ob_item[i - ilow] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
375 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
378 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
379 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
380 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
381 if (!PyList_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
382 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
383 return NULL;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
384 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
385 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
386 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
387
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
388 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
389 list_concat(PyListObject *a, PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
393 PyListObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
394 if (!PyList_Check(bb)) {
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
395 PyErr_Format(PyExc_TypeError,
12817
5f1f8ddc2a0c Improve TypeError exception message for list catenation.
Fred Drake <fdrake@acm.org>
parents: 12816
diff changeset
396 "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
397 bb->ob_type->tp_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
400 #define b ((PyListObject *)bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 size = a->ob_size + b->ob_size;
25742
396c8a24880c Backport listobject.c 2.137 and tupleobject.c 2.75:
Guido van Rossum <guido@python.org>
parents: 23520
diff changeset
402 if (size < 0)
396c8a24880c Backport listobject.c 2.137 and tupleobject.c 2.75:
Guido van Rossum <guido@python.org>
parents: 23520
diff changeset
403 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
404 np = (PyListObject *) PyList_New(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 if (np == NULL) {
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
406 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 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
409 PyObject *v = a->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
410 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 np->ob_item[i] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 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
414 PyObject *v = b->ob_item[i];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
415 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 np->ob_item[i + a->ob_size] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
418 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
422 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
423 list_repeat(PyListObject *a, int n)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
424 {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
425 int i, j;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
426 int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
427 PyListObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
428 PyObject **p;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
429 if (n < 0)
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
430 n = 0;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
431 size = a->ob_size * n;
25749
d43e82b8d79d Darn! Don't divide by zero. Bad fix. :-)
Guido van Rossum <guido@python.org>
parents: 25742
diff changeset
432 if (n && size/n != a->ob_size)
25742
396c8a24880c Backport listobject.c 2.137 and tupleobject.c 2.75:
Guido van Rossum <guido@python.org>
parents: 23520
diff changeset
433 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
434 np = (PyListObject *) PyList_New(size);
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
435 if (np == NULL)
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
436 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
437 p = np->ob_item;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
438 for (i = 0; i < n; i++) {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
439 for (j = 0; j < a->ob_size; j++) {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
440 *p = a->ob_item[j];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
441 Py_INCREF(*p);
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
442 p++;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
443 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
444 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
445 return (PyObject *) np;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
446 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
447
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
449 list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 {
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
451 /* 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
452 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
453 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
454 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
455 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
456 list. :-( */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
457 PyObject **recycle, **p;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
458 PyObject **item;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 int n; /* Size of replacement list */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 int d; /* Change in size */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 int k; /* Loop index */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
462 #define b ((PyListObject *)v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 if (v == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 n = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
465 else if (PyList_Check(v)) {
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 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
467 if (a == b) {
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
468 /* 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
469 int ret;
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
470 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
471 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
472 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
473 return ret;
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
474 }
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
475 }
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
476 else {
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
477 PyErr_Format(PyExc_TypeError,
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
478 "must assign list (not \"%.200s\") to slice",
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
479 v->ob_type->tp_name);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
480 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
481 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 else if (ilow > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 ilow = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 else if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 ihigh = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 item = a->ob_item;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 d = n - (ihigh-ilow);
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
492 if (ihigh > ilow)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
493 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
494 else
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
495 p = recycle = NULL;
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
496 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 for (k = ilow; k < ihigh; k++)
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
498 *p++ = item[k];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 if (d < 0) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 for (/*k = ihigh*/; k < a->ob_size; k++)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 item[k+d] = item[k];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 a->ob_size += d;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
503 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 a->ob_item = item;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 }
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
507 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
508 NRESIZE(item, PyObject *, a->ob_size + d);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
509 if (item == NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
510 if (recycle != NULL)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
511 PyMem_DEL(recycle);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
512 PyErr_NoMemory();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
513 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
514 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 for (k = a->ob_size; --k >= ihigh; )
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 item[k+d] = item[k];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 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
518 *p++ = item[k];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 a->ob_item = item;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 a->ob_size += d;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 for (k = 0; k < n; k++, ilow++) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
523 PyObject *w = b->ob_item[k];
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
524 Py_XINCREF(w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 item[ilow] = w;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 }
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
527 if (recycle) {
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
528 while (--p >= recycle)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
529 Py_XDECREF(*p);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
530 PyMem_DEL(recycle);
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
531 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
532 if (a->ob_size == 0 && a->ob_item != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
533 PyMem_FREE(a->ob_item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
534 a->ob_item = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
535 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
540 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
541 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
542 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
543 if (!PyList_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
544 PyErr_BadInternalCall();
1330
409ea74feb21 Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
545 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
546 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
547 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
548 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
549
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
550 static PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
551 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
552 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
553 PyObject **items;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
554 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
555
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
556
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
557 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
558 if (size == 0) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
559 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
560 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
561 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
562
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
563 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
564
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
565 if (n < 1) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
566 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
567 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
568 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
569 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
570 PyMem_DEL(items);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
571 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
572 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
573 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
574
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
575 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
576 if (items == NULL) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
577 PyErr_NoMemory();
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
578 goto finally;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
579 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
580 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
581 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
582 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
583 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
584 Py_INCREF(o);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
585 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
586 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
587 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
588 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
589 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
590 finally:
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
591 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
592 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
593
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
594 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
595 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
596 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
597 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
598 if (i < 0 || i >= a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
599 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
600 "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
601 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
602 }
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
603 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
604 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
605 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
606 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
607 a->ob_item[i] = v;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
608 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
609 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
610 }
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
611
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
612 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
613 ins(PyListObject *self, int where, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 if (ins1(self, where, v) != 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
617 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
618 return Py_None;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
620
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
621 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
622 listinsert(PyListObject *self, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 int i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
625 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
626 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 return NULL;
1211
406860dfaac7 * stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
628 return ins(self, i, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
631 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
632 listappend(PyListObject *self, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 {
1211
406860dfaac7 * stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents: 1198
diff changeset
634 return ins(self, (int) self->ob_size, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
637 static int
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
638 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
639 {
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
640 PyObject **items;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
641 int selflen = PyList_GET_SIZE(self);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
642 int blen;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
643 register int i;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
644
15707
3075a153a083 fix leak
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15558
diff changeset
645 if (PyObject_Size(b) == 0) {
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
646 /* short circuit when b is empty */
15707
3075a153a083 fix leak
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15558
diff changeset
647 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
648 return 0;
15707
3075a153a083 fix leak
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15558
diff changeset
649 }
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
650
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
651 if (self == (PyListObject*)b) {
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
652 /* 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
653 * situation: a.extend(a)
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
654 *
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
655 * 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
656 * 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
657 */
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
658 Py_DECREF(b);
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
659 b = PyList_New(selflen);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
660 if (!b)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
661 return -1;
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
662 for (i = 0; i < selflen; i++) {
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
663 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
664 Py_INCREF(o);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
665 PyList_SET_ITEM(b, i, o);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
666 }
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
667 }
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
668
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13472
diff changeset
669 blen = PyObject_Size(b);
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
670
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
671 /* resize a using idiom */
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
672 items = self->ob_item;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
673 NRESIZE(items, PyObject*, selflen + blen);
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
674 if (items == NULL) {
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
675 PyErr_NoMemory();
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
676 Py_DECREF(b);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
677 return -1;
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
678 }
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
679
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
680 self->ob_item = items;
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
681
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
682 /* 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
683 for (i = 0; i < blen; i++) {
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
684 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
685 Py_INCREF(o);
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
686 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
687 }
12952
51949de76365 Patch from /F:
Andrew M. Kuchling <amk@amk.ca>
parents: 12925
diff changeset
688 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
689 return 0;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
690 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
691
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
692
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
693 static PyObject *
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
694 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
695 {
17971
73fcc4f4a182 Change list.extend() error msgs and NEWS to reflect that list.extend()
Tim Peters <tim.peters@gmail.com>
parents: 17970
diff changeset
696 other = PySequence_Fast(other, "argument to += must be iterable");
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
697 if (!other)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
698 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
699
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
700 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
701 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
702
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
703 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
704 return (PyObject *)self;
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
705 }
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
706
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
707 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
708 listextend(PyListObject *self, PyObject *b)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
709 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
710
17971
73fcc4f4a182 Change list.extend() error msgs and NEWS to reflect that list.extend()
Tim Peters <tim.peters@gmail.com>
parents: 17970
diff changeset
711 b = PySequence_Fast(b, "list.extend() argument must be iterable");
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
712 if (!b)
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
713 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
714
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
715 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
716 return NULL;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
717
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
718 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
719 return Py_None;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
720 }
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
721
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
722 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
723 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
724 {
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
725 int i = -1;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
726 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
727 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
728 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
729 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
730 /* 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
731 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
732 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
733 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
734 if (i < 0)
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
735 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
736 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
737 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
738 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
739 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
740 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
741 Py_INCREF(v);
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
742 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
743 Py_DECREF(v);
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
744 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
745 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
746 return v;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
747 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
748
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
749 /* 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
750 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
751
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
752 /* 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
753 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
754 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
755 #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
756
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
757 /* 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
758 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
759 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
760 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
761
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
762 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
763 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
764 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
765 PyObject *args, *res;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
766 int i;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
767
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
768 if (compare == NULL) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
769 /* 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
770 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
771 invoke the rich comparison function with Py_LT ('<'), and
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
772 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
773 false. */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
774 i = PyObject_RichCompareBool(x, y, Py_LT);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
775 if (i < 0)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
776 return CMPERROR;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
777 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
778 return -i;
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
779 }
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
780
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
781 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
782 if (args == NULL)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
783 return CMPERROR;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
784 res = PyEval_CallObject(compare, args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
785 Py_DECREF(args);
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
786 if (res == NULL)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
787 return CMPERROR;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
788 if (!PyInt_Check(res)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
789 Py_DECREF(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
790 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
791 "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
792 return CMPERROR;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
793 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
794 i = PyInt_AsLong(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
795 Py_DECREF(res);
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
796 if (i < 0)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
797 return -1;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
798 if (i > 0)
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
799 return 1;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
800 return 0;
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
801 }
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
802
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
803 /* 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
804 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
805 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
806 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
807 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
808 #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
809
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 /* 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
811 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
812 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
813 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
814 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
815 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
816 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
817 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
818 #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
819
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 /* 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
821 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
822 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
823 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
824 (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
825 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
826 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
827 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
828 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
829 #define MAXMERGE 15
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
830
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
831 /* 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
832 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
833 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
834 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
835 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
836 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
837 #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
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
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
840 #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
841
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
842 /* 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
843 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
844 elements.
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
845 [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
846 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
847 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
848 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
849 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
850 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
851 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
852 */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
853
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
854 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
855 binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
856 /* 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
857 {
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
858 /* 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
859 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
860 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
861 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
862 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
863
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 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
865 ++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
866 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
867 /* 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
868 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
869 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
870 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
871 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
872 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
873 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
874 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
875 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
876 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
877 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
878 } 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
879 /* 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
880 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
881 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
882 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
883 *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
884 *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
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 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
887
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 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
889 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
890 }
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
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 /* samplesortslice is the sorting workhorse.
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
893 [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
894 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
895 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
896 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
897 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
898
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
899 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
900 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
901 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
902 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
903 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
904 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
905 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
906 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
907 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
908 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
909 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
910
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 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
912 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
913 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
914 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
915
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 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
917
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 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
919 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
920 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
921 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
922 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
923 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
924 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
925 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
926 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
927 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
928 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
929 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
930 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
931 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
932 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
933 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
934 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
935 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
936 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
937 */
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
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 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
940 /* 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
941 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
942 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
943 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
944 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
945 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
946 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
947 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
948 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
949 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
950 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
951 };
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
952
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
953 /* 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
954 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
955 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
956 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
957 #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
958 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
959 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
960 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
961 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
962 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
963 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
964 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
965 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
966 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
967 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
968 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
969 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
970 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
971 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
972 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
973 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
974 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
975 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
976 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
977 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
978 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
979 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
980 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
981 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
982 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
983 };
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
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
985 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
986 samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
987 /* 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
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 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
990 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
991 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
992 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
993 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
994
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 /* 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
996 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
997
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 /* ----------------------------------------------------------
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 * 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
1000 * --------------------------------------------------------*/
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 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
1002 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
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 /* 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
1005 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
1006 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
1007 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
1008 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
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 /* 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
1011 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
1012 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
1013 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
1014 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
1015 }
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 /* [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
1017 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
1018 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
1019 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
1020
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
1021 /* 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
1022 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
1023 /* 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
1024 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
1025 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
1026 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
1027 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
1028 }
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 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
1030 /* 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
1031 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
1032 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
1033 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
1034 --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
1035 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
1036 ++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
1037 } while (l < r);
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1038 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
1039 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1040
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 /* ----------------------------------------------------------
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 * 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
1043 * --------------------------------------------------------*/
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
1044
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
1045 /* 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
1046 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
1047 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
1048 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
1049 ++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
1050 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
1051 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
1052 /* 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
1053 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
1054 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
1055 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
1056 }
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 /* 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
1058 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
1059 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
1060 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
1061 /* 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
1062
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 /* 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
1064 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
1065 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
1066 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
1067 {
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 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
1069 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
1070 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
1071 /* 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
1072 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
1073 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
1074 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
1075 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
1076 }
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 }
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
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 /* Recursively sort the preselected pivots. */
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1080 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
1081 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
1082
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 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
1084 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
1085 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
1086
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1087 /* ----------------------------------------------------------
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 * 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
1089 * --------------------------------------------------------*/
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 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
1091 /* 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
1092 n = hi - lo;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1093
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
1094 /* 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
1095 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
1096 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
1097 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
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 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
1100 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
1101 /* 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
1102 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
1103 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
1104 ln(original n). */
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1105 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
1106 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
1107 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1108 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
1109 /* 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
1110 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
1111 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
1112 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
1113 /* 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
1114 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
1115 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
1116 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
1117 *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
1118 *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
1119 ++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
1120 } 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
1121 }
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 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
1123 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
1124 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
1125 }
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
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 /* 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
1128 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
1129 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
1130 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
1131 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
1132 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
1133 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
1134 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
1135 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
1136 extra = -extra;
8428
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1137 }
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1138 continue;
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1139 }
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1140
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
1141 /* 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
1142 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
1143 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
1144 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
1145 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
1146 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
1147 [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
1148 *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
1149 (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
1150 [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
1151 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1152 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
1153 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
1154 /* 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
1155 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
1156 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
1157 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
1158 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
1159 ++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
1160 } 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
1161 }
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
1162 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
1163 /* 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
1164 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
1165 --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
1166 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
1167 }
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1168 }
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
1169 --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
1170 pivot = *lo;
8164
ea59bb921380 Quicksort retuned by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 7986
diff changeset
1171
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
1172 /* 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
1173 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
1174 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
1175 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
1176 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
1177 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
1178 "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
1179 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
1180 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
1181 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
1182 /* 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
1183
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
1184 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
1185 /* 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
1186 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
1187 SETK(*l, pivot);
8322
e6ef0e39e35c Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents: 8186
diff changeset
1188 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
1189 ++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
1190 else
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1191 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
1192 } 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
1193
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 /* 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
1195 while (l < r) {
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1196 register PyObject *rval = *r--;
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1197 SETK(rval, pivot);
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1198 if (k < 0) {
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1199 /* swap and advance */
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1200 r[1] = *l;
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1201 *l++ = rval;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1202 break;
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
1203 }
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1204 }
8164
ea59bb921380 Quicksort retuned by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 7986
diff changeset
1205
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
1206 } 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
1207
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 /* 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
1209 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
1210 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
1211 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
1212
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
1213 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
1214 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
1215 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
1216 ++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
1217 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
1218 --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
1219 }
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 /* 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
1221 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
1222 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
1223 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
1224 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
1225 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
1226 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1227 *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
1228 *r = pivot;
8322
e6ef0e39e35c Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents: 8186
diff changeset
1229
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 /* 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
1231 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
1232 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
1233 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
1234
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
1235 /* 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
1236 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
1237 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
1238 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
1239 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
1240 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
1241 */
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1242 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
1243 /* 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
1244 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
1245 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
1246 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
1247 else
8428
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1248 /* <= 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
1249 ++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
1250 }
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
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1252 /* 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
1253 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
1254
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
1255 /* 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
1256 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
1257 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
1258 /* 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
1259 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
1260 /* 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
1261 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
1262 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
1263 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
1264 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
1265 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
1266 }
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 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
1268 /* 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
1269 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
1270 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
1271 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
1272 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
1273 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
1274 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1275 ++top;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1276
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
1277 } /* 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
1278
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1279 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
1280
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1281 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
1282 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
1283 }
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
1284
af48887c9717 Tim's revision of the previous patch. He also added some sparts to
Guido van Rossum <guido@python.org>
parents: 8440
diff changeset
1285 #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
1286
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1287 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
1288
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1289 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1290 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
1291 {
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
1292 int err;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1293 PyObject *compare = NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1294 PyTypeObject *savetype;
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
1295
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1296 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
1297 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
1298 return NULL;
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1299 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1300 savetype = self->ob_type;
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
1301 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
1302 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
1303 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
1304 compare);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1305 self->ob_type = savetype;
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
1306 if (err < 0)
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1307 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1308 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1309 return Py_None;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1310 }
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1311
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
1312 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1313 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
1314 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1315 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
1316 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
1317 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
1318 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1319 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
1320 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
1321 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
1322 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
1323 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
1324 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1325
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1326 static void
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1327 _listreverse(PyListObject *self)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1328 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1329 register PyObject **p, **q;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1330 register PyObject *tmp;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1331
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1332 if (self->ob_size > 1) {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1333 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
1334 p < q;
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1335 p++, q--)
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1336 {
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1337 tmp = *p;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1338 *p = *q;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1339 *q = tmp;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1340 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1341 }
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1342 }
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1343
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1344 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1345 listreverse(PyListObject *self)
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1346 {
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1347 _listreverse(self);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1348 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1349 return Py_None;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1350 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1351
47
05057e3660f8 Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
1352 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1353 PyList_Reverse(PyObject *v)
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1354 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1355 if (v == NULL || !PyList_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1356 PyErr_BadInternalCall();
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1357 return -1;
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1358 }
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
1359 _listreverse((PyListObject *)v);
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1360 return 0;
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1361 }
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
1362
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1363 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1364 PyList_AsTuple(PyObject *v)
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1365 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1366 PyObject *w;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1367 PyObject **p;
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1368 int n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1369 if (v == NULL || !PyList_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1370 PyErr_BadInternalCall();
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1371 return NULL;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1372 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1373 n = ((PyListObject *)v)->ob_size;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1374 w = PyTuple_New(n);
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1375 if (w == NULL)
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1376 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1377 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
1378 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
1379 (void *)((PyListObject *)v)->ob_item,
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1380 n*sizeof(PyObject *));
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1381 while (--n >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1382 Py_INCREF(*p);
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1383 p++;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1384 }
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1385 return w;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1386 }
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
1387
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1388 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1389 listindex(PyListObject *self, PyObject *v)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1390 {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1391 int i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1392
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1393 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1394 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
1395 if (cmp > 0)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1396 return PyInt_FromLong((long)i);
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1397 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1398 return NULL;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1399 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1400 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
1401 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1402 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1403
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1404 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1405 listcount(PyListObject *self, PyObject *v)
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1406 {
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1407 int count = 0;
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1408 int i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1409
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1410 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1411 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
1412 if (cmp > 0)
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1413 count++;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1414 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1415 return NULL;
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1416 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1417 return PyInt_FromLong((long)count);
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1418 }
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
1419
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1420 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1421 listremove(PyListObject *self, PyObject *v)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1422 {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1423 int i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1424
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1425 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1426 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
1427 if (cmp > 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1428 if (list_ass_slice(self, i, i+1,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1429 (PyObject *)NULL) != 0)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1430 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1431 Py_INCREF(Py_None);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1432 return Py_None;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1433 }
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1434 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
1435 return NULL;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1436 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1437 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
1438 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1439 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
1440
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1441 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1442 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
1443 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1444 int i, err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1445 PyObject *x;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1446
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1447 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
1448 x = o->ob_item[i];
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1449 if (x != NULL) {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1450 err = visit(x, arg);
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1451 if (err)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1452 return err;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1453 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1454 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1455 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1456 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1457
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1458 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1459 list_clear(PyListObject *lp)
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1460 {
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1461 (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
1462 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1463 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
1464
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1465 static PyObject *
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1466 list_richcompare(PyObject *v, PyObject *w, int op)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1467 {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1468 PyListObject *vl, *wl;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1469 int i;
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 (!PyList_Check(v) || !PyList_Check(w)) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1472 Py_INCREF(Py_NotImplemented);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1473 return Py_NotImplemented;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1474 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1475
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1476 vl = (PyListObject *)v;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1477 wl = (PyListObject *)w;
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 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
1480 /* Shortcut: if the lengths differ, the lists differ */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1481 PyObject *res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1482 if (op == Py_EQ)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1483 res = Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1484 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1485 res = Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1486 Py_INCREF(res);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1487 return res;
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
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1490 /* Search for the first index where items are different */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1491 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
1492 int k = PyObject_RichCompareBool(vl->ob_item[i],
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1493 wl->ob_item[i], Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1494 if (k < 0)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1495 return NULL;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1496 if (!k)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1497 break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1498 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1499
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1500 if (i >= vl->ob_size || i >= wl->ob_size) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1501 /* No more items to compare -- compare sizes */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1502 int vs = vl->ob_size;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1503 int ws = wl->ob_size;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1504 int cmp;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1505 PyObject *res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1506 switch (op) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1507 case Py_LT: cmp = vs < ws; break;
18409
2836b2686180 SF bug #439104: Tuple richcompares has code-typo.
Tim Peters <tim.peters@gmail.com>
parents: 18129
diff changeset
1508 case Py_LE: cmp = vs <= ws; break;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1509 case Py_EQ: cmp = vs == ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1510 case Py_NE: cmp = vs != ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1511 case Py_GT: cmp = vs > ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1512 case Py_GE: cmp = vs >= ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1513 default: return NULL; /* cannot happen */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1514 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1515 if (cmp)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1516 res = Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1517 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1518 res = Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1519 Py_INCREF(res);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1520 return res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1521 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1522
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1523 /* 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
1524 if (op == Py_EQ) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1525 Py_INCREF(Py_False);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1526 return Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1527 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1528 if (op == Py_NE) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1529 Py_INCREF(Py_True);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1530 return Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1531 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1532
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1533 /* Compare the final item again using the proper operator */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1534 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
1535 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1536
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1537 /* Adapted from newer code by Tim */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1538 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1539 list_fill(PyListObject *result, PyObject *v)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1540 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1541 PyObject *it; /* iter(v) */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1542 int n; /* guess for result list size */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1543 int i;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1544
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1545 n = result->ob_size;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1546
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1547 /* Special-case list(a_list), for speed. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1548 if (PyList_Check(v)) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1549 if (v == (PyObject *)result)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1550 return 0; /* source is destination, we're done */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1551 return list_ass_slice(result, 0, n, v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1552 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1553
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1554 /* Empty previous contents */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1555 if (n != 0) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1556 if (list_ass_slice(result, 0, n, (PyObject *)NULL) != 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1557 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1558 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1559
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1560 /* Get iterator. There may be some low-level efficiency to be gained
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1561 * by caching the tp_iternext slot instead of using PyIter_Next()
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1562 * later, but premature optimization is the root etc.
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1563 */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1564 it = PyObject_GetIter(v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1565 if (it == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1566 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1567
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1568 /* Guess a result list size. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1569 n = -1; /* unknown */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1570 if (PySequence_Check(v) &&
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1571 v->ob_type->tp_as_sequence->sq_length) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1572 n = PySequence_Size(v);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1573 if (n < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1574 PyErr_Clear();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1575 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1576 if (n < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1577 n = 8; /* arbitrary */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1578 NRESIZE(result->ob_item, PyObject*, n);
23520
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
1579 if (result->ob_item == NULL) {
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
1580 PyErr_NoMemory();
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1581 goto error;
23520
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
1582 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1583 for (i = 0; i < n; i++)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1584 result->ob_item[i] = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1585 result->ob_size = n;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1586
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1587 /* Run iterator to exhaustion. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1588 for (i = 0; ; i++) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1589 PyObject *item = PyIter_Next(it);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1590 if (item == NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1591 if (PyErr_Occurred())
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1592 goto error;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1593 break;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1594 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1595 if (i < n)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1596 PyList_SET_ITEM(result, i, item); /* steals ref */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1597 else {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1598 int status = ins1(result, result->ob_size, item);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1599 Py_DECREF(item); /* append creates a new ref */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1600 if (status < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1601 goto error;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1602 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1603 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1604
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1605 /* Cut back result list if initial guess was too large. */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1606 if (i < n && result != NULL) {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1607 if (list_ass_slice(result, i, n, (PyObject *)NULL) != 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1608 goto error;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1609 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1610 Py_DECREF(it);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1611 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1612
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1613 error:
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1614 Py_DECREF(it);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1615 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1616 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1617
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1618 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1619 list_init(PyListObject *self, PyObject *args, PyObject *kw)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1620 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1621 PyObject *arg = NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1622 static char *kwlist[] = {"sequence", 0};
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1623
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1624 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1625 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1626 if (arg != NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1627 return list_fill(self, arg);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1628 if (self->ob_size > 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1629 return list_ass_slice(self, 0, self->ob_size, (PyObject*)NULL);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1630 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1631 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1632
21155
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1633 static long
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1634 list_nohash(PyObject *self)
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1635 {
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1636 PyErr_SetString(PyExc_TypeError, "list objects are unhashable");
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1637 return -1;
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1638 }
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1639
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
1640 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
1641 "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
1642 static char extend_doc[] =
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
1643 "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
1644 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
1645 "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
1646 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
1647 "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
1648 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
1649 "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
1650 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
1651 "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
1652 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
1653 "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
1654 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
1655 "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
1656 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
1657 "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
1658
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1659 static PyMethodDef list_methods[] = {
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1660 {"append", (PyCFunction)listappend, METH_O, append_doc},
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1661 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1662 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1663 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1664 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1665 {"index", (PyCFunction)listindex, METH_O, index_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1666 {"count", (PyCFunction)listcount, METH_O, count_doc},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1667 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1668 {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc},
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1669 {NULL, NULL} /* sentinel */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1670 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1671
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1672 static PySequenceMethods list_as_sequence = {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1673 (inquiry)list_length, /* sq_length */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1674 (binaryfunc)list_concat, /* sq_concat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1675 (intargfunc)list_repeat, /* sq_repeat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1676 (intargfunc)list_item, /* sq_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1677 (intintargfunc)list_slice, /* sq_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1678 (intobjargproc)list_ass_item, /* sq_ass_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1679 (intintobjargproc)list_ass_slice, /* sq_ass_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1680 (objobjproc)list_contains, /* sq_contains */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1681 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1682 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1683 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1684
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1685 static char list_doc[] =
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1686 "list() -> new list\n"
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1687 "list(sequence) -> new list initialized from sequence's items";
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1688
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1689 PyTypeObject PyList_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1690 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1691 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1692 "list",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
1693 sizeof(PyListObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1694 0,
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1695 (destructor)list_dealloc, /* tp_dealloc */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1696 (printfunc)list_print, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1697 0, /* tp_getattr */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1698 0, /* tp_setattr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1699 0, /* tp_compare */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1700 (reprfunc)list_repr, /* tp_repr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1701 0, /* tp_as_number */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1702 &list_as_sequence, /* tp_as_sequence */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1703 0, /* tp_as_mapping */
21155
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1704 list_nohash, /* tp_hash */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1705 0, /* tp_call */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1706 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1707 PyObject_GenericGetAttr, /* tp_getattro */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1708 0, /* tp_setattro */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1709 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
1710 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1711 Py_TPFLAGS_BASETYPE, /* tp_flags */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1712 list_doc, /* tp_doc */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1713 (traverseproc)list_traverse, /* tp_traverse */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1714 (inquiry)list_clear, /* tp_clear */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1715 list_richcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1716 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1717 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1718 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1719 list_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1720 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1721 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1722 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1723 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1724 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1725 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1726 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1727 (initproc)list_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1728 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1729 PyType_GenericNew, /* tp_new */
23520
b8728d58e9c3 Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents: 22626
diff changeset
1730 _PyObject_GC_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1731 };
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
1732
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1733
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1734 /* 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
1735 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
1736 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
1737 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
1738 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
1739
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1740 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1741 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
1742 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1743 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
1744 "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
1745 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
1746 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1747
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1748 static PyMethodDef immutable_list_methods[] = {
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1749 {"append", (PyCFunction)immutable_list_op, METH_VARARGS},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1750 {"insert", (PyCFunction)immutable_list_op, METH_VARARGS},
19597
bbdf86f358e9 Give the internal immutable list type .extend and .pop methods (they
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
1751 {"extend", (PyCFunction)immutable_list_op, METH_O},
bbdf86f358e9 Give the internal immutable list type .extend and .pop methods (they
Tim Peters <tim.peters@gmail.com>
parents: 19581
diff changeset
1752 {"pop", (PyCFunction)immutable_list_op, METH_VARARGS},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1753 {"remove", (PyCFunction)immutable_list_op, METH_VARARGS},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1754 {"index", (PyCFunction)listindex, METH_O},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1755 {"count", (PyCFunction)listcount, METH_O},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1756 {"reverse", (PyCFunction)immutable_list_op, METH_VARARGS},
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 18933
diff changeset
1757 {"sort", (PyCFunction)immutable_list_op, METH_VARARGS},
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
1758 {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
1759 };
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1760
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1761 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
1762 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
1763 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1764 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
1765 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
1766 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1767
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1768 static PySequenceMethods immutable_list_as_sequence = {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1769 (inquiry)list_length, /* sq_length */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1770 (binaryfunc)list_concat, /* sq_concat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1771 (intargfunc)list_repeat, /* sq_repeat */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1772 (intargfunc)list_item, /* sq_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1773 (intintargfunc)list_slice, /* sq_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1774 (intobjargproc)immutable_list_ass, /* sq_ass_item */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1775 (intintobjargproc)immutable_list_ass, /* sq_ass_slice */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1776 (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
1777 };
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1778
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1779 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
1780 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
1781 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
1782 "list (immutable, during sort)",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
1783 sizeof(PyListObject),
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
1784 0,
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1785 0, /* Cannot happen */ /* tp_dealloc */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1786 (printfunc)list_print, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1787 0, /* tp_getattr */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1788 0, /* tp_setattr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1789 0, /* Won't be called */ /* tp_compare */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1790 (reprfunc)list_repr, /* tp_repr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1791 0, /* tp_as_number */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1792 &immutable_list_as_sequence, /* tp_as_sequence */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1793 0, /* tp_as_mapping */
21155
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
1794 list_nohash, /* tp_hash */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1795 0, /* tp_call */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1796 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1797 PyObject_GenericGetAttr, /* tp_getattro */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1798 0, /* tp_setattro */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1799 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
1800 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1801 list_doc, /* tp_doc */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1802 (traverseproc)list_traverse, /* tp_traverse */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1803 0, /* tp_clear */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1804 list_richcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1805 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1806 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1807 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1808 immutable_list_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1809 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1810 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1811 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1812 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1813 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1814 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
1815 0, /* tp_init */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
1816 /* 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
1817 };