gskqsortmacro

gskqsortmacro — inline qsort macro

Synopsis




#define     GSK_QSORT_STACK_MAX_SIZE
#define     GSK_INSERTION_SORT_THRESHOLD
#define     GSK_QSORT                       (array, type, n_elements, compare)
#define     GSK_QSORT_DEBUG                 (array, type, n_elements, compare)
#define     GSK_QSORT_FULL                  (array, type, n_elements, compare, isort_threshold, stack_size, ss_assertion)
#define     GSK_QSELECT_FULL                (array, type, n_elements, n_select, compare, isort_threshold, stack_size, ss_assertion)
#define     GSK_QSORT_ASSERT_STACK_SIZE     (stack_alloced)
#define     GSK_INSERTION_SORT              (array, type, length, compare)
#define     GSK_QSORT_SIMPLE_COMPARATOR     (a,b,compare_rv)

Description

This is an implementation of qsort that avoids recursion and uses a fixed-length stack. As such, it is suitable for a macro implementation.

Please note that usually qsort() or g_qsort_with_data() is nicer to use than the macro version: it will compile to less code, for example. However, the macro-based version is often faster, and can be more convenient to use than the other routines. For example, since the macro is lexically inlined, the comparator can use local variables.

Here is an example: this qsorts an array of integers, considering only the least n_bits bits:


 static void
 qsort_least_significant_bits (guint  array_len,
                               guint *array,
			       guint  n_bits)
 {
   guint mask = (1 << n_bits) - 1;
<link linkend="define"><type>define</type></link> COMPARE(a,b,rv)       \
  { guint tmp_a = mask & a;   \
    guint tmp_b = mask & b;   \
    rv = (tmp_a < tmp_b) ? -1 \
       : (tmp_a > tmp_b) ? +1 \
       0; }
  GSK_QSORT (array, guint, array_len, COMPARE);
<link linkend="undef"><type>undef</type></link> COMPARE
 }

Details

GSK_QSORT_STACK_MAX_SIZE

#define     GSK_QSORT_STACK_MAX_SIZE


GSK_INSERTION_SORT_THRESHOLD

#define GSK_INSERTION_SORT_THRESHOLD	4


GSK_QSORT()

#define     GSK_QSORT(array, type, n_elements, compare)

Sort an array.

array : The array of values to sort.
type : The type of an element of the array.
n_elements : The number of elements in array to sort.
compare : A comparator macro.

GSK_QSORT_DEBUG()

#define     GSK_QSORT_DEBUG(array, type, n_elements, compare)

array :
type :
n_elements :
compare :

GSK_QSORT_FULL()

#define     GSK_QSORT_FULL(array, type, n_elements, compare, isort_threshold, stack_size, ss_assertion)

array :
type :
n_elements :
compare :
isort_threshold :
stack_size :
ss_assertion :

GSK_QSELECT_FULL()

#define     GSK_QSELECT_FULL(array, type, n_elements, n_select, compare, isort_threshold, stack_size, ss_assertion)

array :
type :
n_elements :
n_select :
compare :
isort_threshold :
stack_size :
ss_assertion :

GSK_QSORT_ASSERT_STACK_SIZE()

#define     GSK_QSORT_ASSERT_STACK_SIZE(stack_alloced)

stack_alloced :

GSK_INSERTION_SORT()

#define     GSK_INSERTION_SORT(array, type, length, compare)

array :
type :
length :
compare :

GSK_QSORT_SIMPLE_COMPARATOR()

#define     GSK_QSORT_SIMPLE_COMPARATOR(a,b,compare_rv)

a :
b :
compare_rv :

See Also

The gskrbtreemacros, which can maintain a set in sorted order, so that the set can be walked in O(N) time, and each insert, delete or lookup is O(log N).