aboutsummaryrefslogtreecommitdiff
path: root/common/sp_tree.c
blob: dbd0e8b07bf849376dc69c90225f1dda57aa9115 (plain) (blame)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

/**
 * @file sp_tree.c
 * Implementation of my sp_trees. An sp_tree is a mix of rb-trees and avl-trees,
 * with slightly faster insertion but worse tree depth on average.
 *
 * See https://github.com/Kimplul/sptree
 *
 * \todo Document sp_tree algorithm better.
 */

#include <apos/sp_tree.h>

/**
 * Basic BST left turn.
 * Drop node \c n down to the left side of the right node, letting it take
 * the place of \c n.
 *
 * @param n Node to turn left.
 */
static void __sp_turn_left(struct sp_node *n)
{
	struct sp_node *l = sp_left(n);
	struct sp_node *p = sp_paren(n);

	sp_paren(l) = sp_paren(n);
	sp_left(n) = sp_right(l);
	sp_paren(n) = l;
	sp_right(l) = n;

	if (p && sp_left(p) == n)
		sp_left(p) = l;
	else if (p)
		sp_right(p) = l;

	if (sp_left(n))
		sp_lparen(n) = n;
}

/**
 * Basic BST right turn.
 * Drop node \c n down to the right side of the left node, letting it take the
 * place of \c n.
 *
 * Does not check if right node exists.
 *
 * @param n Node to turn right.
 */
static void __sp_turn_right(struct sp_node *n)
{
	struct sp_node *r = sp_right(n);
	struct sp_node *p = sp_paren(n);

	sp_paren(r) = sp_paren(n);
	sp_right(n) = sp_left(r);
	sp_paren(n) = r;
	sp_left(r) = n;

	if (p && sp_left(p) == n)
		sp_left(p) = r;
	else if (p)
		sp_right(p) = r;

	if (sp_right(n))
		sp_rparen(n) = n;
}

/**
 * Calculate approximate balance of node, based on height hints.
 *
 * @param n Node to calculate balance for.
 * @return Balance of node.
 */
static int_fast16_t __sp_balance(struct sp_node *n)
{
	int_fast16_t l = 0;
	int_fast16_t r = 0;

	if (sp_left(n))
		l = sp_left(n)->hint + 1;

	if (sp_right(n))
		r = sp_right(n)->hint + 1;

	return l - r;
}

/**
 * Get highest hint.
 *
 * @param n Node to calculate highest hint for.
 * @return Highest hint.
 */
static int_fast16_t __sp_max_hint(struct sp_node *n)
{
	int_fast16_t l = 0;
	int_fast16_t r = 0;

	if (sp_left(n))
		l = sp_left(n)->hint + 1;

	if (sp_right(n))
		r = sp_right(n)->hint + 1;

	if (l > r)
		return l;
	else
		return r;
}

/**
 * Balance tree, moving up from c n.
 *
 * @param root Root of tree.
 * @param n Node to start balancing operation from.
 */
static void __sp_update(struct sp_node **root, struct sp_node *n)
{
	while (n) {
		int b = __sp_balance(n);
		int prev_hint = n->hint;
		struct sp_node *p = sp_paren(n);

		if (b < -1) {
			/* leaning to the right */
			if (n == *root)
				*root = sp_right(n);

			__sp_turn_right(n);
		}

		else if (b > 1) {
			/* leaning to the left */
			if (n == *root)
				*root = sp_left(n);

			__sp_turn_left(n);
		}

		n->hint = __sp_max_hint(n);
		if (n->hint == 0 || n->hint != prev_hint)
			n = p;
		else
			return;
	}
}

void sp_insert(struct sp_node **root, struct sp_node *p, struct sp_node *n,
               enum sp_dir d)
{
	if (!*root) {
		*root = n;
		return;
	}

	if (d == LEFT)
		sp_left(p) = n;
	else
		sp_right(p) = n;

	sp_paren(n) = p;
	__sp_update(root, n);
}

/**
 * Replace node \c n with \c l, pulling of the righthand side of \n.
 *
 * @param n Node to replace.
 * @param r Node to replace with.
 */
static void __sp_replace_right(struct sp_node *n, struct sp_node *r)
{
	struct sp_node *p = sp_paren(n);
	struct sp_node *rp = sp_paren(r);

	if (sp_left(rp) == r) {
		sp_left(rp) = sp_right(r);
		if (sp_right(r))
			sp_rparen(r) = rp;
	}

	if (sp_paren(rp) == n)
		sp_paren(rp) = r;

	sp_paren(r) = p;
	sp_left(r) = sp_left(n);

	if (sp_right(n) != r) {
		sp_right(r) = sp_right(n);
		sp_rparen(n) = r;
	}

	if (p && sp_left(p) == n)
		sp_left(p) = r;
	else if (p)
		sp_right(p) = r;

	if (sp_left(n))
		sp_lparen(n) = r;
}

/**
 * Replace node \c n with node \c l, pulling up the lefthand side of \c n.
 *
 * @param n Node to replace.
 * @param l Node to replace with.
 */
static void __sp_replace_left(struct sp_node *n, struct sp_node *l)
{
	struct sp_node *p = sp_paren(n);
	struct sp_node *lp = sp_paren(l);

	if (sp_right(lp) == l) {
		sp_right(lp) = sp_left(l);
		if (sp_left(l))
			sp_lparen(l) = lp;
	}

	if (sp_paren(lp) == n)
		sp_paren(lp) = l;

	sp_paren(l) = p;
	sp_right(l) = sp_right(n);

	if (sp_left(n) != l) {
		sp_left(l) = sp_left(n);
		sp_lparen(n) = l;
	}

	if (p && sp_left(p) == n)
		sp_left(p) = l;
	else if (p)
		sp_right(p) = l;

	if (sp_right(n))
		sp_rparen(n) = l;
}

void sp_remove(struct sp_node **root, struct sp_node *del)
{
	if (sp_right(del)) {
		struct sp_node *least = sp_first(sp_right(del));

		if (del == *root)
			*root = least;

		__sp_replace_right(del, least);
		__sp_update(root, sp_right(least));
		return;
	}

	if (sp_left(del)) {
		struct sp_node *most = sp_last(sp_left(del));

		if (del == *root)
			*root = most;

		__sp_replace_left(del, most);
		__sp_update(root, sp_left(most));
		return;
	}

	if (del == *root) {
		*root = 0;
		return;
	}

	/* empty node */
	struct sp_node *paren = sp_paren(del);

	if (sp_left(paren) == del)
		sp_left(paren) = 0;
	else
		sp_right(paren) = 0;

	__sp_update(root, paren);
}

struct sp_node *sp_first(struct sp_node *n)
{
	if (!sp_left(n))
		return n;

	return sp_first(sp_left(n));
}

struct sp_node *sp_last(struct sp_node *n)
{
	if (!sp_right(n))
		return n;

	return sp_last(sp_right(n));
}