Debian Codegraph @

src/basic/sort-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "basic-forward.h"
5
6 void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
7 comparison_userdata_fn_t compar, void *arg);
8
9 #define typesafe_bsearch_r(k, b, n, func, userdata) \
10 ({ \
11 const typeof((b)[0]) *_k = k; \
12 int (*_func_)(const typeof((b)[0])*, const typeof((b)[0])*, typeof(userdata)) = func; \
13 (typeof((b)[0])*) xbsearch_r((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_userdata_fn_t) _func_, userdata); \
14 })
15
16 void* bsearch_safe_internal(const void *key, const void *base, size_t nmemb, size_t size, comparison_fn_t compar);
17 #define bsearch_safe(key, base, nmemb, size, compar) \
18 const_generic((base), bsearch_safe_internal(key, base, nmemb, size, compar))
19
20 #define typesafe_bsearch(k, b, n, func) \
21 ({ \
22 const typeof((b)[0]) *_k = k; \
23 int (*_func_)(const typeof((b)[0])*, const typeof((b)[0])*) = func; \
24 (typeof((b)[0])*) bsearch_safe((const void*) _k, (b), (n), sizeof((b)[0]), (comparison_fn_t) _func_); \
25 })
26
27 void qsort_safe(void *base, size_t nmemb, size_t size, comparison_fn_t compar);
28
29 /* A wrapper around the above, but that adds typesafety: the element size is automatically derived from the type and so
30 * is the prototype for the comparison function */
31 #define typesafe_qsort(p, n, func) \
32 ({ \
33 int (*_func_)(const typeof((p)[0])*, const typeof((p)[0])*) = func; \
34 qsort_safe((p), (n), sizeof((p)[0]), (comparison_fn_t) _func_); \
35 })
36
37 void qsort_r_safe(void *base, size_t nmemb, size_t size, comparison_userdata_fn_t compar, void *userdata);
38
39 #define typesafe_qsort_r(p, n, func, userdata) \
40 ({ \
41 int (*_func_)(const typeof((p)[0])*, const typeof((p)[0])*, typeof(userdata)) = func; \
42 qsort_r_safe((p), (n), sizeof((p)[0]), (comparison_userdata_fn_t) _func_, userdata); \
43 })
44
45 int cmp_int(const int *a, const int *b);
46 int cmp_uint16(const uint16_t *a, const uint16_t *b);
47 int cmp_unsigned(const unsigned *a, const unsigned *b);