annotate Objects/listobject.c @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents f14ab70c320e
children 8b91ff698f4f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* List object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
11 /* Ensure ob_item has room for at least newsize elements, and set
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
12 * ob_size to newsize. If newsize > ob_size on entry, the content
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
13 * of the new slots at exit is undefined heap trash; it's the caller's
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
14 * responsiblity to overwrite them with sane values.
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
15 * The number of allocated elements may grow, shrink, or stay the same.
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
16 * Failure is impossible if newsize <= self.allocated on entry, although
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
17 * that partly relies on an assumption that the system realloc() never
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
18 * fails when passed a number of bytes <= the number of bytes last
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
19 * allocated (the C standard doesn't guarantee this, but it's hard to
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
20 * imagine a realloc implementation where it wouldn't be true).
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
21 * Note that self->ob_item may change, and even if newsize is less
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
22 * than ob_size on entry.
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
23 */
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
24 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
25 list_resize(PyListObject *self, Py_ssize_t newsize)
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
26 {
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
27 PyObject **items;
33464
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
28 size_t new_allocated;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
29 Py_ssize_t allocated = self->allocated;
17969
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
30
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
31 /* Bypass realloc() when a previous overallocation is large enough
33464
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
32 to accommodate the newsize. If the newsize falls lower than half
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
33 the allocated size, then proceed with the realloc() to shrink the list.
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
34 */
33464
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
35 if (allocated >= newsize && newsize >= (allocated >> 1)) {
32859
45f3222b84ee * Simplify and speed-up list_resize(). Relying on the newly documented
Raymond Hettinger <python@rcn.com>
parents: 32858
diff changeset
36 assert(self->ob_item != NULL || newsize == 0);
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
37 self->ob_size = newsize;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
38 return 0;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
39 }
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
40
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
41 /* This over-allocates proportional to the list size, making room
17969
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
42 * 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
43 * 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
44 * sequence of appends() in the presence of a poorly-performing
31527
27bf05b2a4f9 Fine tune the speed/space trade-off for overallocating small lists.
Raymond Hettinger <python@rcn.com>
parents: 31526
diff changeset
45 * system realloc().
27bf05b2a4f9 Fine tune the speed/space trade-off for overallocating small lists.
Raymond Hettinger <python@rcn.com>
parents: 31526
diff changeset
46 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
17969
ed8b79c6cb4b roundupsize() and friends: fiddle over-allocation strategy for list
Tim Peters <tim.peters@gmail.com>
parents: 16581
diff changeset
47 */
33464
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
48 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6) + newsize;
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
49 if (newsize == 0)
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
50 new_allocated = 0;
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
51 items = self->ob_item;
33464
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
52 if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
53 PyMem_RESIZE(items, PyObject *, new_allocated);
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
54 else
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
55 items = NULL;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
56 if (items == NULL) {
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
57 PyErr_NoMemory();
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
58 return -1;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
59 }
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
60 self->ob_item = items;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
61 self->ob_size = newsize;
33464
d8a261a77028 SF #1022910: Conserve memory with list.pop()
Raymond Hettinger <python@rcn.com>
parents: 33448
diff changeset
62 self->allocated = new_allocated;
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
63 return 0;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
64 }
2257
ee4c954cd5df round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents: 2188
diff changeset
65
31982
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
66 /* Empty list reuse scheme to save calls to malloc and free */
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
67 #define MAXFREELISTS 80
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
68 static PyListObject *free_lists[MAXFREELISTS];
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
69 static int num_free_lists = 0;
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
70
33679
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
71 void
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
72 PyList_Fini(void)
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
73 {
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
74 PyListObject *op;
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
75
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
76 while (num_free_lists) {
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
77 num_free_lists--;
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
78 op = free_lists[num_free_lists];
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
79 assert(PyList_CheckExact(op));
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
80 PyObject_GC_Del(op);
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
81 }
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
82 }
55fba88930bc Finalize the freelist of list objects.
Raymond Hettinger <python@rcn.com>
parents: 33589
diff changeset
83
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
84 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
85 PyList_New(Py_ssize_t size)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
87 PyListObject *op;
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
88 size_t nbytes;
32848
e3a35fbce8ec PyList_New(): we went to all the trouble of computing and bounds-checking
Tim Peters <tim.peters@gmail.com>
parents: 32695
diff changeset
89
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if (size < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
91 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 }
31390
9169d79ed304 Revert change accidentally checked in as part of a whitespace normalization
Tim Peters <tim.peters@gmail.com>
parents: 31389
diff changeset
94 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
95 /* Check for overflow */
31983
Raymond Hettinger <python@rcn.com>
parents: 31982
diff changeset
96 if (nbytes / sizeof(PyObject *) != (size_t)size)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
97 return PyErr_NoMemory();
31982
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
98 if (num_free_lists) {
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
99 num_free_lists--;
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
100 op = free_lists[num_free_lists];
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
101 _Py_NewReference((PyObject *)op);
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
102 } else {
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
103 op = PyObject_GC_New(PyListObject, &PyList_Type);
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
104 if (op == NULL)
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
105 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 }
31983
Raymond Hettinger <python@rcn.com>
parents: 31982
diff changeset
107 if (size <= 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 op->ob_item = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 else {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
110 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
38763
f14ab70c320e Don't leak the list object if there's an error allocating the item storage. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 37759
diff changeset
111 if (op->ob_item == NULL) {
f14ab70c320e Don't leak the list object if there's an error allocating the item storage. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 37759
diff changeset
112 Py_DECREF(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
113 return PyErr_NoMemory();
38763
f14ab70c320e Don't leak the list object if there's an error allocating the item storage. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 37759
diff changeset
114 }
32848
e3a35fbce8ec PyList_New(): we went to all the trouble of computing and bounds-checking
Tim Peters <tim.peters@gmail.com>
parents: 32695
diff changeset
115 memset(op->ob_item, 0, nbytes);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 }
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
117 op->ob_size = size;
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
118 op->allocated = size;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
119 _PyObject_GC_TRACK(op);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
120 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
123 Py_ssize_t
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
124 PyList_Size(PyObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
126 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
127 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
131 return ((PyListObject *)op) -> ob_size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
31983
Raymond Hettinger <python@rcn.com>
parents: 31982
diff changeset
134 static PyObject *indexerr = NULL;
3985
011cfe7490eb Use pre-created string objects for most common exceptions
Guido van Rossum <guido@python.org>
parents: 2827
diff changeset
135
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
136 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
137 PyList_GetItem(PyObject *op, Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
139 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
140 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
143 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
144 if (indexerr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
145 indexerr = PyString_FromString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
146 "list index out of range");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
147 PyErr_SetObject(PyExc_IndexError, indexerr);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
150 return ((PyListObject *)op) -> ob_item[i];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
154 PyList_SetItem(register PyObject *op, register Py_ssize_t i,
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
155 register PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
157 register PyObject *olditem;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
158 register PyObject **p;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
159 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
160 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
161 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
162 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
164 if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
165 Py_XDECREF(newitem);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
166 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
167 "list assignment index out of range");
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
168 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
170 p = ((PyListObject *)op) -> ob_item + i;
2504
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
171 olditem = *p;
425835b88099 a few peephole optimizations
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
172 *p = newitem;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
173 Py_XDECREF(olditem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
178 ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
180 Py_ssize_t i, n = self->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
181 PyObject **items;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
182 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
183 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
184 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
185 }
37673
606818c33e50 Replace INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents: 37589
diff changeset
186 if (n == PY_SSIZE_T_MAX) {
13957
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
187 PyErr_SetString(PyExc_OverflowError,
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
188 "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
189 return -1;
b9002da46f69 Check for overflow in list object insertion and raise OverflowError.
Trent Mick <trentm@activestate.com>
parents: 13709
diff changeset
190 }
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
191
31665
9a4c49c5ac03 Revert last change. Found an application that was worse off with resize
Raymond Hettinger <python@rcn.com>
parents: 31661
diff changeset
192 if (list_resize(self, n+1) == -1)
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
193 return -1;
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
194
28606
436d812f2b94 - list.insert(i, x) now interprets negative i as it would be
Guido van Rossum <guido@python.org>
parents: 28320
diff changeset
195 if (where < 0) {
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
196 where += n;
28606
436d812f2b94 - list.insert(i, x) now interprets negative i as it would be
Guido van Rossum <guido@python.org>
parents: 28320
diff changeset
197 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
198 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
199 }
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
200 if (where > n)
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
201 where = n;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
202 items = self->ob_item;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
203 for (i = n; --i >= where; )
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 items[i+1] = items[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
205 Py_INCREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 items[where] = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
211 PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
213 if (!PyList_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
214 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
215 return -1;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
216 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
217 return ins1((PyListObject *)op, where, newitem);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219
31905
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
220 static int
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
221 app1(PyListObject *self, PyObject *v)
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
222 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
223 Py_ssize_t n = PyList_GET_SIZE(self);
31905
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
224
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
225 assert (v != NULL);
37673
606818c33e50 Replace INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents: 37589
diff changeset
226 if (n == PY_SSIZE_T_MAX) {
31905
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
227 PyErr_SetString(PyExc_OverflowError,
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
228 "cannot add more objects to list");
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
229 return -1;
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
230 }
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
231
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
232 if (list_resize(self, n+1) == -1)
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
233 return -1;
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
234
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
235 Py_INCREF(v);
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
236 PyList_SET_ITEM(self, n, v);
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
237 return 0;
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
238 }
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
239
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
241 PyList_Append(PyObject *op, PyObject *newitem)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 {
31983
Raymond Hettinger <python@rcn.com>
parents: 31982
diff changeset
243 if (PyList_Check(op) && (newitem != NULL))
Raymond Hettinger <python@rcn.com>
parents: 31982
diff changeset
244 return app1((PyListObject *)op, newitem);
Raymond Hettinger <python@rcn.com>
parents: 31982
diff changeset
245 PyErr_BadInternalCall();
Raymond Hettinger <python@rcn.com>
parents: 31982
diff changeset
246 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 static void
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
252 list_dealloc(PyListObject *op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
254 Py_ssize_t i;
22626
0446eeb787b7 Backport to 2.2.1:
Guido van Rossum <guido@python.org>
parents: 21155
diff changeset
255 PyObject_GC_UnTrack(op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11996
diff changeset
256 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
257 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
258 /* 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
259 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
260 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
261 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
262 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
263 while (--i >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
264 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
265 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12516
diff changeset
266 PyMem_FREE(op->ob_item);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 }
31982
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
268 if (num_free_lists < MAXFREELISTS && PyList_CheckExact(op))
51a5fb60934d SF patch #947476: Apply freelist technique to lists
Raymond Hettinger <python@rcn.com>
parents: 31907
diff changeset
269 free_lists[num_free_lists++] = op;
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
270 else
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
271 op->ob_type->tp_free((PyObject *)op);
11999
c94247b5c355 Christian Tismer's "trashcan" patch:
Guido van Rossum <guido@python.org>
parents: 11996
diff changeset
272 Py_TRASHCAN_SAFE_END(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
275 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
276 list_print(PyListObject *op, FILE *fp, int flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
278 int rc;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
279 Py_ssize_t 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
280
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
281 rc = Py_ReprEnter((PyObject*)op);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
282 if (rc != 0) {
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
283 if (rc < 0)
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
284 return rc;
7986
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
285 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
286 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
287 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 fprintf(fp, "[");
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
289 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
290 if (i > 0)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 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
292 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
293 Py_ReprLeave((PyObject *)op);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
294 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
295 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 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
298 Py_ReprLeave((PyObject *)op);
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
299 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
302 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
303 list_repr(PyListObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
305 Py_ssize_t i;
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
306 PyObject *s, *temp;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
307 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
308
971b5bbd92ec Use Py_Repr{Enter,Leave} to display recursive lists in finite space.
Guido van Rossum <guido@python.org>
parents: 7482
diff changeset
309 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
310 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
311 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
312 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
313
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
314 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
315 result = PyString_FromString("[]");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
316 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
317 }
18129
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
318
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
319 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
320 if (pieces == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
321 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
322
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
323 /* 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
324 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
325 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
326 int status;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
327 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
328 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
329 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
330 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
331 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
332 if (status < 0)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
333 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
334 }
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
335
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
336 /* 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
337 assert(PyList_GET_SIZE(pieces) > 0);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
338 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
339 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
340 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
341 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
342 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
343 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
344 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
345 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
346
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
347 s = PyString_FromString("]");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
348 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
349 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
350 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
351 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
352 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
353 if (temp == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
354 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
355
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
356 /* 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
357 s = PyString_FromString(", ");
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
358 if (s == NULL)
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
359 goto Done;
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
360 result = _PyString_Join(s, pieces);
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
361 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
362
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
363 Done:
9d533ae574d2 SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents: 17971
diff changeset
364 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
365 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
366 return result;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
369 static Py_ssize_t
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
370 list_length(PyListObject *a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
375 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
376 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
377 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
378 Py_ssize_t i;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
379 int cmp;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
380
25124
48d108e5b884 Micro-optimization for list_contains. Factored double if test
Raymond Hettinger <python@rcn.com>
parents: 24727
diff changeset
381 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
382 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
383 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
384 return cmp;
12516
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
385 }
e6e914d5b207 add list_contains and tuplecontains: efficient implementations of tp_contains
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11999
diff changeset
386
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
387 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
388 list_item(PyListObject *a, Py_ssize_t i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 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
391 if (indexerr == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
392 indexerr = PyString_FromString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
393 "list index out of range");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
394 PyErr_SetObject(PyExc_IndexError, indexerr);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
397 Py_INCREF(a->ob_item[i]);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 return a->ob_item[i];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
401 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
402 list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
404 PyListObject *np;
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 31621
diff changeset
405 PyObject **src, **dest;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
406 Py_ssize_t i, len;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 else if (ilow > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 ilow = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 else if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 ihigh = a->ob_size;
31621
635eebc473b0 Small optimizations for list_slice() and list_extend_internal().
Raymond Hettinger <python@rcn.com>
parents: 31550
diff changeset
415 len = ihigh - ilow;
635eebc473b0 Small optimizations for list_slice() and list_extend_internal().
Raymond Hettinger <python@rcn.com>
parents: 31550
diff changeset
416 np = (PyListObject *) PyList_New(len);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 if (np == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 return NULL;
31621
635eebc473b0 Small optimizations for list_slice() and list_extend_internal().
Raymond Hettinger <python@rcn.com>
parents: 31550
diff changeset
419
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 31621
diff changeset
420 src = a->ob_item + ilow;
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 31621
diff changeset
421 dest = np->ob_item;
31621
635eebc473b0 Small optimizations for list_slice() and list_extend_internal().
Raymond Hettinger <python@rcn.com>
parents: 31550
diff changeset
422 for (i = 0; i < len; i++) {
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 31621
diff changeset
423 PyObject *v = src[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
424 Py_INCREF(v);
31623
d34e7eaf4cd6 Optimize tuple_slice() and make further improvements to list_slice()
Raymond Hettinger <python@rcn.com>
parents: 31621
diff changeset
425 dest[i] = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
427 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
429
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
430 PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
431 PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
432 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
433 if (!PyList_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
434 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
435 return NULL;
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
436 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
437 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
438 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
439
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
440 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
441 list_concat(PyListObject *a, PyObject *bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
443 Py_ssize_t size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
444 Py_ssize_t i;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
445 PyObject **src, **dest;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
446 PyListObject *np;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
447 if (!PyList_Check(bb)) {
12816
4272b368bf69 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
448 PyErr_Format(PyExc_TypeError,
12817
5f1f8ddc2a0c Improve TypeError exception message for list catenation.
Fred Drake <fdrake@acm.org>
parents: 12816
diff changeset
449 "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
450 bb->ob_type->tp_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
453 #define b ((PyListObject *)bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 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
455 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
456 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
457 np = (PyListObject *) PyList_New(size);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 if (np == NULL) {
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 205
diff changeset
459 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 }
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
461 src = a->ob_item;
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
462 dest = np->ob_item;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 for (i = 0; i < a->ob_size; i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
464 PyObject *v = src[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
465 Py_INCREF(v);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
466 dest[i] = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 }
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
468 src = b->ob_item;
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
469 dest = np->ob_item + a->ob_size;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 for (i = 0; i < b->ob_size; i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
471 PyObject *v = src[i];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
472 Py_INCREF(v);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
473 dest[i] = v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
475 return (PyObject *)np;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
478
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
479 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
480 list_repeat(PyListObject *a, Py_ssize_t n)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
481 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
482 Py_ssize_t i, j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
483 Py_ssize_t size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
484 PyListObject *np;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
485 PyObject **p, **items;
29198
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
486 PyObject *elem;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
487 if (n < 0)
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
488 n = 0;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
489 size = a->ob_size * n;
29198
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
490 if (size == 0)
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
491 return PyList_New(0);
25748
f7a46b0d3c07 Darn! Don't divide by zero. Bad fix. :-)
Guido van Rossum <guido@python.org>
parents: 25740
diff changeset
492 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
493 return PyErr_NoMemory();
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
494 np = (PyListObject *) PyList_New(size);
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
495 if (np == NULL)
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
496 return NULL;
29198
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
497
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
498 items = np->ob_item;
29198
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
499 if (a->ob_size == 1) {
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
500 elem = a->ob_item[0];
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
501 for (i = 0; i < n; i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
502 items[i] = elem;
29198
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
503 Py_INCREF(elem);
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
504 }
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
505 return (PyObject *) np;
b9762f003f30 SF bug #604716: faster [None]*n or []*n
Raymond Hettinger <python@rcn.com>
parents: 29008
diff changeset
506 }
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
507 p = np->ob_item;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
508 items = a->ob_item;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
509 for (i = 0; i < n; i++) {
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
510 for (j = 0; j < a->ob_size; j++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
511 *p = items[j];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
512 Py_INCREF(*p);
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
513 p++;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
514 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
515 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
516 return (PyObject *) np;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
517 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
518
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 static int
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
520 list_clear(PyListObject *a)
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
521 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
522 Py_ssize_t i;
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
523 PyObject **item = a->ob_item;
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
524 if (item != NULL) {
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
525 /* Because XDECREF can recursively invoke operations on
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
526 this list, we make it empty first. */
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
527 i = a->ob_size;
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
528 a->ob_size = 0;
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
529 a->ob_item = NULL;
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
530 a->allocated = 0;
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
531 while (--i >= 0) {
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
532 Py_XDECREF(item[i]);
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
533 }
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
534 PyMem_FREE(item);
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
535 }
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
536 /* Never fails; the return value can be ignored.
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
537 Note that there is no guarantee that the list is actually empty
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
538 at this point, because XDECREF may have populated it again! */
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
539 return 0;
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
540 }
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
541
32874
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
542 /* a[ilow:ihigh] = v if v != NULL.
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
543 * del a[ilow:ihigh] if v == NULL.
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
544 *
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
545 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
546 * guaranteed the call cannot fail.
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
547 */
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
548 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
549 list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 {
2143
7ae847b1b269 fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
551 /* 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
552 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
553 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
554 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
555 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
556 list. :-( */
32869
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
557 PyObject *recycle_on_stack[8];
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
558 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
559 PyObject **item;
31632
dd69e3f5225b Optimize slice assignments.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
560 PyObject **vitem = NULL;
26040
e9aa394c7191 This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents: 25748
diff changeset
561 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
562 Py_ssize_t n; /* # of elements in replacement list */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
563 Py_ssize_t norig; /* # of elements in list getting replaced */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
564 Py_ssize_t d; /* Change in size */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
565 Py_ssize_t k;
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
566 size_t s;
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
567 int result = -1; /* guilty until proved innocent */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
568 #define b ((PyListObject *)v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 if (v == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 n = 0;
26040
e9aa394c7191 This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents: 25748
diff changeset
571 else {
511
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
572 if (a == b) {
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
573 /* Special case "a[i:j] = a" -- copy b first */
30156
b698209b9827 My last fix left n used unitialized in tha a==b case.
Michael W. Hudson <mwh@python.net>
parents: 30145
diff changeset
574 v = list_slice(b, 0, b->ob_size);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28806
diff changeset
575 if (v == NULL)
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
576 return result;
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
577 result = list_ass_slice(a, ilow, ihigh, v);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
578 Py_DECREF(v);
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
579 return result;
511
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
580 }
31632
dd69e3f5225b Optimize slice assignments.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
581 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
30145
7cc0a33946a3 Fix reference leak noted in test_types:
Michael W. Hudson <mwh@python.net>
parents: 29519
diff changeset
582 if(v_as_SF == NULL)
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
583 goto Error;
30145
7cc0a33946a3 Fix reference leak noted in test_types:
Michael W. Hudson <mwh@python.net>
parents: 29519
diff changeset
584 n = PySequence_Fast_GET_SIZE(v_as_SF);
31650
be782d07c518 Make PySequence_Fast_ITEMS public. (Thanks Skip.)
Raymond Hettinger <python@rcn.com>
parents: 31648
diff changeset
585 vitem = PySequence_Fast_ITEMS(v_as_SF);
511
766cc820f511 Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents: 475
diff changeset
586 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 if (ilow < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 ilow = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 else if (ilow > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 ilow = a->ob_size;
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
591
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 if (ihigh < ilow)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 ihigh = ilow;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 else if (ihigh > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 ihigh = a->ob_size;
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
596
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
597 norig = ihigh - ilow;
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
598 assert(norig >= 0);
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
599 d = n - norig;
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
600 if (a->ob_size + d == 0) {
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
601 Py_XDECREF(v_as_SF);
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
602 return list_clear(a);
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
603 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 item = a->ob_item;
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
605 /* recycle the items that we are about to remove */
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
606 s = norig * sizeof(PyObject *);
32869
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
607 if (s > sizeof(recycle_on_stack)) {
32861
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
608 recycle = (PyObject **)PyMem_MALLOC(s);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28806
diff changeset
609 if (recycle == NULL) {
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28806
diff changeset
610 PyErr_NoMemory();
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
611 goto Error;
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28806
diff changeset
612 }
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28806
diff changeset
613 }
32861
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
614 memcpy(recycle, &item[ilow], s);
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
615
32861
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
616 if (d < 0) { /* Delete -d items */
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
617 memmove(&item[ihigh+d], &item[ihigh],
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
618 (a->ob_size - ihigh)*sizeof(PyObject *));
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
619 list_resize(a, a->ob_size + d);
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
620 item = a->ob_item;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 }
32861
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
622 else if (d > 0) { /* Insert d items */
32869
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
623 k = a->ob_size;
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
624 if (list_resize(a, k+d) < 0)
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
625 goto Error;
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
626 item = a->ob_item;
31632
dd69e3f5225b Optimize slice assignments.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
627 memmove(&item[ihigh+d], &item[ihigh],
32869
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
628 (k - ihigh)*sizeof(PyObject *));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 for (k = 0; k < n; k++, ilow++) {
31632
dd69e3f5225b Optimize slice assignments.
Raymond Hettinger <python@rcn.com>
parents: 31623
diff changeset
631 PyObject *w = vitem[k];
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
632 Py_XINCREF(w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 item[ilow] = w;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 }
32869
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
635 for (k = norig - 1; k >= 0; --k)
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
636 Py_XDECREF(recycle[k]);
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
637 result = 0;
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
638 Error:
32869
ca6efc54b19f list_ass_slice(): The difference between "recycle" and "recycled" was
Tim Peters <tim.peters@gmail.com>
parents: 32868
diff changeset
639 if (recycle != recycle_on_stack)
32861
fc3687a627b4 This is a reorganization of list_ass_slice(). It should probably be reviewed,
Armin Rigo <arigo@tunes.org>
parents: 32860
diff changeset
640 PyMem_FREE(recycle);
26040
e9aa394c7191 This is Alex Martelli's patch
Michael W. Hudson <mwh@python.net>
parents: 25748
diff changeset
641 Py_XDECREF(v_as_SF);
32868
bd46e6f80623 Armin asked for a list_ass_slice review in his checkin, so here's the
Tim Peters <tim.peters@gmail.com>
parents: 32861
diff changeset
642 return result;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
645
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
646 int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
647 PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t 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
648 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
649 if (!PyList_Check(a)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
650 PyErr_BadInternalCall();
1330
409ea74feb21 Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents: 1313
diff changeset
651 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
652 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
653 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
654 }
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1211
diff changeset
655
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
656 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
657 list_inplace_repeat(PyListObject *self, Py_ssize_t n)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
658 {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
659 PyObject **items;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
660 Py_ssize_t size, i, j, p;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
661
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
662
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
663 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
664 if (size == 0) {
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
665 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
666 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
667 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
668
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
669 if (n < 1) {
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
670 (void)list_clear(self);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
671 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
672 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
673 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
674
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
675 if (list_resize(self, size*n) == -1)
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
676 return NULL;
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
677
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
678 p = size;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
679 items = self->ob_item;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
680 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
681 for (j = 0; j < size; j++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
682 PyObject *o = items[j];
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
683 Py_INCREF(o);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
684 items[p++] = o;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
685 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
686 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
687 Py_INCREF(self);
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
688 return (PyObject *)self;
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
689 }
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
690
167
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
691 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
692 list_ass_item(PyListObject *a, Py_ssize_t 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
693 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
694 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
695 if (i < 0 || i >= a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
696 PyErr_SetString(PyExc_IndexError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
697 "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
698 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
699 }
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
700 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
701 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
702 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
703 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
704 a->ob_item[i] = v;
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
705 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
706 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
707 }
3fa2b14be270 Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents: 155
diff changeset
708
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
709 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
710 listinsert(PyListObject *self, PyObject *args)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
712 Py_ssize_t i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
713 PyObject *v;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
714 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 return NULL;
31907
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
716 if (ins1(self, i, v) == 0)
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
717 Py_RETURN_NONE;
31906
f39c56f8ccc2 Small refactoring saving one function() and eliminating some indirection.
Raymond Hettinger <python@rcn.com>
parents: 31905
diff changeset
718 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
720
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
721 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
722 listappend(PyListObject *self, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 {
31907
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
724 if (app1(self, v) == 0)
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
725 Py_RETURN_NONE;
31906
f39c56f8ccc2 Small refactoring saving one function() and eliminating some indirection.
Raymond Hettinger <python@rcn.com>
parents: 31905
diff changeset
726 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
728
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
729 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
730 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
731 {
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
732 PyObject *it; /* iter(v) */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
733 Py_ssize_t m; /* size of self */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
734 Py_ssize_t n; /* guess for size of b */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
735 Py_ssize_t mn; /* m + n */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
736 Py_ssize_t i;
31643
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
737 PyObject *(*iternext)(PyObject *);
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
738
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
739 /* Special cases:
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
740 1) lists and tuples which can use PySequence_Fast ops
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
741 2) extending self to self requires making a copy first
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
742 */
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
743 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
31718
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
744 PyObject **src, **dest;
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
745 b = PySequence_Fast(b, "argument must be iterable");
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
746 if (!b)
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
747 return NULL;
31718
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
748 n = PySequence_Fast_GET_SIZE(b);
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
749 if (n == 0) {
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
750 /* short circuit when b is empty */
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
751 Py_DECREF(b);
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
752 Py_RETURN_NONE;
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
753 }
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
754 m = self->ob_size;
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
755 if (list_resize(self, m + n) == -1) {
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
756 Py_DECREF(b);
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
757 return NULL;
31718
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
758 }
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
759 /* note that we may still have self == b here for the
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
760 * situation a.extend(a), but the following code works
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
761 * in that case too. Just make sure to resize self
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
762 * before calling PySequence_Fast_ITEMS.
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
763 */
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
764 /* populate the end of self with b's items */
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
765 src = PySequence_Fast_ITEMS(b);
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
766 dest = self->ob_item + m;
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
767 for (i = 0; i < n; i++) {
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
768 PyObject *o = src[i];
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
769 Py_INCREF(o);
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
770 dest[i] = o;
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
771 }
0420d3cfe4a4 Get rid of listextend_internal() and explain why the special case
Armin Rigo <arigo@tunes.org>
parents: 31686
diff changeset
772 Py_DECREF(b);
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
773 Py_RETURN_NONE;
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
774 }
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
775
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
776 it = PyObject_GetIter(b);
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
777 if (it == NULL)
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
778 return NULL;
31643
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
779 iternext = *it->ob_type->tp_iternext;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
780
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
781 /* Guess a result list size. */
36484
20f75d5e79e9 Renamed _length_cue() to __length_hint__(). See:
Armin Rigo <arigo@tunes.org>
parents: 35999
diff changeset
782 n = _PyObject_LengthHint(b);
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
783 if (n < 0) {
35365
8b07d731be60 SF bug #1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 33679
diff changeset
784 if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
8b07d731be60 SF bug #1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 33679
diff changeset
785 !PyErr_ExceptionMatches(PyExc_AttributeError)) {
8b07d731be60 SF bug #1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 33679
diff changeset
786 Py_DECREF(it);
8b07d731be60 SF bug #1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 33679
diff changeset
787 return NULL;
8b07d731be60 SF bug #1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 33679
diff changeset
788 }
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
789 PyErr_Clear();
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
790 n = 8; /* arbitrary */
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
791 }
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
792 m = self->ob_size;
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
793 mn = m + n;
33589
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
794 if (mn >= m) {
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
795 /* Make room. */
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
796 if (list_resize(self, mn) == -1)
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
797 goto error;
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
798 /* Make the list sane again. */
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
799 self->ob_size = m;
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
800 }
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
801 /* Else m + n overflowed; on the chance that n lied, and there really
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
802 * is enough room, ignore it. If n was telling the truth, we'll
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
803 * eventually run out of memory during the loop.
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
804 */
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
805
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
806 /* Run iterator to exhaustion. */
33589
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
807 for (;;) {
31643
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
808 PyObject *item = iternext(it);
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
809 if (item == NULL) {
31643
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
810 if (PyErr_Occurred()) {
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
811 if (PyErr_ExceptionMatches(PyExc_StopIteration))
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
812 PyErr_Clear();
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
813 else
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
814 goto error;
a0fb27b44d78 Now that list.extend() is at the root of many list operations, it becomes
Raymond Hettinger <python@rcn.com>
parents: 31642
diff changeset
815 }
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
816 break;
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
817 }
33589
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
818 if (self->ob_size < self->allocated) {
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
819 /* steals ref */
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
820 PyList_SET_ITEM(self, self->ob_size, item);
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
821 ++self->ob_size;
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
822 }
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
823 else {
31905
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
824 int status = app1(self, item);
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
825 Py_DECREF(item); /* append creates a new ref */
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
826 if (status < 0)
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
827 goto error;
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
828 }
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
829 }
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
830
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
831 /* Cut back result list if initial guess was too large. */
33589
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
832 if (self->ob_size < self->allocated)
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
833 list_resize(self, self->ob_size); /* shrinking can't fail */
23036c7af687 Checkin Tim's fix to an error discussed on python-dev.
Raymond Hettinger <python@rcn.com>
parents: 33464
diff changeset
834
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
835 Py_DECREF(it);
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
836 Py_RETURN_NONE;
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
837
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
838 error:
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
839 Py_DECREF(it);
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
840 return NULL;
14161
52fd496d3532 Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents: 14125
diff changeset
841 }
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
842
31642
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31641
diff changeset
843 PyObject *
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31641
diff changeset
844 _PyList_Extend(PyListObject *self, PyObject *b)
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31641
diff changeset
845 {
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31641
diff changeset
846 return listextend(self, b);
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31641
diff changeset
847 }
30e305bfdddb Eliminate a big block of duplicate code in PySequence_List() by
Raymond Hettinger <python@rcn.com>
parents: 31641
diff changeset
848
9317
003b42c9f24d listextend(): New method which implements L.extend(L2).
Barry Warsaw <barry@python.org>
parents: 9275
diff changeset
849 static PyObject *
31641
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
850 list_inplace_concat(PyListObject *self, PyObject *other)
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
851 {
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
852 PyObject *result;
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
853
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
854 result = listextend(self, other);
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
855 if (result == NULL)
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
856 return result;
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
857 Py_DECREF(result);
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
858 Py_INCREF(self);
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
859 return (PyObject *)self;
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
860 }
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
861
eea674bf7b59 list_inplace_concat() is now expressed in terms of list_extend() which
Raymond Hettinger <python@rcn.com>
parents: 31636
diff changeset
862 static PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
863 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
864 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
865 Py_ssize_t i = -1;
31544
72e970c0accc Double the speed of list.pop() which was spending most of its time parsing
Raymond Hettinger <python@rcn.com>
parents: 31528
diff changeset
866 PyObject *v, *arg = NULL;
32874
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
867 int status;
31544
72e970c0accc Double the speed of list.pop() which was spending most of its time parsing
Raymond Hettinger <python@rcn.com>
parents: 31528
diff changeset
868
72e970c0accc Double the speed of list.pop() which was spending most of its time parsing
Raymond Hettinger <python@rcn.com>
parents: 31528
diff changeset
869 if (!PyArg_UnpackTuple(args, "pop", 0, 1, &arg))
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
870 return NULL;
31544
72e970c0accc Double the speed of list.pop() which was spending most of its time parsing
Raymond Hettinger <python@rcn.com>
parents: 31528
diff changeset
871 if (arg != NULL) {
72e970c0accc Double the speed of list.pop() which was spending most of its time parsing
Raymond Hettinger <python@rcn.com>
parents: 31528
diff changeset
872 if (PyInt_Check(arg))
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
873 i = PyInt_AS_LONG((PyIntObject*) arg);
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
874 else if (!PyArg_ParseTuple(args, "|n:pop", &i))
31550
bdf006dee698 Keep the list.pop() optimization while restoring the many possibility
Raymond Hettinger <python@rcn.com>
parents: 31544
diff changeset
875 return NULL;
31544
72e970c0accc Double the speed of list.pop() which was spending most of its time parsing
Raymond Hettinger <python@rcn.com>
parents: 31528
diff changeset
876 }
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
877 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
878 /* 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
879 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
880 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
881 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
882 if (i < 0)
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
883 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
884 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
885 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
886 return NULL;
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
887 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
888 v = self->ob_item[i];
31520
58d5ede5f4ce Optimize list.pop() for the common special case of popping off the end.
Raymond Hettinger <python@rcn.com>
parents: 31519
diff changeset
889 if (i == self->ob_size - 1) {
32874
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
890 status = list_resize(self, self->ob_size - 1);
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
891 assert(status >= 0);
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
892 return v; /* and v now owns the reference the list had */
31520
58d5ede5f4ce Optimize list.pop() for the common special case of popping off the end.
Raymond Hettinger <python@rcn.com>
parents: 31519
diff changeset
893 }
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
894 Py_INCREF(v);
32874
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
895 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
896 assert(status >= 0);
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
897 /* Use status, so that in a release build compilers don't
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
898 * complain about the unused name.
62c53b8e4aeb list_ass_slice(): Document the obscure new intent that deleting a slice
Tim Peters <tim.peters@gmail.com>
parents: 32869
diff changeset
899 */
33053
1af3ad49d06f Previous commit was viewed as "perverse". Changed to just cast the unused
Brett Cannon <bcannon@gmail.com>
parents: 32908
diff changeset
900 (void) status;
1af3ad49d06f Previous commit was viewed as "perverse". Changed to just cast the unused
Brett Cannon <bcannon@gmail.com>
parents: 32908
diff changeset
901
1af3ad49d06f Previous commit was viewed as "perverse". Changed to just cast the unused
Brett Cannon <bcannon@gmail.com>
parents: 32908
diff changeset
902 return v;
8595
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
903 }
635c3d94a948 Added doc strings for methods and a new pop() method.
Guido van Rossum <guido@python.org>
parents: 8515
diff changeset
904
24309
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
905 /* 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
906 static void
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
907 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
908 {
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
909 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
910
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
911 --hi;
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
912 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
913 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
914 *lo = *hi;
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
915 *hi = t;
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
916 ++lo;
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
917 --hi;
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
918 }
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
919 }
08d307cbccd9 Cleanup: Define one internal utility for reversing a list slice, and
Tim Peters <tim.peters@gmail.com>
parents: 24280
diff changeset
920
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
921 /* 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
922 * 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
923 */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
924
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
925 /* 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
926 * 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
927 * 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
928 * 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
929 * 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
930 */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
931 static int
24311
a353d6a45276 Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents: 24310
diff changeset
932 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
933 {
24207
561e33a6a313 docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents: 23929
diff changeset
934 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
935 PyObject *args;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
936 Py_ssize_t i;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
937
24540
614358e3c4f6 Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents: 24515
diff changeset
938 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
939 /* 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
940 * 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
941 */
24207
561e33a6a313 docompare(): Use PyTuple_New instead of Py_BuildValue to build compare's
Tim Peters <tim.peters@gmail.com>
parents: 23929
diff changeset
942 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
943 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
944 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
945 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
946 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
947 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
948 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
949 res = PyObject_Call(compare, args, NULL);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
950 Py_DECREF(args);
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
951 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
952 return -1;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
953 if (!PyInt_Check(res)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
954 Py_DECREF(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
955 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
956 "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
957 return -1;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
958 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
959 i = PyInt_AsLong(res);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
960 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
961 return i < 0;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
962 }
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
963
24540
614358e3c4f6 Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents: 24515
diff changeset
964 /* 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
965 * 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
966 * 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
967 * 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
968 */
614358e3c4f6 Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents: 24515
diff changeset
969 #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
970 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
971 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
972
614358e3c4f6 Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents: 24515
diff changeset
973 /* 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
974 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
975 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
976 */
24540
614358e3c4f6 Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents: 24515
diff changeset
977 #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
978 if (k)
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
979
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
980 /* 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
981 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
982 elements.
8515
16b3a121ff77 Minor cleanup by Tim after my changes:
Guido van Rossum <guido@python.org>
parents: 8511
diff changeset
983 [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
984 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
985 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
986 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
987 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
988 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
989 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
990 */
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
991 static int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
992 binarysort(PyObject **lo, PyObject **hi, PyObject **start, PyObject *compare)
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
993 /* 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
994 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
995 register Py_ssize_t k;
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
996 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
997 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
998
24311
a353d6a45276 Cleanup yielding a small speed boost: before rich comparisons were
Tim Peters <tim.peters@gmail.com>
parents: 24310
diff changeset
999 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
1000 /* 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
1001 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
1002 ++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
1003 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
1004 /* 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
1005 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
1006 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
1007 pivot = *r;
24313
658e12cc0336 binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents: 24312
diff changeset
1008 /* Invariants:
658e12cc0336 binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents: 24312
diff changeset
1009 * 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
1010 * 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
1011 * 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
1012 */
658e12cc0336 binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents: 24312
diff changeset
1013 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
1014 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
1015 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
1016 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
1017 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
1018 else
24313
658e12cc0336 binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents: 24312
diff changeset
1019 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
1020 } while (l < r);
24313
658e12cc0336 binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents: 24312
diff changeset
1021 assert(l == r);
658e12cc0336 binarysort() cleanup: Documented the key invariants, explained why they
Tim Peters <tim.peters@gmail.com>
parents: 24312
diff changeset
1022 /* 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
1023 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
1024 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
1025 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
1026 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
1027 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
1028 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
1029 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
1030 *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
1031 *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
1032 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1033 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
1034
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1035 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
1036 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
1037 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1038
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1039 /*
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1040 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
1041 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
1042
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1043 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
1044
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1045 or the longest descending sequence, with
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
1046
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1047 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
1048
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1049 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
1050 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
1051 "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
1052 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
1053 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
1054
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1055 Returns -1 in case of error.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1056 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1057 static Py_ssize_t
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1058 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
1059 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1060 Py_ssize_t k;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1061 Py_ssize_t 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
1062
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1063 assert(lo < hi);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1064 *descending = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1065 ++lo;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1066 if (lo == hi)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1067 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
1068
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1069 n = 2;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1070 IFLT(*lo, *(lo-1)) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1071 *descending = 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1072 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
1073 IFLT(*lo, *(lo-1))
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1074 ;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1075 else
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1076 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1077 }
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
1078 }
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1079 else {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1080 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
1081 IFLT(*lo, *(lo-1))
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1082 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
1083 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1084 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1085
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1086 return n;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1087 fail:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1088 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1089 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1090
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1091 /*
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1092 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
1093 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
1094 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
1095 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
1096
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1097 "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
1098
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1099 "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
1100 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
1101
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1102 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
1103
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1104 a[k-1] < key <= a[k]
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1105
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1106 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
1107 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
1108 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
1109
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1110 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
1111 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1112 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1113 gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1114 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1115 Py_ssize_t ofs;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1116 Py_ssize_t lastofs;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1117 Py_ssize_t k;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1118
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1119 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
1120
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1121 a += hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1122 lastofs = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1123 ofs = 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1124 IFLT(*a, key) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1125 /* a[hint] < key -- gallop right, until
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1126 * 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
1127 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1128 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1129 while (ofs < maxofs) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1130 IFLT(a[ofs], key) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1131 lastofs = ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1132 ofs = (ofs << 1) + 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1133 if (ofs <= 0) /* int overflow */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1134 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1135 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1136 else /* key <= a[hint + ofs] */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1137 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1138 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1139 if (ofs > maxofs)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1140 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1141 /* 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
1142 lastofs += hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1143 ofs += hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1144 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1145 else {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1146 /* key <= a[hint] -- gallop left, until
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1147 * 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
1148 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1149 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1150 while (ofs < maxofs) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1151 IFLT(*(a-ofs), key)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1152 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1153 /* key <= a[hint - ofs] */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1154 lastofs = ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1155 ofs = (ofs << 1) + 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1156 if (ofs <= 0) /* int overflow */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1157 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1158 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1159 if (ofs > maxofs)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1160 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1161 /* 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
1162 k = lastofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1163 lastofs = hint - ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1164 ofs = hint - k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1165 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1166 a -= hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1167
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1168 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
1169 /* 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
1170 * 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
1171 * 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
1172 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1173 ++lastofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1174 while (lastofs < ofs) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1175 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1176
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1177 IFLT(a[m], key)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1178 lastofs = m+1; /* a[m] < key */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1179 else
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1180 ofs = m; /* key <= a[m] */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1181 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1182 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
1183 return ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1184
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1185 fail:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1186 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1187 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1188
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1189 /*
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1190 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
1191 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
1192
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1193 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
1194
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1195 a[k-1] <= key < a[k]
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1196
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1197 or -1 if error.
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 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
1200 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
1201 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
1202 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1203 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1204 gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1205 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1206 Py_ssize_t ofs;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1207 Py_ssize_t lastofs;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1208 Py_ssize_t k;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1209
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1210 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
1211
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1212 a += hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1213 lastofs = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1214 ofs = 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1215 IFLT(key, *a) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1216 /* key < a[hint] -- gallop left, until
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1217 * 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
1218 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1219 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1220 while (ofs < maxofs) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1221 IFLT(key, *(a-ofs)) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1222 lastofs = ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1223 ofs = (ofs << 1) + 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1224 if (ofs <= 0) /* int overflow */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1225 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1226 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1227 else /* a[hint - ofs] <= key */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1228 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1229 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1230 if (ofs > maxofs)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1231 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1232 /* 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
1233 k = lastofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1234 lastofs = hint - ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1235 ofs = hint - k;
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 else {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1238 /* a[hint] <= key -- gallop right, until
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1239 * 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
1240 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1241 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1242 while (ofs < maxofs) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1243 IFLT(key, a[ofs])
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1244 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1245 /* a[hint + ofs] <= key */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1246 lastofs = ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1247 ofs = (ofs << 1) + 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1248 if (ofs <= 0) /* int overflow */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1249 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1250 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1251 if (ofs > maxofs)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1252 ofs = maxofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1253 /* 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
1254 lastofs += hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1255 ofs += hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1256 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1257 a -= hint;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1258
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1259 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
1260 /* 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
1261 * 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
1262 * 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
1263 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1264 ++lastofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1265 while (lastofs < ofs) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1266 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
24456
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 IFLT(key, a[m])
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1269 ofs = m; /* key < a[m] */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1270 else
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1271 lastofs = m+1; /* a[m] <= key */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1272 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1273 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
1274 return ofs;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1275
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1276 fail:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1277 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1278 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1279
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1280 /* 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
1281 * 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
1282 * 32 * phi ** MAX_MERGE_PENDING
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1283 * 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
1284 * with 2**64 elements.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1285 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1286 #define MAX_MERGE_PENDING 85
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1287
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
1288 /* 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
1289 * 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
1290 */
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
1291 #define MIN_GALLOP 7
24456
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 /* Avoid malloc for small temp arrays. */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1294 #define MERGESTATE_TEMP_SIZE 256
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1295
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1296 /* 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
1297 * 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
1298 */
24727
b5640e74aa5c 1. Combined the base and length arrays into a single array of structs.
Tim Peters <tim.peters@gmail.com>
parents: 24657
diff changeset
1299 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
1300 PyObject **base;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1301 Py_ssize_t len;
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 };
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
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1304 typedef struct s_MergeState {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1305 /* 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
1306 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
1307
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
1308 /* 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
1309 * 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
1310 * 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
1311 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1312 Py_ssize_t 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
1313
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1314 /* '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
1315 * alloced entries.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1316 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1317 PyObject **a; /* may point to temparray below */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1318 Py_ssize_t alloced;
24456
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 /* 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
1321 * 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
1322 * 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
1323 *
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
1324 * 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
1325 *
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1326 * 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
1327 * 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
1328 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1329 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
1330 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
1331
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1332 /* '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
1333 PyObject *temparray[MERGESTATE_TEMP_SIZE];
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1334 } MergeState;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1335
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1336 /* Conceptually a MergeState's constructor. */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1337 static void
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1338 merge_init(MergeState *ms, PyObject *compare)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1339 {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1340 assert(ms != NULL);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1341 ms->compare = compare;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1342 ms->a = ms->temparray;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1343 ms->alloced = MERGESTATE_TEMP_SIZE;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1344 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
1345 ms->min_gallop = MIN_GALLOP;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1346 }
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 /* 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
1349 * 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
1350 * 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
1351 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1352 static void
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1353 merge_freemem(MergeState *ms)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1354 {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1355 assert(ms != NULL);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1356 if (ms->a != ms->temparray)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1357 PyMem_Free(ms->a);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1358 ms->a = ms->temparray;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1359 ms->alloced = MERGESTATE_TEMP_SIZE;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1360 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1361
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1362 /* 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
1363 * 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
1364 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1365 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1366 merge_getmem(MergeState *ms, Py_ssize_t need)
24456
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 assert(ms != NULL);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1369 if (need <= ms->alloced)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1370 return 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1371 /* 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
1372 * 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
1373 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1374 merge_freemem(ms);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1375 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
1376 if (ms->a) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1377 ms->alloced = need;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1378 return 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1379 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1380 PyErr_NoMemory();
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1381 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
1382 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1383 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1384 #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
1385 merge_getmem(MS, NEED))
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1386
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1387 /* 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
1388 * 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
1389 * 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
1390 * 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
1391 * 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
1392 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1393 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1394 merge_lo(MergeState *ms, PyObject **pa, Py_ssize_t na,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1395 PyObject **pb, Py_ssize_t nb)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1396 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1397 Py_ssize_t k;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1398 PyObject *compare;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1399 PyObject **dest;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1400 int result = -1; /* guilty until proved innocent */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1401 Py_ssize_t min_gallop = ms->min_gallop;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1402
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1403 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
1404 if (MERGE_GETMEM(ms, na) < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1405 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1406 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
1407 dest = pa;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1408 pa = ms->a;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1409
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1410 *dest++ = *pb++;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1411 --nb;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1412 if (nb == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1413 goto Succeed;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1414 if (na == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1415 goto CopyB;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1416
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1417 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
1418 for (;;) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1419 Py_ssize_t acount = 0; /* # of times A won in a row */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1420 Py_ssize_t 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
1421
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1422 /* 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
1423 * appears to win consistently.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1424 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1425 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
1426 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
1427 k = ISLT(*pb, *pa, compare);
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1428 if (k) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1429 if (k < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1430 goto Fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1431 *dest++ = *pb++;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1432 ++bcount;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1433 acount = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1434 --nb;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1435 if (nb == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1436 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
1437 if (bcount >= min_gallop)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1438 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
1439 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
1440 else {
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1441 *dest++ = *pa++;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1442 ++acount;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1443 bcount = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1444 --na;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1445 if (na == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1446 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
1447 if (acount >= min_gallop)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1448 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1449 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1450 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1451
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1452 /* 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
1453 * 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
1454 * (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
1455 * anymore.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1456 */
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
1457 ++min_gallop;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1458 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
1459 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
1460 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
1461 ms->min_gallop = min_gallop;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1462 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
1463 acount = 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 if (k < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1466 goto Fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1467 memcpy(dest, pa, k * sizeof(PyObject *));
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1468 dest += k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1469 pa += k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1470 na -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1471 if (na == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1472 goto CopyB;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1473 /* 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
1474 * 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
1475 * that it is.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1476 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1477 if (na == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1478 goto Succeed;
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
1479 }
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1480 *dest++ = *pb++;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1481 --nb;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1482 if (nb == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1483 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
1484
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1485 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
1486 bcount = k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1487 if (k) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1488 if (k < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1489 goto Fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1490 memmove(dest, pb, k * sizeof(PyObject *));
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1491 dest += k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1492 pb += k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1493 nb -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1494 if (nb == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1495 goto Succeed;
8428
3e653e204ad7 Tim's quicksort on May 25.
Guido van Rossum <guido@python.org>
parents: 8323
diff changeset
1496 }
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1497 *dest++ = *pa++;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1498 --na;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1499 if (na == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1500 goto CopyB;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1501 } 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
1502 ++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
1503 ms->min_gallop = min_gallop;
24456
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 Succeed:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1506 result = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1507 Fail:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1508 if (na)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1509 memcpy(dest, pa, na * sizeof(PyObject*));
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1510 return result;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1511 CopyB:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1512 assert(na == 1 && nb > 0);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1513 /* 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
1514 memmove(dest, pb, nb * sizeof(PyObject *));
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1515 dest[nb] = *pa;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1516 return 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1517 }
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1518
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1519 /* 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
1520 * 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
1521 * 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
1522 * 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
1523 * 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
1524 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1525 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1526 merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1527 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1528 Py_ssize_t k;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1529 PyObject *compare;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1530 PyObject **dest;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1531 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
1532 PyObject **basea;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1533 PyObject **baseb;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1534 Py_ssize_t min_gallop = ms->min_gallop;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1535
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1536 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
1537 if (MERGE_GETMEM(ms, nb) < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1538 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1539 dest = pb + nb - 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1540 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
1541 basea = pa;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1542 baseb = ms->a;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1543 pb = ms->a + nb - 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1544 pa += na - 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1545
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1546 *dest-- = *pa--;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1547 --na;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1548 if (na == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1549 goto Succeed;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1550 if (nb == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1551 goto CopyA;
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 compare = ms->compare;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1554 for (;;) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1555 Py_ssize_t acount = 0; /* # of times A won in a row */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1556 Py_ssize_t bcount = 0; /* # of times B won in a row */
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1557
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1558 /* 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
1559 * appears to win consistently.
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 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
1562 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
1563 k = ISLT(*pb, *pa, compare);
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1564 if (k) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1565 if (k < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1566 goto Fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1567 *dest-- = *pa--;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1568 ++acount;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1569 bcount = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1570 --na;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1571 if (na == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1572 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
1573 if (acount >= min_gallop)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1574 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
1575 }
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1576 else {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1577 *dest-- = *pb--;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1578 ++bcount;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1579 acount = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1580 --nb;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1581 if (nb == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1582 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
1583 if (bcount >= min_gallop)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1584 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1585 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1586 }
8164
ea59bb921380 Quicksort retuned by Tim Peters.
Guido van Rossum <guido@python.org>
parents: 7986
diff changeset
1587
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1588 /* 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
1589 * 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
1590 * (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
1591 * anymore.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1592 */
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
1593 ++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
1594 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
1595 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
1596 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
1597 ms->min_gallop = min_gallop;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1598 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
1599 if (k < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1600 goto Fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1601 k = na - k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1602 acount = k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1603 if (k) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1604 dest -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1605 pa -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1606 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
1607 na -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1608 if (na == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1609 goto Succeed;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1610 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1611 *dest-- = *pb--;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1612 --nb;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1613 if (nb == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1614 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
1615
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1616 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
1617 if (k < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1618 goto Fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1619 k = nb - k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1620 bcount = k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1621 if (k) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1622 dest -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1623 pb -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1624 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
1625 nb -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1626 if (nb == 1)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1627 goto CopyA;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1628 /* 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
1629 * 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
1630 * that it is.
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 if (nb == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1633 goto Succeed;
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1634 }
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1635 *dest-- = *pa--;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1636 --na;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1637 if (na == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1638 goto Succeed;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1639 } 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
1640 ++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
1641 ms->min_gallop = min_gallop;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1642 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1643 Succeed:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1644 result = 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1645 Fail:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1646 if (nb)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1647 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
1648 return result;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1649 CopyA:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1650 assert(nb == 1 && na > 0);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1651 /* 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
1652 dest -= na;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1653 pa -= na;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1654 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
1655 *dest = *pb;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1656 return 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1657 }
6814
0fe3dc28b92c Modified quicksort by Raymund Galvin, after studying the GNU libg++
Guido van Rossum <guido@python.org>
parents: 6154
diff changeset
1658
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1659 /* 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
1660 * 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
1661 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1662 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1663 merge_at(MergeState *ms, Py_ssize_t i)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1664 {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1665 PyObject **pa, **pb;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1666 Py_ssize_t na, nb;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1667 Py_ssize_t k;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1668 PyObject *compare;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1669
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1670 assert(ms != NULL);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1671 assert(ms->n >= 2);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1672 assert(i >= 0);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1673 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
1674
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
1675 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
1676 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
1677 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
1678 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
1679 assert(na > 0 && nb > 0);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1680 assert(pa + na == pb);
8322
e6ef0e39e35c Tim's quicksort on May 10.
Guido van Rossum <guido@python.org>
parents: 8186
diff changeset
1681
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1682 /* 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
1683 * 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
1684 * 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
1685 */
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
1686 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
1687 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
1688 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
1689 --ms->n;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1690
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1691 /* 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
1692 * ignored (already in place).
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1693 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1694 compare = ms->compare;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1695 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
1696 if (k < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1697 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1698 pa += k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1699 na -= k;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1700 if (na == 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1701 return 0;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1702
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1703 /* 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
1704 * ignored (already in place).
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 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
1707 if (nb <= 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1708 return nb;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1709
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1710 /* 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
1711 * min(na, nb) elements.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1712 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1713 if (na <= nb)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1714 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
1715 else
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1716 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
1717 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1718
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1719 /* 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
1720 * 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
1721 *
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1722 * 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
1723 * 2. len[-2] > len[-1]
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1724 *
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1725 * See listsort.txt for more info.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1726 *
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1727 * 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
1728 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1729 static int
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1730 merge_collapse(MergeState *ms)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1731 {
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
1732 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
1733
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1734 assert(ms);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1735 while (ms->n > 1) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1736 Py_ssize_t 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
1737 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
1738 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
1739 --n;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1740 if (merge_at(ms, n) < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1741 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1742 }
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
1743 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
1744 if (merge_at(ms, n) < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1745 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
1746 }
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1747 else
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1748 break;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1749 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1750 return 0;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1751 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1752
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1753 /* 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
1754 * 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
1755 *
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1756 * 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
1757 */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1758 static int
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1759 merge_force_collapse(MergeState *ms)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1760 {
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
1761 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
1762
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1763 assert(ms);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1764 while (ms->n > 1) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1765 Py_ssize_t 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
1766 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
1767 --n;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1768 if (merge_at(ms, n) < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1769 return -1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1770 }
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1771 return 0;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1772 }
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
1773
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1774 /* 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
1775 * 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
1776 *
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1777 * 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
1778 * 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
1779 * 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
1780 * 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
1781 *
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1782 * See listsort.txt for more info.
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1783 */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1784 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1785 merge_compute_minrun(Py_ssize_t n)
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1786 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1787 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1788
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1789 assert(n >= 0);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1790 while (n >= 64) {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1791 r |= n & 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1792 n >>= 1;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1793 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1794 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
1795 }
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
1796
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1797 /* Special wrapper to support stable sorting using the decorate-sort-undecorate
33448
5570fae1f01c Typo fix: 'comparisions' is not a word
Andrew M. Kuchling <amk@amk.ca>
parents: 33114
diff changeset
1798 pattern. Holds a key which is used for comparisons and the original record
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1799 which is returned during the undecorate phase. By exposing only the key
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1800 during comparisons, the underlying sort stability characteristics are left
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1801 unchanged. Also, if a custom comparison function is used, it will only see
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1802 the key instead of a full record. */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1803
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1804 typedef struct {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1805 PyObject_HEAD
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1806 PyObject *key;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1807 PyObject *value;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1808 } sortwrapperobject;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1809
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1810 PyDoc_STRVAR(sortwrapper_doc, "Object wrapper with a custom sort key.");
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1811 static PyObject *
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1812 sortwrapper_richcompare(sortwrapperobject *, sortwrapperobject *, int);
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1813 static void
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1814 sortwrapper_dealloc(sortwrapperobject *);
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1815
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1816 static PyTypeObject sortwrapper_type = {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1817 PyObject_HEAD_INIT(&PyType_Type)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1818 0, /* ob_size */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1819 "sortwrapper", /* tp_name */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1820 sizeof(sortwrapperobject), /* tp_basicsize */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1821 0, /* tp_itemsize */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1822 /* methods */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1823 (destructor)sortwrapper_dealloc, /* tp_dealloc */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1824 0, /* tp_print */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1825 0, /* tp_getattr */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1826 0, /* tp_setattr */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1827 0, /* tp_compare */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1828 0, /* tp_repr */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1829 0, /* tp_as_number */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1830 0, /* tp_as_sequence */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1831 0, /* tp_as_mapping */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1832 0, /* tp_hash */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1833 0, /* tp_call */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1834 0, /* tp_str */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1835 PyObject_GenericGetAttr, /* tp_getattro */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1836 0, /* tp_setattro */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1837 0, /* tp_as_buffer */
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1838 Py_TPFLAGS_DEFAULT |
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1839 Py_TPFLAGS_HAVE_RICHCOMPARE, /* tp_flags */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1840 sortwrapper_doc, /* tp_doc */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1841 0, /* tp_traverse */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1842 0, /* tp_clear */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1843 (richcmpfunc)sortwrapper_richcompare, /* tp_richcompare */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1844 };
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1845
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1846
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1847 static PyObject *
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1848 sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1849 {
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1850 if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1851 PyErr_SetString(PyExc_TypeError,
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1852 "expected a sortwrapperobject");
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1853 return NULL;
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1854 }
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1855 return PyObject_RichCompare(a->key, b->key, op);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1856 }
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1857
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1858 static void
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1859 sortwrapper_dealloc(sortwrapperobject *so)
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1860 {
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1861 Py_XDECREF(so->key);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1862 Py_XDECREF(so->value);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1863 PyObject_Del(so);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1864 }
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
1865
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1866 /* Returns a new reference to a sortwrapper.
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1867 Consumes the references to the two underlying objects. */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1868
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1869 static PyObject *
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1870 build_sortwrapper(PyObject *key, PyObject *value)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1871 {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1872 sortwrapperobject *so;
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1873
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1874 so = PyObject_New(sortwrapperobject, &sortwrapper_type);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1875 if (so == NULL)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1876 return NULL;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1877 so->key = key;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1878 so->value = value;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1879 return (PyObject *)so;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1880 }
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1881
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1882 /* Returns a new reference to the value underlying the wrapper. */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1883 static PyObject *
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1884 sortwrapper_getvalue(PyObject *so)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1885 {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1886 PyObject *value;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1887
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1888 if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1889 PyErr_SetString(PyExc_TypeError,
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1890 "expected a sortwrapperobject");
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1891 return NULL;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1892 }
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1893 value = ((sortwrapperobject *)so)->value;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1894 Py_INCREF(value);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1895 return value;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1896 }
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1897
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1898 /* Wrapper for user specified cmp functions in combination with a
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1899 specified key function. Makes sure the cmp function is presented
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1900 with the actual key instead of the sortwrapper */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1901
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1902 typedef struct {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1903 PyObject_HEAD
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1904 PyObject *func;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1905 } cmpwrapperobject;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1906
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1907 static void
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1908 cmpwrapper_dealloc(cmpwrapperobject *co)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1909 {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1910 Py_XDECREF(co->func);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1911 PyObject_Del(co);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1912 }
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1913
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1914 static PyObject *
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1915 cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1916 {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1917 PyObject *x, *y, *xx, *yy;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1918
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1919 if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1920 return NULL;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1921 if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
30699
efed489df545 Fix typo found by Neal Norwitz.
Raymond Hettinger <python@rcn.com>
parents: 30697
diff changeset
1922 !PyObject_TypeCheck(y, &sortwrapper_type)) {
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1923 PyErr_SetString(PyExc_TypeError,
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1924 "expected a sortwrapperobject");
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1925 return NULL;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1926 }
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1927 xx = ((sortwrapperobject *)x)->key;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1928 yy = ((sortwrapperobject *)y)->key;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1929 return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1930 }
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1931
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1932 PyDoc_STRVAR(cmpwrapper_doc, "cmp() wrapper for sort with custom keys.");
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1933
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1934 static PyTypeObject cmpwrapper_type = {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1935 PyObject_HEAD_INIT(&PyType_Type)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1936 0, /* ob_size */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1937 "cmpwrapper", /* tp_name */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1938 sizeof(cmpwrapperobject), /* tp_basicsize */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1939 0, /* tp_itemsize */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1940 /* methods */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1941 (destructor)cmpwrapper_dealloc, /* tp_dealloc */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1942 0, /* tp_print */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1943 0, /* tp_getattr */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1944 0, /* tp_setattr */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1945 0, /* tp_compare */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1946 0, /* tp_repr */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1947 0, /* tp_as_number */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1948 0, /* tp_as_sequence */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1949 0, /* tp_as_mapping */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1950 0, /* tp_hash */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1951 (ternaryfunc)cmpwrapper_call, /* tp_call */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1952 0, /* tp_str */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1953 PyObject_GenericGetAttr, /* tp_getattro */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1954 0, /* tp_setattro */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1955 0, /* tp_as_buffer */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1956 Py_TPFLAGS_DEFAULT, /* tp_flags */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1957 cmpwrapper_doc, /* tp_doc */
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1958 };
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1959
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1960 static PyObject *
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1961 build_cmpwrapper(PyObject *cmpfunc)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1962 {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1963 cmpwrapperobject *co;
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
1964
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1965 co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1966 if (co == NULL)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1967 return NULL;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1968 Py_INCREF(cmpfunc);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1969 co->func = cmpfunc;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1970 return (PyObject *)co;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1971 }
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1972
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1973 /* 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
1974 * 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
1975 * 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
1976 * duplicated).
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1977 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1978 static PyObject *
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1979 listsort(PyListObject *self, PyObject *args, PyObject *kwds)
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1980 {
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1981 MergeState ms;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1982 PyObject **lo, **hi;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1983 Py_ssize_t nremaining;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1984 Py_ssize_t minrun;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1985 Py_ssize_t saved_ob_size, saved_allocated;
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
1986 PyObject **saved_ob_item;
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
1987 PyObject **final_ob_item;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1988 PyObject *compare = NULL;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1989 PyObject *result = NULL; /* guilty until proved innocent */
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1990 int reverse = 0;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1991 PyObject *keyfunc = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1992 Py_ssize_t i;
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1993 PyObject *key, *value, *kvpair;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36519
diff changeset
1994 static char *kwlist[] = {"cmp", "key", "reverse", 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
1995
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
1996 assert(self != NULL);
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1997 assert (PyList_Check(self));
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
1998 if (args != NULL) {
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
1999 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2000 kwlist, &compare, &keyfunc, &reverse))
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
2001 return NULL;
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
2002 }
27036
41f7726e3e51 Allow list sort's comparison function to explicitly be None. See SF patch
Skip Montanaro <skip@pobox.com>
parents: 26818
diff changeset
2003 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
2004 compare = NULL;
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2005 if (keyfunc == Py_None)
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2006 keyfunc = NULL;
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2007 if (compare != NULL && keyfunc != NULL) {
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2008 compare = build_cmpwrapper(compare);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2009 if (compare == NULL)
31164
aab3612f2c35 Fix memory error treatment correctly. Going to dsu_fail causes
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31093
diff changeset
2010 return NULL;
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2011 } else
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2012 Py_XINCREF(compare);
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2013
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2014 /* 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
2015 * 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
2016 * 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
2017 * factory, since ob_item may change).
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2018 */
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2019 saved_ob_size = self->ob_size;
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2020 saved_ob_item = self->ob_item;
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
2021 saved_allocated = self->allocated;
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2022 self->ob_size = 0;
32854
dcb241c89f42 Fix obscure breakage (relative to 2.3) in listsort: the test for list
Tim Peters <tim.peters@gmail.com>
parents: 32850
diff changeset
2023 self->ob_item = NULL;
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2024 self->allocated = -1; /* any operation will reset it to >= 0 */
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2025
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2026 if (keyfunc != NULL) {
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2027 for (i=0 ; i < saved_ob_size ; i++) {
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2028 value = saved_ob_item[i];
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
2029 key = PyObject_CallFunctionObjArgs(keyfunc, value,
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2030 NULL);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2031 if (key == NULL) {
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2032 for (i=i-1 ; i>=0 ; i--) {
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2033 kvpair = saved_ob_item[i];
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2034 value = sortwrapper_getvalue(kvpair);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2035 saved_ob_item[i] = value;
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2036 Py_DECREF(kvpair);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2037 }
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2038 goto dsu_fail;
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2039 }
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2040 kvpair = build_sortwrapper(key, value);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2041 if (kvpair == NULL)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2042 goto dsu_fail;
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2043 saved_ob_item[i] = kvpair;
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2044 }
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2045 }
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2046
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2047 /* Reverse sort stability achieved by initially reversing the list,
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2048 applying a stable forward sort, then reversing the final result. */
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2049 if (reverse && saved_ob_size > 1)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2050 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2051
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2052 merge_init(&ms, compare);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2053
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2054 nremaining = saved_ob_size;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2055 if (nremaining < 2)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2056 goto succeed;
24316
dd38291b89fb More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents: 24313
diff changeset
2057
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2058 /* 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
2059 * 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
2060 */
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2061 lo = saved_ob_item;
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2062 hi = lo + nremaining;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2063 minrun = merge_compute_minrun(nremaining);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2064 do {
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2065 int descending;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2066 Py_ssize_t n;
24316
dd38291b89fb More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents: 24313
diff changeset
2067
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2068 /* Identify next run. */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2069 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
2070 if (n < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2071 goto fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2072 if (descending)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2073 reverse_slice(lo, lo + n);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2074 /* 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
2075 if (n < minrun) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2076 const Py_ssize_t force = nremaining <= minrun ?
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2077 nremaining : minrun;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2078 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
2079 goto fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2080 n = force;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2081 }
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2082 /* 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
2083 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
2084 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
2085 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
2086 ++ms.n;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2087 if (merge_collapse(&ms) < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2088 goto fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2089 /* Advance to find next run. */
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2090 lo += n;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2091 nremaining -= n;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2092 } while (nremaining);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2093 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
2094
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2095 if (merge_force_collapse(&ms) < 0)
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2096 goto fail;
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2097 assert(ms.n == 1);
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2098 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
2099 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
2100
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2101 succeed:
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2102 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
2103 fail:
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2104 if (keyfunc != NULL) {
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2105 for (i=0 ; i < saved_ob_size ; i++) {
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2106 kvpair = saved_ob_item[i];
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2107 value = sortwrapper_getvalue(kvpair);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2108 saved_ob_item[i] = value;
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2109 Py_DECREF(kvpair);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2110 }
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2111 }
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2112
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2113 if (self->allocated != -1 && result != NULL) {
32854
dcb241c89f42 Fix obscure breakage (relative to 2.3) in listsort: the test for list
Tim Peters <tim.peters@gmail.com>
parents: 32850
diff changeset
2114 /* The user mucked with the list during the sort,
dcb241c89f42 Fix obscure breakage (relative to 2.3) in listsort: the test for list
Tim Peters <tim.peters@gmail.com>
parents: 32850
diff changeset
2115 * and we don't already have another error to report.
dcb241c89f42 Fix obscure breakage (relative to 2.3) in listsort: the test for list
Tim Peters <tim.peters@gmail.com>
parents: 32850
diff changeset
2116 */
dcb241c89f42 Fix obscure breakage (relative to 2.3) in listsort: the test for list
Tim Peters <tim.peters@gmail.com>
parents: 32850
diff changeset
2117 PyErr_SetString(PyExc_ValueError, "list modified during sort");
dcb241c89f42 Fix obscure breakage (relative to 2.3) in listsort: the test for list
Tim Peters <tim.peters@gmail.com>
parents: 32850
diff changeset
2118 result = NULL;
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2119 }
31093
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2120
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2121 if (reverse && saved_ob_size > 1)
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2122 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2123
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2124 merge_freemem(&ms);
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2125
f2e7646f7763 Fixes and tests for various "holding pointers when arbitrary Python code
Michael W. Hudson <mwh@python.net>
parents: 31041
diff changeset
2126 dsu_fail:
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2127 final_ob_item = self->ob_item;
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2128 i = self->ob_size;
26161
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2129 self->ob_size = saved_ob_size;
dd10d97d329a SF patch 637176: list.sort crasher
Tim Peters <tim.peters@gmail.com>
parents: 26064
diff changeset
2130 self->ob_item = saved_ob_item;
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
2131 self->allocated = saved_allocated;
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2132 if (final_ob_item != NULL) {
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2133 /* we cannot use list_clear() for this because it does not
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2134 guarantee that the list is really empty when it returns */
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2135 while (--i >= 0) {
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2136 Py_XDECREF(final_ob_item[i]);
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2137 }
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2138 PyMem_FREE(final_ob_item);
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2139 }
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2140 Py_XDECREF(compare);
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2141 Py_XINCREF(result);
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2142 return result;
4673
a3cdc8164990 Added new quicksort implementation, tailored to sorting arrays of
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
2143 }
24316
dd38291b89fb More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents: 24313
diff changeset
2144 #undef IFLT
24540
614358e3c4f6 Sped the usual case for sorting by calling PyObject_RichCompareBool
Tim Peters <tim.peters@gmail.com>
parents: 24515
diff changeset
2145 #undef ISLT
24316
dd38291b89fb More sort cleanup: Moved the special cases from samplesortslice into
Tim Peters <tim.peters@gmail.com>
parents: 24313
diff changeset
2146
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
2147 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
2148 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
2149 {
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
2150 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
2151 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
2152 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
2153 }
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2154 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
8511
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
2155 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
2156 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
2157 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
2158 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
2159 }
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
2160
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
2161 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
2162 listreverse(PyListObject *self)
16581
c0cdf82599e0 Fix core dump whenever PyList_Reverse() was called.
Guido van Rossum <guido@python.org>
parents: 15969
diff changeset
2163 {
24312
c4b3a2b247ac listreverse(): Don't call the new reverse_slice unless the list
Tim Peters <tim.peters@gmail.com>
parents: 24311
diff changeset
2164 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
2165 reverse_slice(self->ob_item, self->ob_item + self->ob_size);
31907
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
2166 Py_RETURN_NONE;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2167 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2168
47
05057e3660f8 Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
2169 int
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
2170 PyList_Reverse(PyObject *v)
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
2171 {
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
2172 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
2173
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2174 if (v == NULL || !PyList_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2175 PyErr_BadInternalCall();
2159
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
2176 return -1;
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
2177 }
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
2178 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
2179 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
2180 return 0;
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
2181 }
a619a61aad1e added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents: 2143
diff changeset
2182
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2183 PyObject *
13472
dbe83fd23393 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
2184 PyList_AsTuple(PyObject *v)
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2185 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2186 PyObject *w;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2187 PyObject **p;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2188 Py_ssize_t n;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2189 if (v == NULL || !PyList_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2190 PyErr_BadInternalCall();
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2191 return NULL;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2192 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2193 n = ((PyListObject *)v)->ob_size;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2194 w = PyTuple_New(n);
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2195 if (w == NULL)
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2196 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2197 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
2198 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
2199 (void *)((PyListObject *)v)->ob_item,
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2200 n*sizeof(PyObject *));
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2201 while (--n >= 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2202 Py_INCREF(*p);
1836
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2203 p++;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2204 }
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2205 return w;
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2206 }
1203f0629527 Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
2207
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2208 static PyObject *
29510
01e925371d7c SF #754014: list.index() should accept optional start, end arguments
Raymond Hettinger <python@rcn.com>
parents: 29198
diff changeset
2209 listindex(PyListObject *self, PyObject *args)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2210 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2211 Py_ssize_t i, start=0, stop=self->ob_size;
29510
01e925371d7c SF #754014: list.index() should accept optional start, end arguments
Raymond Hettinger <python@rcn.com>
parents: 29198
diff changeset
2212 PyObject *v;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
2213
29519
c6eb569e4b79 Use _PyEval_SliceIndex to handle list.index() calls with
Walter Dörwald <walter@livinglogic.de>
parents: 29513
diff changeset
2214 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
c6eb569e4b79 Use _PyEval_SliceIndex to handle list.index() calls with
Walter Dörwald <walter@livinglogic.de>
parents: 29513
diff changeset
2215 _PyEval_SliceIndex, &start,
c6eb569e4b79 Use _PyEval_SliceIndex to handle list.index() calls with
Walter Dörwald <walter@livinglogic.de>
parents: 29513
diff changeset
2216 _PyEval_SliceIndex, &stop))
29510
01e925371d7c SF #754014: list.index() should accept optional start, end arguments
Raymond Hettinger <python@rcn.com>
parents: 29198
diff changeset
2217 return NULL;
29513
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2218 if (start < 0) {
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2219 start += self->ob_size;
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2220 if (start < 0)
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2221 start = 0;
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2222 }
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2223 if (stop < 0) {
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2224 stop += self->ob_size;
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2225 if (stop < 0)
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2226 stop = 0;
e54e2de46bc3 Fix sloppy index() implementation:
Guido van Rossum <guido@python.org>
parents: 29510
diff changeset
2227 }
33114
e4a73efa4d23 SF patch #1005778, Fix seg fault if list object is modified during list.index()
Neal Norwitz <nnorwitz@gmail.com>
parents: 33053
diff changeset
2228 for (i = start; i < stop && i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2229 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
2230 if (cmp > 0)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2231 return PyInt_FromSsize_t(i);
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2232 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
2233 return NULL;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2234 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2235 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
2236 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2237 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2238
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2239 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
2240 listcount(PyListObject *self, PyObject *v)
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
2241 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2242 Py_ssize_t count = 0;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2243 Py_ssize_t i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
2244
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
2245 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2246 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
2247 if (cmp > 0)
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
2248 count++;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2249 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
2250 return NULL;
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
2251 }
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2252 return PyInt_FromSsize_t(count);
394
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
2253 }
8154385248d8 Added count() method.
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
2254
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2255 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
2256 listremove(PyListObject *self, PyObject *v)
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2257 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2258 Py_ssize_t i;
11846
8b70eaaadf4b Made all list methods use PyArg_ParseTuple(), for more accurate
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
2259
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2260 for (i = 0; i < self->ob_size; i++) {
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2261 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
2262 if (cmp > 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2263 if (list_ass_slice(self, i, i+1,
31907
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
2264 (PyObject *)NULL) == 0)
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
2265 Py_RETURN_NONE;
88d625d928ac Use Py_RETURN_NONE macro where applicable.
Raymond Hettinger <python@rcn.com>
parents: 31906
diff changeset
2266 return NULL;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2267 }
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2268 else if (cmp < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
2269 return NULL;
155
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2270 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2271 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
2272 return NULL;
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2273 }
896c825f2f71 Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
2274
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
2275 static int
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
2276 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
2277 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2278 Py_ssize_t i;
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
2279
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37673
diff changeset
2280 for (i = o->ob_size; --i >= 0; )
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37673
diff changeset
2281 Py_VISIT(o->ob_item[i]);
12990
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
2282 return 0;
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
2283 }
e615596c5c47 Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12952
diff changeset
2284
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2285 static PyObject *
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2286 list_richcompare(PyObject *v, PyObject *w, int op)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2287 {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2288 PyListObject *vl, *wl;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2289 Py_ssize_t i;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2290
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2291 if (!PyList_Check(v) || !PyList_Check(w)) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2292 Py_INCREF(Py_NotImplemented);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2293 return Py_NotImplemented;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2294 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2295
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2296 vl = (PyListObject *)v;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2297 wl = (PyListObject *)w;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2298
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2299 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
2300 /* Shortcut: if the lengths differ, the lists differ */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2301 PyObject *res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2302 if (op == Py_EQ)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2303 res = Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2304 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2305 res = Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2306 Py_INCREF(res);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2307 return res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2308 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2309
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2310 /* Search for the first index where items are different */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2311 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
2312 int k = PyObject_RichCompareBool(vl->ob_item[i],
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2313 wl->ob_item[i], Py_EQ);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2314 if (k < 0)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2315 return NULL;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2316 if (!k)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2317 break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2318 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2319
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2320 if (i >= vl->ob_size || i >= wl->ob_size) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2321 /* No more items to compare -- compare sizes */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2322 Py_ssize_t vs = vl->ob_size;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2323 Py_ssize_t ws = wl->ob_size;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2324 int cmp;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2325 PyObject *res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2326 switch (op) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2327 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
2328 case Py_LE: cmp = vs <= ws; break;
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2329 case Py_EQ: cmp = vs == ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2330 case Py_NE: cmp = vs != ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2331 case Py_GT: cmp = vs > ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2332 case Py_GE: cmp = vs >= ws; break;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2333 default: return NULL; /* cannot happen */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2334 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2335 if (cmp)
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2336 res = Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2337 else
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2338 res = Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2339 Py_INCREF(res);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2340 return res;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2341 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2342
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2343 /* 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
2344 if (op == Py_EQ) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2345 Py_INCREF(Py_False);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2346 return Py_False;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2347 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2348 if (op == Py_NE) {
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2349 Py_INCREF(Py_True);
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2350 return Py_True;
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2351 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2352
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2353 /* Compare the final item again using the proper operator */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2354 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
2355 }
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2356
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2357 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2358 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
2359 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2360 PyObject *arg = NULL;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36519
diff changeset
2361 static char *kwlist[] = {"sequence", 0};
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2362
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2363 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
2364 return -1;
32859
45f3222b84ee * Simplify and speed-up list_resize(). Relying on the newly documented
Raymond Hettinger <python@rcn.com>
parents: 32858
diff changeset
2365
45f3222b84ee * Simplify and speed-up list_resize(). Relying on the newly documented
Raymond Hettinger <python@rcn.com>
parents: 32858
diff changeset
2366 /* Verify list invariants established by PyType_GenericAlloc() */
32860
48eae20d70c5 What if you call lst.__init__() while it is being sorted? :-)
Armin Rigo <arigo@tunes.org>
parents: 32859
diff changeset
2367 assert(0 <= self->ob_size);
48eae20d70c5 What if you call lst.__init__() while it is being sorted? :-)
Armin Rigo <arigo@tunes.org>
parents: 32859
diff changeset
2368 assert(self->ob_size <= self->allocated || self->allocated == -1);
48eae20d70c5 What if you call lst.__init__() while it is being sorted? :-)
Armin Rigo <arigo@tunes.org>
parents: 32859
diff changeset
2369 assert(self->ob_item != NULL ||
48eae20d70c5 What if you call lst.__init__() while it is being sorted? :-)
Armin Rigo <arigo@tunes.org>
parents: 32859
diff changeset
2370 self->allocated == 0 || self->allocated == -1);
32859
45f3222b84ee * Simplify and speed-up list_resize(). Relying on the newly documented
Raymond Hettinger <python@rcn.com>
parents: 32858
diff changeset
2371
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2372 /* Empty previous contents */
32858
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2373 if (self->ob_item != NULL) {
fa0526c30da8 * drop the unreasonable list invariant that ob_item should never come back
Armin Rigo <arigo@tunes.org>
parents: 32857
diff changeset
2374 (void)list_clear(self);
31528
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2375 }
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2376 if (arg != NULL) {
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2377 PyObject *rv = listextend(self, arg);
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2378 if (rv == NULL)
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2379 return -1;
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2380 Py_DECREF(rv);
1233bd95da1a Refactor list_extend() and list_fill() for gains in code size, memory
Raymond Hettinger <python@rcn.com>
parents: 31527
diff changeset
2381 }
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2382 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2383 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2384
21155
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
2385 static long
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
2386 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
2387 {
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
2388 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
2389 return -1;
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
2390 }
bca53a37a8a5 Fix of SF bug #475877 (Mutable subtype instances are hashable).
Guido van Rossum <guido@python.org>
parents: 20277
diff changeset
2391
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2392 static PyObject *list_iter(PyObject *seq);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2393 static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2394
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31164
diff changeset
2395 PyDoc_STRVAR(getitem_doc,
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31164
diff changeset
2396 "x.__getitem__(y) <==> x[y]");
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2397 PyDoc_STRVAR(reversed_doc,
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2398 "L.__reversed__() -- return a reverse iterator over the list");
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2399 PyDoc_STRVAR(append_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2400 "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
2401 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
2402 "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
2403 PyDoc_STRVAR(insert_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2404 "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
2405 PyDoc_STRVAR(pop_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2406 "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
2407 PyDoc_STRVAR(remove_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2408 "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
2409 PyDoc_STRVAR(index_doc,
29510
01e925371d7c SF #754014: list.index() should accept optional start, end arguments
Raymond Hettinger <python@rcn.com>
parents: 29198
diff changeset
2410 "L.index(value, [start, [stop]]) -> integer -- return first index of value");
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2411 PyDoc_STRVAR(count_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2412 "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
2413 PyDoc_STRVAR(reverse_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2414 "L.reverse() -- reverse *IN PLACE*");
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23779
diff changeset
2415 PyDoc_STRVAR(sort_doc,
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2416 "L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\n\
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2417 cmp(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
2418
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31164
diff changeset
2419 static PyObject *list_subscript(PyListObject*, PyObject*);
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31164
diff changeset
2420
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2421 static PyMethodDef list_methods[] = {
31172
e4fb6164158c * Added a new method flag, METH_COEXIST.
Raymond Hettinger <python@rcn.com>
parents: 31164
diff changeset
2422 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2423 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_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
2424 {"append", (PyCFunction)listappend, METH_O, append_doc},
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2425 {"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
2426 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2427 {"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
2428 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
29510
01e925371d7c SF #754014: list.index() should accept optional start, end arguments
Raymond Hettinger <python@rcn.com>
parents: 29198
diff changeset
2429 {"index", (PyCFunction)listindex, METH_VARARGS, index_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
2430 {"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
2431 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
30697
1345c23a0591 * list.sort() now supports three keyword arguments: cmp, key, and reverse.
Raymond Hettinger <python@rcn.com>
parents: 30156
diff changeset
2432 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
24456
d216e67ac221 Replaced samplesort with a stable, adaptive mergesort.
Tim Peters <tim.peters@gmail.com>
parents: 24424
diff changeset
2433 {NULL, NULL} /* sentinel */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2434 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2435
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2436 static PySequenceMethods list_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2437 (lenfunc)list_length, /* sq_length */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2438 (binaryfunc)list_concat, /* sq_concat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2439 (ssizeargfunc)list_repeat, /* sq_repeat */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2440 (ssizeargfunc)list_item, /* sq_item */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2441 (ssizessizeargfunc)list_slice, /* sq_slice */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2442 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2443 (ssizessizeobjargproc)list_ass_slice, /* sq_ass_slice */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2444 (objobjproc)list_contains, /* sq_contains */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2445 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2446 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2447 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2448
23855
2b071337205c Missed one use of new PyDoc_STRVAR macro
Neal Norwitz <nnorwitz@gmail.com>
parents: 23834
diff changeset
2449 PyDoc_STRVAR(list_doc,
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2450 "list() -> new list\n"
23855
2b071337205c Missed one use of new PyDoc_STRVAR macro
Neal Norwitz <nnorwitz@gmail.com>
parents: 23834
diff changeset
2451 "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
2452
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2453 #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX)
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2454
24230
2860be244263 Don't declare a function with staticforward.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 24207
diff changeset
2455 static PyObject *
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2456 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
2457 {
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2458 PyNumberMethods *nb = item->ob_type->tp_as_number;
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2459 if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) {
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2460 Py_ssize_t i = nb->nb_index(item);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2461 if (i == -1 && PyErr_Occurred())
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2462 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2463 if (i < 0)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2464 i += PyList_GET_SIZE(self);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2465 return list_item(self, i);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2466 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2467 else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2468 Py_ssize_t start, stop, step, slicelength, cur, i;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2469 PyObject* result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2470 PyObject* it;
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2471 PyObject **src, **dest;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2472
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2473 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
2474 &start, &stop, &step, &slicelength) < 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2475 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2476 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2477
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2478 if (slicelength <= 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2479 return PyList_New(0);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2480 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2481 else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2482 result = PyList_New(slicelength);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2483 if (!result) return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2484
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2485 src = self->ob_item;
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2486 dest = ((PyListObject *)result)->ob_item;
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2487 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
2488 cur += step, i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2489 it = src[cur];
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2490 Py_INCREF(it);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2491 dest[i] = it;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2492 }
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2493
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2494 return result;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2495 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2496 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2497 else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2498 PyErr_SetString(PyExc_TypeError,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2499 "list indices must be integers");
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2500 return NULL;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2501 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2502 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2503
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2504 static int
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2505 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
2506 {
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2507 PyNumberMethods *nb = item->ob_type->tp_as_number;
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2508 if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) {
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36675
diff changeset
2509 Py_ssize_t i = nb->nb_index(item);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2510 if (i == -1 && PyErr_Occurred())
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2511 return -1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2512 if (i < 0)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2513 i += PyList_GET_SIZE(self);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2514 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
2515 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2516 else if (PySlice_Check(item)) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2517 Py_ssize_t start, stop, step, slicelength;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2518
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2519 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
2520 &start, &stop, &step, &slicelength) < 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2521 return -1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2522 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2523
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23855
diff changeset
2524 /* 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
2525 if (step == 1 && ((PySliceObject*)item)->step == Py_None)
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23855
diff changeset
2526 return list_ass_slice(self, start, stop, value);
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23855
diff changeset
2527
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2528 if (value == NULL) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2529 /* delete slice */
31519
bd0acc42829e * Optimized list appends and pops by making fewer calls the underlying system
Raymond Hettinger <python@rcn.com>
parents: 31390
diff changeset
2530 PyObject **garbage;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2531 Py_ssize_t cur, i;
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2532
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2533 if (slicelength <= 0)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2534 return 0;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2535
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2536 if (step < 0) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2537 stop = start + 1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2538 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
2539 step = -step;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2540 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2541
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2542 garbage = (PyObject**)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2543 PyMem_MALLOC(slicelength*sizeof(PyObject*));
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2544
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2545 /* drawing pictures might help
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2546 understand these for loops */
23779
55c922669913 Fold remaining long lines.
Guido van Rossum <guido@python.org>
parents: 23778
diff changeset
2547 for (cur = start, i = 0;
55c922669913 Fold remaining long lines.
Guido van Rossum <guido@python.org>
parents: 23778
diff changeset
2548 cur < stop;
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24414
diff changeset
2549 cur += step, i++) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2550 Py_ssize_t lim = step;
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24414
diff changeset
2551
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2552 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
2553
24424
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24414
diff changeset
2554 if (cur + step >= self->ob_size) {
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24414
diff changeset
2555 lim = self->ob_size - cur - 1;
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24414
diff changeset
2556 }
f4cd78b3b774 Fix for
Michael W. Hudson <mwh@python.net>
parents: 24414
diff changeset
2557
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
2558 memmove(self->ob_item + cur - i,
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2559 self->ob_item + cur + 1,
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2560 lim * sizeof(PyObject *));
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2561 }
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2562
23929
26ba17b758b9 Fix the bug described in
Michael W. Hudson <mwh@python.net>
parents: 23855
diff changeset
2563 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
2564 cur < self->ob_size; cur++) {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2565 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
2566 PyList_GET_ITEM(self, cur));
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2567 }
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2568
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2569 self->ob_size -= slicelength;
31665
9a4c49c5ac03 Revert last change. Found an application that was worse off with resize
Raymond Hettinger <python@rcn.com>
parents: 31661
diff changeset
2570 list_resize(self, self->ob_size);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2571
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2572 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
2573 Py_DECREF(garbage[i]);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2574 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2575 PyMem_FREE(garbage);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2576
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2577 return 0;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2578 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2579 else {
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2580 /* assign slice */
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2581 PyObject **garbage, *ins, *seq, **seqitems, **selfitems;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2582 Py_ssize_t cur, i;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2583
26543
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2584 /* protect against a[::-1] = a */
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2585 if (self == (PyListObject*)value) {
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2586 seq = list_slice((PyListObject*)value, 0,
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2587 PyList_GET_SIZE(value));
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2588 }
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2589 else {
32850
09e633c52730 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32848
diff changeset
2590 seq = PySequence_Fast(value,
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2591 "must assign iterable to extended slice");
26543
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2592 if (!seq)
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2593 return -1;
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2594 }
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2595
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2596 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2597 PyErr_Format(PyExc_ValueError,
36510
0f3894bb2ada Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents: 36509
diff changeset
2598 "attempt to assign sequence of size %zd to extended slice of size %zd",
36511
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
2599 PySequence_Fast_GET_SIZE(seq),
1596ff5c79b5 Revert 42400.
Martin v. Löwis <martin@v.loewis.de>
parents: 36510
diff changeset
2600 slicelength);
26543
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2601 Py_DECREF(seq);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2602 return -1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2603 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2604
26543
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2605 if (!slicelength) {
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2606 Py_DECREF(seq);
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2607 return 0;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2608 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2609
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2610 garbage = (PyObject**)
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2611 PyMem_MALLOC(slicelength*sizeof(PyObject*));
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2612
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2613 selfitems = self->ob_item;
31650
be782d07c518 Make PySequence_Fast_ITEMS public. (Thanks Skip.)
Raymond Hettinger <python@rcn.com>
parents: 31648
diff changeset
2614 seqitems = PySequence_Fast_ITEMS(seq);
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2615 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
2616 cur += step, i++) {
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2617 garbage[i] = selfitems[cur];
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2618 ins = seqitems[i];
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2619 Py_INCREF(ins);
31633
5bd455638c7a Optimize inner loops for subscript, repeat, and concat.
Raymond Hettinger <python@rcn.com>
parents: 31632
diff changeset
2620 selfitems[cur] = ins;
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2621 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2622
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2623 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
2624 Py_DECREF(garbage[i]);
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2625 }
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2626
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2627 PyMem_FREE(garbage);
26543
975482d5eb5c The final tweaks before closing
Michael W. Hudson <mwh@python.net>
parents: 26161
diff changeset
2628 Py_DECREF(seq);
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2629
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2630 return 0;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2631 }
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2632 }
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2633 else {
24310
5ede9bcc651e Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 24309
diff changeset
2634 PyErr_SetString(PyExc_TypeError,
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2635 "list indices must be integers");
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2636 return -1;
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2637 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2638 }
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2639
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2640 static PyMappingMethods list_as_mapping = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2641 (lenfunc)list_length,
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2642 (binaryfunc)list_subscript,
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2643 (objobjargproc)list_ass_subscript
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2644 };
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23615
diff changeset
2645
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2646 PyTypeObject PyList_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
2647 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2648 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2649 "list",
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
2650 sizeof(PyListObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2651 0,
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2652 (destructor)list_dealloc, /* tp_dealloc */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2653 (printfunc)list_print, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2654 0, /* tp_getattr */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2655 0, /* tp_setattr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2656 0, /* tp_compare */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2657 (reprfunc)list_repr, /* tp_repr */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2658 0, /* tp_as_number */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2659 &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
2660 &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
2661 list_nohash, /* tp_hash */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2662 0, /* tp_call */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2663 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2664 PyObject_GenericGetAttr, /* tp_getattro */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2665 0, /* tp_setattro */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2666 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19301
diff changeset
2667 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
2668 Py_TPFLAGS_BASETYPE, /* tp_flags */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2669 list_doc, /* tp_doc */
15969
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2670 (traverseproc)list_traverse, /* tp_traverse */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2671 (inquiry)list_clear, /* tp_clear */
e21da84d0c65 Convert to rich comparisons:
Guido van Rossum <guido@python.org>
parents: 15707
diff changeset
2672 list_richcompare, /* tp_richcompare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2673 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
2674 list_iter, /* tp_iter */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2675 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2676 list_methods, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2677 0, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2678 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2679 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2680 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2681 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2682 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2683 0, /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2684 (initproc)list_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2685 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18409
diff changeset
2686 PyType_GenericNew, /* tp_new */
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2687 PyObject_GC_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2688 };
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
2689
69662f16a838 Tim's latest, with some of my changes (also a TP suggestion) added:
Guido van Rossum <guido@python.org>
parents: 8457
diff changeset
2690
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2691 /*********************** List Iterator **************************/
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2692
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2693 typedef struct {
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2694 PyObject_HEAD
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2695 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
2696 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
2697 } listiterobject;
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2698
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2699 static PyObject *list_iter(PyObject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2700 static void listiter_dealloc(listiterobject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2701 static int listiter_traverse(listiterobject *, visitproc, void *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2702 static PyObject *listiter_next(listiterobject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2703 static PyObject *listiter_len(listiterobject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2704
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2705 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2706
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2707 static PyMethodDef listiter_methods[] = {
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2708 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2709 {NULL, NULL} /* sentinel */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2710 };
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2711
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2712 PyTypeObject PyListIter_Type = {
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2713 PyObject_HEAD_INIT(&PyType_Type)
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2714 0, /* ob_size */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2715 "listiterator", /* tp_name */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2716 sizeof(listiterobject), /* tp_basicsize */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2717 0, /* tp_itemsize */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2718 /* methods */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2719 (destructor)listiter_dealloc, /* tp_dealloc */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2720 0, /* tp_print */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2721 0, /* tp_getattr */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2722 0, /* tp_setattr */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2723 0, /* tp_compare */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2724 0, /* tp_repr */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2725 0, /* tp_as_number */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2726 0, /* tp_as_sequence */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2727 0, /* tp_as_mapping */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2728 0, /* tp_hash */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2729 0, /* tp_call */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2730 0, /* tp_str */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2731 PyObject_GenericGetAttr, /* tp_getattro */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2732 0, /* tp_setattro */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2733 0, /* tp_as_buffer */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2734 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2735 0, /* tp_doc */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2736 (traverseproc)listiter_traverse, /* tp_traverse */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2737 0, /* tp_clear */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2738 0, /* tp_richcompare */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2739 0, /* tp_weaklistoffset */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2740 PyObject_SelfIter, /* tp_iter */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2741 (iternextfunc)listiter_next, /* tp_iternext */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2742 listiter_methods, /* tp_methods */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2743 0, /* tp_members */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2744 };
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2745
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2746
24247
5d3ec28e9a7a Make list_iter() really static.
Guido van Rossum <guido@python.org>
parents: 24236
diff changeset
2747 static PyObject *
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2748 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
2749 {
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2750 listiterobject *it;
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2751
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2752 if (!PyList_Check(seq)) {
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2753 PyErr_BadInternalCall();
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2754 return NULL;
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2755 }
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2756 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2757 if (it == NULL)
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2758 return NULL;
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2759 it->it_index = 0;
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2760 Py_INCREF(seq);
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2761 it->it_seq = (PyListObject *)seq;
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2762 _PyObject_GC_TRACK(it);
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2763 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
2764 }
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2765
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2766 static void
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2767 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
2768 {
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2769 _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
2770 Py_XDECREF(it->it_seq);
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2771 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
2772 }
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2773
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2774 static int
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2775 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
2776 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37673
diff changeset
2777 Py_VISIT(it->it_seq);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37673
diff changeset
2778 return 0;
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2779 }
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2780
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2781 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
2782 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
2783 {
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2784 PyListObject *seq;
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2785 PyObject *item;
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2786
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
2787 assert(it != NULL);
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2788 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
2789 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
2790 return NULL;
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2791 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
2792
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2793 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
2794 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
2795 ++it->it_index;
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2796 Py_INCREF(item);
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2797 return item;
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2798 }
24255
41c54b37e030 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 24247
diff changeset
2799
41c54b37e030 Make StopIteration a sink state. This is done by clearing out the
Guido van Rossum <guido@python.org>
parents: 24247
diff changeset
2800 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
2801 it->it_seq = NULL;
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2802 return NULL;
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2803 }
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2804
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35365
diff changeset
2805 static PyObject *
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31665
diff changeset
2806 listiter_len(listiterobject *it)
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31665
diff changeset
2807 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2808 Py_ssize_t len;
31905
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
2809 if (it->it_seq) {
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
2810 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
2811 if (len >= 0)
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2812 return PyInt_FromSsize_t(len);
31905
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
2813 }
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35365
diff changeset
2814 return PyInt_FromLong(0);
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31665
diff changeset
2815 }
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2816 /*********************** List Reverse Iterator **************************/
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31665
diff changeset
2817
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2818 typedef struct {
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2819 PyObject_HEAD
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2820 Py_ssize_t it_index;
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2821 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2822 } listreviterobject;
35636
e22334b1da67 Convert iterator __len__() methods to a private API.
Raymond Hettinger <python@rcn.com>
parents: 35365
diff changeset
2823
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2824 static PyObject *list_reversed(PyListObject *, PyObject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2825 static void listreviter_dealloc(listreviterobject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2826 static int listreviter_traverse(listreviterobject *, visitproc, void *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2827 static PyObject *listreviter_next(listreviterobject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2828 static Py_ssize_t listreviter_len(listreviterobject *);
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2829
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2830 static PySequenceMethods listreviter_as_sequence = {
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2831 (lenfunc)listreviter_len, /* sq_length */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2832 0, /* sq_concat */
31686
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31665
diff changeset
2833 };
823279ae43eb Make iterators length transparent where possible.
Raymond Hettinger <python@rcn.com>
parents: 31665
diff changeset
2834
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2835 PyTypeObject PyListRevIter_Type = {
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2836 PyObject_HEAD_INIT(&PyType_Type)
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2837 0, /* ob_size */
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2838 "listreverseiterator", /* tp_name */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2839 sizeof(listreviterobject), /* tp_basicsize */
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2840 0, /* tp_itemsize */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2841 /* methods */
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2842 (destructor)listreviter_dealloc, /* tp_dealloc */
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2843 0, /* tp_print */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2844 0, /* tp_getattr */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2845 0, /* tp_setattr */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2846 0, /* tp_compare */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2847 0, /* tp_repr */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2848 0, /* tp_as_number */
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2849 &listreviter_as_sequence, /* tp_as_sequence */
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2850 0, /* tp_as_mapping */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2851 0, /* tp_hash */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2852 0, /* tp_call */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2853 0, /* tp_str */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2854 PyObject_GenericGetAttr, /* tp_getattro */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2855 0, /* tp_setattro */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2856 0, /* tp_as_buffer */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2857 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2858 0, /* tp_doc */
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2859 (traverseproc)listreviter_traverse, /* tp_traverse */
24257
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2860 0, /* tp_clear */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2861 0, /* tp_richcompare */
a6717275195c Whitespace normalization.
Guido van Rossum <guido@python.org>
parents: 24255
diff changeset
2862 0, /* tp_weaklistoffset */
28320
fd5ef7003469 Renamed PyObject_GenericGetIter to PyObject_SelfIter
Raymond Hettinger <python@rcn.com>
parents: 28308
diff changeset
2863 PyObject_SelfIter, /* tp_iter */
37589
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2864 (iternextfunc)listreviter_next, /* tp_iternext */
aeee85b6a5dd More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36882
diff changeset
2865 0,
23606
5edea752a421 SF 560736. Optimize list iteration by filling the tp_iter slot.
Raymond Hettinger <python@rcn.com>
parents: 23517
diff changeset
2866 };
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2867
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2868 static PyObject *
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2869 list_reversed(PyListObject *seq, PyObject *unused)
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2870 {
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2871 listreviterobject *it;
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2872
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2873 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2874 if (it == NULL)
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2875 return NULL;
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2876 assert(PyList_Check(seq));
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2877 it->it_index = PyList_GET_SIZE(seq) - 1;
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2878 Py_INCREF(seq);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2879 it->it_seq = seq;
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2880 PyObject_GC_Track(it);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2881 return (PyObject *)it;
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2882 }
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2883
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2884 static void
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2885 listreviter_dealloc(listreviterobject *it)
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2886 {
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2887 PyObject_GC_UnTrack(it);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2888 Py_XDECREF(it->it_seq);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2889 PyObject_GC_Del(it);
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2890 }
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2891
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2892 static int
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2893 listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2894 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37673
diff changeset
2895 Py_VISIT(it->it_seq);
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 37673
diff changeset
2896 return 0;
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2897 }
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2898
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2899 static PyObject *
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2900 listreviter_next(listreviterobject *it)
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2901 {
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2902 PyObject *item;
36519
d6d415df0df0 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36511
diff changeset
2903 Py_ssize_t index = it->it_index;
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2904 PyListObject *seq = it->it_seq;
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2905
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2906 if (index>=0 && index < PyList_GET_SIZE(seq)) {
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2907 item = PyList_GET_ITEM(seq, index);
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2908 it->it_index--;
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2909 Py_INCREF(item);
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2910 return item;
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2911 }
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2912 it->it_index = -1;
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2913 if (seq != NULL) {
30879
4ed40b3f8802 Improve the reverse list iterator to free memory as soon as the iterator
Raymond Hettinger <python@rcn.com>
parents: 30876
diff changeset
2914 it->it_seq = NULL;
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2915 Py_DECREF(seq);
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2916 }
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2917 return NULL;
30874
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2918 }
682d1b67c7b5 Optimize reversed(list) using a custom iterator.
Raymond Hettinger <python@rcn.com>
parents: 30849
diff changeset
2919
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2920 static Py_ssize_t
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2921 listreviter_len(listreviterobject *it)
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2922 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2923 Py_ssize_t len = it->it_index + 1;
31905
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
2924 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
2925 return 0;
e76092d4902c * Specialize ins1() into app1() for appends. Saves several unnecessary
Raymond Hettinger <python@rcn.com>
parents: 31718
diff changeset
2926 return len;
31635
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2927 }
04eb2ebb46e2 Tidied up the implementations of reversed (including the custom ones
Raymond Hettinger <python@rcn.com>
parents: 31633
diff changeset
2928