asort.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. * asort - sort an array of elements
  9. * @base: pointer to data to sort
  10. * @num: number of elements
  11. * @cmp: pointer to comparison function
  12. * @ctx: a context pointer for the cmp function
  13. *
  14. * This function does a sort on the given array. The resulting array
  15. * will be in ascending sorted order by the provided comparison function.
  16. *
  17. * The @cmp function should exactly match the type of the @base and
  18. * @ctx arguments. Otherwise it can take three const void *.
  19. */
  20. #define asort(base, num, cmp, ctx) \
  21. _asort((base), (num), sizeof(*(base)), \
  22. typesafe_cb_cast(int (*)(const void *, const void *, void *), \
  23. int (*)(const __typeof__(*(base)) *, \
  24. const __typeof__(*(base)) *, \
  25. __typeof__(ctx)), \
  26. (cmp)), \
  27. (ctx))
  28. #if HAVE_QSORT_R_PRIVATE_LAST
  29. #define _asort(b, n, s, cmp, ctx) qsort_r(b, n, s, cmp, ctx)
  30. #else
  31. void _asort(void *base, size_t nmemb, size_t size,
  32. int(*compar)(const void *, const void *, void *),
  33. void *ctx);
  34. #endif
  35. #endif /* CCAN_ASORT_H */