generic swap(): lib/sort.c: rename swap to swap_func

This is to avoid name clashes for the introduction of a global swap()
macro.

Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Wu Fengguang 2009-01-07 18:09:11 -08:00 committed by Linus Torvalds
parent b67445fc17
commit b53907c010

View file

@ -32,11 +32,11 @@ static void generic_swap(void *a, void *b, int size)
* @base: pointer to data to sort * @base: pointer to data to sort
* @num: number of elements * @num: number of elements
* @size: size of each element * @size: size of each element
* @cmp: pointer to comparison function * @cmp_func: pointer to comparison function
* @swap: pointer to swap function or NULL * @swap_func: pointer to swap function or NULL
* *
* This function does a heapsort on the given array. You may provide a * This function does a heapsort on the given array. You may provide a
* swap function optimized to your element type. * swap_func function optimized to your element type.
* *
* Sorting time is O(n log n) both on average and worst-case. While * Sorting time is O(n log n) both on average and worst-case. While
* qsort is about 20% faster on average, it suffers from exploitable * qsort is about 20% faster on average, it suffers from exploitable
@ -45,37 +45,39 @@ static void generic_swap(void *a, void *b, int size)
*/ */
void sort(void *base, size_t num, size_t size, void sort(void *base, size_t num, size_t size,
int (*cmp)(const void *, const void *), int (*cmp_func)(const void *, const void *),
void (*swap)(void *, void *, int size)) void (*swap_func)(void *, void *, int size))
{ {
/* pre-scale counters for performance */ /* pre-scale counters for performance */
int i = (num/2 - 1) * size, n = num * size, c, r; int i = (num/2 - 1) * size, n = num * size, c, r;
if (!swap) if (!swap_func)
swap = (size == 4 ? u32_swap : generic_swap); swap_func = (size == 4 ? u32_swap : generic_swap);
/* heapify */ /* heapify */
for ( ; i >= 0; i -= size) { for ( ; i >= 0; i -= size) {
for (r = i; r * 2 + size < n; r = c) { for (r = i; r * 2 + size < n; r = c) {
c = r * 2 + size; c = r * 2 + size;
if (c < n - size && cmp(base + c, base + c + size) < 0) if (c < n - size &&
cmp_func(base + c, base + c + size) < 0)
c += size; c += size;
if (cmp(base + r, base + c) >= 0) if (cmp_func(base + r, base + c) >= 0)
break; break;
swap(base + r, base + c, size); swap_func(base + r, base + c, size);
} }
} }
/* sort */ /* sort */
for (i = n - size; i > 0; i -= size) { for (i = n - size; i > 0; i -= size) {
swap(base, base + i, size); swap_func(base, base + i, size);
for (r = 0; r * 2 + size < i; r = c) { for (r = 0; r * 2 + size < i; r = c) {
c = r * 2 + size; c = r * 2 + size;
if (c < i - size && cmp(base + c, base + c + size) < 0) if (c < i - size &&
cmp_func(base + c, base + c + size) < 0)
c += size; c += size;
if (cmp(base + r, base + c) >= 0) if (cmp_func(base + r, base + c) >= 0)
break; break;
swap(base + r, base + c, size); swap_func(base + r, base + c, size);
} }
} }
} }