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