asort.h 1.0 KB

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