Mercurial > cpython-lru_cache-weakref
annotate Objects/listobject.c @ 27998:e27f58a50737 legacy-trunk
SF bug #690377: Dud footnote reference in distutils docs
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Fri, 21 Feb 2003 02:47:09 +0000 |
| parents | 41f7726e3e51 |
| children | 9c3572a5575f |
| rev | line source |
|---|---|
| 6 | 1 /* List object implementation */ |
| 2 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3 #include "Python.h" |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4 |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
5 #ifdef STDC_HEADERS |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
6 #include <stddef.h> |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
7 #else |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
8 #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
|
9 #endif |
| 6 | 10 |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
11 static int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
12 roundupsize(int n) |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
13 { |
|
17969
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
14 unsigned int nbits = 0; |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
15 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
|
16 |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
17 /* Round up: |
|
17969
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
18 * 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
|
19 * 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
|
20 * 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
|
21 * 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
|
22 * 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
|
23 * 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
|
24 * 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
|
25 * 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
|
26 * ... |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
27 * 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
|
28 * |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
29 * 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
|
30 * 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
|
31 * 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
|
32 * 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
|
33 * 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
|
34 * 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
|
35 * 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
|
36 * 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
|
37 * 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
|
38 */ |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
39 do { |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
40 n2 >>= 3; |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
41 nbits += 3; |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
42 } while (n2); |
|
ed8b79c6cb4b
roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents:
16581
diff
changeset
|
43 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
|
44 } |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
45 |
|
23517
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
46 #define NRESIZE(var, type, nitems) \ |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
47 do { \ |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
48 size_t _new_size = roundupsize(nitems); \ |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
49 if (_new_size <= ((~(size_t)0) / sizeof(type))) \ |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
50 PyMem_RESIZE(var, type, _new_size); \ |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
51 else \ |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
52 var = NULL; \ |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
53 } while (0) |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
54 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
55 PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
56 PyList_New(int size) |
| 6 | 57 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
58 PyListObject *op; |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
59 size_t nbytes; |
| 6 | 60 if (size < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
61 PyErr_BadInternalCall(); |
| 6 | 62 return NULL; |
| 63 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
64 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
|
65 /* Check for overflow */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
66 if (nbytes / sizeof(PyObject *) != (size_t)size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
67 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
|
68 } |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
69 op = PyObject_GC_New(PyListObject, &PyList_Type); |
| 6 | 70 if (op == NULL) { |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
71 return NULL; |
| 6 | 72 } |
| 73 if (size <= 0) { | |
| 74 op->ob_item = NULL; | |
| 75 } | |
| 76 else { | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
77 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes); |
| 6 | 78 if (op->ob_item == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
79 return PyErr_NoMemory(); |
| 6 | 80 } |
|
23834
8ae26d9be1b4
SF #561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
81 memset(op->ob_item, 0, sizeof(*op->ob_item) * size); |
| 6 | 82 } |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
83 op->ob_size = size; |
|
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
84 _PyObject_GC_TRACK(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
85 return (PyObject *) op; |
| 6 | 86 } |
| 87 | |
| 88 int | |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
89 PyList_Size(PyObject *op) |
| 6 | 90 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
91 if (!PyList_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
92 PyErr_BadInternalCall(); |
| 6 | 93 return -1; |
| 94 } | |
| 95 else | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
96 return ((PyListObject *)op) -> ob_size; |
| 6 | 97 } |
| 98 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
99 static PyObject *indexerr; |
|
3985
011cfe7490eb
Use pre-created string objects for most common exceptions
Guido van Rossum <guido@python.org>
parents:
2827
diff
changeset
|
100 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
101 PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
102 PyList_GetItem(PyObject *op, int i) |
| 6 | 103 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
104 if (!PyList_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
105 PyErr_BadInternalCall(); |
| 6 | 106 return NULL; |
| 107 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
108 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
|
109 if (indexerr == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
110 indexerr = PyString_FromString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
111 "list index out of range"); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
112 PyErr_SetObject(PyExc_IndexError, indexerr); |
| 6 | 113 return NULL; |
| 114 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
115 return ((PyListObject *)op) -> ob_item[i]; |
| 6 | 116 } |
| 117 | |
| 118 int | |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
119 PyList_SetItem(register PyObject *op, register int i, |
|
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
120 register PyObject *newitem) |
| 6 | 121 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
122 register PyObject *olditem; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
123 register PyObject **p; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
124 if (!PyList_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
125 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
126 PyErr_BadInternalCall(); |
| 27 | 127 return -1; |
| 6 | 128 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
129 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
130 Py_XDECREF(newitem); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
131 PyErr_SetString(PyExc_IndexError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
132 "list assignment index out of range"); |
| 27 | 133 return -1; |
| 6 | 134 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
135 p = ((PyListObject *)op) -> ob_item + i; |
|
2504
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
136 olditem = *p; |
|
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
137 *p = newitem; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
138 Py_XDECREF(olditem); |
| 6 | 139 return 0; |
| 140 } | |
| 141 | |
| 142 static int | |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
143 ins1(PyListObject *self, int where, PyObject *v) |
| 6 | 144 { |
| 145 int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
146 PyObject **items; |
| 27 | 147 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
148 PyErr_BadInternalCall(); |
| 27 | 149 return -1; |
| 150 } | |
|
13957
b9002da46f69
Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents:
13709
diff
changeset
|
151 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
|
152 PyErr_SetString(PyExc_OverflowError, |
|
b9002da46f69
Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents:
13709
diff
changeset
|
153 "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
|
154 return -1; |
|
b9002da46f69
Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents:
13709
diff
changeset
|
155 } |
| 6 | 156 items = self->ob_item; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
157 NRESIZE(items, PyObject *, self->ob_size+1); |
| 27 | 158 if (items == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
159 PyErr_NoMemory(); |
| 27 | 160 return -1; |
| 161 } | |
| 6 | 162 if (where < 0) |
| 163 where = 0; | |
| 164 if (where > self->ob_size) | |
| 165 where = self->ob_size; | |
| 166 for (i = self->ob_size; --i >= where; ) | |
| 167 items[i+1] = items[i]; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
168 Py_INCREF(v); |
| 6 | 169 items[where] = v; |
| 170 self->ob_item = items; | |
| 171 self->ob_size++; | |
| 172 return 0; | |
| 173 } | |
| 174 | |
| 175 int | |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
176 PyList_Insert(PyObject *op, int where, PyObject *newitem) |
| 6 | 177 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
178 if (!PyList_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
179 PyErr_BadInternalCall(); |
| 27 | 180 return -1; |
| 181 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
182 return ins1((PyListObject *)op, where, newitem); |
| 6 | 183 } |
| 184 | |
| 185 int | |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
186 PyList_Append(PyObject *op, PyObject *newitem) |
| 6 | 187 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
188 if (!PyList_Check(op)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
189 PyErr_BadInternalCall(); |
| 27 | 190 return -1; |
| 191 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
192 return ins1((PyListObject *)op, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
193 (int) ((PyListObject *)op)->ob_size, newitem); |
| 6 | 194 } |
| 195 | |
| 196 /* Methods */ | |
| 197 | |
| 198 static void | |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
199 list_dealloc(PyListObject *op) |
| 6 | 200 { |
| 201 int i; | |
| 22626 | 202 PyObject_GC_UnTrack(op); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11996
diff
changeset
|
203 Py_TRASHCAN_SAFE_BEGIN(op) |
|
2188
6461ff0c4bee
Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents:
2159
diff
changeset
|
204 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
|
205 /* 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
|
206 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
|
207 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
|
208 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
|
209 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
|
210 while (--i >= 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
211 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
|
212 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
213 PyMem_FREE(op->ob_item); |
| 6 | 214 } |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
19597
diff
changeset
|
215 op->ob_type->tp_free((PyObject *)op); |
|
11999
c94247b5c355
Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents:
11996
diff
changeset
|
216 Py_TRASHCAN_SAFE_END(op) |
| 6 | 217 } |
| 218 | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
219 static int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
220 list_print(PyListObject *op, FILE *fp, int flags) |
| 6 | 221 { |
| 222 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
|
223 |
|
971b5bbd92ec
Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents:
7482
diff
changeset
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 } |
| 6 | 231 fprintf(fp, "["); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
232 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
|
233 if (i > 0) |
| 6 | 234 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
|
235 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
|
236 Py_ReprLeave((PyObject *)op); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
237 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
|
238 } |
| 6 | 239 } |
| 240 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
|
241 Py_ReprLeave((PyObject *)op); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
242 return 0; |
| 6 | 243 } |
| 244 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
245 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
246 list_repr(PyListObject *v) |
| 6 | 247 { |
| 248 int i; | |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
249 PyObject *s, *temp; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
250 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
|
251 |
|
971b5bbd92ec
Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents:
7482
diff
changeset
|
252 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
|
253 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
|
254 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
|
255 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
256 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
257 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
|
258 result = PyString_FromString("[]"); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
259 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
|
260 } |
|
18129
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 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
|
263 if (pieces == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
264 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
265 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
266 /* 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
|
267 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
|
268 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
|
269 int status; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
270 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
|
271 if (s == NULL) |
|
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 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
|
274 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
|
275 if (status < 0) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
276 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
277 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
278 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
279 /* 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
|
280 assert(PyList_GET_SIZE(pieces) > 0); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
281 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
|
282 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
283 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
284 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
|
285 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
|
286 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
|
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 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
290 s = PyString_FromString("]"); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
291 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
292 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
293 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
|
294 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
|
295 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
|
296 if (temp == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
297 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
298 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
299 /* 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
|
300 s = PyString_FromString(", "); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
301 if (s == NULL) |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
302 goto Done; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
303 result = _PyString_Join(s, pieces); |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
304 Py_DECREF(s); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
305 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
306 Done: |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
17971
diff
changeset
|
307 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
|
308 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
|
309 return result; |
| 6 | 310 } |
| 311 | |
| 312 static int | |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
313 list_length(PyListObject *a) |
| 6 | 314 { |
| 315 return a->ob_size; | |
| 316 } | |
| 317 | |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
318 |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
319 |
|
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
320 static int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
321 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
|
322 { |
|
25124
48d108e5b884
Micro-optimization for list_contains. Factored double if test
Raymond Hettinger <python@rcn.com>
parents:
24727
diff
changeset
|
323 int i, cmp; |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
324 |
|
25124
48d108e5b884
Micro-optimization for list_contains. Factored double if test
Raymond Hettinger <python@rcn.com>
parents:
24727
diff
changeset
|
325 for (i = 0, cmp = 0 ; cmp == 0 && i < a->ob_size; ++i) |
|
48d108e5b884
Micro-optimization for list_contains. Factored double if test
Raymond Hettinger <python@rcn.com>
parents:
24727
diff
changeset
|
326 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i), |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
327 Py_EQ); |
|
25128
f1c69c32c42b
PyObject_RichCompareBool() already returns -1, 0, or 1, so return its value
Neal Norwitz <nnorwitz@gmail.com>
parents:
25124
diff
changeset
|
328 return cmp; |
|
12516
e6e914d5b207
add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11999
diff
changeset
|
329 } |
|
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
332 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
333 list_item(PyListObject *a, int i) |
| 6 | 334 { |
| 335 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
|
336 if (indexerr == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
337 indexerr = PyString_FromString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
338 "list index out of range"); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
339 PyErr_SetObject(PyExc_IndexError, indexerr); |
| 6 | 340 return NULL; |
| 341 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
342 Py_INCREF(a->ob_item[i]); |
| 6 | 343 return a->ob_item[i]; |
| 344 } | |
| 345 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
346 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
347 list_slice(PyListObject *a, int ilow, int ihigh) |
| 6 | 348 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
349 PyListObject *np; |
| 6 | 350 int i; |
| 351 if (ilow < 0) | |
| 352 ilow = 0; | |
| 353 else if (ilow > a->ob_size) | |
| 354 ilow = a->ob_size; | |
| 355 if (ihigh < ilow) | |
| 356 ihigh = ilow; | |
| 357 else if (ihigh > a->ob_size) | |
| 358 ihigh = a->ob_size; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
359 np = (PyListObject *) PyList_New(ihigh - ilow); |
| 6 | 360 if (np == NULL) |
| 361 return NULL; | |
| 362 for (i = ilow; i < ihigh; i++) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
363 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
364 Py_INCREF(v); |
| 6 | 365 np->ob_item[i - ilow] = v; |
| 366 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
367 return (PyObject *)np; |
| 6 | 368 } |
| 369 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
370 PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
371 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
|
372 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
373 if (!PyList_Check(a)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
374 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
|
375 return NULL; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
376 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
377 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
|
378 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
379 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
380 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
381 list_concat(PyListObject *a, PyObject *bb) |
| 6 | 382 { |
| 383 int size; | |
| 384 int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
385 PyListObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
386 if (!PyList_Check(bb)) { |
|
12816
4272b368bf69
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12576
diff
changeset
|
387 PyErr_Format(PyExc_TypeError, |
|
12817
5f1f8ddc2a0c
Improve TypeError exception message for list catenation.
Fred Drake <fdrake@acm.org>
parents:
12816
diff
changeset
|
388 "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
|
389 bb->ob_type->tp_name); |
| 6 | 390 return NULL; |
| 391 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
392 #define b ((PyListObject *)bb) |
| 6 | 393 size = a->ob_size + b->ob_size; |
|
25740
70f9026a2b16
Add checks for size overflow on list*n, list+list, tuple+tuple.
Guido van Rossum <guido@python.org>
parents:
25128
diff
changeset
|
394 if (size < 0) |
|
70f9026a2b16
Add checks for size overflow on list*n, list+list, tuple+tuple.
Guido van Rossum <guido@python.org>
parents:
25128
diff
changeset
|
395 return PyErr_NoMemory(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
396 np = (PyListObject *) PyList_New(size); |
| 6 | 397 if (np == NULL) { |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
398 return NULL; |
| 6 | 399 } |
| 400 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
|
401 PyObject *v = a->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
402 Py_INCREF(v); |
| 6 | 403 np->ob_item[i] = v; |
| 404 } | |
| 405 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
|
406 PyObject *v = b->ob_item[i]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
407 Py_INCREF(v); |
| 6 | 408 np->ob_item[i + a->ob_size] = v; |
| 409 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
410 return (PyObject *)np; |
| 6 | 411 #undef b |
| 412 } | |
| 413 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
414 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
415 list_repeat(PyListObject *a, int n) |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
416 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
417 int i, j; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
418 int size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
419 PyListObject *np; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
420 PyObject **p; |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
421 if (n < 0) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
422 n = 0; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
423 size = a->ob_size * n; |
|
25748
f7a46b0d3c07
Darn! Don't divide by zero. Bad fix. :-)
Guido van Rossum <guido@python.org>
parents:
25740
diff
changeset
|
424 if (n && size/n != a->ob_size) |
|
25740
70f9026a2b16
Add checks for size overflow on list*n, list+list, tuple+tuple.
Guido van Rossum <guido@python.org>
parents:
25128
diff
changeset
|
425 return PyErr_NoMemory(); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
426 np = (PyListObject *) PyList_New(size); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
427 if (np == NULL) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
428 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
429 p = np->ob_item; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
430 for (i = 0; i < n; i++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
431 for (j = 0; j < a->ob_size; j++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
432 *p = a->ob_item[j]; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
433 Py_INCREF(*p); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
434 p++; |
|
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 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
437 return (PyObject *) np; |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
438 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
439 |
| 6 | 440 static int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
441 list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) |
| 6 | 442 { |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
443 /* 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
|
444 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
|
445 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
|
446 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
|
447 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
|
448 list. :-( */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
449 PyObject **recycle, **p; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
450 PyObject **item; |
|
26040
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
451 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */ |
| 6 | 452 int n; /* Size of replacement list */ |
| 453 int d; /* Change in size */ | |
| 454 int k; /* Loop index */ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
455 #define b ((PyListObject *)v) |
| 6 | 456 if (v == NULL) |
| 457 n = 0; | |
|
26040
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
458 else { |
|
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
459 char msg[256]; |
|
26064
951407932001
Use PyOS_snprintf() instead of sprintf and wrap the long line
Neal Norwitz <nnorwitz@gmail.com>
parents:
26040
diff
changeset
|
460 PyOS_snprintf(msg, sizeof(msg), |
|
951407932001
Use PyOS_snprintf() instead of sprintf and wrap the long line
Neal Norwitz <nnorwitz@gmail.com>
parents:
26040
diff
changeset
|
461 "must assign sequence" |
|
951407932001
Use PyOS_snprintf() instead of sprintf and wrap the long line
Neal Norwitz <nnorwitz@gmail.com>
parents:
26040
diff
changeset
|
462 " (not \"%.200s\") to slice", |
|
951407932001
Use PyOS_snprintf() instead of sprintf and wrap the long line
Neal Norwitz <nnorwitz@gmail.com>
parents:
26040
diff
changeset
|
463 v->ob_type->tp_name); |
|
26040
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
464 v_as_SF = PySequence_Fast(v, msg); |
|
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
465 if(v_as_SF == NULL) |
|
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
466 return -1; |
|
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
467 n = PySequence_Fast_GET_SIZE(v_as_SF); |
|
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
468 |
|
511
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
469 if (a == b) { |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
470 /* 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
|
471 int ret; |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
472 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
|
473 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
|
474 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
|
475 return ret; |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
476 } |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
477 } |
| 6 | 478 if (ilow < 0) |
| 479 ilow = 0; | |
| 480 else if (ilow > a->ob_size) | |
| 481 ilow = a->ob_size; | |
| 482 if (ihigh < ilow) | |
| 483 ihigh = ilow; | |
| 484 else if (ihigh > a->ob_size) | |
| 485 ihigh = a->ob_size; | |
| 486 item = a->ob_item; | |
| 487 d = n - (ihigh-ilow); | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
488 if (ihigh > ilow) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
489 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
|
490 else |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
491 p = recycle = NULL; |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
492 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ |
| 6 | 493 for (k = ilow; k < ihigh; k++) |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
494 *p++ = item[k]; |
| 6 | 495 if (d < 0) { |
| 496 for (/*k = ihigh*/; k < a->ob_size; k++) | |
| 497 item[k+d] = item[k]; | |
| 498 a->ob_size += d; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
499 NRESIZE(item, PyObject *, a->ob_size); /* Can't fail */ |
| 6 | 500 a->ob_item = item; |
| 501 } | |
| 502 } | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
503 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
|
504 NRESIZE(item, PyObject *, a->ob_size + d); |
| 27 | 505 if (item == NULL) { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
506 if (recycle != NULL) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12516
diff
changeset
|
507 PyMem_DEL(recycle); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
508 PyErr_NoMemory(); |
| 27 | 509 return -1; |
| 510 } | |
| 6 | 511 for (k = a->ob_size; --k >= ihigh; ) |
| 512 item[k+d] = item[k]; | |
| 513 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
|
514 *p++ = item[k]; |
| 6 | 515 a->ob_item = item; |
| 516 a->ob_size += d; | |
| 517 } | |
| 518 for (k = 0; k < n; k++, ilow++) { | |
|
26040
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
519 PyObject *w = PySequence_Fast_GET_ITEM(v_as_SF, k); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
520 Py_XINCREF(w); |
| 6 | 521 item[ilow] = w; |
| 522 } | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
523 if (recycle) { |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
524 while (--p >= recycle) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
525 Py_XDECREF(*p); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
526 PyMem_DEL(recycle); |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
527 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
528 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
|
529 PyMem_FREE(a->ob_item); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
530 a->ob_item = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
531 } |
|
26040
e9aa394c7191
This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents:
25748
diff
changeset
|
532 Py_XDECREF(v_as_SF); |
| 6 | 533 return 0; |
| 534 #undef b | |
| 535 } | |
| 536 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
537 int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
538 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
|
539 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
540 if (!PyList_Check(a)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
541 PyErr_BadInternalCall(); |
|
1330
409ea74feb21
Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents:
1313
diff
changeset
|
542 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
|
543 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
544 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
|
545 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
546 |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
547 static PyObject * |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
548 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
|
549 { |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
550 PyObject **items; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
551 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
|
552 |
|
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 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
|
555 if (size == 0) { |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
556 Py_INCREF(self); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
557 return (PyObject *)self; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
558 } |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
559 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
560 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
|
561 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
562 if (n < 1) { |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
563 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
|
564 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
|
565 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
|
566 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
|
567 PyMem_DEL(items); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
568 Py_INCREF(self); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
569 return (PyObject *)self; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
570 } |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
571 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
572 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
|
573 if (items == NULL) { |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
574 PyErr_NoMemory(); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
575 goto finally; |
|
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 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
|
578 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
|
579 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
|
580 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
|
581 Py_INCREF(o); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
582 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
|
583 } |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
584 } |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
585 Py_INCREF(self); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
586 return (PyObject *)self; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
587 finally: |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
588 return NULL; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
589 } |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
590 |
|
167
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
591 static int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
592 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
|
593 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
594 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
|
595 if (i < 0 || i >= a->ob_size) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
596 PyErr_SetString(PyExc_IndexError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
597 "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
|
598 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
|
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 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
|
601 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
|
602 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
|
603 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
|
604 a->ob_item[i] = v; |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
605 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
|
606 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
|
607 } |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
608 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
609 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
610 ins(PyListObject *self, int where, PyObject *v) |
| 6 | 611 { |
| 612 if (ins1(self, where, v) != 0) | |
| 613 return NULL; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
614 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
615 return Py_None; |
| 6 | 616 } |
| 617 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
618 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
619 listinsert(PyListObject *self, PyObject *args) |
| 6 | 620 { |
| 621 int i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
622 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
|
623 if (!PyArg_ParseTuple(args, "iO:insert", &i, &v)) |
| 6 | 624 return NULL; |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
625 return ins(self, i, v); |
| 6 | 626 } |
| 627 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
628 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
|
629 listappend(PyListObject *self, PyObject *v) |
| 6 | 630 { |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
631 return ins(self, (int) self->ob_size, v); |
| 6 | 632 } |
| 633 | |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
634 static int |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
635 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
|
636 { |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
637 PyObject **items; |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
638 int selflen = PyList_GET_SIZE(self); |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
639 int blen; |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
640 register int i; |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
641 |
| 15707 | 642 if (PyObject_Size(b) == 0) { |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
643 /* short circuit when b is empty */ |
| 15707 | 644 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
|
645 return 0; |
| 15707 | 646 } |
| 12952 | 647 |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
648 if (self == (PyListObject*)b) { |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
649 /* 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
|
650 * situation: a.extend(a) |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
651 * |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
652 * 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
|
653 * 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
|
654 */ |
| 12952 | 655 Py_DECREF(b); |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
656 b = PyList_New(selflen); |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
657 if (!b) |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
658 return -1; |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
659 for (i = 0; i < selflen; i++) { |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
660 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
|
661 Py_INCREF(o); |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
662 PyList_SET_ITEM(b, i, o); |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
663 } |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
664 } |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
665 |
|
13576
61e4c29d53d8
replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13472
diff
changeset
|
666 blen = PyObject_Size(b); |
| 12952 | 667 |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
668 /* resize a using idiom */ |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
669 items = self->ob_item; |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
670 NRESIZE(items, PyObject*, selflen + blen); |
| 12952 | 671 if (items == NULL) { |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
672 PyErr_NoMemory(); |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
673 Py_DECREF(b); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
674 return -1; |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
675 } |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
676 |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
677 self->ob_item = items; |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
678 |
| 12952 | 679 /* 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
|
680 for (i = 0; i < blen; i++) { |
| 12952 | 681 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
|
682 Py_INCREF(o); |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
683 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
|
684 } |
| 12952 | 685 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
|
686 return 0; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
687 } |
|
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 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
690 static PyObject * |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
691 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
|
692 { |
|
17971
73fcc4f4a182
Change list.extend() error msgs and NEWS to reflect that list.extend()
Tim Peters <tim.peters@gmail.com>
parents:
17970
diff
changeset
|
693 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
|
694 if (!other) |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
695 return NULL; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
696 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
697 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
|
698 return NULL; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
699 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
700 Py_INCREF(self); |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
701 return (PyObject *)self; |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
702 } |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
703 |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
704 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
|
705 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
|
706 { |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
707 |
|
17971
73fcc4f4a182
Change list.extend() error msgs and NEWS to reflect that list.extend()
Tim Peters <tim.peters@gmail.com>
parents:
17970
diff
changeset
|
708 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
|
709 if (!b) |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
710 return NULL; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
711 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
712 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
|
713 return NULL; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
714 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
715 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
|
716 return Py_None; |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14125
diff
changeset
|
717 } |
|
9317
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
718 |
|
003b42c9f24d
listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents:
9275
diff
changeset
|
719 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
720 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
|
721 { |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
722 int i = -1; |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
723 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
|
724 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
|
725 return NULL; |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
726 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
|
727 /* 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
|
728 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
|
729 return NULL; |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
730 } |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
731 if (i < 0) |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
732 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
|
733 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
|
734 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
|
735 return NULL; |
|
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 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
|
738 Py_INCREF(v); |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
739 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
|
740 Py_DECREF(v); |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
741 return NULL; |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
742 } |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
743 return v; |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
744 } |
|
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
745 |
|
24309
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
746 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */ |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
747 static void |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
748 reverse_slice(PyObject **lo, PyObject **hi) |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
749 { |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
750 assert(lo && hi); |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
751 |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
752 --hi; |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
753 while (lo < hi) { |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
754 PyObject *t = *lo; |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
755 *lo = *hi; |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
756 *hi = t; |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
757 ++lo; |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
758 --hi; |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
759 } |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
760 } |
|
08d307cbccd9
Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents:
24280
diff
changeset
|
761 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
762 /* Lots of code for an adaptive, stable, natural mergesort. There are many |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
763 * pieces to this algorithm; read listsort.txt for overviews and details. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
764 */ |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
765 |
|
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
766 /* Comparison function. Takes care of calling a user-supplied |
|
24540
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
767 * comparison function (any callable Python object), which must not be |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
768 * NULL (use the ISLT macro if you don't know, or call PyObject_RichCompareBool |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
769 * with Py_LT if you know it's NULL). |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
770 * Returns -1 on error, 1 if x < y, 0 if x >= y. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
771 */ |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
772 static int |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
773 islt(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
|
774 { |
|
24207
561e33a6a313
docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents:
23929
diff
changeset
|
775 PyObject *res; |
|
561e33a6a313
docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents:
23929
diff
changeset
|
776 PyObject *args; |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
777 int i; |
|
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
778 |
|
24540
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
779 assert(compare != NULL); |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
780 /* Call the user's comparison function and translate the 3-way |
|
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
781 * result into true or false (or error). |
|
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
782 */ |
|
24207
561e33a6a313
docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents:
23929
diff
changeset
|
783 args = PyTuple_New(2); |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
784 if (args == NULL) |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
785 return -1; |
|
24207
561e33a6a313
docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents:
23929
diff
changeset
|
786 Py_INCREF(x); |
|
561e33a6a313
docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents:
23929
diff
changeset
|
787 Py_INCREF(y); |
|
561e33a6a313
docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents:
23929
diff
changeset
|
788 PyTuple_SET_ITEM(args, 0, x); |
|
561e33a6a313
docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents:
23929
diff
changeset
|
789 PyTuple_SET_ITEM(args, 1, y); |
|
24236
2be63e20a498
docompare(): Another reasonable optimization from Jonathan Hogg for the
Tim Peters <tim.peters@gmail.com>
parents:
24230
diff
changeset
|
790 res = PyObject_Call(compare, args, NULL); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
791 Py_DECREF(args); |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
792 if (res == NULL) |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
793 return -1; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
794 if (!PyInt_Check(res)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
795 Py_DECREF(res); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
796 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
|
797 "comparison function must return int"); |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
798 return -1; |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
799 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
800 i = PyInt_AsLong(res); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
801 Py_DECREF(res); |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
802 return i < 0; |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
803 } |
|
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
804 |
|
24540
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
805 /* If COMPARE is NULL, calls PyObject_RichCompareBool with Py_LT, else calls |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
806 * islt. This avoids a layer of function call in the usual case, and |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
807 * sorting does many comparisons. |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
808 * Returns -1 on error, 1 if x < y, 0 if x >= y. |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
809 */ |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
810 #define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ? \ |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
811 PyObject_RichCompareBool(X, Y, Py_LT) : \ |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
812 islt(X, Y, COMPARE)) |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
813 |
|
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
814 /* Compare X to Y via "<". Goto "fail" if the comparison raises an |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
815 error. Else "k" is set to true iff X<Y, and an "if (k)" block is |
|
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
816 started. It makes more sense in context <wink>. X and Y are PyObject*s. |
|
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
817 */ |
|
24540
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
818 #define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail; \ |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
819 if (k) |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
820 |
|
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 /* 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
|
822 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
|
823 elements. |
|
8515
16b3a121ff77
Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents:
8511
diff
changeset
|
824 [lo, hi) is a contiguous slice of a list, and is sorted via |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
825 binary insertion. This sort is stable. |
|
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
|
826 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
|
827 sorted (pass start == lo if you don't know!). |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
828 If islt() complains return -1, else 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
|
829 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
|
830 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
|
831 */ |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
832 static int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
833 binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare) |
|
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
834 /* 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
|
835 { |
|
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
|
836 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
|
837 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
|
838 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
|
839 |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
840 assert(lo <= start && start <= hi); |
|
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
841 /* assert [lo, start) is sorted */ |
|
8511
69662f16a838
Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents:
8457
diff
changeset
|
842 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
|
843 ++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
|
844 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
|
845 /* 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
|
846 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
|
847 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
|
848 pivot = *r; |
|
24313
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
849 /* Invariants: |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
850 * pivot >= all in [lo, l). |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
851 * pivot < all in [r, start). |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
852 * The second is vacuously true at the start. |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
853 */ |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
854 assert(l < r); |
|
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
|
855 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
|
856 p = l + ((r - l) >> 1); |
|
24311
a353d6a45276
Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents:
24310
diff
changeset
|
857 IFLT(pivot, *p) |
|
8511
69662f16a838
Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents:
8457
diff
changeset
|
858 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
|
859 else |
|
24313
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
860 l = p+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
|
861 } while (l < r); |
|
24313
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
862 assert(l == r); |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
863 /* The invariants still hold, so pivot >= all in [lo, l) and |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
864 pivot < all in [l, start), so pivot belongs at l. Note |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
865 that if there are elements equal to pivot, l points to the |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
866 first slot after them -- that's why this sort is stable. |
|
658e12cc0336
binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents:
24312
diff
changeset
|
867 Slide over to make room. |
|
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
|
868 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
|
869 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
|
870 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
|
871 *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
|
872 *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
|
873 } |
|
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 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
|
875 |
|
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 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
|
877 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
|
878 } |
|
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 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
880 /* |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
881 Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
882 is required on entry. "A run" is the longest ascending sequence, with |
|
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 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
884 lo[0] <= lo[1] <= lo[2] <= ... |
|
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
|
885 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
886 or the longest descending sequence, with |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
887 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
888 lo[0] > lo[1] > lo[2] > ... |
|
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
|
889 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
890 Boolean *descending is set to 0 in the former case, or to 1 in the latter. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
891 For its intended use in a stable mergesort, the strictness of the defn of |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
892 "descending" is needed so that the caller can safely reverse a descending |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
893 sequence without violating stability (strict > ensures there are no equal |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
894 elements to get out of order). |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
895 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
896 Returns -1 in case of error. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
897 */ |
|
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
|
898 static int |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
899 count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending) |
|
8511
69662f16a838
Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents:
8457
diff
changeset
|
900 { |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
901 int k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
902 int n; |
|
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
|
903 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
904 assert(lo < hi); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
905 *descending = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
906 ++lo; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
907 if (lo == hi) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
908 return 1; |
|
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
|
909 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
910 n = 2; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
911 IFLT(*lo, *(lo-1)) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
912 *descending = 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
913 for (lo = lo+1; lo < hi; ++lo, ++n) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
914 IFLT(*lo, *(lo-1)) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
915 ; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
916 else |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
917 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
918 } |
|
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
|
919 } |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
920 else { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
921 for (lo = lo+1; lo < hi; ++lo, ++n) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
922 IFLT(*lo, *(lo-1)) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
923 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
|
924 } |
|
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 } |
|
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 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
927 return n; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
928 fail: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
929 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
930 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
931 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
932 /* |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
933 Locate the proper position of key in a sorted vector; if the vector contains |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
934 an element equal to key, return the position immediately to the left of |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
935 the leftmost equal element. [gallop_right() does the same except returns |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
936 the position to the right of the rightmost equal element (if any).] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
937 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
938 "a" is a sorted vector with n elements, starting at a[0]. n must be > 0. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
939 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
940 "hint" is an index at which to begin the search, 0 <= hint < n. The closer |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
941 hint is to the final result, the faster this runs. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
942 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
943 The return value is the int k in 0..n such that |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
944 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
945 a[k-1] < key <= a[k] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
946 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
947 pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW, |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
948 key belongs at index k; or, IOW, the first k elements of a should precede |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
949 key, and the last n-k should follow key. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
950 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
951 Returns -1 on error. See listsort.txt for info on the method. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
952 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
953 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
954 gallop_left(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
955 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
956 int ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
957 int lastofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
958 int k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
959 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
960 assert(key && a && n > 0 && hint >= 0 && hint < n); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
961 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
962 a += hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
963 lastofs = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
964 ofs = 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
965 IFLT(*a, key) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
966 /* a[hint] < key -- gallop right, until |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
967 * a[hint + lastofs] < key <= a[hint + ofs] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
968 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
969 const int maxofs = n - hint; /* &a[n-1] is highest */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
970 while (ofs < maxofs) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
971 IFLT(a[ofs], key) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
972 lastofs = ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
973 ofs = (ofs << 1) + 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
974 if (ofs <= 0) /* int overflow */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
975 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
976 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
977 else /* key <= a[hint + ofs] */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
978 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
979 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
980 if (ofs > maxofs) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
981 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
982 /* Translate back to offsets relative to &a[0]. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
983 lastofs += hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
984 ofs += hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
985 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
986 else { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
987 /* key <= a[hint] -- gallop left, until |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
988 * a[hint - ofs] < key <= a[hint - lastofs] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
989 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
990 const int maxofs = hint + 1; /* &a[0] is lowest */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
991 while (ofs < maxofs) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
992 IFLT(*(a-ofs), key) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
993 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
994 /* key <= a[hint - ofs] */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
995 lastofs = ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
996 ofs = (ofs << 1) + 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
997 if (ofs <= 0) /* int overflow */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
998 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
999 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1000 if (ofs > maxofs) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1001 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1002 /* Translate back to positive offsets relative to &a[0]. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1003 k = lastofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1004 lastofs = hint - ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1005 ofs = hint - k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1006 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1007 a -= hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1008 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1009 assert(-1 <= lastofs && lastofs < ofs && ofs <= n); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1010 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1011 * right of lastofs but no farther right than ofs. Do a binary |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1012 * search, with invariant a[lastofs-1] < key <= a[ofs]. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1013 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1014 ++lastofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1015 while (lastofs < ofs) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1016 int m = lastofs + ((ofs - lastofs) >> 1); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1017 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1018 IFLT(a[m], key) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1019 lastofs = m+1; /* a[m] < key */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1020 else |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1021 ofs = m; /* key <= a[m] */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1022 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1023 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1024 return ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1025 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1026 fail: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1027 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1028 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1029 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1030 /* |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1031 Exactly like gallop_left(), except that if key already exists in a[0:n], |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1032 finds the position immediately to the right of the rightmost equal value. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1033 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1034 The return value is the int k in 0..n such that |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1035 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1036 a[k-1] <= key < a[k] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1037 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1038 or -1 if error. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1039 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1040 The code duplication is massive, but this is enough different given that |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1041 we're sticking to "<" comparisons that it's much harder to follow if |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1042 written as one routine with yet another "left or right?" flag. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1043 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1044 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1045 gallop_right(PyObject *key, PyObject **a, int n, int hint, PyObject *compare) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1046 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1047 int ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1048 int lastofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1049 int k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1050 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1051 assert(key && a && n > 0 && hint >= 0 && hint < n); |
|
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
|
1052 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1053 a += hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1054 lastofs = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1055 ofs = 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1056 IFLT(key, *a) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1057 /* key < a[hint] -- gallop left, until |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1058 * a[hint - ofs] <= key < a[hint - lastofs] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1059 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1060 const int maxofs = hint + 1; /* &a[0] is lowest */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1061 while (ofs < maxofs) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1062 IFLT(key, *(a-ofs)) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1063 lastofs = ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1064 ofs = (ofs << 1) + 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1065 if (ofs <= 0) /* int overflow */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1066 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1067 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1068 else /* a[hint - ofs] <= key */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1069 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1070 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1071 if (ofs > maxofs) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1072 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1073 /* Translate back to positive offsets relative to &a[0]. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1074 k = lastofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1075 lastofs = hint - ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1076 ofs = hint - k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1077 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1078 else { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1079 /* a[hint] <= key -- gallop right, until |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1080 * a[hint + lastofs] <= key < a[hint + ofs] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1081 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1082 const int maxofs = n - hint; /* &a[n-1] is highest */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1083 while (ofs < maxofs) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1084 IFLT(key, a[ofs]) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1085 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1086 /* a[hint + ofs] <= key */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1087 lastofs = ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1088 ofs = (ofs << 1) + 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1089 if (ofs <= 0) /* int overflow */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1090 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1091 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1092 if (ofs > maxofs) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1093 ofs = maxofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1094 /* Translate back to offsets relative to &a[0]. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1095 lastofs += hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1096 ofs += hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1097 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1098 a -= hint; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1099 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1100 assert(-1 <= lastofs && lastofs < ofs && ofs <= n); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1101 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1102 * right of lastofs but no farther right than ofs. Do a binary |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1103 * search, with invariant a[lastofs-1] <= key < a[ofs]. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1104 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1105 ++lastofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1106 while (lastofs < ofs) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1107 int m = lastofs + ((ofs - lastofs) >> 1); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1108 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1109 IFLT(key, a[m]) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1110 ofs = m; /* key < a[m] */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1111 else |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1112 lastofs = m+1; /* a[m] <= key */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1113 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1114 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1115 return ofs; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1116 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1117 fail: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1118 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1119 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1120 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1121 /* The maximum number of entries in a MergeState's pending-runs stack. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1122 * This is enough to sort arrays of size up to about |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1123 * 32 * phi ** MAX_MERGE_PENDING |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1124 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1125 * with 2**64 elements. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1126 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1127 #define MAX_MERGE_PENDING 85 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1128 |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1129 /* When we get into galloping mode, we stay there until both runs win less |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1130 * often than MIN_GALLOP consecutive times. See listsort.txt for more info. |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1131 */ |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1132 #define MIN_GALLOP 7 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1133 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1134 /* Avoid malloc for small temp arrays. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1135 #define MERGESTATE_TEMP_SIZE 256 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1136 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1137 /* One MergeState exists on the stack per invocation of mergesort. It's just |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1138 * a convenient way to pass state around among the helper functions. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1139 */ |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1140 struct s_slice { |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1141 PyObject **base; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1142 int len; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1143 }; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1144 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1145 typedef struct s_MergeState { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1146 /* The user-supplied comparison function. or NULL if none given. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1147 PyObject *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
|
1148 |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1149 /* This controls when we get *into* galloping mode. It's initialized |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1150 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1151 * random data, and lower for highly structured data. |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1152 */ |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1153 int min_gallop; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1154 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1155 /* 'a' is temp storage to help with merges. It contains room for |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1156 * alloced entries. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1157 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1158 PyObject **a; /* may point to temparray below */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1159 int alloced; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1160 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1161 /* A stack of n pending runs yet to be merged. Run #i starts at |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1162 * address base[i] and extends for len[i] elements. It's always |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1163 * true (so long as the indices are in bounds) that |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1164 * |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1165 * pending[i].base + pending[i].len == pending[i+1].base |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1166 * |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1167 * so we could cut the storage for this, but it's a minor amount, |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1168 * and keeping all the info explicit simplifies the code. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1169 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1170 int n; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1171 struct s_slice pending[MAX_MERGE_PENDING]; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1172 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1173 /* 'a' points to this when possible, rather than muck with malloc. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1174 PyObject *temparray[MERGESTATE_TEMP_SIZE]; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1175 } MergeState; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1176 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1177 /* Conceptually a MergeState's constructor. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1178 static void |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1179 merge_init(MergeState *ms, PyObject *compare) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1180 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1181 assert(ms != NULL); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1182 ms->compare = compare; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1183 ms->a = ms->temparray; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1184 ms->alloced = MERGESTATE_TEMP_SIZE; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1185 ms->n = 0; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1186 ms->min_gallop = MIN_GALLOP; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1187 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1188 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1189 /* Free all the temp memory owned by the MergeState. This must be called |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1190 * when you're done with a MergeState, and may be called before then if |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1191 * you want to free the temp memory early. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1192 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1193 static void |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1194 merge_freemem(MergeState *ms) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1195 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1196 assert(ms != NULL); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1197 if (ms->a != ms->temparray) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1198 PyMem_Free(ms->a); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1199 ms->a = ms->temparray; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1200 ms->alloced = MERGESTATE_TEMP_SIZE; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1201 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1202 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1203 /* Ensure enough temp memory for 'need' array slots is available. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1204 * Returns 0 on success and -1 if the memory can't be gotten. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1205 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1206 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1207 merge_getmem(MergeState *ms, int need) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1208 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1209 assert(ms != NULL); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1210 if (need <= ms->alloced) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1211 return 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1212 /* Don't realloc! That can cost cycles to copy the old data, but |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1213 * we don't care what's in the block. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1214 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1215 merge_freemem(ms); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1216 ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1217 if (ms->a) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1218 ms->alloced = need; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1219 return 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1220 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1221 PyErr_NoMemory(); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1222 merge_freemem(ms); /* reset to sane state */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1223 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1224 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1225 #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1226 merge_getmem(MS, NEED)) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1227 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1228 /* Merge the na elements starting at pa with the nb elements starting at pb |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1229 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1230 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1231 * merge, and should have na <= nb. See listsort.txt for more info. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1232 * Return 0 if successful, -1 if error. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1233 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1234 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1235 merge_lo(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1236 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1237 int k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1238 PyObject *compare; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1239 PyObject **dest; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1240 int result = -1; /* guilty until proved innocent */ |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1241 int min_gallop = ms->min_gallop; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1242 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1243 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1244 if (MERGE_GETMEM(ms, na) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1245 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1246 memcpy(ms->a, pa, na * sizeof(PyObject*)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1247 dest = pa; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1248 pa = ms->a; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1249 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1250 *dest++ = *pb++; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1251 --nb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1252 if (nb == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1253 goto Succeed; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1254 if (na == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1255 goto CopyB; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1256 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1257 compare = ms->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
|
1258 for (;;) { |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1259 int acount = 0; /* # of times A won in a row */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1260 int bcount = 0; /* # of times B won in a row */ |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1261 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1262 /* Do the straightforward thing until (if ever) one run |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1263 * appears to win consistently. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1264 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1265 for (;;) { |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1266 assert(na > 1 && nb > 0); |
|
24540
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
1267 k = ISLT(*pb, *pa, compare); |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1268 if (k) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1269 if (k < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1270 goto Fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1271 *dest++ = *pb++; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1272 ++bcount; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1273 acount = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1274 --nb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1275 if (nb == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1276 goto Succeed; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1277 if (bcount >= min_gallop) |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1278 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
|
1279 } |
|
69662f16a838
Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents:
8457
diff
changeset
|
1280 else { |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1281 *dest++ = *pa++; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1282 ++acount; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1283 bcount = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1284 --na; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1285 if (na == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1286 goto CopyB; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1287 if (acount >= min_gallop) |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1288 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1289 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1290 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1291 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1292 /* One run is winning so consistently that galloping may |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1293 * be a huge win. So try that, and continue galloping until |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1294 * (if ever) neither run appears to be winning consistently |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1295 * anymore. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1296 */ |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1297 ++min_gallop; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1298 do { |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1299 assert(na > 1 && nb > 0); |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1300 min_gallop -= min_gallop > 1; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1301 ms->min_gallop = min_gallop; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1302 k = gallop_right(*pb, pa, na, 0, compare); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1303 acount = k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1304 if (k) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1305 if (k < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1306 goto Fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1307 memcpy(dest, pa, k * sizeof(PyObject *)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1308 dest += k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1309 pa += k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1310 na -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1311 if (na == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1312 goto CopyB; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1313 /* na==0 is impossible now if the comparison |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1314 * function is consistent, but we can't assume |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1315 * that it is. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1316 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1317 if (na == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1318 goto Succeed; |
|
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
|
1319 } |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1320 *dest++ = *pb++; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1321 --nb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1322 if (nb == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1323 goto Succeed; |
|
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
|
1324 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1325 k = gallop_left(*pa, pb, nb, 0, compare); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1326 bcount = k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1327 if (k) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1328 if (k < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1329 goto Fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1330 memmove(dest, pb, k * sizeof(PyObject *)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1331 dest += k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1332 pb += k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1333 nb -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1334 if (nb == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1335 goto Succeed; |
|
8428
3e653e204ad7
Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents:
8323
diff
changeset
|
1336 } |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1337 *dest++ = *pa++; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1338 --na; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1339 if (na == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1340 goto CopyB; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1341 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1342 ++min_gallop; /* penalize it for leaving galloping mode */ |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1343 ms->min_gallop = min_gallop; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1344 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1345 Succeed: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1346 result = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1347 Fail: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1348 if (na) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1349 memcpy(dest, pa, na * sizeof(PyObject*)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1350 return result; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1351 CopyB: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1352 assert(na == 1 && nb > 0); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1353 /* The last element of pa belongs at the end of the merge. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1354 memmove(dest, pb, nb * sizeof(PyObject *)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1355 dest[nb] = *pa; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1356 return 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1357 } |
|
6814
0fe3dc28b92c
Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents:
6154
diff
changeset
|
1358 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1359 /* Merge the na elements starting at pa with the nb elements starting at pb |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1360 * in a stable way, in-place. na and nb must be > 0, and pa + na == pb. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1361 * Must also have that *pb < *pa, that pa[na-1] belongs at the end of the |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1362 * merge, and should have na >= nb. See listsort.txt for more info. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1363 * Return 0 if successful, -1 if error. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1364 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1365 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1366 merge_hi(MergeState *ms, PyObject **pa, int na, PyObject **pb, int nb) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1367 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1368 int k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1369 PyObject *compare; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1370 PyObject **dest; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1371 int result = -1; /* guilty until proved innocent */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1372 PyObject **basea; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1373 PyObject **baseb; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1374 int min_gallop = ms->min_gallop; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1375 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1376 assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1377 if (MERGE_GETMEM(ms, nb) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1378 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1379 dest = pb + nb - 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1380 memcpy(ms->a, pb, nb * sizeof(PyObject*)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1381 basea = pa; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1382 baseb = ms->a; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1383 pb = ms->a + nb - 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1384 pa += na - 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1385 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1386 *dest-- = *pa--; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1387 --na; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1388 if (na == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1389 goto Succeed; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1390 if (nb == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1391 goto CopyA; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1392 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1393 compare = ms->compare; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1394 for (;;) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1395 int acount = 0; /* # of times A won in a row */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1396 int bcount = 0; /* # of times B won in a row */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1397 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1398 /* Do the straightforward thing until (if ever) one run |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1399 * appears to win consistently. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1400 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1401 for (;;) { |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1402 assert(na > 0 && nb > 1); |
|
24540
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
1403 k = ISLT(*pb, *pa, compare); |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1404 if (k) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1405 if (k < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1406 goto Fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1407 *dest-- = *pa--; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1408 ++acount; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1409 bcount = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1410 --na; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1411 if (na == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1412 goto Succeed; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1413 if (acount >= min_gallop) |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1414 break; |
|
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
|
1415 } |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1416 else { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1417 *dest-- = *pb--; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1418 ++bcount; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1419 acount = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1420 --nb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1421 if (nb == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1422 goto CopyA; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1423 if (bcount >= min_gallop) |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1424 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1425 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1426 } |
|
8164
ea59bb921380
Quicksort retuned by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
7986
diff
changeset
|
1427 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1428 /* One run is winning so consistently that galloping may |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1429 * be a huge win. So try that, and continue galloping until |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1430 * (if ever) neither run appears to be winning consistently |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1431 * anymore. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1432 */ |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1433 ++min_gallop; |
|
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
|
1434 do { |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1435 assert(na > 0 && nb > 1); |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1436 min_gallop -= min_gallop > 1; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1437 ms->min_gallop = min_gallop; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1438 k = gallop_right(*pb, basea, na, na-1, compare); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1439 if (k < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1440 goto Fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1441 k = na - k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1442 acount = k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1443 if (k) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1444 dest -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1445 pa -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1446 memmove(dest+1, pa+1, k * sizeof(PyObject *)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1447 na -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1448 if (na == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1449 goto Succeed; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1450 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1451 *dest-- = *pb--; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1452 --nb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1453 if (nb == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1454 goto CopyA; |
|
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
|
1455 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1456 k = gallop_left(*pa, baseb, nb, nb-1, compare); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1457 if (k < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1458 goto Fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1459 k = nb - k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1460 bcount = k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1461 if (k) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1462 dest -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1463 pb -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1464 memcpy(dest+1, pb+1, k * sizeof(PyObject *)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1465 nb -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1466 if (nb == 1) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1467 goto CopyA; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1468 /* nb==0 is impossible now if the comparison |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1469 * function is consistent, but we can't assume |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1470 * that it is. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1471 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1472 if (nb == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1473 goto Succeed; |
|
6814
0fe3dc28b92c
Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents:
6154
diff
changeset
|
1474 } |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1475 *dest-- = *pa--; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1476 --na; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1477 if (na == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1478 goto Succeed; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1479 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP); |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1480 ++min_gallop; /* penalize it for leaving galloping mode */ |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1481 ms->min_gallop = min_gallop; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1482 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1483 Succeed: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1484 result = 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1485 Fail: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1486 if (nb) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1487 memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1488 return result; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1489 CopyA: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1490 assert(nb == 1 && na > 0); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1491 /* The first element of pb belongs at the front of the merge. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1492 dest -= na; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1493 pa -= na; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1494 memmove(dest+1, pa+1, na * sizeof(PyObject *)); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1495 *dest = *pb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1496 return 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1497 } |
|
6814
0fe3dc28b92c
Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents:
6154
diff
changeset
|
1498 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1499 /* Merge the two runs at stack indices i and i+1. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1500 * Returns 0 on success, -1 on error. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1501 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1502 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1503 merge_at(MergeState *ms, int i) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1504 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1505 PyObject **pa, **pb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1506 int na, nb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1507 int k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1508 PyObject *compare; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1509 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1510 assert(ms != NULL); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1511 assert(ms->n >= 2); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1512 assert(i >= 0); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1513 assert(i == ms->n - 2 || i == ms->n - 3); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1514 |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1515 pa = ms->pending[i].base; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1516 na = ms->pending[i].len; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1517 pb = ms->pending[i+1].base; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1518 nb = ms->pending[i+1].len; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1519 assert(na > 0 && nb > 0); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1520 assert(pa + na == pb); |
|
8322
e6ef0e39e35c
Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents:
8186
diff
changeset
|
1521 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1522 /* Record the length of the combined runs; if i is the 3rd-last |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1523 * run now, also slide over the last run (which isn't involved |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1524 * in this merge). The current run i+1 goes away in any case. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1525 */ |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1526 ms->pending[i].len = na + nb; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1527 if (i == ms->n - 3) |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1528 ms->pending[i+1] = ms->pending[i+2]; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1529 --ms->n; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1530 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1531 /* Where does b start in a? Elements in a before that can be |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1532 * ignored (already in place). |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1533 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1534 compare = ms->compare; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1535 k = gallop_right(*pb, pa, na, 0, compare); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1536 if (k < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1537 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1538 pa += k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1539 na -= k; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1540 if (na == 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1541 return 0; |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1542 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1543 /* Where does a end in b? Elements in b after that can be |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1544 * ignored (already in place). |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1545 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1546 nb = gallop_left(pa[na-1], pb, nb, nb-1, compare); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1547 if (nb <= 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1548 return nb; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1549 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1550 /* Merge what remains of the runs, using a temp array with |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1551 * min(na, nb) elements. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1552 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1553 if (na <= nb) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1554 return merge_lo(ms, pa, na, pb, nb); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1555 else |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1556 return merge_hi(ms, pa, na, pb, nb); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1557 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1558 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1559 /* Examine the stack of runs waiting to be merged, merging adjacent runs |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1560 * until the stack invariants are re-established: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1561 * |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1562 * 1. len[-3] > len[-2] + len[-1] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1563 * 2. len[-2] > len[-1] |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1564 * |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1565 * See listsort.txt for more info. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1566 * |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1567 * Returns 0 on success, -1 on error. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1568 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1569 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1570 merge_collapse(MergeState *ms) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1571 { |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1572 struct s_slice *p = ms->pending; |
|
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
|
1573 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1574 assert(ms); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1575 while (ms->n > 1) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1576 int n = ms->n - 2; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1577 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) { |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1578 if (p[n-1].len < p[n+1].len) |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1579 --n; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1580 if (merge_at(ms, n) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1581 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1582 } |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1583 else if (p[n].len <= p[n+1].len) { |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1584 if (merge_at(ms, n) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1585 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
|
1586 } |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1587 else |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1588 break; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1589 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1590 return 0; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1591 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1592 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1593 /* Regardless of invariants, merge all runs on the stack until only one |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1594 * remains. This is used at the end of the mergesort. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1595 * |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1596 * Returns 0 on success, -1 on error. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1597 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1598 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1599 merge_force_collapse(MergeState *ms) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1600 { |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1601 struct s_slice *p = ms->pending; |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1602 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1603 assert(ms); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1604 while (ms->n > 1) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1605 int n = ms->n - 2; |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1606 if (n > 0 && p[n-1].len < p[n+1].len) |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1607 --n; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1608 if (merge_at(ms, n) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1609 return -1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1610 } |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1611 return 0; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1612 } |
|
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
|
1613 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1614 /* Compute a good value for the minimum run length; natural runs shorter |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1615 * than this are boosted artificially via binary insertion. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1616 * |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1617 * If n < 64, return n (it's too small to bother with fancy stuff). |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1618 * Else if n is an exact power of 2, return 32. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1619 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1620 * strictly less than, an exact power of 2. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1621 * |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1622 * See listsort.txt for more info. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1623 */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1624 static int |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1625 merge_compute_minrun(int n) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1626 { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1627 int r = 0; /* becomes 1 if any 1 bits are shifted off */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1628 |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1629 assert(n >= 0); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1630 while (n >= 64) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1631 r |= n & 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1632 n >>= 1; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1633 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1634 return n + r; |
|
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
|
1635 } |
|
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
|
1636 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1637 /* An adaptive, stable, natural mergesort. See listsort.txt. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1638 * Returns Py_None on success, NULL on error. Even in case of error, the |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1639 * list will be some permutation of its input state (nothing is lost or |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1640 * duplicated). |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1641 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1642 static PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
1643 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
|
1644 { |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1645 MergeState ms; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1646 PyObject **lo, **hi; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1647 int nremaining; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1648 int minrun; |
|
26161
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1649 int saved_ob_size; |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1650 PyObject **saved_ob_item; |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1651 PyObject **empty_ob_item; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1652 PyObject *compare = NULL; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1653 PyObject *result = NULL; /* guilty until proved innocent */ |
|
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
|
1654 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1655 assert(self != NULL); |
|
11846
8b70eaaadf4b
Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1656 if (args != NULL) { |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26816
diff
changeset
|
1657 if (!PyArg_UnpackTuple(args, "sort", 0, 1, &compare)) |
|
11846
8b70eaaadf4b
Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1658 return NULL; |
|
8b70eaaadf4b
Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1659 } |
|
27036
41f7726e3e51
Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents:
26818
diff
changeset
|
1660 if (compare == Py_None) |
|
41f7726e3e51
Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents:
26818
diff
changeset
|
1661 compare = NULL; |
|
41f7726e3e51
Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents:
26818
diff
changeset
|
1662 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1663 merge_init(&ms, compare); |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1664 |
|
26161
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1665 /* The list is temporarily made empty, so that mutations performed |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1666 * by comparison functions can't affect the slice of memory we're |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1667 * sorting (allowing mutations during sorting is a core-dump |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1668 * factory, since ob_item may change). |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1669 */ |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1670 saved_ob_size = self->ob_size; |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1671 saved_ob_item = self->ob_item; |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1672 self->ob_size = 0; |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1673 self->ob_item = empty_ob_item = PyMem_NEW(PyObject *, 0); |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1674 |
|
26161
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1675 nremaining = saved_ob_size; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1676 if (nremaining < 2) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1677 goto succeed; |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1678 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1679 /* March over the array once, left to right, finding natural runs, |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1680 * and extending short natural runs to minrun elements. |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1681 */ |
|
26161
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1682 lo = saved_ob_item; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1683 hi = lo + nremaining; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1684 minrun = merge_compute_minrun(nremaining); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1685 do { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1686 int descending; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1687 int n; |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1688 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1689 /* Identify next run. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1690 n = count_run(lo, hi, compare, &descending); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1691 if (n < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1692 goto fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1693 if (descending) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1694 reverse_slice(lo, lo + n); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1695 /* If short, extend to min(minrun, nremaining). */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1696 if (n < minrun) { |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1697 const int force = nremaining <= minrun ? |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1698 nremaining : minrun; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1699 if (binarysort(lo, lo + force, lo + n, compare) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1700 goto fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1701 n = force; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1702 } |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1703 /* Push run onto pending-runs stack, and maybe merge. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1704 assert(ms.n < MAX_MERGE_PENDING); |
|
24727
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1705 ms.pending[ms.n].base = lo; |
|
b5640e74aa5c
1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents:
24657
diff
changeset
|
1706 ms.pending[ms.n].len = n; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1707 ++ms.n; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1708 if (merge_collapse(&ms) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1709 goto fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1710 /* Advance to find next run. */ |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1711 lo += n; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1712 nremaining -= n; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1713 } while (nremaining); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1714 assert(lo == hi); |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1715 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1716 if (merge_force_collapse(&ms) < 0) |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1717 goto fail; |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1718 assert(ms.n == 1); |
|
26161
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1719 assert(ms.pending[0].base == saved_ob_item); |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1720 assert(ms.pending[0].len == saved_ob_size); |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1721 |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1722 succeed: |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1723 result = Py_None; |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1724 fail: |
|
26161
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1725 if (self->ob_item != empty_ob_item || self->ob_size) { |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1726 /* The user mucked with the list during the sort. */ |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1727 (void)list_ass_slice(self, 0, self->ob_size, (PyObject *)NULL); |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1728 if (result != NULL) { |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1729 PyErr_SetString(PyExc_ValueError, |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1730 "list modified during sort"); |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1731 result = NULL; |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1732 } |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1733 } |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1734 if (self->ob_item == empty_ob_item) |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1735 PyMem_FREE(empty_ob_item); |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1736 self->ob_size = saved_ob_size; |
|
dd10d97d329a
SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents:
26064
diff
changeset
|
1737 self->ob_item = saved_ob_item; |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1738 merge_freemem(&ms); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1739 Py_XINCREF(result); |
|
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
1740 return result; |
|
4673
a3cdc8164990
Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
1741 } |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1742 #undef IFLT |
|
24540
614358e3c4f6
Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents:
24515
diff
changeset
|
1743 #undef ISLT |
|
24316
dd38291b89fb
More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents:
24313
diff
changeset
|
1744 |
|
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 int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
1746 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
|
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 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
|
1749 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
|
1750 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
|
1751 } |
|
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 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
|
1753 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
|
1754 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
|
1755 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
|
1756 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
|
1757 } |
|
69662f16a838
Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents:
8457
diff
changeset
|
1758 |
|
16581
c0cdf82599e0
Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents:
15969
diff
changeset
|
1759 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
|
1760 listreverse(PyListObject *self) |
|
16581
c0cdf82599e0
Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents:
15969
diff
changeset
|
1761 { |
|
24312
c4b3a2b247ac
listreverse(): Don't call the new reverse_slice unless the list
Tim Peters <tim.peters@gmail.com>
parents:
24311
diff
changeset
|
1762 if (self->ob_size > 1) |
|
c4b3a2b247ac
listreverse(): Don't call the new reverse_slice unless the list
Tim Peters <tim.peters@gmail.com>
parents:
24311
diff
changeset
|
1763 reverse_slice(self->ob_item, self->ob_item + self->ob_size); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1764 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1765 return Py_None; |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1766 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1767 |
|
47
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
1768 int |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
1769 PyList_Reverse(PyObject *v) |
|
2159
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
1770 { |
|
24657
955914b0a4d6
PyList_Reverse(): This was leaking a reference to Py_None on every call.
Tim Peters <tim.peters@gmail.com>
parents:
24540
diff
changeset
|
1771 PyListObject *self = (PyListObject *)v; |
|
955914b0a4d6
PyList_Reverse(): This was leaking a reference to Py_None on every call.
Tim Peters <tim.peters@gmail.com>
parents:
24540
diff
changeset
|
1772 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1773 if (v == NULL || !PyList_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1774 PyErr_BadInternalCall(); |
|
2159
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
1775 return -1; |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
1776 } |
|
24657
955914b0a4d6
PyList_Reverse(): This was leaking a reference to Py_None on every call.
Tim Peters <tim.peters@gmail.com>
parents:
24540
diff
changeset
|
1777 if (self->ob_size > 1) |
|
955914b0a4d6
PyList_Reverse(): This was leaking a reference to Py_None on every call.
Tim Peters <tim.peters@gmail.com>
parents:
24540
diff
changeset
|
1778 reverse_slice(self->ob_item, self->ob_item + self->ob_size); |
|
2159
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
1779 return 0; |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
1780 } |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
1781 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1782 PyObject * |
|
13472
dbe83fd23393
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
1783 PyList_AsTuple(PyObject *v) |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1784 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1785 PyObject *w; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1786 PyObject **p; |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1787 int n; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1788 if (v == NULL || !PyList_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1789 PyErr_BadInternalCall(); |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1790 return NULL; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1791 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1792 n = ((PyListObject *)v)->ob_size; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1793 w = PyTuple_New(n); |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1794 if (w == NULL) |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1795 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1796 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
|
1797 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
|
1798 (void *)((PyListObject *)v)->ob_item, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1799 n*sizeof(PyObject *)); |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1800 while (--n >= 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1801 Py_INCREF(*p); |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1802 p++; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1803 } |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1804 return w; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1805 } |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
1806 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1807 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
|
1808 listindex(PyListObject *self, PyObject *v) |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1809 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1810 int i; |
|
11846
8b70eaaadf4b
Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1811 |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1812 for (i = 0; i < self->ob_size; i++) { |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1813 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
|
1814 if (cmp > 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1815 return PyInt_FromLong((long)i); |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1816 else if (cmp < 0) |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1817 return NULL; |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1818 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1819 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
|
1820 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1821 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1822 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1823 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
|
1824 listcount(PyListObject *self, PyObject *v) |
| 394 | 1825 { |
| 1826 int count = 0; | |
| 1827 int i; | |
|
11846
8b70eaaadf4b
Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1828 |
| 394 | 1829 for (i = 0; i < self->ob_size; i++) { |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1830 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
|
1831 if (cmp > 0) |
| 394 | 1832 count++; |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1833 else if (cmp < 0) |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1834 return NULL; |
| 394 | 1835 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1836 return PyInt_FromLong((long)count); |
| 394 | 1837 } |
| 1838 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1839 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
|
1840 listremove(PyListObject *self, PyObject *v) |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1841 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1842 int i; |
|
11846
8b70eaaadf4b
Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1843 |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1844 for (i = 0; i < self->ob_size; i++) { |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1845 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
|
1846 if (cmp > 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1847 if (list_ass_slice(self, i, i+1, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1848 (PyObject *)NULL) != 0) |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1849 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1850 Py_INCREF(Py_None); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1851 return Py_None; |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1852 } |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1853 else if (cmp < 0) |
|
5610
10e9cfa010f0
PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1854 return NULL; |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1855 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1856 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
|
1857 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1858 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
1859 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1860 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1861 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
|
1862 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1863 int i, err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1864 PyObject *x; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1865 |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1866 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
|
1867 x = o->ob_item[i]; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1868 if (x != NULL) { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1869 err = visit(x, arg); |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1870 if (err) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1871 return err; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1872 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1873 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1874 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1875 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1876 |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1877 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1878 list_clear(PyListObject *lp) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1879 { |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1880 (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
|
1881 return 0; |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1882 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12952
diff
changeset
|
1883 |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1884 static PyObject * |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1885 list_richcompare(PyObject *v, PyObject *w, int op) |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1886 { |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1887 PyListObject *vl, *wl; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1888 int i; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1889 |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1890 if (!PyList_Check(v) || !PyList_Check(w)) { |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1891 Py_INCREF(Py_NotImplemented); |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1892 return Py_NotImplemented; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1893 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1894 |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1895 vl = (PyListObject *)v; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1896 wl = (PyListObject *)w; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1897 |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1898 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
|
1899 /* Shortcut: if the lengths differ, the lists differ */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1900 PyObject *res; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1901 if (op == Py_EQ) |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1902 res = Py_False; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1903 else |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1904 res = Py_True; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1905 Py_INCREF(res); |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1906 return res; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1907 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1908 |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1909 /* Search for the first index where items are different */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1910 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
|
1911 int k = PyObject_RichCompareBool(vl->ob_item[i], |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1912 wl->ob_item[i], Py_EQ); |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1913 if (k < 0) |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1914 return NULL; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1915 if (!k) |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1916 break; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1917 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1918 |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1919 if (i >= vl->ob_size || i >= wl->ob_size) { |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1920 /* No more items to compare -- compare sizes */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1921 int vs = vl->ob_size; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1922 int ws = wl->ob_size; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1923 int cmp; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1924 PyObject *res; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1925 switch (op) { |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1926 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
|
1927 case Py_LE: cmp = vs <= ws; break; |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1928 case Py_EQ: cmp = vs == ws; break; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1929 case Py_NE: cmp = vs != ws; break; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1930 case Py_GT: cmp = vs > ws; break; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1931 case Py_GE: cmp = vs >= ws; break; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1932 default: return NULL; /* cannot happen */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1933 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1934 if (cmp) |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1935 res = Py_True; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1936 else |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1937 res = Py_False; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1938 Py_INCREF(res); |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1939 return res; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1940 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1941 |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1942 /* 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
|
1943 if (op == Py_EQ) { |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1944 Py_INCREF(Py_False); |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1945 return Py_False; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1946 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1947 if (op == Py_NE) { |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1948 Py_INCREF(Py_True); |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1949 return Py_True; |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1950 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1951 |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1952 /* Compare the final item again using the proper operator */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1953 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
|
1954 } |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
1955 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1956 /* Adapted from newer code by Tim */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1957 static int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1958 list_fill(PyListObject *result, PyObject *v) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1959 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1960 PyObject *it; /* iter(v) */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1961 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
|
1962 int i; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1963 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1964 n = result->ob_size; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1965 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1966 /* 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
|
1967 if (PyList_Check(v)) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1968 if (v == (PyObject *)result) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1969 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
|
1970 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
|
1971 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1972 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1973 /* Empty previous contents */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1974 if (n != 0) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1975 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
|
1976 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1977 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1978 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1979 /* 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
|
1980 * 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
|
1981 * 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
|
1982 */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1983 it = PyObject_GetIter(v); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1984 if (it == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1985 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1986 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1987 /* Guess a result list size. */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1988 n = -1; /* unknown */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1989 if (PySequence_Check(v) && |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1990 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
|
1991 n = PySequence_Size(v); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1992 if (n < 0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1993 PyErr_Clear(); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1994 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1995 if (n < 0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1996 n = 8; /* arbitrary */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
1997 NRESIZE(result->ob_item, PyObject*, n); |
|
23517
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
1998 if (result->ob_item == NULL) { |
|
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
1999 PyErr_NoMemory(); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2000 goto error; |
|
23517
73c01b1cbdb2
Closes: #556025 seg fault when doing list(xrange(1e9))
Neal Norwitz <nnorwitz@gmail.com>
parents:
22975
diff
changeset
|
2001 } |
|
23834
8ae26d9be1b4
SF #561244 Micro optimizations
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
2002 memset(result->ob_item, 0, sizeof(*result->ob_item) * n); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2003 result->ob_size = n; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2004 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2005 /* Run iterator to exhaustion. */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2006 for (i = 0; ; i++) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2007 PyObject *item = PyIter_Next(it); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2008 if (item == NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2009 if (PyErr_Occurred()) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2010 goto error; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2011 break; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2012 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2013 if (i < n) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2014 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
|
2015 else { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2016 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
|
2017 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
|
2018 if (status < 0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2019 goto error; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2020 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2021 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2022 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2023 /* 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
|
2024 if (i < n && result != NULL) { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2025 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
|
2026 goto error; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2027 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2028 Py_DECREF(it); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2029 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2030 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2031 error: |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2032 Py_DECREF(it); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2033 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2034 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2035 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2036 static int |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2037 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
|
2038 { |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2039 PyObject *arg = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2040 static char *kwlist[] = {"sequence", 0}; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2041 |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2042 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
|
2043 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2044 if (arg != NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2045 return list_fill(self, arg); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2046 if (self->ob_size > 0) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2047 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
|
2048 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2049 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2050 |
|
21155
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
2051 static long |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
2052 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
|
2053 { |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
2054 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
|
2055 return -1; |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
2056 } |
|
bca53a37a8a5
Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents:
20277
diff
changeset
|
2057 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2058 PyDoc_STRVAR(append_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2059 "L.append(object) -- append object to end"); |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2060 PyDoc_STRVAR(extend_doc, |
|
26816
6932794817c2
SF Bug 645777: list.extend() works with any iterable and is no longer
Raymond Hettinger <python@rcn.com>
parents:
26543
diff
changeset
|
2061 "L.extend(iterable) -- extend list by appending elements from the iterable"); |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2062 PyDoc_STRVAR(insert_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2063 "L.insert(index, object) -- insert object before index"); |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2064 PyDoc_STRVAR(pop_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2065 "L.pop([index]) -> item -- remove and return item at index (default last)"); |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2066 PyDoc_STRVAR(remove_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2067 "L.remove(value) -- remove first occurrence of value"); |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2068 PyDoc_STRVAR(index_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2069 "L.index(value) -> integer -- return index of first occurrence of value"); |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2070 PyDoc_STRVAR(count_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2071 "L.count(value) -> integer -- return number of occurrences of value"); |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2072 PyDoc_STRVAR(reverse_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2073 "L.reverse() -- reverse *IN PLACE*"); |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23779
diff
changeset
|
2074 PyDoc_STRVAR(sort_doc, |
|
27036
41f7726e3e51
Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents:
26818
diff
changeset
|
2075 "L.sort(cmpfunc=None) -- stable sort *IN PLACE*; cmpfunc(x, y) -> -1, 0, 1"); |
|
8595
635c3d94a948
Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents:
8515
diff
changeset
|
2076 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2077 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
|
2078 {"append", (PyCFunction)listappend, METH_O, append_doc}, |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2079 {"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
|
2080 {"extend", (PyCFunction)listextend, METH_O, extend_doc}, |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2081 {"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
|
2082 {"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
|
2083 {"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
|
2084 {"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
|
2085 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc}, |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2086 {"sort", (PyCFunction)listsort, METH_VARARGS, sort_doc}, |
|
24456
d216e67ac221
Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents:
24424
diff
changeset
|
2087 {NULL, NULL} /* sentinel */ |
| 6 | 2088 }; |
| 2089 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2090 static PySequenceMethods list_as_sequence = { |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2091 (inquiry)list_length, /* sq_length */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2092 (binaryfunc)list_concat, /* sq_concat */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2093 (intargfunc)list_repeat, /* sq_repeat */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2094 (intargfunc)list_item, /* sq_item */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2095 (intintargfunc)list_slice, /* sq_slice */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2096 (intobjargproc)list_ass_item, /* sq_ass_item */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2097 (intintobjargproc)list_ass_slice, /* sq_ass_slice */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2098 (objobjproc)list_contains, /* sq_contains */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2099 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2100 (intargfunc)list_inplace_repeat, /* sq_inplace_repeat */ |
| 6 | 2101 }; |
| 2102 | |
|
23855
2b071337205c
Missed one use of new PyDoc_STRVAR macro
Neal Norwitz <nnorwitz@gmail.com>
parents:
23834
diff
changeset
|
2103 PyDoc_STRVAR(list_doc, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2104 "list() -> new list\n" |
|
23855
2b071337205c
Missed one use of new PyDoc_STRVAR macro
Neal Norwitz <nnorwitz@gmail.com>
parents:
23834
diff
changeset
|
2105 "list(sequence) -> new list initialized from sequence's items"); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2106 |
|
24230
2860be244263
Don't declare a function with staticforward.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24207
diff
changeset
|
2107 static PyObject *list_iter(PyObject *seq); |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2108 |
|
24230
2860be244263
Don't declare a function with staticforward.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24207
diff
changeset
|
2109 static PyObject * |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2110 list_subscript(PyListObject* self, PyObject* item) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2111 { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2112 if (PyInt_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2113 long i = PyInt_AS_LONG(item); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2114 if (i < 0) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2115 i += PyList_GET_SIZE(self); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2116 return list_item(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2117 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2118 else if (PyLong_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2119 long i = PyLong_AsLong(item); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2120 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2121 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2122 if (i < 0) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2123 i += PyList_GET_SIZE(self); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2124 return list_item(self, i); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2125 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2126 else if (PySlice_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2127 int start, stop, step, slicelength, cur, i; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2128 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2129 PyObject* it; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2130 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2131 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2132 &start, &stop, &step, &slicelength) < 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2133 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2134 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2135 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2136 if (slicelength <= 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2137 return PyList_New(0); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2138 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2139 else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2140 result = PyList_New(slicelength); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2141 if (!result) return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2142 |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2143 for (cur = start, i = 0; i < slicelength; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2144 cur += step, i++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2145 it = PyList_GET_ITEM(self, cur); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2146 Py_INCREF(it); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2147 PyList_SET_ITEM(result, i, it); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2148 } |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2149 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2150 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2151 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2152 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2153 else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2154 PyErr_SetString(PyExc_TypeError, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2155 "list indices must be integers"); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2156 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2157 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2158 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2159 |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2160 static int |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2161 list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2162 { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2163 if (PyInt_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2164 long i = PyInt_AS_LONG(item); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2165 if (i < 0) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2166 i += PyList_GET_SIZE(self); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2167 return list_ass_item(self, i, value); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2168 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2169 else if (PyLong_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2170 long i = PyLong_AsLong(item); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2171 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2172 return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2173 if (i < 0) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2174 i += PyList_GET_SIZE(self); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2175 return list_ass_item(self, i, value); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2176 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2177 else if (PySlice_Check(item)) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2178 int start, stop, step, slicelength; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2179 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2180 if (PySlice_GetIndicesEx((PySliceObject*)item, self->ob_size, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2181 &start, &stop, &step, &slicelength) < 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2182 return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2183 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2184 |
|
23929
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2185 /* treat L[slice(a,b)] = v _exactly_ like L[a:b] = v */ |
|
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2186 if (step == 1 && ((PySliceObject*)item)->step == Py_None) |
|
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2187 return list_ass_slice(self, start, stop, value); |
|
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2188 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2189 if (value == NULL) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2190 /* delete slice */ |
|
23929
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2191 PyObject **garbage, **it; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2192 int cur, i, j; |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2193 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2194 if (slicelength <= 0) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2195 return 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2196 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2197 if (step < 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2198 stop = start + 1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2199 start = stop + step*(slicelength - 1) - 1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2200 step = -step; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2201 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2202 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2203 garbage = (PyObject**) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2204 PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2205 |
|
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2206 /* drawing pictures might help |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2207 understand these for loops */ |
|
23779
55c922669913
Fold remaining long lines.
Guido van Rossum <guido@python.org>
parents:
23778
diff
changeset
|
2208 for (cur = start, i = 0; |
|
55c922669913
Fold remaining long lines.
Guido van Rossum <guido@python.org>
parents:
23778
diff
changeset
|
2209 cur < stop; |
| 24424 | 2210 cur += step, i++) { |
| 2211 int lim = step; | |
| 2212 | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2213 garbage[i] = PyList_GET_ITEM(self, cur); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2214 |
| 24424 | 2215 if (cur + step >= self->ob_size) { |
| 2216 lim = self->ob_size - cur - 1; | |
| 2217 } | |
| 2218 | |
| 2219 for (j = 0; j < lim; j++) { | |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2220 PyList_SET_ITEM(self, cur + j - i, |
|
23779
55c922669913
Fold remaining long lines.
Guido van Rossum <guido@python.org>
parents:
23778
diff
changeset
|
2221 PyList_GET_ITEM(self, |
|
55c922669913
Fold remaining long lines.
Guido van Rossum <guido@python.org>
parents:
23778
diff
changeset
|
2222 cur + j + 1)); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2223 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2224 } |
|
23929
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2225 for (cur = start + slicelength*step + 1; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2226 cur < self->ob_size; cur++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2227 PyList_SET_ITEM(self, cur - slicelength, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2228 PyList_GET_ITEM(self, cur)); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2229 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2230 self->ob_size -= slicelength; |
|
23929
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2231 it = self->ob_item; |
|
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2232 NRESIZE(it, PyObject*, self->ob_size); |
|
26ba17b758b9
Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents:
23855
diff
changeset
|
2233 self->ob_item = it; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2234 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2235 for (i = 0; i < slicelength; i++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2236 Py_DECREF(garbage[i]); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2237 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2238 PyMem_FREE(garbage); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2239 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2240 return 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2241 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2242 else { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2243 /* assign slice */ |
|
26543
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2244 PyObject **garbage, *ins, *seq; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2245 int cur, i; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2246 |
|
26543
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2247 /* protect against a[::-1] = a */ |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2248 if (self == (PyListObject*)value) { |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2249 seq = list_slice((PyListObject*)value, 0, |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2250 PyList_GET_SIZE(value)); |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2251 } |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2252 else { |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2253 char msg[256]; |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2254 PyOS_snprintf(msg, sizeof(msg), |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2255 "must assign sequence (not \"%.200s\") to extended slice", |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2256 value->ob_type->tp_name); |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2257 seq = PySequence_Fast(value, msg); |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2258 if (!seq) |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2259 return -1; |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2260 } |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2261 |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2262 if (PySequence_Fast_GET_SIZE(seq) != slicelength) { |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2263 PyErr_Format(PyExc_ValueError, |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2264 "attempt to assign sequence of size %d to extended slice of size %d", |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2265 PySequence_Fast_GET_SIZE(seq), |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2266 slicelength); |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2267 Py_DECREF(seq); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2268 return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2269 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2270 |
|
26543
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2271 if (!slicelength) { |
|
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2272 Py_DECREF(seq); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2273 return 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2274 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2275 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2276 garbage = (PyObject**) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2277 PyMem_MALLOC(slicelength*sizeof(PyObject*)); |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2278 |
|
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2279 for (cur = start, i = 0; i < slicelength; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2280 cur += step, i++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2281 garbage[i] = PyList_GET_ITEM(self, cur); |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2282 |
|
26543
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2283 ins = PySequence_Fast_GET_ITEM(seq, i); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2284 Py_INCREF(ins); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2285 PyList_SET_ITEM(self, cur, ins); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2286 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2287 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2288 for (i = 0; i < slicelength; i++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2289 Py_DECREF(garbage[i]); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2290 } |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2291 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2292 PyMem_FREE(garbage); |
|
26543
975482d5eb5c
The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents:
26161
diff
changeset
|
2293 Py_DECREF(seq); |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2294 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2295 return 0; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2296 } |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2297 } |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2298 else { |
|
24310
5ede9bcc651e
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
24309
diff
changeset
|
2299 PyErr_SetString(PyExc_TypeError, |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2300 "list indices must be integers"); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2301 return -1; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2302 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2303 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2304 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2305 static PyMappingMethods list_as_mapping = { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2306 (inquiry)list_length, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2307 (binaryfunc)list_subscript, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2308 (objobjargproc)list_ass_subscript |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2309 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2310 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2311 PyTypeObject PyList_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2312 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 2313 0, |
| 2314 "list", | |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
2315 sizeof(PyListObject), |
| 6 | 2316 0, |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2317 (destructor)list_dealloc, /* tp_dealloc */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2318 (printfunc)list_print, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2319 0, /* tp_getattr */ |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2320 0, /* tp_setattr */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2321 0, /* tp_compare */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2322 (reprfunc)list_repr, /* tp_repr */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2323 0, /* tp_as_number */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2324 &list_as_sequence, /* tp_as_sequence */ |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23615
diff
changeset
|
2325 &list_as_mapping, /* 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
|
2326 list_nohash, /* tp_hash */ |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2327 0, /* tp_call */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2328 0, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2329 PyObject_GenericGetAttr, /* tp_getattro */ |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2330 0, /* tp_setattro */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2331 0, /* tp_as_buffer */ |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19301
diff
changeset
|
2332 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
|
2333 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2334 list_doc, /* tp_doc */ |
|
15969
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2335 (traverseproc)list_traverse, /* tp_traverse */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2336 (inquiry)list_clear, /* tp_clear */ |
|
e21da84d0c65
Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents:
15707
diff
changeset
|
2337 list_richcompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2338 0, /* tp_weaklistoffset */ |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2339 list_iter, /* tp_iter */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2340 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2341 list_methods, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2342 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2343 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2344 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2345 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2346 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2347 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2348 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2349 (initproc)list_init, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2350 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18409
diff
changeset
|
2351 PyType_GenericNew, /* tp_new */ |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2352 PyObject_GC_Del, /* tp_free */ |
| 6 | 2353 }; |
|
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
|
2354 |
|
69662f16a838
Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents:
8457
diff
changeset
|
2355 |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2356 /*********************** List Iterator **************************/ |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2357 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2358 typedef struct { |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2359 PyObject_HEAD |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2360 long it_index; |
|
24255
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2361 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */ |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2362 } listiterobject; |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2363 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2364 PyTypeObject PyListIter_Type; |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2365 |
|
24247
5d3ec28e9a7a
Make list_iter() really static.
Guido van Rossum <guido@python.org>
parents:
24236
diff
changeset
|
2366 static PyObject * |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2367 list_iter(PyObject *seq) |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2368 { |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2369 listiterobject *it; |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2370 |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2371 if (!PyList_Check(seq)) { |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2372 PyErr_BadInternalCall(); |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2373 return NULL; |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2374 } |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2375 it = PyObject_GC_New(listiterobject, &PyListIter_Type); |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2376 if (it == NULL) |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2377 return NULL; |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2378 it->it_index = 0; |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2379 Py_INCREF(seq); |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2380 it->it_seq = (PyListObject *)seq; |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2381 _PyObject_GC_TRACK(it); |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2382 return (PyObject *)it; |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2383 } |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2384 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2385 static void |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2386 listiter_dealloc(listiterobject *it) |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2387 { |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2388 _PyObject_GC_UNTRACK(it); |
|
24255
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2389 Py_XDECREF(it->it_seq); |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2390 PyObject_GC_Del(it); |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2391 } |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2392 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2393 static int |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2394 listiter_traverse(listiterobject *it, visitproc visit, void *arg) |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2395 { |
|
24255
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2396 if (it->it_seq == NULL) |
|
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2397 return 0; |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2398 return visit((PyObject *)it->it_seq, arg); |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2399 } |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2400 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2401 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2402 static PyObject * |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2403 listiter_getiter(PyObject *it) |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2404 { |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2405 Py_INCREF(it); |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2406 return it; |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2407 } |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2408 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2409 static PyObject * |
|
23615
a6715ad416e5
A bogus assert in the new listiter code prevented starting Python in a
Tim Peters <tim.peters@gmail.com>
parents:
23606
diff
changeset
|
2410 listiter_next(listiterobject *it) |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2411 { |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2412 PyListObject *seq; |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2413 PyObject *item; |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2414 |
|
23615
a6715ad416e5
A bogus assert in the new listiter code prevented starting Python in a
Tim Peters <tim.peters@gmail.com>
parents:
23606
diff
changeset
|
2415 assert(it != NULL); |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2416 seq = it->it_seq; |
|
24255
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2417 if (seq == NULL) |
|
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2418 return NULL; |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2419 assert(PyList_Check(seq)); |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2420 |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2421 if (it->it_index < PyList_GET_SIZE(seq)) { |
|
23615
a6715ad416e5
A bogus assert in the new listiter code prevented starting Python in a
Tim Peters <tim.peters@gmail.com>
parents:
23606
diff
changeset
|
2422 item = PyList_GET_ITEM(seq, it->it_index); |
|
a6715ad416e5
A bogus assert in the new listiter code prevented starting Python in a
Tim Peters <tim.peters@gmail.com>
parents:
23606
diff
changeset
|
2423 ++it->it_index; |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2424 Py_INCREF(item); |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2425 return item; |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2426 } |
|
24255
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2427 |
|
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2428 Py_DECREF(seq); |
|
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2429 it->it_seq = NULL; |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2430 return NULL; |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2431 } |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2432 |
|
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2433 PyTypeObject PyListIter_Type = { |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2434 PyObject_HEAD_INIT(&PyType_Type) |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2435 0, /* ob_size */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2436 "listiterator", /* tp_name */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2437 sizeof(listiterobject), /* tp_basicsize */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2438 0, /* tp_itemsize */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2439 /* methods */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2440 (destructor)listiter_dealloc, /* tp_dealloc */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2441 0, /* tp_print */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2442 0, /* tp_getattr */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2443 0, /* tp_setattr */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2444 0, /* tp_compare */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2445 0, /* tp_repr */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2446 0, /* tp_as_number */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2447 0, /* tp_as_sequence */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2448 0, /* tp_as_mapping */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2449 0, /* tp_hash */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2450 0, /* tp_call */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2451 0, /* tp_str */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2452 PyObject_GenericGetAttr, /* tp_getattro */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2453 0, /* tp_setattro */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2454 0, /* tp_as_buffer */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2455 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2456 0, /* tp_doc */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2457 (traverseproc)listiter_traverse, /* tp_traverse */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2458 0, /* tp_clear */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2459 0, /* tp_richcompare */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2460 0, /* tp_weaklistoffset */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2461 (getiterfunc)listiter_getiter, /* tp_iter */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2462 (iternextfunc)listiter_next, /* tp_iternext */ |
|
24255
41c54b37e030
Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents:
24247
diff
changeset
|
2463 0, /* tp_methods */ |
|
24257
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2464 0, /* tp_members */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2465 0, /* tp_getset */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2466 0, /* tp_base */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2467 0, /* tp_dict */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2468 0, /* tp_descr_get */ |
|
a6717275195c
Whitespace normalization.
Guido van Rossum <guido@python.org>
parents:
24255
diff
changeset
|
2469 0, /* tp_descr_set */ |
|
23606
5edea752a421
SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents:
23517
diff
changeset
|
2470 }; |
