|
Revision 5646, 1.0 KB
(checked in by westram, 3 years ago)
|
- replaced GB_mergesort() and GBT_quicksort() by GB_sort()
- implemented GB_sort() using qsort(). current qsort is implemented as intronsort, which has mean AND worst case complexity O(N*log N), w/o the memory complexity of mergesort.
- fixed constness of compare function type (hash and array)
- added and using standard comparators (GB_string_comparator, GBS_HCF_sortedByKey)
- fixed/reimplemented several custom comparators
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 | #include <stdlib.h> |
|---|
| 2 | #include <string.h> |
|---|
| 3 | |
|---|
| 4 | #include "arbdb.h" |
|---|
| 5 | |
|---|
| 6 | struct comparator { |
|---|
| 7 | gb_compare_function compare; |
|---|
| 8 | char *client_data; |
|---|
| 9 | }; |
|---|
| 10 | |
|---|
| 11 | static struct comparator Compare; // current compare function + client data |
|---|
| 12 | |
|---|
| 13 | static int qsort_compare(const void *v1, const void *v2) { |
|---|
| 14 | return Compare.compare(*(void**)v1, *(void**)v2, Compare.client_data); |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | void GB_sort(void **array, size_t first, size_t behind_last, gb_compare_function compare, void *client_data) { |
|---|
| 18 | /* sort 'array' of pointers from 'first' to last element |
|---|
| 19 | * (specified by following element 'behind_last') |
|---|
| 20 | * 'compare' is a compare function, with a strcmp-like result value |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | Compare.compare = compare; |
|---|
| 24 | Compare.client_data = client_data; |
|---|
| 25 | |
|---|
| 26 | qsort(array, behind_last-first, sizeof(*array), qsort_compare); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | /* -------------------------- */ |
|---|
| 30 | /* some comparators */ |
|---|
| 31 | |
|---|
| 32 | int GB_string_comparator(const void *v0, const void *v1, void *unused) { |
|---|
| 33 | GBUSE(unused); |
|---|
| 34 | return strcmp((const char *)v0, (const char *)v1); |
|---|
| 35 | } |
|---|