1
0

Order.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Order_H
  16. #define Order_H
  17. #include "util/Linker.h"
  18. Linker_require("util/Order.c")
  19. #include <stddef.h>
  20. typedef int (* Order_Comparator)(const void* a, const void* b);
  21. void Order_qsort(void* base, size_t num, size_t size, Order_Comparator compare);
  22. #endif
  23. #ifdef Order_TYPE
  24. #ifndef Order_NAME
  25. #error must give this ordering a name by defining Order_NAME
  26. #endif
  27. #ifndef Order_COMPARE
  28. #error must define a comparitor
  29. #endif
  30. #define Order_FUNCTION(name) Order_GLUE(Order_GLUE(Order_GLUE(Order_, Order_NAME),_), name)
  31. #define Order_GLUE(x, y) Order_GLUE2(x, y)
  32. #define Order_GLUE2(x, y) x ## y
  33. static inline int Order_COMPARE(const Order_TYPE* one, const Order_TYPE* two);
  34. static inline void Order_FUNCTION(qsort)(Order_TYPE* array, int size)
  35. {
  36. Order_qsort(array, size, sizeof(Order_TYPE), (Order_Comparator) Order_COMPARE);
  37. }
  38. #undef Order_TYPE
  39. #undef Order_NAME
  40. #undef Order_COMPARE
  41. #undef Order_FUNCTION
  42. #undef Order_GLUE
  43. #undef Order_GLUE2
  44. #endif