]> git.ozlabs.org Git - ccan/blob - ccan/asort/asort.h
1139238e197d82c269ba5ac972295c6d4a22d5d1
[ccan] / ccan / asort / asort.h
1 #ifndef CCAN_ASORT_H
2 #define CCAN_ASORT_H
3 #include <ccan/typesafe_cb/typesafe_cb.h>
4 #include <stdlib.h>
5
6 /**
7  * asort - sort an array of elements
8  * @base: pointer to data to sort
9  * @num: number of elements
10  * @cmp: pointer to comparison function
11  * @ctx: a context pointer for the cmp function
12  *
13  * This function does a sort on the given array.  The resulting array
14  * will be in ascending sorted order by the provided comparison function.
15  *
16  * The @cmp function should exactly match the type of the @base and
17  * @ctx arguments.  Otherwise it can take three const void *.
18  */
19 #if HAVE_TYPEOF
20 #define asort(base, num, cmp, ctx)                                      \
21 _asort((base), (num), sizeof(*(base)),                                  \
22        cast_if_type((cmp),                                              \
23                     int (*)(const __typeof__(*(base)) *,                \
24                             const __typeof__(*(base)) *,                \
25                             __typeof__(ctx)),                           \
26                     int (*)(const void *, const void *, const void *)), (ctx))
27 #else
28 #define asort(base, num, cmp, ctx)                              \
29         _asort((base), (num), sizeof(*(base)),                  \
30                (int (*)(const void *, const void *, const void *))(cmp), ctx)
31 #endif
32
33 void _asort(void *base, size_t nmemb, size_t size,
34             int(*compar)(const void *, const void *, const void *),
35             const void *ctx);
36
37 #endif /* CCAN_ASORT_H */