📄 listobject.c
字号:
would slow things down.
*/
struct SamplesortStackNode {
/* Represents a slice of the array, from (& including) lo up
to (but excluding) hi. "extra" additional & adjacent elements
are pre-selected pivots (PPs), spanning [lo-extra, lo) if
extra > 0, or [hi, hi-extra) if extra < 0. The PPs are
already sorted, but nothing is known about the other elements
in [lo, hi). |extra| is always one less than a power of 2.
When extra is 0, we're out of PPs, and the slice must be
sorted by some other means. */
PyObject **lo;
PyObject **hi;
int extra;
};
/* The number of PPs we want is 2**k - 1, where 2**k is as close to
N / ln(N) as possible. So k ~= lg(N / ln(N)). Calling libm routines
is undesirable, so cutoff values are canned in the "cutoff" table
below: cutoff[i] is the smallest N such that k == CUTOFFBASE + i. */
#define CUTOFFBASE 4
const static long cutoff[] = {
43, /* smallest N such that k == 4 */
106, /* etc */
250,
576,
1298,
2885,
6339,
13805,
29843,
64116,
137030,
291554,
617916,
1305130,
2748295,
5771662,
12091672,
25276798,
52734615,
109820537,
228324027,
473977813,
982548444, /* smallest N such that k == 26 */
2034159050 /* largest N that fits in signed 32-bit; k == 27 */
};
static int
samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
/* compare -- comparison function object, or NULL for default */
{
register PyObject **l, **r;
register PyObject *tmp, *pivot;
register int k;
int n, extra, top, extraOnRight;
struct SamplesortStackNode stack[STACKSIZE];
/* assert lo <= hi */
n = hi - lo;
/* ----------------------------------------------------------
* Special cases
* --------------------------------------------------------*/
if (n < 2)
return 0;
/* Set r to the largest value such that [lo,r) is sorted.
This catches the already-sorted case, the all-the-same
case, and the appended-a-few-elements-to-a-sorted-list case.
If the array is unsorted, we're very likely to get out of
the loop fast, so the test is cheap if it doesn't pay off.
*/
/* assert lo < hi */
for (r = lo+1; r < hi; ++r) {
SETK(*r, *(r-1));
if (k < 0)
break;
}
/* [lo,r) is sorted, [r,hi) unknown. Get out cheap if there are
few unknowns, or few elements in total. */
if (hi - r <= MAXMERGE || n < MINSIZE)
return binarysort(lo, hi, r, compare);
/* Check for the array already being reverse-sorted. Typical
benchmark-driven silliness <wink>. */
/* assert lo < hi */
for (r = lo+1; r < hi; ++r) {
SETK(*(r-1), *r);
if (k < 0)
break;
}
if (hi - r <= MAXMERGE) {
/* Reverse the reversed prefix, then insert the tail */
PyObject **originalr = r;
l = lo;
do {
--r;
tmp = *l; *l = *r; *r = tmp;
++l;
} while (l < r);
return binarysort(lo, hi, originalr, compare);
}
/* ----------------------------------------------------------
* Normal case setup: a large array without obvious pattern.
* --------------------------------------------------------*/
/* extra := a power of 2 ~= n/ln(n), less 1.
First find the smallest extra s.t. n < cutoff[extra] */
for (extra = 0;
extra < sizeof(cutoff) / sizeof(cutoff[0]);
++extra) {
if (n < cutoff[extra])
break;
/* note that if we fall out of the loop, the value of
extra still makes *sense*, but may be smaller than
we would like (but the array has more than ~= 2**31
elements in this case!) */
}
/* Now k == extra - 1 + CUTOFFBASE. The smallest value k can
have is CUTOFFBASE-1, so
assert MINSIZE >= 2**(CUTOFFBASE-1) - 1 */
extra = (1 << (extra - 1 + CUTOFFBASE)) - 1;
/* assert extra > 0 and n >= extra */
/* Swap that many values to the start of the array. The
selection of elements is pseudo-random, but the same on
every run (this is intentional! timing algorithm changes is
a pain if timing varies across runs). */
{
unsigned int seed = n / extra; /* arbitrary */
unsigned int i;
for (i = 0; i < (unsigned)extra; ++i) {
/* j := random int in [i, n) */
unsigned int j;
seed = seed * 69069 + 7;
j = i + seed % (n - i);
tmp = lo[i]; lo[i] = lo[j]; lo[j] = tmp;
}
}
/* Recursively sort the preselected pivots. */
if (samplesortslice(lo, lo + extra, compare) < 0)
goto fail;
top = 0; /* index of available stack slot */
lo += extra; /* point to first unknown */
extraOnRight = 0; /* the PPs are at the left end */
/* ----------------------------------------------------------
* Partition [lo, hi), and repeat until out of work.
* --------------------------------------------------------*/
for (;;) {
/* assert lo <= hi, so n >= 0 */
n = hi - lo;
/* We may not want, or may not be able, to partition:
If n is small, it's quicker to insert.
If extra is 0, we're out of pivots, and *must* use
another method.
*/
if (n < MINPARTITIONSIZE || extra == 0) {
if (n >= MINSIZE) {
/* assert extra == 0
This is rare, since the average size
of a final block is only about
ln(original n). */
if (samplesortslice(lo, hi, compare) < 0)
goto fail;
}
else {
/* Binary insertion should be quicker,
and we can take advantage of the PPs
already being sorted. */
if (extraOnRight && extra) {
/* swap the PPs to the left end */
k = extra;
do {
tmp = *lo;
*lo = *hi;
*hi = tmp;
++lo; ++hi;
} while (--k);
}
if (binarysort(lo - extra, hi, lo,
compare) < 0)
goto fail;
}
/* Find another slice to work on. */
if (--top < 0)
break; /* no more -- done! */
lo = stack[top].lo;
hi = stack[top].hi;
extra = stack[top].extra;
extraOnRight = 0;
if (extra < 0) {
extraOnRight = 1;
extra = -extra;
}
continue;
}
/* Pretend the PPs are indexed 0, 1, ..., extra-1.
Then our preselected pivot is at (extra-1)/2, and we
want to move the PPs before that to the left end of
the slice, and the PPs after that to the right end.
The following section changes extra, lo, hi, and the
slice such that:
[lo-extra, lo) contains the smaller PPs.
*lo == our PP.
(lo, hi) contains the unknown elements.
[hi, hi+extra) contains the larger PPs.
*/
k = extra >>= 1; /* num PPs to move */
if (extraOnRight) {
/* Swap the smaller PPs to the left end.
Note that this loop actually moves k+1 items:
the last is our PP */
do {
tmp = *lo; *lo = *hi; *hi = tmp;
++lo; ++hi;
} while (k--);
}
else {
/* Swap the larger PPs to the right end. */
while (k--) {
--lo; --hi;
tmp = *lo; *lo = *hi; *hi = tmp;
}
}
--lo; /* *lo is now our PP */
pivot = *lo;
/* Now an almost-ordinary quicksort partition step.
Note that most of the time is spent here!
Only odd thing is that we partition into < and >=,
instead of the usual <= and >=. This helps when
there are lots of duplicates of different values,
because it eventually tends to make subfiles
"pure" (all duplicates), and we special-case for
duplicates later. */
l = lo + 1;
r = hi - 1;
/* assert lo < l < r < hi (small n weeded out above) */
do {
/* slide l right, looking for key >= pivot */
do {
SETK(*l, pivot);
if (k < 0)
++l;
else
break;
} while (l < r);
/* slide r left, looking for key < pivot */
while (l < r) {
register PyObject *rval = *r--;
SETK(rval, pivot);
if (k < 0) {
/* swap and advance */
r[1] = *l;
*l++ = rval;
break;
}
}
} while (l < r);
/* assert lo < r <= l < hi
assert r == l or r+1 == l
everything to the left of l is < pivot, and
everything to the right of r is >= pivot */
if (l == r) {
SETK(*r, pivot);
if (k < 0)
++l;
else
--r;
}
/* assert lo <= r and r+1 == l and l <= hi
assert r == lo or a[r] < pivot
assert a[lo] is pivot
assert l == hi or a[l] >= pivot
Swap the pivot into "the middle", so we can henceforth
ignore it.
*/
*lo = *r;
*r = pivot;
/* The following is true now, & will be preserved:
All in [lo,r) are < pivot
All in [r,l) == pivot (& so can be ignored)
All in [l,hi) are >= pivot */
/* Check for duplicates of the pivot. One compare is
wasted if there are no duplicates, but can win big
when there are.
Tricky: we're sticking to "<" compares, so deduce
equality indirectly. We know pivot <= *l, so they're
equal iff not pivot < *l.
*/
while (l < hi) {
/* pivot <= *l known */
SETK(pivot, *l);
if (k < 0)
break;
else
/* <= and not < implies == */
++l;
}
/* assert lo <= r < l <= hi
Partitions are [lo, r) and [l, hi) */
/* push fattest first; remember we still have extra PPs
to the left of the left chunk and to the right of
the right chunk! */
/* assert top < STACKSIZE */
if (r - lo <= hi - l) {
/* second is bigger */
stack[top].lo = l;
stack[top].hi = hi;
stack[top].extra = -extra;
hi = r;
extraOnRight = 0;
}
else {
/* first is bigger */
stack[top].lo = lo;
stack[top].hi = r;
stack[top].extra = extra;
lo = l;
extraOnRight = 1;
}
++top;
} /* end of partitioning loop */
return 0;
fail:
return -1;
}
#undef SETK
#ifndef SYMBIAN
staticforward PyTypeObject immutable_list_type;
#endif
static PyObject *
listsort(PyListObject *self, PyObject *args)
{
int err;
PyObject *compare = NULL;
PyTypeObject *savetype;
if (args != NULL) {
if (!PyArg_ParseTuple(args, "|O:sort", &compare))
return NULL;
}
savetype = self->ob_type;
self->ob_type = &immutable_list_type;
err = samplesortslice(self->ob_item,
self->ob_item + self->ob_size,
compare);
self->ob_type = savetype;
if (err < 0)
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
DL_EXPORT(int)
PyList_Sort(PyObject *v)
{
if (v == NULL || !PyList_Check(v)) {
PyErr_BadInternalCall();
return -1;
}
v = listsort((PyListObject *)v, (PyObject *)NULL);
if (v == NULL)
return -1;
Py_DECREF(v);
return 0;
}
static void
_listreverse(PyListObject *self)
{
register PyObject **p, **q;
register PyObject *tmp;
if (self->ob_size > 1) {
for (p = self->ob_item, q = self->ob_item + self->ob_size - 1;
p < q;
p++, q--)
{
tmp = *p;
*p = *q;
*q = tmp;
}
}
}
static PyObject *
listreverse(PyListObject *self)
{
_listreverse(self);
Py_INCREF(Py_None);
return Py_None;
}
DL_EXPORT(int)
PyList_Reverse(PyObject *v)
{
if (v == NULL || !PyList_Check(v)) {
PyErr_BadInternalCall();
return -1;
}
_listreverse((PyListObject *)v);
return 0;
}
DL_EXPORT(PyObject *)
PyList_AsTuple(PyObject *v)
{
PyObject *w;
PyObject **p;
int n;
if (v == NULL || !PyList_Check(v)) {
PyErr_BadInternalCall();
return NULL;
}
n = ((PyListObject *)v)->ob_size;
w = PyTuple_New(n);
if (w == NULL)
return NULL;
p = ((PyTupleObject *)w)->ob_item;
memcpy((void *)p,
(void *)((PyListObject *)v)->ob_item,
n*sizeof(PyObject *));
while (--n >= 0) {
Py_INCREF(*p);
p++;
}
return w;
}
static PyObject *
listindex(PyListObject *self, PyObject *v)
{
int i;
for (i = 0; i < self->ob_size; i++) {
int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
if (cmp > 0)
return PyInt_FromLong((long)i);
else if (cmp < 0)
return NULL;
}
PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
return NULL;
}
static PyObject *
listcount(PyListObject *self, PyObject *v)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -