summaryrefslogtreecommitdiff
path: root/ccan/order/test/fancy_cmp.h
blob: b7940377a36536b84f52eb566ddbc5d7fa3731dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef _FANCY_CMP_H
#define _FANCY_CMP_H

struct cmp_info {
	unsigned xcode;
	int offset;
};

struct item {
	unsigned value;
	char *str;
};

static inline int fancy_cmp(const struct item *a, const struct item *b,
			    struct cmp_info *ctx)
{
	unsigned vala = a->value ^ ctx->xcode;
	unsigned valb = b->value ^ ctx->xcode;
	const char *stra, *strb;

	if (vala < valb)
		return -1;
	else if (valb < vala)
		return 1;

	stra = a->str + ctx->offset;
	strb = b->str + ctx->offset;

	return strcmp(stra, strb);
}

static inline int fancy_cmp_noctx(const void *av, const void *bv)
{
	const struct item *a = (const struct item *)av;
	const struct item *b = (const struct item *)bv;
	struct cmp_info ctx_default = {
		.xcode = 0x1234,
		.offset = 3,
	};
	total_order(default_order, struct item, struct cmp_info *) = {
		fancy_cmp, &ctx_default,
	};

	return default_order.cb(a, b, default_order.ctx);
}

#endif /* _FANCY_CMP_H */