blob: 87c2f980750006ae5aea781705d04d0e119f4f08 [file] [log] [blame]
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +01001/*
2 * Elastic Binary Trees - macros and structures for operations on 32bit nodes.
3 * (C) 2002-2007 - Willy Tarreau <w@1wt.eu>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include "ebtree.h"
21
22
23/* Return the structure of type <type> whose member <member> points to <ptr> */
24#define eb32_entry(ptr, type, member) container_of(ptr, type, member)
25
26#define EB32_ROOT EB_ROOT
27#define EB32_TREE_HEAD EB_TREE_HEAD
28
29/* These types may sometimes already be defined */
30typedef unsigned int u32;
31typedef signed int s32;
32
33/* This structure carries a node, a leaf, and a key. It must start with the
34 * eb_node so that it can be cast into an eb_node. We could also have put some
35 * sort of transparent union here to reduce the indirection level, but the fact
36 * is, the end user is not meant to manipulate internals, so this is pointless.
37 */
38struct eb32_node {
39 struct eb_node node; /* the tree node, must be at the beginning */
40 u32 key;
41};
42
43/*
44 * Exported functions and macros.
45 * Many of them are always inlined because they are extremely small, and
46 * are generally called at most once or twice in a program.
47 */
48
49/* Return leftmost node in the tree, or NULL if none */
50static inline struct eb32_node *eb32_first(struct eb_root *root)
51{
52 return eb32_entry(eb_first(root), struct eb32_node, node);
53}
54
55/* Return rightmost node in the tree, or NULL if none */
56static inline struct eb32_node *eb32_last(struct eb_root *root)
57{
58 return eb32_entry(eb_last(root), struct eb32_node, node);
59}
60
61/* Return next node in the tree, or NULL if none */
62static inline struct eb32_node *eb32_next(struct eb32_node *eb32)
63{
64 return eb32_entry(eb_next(&eb32->node), struct eb32_node, node);
65}
66
67/* Return previous node in the tree, or NULL if none */
68static inline struct eb32_node *eb32_prev(struct eb32_node *eb32)
69{
70 return eb32_entry(eb_prev(&eb32->node), struct eb32_node, node);
71}
72
73/* Return next node in the tree, skipping duplicates, or NULL if none */
74static inline struct eb32_node *eb32_next_unique(struct eb32_node *eb32)
75{
76 return eb32_entry(eb_next_unique(&eb32->node), struct eb32_node, node);
77}
78
79/* Return previous node in the tree, skipping duplicates, or NULL if none */
80static inline struct eb32_node *eb32_prev_unique(struct eb32_node *eb32)
81{
82 return eb32_entry(eb_prev_unique(&eb32->node), struct eb32_node, node);
83}
84
85/* Delete node from the tree if it was linked in. Mark the node unused. Note
86 * that this function relies on a non-inlined generic function: eb_delete.
87 */
88static inline void eb32_delete(struct eb32_node *eb32)
89{
90 eb_delete(&eb32->node);
91}
92
93/*
94 * The following functions are not inlined by default. They are declared
95 * in eb32tree.c, which simply relies on their inline version.
96 */
97REGPRM2 struct eb32_node *eb32_lookup(struct eb_root *root, u32 x);
98REGPRM2 struct eb32_node *eb32i_lookup(struct eb_root *root, s32 x);
99REGPRM2 struct eb32_node *eb32_insert(struct eb_root *root, struct eb32_node *new);
100REGPRM2 struct eb32_node *eb32i_insert(struct eb_root *root, struct eb32_node *new);
101
102/*
103 * The following functions are less likely to be used directly, because their
104 * code is larger. The non-inlined version is preferred.
105 */
106
107/* Delete node from the tree if it was linked in. Mark the node unused. */
108static inline void __eb32_delete(struct eb32_node *eb32)
109{
110 __eb_delete(&eb32->node);
111}
112
113/*
114 * Find the first occurence of a key in the tree <root>. If none can be
115 * found, return NULL.
116 */
117static inline struct eb32_node *__eb32_lookup(struct eb_root *root, u32 x)
118{
119 struct eb32_node *node;
120 eb_troot_t *troot;
121
122 troot = root->b[EB_LEFT];
123 if (unlikely(troot == NULL))
124 return NULL;
125
126 while (1) {
127 if ((eb_gettag(troot) == EB_LEAF)) {
128 node = container_of(eb_untag(troot, EB_LEAF),
129 struct eb32_node, node.branches);
130 if (node->key == x)
131 return node;
132 else
133 return NULL;
134 }
135 node = container_of(eb_untag(troot, EB_NODE),
136 struct eb32_node, node.branches);
137
138 if (x == node->key) {
139 /* Either we found the node which holds the key, or
140 * we have a dup tree. In the later case, we have to
141 * walk it down left to get the first entry.
142 */
143 if (node->node.bit < 0) {
144 troot = node->node.branches.b[EB_LEFT];
145 while (eb_gettag(troot) != EB_LEAF)
146 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
147 node = container_of(eb_untag(troot, EB_LEAF),
148 struct eb32_node, node.branches);
149 }
150 return node;
151 }
152
153 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
154 }
155}
156
157/*
158 * Find the first occurence of a signed key in the tree <root>. If none can
159 * be found, return NULL.
160 */
161static inline struct eb32_node *__eb32i_lookup(struct eb_root *root, s32 x)
162{
163 struct eb32_node *node;
164 eb_troot_t *troot;
165 u32 key = x ^ 0x80000000;
166
167 troot = root->b[EB_LEFT];
168 if (unlikely(troot == NULL))
169 return NULL;
170
171 while (1) {
172 if ((eb_gettag(troot) == EB_LEAF)) {
173 node = container_of(eb_untag(troot, EB_LEAF),
174 struct eb32_node, node.branches);
175 if (node->key == x)
176 return node;
177 else
178 return NULL;
179 }
180 node = container_of(eb_untag(troot, EB_NODE),
181 struct eb32_node, node.branches);
182
183 if (x == node->key) {
184 /* Either we found the node which holds the key, or
185 * we have a dup tree. In the later case, we have to
186 * walk it down left to get the first entry.
187 */
188 if (node->node.bit < 0) {
189 troot = node->node.branches.b[EB_LEFT];
190 while (eb_gettag(troot) != EB_LEAF)
191 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
192 node = container_of(eb_untag(troot, EB_LEAF),
193 struct eb32_node, node.branches);
194 }
195 return node;
196 }
197
198 troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
199 }
200}
201
202/* Insert eb32_node <new> into subtree starting at node root <root>.
203 * Only new->key needs be set with the key. The eb32_node is returned.
204 */
205static inline struct eb32_node *
206__eb32_insert(struct eb_root *root, struct eb32_node *new) {
207 struct eb32_node *old;
208 unsigned int side;
209 eb_troot_t *troot;
210 u32 newkey; /* caching the key saves approximately one cycle */
211
212 side = EB_LEFT;
213 troot = root->b[EB_LEFT];
214 if (unlikely(troot == NULL)) {
215 /* Tree is empty, insert the leaf part below the left branch */
216 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
217 new->node.leaf_p = eb_dotag(root, EB_LEFT);
218 new->node.node_p = NULL; /* node part unused */
219 return new;
220 }
221
222 /* The tree descent is fairly easy :
223 * - first, check if we have reached a leaf node
224 * - second, check if we have gone too far
225 * - third, reiterate
226 * Everywhere, we use <new> for the node node we are inserting, <root>
227 * for the node we attach it to, and <old> for the node we are
228 * displacing below <new>. <troot> will always point to the future node
229 * (tagged with its type). <side> carries the side the node <new> is
230 * attached to below its parent, which is also where previous node
231 * was attached. <newkey> carries the key being inserted.
232 */
233 newkey = new->key;
234
235 while (1) {
236 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
237 eb_troot_t *new_left, *new_rght;
238 eb_troot_t *new_leaf, *old_leaf;
239
240 old = container_of(eb_untag(troot, EB_LEAF),
241 struct eb32_node, node.branches);
242
243 new_left = eb_dotag(&new->node.branches, EB_LEFT);
244 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
245 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
246 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
247
248 new->node.node_p = old->node.leaf_p;
249
250 /* Right here, we have 3 possibilities :
251 - the tree does not contain the key, and we have
252 new->key < old->key. We insert new above old, on
253 the left ;
254
255 - the tree does not contain the key, and we have
256 new->key > old->key. We insert new above old, on
257 the right ;
258
259 - the tree does contain the key, which implies it
260 is alone. We add the new key next to it as a
261 first duplicate.
262
263 The last two cases can easily be partially merged.
264 */
265
266 if (new->key < old->key) {
267 new->node.leaf_p = new_left;
268 old->node.leaf_p = new_rght;
269 new->node.branches.b[EB_LEFT] = new_leaf;
270 new->node.branches.b[EB_RGHT] = old_leaf;
271 } else {
272 /* new->key >= old->key, new goes the right */
273 old->node.leaf_p = new_left;
274 new->node.leaf_p = new_rght;
275 new->node.branches.b[EB_LEFT] = old_leaf;
276 new->node.branches.b[EB_RGHT] = new_leaf;
277
278 if (new->key == old->key) {
279 new->node.bit = -1;
280 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
281 return new;
282 }
283 }
284 break;
285 }
286
287 /* OK we're walking down this link */
288 old = container_of(eb_untag(troot, EB_NODE),
289 struct eb32_node, node.branches);
290
291 /* Stop going down when we don't have common bits anymore. We
292 * also stop in front of a duplicates tree because it means we
293 * have to insert above.
294 */
295
296 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
297 (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
298 /* The tree did not contain the key, so we insert <new> before the node
299 * <old>, and set ->bit to designate the lowest bit position in <new>
300 * which applies to ->branches.b[].
301 */
302 eb_troot_t *new_left, *new_rght;
303 eb_troot_t *new_leaf, *old_node;
304
305 new_left = eb_dotag(&new->node.branches, EB_LEFT);
306 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
307 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
308 old_node = eb_dotag(&old->node.branches, EB_NODE);
309
310 new->node.node_p = old->node.node_p;
311
312 if (new->key < old->key) {
313 new->node.leaf_p = new_left;
314 old->node.node_p = new_rght;
315 new->node.branches.b[EB_LEFT] = new_leaf;
316 new->node.branches.b[EB_RGHT] = old_node;
317 }
318 else if (new->key > old->key) {
319 old->node.node_p = new_left;
320 new->node.leaf_p = new_rght;
321 new->node.branches.b[EB_LEFT] = old_node;
322 new->node.branches.b[EB_RGHT] = new_leaf;
323 }
324 else {
325 struct eb_node *ret;
326 ret = eb_insert_dup(&old->node, &new->node);
327 return container_of(ret, struct eb32_node, node);
328 }
329 break;
330 }
331
332 /* walk down */
333 root = &old->node.branches;
334 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
335 troot = root->b[side];
336 }
337
338 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
339 * parent is already set to <new>, and the <root>'s branch is still in
340 * <side>. Update the root's leaf till we have it. Note that we can also
341 * find the side by checking the side of new->node.node_p.
342 */
343
344 /* We need the common higher bits between new->key and old->key.
345 * What differences are there between new->key and the node here ?
346 * NOTE that bit(new) is always < bit(root) because highest
347 * bit of new->key and old->key are identical here (otherwise they
348 * would sit on different branches).
349 */
350 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
351 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
352 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
353
354 return new;
355}
356
357/* Insert eb32_node <new> into subtree starting at node root <root>, using
358 * signed keys. Only new->key needs be set with the key. The eb32_node
359 * is returned
360 */
361static inline struct eb32_node *
362__eb32i_insert(struct eb_root *root, struct eb32_node *new) {
363 struct eb32_node *old;
364 unsigned int side;
365 eb_troot_t *troot;
366 int newkey; /* caching the key saves approximately one cycle */
367
368 side = EB_LEFT;
369 troot = root->b[EB_LEFT];
370 if (unlikely(troot == NULL)) {
371 /* Tree is empty, insert the leaf part below the left branch */
372 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
373 new->node.leaf_p = eb_dotag(root, EB_LEFT);
374 new->node.node_p = NULL; /* node part unused */
375 return new;
376 }
377
378 /* The tree descent is fairly easy :
379 * - first, check if we have reached a leaf node
380 * - second, check if we have gone too far
381 * - third, reiterate
382 * Everywhere, we use <new> for the node node we are inserting, <root>
383 * for the node we attach it to, and <old> for the node we are
384 * displacing below <new>. <troot> will always point to the future node
385 * (tagged with its type). <side> carries the side the node <new> is
386 * attached to below its parent, which is also where previous node
387 * was attached. <newkey> carries a high bit shift of the key being
388 * inserted in order to have negative keys stored before positive
389 * ones.
390 */
391 newkey = new->key + 0x80000000;
392
393 while (1) {
394 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
395 eb_troot_t *new_left, *new_rght;
396 eb_troot_t *new_leaf, *old_leaf;
397
398 old = container_of(eb_untag(troot, EB_LEAF),
399 struct eb32_node, node.branches);
400
401 new_left = eb_dotag(&new->node.branches, EB_LEFT);
402 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
403 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
404 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
405
406 new->node.node_p = old->node.leaf_p;
407
408 /* Right here, we have 3 possibilities :
409 - the tree does not contain the key, and we have
410 new->key < old->key. We insert new above old, on
411 the left ;
412
413 - the tree does not contain the key, and we have
414 new->key > old->key. We insert new above old, on
415 the right ;
416
417 - the tree does contain the key, which implies it
418 is alone. We add the new key next to it as a
419 first duplicate.
420
421 The last two cases can easily be partially merged.
422 */
423
424 if ((s32)new->key < (s32)old->key) {
425 new->node.leaf_p = new_left;
426 old->node.leaf_p = new_rght;
427 new->node.branches.b[EB_LEFT] = new_leaf;
428 new->node.branches.b[EB_RGHT] = old_leaf;
429 } else {
430 /* new->key >= old->key, new goes the right */
431 old->node.leaf_p = new_left;
432 new->node.leaf_p = new_rght;
433 new->node.branches.b[EB_LEFT] = old_leaf;
434 new->node.branches.b[EB_RGHT] = new_leaf;
435
436 if (new->key == old->key) {
437 new->node.bit = -1;
438 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
439 return new;
440 }
441 }
442 break;
443 }
444
445 /* OK we're walking down this link */
446 old = container_of(eb_untag(troot, EB_NODE),
447 struct eb32_node, node.branches);
448
449 /* Stop going down when we don't have common bits anymore. We
450 * also stop in front of a duplicates tree because it means we
451 * have to insert above.
452 */
453
454 if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
455 (((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
456 /* The tree did not contain the key, so we insert <new> before the node
457 * <old>, and set ->bit to designate the lowest bit position in <new>
458 * which applies to ->branches.b[].
459 */
460 eb_troot_t *new_left, *new_rght;
461 eb_troot_t *new_leaf, *old_node;
462
463 new_left = eb_dotag(&new->node.branches, EB_LEFT);
464 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
465 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
466 old_node = eb_dotag(&old->node.branches, EB_NODE);
467
468 new->node.node_p = old->node.node_p;
469
470 if ((s32)new->key < (s32)old->key) {
471 new->node.leaf_p = new_left;
472 old->node.node_p = new_rght;
473 new->node.branches.b[EB_LEFT] = new_leaf;
474 new->node.branches.b[EB_RGHT] = old_node;
475 }
476 else if ((s32)new->key > (s32)old->key) {
477 old->node.node_p = new_left;
478 new->node.leaf_p = new_rght;
479 new->node.branches.b[EB_LEFT] = old_node;
480 new->node.branches.b[EB_RGHT] = new_leaf;
481 }
482 else {
483 struct eb_node *ret;
484 ret = eb_insert_dup(&old->node, &new->node);
485 return container_of(ret, struct eb32_node, node);
486 }
487 break;
488 }
489
490 /* walk down */
491 root = &old->node.branches;
492 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
493 troot = root->b[side];
494 }
495
496 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
497 * parent is already set to <new>, and the <root>'s branch is still in
498 * <side>. Update the root's leaf till we have it. Note that we can also
499 * find the side by checking the side of new->node.node_p.
500 */
501
502 /* We need the common higher bits between new->key and old->key.
503 * What differences are there between new->key and the node here ?
504 * NOTE that bit(new) is always < bit(root) because highest
505 * bit of new->key and old->key are identical here (otherwise they
506 * would sit on different branches).
507 */
508 // note that if EB_NODE_BITS > 1, we should check that it's still >= 0
509 new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
510 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
511
512 return new;
513}