summaryrefslogtreecommitdiffstats
path: root/Objects/listobject.c
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2002-07-19 02:33:08 (GMT)
committerTim Peters <tim.peters@gmail.com>2002-07-19 02:33:08 (GMT)
commit8e2e7ca3302ad34f1967200f580d4c56658a5fdd (patch)
tree52a0c152902000bb159c43a89c1480ca0510a9a8 /Objects/listobject.c
parent7aeac9180e3d6df3d5db89ee7ff5941a81dc5a5d (diff)
downloadcpython-8e2e7ca3302ad34f1967200f580d4c56658a5fdd.zip
cpython-8e2e7ca3302ad34f1967200f580d4c56658a5fdd.tar.gz
cpython-8e2e7ca3302ad34f1967200f580d4c56658a5fdd.tar.bz2
Cleanup: Define one internal utility for reversing a list slice, and
use that everywhere.
Diffstat (limited to 'Objects/listobject.c')
-rw-r--r--Objects/listobject.c48
1 files changed, 20 insertions, 28 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 8173025..f24b95e 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -739,6 +739,22 @@ listpop(PyListObject *self, PyObject *args)
return v;
}
+/* Reverse a slice of a list in place, from lo up to (exclusive) hi. */
+static void
+reverse_slice(PyObject **lo, PyObject **hi)
+{
+ assert(lo && hi);
+
+ --hi;
+ while (lo < hi) {
+ PyObject *t = *lo;
+ *lo = *hi;
+ *hi = t;
+ ++lo;
+ --hi;
+ }
+}
+
/* New quicksort implementation for arrays of object pointers.
Thanks to discussions with Tim Peters. */
@@ -1026,14 +1042,8 @@ samplesortslice(PyObject **lo, PyObject **hi, PyObject *compare)
}
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);
+ reverse_slice(lo, r);
+ return binarysort(lo, hi, r, compare);
}
/* ----------------------------------------------------------
@@ -1321,28 +1331,10 @@ PyList_Sort(PyObject *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);
+ reverse_slice(self->ob_item, self->ob_item + self->ob_size);
Py_INCREF(Py_None);
return Py_None;
}
@@ -1354,7 +1346,7 @@ PyList_Reverse(PyObject *v)
PyErr_BadInternalCall();
return -1;
}
- _listreverse((PyListObject *)v);
+ listreverse((PyListObject *)v);
return 0;
}