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