summaryrefslogtreecommitdiff
path: root/vcs-svn/trp.h
blob: c32b9184e9b8420e4d4e2904806bd2f14814132b (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * C macro implementation of treaps.
 *
 * Usage:
 *   #include <stdint.h>
 *   #include "trp.h"
 *   trp_gen(...)
 *
 * Licensed under a two-clause BSD-style license.
 * See LICENSE for details.
 */

#ifndef TRP_H_
#define TRP_H_

#define MAYBE_UNUSED __attribute__((__unused__))

/* Node structure. */
struct trp_node {
	uint32_t trpn_left;
	uint32_t trpn_right;
};

/* Root structure. */
struct trp_root {
	uint32_t trp_root;
};

/* Pointer/Offset conversion. */
#define trpn_pointer(a_base, a_offset) (a_base##_pointer(a_offset))
#define trpn_offset(a_base, a_pointer) (a_base##_offset(a_pointer))
#define trpn_modify(a_base, a_offset) \
	do { \
		if ((a_offset) < a_base##_pool.committed) { \
			uint32_t old_offset = (a_offset);\
			(a_offset) = a_base##_alloc(1); \
			*trpn_pointer(a_base, a_offset) = \
				*trpn_pointer(a_base, old_offset); \
		} \
	} while (0)

/* Left accessors. */
#define trp_left_get(a_base, a_field, a_node) \
	(trpn_pointer(a_base, a_node)->a_field.trpn_left)
#define trp_left_set(a_base, a_field, a_node, a_left) \
	do { \
		trpn_modify(a_base, a_node); \
		trp_left_get(a_base, a_field, a_node) = (a_left); \
	} while (0)

/* Right accessors. */
#define trp_right_get(a_base, a_field, a_node) \
	(trpn_pointer(a_base, a_node)->a_field.trpn_right)
#define trp_right_set(a_base, a_field, a_node, a_right) \
	do { \
		trpn_modify(a_base, a_node); \
		trp_right_get(a_base, a_field, a_node) = (a_right); \
	} while (0)

/*
 * Fibonacci hash function.
 * The multiplier is the nearest prime to (2^32 times (√5 - 1)/2).
 * See Knuth §6.4: volume 3, 3rd ed, p518.
 */
#define trpn_hash(a_node) (uint32_t) (2654435761u * (a_node))

/* Priority accessors. */
#define trp_prio_get(a_node) trpn_hash(a_node)

/* Node initializer. */
#define trp_node_new(a_base, a_field, a_node) \
	do { \
		trp_left_set(a_base, a_field, (a_node), ~0); \
		trp_right_set(a_base, a_field, (a_node), ~0); \
	} while (0)

/* Internal utility macros. */
#define trpn_first(a_base, a_field, a_root, r_node) \
	do { \
		(r_node) = (a_root); \
		if ((r_node) == ~0) \
			return NULL; \
		while (~trp_left_get(a_base, a_field, (r_node))) \
			(r_node) = trp_left_get(a_base, a_field, (r_node)); \
	} while (0)

#define trpn_rotate_left(a_base, a_field, a_node, r_node) \
	do { \
		(r_node) = trp_right_get(a_base, a_field, (a_node)); \
		trp_right_set(a_base, a_field, (a_node), \
			trp_left_get(a_base, a_field, (r_node))); \
		trp_left_set(a_base, a_field, (r_node), (a_node)); \
	} while (0)

#define trpn_rotate_right(a_base, a_field, a_node, r_node) \
	do { \
		(r_node) = trp_left_get(a_base, a_field, (a_node)); \
		trp_left_set(a_base, a_field, (a_node), \
			trp_right_get(a_base, a_field, (r_node))); \
		trp_right_set(a_base, a_field, (r_node), (a_node)); \
	} while (0)

#define trp_gen(a_attr, a_pre, a_type, a_field, a_base, a_cmp) \
a_attr a_type MAYBE_UNUSED *a_pre##first(struct trp_root *treap) \
{ \
	uint32_t ret; \
	trpn_first(a_base, a_field, treap->trp_root, ret); \
	return trpn_pointer(a_base, ret); \
} \
a_attr a_type MAYBE_UNUSED *a_pre##next(struct trp_root *treap, a_type *node) \
{ \
	uint32_t ret; \
	uint32_t offset = trpn_offset(a_base, node); \
	if (~trp_right_get(a_base, a_field, offset)) { \
		trpn_first(a_base, a_field, \
			trp_right_get(a_base, a_field, offset), ret); \
	} else { \
		uint32_t tnode = treap->trp_root; \
		ret = ~0; \
		while (1) { \
			int cmp = (a_cmp)(trpn_pointer(a_base, offset), \
				trpn_pointer(a_base, tnode)); \
			if (cmp < 0) { \
				ret = tnode; \
				tnode = trp_left_get(a_base, a_field, tnode); \
			} else if (cmp > 0) { \
				tnode = trp_right_get(a_base, a_field, tnode); \
			} else { \
				break; \
			} \
		} \
	} \
	return trpn_pointer(a_base, ret); \
} \
a_attr a_type MAYBE_UNUSED *a_pre##search(struct trp_root *treap, a_type *key) \
{ \
	int cmp; \
	uint32_t ret = treap->trp_root; \
	while (~ret && (cmp = (a_cmp)(key, trpn_pointer(a_base, ret)))) { \
		if (cmp < 0) { \
			ret = trp_left_get(a_base, a_field, ret); \
		} else { \
			ret = trp_right_get(a_base, a_field, ret); \
		} \
	} \
	return trpn_pointer(a_base, ret); \
} \
a_attr a_type MAYBE_UNUSED *a_pre##nsearch(struct trp_root *treap, a_type *key) \
{ \
	int cmp; \
	uint32_t ret = treap->trp_root; \
	while (~ret && (cmp = (a_cmp)(key, trpn_pointer(a_base, ret)))) { \
		if (cmp < 0) { \
			if (!~trp_left_get(a_base, a_field, ret)) \
				break; \
			ret = trp_left_get(a_base, a_field, ret); \
		} else { \
			ret = trp_right_get(a_base, a_field, ret); \
		} \
	} \
	return trpn_pointer(a_base, ret); \
} \
a_attr uint32_t MAYBE_UNUSED a_pre##insert_recurse(uint32_t cur_node, uint32_t ins_node) \
{ \
	if (cur_node == ~0) { \
		return ins_node; \
	} else { \
		uint32_t ret; \
		int cmp = (a_cmp)(trpn_pointer(a_base, ins_node), \
					trpn_pointer(a_base, cur_node)); \
		if (cmp < 0) { \
			uint32_t left = a_pre##insert_recurse( \
				trp_left_get(a_base, a_field, cur_node), ins_node); \
			trp_left_set(a_base, a_field, cur_node, left); \
			if (trp_prio_get(left) < trp_prio_get(cur_node)) \
				trpn_rotate_right(a_base, a_field, cur_node, ret); \
			else \
				ret = cur_node; \
		} else { \
			uint32_t right = a_pre##insert_recurse( \
				trp_right_get(a_base, a_field, cur_node), ins_node); \
			trp_right_set(a_base, a_field, cur_node, right); \
			if (trp_prio_get(right) < trp_prio_get(cur_node)) \
				trpn_rotate_left(a_base, a_field, cur_node, ret); \
			else \
				ret = cur_node; \
		} \
		return ret; \
	} \
} \
a_attr a_type *MAYBE_UNUSED a_pre##insert(struct trp_root *treap, a_type *node) \
{ \
	uint32_t offset = trpn_offset(a_base, node); \
	trp_node_new(a_base, a_field, offset); \
	treap->trp_root = a_pre##insert_recurse(treap->trp_root, offset); \
	return trpn_pointer(a_base, offset); \
} \
a_attr uint32_t MAYBE_UNUSED a_pre##remove_recurse(uint32_t cur_node, uint32_t rem_node) \
{ \
	int cmp = a_cmp(trpn_pointer(a_base, rem_node), \
			trpn_pointer(a_base, cur_node)); \
	if (cmp == 0) { \
		uint32_t ret; \
		uint32_t left = trp_left_get(a_base, a_field, cur_node); \
		uint32_t right = trp_right_get(a_base, a_field, cur_node); \
		if (left == ~0) { \
			if (right == ~0) \
				return ~0; \
		} else if (right == ~0 || trp_prio_get(left) < trp_prio_get(right)) { \
			trpn_rotate_right(a_base, a_field, cur_node, ret); \
			right = a_pre##remove_recurse(cur_node, rem_node); \
			trp_right_set(a_base, a_field, ret, right); \
			return ret; \
		} \
		trpn_rotate_left(a_base, a_field, cur_node, ret); \
		left = a_pre##remove_recurse(cur_node, rem_node); \
		trp_left_set(a_base, a_field, ret, left); \
		return ret; \
	} else if (cmp < 0) { \
		uint32_t left = a_pre##remove_recurse( \
			trp_left_get(a_base, a_field, cur_node), rem_node); \
		trp_left_set(a_base, a_field, cur_node, left); \
		return cur_node; \
	} else { \
		uint32_t right = a_pre##remove_recurse( \
			trp_right_get(a_base, a_field, cur_node), rem_node); \
		trp_right_set(a_base, a_field, cur_node, right); \
		return cur_node; \
	} \
} \
a_attr void MAYBE_UNUSED a_pre##remove(struct trp_root *treap, a_type *node) \
{ \
	treap->trp_root = a_pre##remove_recurse(treap->trp_root, \
		trpn_offset(a_base, node)); \
} \

#endif