1 #ifndef _FANCY_CMP_H
2 #define _FANCY_CMP_H
3
4 struct cmp_info {
5 unsigned xcode;
6 int offset;
7 };
8
9 struct item {
10 unsigned value;
11 const char *str;
12 };
13
fancy_cmp(const struct item * a,const struct item * b,struct cmp_info * ctx)14 static inline int fancy_cmp(const struct item *a, const struct item *b,
15 struct cmp_info *ctx)
16 {
17 unsigned vala = a->value ^ ctx->xcode;
18 unsigned valb = b->value ^ ctx->xcode;
19 const char *stra, *strb;
20
21 if (vala < valb)
22 return -1;
23 else if (valb < vala)
24 return 1;
25
26 stra = a->str + ctx->offset;
27 strb = b->str + ctx->offset;
28
29 return strcmp(stra, strb);
30 }
31
fancy_cmp_noctx(const void * av,const void * bv)32 static inline int fancy_cmp_noctx(const void *av, const void *bv)
33 {
34 const struct item *a = (const struct item *)av;
35 const struct item *b = (const struct item *)bv;
36 struct cmp_info ctx_default = {
37 .xcode = 0x1234,
38 .offset = 3,
39 };
40 total_order(default_order, struct item, struct cmp_info *) = {
41 fancy_cmp, &ctx_default,
42 };
43
44 return default_order.cb(a, b, default_order.ctx);
45 }
46
47 #endif /* _FANCY_CMP_H */
48