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