ARB
arb_sort.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : arb_sort.cxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #include "arb_sort.h"
12 #include <cstring>
13 #include "arb_assert.h"
14 
15 
16 struct comparator {
18  void *client_data;
19 };
20 
21 static comparator Compare; // current compare function + client data
22 
23 static int qsort_compare(const void *v1, const void *v2) {
24  return Compare.compare(*(void**)v1, *(void**)v2, Compare.client_data);
25 }
26 
27 void GB_sort(void **array, size_t first, size_t behind_last, gb_compare_function compare, void *client_data) {
28  /* sort 'array' of pointers from 'first' to last element
29  * (specified by following element 'behind_last')
30  * 'compare' is a compare function, with a strcmp-like result value
31  */
32 
33  if (!array) {
34  arb_assert(first == behind_last); // accept empty NULp-array
35  return;
36  }
37 
38  Compare.compare = compare;
39  Compare.client_data = client_data;
40 
41  qsort(array, behind_last-first, sizeof(*array), qsort_compare);
42 }
43 
44 // --------------------------
45 // some comparators
46 
47 int GB_string_comparator(const void *v0, const void *v1, void */*unused*/) {
48  return strcmp((const char *)v0, (const char *)v1);
49 }
50 
gb_compare_function compare
Definition: arb_sort.cxx:17
#define arb_assert(cond)
Definition: arb_assert.h:245
void GB_sort(void **array, size_t first, size_t behind_last, gb_compare_function compare, void *client_data)
Definition: arb_sort.cxx:27
int GB_string_comparator(const void *v0, const void *v1, void *)
Definition: arb_sort.cxx:47
int(* gb_compare_function)(const void *p1, const void *p2, void *client_data)
Definition: arb_sort.h:19
static int qsort_compare(const void *v1, const void *v2)
Definition: arb_sort.cxx:23
static comparator Compare
Definition: arb_sort.cxx:21
void * client_data
Definition: arb_sort.cxx:18