Have xxx_T() macros that only add type safety to the function they wrap.
Have XXX() macros that do more magic; in this case, they deduce the
comparison function that is appropriate for the type.
This will allow using the xxx_T() macros in more complex cases, where
the function can't be deduced just from the type.
Signed-off-by: Alejandro Colomar <alx@kernel.org>
#include "search/cmp/cmp.h"
-#define LFIND(T, k, a, n) \
+// lfind_T - linear find type-safe
+#define lfind_T(T, k, a, n, cmp) \
({ \
_Generic(k, T *: 0, const T *: 0); \
_Generic(a, T *: 0, const T *: 0); \
- (T *) lfind_(k, a, n, sizeof(T), CMP(T)); \
+ (T *) lfind_(k, a, n, sizeof(T), cmp); \
})
+#define LFIND(T, ...) lfind_T(T, __VA_ARGS__, CMP(T))
+
inline void *lfind_(const void *k, const void *a, size_t n, size_t ksize,
typeof(int (const void *k, const void *elt)) *cmp);
#include "search/cmp/cmp.h"
-#define LSEARCH(T, k, a, n) \
+// lsearch_T - linear search-and-insert type-safe
+#define lsearch_T(T, k, a, n, cmp) \
({ \
_Generic(k, T *: 0, const T *: 0); \
_Generic(a, T *: 0); \
- (T *) lsearch(k, a, n, sizeof(T), CMP(T)); \
+ (T *) lsearch(k, a, n, sizeof(T), cmp); \
})
+#define LSEARCH(T, ...) lsearch_T(T, __VA_ARGS__, CMP(T))
+
#endif // include guard
#include "search/cmp/cmp.h"
-#define QSORT(T, a, n) do \
+// qsort_T - sort type-safe
+#define qsort_T(T, a, n, cmp) do \
{ \
_Generic(a, T *: 0); \
- qsort(a, n, sizeof(T), CMP(T)); \
+ qsort(a, n, sizeof(T), cmp); \
} while (0)
+#define QSORT(T, ...) qsort_T(T, __VA_ARGS__, CMP(T))
+
#endif // include guard