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