annotate cos/python/Objects/listobject.c @ 34:8012221dd740

Fixes for uninitialized data. This causes problems on real machines
author windel
date Mon, 16 Jan 2012 13:46:06 +0100
parents 7f74363f4c82
children
rev   line source
27
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1 /* List object implementation */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3 #include "Python.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
5 /* Ensure ob_item has room for at least newsize elements, and set
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
6 * ob_size to newsize. If newsize > ob_size on entry, the content
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
7 * of the new slots at exit is undefined heap trash; it's the caller's
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
8 * responsibility to overwrite them with sane values.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
9 * The number of allocated elements may grow, shrink, or stay the same.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
10 * Failure is impossible if newsize <= self.allocated on entry, although
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
11 * that partly relies on an assumption that the system realloc() never
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
12 * fails when passed a number of bytes <= the number of bytes last
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
13 * allocated (the C standard doesn't guarantee this, but it's hard to
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
14 * imagine a realloc implementation where it wouldn't be true).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
15 * Note that self->ob_item may change, and even if newsize is less
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
16 * than ob_size on entry.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
17 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
18 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
19 list_resize(PyListObject *self, Py_ssize_t newsize)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
20 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
21 PyObject **items;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
22 size_t new_allocated;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
23 Py_ssize_t allocated = self->allocated;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
24
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
25 /* Bypass realloc() when a previous overallocation is large enough
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
26 to accommodate the newsize. If the newsize falls lower than half
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
27 the allocated size, then proceed with the realloc() to shrink the list.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
28 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
29 if (allocated >= newsize && newsize >= (allocated >> 1)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
30 assert(self->ob_item != NULL || newsize == 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
31 Py_SIZE(self) = newsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
32 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
33 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
34
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
35 /* This over-allocates proportional to the list size, making room
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
36 * for additional growth. The over-allocation is mild, but is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
37 * enough to give linear-time amortized behavior over a long
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
38 * sequence of appends() in the presence of a poorly-performing
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
39 * system realloc().
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
40 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
41 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
42 new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
43
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
44 /* check for integer overflow */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
45 if (new_allocated > PY_SIZE_MAX - newsize) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
46 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
47 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
48 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
49 new_allocated += newsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
50 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
51
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
52 if (newsize == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
53 new_allocated = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
54 items = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
55 if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
56 PyMem_RESIZE(items, PyObject *, new_allocated);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
57 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
58 items = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
59 if (items == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
60 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
61 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
62 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
63 self->ob_item = items;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
64 Py_SIZE(self) = newsize;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
65 self->allocated = new_allocated;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
66 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
67 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
68
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
69
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
70 /* Empty list reuse scheme to save calls to malloc and free */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
71 #ifndef PyList_MAXFREELIST
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
72 #define PyList_MAXFREELIST 80
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
73 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
74 static PyListObject *free_list[PyList_MAXFREELIST];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
75 static int numfree = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
76
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
77 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
78 PyList_ClearFreeList(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
79 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
80 PyListObject *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
81 int ret = numfree;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
82 while (numfree) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
83 op = free_list[--numfree];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
84 assert(PyList_CheckExact(op));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
85 PyObject_GC_Del(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
86 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
87 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
88 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
89
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
90 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
91 PyList_Fini(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
92 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
93 PyList_ClearFreeList();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
94 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
95
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
96 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
97 PyList_New(Py_ssize_t size)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
98 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
99 PyListObject *op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
100 size_t nbytes;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
101
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
102 if (size < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
103 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
104 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
105 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
106 /* Check for overflow without an actual overflow,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
107 * which can cause compiler to optimise out */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
108 if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
109 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
110 nbytes = size * sizeof(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
111 if (numfree) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
112 numfree--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
113 op = free_list[numfree];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
114 _Py_NewReference((PyObject *)op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
115 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
116 op = PyObject_GC_New(PyListObject, &PyList_Type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
117 if (op == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
118 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
119 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
120 if (size <= 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
121 op->ob_item = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
122 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
123 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
124 if (op->ob_item == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
125 Py_DECREF(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
126 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
127 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
128 memset(op->ob_item, 0, nbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
129 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
130 Py_SIZE(op) = size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
131 op->allocated = size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
132 _PyObject_GC_TRACK(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
133 return (PyObject *) op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
134 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
135
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
136 Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
137 PyList_Size(PyObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
138 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
139 if (!PyList_Check(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
140 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
141 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
142 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
143 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
144 return Py_SIZE(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
145 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
146
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
147 static PyObject *indexerr = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
148
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
149 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
150 PyList_GetItem(PyObject *op, Py_ssize_t i)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
151 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
152 if (!PyList_Check(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
153 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
154 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
155 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
156 if (i < 0 || i >= Py_SIZE(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
157 if (indexerr == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
158 indexerr = PyUnicode_FromString(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
159 "list index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
160 if (indexerr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
161 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
162 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
163 PyErr_SetObject(PyExc_IndexError, indexerr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
164 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
165 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
166 return ((PyListObject *)op) -> ob_item[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
167 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
168
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
169 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
170 PyList_SetItem(register PyObject *op, register int i,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
171 register PyObject *newitem)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
172 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
173 register PyObject *olditem;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
174 register PyObject **p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
175 if (!PyList_Check(op))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
176 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
177 Py_XDECREF(newitem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
178 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
179 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
180 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
181 if (i < 0 || i >= Py_SIZE(op))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
182 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
183 Py_XDECREF(newitem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
184 PyErr_SetString(PyExc_IndexError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
185 "list assignment index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
186 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
187 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
188 p = ((PyListObject *)op) -> ob_item + i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
189 olditem = *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
190 *p = newitem;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
191 Py_XDECREF(olditem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
192 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
193 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
194
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
195 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
196 ins1(PyListObject *self, int where, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
197 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
198 int i, n = Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
199 PyObject **items;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
200 if (v == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
201 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
202 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
203 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
204 if (n == PY_SSIZE_T_MAX) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
205 PyErr_SetString(PyExc_OverflowError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
206 "cannot add more objects to list");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
207 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
208 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
209
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
210 if (list_resize(self, n+1) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
211 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
212
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
213 if (where < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
214 where += n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
215 if (where < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
216 where = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
217 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
218 if (where > n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
219 where = n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
220 items = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
221 for (i = n; --i >= where; )
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
222 items[i+1] = items[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
223 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
224 items[where] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
225 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
226 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
227
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
228 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
229 PyList_Insert(PyObject *op, int where, PyObject *newitem)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
230 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
231 if (!PyList_Check(op)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
232 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
233 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
234 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
235 return ins1((PyListObject *)op, where, newitem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
236 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
237
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
238 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
239 app1(PyListObject *self, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
240 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
241 Py_ssize_t n = PyList_GET_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
242
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
243 assert (v != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
244 if (n == PY_SSIZE_T_MAX) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
245 PyErr_SetString(PyExc_OverflowError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
246 "cannot add more objects to list");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
247 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
248 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
249
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
250 if (list_resize(self, n+1) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
251 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
252
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
253 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
254 PyList_SET_ITEM(self, n, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
255 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
256 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
257
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
258 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
259 PyList_Append(PyObject *op, PyObject *newitem)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
260 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
261 if (PyList_Check(op) && (newitem != NULL))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
262 return app1((PyListObject *)op, newitem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
263 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
264 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
265 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
266
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
267 /* Methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
268
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
269 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
270 list_dealloc(PyListObject *op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
271 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
272 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
273 PyObject_GC_UnTrack(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
274 Py_TRASHCAN_SAFE_BEGIN(op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
275 if (op->ob_item != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
276 /* Do it backwards, for Christian Tismer.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
277 There's a simple test case where somehow this reduces
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
278 thrashing when a *very* large list is created and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
279 immediately deleted. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
280 i = Py_SIZE(op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
281 while (--i >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
282 Py_XDECREF(op->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
283 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
284 PyMem_FREE(op->ob_item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
285 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
286 if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
287 free_list[numfree++] = op;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
288 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
289 Py_TYPE(op)->tp_free((PyObject *)op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
290 Py_TRASHCAN_SAFE_END(op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
291 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
292
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
293 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
294 list_repr(PyListObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
295 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
296 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
297 PyObject *s = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
298 _PyAccu acc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
299 static PyObject *sep = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
300
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
301 if (Py_SIZE(v) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
302 return PyUnicode_FromString("[]");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
303 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
304
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
305 if (sep == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
306 sep = PyUnicode_FromString(", ");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
307 if (sep == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
308 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
309 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
310
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
311 i = Py_ReprEnter((PyObject*)v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
312 if (i != 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
313 return i > 0 ? PyUnicode_FromString("[...]") : NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
314 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
315
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
316 if (_PyAccu_Init(&acc))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
317 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
318
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
319 s = PyUnicode_FromString("[");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
320 if (s == NULL || _PyAccu_Accumulate(&acc, s))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
321 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
322 Py_CLEAR(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
323
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
324 /* Do repr() on each element. Note that this may mutate the list,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
325 so must refetch the list size on each iteration. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
326 for (i = 0; i < Py_SIZE(v); ++i) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
327 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
328 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
329 s = PyObject_Repr(v->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
330 Py_LeaveRecursiveCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
331 if (i > 0 && _PyAccu_Accumulate(&acc, sep))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
332 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
333 if (s == NULL || _PyAccu_Accumulate(&acc, s))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
334 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
335 Py_CLEAR(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
336 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
337 s = PyUnicode_FromString("]");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
338 if (s == NULL || _PyAccu_Accumulate(&acc, s))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
339 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
340 Py_CLEAR(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
341
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
342 Py_ReprLeave((PyObject *)v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
343 return _PyAccu_Finish(&acc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
344
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
345 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
346 _PyAccu_Destroy(&acc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
347 Py_XDECREF(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
348 Py_ReprLeave((PyObject *)v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
349 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
350 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
351
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
352 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
353 list_length(PyListObject *a)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
354 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
355 return Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
356 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
357
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
358 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
359 list_contains(PyListObject *a, PyObject *el)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
360 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
361 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
362 int cmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
363
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
364 for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
365 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
366 Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
367 return cmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
368 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
369
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
370 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
371 list_item(PyListObject *a, int i)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
372 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
373 if (i < 0 || i >= Py_SIZE(a)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
374 if (indexerr == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
375 indexerr = PyUnicode_FromString(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
376 "list index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
377 if (indexerr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
378 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
379 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
380 PyErr_SetObject(PyExc_IndexError, indexerr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
381 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
382 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
383 Py_INCREF(a->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
384 return a->ob_item[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
385 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
386
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
387 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
388 list_slice(PyListObject *a, int ilow, int ihigh)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
389 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
390 PyListObject *np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
391 PyObject **src, **dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
392 int i, len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
393 if (ilow < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
394 ilow = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
395 else if (ilow > Py_SIZE(a))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
396 ilow = Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
397 if (ihigh < ilow)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
398 ihigh = ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
399 else if (ihigh > Py_SIZE(a))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
400 ihigh = Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
401 len = ihigh - ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
402 np = (PyListObject *) PyList_New(len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
403 if (np == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
404 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
405
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
406 src = a->ob_item + ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
407 dest = np->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
408 for (i = 0; i < len; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
409 PyObject *v = src[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
410 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
411 dest[i] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
412 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
413 return (PyObject *)np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
414 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
415
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
416 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
417 PyList_GetSlice(PyObject *a, int ilow, int ihigh)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
418 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
419 if (!PyList_Check(a)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
420 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
421 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
422 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
423 return list_slice((PyListObject *)a, ilow, ihigh);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
424 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
425
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
426 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
427 list_concat(PyListObject *a, PyObject *bb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
428 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
429 Py_ssize_t size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
430 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
431 PyObject **src, **dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
432 PyListObject *np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
433 if (!PyList_Check(bb)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
434 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
435 "can only concatenate list (not \"%.200s\") to list",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
436 bb->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
437 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
438 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
439 #define b ((PyListObject *)bb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
440 size = Py_SIZE(a) + Py_SIZE(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
441 if (size < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
442 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
443 np = (PyListObject *) PyList_New(size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
444 if (np == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
445 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
446 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
447 src = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
448 dest = np->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
449 for (i = 0; i < Py_SIZE(a); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
450 PyObject *v = src[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
451 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
452 dest[i] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
453 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
454 src = b->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
455 dest = np->ob_item + Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
456 for (i = 0; i < Py_SIZE(b); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
457 PyObject *v = src[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
458 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
459 dest[i] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
460 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
461 return (PyObject *)np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
462 #undef b
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
463 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
464
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
465 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
466 list_repeat(PyListObject *a, int n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
467 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
468 Py_ssize_t i, j;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
469 Py_ssize_t size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
470 PyListObject *np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
471 PyObject **p, **items;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
472 PyObject *elem;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
473 if (n < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
474 n = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
475 if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
476 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
477 size = Py_SIZE(a) * n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
478 if (size == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
479 return PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
480 np = (PyListObject *) PyList_New(size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
481 if (np == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
482 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
483
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
484 items = np->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
485 if (Py_SIZE(a) == 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
486 elem = a->ob_item[0];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
487 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
488 items[i] = elem;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
489 Py_INCREF(elem);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
490 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
491 return (PyObject *) np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
492 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
493 p = np->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
494 items = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
495 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
496 for (j = 0; j < Py_SIZE(a); j++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
497 *p = items[j];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
498 Py_INCREF(*p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
499 p++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
500 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
501 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
502 return (PyObject *) np;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
503 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
504
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
505 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
506 list_clear(PyListObject *a)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
507 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
508 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
509 PyObject **item = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
510 if (item != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
511 /* Because XDECREF can recursively invoke operations on
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
512 this list, we make it empty first. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
513 i = Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
514 Py_SIZE(a) = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
515 a->ob_item = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
516 a->allocated = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
517 while (--i >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
518 Py_XDECREF(item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
519 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
520 PyMem_FREE(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
521 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
522 /* Never fails; the return value can be ignored.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
523 Note that there is no guarantee that the list is actually empty
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
524 at this point, because XDECREF may have populated it again! */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
525 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
526 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
527
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
528 /* a[ilow:ihigh] = v if v != NULL.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
529 * del a[ilow:ihigh] if v == NULL.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
530 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
531 * Special speed gimmick: when v is NULL and ihigh - ilow <= 8, it's
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
532 * guaranteed the call cannot fail.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
533 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
534 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
535 list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
536 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
537 /* Because [X]DECREF can recursively invoke list operations on
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
538 this list, we must postpone all [X]DECREF activity until
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
539 after the list is back in its canonical shape. Therefore
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
540 we must allocate an additional array, 'recycle', into which
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
541 we temporarily copy the items that are deleted from the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
542 list. :-( */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
543 PyObject *recycle_on_stack[8];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
544 PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
545 PyObject **item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
546 PyObject **vitem = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
547 PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
548 Py_ssize_t n; /* # of elements in replacement list */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
549 Py_ssize_t norig; /* # of elements in list getting replaced */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
550 Py_ssize_t d; /* Change in size */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
551 Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
552 size_t s;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
553 int result = -1; /* guilty until proved innocent */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
554 #define b ((PyListObject *)v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
555 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
556 n = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
557 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
558 if (a == b) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
559 /* Special case "a[i:j] = a" -- copy b first */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
560 v = list_slice(b, 0, Py_SIZE(b));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
561 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
562 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
563 result = list_ass_slice(a, ilow, ihigh, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
564 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
565 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
566 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
567 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
568 if(v_as_SF == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
569 goto Error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
570 n = PySequence_Fast_GET_SIZE(v_as_SF);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
571 vitem = PySequence_Fast_ITEMS(v_as_SF);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
572 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
573 if (ilow < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
574 ilow = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
575 else if (ilow > Py_SIZE(a))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
576 ilow = Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
577
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
578 if (ihigh < ilow)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
579 ihigh = ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
580 else if (ihigh > Py_SIZE(a))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
581 ihigh = Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
582
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
583 norig = ihigh - ilow;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
584 assert(norig >= 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
585 d = n - norig;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
586 if (Py_SIZE(a) + d == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
587 Py_XDECREF(v_as_SF);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
588 return list_clear(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
589 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
590 item = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
591 /* recycle the items that we are about to remove */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
592 s = norig * sizeof(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
593 if (s > sizeof(recycle_on_stack)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
594 recycle = (PyObject **)PyMem_MALLOC(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
595 if (recycle == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
596 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
597 goto Error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
598 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
599 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
600 memcpy(recycle, &item[ilow], s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
601
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
602 if (d < 0) { /* Delete -d items */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
603 memmove(&item[ihigh+d], &item[ihigh],
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
604 (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
605 list_resize(a, Py_SIZE(a) + d);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
606 item = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
607 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
608 else if (d > 0) { /* Insert d items */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
609 k = Py_SIZE(a);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
610 if (list_resize(a, k+d) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
611 goto Error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
612 item = a->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
613 memmove(&item[ihigh+d], &item[ihigh],
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
614 (k - ihigh)*sizeof(PyObject *));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
615 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
616 for (k = 0; k < n; k++, ilow++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
617 PyObject *w = vitem[k];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
618 Py_XINCREF(w);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
619 item[ilow] = w;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
620 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
621 for (k = norig - 1; k >= 0; --k)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
622 Py_XDECREF(recycle[k]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
623 result = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
624 Error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
625 if (recycle != recycle_on_stack)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
626 PyMem_FREE(recycle);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
627 Py_XDECREF(v_as_SF);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
628 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
629 #undef b
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
630 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
631
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
632 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
633 PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
634 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
635 if (!PyList_Check(a)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
636 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
637 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
638 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
639 return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
640 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
641
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
642 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
643 list_inplace_repeat(PyListObject *self, Py_ssize_t n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
644 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
645 PyObject **items;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
646 Py_ssize_t size, i, j, p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
647
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
648
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
649 size = PyList_GET_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
650 if (size == 0 || n == 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
651 Py_INCREF(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
652 return (PyObject *)self;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
653 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
654
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
655 if (n < 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
656 (void)list_clear(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
657 Py_INCREF(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
658 return (PyObject *)self;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
659 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
660
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
661 if (size > PY_SSIZE_T_MAX / n) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
662 return PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
663 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
664
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
665 if (list_resize(self, size*n) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
666 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
667
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
668 p = size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
669 items = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
670 for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
671 for (j = 0; j < size; j++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
672 PyObject *o = items[j];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
673 Py_INCREF(o);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
674 items[p++] = o;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
675 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
676 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
677 Py_INCREF(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
678 return (PyObject *)self;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
679 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
680
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
681 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
682 list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
683 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
684 PyObject *old_value;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
685 if (i < 0 || i >= Py_SIZE(a)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
686 PyErr_SetString(PyExc_IndexError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
687 "list assignment index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
688 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
689 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
690 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
691 return list_ass_slice(a, i, i+1, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
692 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
693 old_value = a->ob_item[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
694 a->ob_item[i] = v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
695 Py_DECREF(old_value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
696 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
697 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
698
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
699 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
700 listinsert(PyListObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
701 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
702 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
703 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
704 if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
705 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
706 if (ins1(self, i, v) == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
707 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
708 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
709 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
710
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
711 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
712 listclear(PyListObject *self)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
713 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
714 list_clear(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
715 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
716 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
717
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
718 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
719 listcopy(PyListObject *self)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
720 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
721 return list_slice(self, 0, Py_SIZE(self));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
722 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
723
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
724 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
725 listappend(PyListObject *self, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
726 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
727 if (app1(self, v) == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
728 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
729 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
730 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
731
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
732 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
733 listextend(PyListObject *self, PyObject *b)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
734 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
735 PyObject *it; /* iter(v) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
736 Py_ssize_t m; /* size of self */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
737 Py_ssize_t n; /* guess for size of b */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
738 Py_ssize_t mn; /* m + n */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
739 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
740 PyObject *(*iternext)(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
741
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
742 /* Special cases:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
743 1) lists and tuples which can use PySequence_Fast ops
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
744 2) extending self to self requires making a copy first
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
745 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
746 if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
747 PyObject **src, **dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
748 b = PySequence_Fast(b, "argument must be iterable");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
749 if (!b)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
750 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
751 n = PySequence_Fast_GET_SIZE(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
752 if (n == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
753 /* short circuit when b is empty */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
754 Py_DECREF(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
755 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
756 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
757 m = Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
758 if (list_resize(self, m + n) == -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
759 Py_DECREF(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
760 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
761 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
762 /* note that we may still have self == b here for the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
763 * situation a.extend(a), but the following code works
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
764 * in that case too. Just make sure to resize self
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
765 * before calling PySequence_Fast_ITEMS.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
766 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
767 /* populate the end of self with b's items */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
768 src = PySequence_Fast_ITEMS(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
769 dest = self->ob_item + m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
770 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
771 PyObject *o = src[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
772 Py_INCREF(o);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
773 dest[i] = o;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
774 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
775 Py_DECREF(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
776 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
777 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
778
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
779 it = PyObject_GetIter(b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
780 if (it == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
781 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
782 iternext = *it->ob_type->tp_iternext;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
783
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
784 /* Guess a result list size. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
785 n = _PyObject_LengthHint(b, 8);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
786 if (n == -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
787 Py_DECREF(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
788 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
789 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
790 m = Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
791 mn = m + n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
792 if (mn >= m) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
793 /* Make room. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
794 if (list_resize(self, mn) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
795 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
796 /* Make the list sane again. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
797 Py_SIZE(self) = m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
798 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
799 /* Else m + n overflowed; on the chance that n lied, and there really
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
800 * is enough room, ignore it. If n was telling the truth, we'll
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
801 * eventually run out of memory during the loop.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
802 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
803
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
804 /* Run iterator to exhaustion. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
805 for (;;) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
806 PyObject *item = iternext(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
807 if (item == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
808 if (PyErr_Occurred()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
809 if (PyErr_ExceptionMatches(PyExc_StopIteration))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
810 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
811 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
812 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
813 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
814 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
815 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
816 if (Py_SIZE(self) < self->allocated) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
817 /* steals ref */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
818 PyList_SET_ITEM(self, Py_SIZE(self), item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
819 ++Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
820 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
821 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
822 int status = app1(self, item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
823 Py_DECREF(item); /* append creates a new ref */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
824 if (status < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
825 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
826 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
827 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
828
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
829 /* Cut back result list if initial guess was too large. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
830 if (Py_SIZE(self) < self->allocated)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
831 list_resize(self, Py_SIZE(self)); /* shrinking can't fail */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
832
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
833 Py_DECREF(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
834 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
835
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
836 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
837 Py_DECREF(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
838 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
839 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
840
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
841 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
842 _PyList_Extend(PyListObject *self, PyObject *b)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
843 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
844 return listextend(self, b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
845 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
846
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
847 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
848 list_inplace_concat(PyListObject *self, PyObject *other)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
849 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
850 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
851
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
852 result = listextend(self, other);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
853 if (result == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
854 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
855 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
856 Py_INCREF(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
857 return (PyObject *)self;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
858 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
859
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
860 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
861 listpop(PyListObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
862 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
863 Py_ssize_t i = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
864 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
865 int status;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
866
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
867 if (!PyArg_ParseTuple(args, "|n:pop", &i))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
868 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
869
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
870 if (Py_SIZE(self) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
871 /* Special-case most common failure cause */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
872 PyErr_SetString(PyExc_IndexError, "pop from empty list");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
873 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
874 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
875 if (i < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
876 i += Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
877 if (i < 0 || i >= Py_SIZE(self)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
878 PyErr_SetString(PyExc_IndexError, "pop index out of range");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
879 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
880 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
881 v = self->ob_item[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
882 if (i == Py_SIZE(self) - 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
883 status = list_resize(self, Py_SIZE(self) - 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
884 assert(status >= 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
885 return v; /* and v now owns the reference the list had */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
886 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
887 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
888 status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
889 assert(status >= 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
890 /* Use status, so that in a release build compilers don't
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
891 * complain about the unused name.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
892 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
893 (void) status;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
894
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
895 return v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
896 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
897
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
898 /* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
899 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
900 reverse_slice(PyObject **lo, PyObject **hi)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
901 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
902 assert(lo && hi);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
903
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
904 --hi;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
905 while (lo < hi) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
906 PyObject *t = *lo;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
907 *lo = *hi;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
908 *hi = t;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
909 ++lo;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
910 --hi;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
911 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
912 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
913
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
914 /* Lots of code for an adaptive, stable, natural mergesort. There are many
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
915 * pieces to this algorithm; read listsort.txt for overviews and details.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
916 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
917
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
918 /* A sortslice contains a pointer to an array of keys and a pointer to
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
919 * an array of corresponding values. In other words, keys[i]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
920 * corresponds with values[i]. If values == NULL, then the keys are
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
921 * also the values.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
922 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
923 * Several convenience routines are provided here, so that keys and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
924 * values are always moved in sync.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
925 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
926
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
927 typedef struct {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
928 PyObject **keys;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
929 PyObject **values;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
930 } sortslice;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
931
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
932 Py_LOCAL_INLINE(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
933 sortslice_copy(sortslice *s1, int i, sortslice *s2, int j)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
934 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
935 s1->keys[i] = s2->keys[j];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
936 if (s1->values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
937 s1->values[i] = s2->values[j];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
938 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
939
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
940 Py_LOCAL_INLINE(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
941 sortslice_copy_incr(sortslice *dst, sortslice *src)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
942 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
943 *dst->keys++ = *src->keys++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
944 if (dst->values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
945 *dst->values++ = *src->values++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
946 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
947
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
948 Py_LOCAL_INLINE(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
949 sortslice_copy_decr(sortslice *dst, sortslice *src)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
950 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
951 *dst->keys-- = *src->keys--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
952 if (dst->values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
953 *dst->values-- = *src->values--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
954 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
955
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
956
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
957 Py_LOCAL_INLINE(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
958 sortslice_memcpy(sortslice *s1, int i, sortslice *s2, int j, int n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
959 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
960 memcpy(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
961 if (s1->values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
962 memcpy(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
963 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
964
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
965 Py_LOCAL_INLINE(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
966 sortslice_memmove(sortslice *s1, Py_ssize_t i, sortslice *s2, Py_ssize_t j,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
967 Py_ssize_t n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
968 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
969 memmove(&s1->keys[i], &s2->keys[j], sizeof(PyObject *) * n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
970 if (s1->values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
971 memmove(&s1->values[i], &s2->values[j], sizeof(PyObject *) * n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
972 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
973
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
974 Py_LOCAL_INLINE(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
975 sortslice_advance(sortslice *slice, Py_ssize_t n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
976 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
977 slice->keys += n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
978 if (slice->values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
979 slice->values += n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
980 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
981
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
982 /* Comparison function: PyObject_RichCompareBool with Py_LT.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
983 * Returns -1 on error, 1 if x < y, 0 if x >= y.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
984 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
985
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
986 #define ISLT(X, Y) (PyObject_RichCompareBool(X, Y, Py_LT))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
987
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
988 /* Compare X to Y via "<". Goto "fail" if the comparison raises an
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
989 error. Else "k" is set to true iff X<Y, and an "if (k)" block is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
990 started. It makes more sense in context <wink>. X and Y are PyObject*s.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
991 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
992 #define IFLT(X, Y) if ((k = ISLT(X, Y)) < 0) goto fail; \
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
993 if (k)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
994
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
995 /* binarysort is the best method for sorting small arrays: it does
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
996 few compares, but can do data movement quadratic in the number of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
997 elements.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
998 [lo, hi) is a contiguous slice of a list, and is sorted via
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
999 binary insertion. This sort is stable.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1000 On entry, must have lo <= start <= hi, and that [lo, start) is already
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1001 sorted (pass start == lo if you don't know!).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1002 If islt() complains return -1, else 0.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1003 Even in case of error, the output slice will be some permutation of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1004 the input (nothing is lost or duplicated).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1005 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1006 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1007 binarysort(sortslice lo, PyObject **hi, PyObject **start)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1008 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1009 register Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1010 register PyObject **l, **p, **r;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1011 register PyObject *pivot;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1012
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1013 assert(lo.keys <= start && start <= hi);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1014 /* assert [lo, start) is sorted */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1015 if (lo.keys == start)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1016 ++start;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1017 for (; start < hi; ++start) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1018 /* set l to where *start belongs */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1019 l = lo.keys;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1020 r = start;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1021 pivot = *r;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1022 /* Invariants:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1023 * pivot >= all in [lo, l).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1024 * pivot < all in [r, start).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1025 * The second is vacuously true at the start.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1026 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1027 assert(l < r);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1028 do {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1029 p = l + ((r - l) >> 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1030 IFLT(pivot, *p)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1031 r = p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1032 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1033 l = p+1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1034 } while (l < r);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1035 assert(l == r);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1036 /* The invariants still hold, so pivot >= all in [lo, l) and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1037 pivot < all in [l, start), so pivot belongs at l. Note
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1038 that if there are elements equal to pivot, l points to the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1039 first slot after them -- that's why this sort is stable.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1040 Slide over to make room.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1041 Caution: using memmove is much slower under MSVC 5;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1042 we're not usually moving many slots. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1043 for (p = start; p > l; --p)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1044 *p = *(p-1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1045 *l = pivot;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1046 if (lo.values != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1047 Py_ssize_t offset = lo.values - lo.keys;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1048 p = start + offset;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1049 pivot = *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1050 l += offset;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1051 for (p = start + offset; p > l; --p)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1052 *p = *(p-1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1053 *l = pivot;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1054 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1055 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1056 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1057
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1058 fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1059 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1060 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1061
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1062 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1063 Return the length of the run beginning at lo, in the slice [lo, hi). lo < hi
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1064 is required on entry. "A run" is the longest ascending sequence, with
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1065
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1066 lo[0] <= lo[1] <= lo[2] <= ...
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1067
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1068 or the longest descending sequence, with
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1069
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1070 lo[0] > lo[1] > lo[2] > ...
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1071
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1072 Boolean *descending is set to 0 in the former case, or to 1 in the latter.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1073 For its intended use in a stable mergesort, the strictness of the defn of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1074 "descending" is needed so that the caller can safely reverse a descending
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1075 sequence without violating stability (strict > ensures there are no equal
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1076 elements to get out of order).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1077
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1078 Returns -1 in case of error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1079 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1080 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1081 count_run(PyObject **lo, PyObject **hi, int *descending)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1082 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1083 Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1084 Py_ssize_t n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1085
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1086 assert(lo < hi);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1087 *descending = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1088 ++lo;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1089 if (lo == hi)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1090 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1091
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1092 n = 2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1093 IFLT(*lo, *(lo-1)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1094 *descending = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1095 for (lo = lo+1; lo < hi; ++lo, ++n) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1096 IFLT(*lo, *(lo-1))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1097 ;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1098 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1099 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1100 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1101 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1102 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1103 for (lo = lo+1; lo < hi; ++lo, ++n) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1104 IFLT(*lo, *(lo-1))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1105 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1106 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1107 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1108
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1109 return n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1110 fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1111 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1112 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1113
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1114 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1115 Locate the proper position of key in a sorted vector; if the vector contains
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1116 an element equal to key, return the position immediately to the left of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1117 the leftmost equal element. [gallop_right() does the same except returns
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1118 the position to the right of the rightmost equal element (if any).]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1119
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1120 "a" is a sorted vector with n elements, starting at a[0]. n must be > 0.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1121
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1122 "hint" is an index at which to begin the search, 0 <= hint < n. The closer
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1123 hint is to the final result, the faster this runs.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1124
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1125 The return value is the int k in 0..n such that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1126
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1127 a[k-1] < key <= a[k]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1128
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1129 pretending that *(a-1) is minus infinity and a[n] is plus infinity. IOW,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1130 key belongs at index k; or, IOW, the first k elements of a should precede
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1131 key, and the last n-k should follow key.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1132
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1133 Returns -1 on error. See listsort.txt for info on the method.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1134 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1135 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1136 gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1137 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1138 Py_ssize_t ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1139 Py_ssize_t lastofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1140 Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1141
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1142 assert(key && a && n > 0 && hint >= 0 && hint < n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1143
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1144 a += hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1145 lastofs = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1146 ofs = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1147 IFLT(*a, key) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1148 /* a[hint] < key -- gallop right, until
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1149 * a[hint + lastofs] < key <= a[hint + ofs]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1150 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1151 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1152 while (ofs < maxofs) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1153 IFLT(a[ofs], key) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1154 lastofs = ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1155 ofs = (ofs << 1) + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1156 if (ofs <= 0) /* int overflow */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1157 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1158 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1159 else /* key <= a[hint + ofs] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1160 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1161 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1162 if (ofs > maxofs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1163 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1164 /* Translate back to offsets relative to &a[0]. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1165 lastofs += hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1166 ofs += hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1167 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1168 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1169 /* key <= a[hint] -- gallop left, until
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1170 * a[hint - ofs] < key <= a[hint - lastofs]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1171 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1172 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1173 while (ofs < maxofs) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1174 IFLT(*(a-ofs), key)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1175 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1176 /* key <= a[hint - ofs] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1177 lastofs = ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1178 ofs = (ofs << 1) + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1179 if (ofs <= 0) /* int overflow */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1180 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1181 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1182 if (ofs > maxofs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1183 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1184 /* Translate back to positive offsets relative to &a[0]. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1185 k = lastofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1186 lastofs = hint - ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1187 ofs = hint - k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1188 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1189 a -= hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1190
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1191 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1192 /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1193 * right of lastofs but no farther right than ofs. Do a binary
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1194 * search, with invariant a[lastofs-1] < key <= a[ofs].
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1195 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1196 ++lastofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1197 while (lastofs < ofs) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1198 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1199
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1200 IFLT(a[m], key)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1201 lastofs = m+1; /* a[m] < key */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1202 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1203 ofs = m; /* key <= a[m] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1204 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1205 assert(lastofs == ofs); /* so a[ofs-1] < key <= a[ofs] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1206 return ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1207
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1208 fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1209 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1210 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1211
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1212 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1213 Exactly like gallop_left(), except that if key already exists in a[0:n],
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1214 finds the position immediately to the right of the rightmost equal value.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1215
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1216 The return value is the int k in 0..n such that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1217
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1218 a[k-1] <= key < a[k]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1219
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1220 or -1 if error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1221
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1222 The code duplication is massive, but this is enough different given that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1223 we're sticking to "<" comparisons that it's much harder to follow if
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1224 written as one routine with yet another "left or right?" flag.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1225 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1226 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1227 gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1228 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1229 Py_ssize_t ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1230 Py_ssize_t lastofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1231 Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1232
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1233 assert(key && a && n > 0 && hint >= 0 && hint < n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1234
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1235 a += hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1236 lastofs = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1237 ofs = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1238 IFLT(key, *a) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1239 /* key < a[hint] -- gallop left, until
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1240 * a[hint - ofs] <= key < a[hint - lastofs]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1241 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1242 const Py_ssize_t maxofs = hint + 1; /* &a[0] is lowest */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1243 while (ofs < maxofs) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1244 IFLT(key, *(a-ofs)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1245 lastofs = ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1246 ofs = (ofs << 1) + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1247 if (ofs <= 0) /* int overflow */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1248 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1249 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1250 else /* a[hint - ofs] <= key */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1251 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1252 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1253 if (ofs > maxofs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1254 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1255 /* Translate back to positive offsets relative to &a[0]. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1256 k = lastofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1257 lastofs = hint - ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1258 ofs = hint - k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1259 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1260 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1261 /* a[hint] <= key -- gallop right, until
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1262 * a[hint + lastofs] <= key < a[hint + ofs]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1263 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1264 const Py_ssize_t maxofs = n - hint; /* &a[n-1] is highest */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1265 while (ofs < maxofs) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1266 IFLT(key, a[ofs])
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1267 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1268 /* a[hint + ofs] <= key */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1269 lastofs = ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1270 ofs = (ofs << 1) + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1271 if (ofs <= 0) /* int overflow */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1272 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1273 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1274 if (ofs > maxofs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1275 ofs = maxofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1276 /* Translate back to offsets relative to &a[0]. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1277 lastofs += hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1278 ofs += hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1279 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1280 a -= hint;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1281
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1282 assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1283 /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1284 * right of lastofs but no farther right than ofs. Do a binary
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1285 * search, with invariant a[lastofs-1] <= key < a[ofs].
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1286 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1287 ++lastofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1288 while (lastofs < ofs) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1289 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1290
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1291 IFLT(key, a[m])
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1292 ofs = m; /* key < a[m] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1293 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1294 lastofs = m+1; /* a[m] <= key */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1295 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1296 assert(lastofs == ofs); /* so a[ofs-1] <= key < a[ofs] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1297 return ofs;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1298
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1299 fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1300 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1301 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1302
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1303 /* The maximum number of entries in a MergeState's pending-runs stack.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1304 * This is enough to sort arrays of size up to about
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1305 * 32 * phi ** MAX_MERGE_PENDING
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1306 * where phi ~= 1.618. 85 is ridiculouslylarge enough, good for an array
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1307 * with 2**64 elements.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1308 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1309 #define MAX_MERGE_PENDING 85
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1310
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1311 /* When we get into galloping mode, we stay there until both runs win less
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1312 * often than MIN_GALLOP consecutive times. See listsort.txt for more info.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1313 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1314 #define MIN_GALLOP 7
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1315
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1316 /* Avoid malloc for small temp arrays. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1317 #define MERGESTATE_TEMP_SIZE 256
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1318
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1319 /* One MergeState exists on the stack per invocation of mergesort. It's just
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1320 * a convenient way to pass state around among the helper functions.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1321 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1322 struct s_slice {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1323 sortslice base;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1324 Py_ssize_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1325 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1326
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1327 typedef struct s_MergeState {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1328 /* This controls when we get *into* galloping mode. It's initialized
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1329 * to MIN_GALLOP. merge_lo and merge_hi tend to nudge it higher for
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1330 * random data, and lower for highly structured data.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1331 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1332 Py_ssize_t min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1333
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1334 /* 'a' is temp storage to help with merges. It contains room for
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1335 * alloced entries.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1336 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1337 sortslice a; /* may point to temparray below */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1338 Py_ssize_t alloced;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1339
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1340 /* A stack of n pending runs yet to be merged. Run #i starts at
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1341 * address base[i] and extends for len[i] elements. It's always
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1342 * true (so long as the indices are in bounds) that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1343 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1344 * pending[i].base + pending[i].len == pending[i+1].base
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1345 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1346 * so we could cut the storage for this, but it's a minor amount,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1347 * and keeping all the info explicit simplifies the code.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1348 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1349 int n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1350 struct s_slice pending[MAX_MERGE_PENDING];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1351
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1352 /* 'a' points to this when possible, rather than muck with malloc. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1353 PyObject *temparray[MERGESTATE_TEMP_SIZE];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1354 } MergeState;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1355
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1356 /* Conceptually a MergeState's constructor. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1357 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1358 merge_init(MergeState *ms, Py_ssize_t list_size, int has_keyfunc)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1359 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1360 assert(ms != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1361 if (has_keyfunc) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1362 /* The temporary space for merging will need at most half the list
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1363 * size rounded up. Use the minimum possible space so we can use the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1364 * rest of temparray for other things. In particular, if there is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1365 * enough extra space, listsort() will use it to store the keys.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1366 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1367 ms->alloced = (list_size + 1) / 2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1368
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1369 /* ms->alloced describes how many keys will be stored at
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1370 ms->temparray, but we also need to store the values. Hence,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1371 ms->alloced is capped at half of MERGESTATE_TEMP_SIZE. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1372 if (MERGESTATE_TEMP_SIZE / 2 < ms->alloced)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1373 ms->alloced = MERGESTATE_TEMP_SIZE / 2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1374 ms->a.values = &ms->temparray[ms->alloced];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1375 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1376 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1377 ms->alloced = MERGESTATE_TEMP_SIZE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1378 ms->a.values = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1379 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1380 ms->a.keys = ms->temparray;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1381 ms->n = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1382 ms->min_gallop = MIN_GALLOP;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1383 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1384
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1385 /* Free all the temp memory owned by the MergeState. This must be called
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1386 * when you're done with a MergeState, and may be called before then if
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1387 * you want to free the temp memory early.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1388 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1389 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1390 merge_freemem(MergeState *ms)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1391 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1392 assert(ms != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1393 if (ms->a.keys != ms->temparray)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1394 PyMem_Free(ms->a.keys);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1395 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1396
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1397 /* Ensure enough temp memory for 'need' array slots is available.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1398 * Returns 0 on success and -1 if the memory can't be gotten.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1399 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1400 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1401 merge_getmem(MergeState *ms, Py_ssize_t need)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1402 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1403 int multiplier;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1404
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1405 assert(ms != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1406 if (need <= ms->alloced)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1407 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1408
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1409 multiplier = ms->a.values != NULL ? 2 : 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1410
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1411 /* Don't realloc! That can cost cycles to copy the old data, but
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1412 * we don't care what's in the block.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1413 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1414 merge_freemem(ms);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1415 if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*) / multiplier) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1416 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1417 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1418 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1419 ms->a.keys = (PyObject**)PyMem_Malloc(multiplier * need
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1420 * sizeof(PyObject *));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1421 if (ms->a.keys != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1422 ms->alloced = need;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1423 if (ms->a.values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1424 ms->a.values = &ms->a.keys[need];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1425 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1426 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1427 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1428 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1429 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1430 #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 : \
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1431 merge_getmem(MS, NEED))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1432
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1433 /* Merge the na elements starting at ssa with the nb elements starting at
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1434 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1435 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1436 * should have na <= nb. See listsort.txt for more info. Return 0 if
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1437 * successful, -1 if error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1438 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1439 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1440 merge_lo(MergeState *ms, sortslice ssa, Py_ssize_t na,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1441 sortslice ssb, Py_ssize_t nb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1442 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1443 Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1444 sortslice dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1445 int result = -1; /* guilty until proved innocent */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1446 Py_ssize_t min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1447
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1448 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1449 assert(ssa.keys + na == ssb.keys);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1450 if (MERGE_GETMEM(ms, na) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1451 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1452 sortslice_memcpy(&ms->a, 0, &ssa, 0, na);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1453 dest = ssa;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1454 ssa = ms->a;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1455
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1456 sortslice_copy_incr(&dest, &ssb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1457 --nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1458 if (nb == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1459 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1460 if (na == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1461 goto CopyB;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1462
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1463 min_gallop = ms->min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1464 for (;;) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1465 Py_ssize_t acount = 0; /* # of times A won in a row */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1466 Py_ssize_t bcount = 0; /* # of times B won in a row */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1467
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1468 /* Do the straightforward thing until (if ever) one run
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1469 * appears to win consistently.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1470 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1471 for (;;) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1472 assert(na > 1 && nb > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1473 k = ISLT(ssb.keys[0], ssa.keys[0]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1474 if (k) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1475 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1476 goto Fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1477 sortslice_copy_incr(&dest, &ssb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1478 ++bcount;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1479 acount = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1480 --nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1481 if (nb == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1482 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1483 if (bcount >= min_gallop)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1484 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1485 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1486 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1487 sortslice_copy_incr(&dest, &ssa);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1488 ++acount;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1489 bcount = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1490 --na;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1491 if (na == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1492 goto CopyB;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1493 if (acount >= min_gallop)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1494 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1495 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1496 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1497
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1498 /* One run is winning so consistently that galloping may
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1499 * be a huge win. So try that, and continue galloping until
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1500 * (if ever) neither run appears to be winning consistently
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1501 * anymore.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1502 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1503 ++min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1504 do {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1505 assert(na > 1 && nb > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1506 min_gallop -= min_gallop > 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1507 ms->min_gallop = min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1508 k = gallop_right(ssb.keys[0], ssa.keys, na, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1509 acount = k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1510 if (k) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1511 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1512 goto Fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1513 sortslice_memcpy(&dest, 0, &ssa, 0, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1514 sortslice_advance(&dest, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1515 sortslice_advance(&ssa, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1516 na -= k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1517 if (na == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1518 goto CopyB;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1519 /* na==0 is impossible now if the comparison
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1520 * function is consistent, but we can't assume
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1521 * that it is.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1522 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1523 if (na == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1524 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1525 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1526 sortslice_copy_incr(&dest, &ssb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1527 --nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1528 if (nb == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1529 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1530
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1531 k = gallop_left(ssa.keys[0], ssb.keys, nb, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1532 bcount = k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1533 if (k) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1534 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1535 goto Fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1536 sortslice_memmove(&dest, 0, &ssb, 0, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1537 sortslice_advance(&dest, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1538 sortslice_advance(&ssb, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1539 nb -= k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1540 if (nb == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1541 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1542 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1543 sortslice_copy_incr(&dest, &ssa);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1544 --na;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1545 if (na == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1546 goto CopyB;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1547 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1548 ++min_gallop; /* penalize it for leaving galloping mode */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1549 ms->min_gallop = min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1550 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1551 Succeed:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1552 result = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1553 Fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1554 if (na)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1555 sortslice_memcpy(&dest, 0, &ssa, 0, na);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1556 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1557 CopyB:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1558 assert(na == 1 && nb > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1559 /* The last element of ssa belongs at the end of the merge. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1560 sortslice_memmove(&dest, 0, &ssb, 0, nb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1561 sortslice_copy(&dest, nb, &ssa, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1562 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1563 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1564
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1565 /* Merge the na elements starting at pa with the nb elements starting at
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1566 * ssb.keys = ssa.keys + na in a stable way, in-place. na and nb must be > 0.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1567 * Must also have that ssa.keys[na-1] belongs at the end of the merge, and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1568 * should have na >= nb. See listsort.txt for more info. Return 0 if
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1569 * successful, -1 if error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1570 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1571 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1572 merge_hi(MergeState *ms, sortslice ssa, Py_ssize_t na,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1573 sortslice ssb, Py_ssize_t nb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1574 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1575 Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1576 sortslice dest, basea, baseb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1577 int result = -1; /* guilty until proved innocent */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1578 Py_ssize_t min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1579
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1580 assert(ms && ssa.keys && ssb.keys && na > 0 && nb > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1581 assert(ssa.keys + na == ssb.keys);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1582 if (MERGE_GETMEM(ms, nb) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1583 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1584 dest = ssb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1585 sortslice_advance(&dest, nb-1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1586 sortslice_memcpy(&ms->a, 0, &ssb, 0, nb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1587 basea = ssa;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1588 baseb = ms->a;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1589 ssb.keys = ms->a.keys + nb - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1590 if (ssb.values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1591 ssb.values = ms->a.values + nb - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1592 sortslice_advance(&ssa, na - 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1593
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1594 sortslice_copy_decr(&dest, &ssa);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1595 --na;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1596 if (na == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1597 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1598 if (nb == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1599 goto CopyA;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1600
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1601 min_gallop = ms->min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1602 for (;;) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1603 Py_ssize_t acount = 0; /* # of times A won in a row */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1604 Py_ssize_t bcount = 0; /* # of times B won in a row */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1605
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1606 /* Do the straightforward thing until (if ever) one run
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1607 * appears to win consistently.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1608 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1609 for (;;) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1610 assert(na > 0 && nb > 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1611 k = ISLT(ssb.keys[0], ssa.keys[0]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1612 if (k) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1613 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1614 goto Fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1615 sortslice_copy_decr(&dest, &ssa);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1616 ++acount;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1617 bcount = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1618 --na;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1619 if (na == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1620 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1621 if (acount >= min_gallop)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1622 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1623 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1624 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1625 sortslice_copy_decr(&dest, &ssb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1626 ++bcount;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1627 acount = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1628 --nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1629 if (nb == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1630 goto CopyA;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1631 if (bcount >= min_gallop)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1632 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1633 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1634 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1635
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1636 /* One run is winning so consistently that galloping may
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1637 * be a huge win. So try that, and continue galloping until
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1638 * (if ever) neither run appears to be winning consistently
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1639 * anymore.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1640 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1641 ++min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1642 do {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1643 assert(na > 0 && nb > 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1644 min_gallop -= min_gallop > 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1645 ms->min_gallop = min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1646 k = gallop_right(ssb.keys[0], basea.keys, na, na-1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1647 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1648 goto Fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1649 k = na - k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1650 acount = k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1651 if (k) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1652 sortslice_advance(&dest, -k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1653 sortslice_advance(&ssa, -k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1654 sortslice_memmove(&dest, 1, &ssa, 1, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1655 na -= k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1656 if (na == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1657 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1658 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1659 sortslice_copy_decr(&dest, &ssb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1660 --nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1661 if (nb == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1662 goto CopyA;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1663
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1664 k = gallop_left(ssa.keys[0], baseb.keys, nb, nb-1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1665 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1666 goto Fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1667 k = nb - k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1668 bcount = k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1669 if (k) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1670 sortslice_advance(&dest, -k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1671 sortslice_advance(&ssb, -k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1672 sortslice_memcpy(&dest, 1, &ssb, 1, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1673 nb -= k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1674 if (nb == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1675 goto CopyA;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1676 /* nb==0 is impossible now if the comparison
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1677 * function is consistent, but we can't assume
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1678 * that it is.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1679 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1680 if (nb == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1681 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1682 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1683 sortslice_copy_decr(&dest, &ssa);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1684 --na;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1685 if (na == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1686 goto Succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1687 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1688 ++min_gallop; /* penalize it for leaving galloping mode */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1689 ms->min_gallop = min_gallop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1690 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1691 Succeed:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1692 result = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1693 Fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1694 if (nb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1695 sortslice_memcpy(&dest, -(nb-1), &baseb, 0, nb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1696 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1697 CopyA:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1698 assert(nb == 1 && na > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1699 /* The first element of ssb belongs at the front of the merge. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1700 sortslice_memmove(&dest, 1-na, &ssa, 1-na, na);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1701 sortslice_advance(&dest, -na);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1702 sortslice_advance(&ssa, -na);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1703 sortslice_copy(&dest, 0, &ssb, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1704 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1705 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1706
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1707 /* Merge the two runs at stack indices i and i+1.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1708 * Returns 0 on success, -1 on error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1709 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1710 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1711 merge_at(MergeState *ms, Py_ssize_t i)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1712 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1713 sortslice ssa, ssb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1714 Py_ssize_t na, nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1715 Py_ssize_t k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1716
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1717 assert(ms != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1718 assert(ms->n >= 2);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1719 assert(i >= 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1720 assert(i == ms->n - 2 || i == ms->n - 3);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1721
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1722 ssa = ms->pending[i].base;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1723 na = ms->pending[i].len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1724 ssb = ms->pending[i+1].base;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1725 nb = ms->pending[i+1].len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1726 assert(na > 0 && nb > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1727 assert(ssa.keys + na == ssb.keys);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1728
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1729 /* Record the length of the combined runs; if i is the 3rd-last
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1730 * run now, also slide over the last run (which isn't involved
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1731 * in this merge). The current run i+1 goes away in any case.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1732 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1733 ms->pending[i].len = na + nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1734 if (i == ms->n - 3)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1735 ms->pending[i+1] = ms->pending[i+2];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1736 --ms->n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1737
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1738 /* Where does b start in a? Elements in a before that can be
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1739 * ignored (already in place).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1740 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1741 k = gallop_right(*ssb.keys, ssa.keys, na, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1742 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1743 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1744 sortslice_advance(&ssa, k);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1745 na -= k;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1746 if (na == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1747 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1748
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1749 /* Where does a end in b? Elements in b after that can be
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1750 * ignored (already in place).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1751 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1752 nb = gallop_left(ssa.keys[na-1], ssb.keys, nb, nb-1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1753 if (nb <= 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1754 return nb;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1755
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1756 /* Merge what remains of the runs, using a temp array with
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1757 * min(na, nb) elements.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1758 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1759 if (na <= nb)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1760 return merge_lo(ms, ssa, na, ssb, nb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1761 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1762 return merge_hi(ms, ssa, na, ssb, nb);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1763 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1764
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1765 /* Examine the stack of runs waiting to be merged, merging adjacent runs
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1766 * until the stack invariants are re-established:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1767 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1768 * 1. len[-3] > len[-2] + len[-1]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1769 * 2. len[-2] > len[-1]
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1770 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1771 * See listsort.txt for more info.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1772 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1773 * Returns 0 on success, -1 on error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1774 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1775 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1776 merge_collapse(MergeState *ms)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1777 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1778 struct s_slice *p = ms->pending;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1779
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1780 assert(ms);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1781 while (ms->n > 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1782 Py_ssize_t n = ms->n - 2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1783 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1784 if (p[n-1].len < p[n+1].len)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1785 --n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1786 if (merge_at(ms, n) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1787 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1788 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1789 else if (p[n].len <= p[n+1].len) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1790 if (merge_at(ms, n) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1791 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1792 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1793 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1794 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1795 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1796 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1797 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1798
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1799 /* Regardless of invariants, merge all runs on the stack until only one
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1800 * remains. This is used at the end of the mergesort.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1801 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1802 * Returns 0 on success, -1 on error.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1803 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1804 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1805 merge_force_collapse(MergeState *ms)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1806 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1807 struct s_slice *p = ms->pending;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1808
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1809 assert(ms);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1810 while (ms->n > 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1811 Py_ssize_t n = ms->n - 2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1812 if (n > 0 && p[n-1].len < p[n+1].len)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1813 --n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1814 if (merge_at(ms, n) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1815 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1816 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1817 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1818 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1819
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1820 /* Compute a good value for the minimum run length; natural runs shorter
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1821 * than this are boosted artificially via binary insertion.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1822 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1823 * If n < 64, return n (it's too small to bother with fancy stuff).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1824 * Else if n is an exact power of 2, return 32.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1825 * Else return an int k, 32 <= k <= 64, such that n/k is close to, but
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1826 * strictly less than, an exact power of 2.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1827 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1828 * See listsort.txt for more info.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1829 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1830 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1831 merge_compute_minrun(Py_ssize_t n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1832 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1833 Py_ssize_t r = 0; /* becomes 1 if any 1 bits are shifted off */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1834
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1835 assert(n >= 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1836 while (n >= 64) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1837 r |= n & 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1838 n >>= 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1839 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1840 return n + r;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1841 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1842
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1843 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1844 reverse_sortslice(sortslice *s, Py_ssize_t n)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1845 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1846 reverse_slice(s->keys, &s->keys[n]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1847 if (s->values != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1848 reverse_slice(s->values, &s->values[n]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1849 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1850
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1851 /* An adaptive, stable, natural mergesort. See listsort.txt.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1852 * Returns Py_None on success, NULL on error. Even in case of error, the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1853 * list will be some permutation of its input state (nothing is lost or
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1854 * duplicated).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1855 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1856 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1857 listsort(PyListObject *self, PyObject *args, PyObject *kwds)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1858 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1859 MergeState ms;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1860 Py_ssize_t nremaining;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1861 Py_ssize_t minrun;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1862 sortslice lo;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1863 Py_ssize_t saved_ob_size, saved_allocated;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1864 PyObject **saved_ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1865 PyObject **final_ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1866 PyObject *result = NULL; /* guilty until proved innocent */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1867 int reverse = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1868 PyObject *keyfunc = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1869 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1870 static char *kwlist[] = {"key", "reverse", 0};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1871 PyObject **keys;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1872
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1873 assert(self != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1874 assert (PyList_Check(self));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1875 if (args != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1876 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:sort",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1877 kwlist, &keyfunc, &reverse))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1878 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1879 if (Py_SIZE(args) > 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1880 PyErr_SetString(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1881 "must use keyword argument for key function");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1882 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1883 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1884 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1885 if (keyfunc == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1886 keyfunc = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1887
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1888 /* The list is temporarily made empty, so that mutations performed
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1889 * by comparison functions can't affect the slice of memory we're
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1890 * sorting (allowing mutations during sorting is a core-dump
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1891 * factory, since ob_item may change).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1892 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1893 saved_ob_size = Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1894 saved_ob_item = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1895 saved_allocated = self->allocated;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1896 Py_SIZE(self) = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1897 self->ob_item = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1898 self->allocated = -1; /* any operation will reset it to >= 0 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1899
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1900 if (keyfunc == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1901 keys = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1902 lo.keys = saved_ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1903 lo.values = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1904 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1905 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1906 if (saved_ob_size < MERGESTATE_TEMP_SIZE/2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1907 /* Leverage stack space we allocated but won't otherwise use */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1908 keys = &ms.temparray[saved_ob_size+1];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1909 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1910 keys = PyMem_MALLOC(sizeof(PyObject *) * saved_ob_size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1911 if (keys == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1912 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1913 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1914
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1915 for (i = 0; i < saved_ob_size ; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1916 keys[i] = PyObject_CallFunctionObjArgs(keyfunc, saved_ob_item[i],
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1917 NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1918 if (keys[i] == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1919 for (i=i-1 ; i>=0 ; i--)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1920 Py_DECREF(keys[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1921 if (keys != &ms.temparray[saved_ob_size+1])
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1922 PyMem_FREE(keys);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1923 goto keyfunc_fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1924 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1925 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1926
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1927 lo.keys = keys;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1928 lo.values = saved_ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1929 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1930
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1931 merge_init(&ms, saved_ob_size, keys != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1932
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1933 nremaining = saved_ob_size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1934 if (nremaining < 2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1935 goto succeed;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1936
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1937 /* Reverse sort stability achieved by initially reversing the list,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1938 applying a stable forward sort, then reversing the final result. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1939 if (reverse) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1940 if (keys != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1941 reverse_slice(&keys[0], &keys[saved_ob_size]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1942 reverse_slice(&saved_ob_item[0], &saved_ob_item[saved_ob_size]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1943 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1944
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1945 /* March over the array once, left to right, finding natural runs,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1946 * and extending short natural runs to minrun elements.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1947 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1948 minrun = merge_compute_minrun(nremaining);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1949 do {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1950 int descending;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1951 Py_ssize_t n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1952
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1953 /* Identify next run. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1954 n = count_run(lo.keys, lo.keys + nremaining, &descending);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1955 if (n < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1956 goto fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1957 if (descending)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1958 reverse_sortslice(&lo, n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1959 /* If short, extend to min(minrun, nremaining). */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1960 if (n < minrun) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1961 const Py_ssize_t force = nremaining <= minrun ?
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1962 nremaining : minrun;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1963 if (binarysort(lo, lo.keys + force, lo.keys + n) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1964 goto fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1965 n = force;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1966 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1967 /* Push run onto pending-runs stack, and maybe merge. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1968 assert(ms.n < MAX_MERGE_PENDING);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1969 ms.pending[ms.n].base = lo;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1970 ms.pending[ms.n].len = n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1971 ++ms.n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1972 if (merge_collapse(&ms) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1973 goto fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1974 /* Advance to find next run. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1975 sortslice_advance(&lo, n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1976 nremaining -= n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1977 } while (nremaining);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1978
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1979 if (merge_force_collapse(&ms) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1980 goto fail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1981 assert(ms.n == 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1982 assert(keys == NULL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1983 ? ms.pending[0].base.keys == saved_ob_item
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1984 : ms.pending[0].base.keys == &keys[0]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1985 assert(ms.pending[0].len == saved_ob_size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1986 lo = ms.pending[0].base;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1987
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1988 succeed:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1989 result = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1990 fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1991 if (keys != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1992 for (i = 0; i < saved_ob_size; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1993 Py_DECREF(keys[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1994 if (keys != &ms.temparray[saved_ob_size+1])
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1995 PyMem_FREE(keys);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1996 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1997
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1998 if (self->allocated != -1 && result != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1999 /* The user mucked with the list during the sort,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2000 * and we don't already have another error to report.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2001 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2002 PyErr_SetString(PyExc_ValueError, "list modified during sort");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2003 result = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2004 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2005
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2006 if (reverse && saved_ob_size > 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2007 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2008
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2009 merge_freemem(&ms);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2010
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2011 keyfunc_fail:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2012 final_ob_item = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2013 i = Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2014 Py_SIZE(self) = saved_ob_size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2015 self->ob_item = saved_ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2016 self->allocated = saved_allocated;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2017 if (final_ob_item != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2018 /* we cannot use list_clear() for this because it does not
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2019 guarantee that the list is really empty when it returns */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2020 while (--i >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2021 Py_XDECREF(final_ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2022 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2023 PyMem_FREE(final_ob_item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2024 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2025 Py_XINCREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2026 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2027 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2028 #undef IFLT
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2029 #undef ISLT
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2030
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2031 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2032 PyList_Sort(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2033 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2034 if (v == NULL || !PyList_Check(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2035 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2036 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2037 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2038 v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2039 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2040 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2041 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2042 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2043 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2044
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2045 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2046 listreverse(PyListObject *self)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2047 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2048 if (Py_SIZE(self) > 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2049 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2050 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2051 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2052
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2053 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2054 PyList_Reverse(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2055 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2056 PyListObject *self = (PyListObject *)v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2057
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2058 if (v == NULL || !PyList_Check(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2059 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2060 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2061 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2062 if (Py_SIZE(self) > 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2063 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2064 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2065 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2066
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2067 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2068 PyList_AsTuple(PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2069 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2070 PyObject *w;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2071 PyObject **p, **q;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2072 Py_ssize_t n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2073 if (v == NULL || !PyList_Check(v)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2074 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2075 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2076 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2077 n = Py_SIZE(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2078 w = PyTuple_New(n);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2079 if (w == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2080 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2081 p = ((PyTupleObject *)w)->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2082 q = ((PyListObject *)v)->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2083 while (--n >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2084 Py_INCREF(*q);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2085 *p = *q;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2086 p++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2087 q++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2088 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2089 return w;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2090 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2091
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2092 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2093 listindex(PyListObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2094 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2095 Py_ssize_t i, start=0, stop=Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2096 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2097
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2098 if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2099 _PyEval_SliceIndex, &start,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2100 _PyEval_SliceIndex, &stop))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2101 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2102 if (start < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2103 start += Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2104 if (start < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2105 start = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2106 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2107 if (stop < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2108 stop += Py_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2109 if (stop < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2110 stop = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2111 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2112 for (i = start; i < stop && i < Py_SIZE(self); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2113 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2114 if (cmp > 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2115 return PyLong_FromSsize_t(i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2116 else if (cmp < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2117 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2118 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2119 PyErr_Format(PyExc_ValueError, "%R is not in list", v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2120 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2121 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2122
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2123 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2124 listcount(PyListObject *self, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2125 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2126 Py_ssize_t count = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2127 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2128
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2129 for (i = 0; i < Py_SIZE(self); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2130 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2131 if (cmp > 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2132 count++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2133 else if (cmp < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2134 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2135 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2136 return PyLong_FromSsize_t(count);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2137 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2138
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2139 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2140 listremove(PyListObject *self, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2141 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2142 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2143
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2144 for (i = 0; i < Py_SIZE(self); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2145 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2146 if (cmp > 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2147 if (list_ass_slice(self, i, i+1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2148 (PyObject *)NULL) == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2149 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2150 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2151 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2152 else if (cmp < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2153 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2154 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2155 PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2156 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2157 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2158
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2159 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2160 list_traverse(PyListObject *o, visitproc visit, void *arg)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2161 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2162 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2163
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2164 for (i = Py_SIZE(o); --i >= 0; )
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2165 Py_VISIT(o->ob_item[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2166 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2167 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2168
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2169 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2170 list_richcompare(PyObject *v, PyObject *w, int op)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2171 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2172 PyListObject *vl, *wl;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2173 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2174
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2175 if (!PyList_Check(v) || !PyList_Check(w))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2176 Py_RETURN_NOTIMPLEMENTED;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2177
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2178 vl = (PyListObject *)v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2179 wl = (PyListObject *)w;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2180
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2181 if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2182 /* Shortcut: if the lengths differ, the lists differ */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2183 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2184 if (op == Py_EQ)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2185 res = Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2186 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2187 res = Py_True;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2188 Py_INCREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2189 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2190 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2191
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2192 /* Search for the first index where items are different */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2193 for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2194 int k = PyObject_RichCompareBool(vl->ob_item[i],
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2195 wl->ob_item[i], Py_EQ);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2196 if (k < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2197 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2198 if (!k)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2199 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2200 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2201
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2202 if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2203 /* No more items to compare -- compare sizes */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2204 Py_ssize_t vs = Py_SIZE(vl);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2205 Py_ssize_t ws = Py_SIZE(wl);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2206 int cmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2207 PyObject *res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2208 switch (op) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2209 case Py_LT: cmp = vs < ws; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2210 case Py_LE: cmp = vs <= ws; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2211 case Py_EQ: cmp = vs == ws; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2212 case Py_NE: cmp = vs != ws; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2213 case Py_GT: cmp = vs > ws; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2214 case Py_GE: cmp = vs >= ws; break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2215 default: return NULL; /* cannot happen */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2216 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2217 if (cmp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2218 res = Py_True;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2219 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2220 res = Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2221 Py_INCREF(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2222 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2223 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2224
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2225 /* We have an item that differs -- shortcuts for EQ/NE */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2226 if (op == Py_EQ) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2227 Py_INCREF(Py_False);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2228 return Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2229 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2230 if (op == Py_NE) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2231 Py_INCREF(Py_True);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2232 return Py_True;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2233 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2234
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2235 /* Compare the final item again using the proper operator */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2236 return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2237 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2238
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2239 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2240 list_init(PyListObject *self, PyObject *args, PyObject *kw)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2241 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2242 PyObject *arg = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2243 static char *kwlist[] = {"sequence", 0};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2244
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2245 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2246 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2247
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2248 /* Verify list invariants established by PyType_GenericAlloc() */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2249 assert(0 <= Py_SIZE(self));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2250 assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2251 assert(self->ob_item != NULL ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2252 self->allocated == 0 || self->allocated == -1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2253
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2254 /* Empty previous contents */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2255 if (self->ob_item != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2256 (void)list_clear(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2257 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2258 if (arg != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2259 PyObject *rv = listextend(self, arg);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2260 if (rv == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2261 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2262 Py_DECREF(rv);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2263 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2264 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2265 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2266
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2267 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2268 list_sizeof(PyListObject *self)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2269 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2270 Py_ssize_t res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2271
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2272 res = sizeof(PyListObject) + self->allocated * sizeof(void*);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2273 return PyLong_FromSsize_t(res);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2274 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2275
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2276 static PyObject *list_iter(PyObject *seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2277 static PyObject *list_reversed(PyListObject* seq, PyObject* unused);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2278
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2279 PyDoc_STRVAR(getitem_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2280 "x.__getitem__(y) <==> x[y]");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2281 PyDoc_STRVAR(reversed_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2282 "L.__reversed__() -- return a reverse iterator over the list");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2283 PyDoc_STRVAR(sizeof_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2284 "L.__sizeof__() -- size of L in memory, in bytes");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2285 PyDoc_STRVAR(clear_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2286 "L.clear() -> None -- remove all items from L");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2287 PyDoc_STRVAR(copy_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2288 "L.copy() -> list -- a shallow copy of L");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2289 PyDoc_STRVAR(append_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2290 "L.append(object) -> None -- append object to end");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2291 PyDoc_STRVAR(extend_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2292 "L.extend(iterable) -> None -- extend list by appending elements from the iterable");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2293 PyDoc_STRVAR(insert_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2294 "L.insert(index, object) -- insert object before index");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2295 PyDoc_STRVAR(pop_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2296 "L.pop([index]) -> item -- remove and return item at index (default last).\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2297 "Raises IndexError if list is empty or index is out of range.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2298 PyDoc_STRVAR(remove_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2299 "L.remove(value) -> None -- remove first occurrence of value.\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2300 "Raises ValueError if the value is not present.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2301 PyDoc_STRVAR(index_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2302 "L.index(value, [start, [stop]]) -> integer -- return first index of value.\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2303 "Raises ValueError if the value is not present.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2304 PyDoc_STRVAR(count_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2305 "L.count(value) -> integer -- return number of occurrences of value");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2306 PyDoc_STRVAR(reverse_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2307 "L.reverse() -- reverse *IN PLACE*");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2308 PyDoc_STRVAR(sort_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2309 "L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2310
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2311 static PyObject *list_subscript(PyListObject*, PyObject*);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2312
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2313 static PyMethodDef list_methods[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2314 {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2315 {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2316 {"__sizeof__", (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2317 {"clear", (PyCFunction)listclear, METH_NOARGS, clear_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2318 {"copy", (PyCFunction)listcopy, METH_NOARGS, copy_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2319 {"append", (PyCFunction)listappend, METH_O, append_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2320 {"insert", (PyCFunction)listinsert, METH_VARARGS, insert_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2321 {"extend", (PyCFunction)listextend, METH_O, extend_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2322 {"pop", (PyCFunction)listpop, METH_VARARGS, pop_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2323 {"remove", (PyCFunction)listremove, METH_O, remove_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2324 {"index", (PyCFunction)listindex, METH_VARARGS, index_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2325 {"count", (PyCFunction)listcount, METH_O, count_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2326 {"reverse", (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2327 {"sort", (PyCFunction)listsort, METH_VARARGS | METH_KEYWORDS, sort_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2328 {NULL, NULL} /* sentinel */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2329 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2330
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2331 static PySequenceMethods list_as_sequence = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2332 (lenfunc)list_length, /* sq_length */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2333 (binaryfunc)list_concat, /* sq_concat */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2334 (ssizeargfunc)list_repeat, /* sq_repeat */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2335 (ssizeargfunc)list_item, /* sq_item */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2336 0, /* sq_slice */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2337 (ssizeobjargproc)list_ass_item, /* sq_ass_item */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2338 0, /* sq_ass_slice */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2339 (objobjproc)list_contains, /* sq_contains */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2340 (binaryfunc)list_inplace_concat, /* sq_inplace_concat */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2341 (ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2342 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2343
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2344 PyDoc_STRVAR(list_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2345 "list() -> new empty list\n"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2346 "list(iterable) -> new list initialized from iterable's items");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2347
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2348 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2349 list_subscript(PyListObject* self, PyObject* item)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2350 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2351 if (PyIndex_Check(item)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2352 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2353 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2354 if (i == -1 && PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2355 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2356 if (i < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2357 i += PyList_GET_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2358 return list_item(self, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2359 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2360 else if (PySlice_Check(item)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2361 Py_ssize_t start, stop, step, slicelength, cur, i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2362 PyObject* result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2363 PyObject* it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2364 PyObject **src, **dest;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2365
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2366 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2367 &start, &stop, &step, &slicelength) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2368 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2369 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2370
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2371 if (slicelength <= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2372 return PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2373 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2374 else if (step == 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2375 return list_slice(self, start, stop);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2376 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2377 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2378 result = PyList_New(slicelength);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2379 if (!result) return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2380
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2381 src = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2382 dest = ((PyListObject *)result)->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2383 for (cur = start, i = 0; i < slicelength;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2384 cur += (size_t)step, i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2385 it = src[cur];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2386 Py_INCREF(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2387 dest[i] = it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2388 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2389
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2390 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2391 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2392 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2393 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2394 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2395 "list indices must be integers, not %.200s",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2396 item->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2397 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2398 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2399 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2400
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2401 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2402 list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2403 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2404 if (PyIndex_Check(item)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2405 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2406 if (i == -1 && PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2407 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2408 if (i < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2409 i += PyList_GET_SIZE(self);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2410 return list_ass_item(self, i, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2411 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2412 else if (PySlice_Check(item)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2413 Py_ssize_t start, stop, step, slicelength;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2414
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2415 if (PySlice_GetIndicesEx(item, Py_SIZE(self),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2416 &start, &stop, &step, &slicelength) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2417 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2418 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2419
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2420 if (step == 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2421 return list_ass_slice(self, start, stop, value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2422
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2423 /* Make sure s[5:2] = [..] inserts at the right place:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2424 before 5, not before 2. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2425 if ((step < 0 && start < stop) ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2426 (step > 0 && start > stop))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2427 stop = start;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2428
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2429 if (value == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2430 /* delete slice */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2431 PyObject **garbage;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2432 size_t cur;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2433 Py_ssize_t i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2434
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2435 if (slicelength <= 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2436 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2437
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2438 if (step < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2439 stop = start + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2440 start = stop + step*(slicelength - 1) - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2441 step = -step;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2442 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2443
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2444 assert((size_t)slicelength <=
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2445 PY_SIZE_MAX / sizeof(PyObject*));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2446
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2447 garbage = (PyObject**)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2448 PyMem_MALLOC(slicelength*sizeof(PyObject*));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2449 if (!garbage) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2450 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2451 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2452 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2453
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2454 /* drawing pictures might help understand these for
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2455 loops. Basically, we memmove the parts of the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2456 list that are *not* part of the slice: step-1
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2457 items for each item that is part of the slice,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2458 and then tail end of the list that was not
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2459 covered by the slice */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2460 for (cur = start, i = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2461 cur < (size_t)stop;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2462 cur += step, i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2463 Py_ssize_t lim = step - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2464
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2465 garbage[i] = PyList_GET_ITEM(self, cur);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2466
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2467 if (cur + step >= (size_t)Py_SIZE(self)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2468 lim = Py_SIZE(self) - cur - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2469 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2470
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2471 memmove(self->ob_item + cur - i,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2472 self->ob_item + cur + 1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2473 lim * sizeof(PyObject *));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2474 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2475 cur = start + (size_t)slicelength * step;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2476 if (cur < (size_t)Py_SIZE(self)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2477 memmove(self->ob_item + cur - slicelength,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2478 self->ob_item + cur,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2479 (Py_SIZE(self) - cur) *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2480 sizeof(PyObject *));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2481 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2482
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2483 Py_SIZE(self) -= slicelength;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2484 list_resize(self, Py_SIZE(self));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2485
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2486 for (i = 0; i < slicelength; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2487 Py_DECREF(garbage[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2488 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2489 PyMem_FREE(garbage);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2490
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2491 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2492 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2493 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2494 /* assign slice */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2495 PyObject *ins, *seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2496 PyObject **garbage, **seqitems, **selfitems;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2497 Py_ssize_t cur, i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2498
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2499 /* protect against a[::-1] = a */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2500 if (self == (PyListObject*)value) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2501 seq = list_slice((PyListObject*)value, 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2502 PyList_GET_SIZE(value));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2503 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2504 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2505 seq = PySequence_Fast(value,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2506 "must assign iterable "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2507 "to extended slice");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2508 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2509 if (!seq)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2510 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2511
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2512 if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2513 PyErr_Format(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2514 "attempt to assign sequence of "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2515 "size %zd to extended slice of "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2516 "size %zd",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2517 PySequence_Fast_GET_SIZE(seq),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2518 slicelength);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2519 Py_DECREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2520 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2521 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2522
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2523 if (!slicelength) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2524 Py_DECREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2525 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2526 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2527
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2528 garbage = (PyObject**)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2529 PyMem_MALLOC(slicelength*sizeof(PyObject*));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2530 if (!garbage) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2531 Py_DECREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2532 PyErr_NoMemory();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2533 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2534 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2535
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2536 selfitems = self->ob_item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2537 seqitems = PySequence_Fast_ITEMS(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2538 for (cur = start, i = 0; i < slicelength;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2539 cur += (size_t)step, i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2540 garbage[i] = selfitems[cur];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2541 ins = seqitems[i];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2542 Py_INCREF(ins);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2543 selfitems[cur] = ins;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2544 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2545
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2546 for (i = 0; i < slicelength; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2547 Py_DECREF(garbage[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2548 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2549
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2550 PyMem_FREE(garbage);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2551 Py_DECREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2552
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2553 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2554 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2555 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2556 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2557 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2558 "list indices must be integers, not %.200s",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2559 item->ob_type->tp_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2560 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2561 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2562 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2563
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2564 static PyMappingMethods list_as_mapping = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2565 (lenfunc)list_length,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2566 (binaryfunc)list_subscript,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2567 (objobjargproc)list_ass_subscript
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2568 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2569
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2570 PyTypeObject PyList_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2571 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2572 "list",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2573 sizeof(PyListObject),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2574 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2575 (destructor)list_dealloc, /* tp_dealloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2576 0, /* tp_print */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2577 0, /* tp_getattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2578 0, /* tp_setattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2579 0, /* tp_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2580 (reprfunc)list_repr, /* tp_repr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2581 0, /* tp_as_number */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2582 &list_as_sequence, /* tp_as_sequence */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2583 &list_as_mapping, /* tp_as_mapping */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2584 PyObject_HashNotImplemented, /* tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2585 0, /* tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2586 0, /* tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2587 PyObject_GenericGetAttr, /* tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2588 0, /* tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2589 0, /* tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2590 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2591 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2592 list_doc, /* tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2593 (traverseproc)list_traverse, /* tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2594 (inquiry)list_clear, /* tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2595 list_richcompare, /* tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2596 0, /* tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2597 list_iter, /* tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2598 0, /* tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2599 list_methods, /* tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2600 0, /* tp_members */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2601 0, /* tp_getset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2602 0, /* tp_base */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2603 0, /* tp_dict */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2604 0, /* tp_descr_get */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2605 0, /* tp_descr_set */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2606 0, /* tp_dictoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2607 (initproc)list_init, /* tp_init */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2608 PyType_GenericAlloc, /* tp_alloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2609 PyType_GenericNew, /* tp_new */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2610 PyObject_GC_Del, /* tp_free */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2611 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2612
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2613
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2614 /*********************** List Iterator **************************/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2615
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2616 typedef struct {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2617 PyObject_HEAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2618 long it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2619 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2620 } listiterobject;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2621
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2622 static PyObject *list_iter(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2623 static void listiter_dealloc(listiterobject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2624 static int listiter_traverse(listiterobject *, visitproc, void *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2625 static PyObject *listiter_next(listiterobject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2626 static PyObject *listiter_len(listiterobject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2627
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2628 PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it)).");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2629
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2630 static PyMethodDef listiter_methods[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2631 {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2632 {NULL, NULL} /* sentinel */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2633 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2634
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2635 PyTypeObject PyListIter_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2636 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2637 "list_iterator", /* tp_name */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2638 sizeof(listiterobject), /* tp_basicsize */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2639 0, /* tp_itemsize */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2640 /* methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2641 (destructor)listiter_dealloc, /* tp_dealloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2642 0, /* tp_print */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2643 0, /* tp_getattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2644 0, /* tp_setattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2645 0, /* tp_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2646 0, /* tp_repr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2647 0, /* tp_as_number */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2648 0, /* tp_as_sequence */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2649 0, /* tp_as_mapping */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2650 0, /* tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2651 0, /* tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2652 0, /* tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2653 PyObject_GenericGetAttr, /* tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2654 0, /* tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2655 0, /* tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2656 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2657 0, /* tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2658 (traverseproc)listiter_traverse, /* tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2659 0, /* tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2660 0, /* tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2661 0, /* tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2662 PyObject_SelfIter, /* tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2663 (iternextfunc)listiter_next, /* tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2664 listiter_methods, /* tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2665 0, /* tp_members */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2666 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2667
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2668
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2669 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2670 list_iter(PyObject *seq)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2671 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2672 listiterobject *it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2673
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2674 if (!PyList_Check(seq)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2675 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2676 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2677 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2678 it = PyObject_GC_New(listiterobject, &PyListIter_Type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2679 if (it == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2680 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2681 it->it_index = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2682 Py_INCREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2683 it->it_seq = (PyListObject *)seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2684 _PyObject_GC_TRACK(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2685 return (PyObject *)it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2686 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2687
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2688 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2689 listiter_dealloc(listiterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2690 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2691 _PyObject_GC_UNTRACK(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2692 Py_XDECREF(it->it_seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2693 PyObject_GC_Del(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2694 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2695
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2696 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2697 listiter_traverse(listiterobject *it, visitproc visit, void *arg)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2698 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2699 Py_VISIT(it->it_seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2700 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2701 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2702
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2703 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2704 listiter_next(listiterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2705 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2706 PyListObject *seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2707 PyObject *item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2708
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2709 assert(it != NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2710 seq = it->it_seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2711 if (seq == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2712 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2713 assert(PyList_Check(seq));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2714
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2715 if (it->it_index < PyList_GET_SIZE(seq)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2716 item = PyList_GET_ITEM(seq, it->it_index);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2717 ++it->it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2718 Py_INCREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2719 return item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2720 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2721
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2722 Py_DECREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2723 it->it_seq = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2724 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2725 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2726
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2727 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2728 listiter_len(listiterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2729 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2730 Py_ssize_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2731 if (it->it_seq) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2732 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2733 if (len >= 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2734 return PyLong_FromSsize_t(len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2735 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2736 return PyLong_FromLong(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2737 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2738 /*********************** List Reverse Iterator **************************/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2739
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2740 typedef struct {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2741 PyObject_HEAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2742 Py_ssize_t it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2743 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2744 } listreviterobject;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2745
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2746 static PyObject *list_reversed(PyListObject *, PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2747 static void listreviter_dealloc(listreviterobject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2748 static int listreviter_traverse(listreviterobject *, visitproc, void *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2749 static PyObject *listreviter_next(listreviterobject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2750 static PyObject *listreviter_len(listreviterobject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2751
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2752 static PyMethodDef listreviter_methods[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2753 {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2754 {NULL, NULL} /* sentinel */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2755 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2756
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2757 PyTypeObject PyListRevIter_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2758 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2759 "list_reverseiterator", /* tp_name */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2760 sizeof(listreviterobject), /* tp_basicsize */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2761 0, /* tp_itemsize */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2762 /* methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2763 (destructor)listreviter_dealloc, /* tp_dealloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2764 0, /* tp_print */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2765 0, /* tp_getattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2766 0, /* tp_setattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2767 0, /* tp_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2768 0, /* tp_repr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2769 0, /* tp_as_number */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2770 0, /* tp_as_sequence */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2771 0, /* tp_as_mapping */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2772 0, /* tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2773 0, /* tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2774 0, /* tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2775 PyObject_GenericGetAttr, /* tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2776 0, /* tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2777 0, /* tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2778 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2779 0, /* tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2780 (traverseproc)listreviter_traverse, /* tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2781 0, /* tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2782 0, /* tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2783 0, /* tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2784 PyObject_SelfIter, /* tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2785 (iternextfunc)listreviter_next, /* tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2786 listreviter_methods, /* tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2787 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2788 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2789
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2790 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2791 list_reversed(PyListObject *seq, PyObject *unused)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2792 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2793 listreviterobject *it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2794
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2795 it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2796 if (it == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2797 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2798 assert(PyList_Check(seq));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2799 it->it_index = PyList_GET_SIZE(seq) - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2800 Py_INCREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2801 it->it_seq = seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2802 PyObject_GC_Track(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2803 return (PyObject *)it;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2804 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2805
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2806 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2807 listreviter_dealloc(listreviterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2808 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2809 PyObject_GC_UnTrack(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2810 Py_XDECREF(it->it_seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2811 PyObject_GC_Del(it);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2812 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2813
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2814 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2815 listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2816 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2817 Py_VISIT(it->it_seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2818 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2819 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2820
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2821 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2822 listreviter_next(listreviterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2823 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2824 PyObject *item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2825 Py_ssize_t index = it->it_index;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2826 PyListObject *seq = it->it_seq;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2827
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2828 if (index>=0 && index < PyList_GET_SIZE(seq)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2829 item = PyList_GET_ITEM(seq, index);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2830 it->it_index--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2831 Py_INCREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2832 return item;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2833 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2834 it->it_index = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2835 if (seq != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2836 it->it_seq = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2837 Py_DECREF(seq);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2838 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2839 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2840 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2841
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2842 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2843 listreviter_len(listreviterobject *it)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2844 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2845 Py_ssize_t len = it->it_index + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2846 if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2847 len = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2848 return PyLong_FromSsize_t(len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2849 }