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 { |
---|
17 | gb_compare_function compare; |
---|
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 | |
---|