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