2
* Elastic Binary Trees - macros and structures for operations on 32bit nodes.
4
* (C) 2002-2009 - Willy Tarreau <w@1wt.eu>
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.
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.
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
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)
30
#define EB32_ROOT EB_ROOT
31
#define EB32_TREE_HEAD EB_TREE_HEAD
33
/* These types may sometimes already be defined */
34
typedef unsigned int u32;
35
typedef signed int s32;
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.
43
struct eb_node node; /* the tree node, must be at the beginning */
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.
53
/* Return leftmost node in the tree, or NULL if none */
54
static inline struct eb32_node *eb32_first(struct eb_root *root)
56
return eb32_entry(eb_first(root), struct eb32_node, node);
59
/* Return rightmost node in the tree, or NULL if none */
60
static inline struct eb32_node *eb32_last(struct eb_root *root)
62
return eb32_entry(eb_last(root), struct eb32_node, node);
65
/* Return next node in the tree, or NULL if none */
66
static inline struct eb32_node *eb32_next(struct eb32_node *eb32)
68
return eb32_entry(eb_next(&eb32->node), struct eb32_node, node);
71
/* Return previous node in the tree, or NULL if none */
72
static inline struct eb32_node *eb32_prev(struct eb32_node *eb32)
74
return eb32_entry(eb_prev(&eb32->node), struct eb32_node, node);
77
/* Return next node in the tree, skipping duplicates, or NULL if none */
78
static inline struct eb32_node *eb32_next_unique(struct eb32_node *eb32)
80
return eb32_entry(eb_next_unique(&eb32->node), struct eb32_node, node);
83
/* Return previous node in the tree, skipping duplicates, or NULL if none */
84
static inline struct eb32_node *eb32_prev_unique(struct eb32_node *eb32)
86
return eb32_entry(eb_prev_unique(&eb32->node), struct eb32_node, node);
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.
92
static inline void eb32_delete(struct eb32_node *eb32)
94
eb_delete(&eb32->node);
98
* The following functions are not inlined by default. They are declared
99
* in eb32tree.c, which simply relies on their inline version.
101
REGPRM2 struct eb32_node *eb32_lookup(struct eb_root *root, u32 x);
102
REGPRM2 struct eb32_node *eb32i_lookup(struct eb_root *root, s32 x);
103
REGPRM2 struct eb32_node *eb32_lookup_le(struct eb_root *root, u32 x);
104
REGPRM2 struct eb32_node *eb32_lookup_ge(struct eb_root *root, u32 x);
105
REGPRM2 struct eb32_node *eb32_insert(struct eb_root *root, struct eb32_node *new);
106
REGPRM2 struct eb32_node *eb32i_insert(struct eb_root *root, struct eb32_node *new);
109
* The following functions are less likely to be used directly, because their
110
* code is larger. The non-inlined version is preferred.
113
/* Delete node from the tree if it was linked in. Mark the node unused. */
114
static forceinline void __eb32_delete(struct eb32_node *eb32)
116
__eb_delete(&eb32->node);
120
* Find the first occurence of a key in the tree <root>. If none can be
121
* found, return NULL.
123
static forceinline struct eb32_node *__eb32_lookup(struct eb_root *root, u32 x)
125
struct eb32_node *node;
129
troot = root->b[EB_LEFT];
130
if (unlikely(troot == NULL))
134
if ((eb_gettag(troot) == EB_LEAF)) {
135
node = container_of(eb_untag(troot, EB_LEAF),
136
struct eb32_node, node.branches);
142
node = container_of(eb_untag(troot, EB_NODE),
143
struct eb32_node, node.branches);
147
/* Either we found the node which holds the key, or
148
* we have a dup tree. In the later case, we have to
149
* walk it down left to get the first entry.
151
if (node->node.bit < 0) {
152
troot = node->node.branches.b[EB_LEFT];
153
while (eb_gettag(troot) != EB_LEAF)
154
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
155
node = container_of(eb_untag(troot, EB_LEAF),
156
struct eb32_node, node.branches);
161
if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
162
return NULL; /* no more common bits */
164
troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
169
* Find the first occurence of a signed key in the tree <root>. If none can
170
* be found, return NULL.
172
static forceinline struct eb32_node *__eb32i_lookup(struct eb_root *root, s32 x)
174
struct eb32_node *node;
176
u32 key = x ^ 0x80000000;
179
troot = root->b[EB_LEFT];
180
if (unlikely(troot == NULL))
184
if ((eb_gettag(troot) == EB_LEAF)) {
185
node = container_of(eb_untag(troot, EB_LEAF),
186
struct eb32_node, node.branches);
192
node = container_of(eb_untag(troot, EB_NODE),
193
struct eb32_node, node.branches);
197
/* Either we found the node which holds the key, or
198
* we have a dup tree. In the later case, we have to
199
* walk it down left to get the first entry.
201
if (node->node.bit < 0) {
202
troot = node->node.branches.b[EB_LEFT];
203
while (eb_gettag(troot) != EB_LEAF)
204
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
205
node = container_of(eb_untag(troot, EB_LEAF),
206
struct eb32_node, node.branches);
211
if ((y >> node->node.bit) >= EB_NODE_BRANCHES)
212
return NULL; /* no more common bits */
214
troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
218
/* Insert eb32_node <new> into subtree starting at node root <root>.
219
* Only new->key needs be set with the key. The eb32_node is returned.
220
* If root->b[EB_RGHT]==1, the tree may only contain unique keys.
222
static forceinline struct eb32_node *
223
__eb32_insert(struct eb_root *root, struct eb32_node *new) {
224
struct eb32_node *old;
227
u32 newkey; /* caching the key saves approximately one cycle */
228
eb_troot_t *root_right = root;
231
troot = root->b[EB_LEFT];
232
root_right = root->b[EB_RGHT];
233
if (unlikely(troot == NULL)) {
234
/* Tree is empty, insert the leaf part below the left branch */
235
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
236
new->node.leaf_p = eb_dotag(root, EB_LEFT);
237
new->node.node_p = NULL; /* node part unused */
241
/* The tree descent is fairly easy :
242
* - first, check if we have reached a leaf node
243
* - second, check if we have gone too far
245
* Everywhere, we use <new> for the node node we are inserting, <root>
246
* for the node we attach it to, and <old> for the node we are
247
* displacing below <new>. <troot> will always point to the future node
248
* (tagged with its type). <side> carries the side the node <new> is
249
* attached to below its parent, which is also where previous node
250
* was attached. <newkey> carries the key being inserted.
255
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
256
eb_troot_t *new_left, *new_rght;
257
eb_troot_t *new_leaf, *old_leaf;
259
old = container_of(eb_untag(troot, EB_LEAF),
260
struct eb32_node, node.branches);
262
new_left = eb_dotag(&new->node.branches, EB_LEFT);
263
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
264
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
265
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
267
new->node.node_p = old->node.leaf_p;
269
/* Right here, we have 3 possibilities :
270
- the tree does not contain the key, and we have
271
new->key < old->key. We insert new above old, on
274
- the tree does not contain the key, and we have
275
new->key > old->key. We insert new above old, on
278
- the tree does contain the key, which implies it
279
is alone. We add the new key next to it as a
282
The last two cases can easily be partially merged.
285
if (new->key < old->key) {
286
new->node.leaf_p = new_left;
287
old->node.leaf_p = new_rght;
288
new->node.branches.b[EB_LEFT] = new_leaf;
289
new->node.branches.b[EB_RGHT] = old_leaf;
291
/* we may refuse to duplicate this key if the tree is
292
* tagged as containing only unique keys.
294
if ((new->key == old->key) && eb_gettag(root_right))
297
/* new->key >= old->key, new goes the right */
298
old->node.leaf_p = new_left;
299
new->node.leaf_p = new_rght;
300
new->node.branches.b[EB_LEFT] = old_leaf;
301
new->node.branches.b[EB_RGHT] = new_leaf;
303
if (new->key == old->key) {
305
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
312
/* OK we're walking down this link */
313
old = container_of(eb_untag(troot, EB_NODE),
314
struct eb32_node, node.branches);
316
/* Stop going down when we don't have common bits anymore. We
317
* also stop in front of a duplicates tree because it means we
318
* have to insert above.
321
if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
322
(((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
323
/* The tree did not contain the key, so we insert <new> before the node
324
* <old>, and set ->bit to designate the lowest bit position in <new>
325
* which applies to ->branches.b[].
327
eb_troot_t *new_left, *new_rght;
328
eb_troot_t *new_leaf, *old_node;
330
new_left = eb_dotag(&new->node.branches, EB_LEFT);
331
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
332
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
333
old_node = eb_dotag(&old->node.branches, EB_NODE);
335
new->node.node_p = old->node.node_p;
337
if (new->key < old->key) {
338
new->node.leaf_p = new_left;
339
old->node.node_p = new_rght;
340
new->node.branches.b[EB_LEFT] = new_leaf;
341
new->node.branches.b[EB_RGHT] = old_node;
343
else if (new->key > old->key) {
344
old->node.node_p = new_left;
345
new->node.leaf_p = new_rght;
346
new->node.branches.b[EB_LEFT] = old_node;
347
new->node.branches.b[EB_RGHT] = new_leaf;
351
ret = eb_insert_dup(&old->node, &new->node);
352
return container_of(ret, struct eb32_node, node);
358
root = &old->node.branches;
359
side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
360
troot = root->b[side];
363
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
364
* parent is already set to <new>, and the <root>'s branch is still in
365
* <side>. Update the root's leaf till we have it. Note that we can also
366
* find the side by checking the side of new->node.node_p.
369
/* We need the common higher bits between new->key and old->key.
370
* What differences are there between new->key and the node here ?
371
* NOTE that bit(new) is always < bit(root) because highest
372
* bit of new->key and old->key are identical here (otherwise they
373
* would sit on different branches).
375
// note that if EB_NODE_BITS > 1, we should check that it's still >= 0
376
new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
377
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
382
/* Insert eb32_node <new> into subtree starting at node root <root>, using
383
* signed keys. Only new->key needs be set with the key. The eb32_node
384
* is returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys.
386
static forceinline struct eb32_node *
387
__eb32i_insert(struct eb_root *root, struct eb32_node *new) {
388
struct eb32_node *old;
391
int newkey; /* caching the key saves approximately one cycle */
392
eb_troot_t *root_right = root;
395
troot = root->b[EB_LEFT];
396
root_right = root->b[EB_RGHT];
397
if (unlikely(troot == NULL)) {
398
/* Tree is empty, insert the leaf part below the left branch */
399
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
400
new->node.leaf_p = eb_dotag(root, EB_LEFT);
401
new->node.node_p = NULL; /* node part unused */
405
/* The tree descent is fairly easy :
406
* - first, check if we have reached a leaf node
407
* - second, check if we have gone too far
409
* Everywhere, we use <new> for the node node we are inserting, <root>
410
* for the node we attach it to, and <old> for the node we are
411
* displacing below <new>. <troot> will always point to the future node
412
* (tagged with its type). <side> carries the side the node <new> is
413
* attached to below its parent, which is also where previous node
414
* was attached. <newkey> carries a high bit shift of the key being
415
* inserted in order to have negative keys stored before positive
418
newkey = new->key + 0x80000000;
421
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
422
eb_troot_t *new_left, *new_rght;
423
eb_troot_t *new_leaf, *old_leaf;
425
old = container_of(eb_untag(troot, EB_LEAF),
426
struct eb32_node, node.branches);
428
new_left = eb_dotag(&new->node.branches, EB_LEFT);
429
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
430
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
431
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
433
new->node.node_p = old->node.leaf_p;
435
/* Right here, we have 3 possibilities :
436
- the tree does not contain the key, and we have
437
new->key < old->key. We insert new above old, on
440
- the tree does not contain the key, and we have
441
new->key > old->key. We insert new above old, on
444
- the tree does contain the key, which implies it
445
is alone. We add the new key next to it as a
448
The last two cases can easily be partially merged.
451
if ((s32)new->key < (s32)old->key) {
452
new->node.leaf_p = new_left;
453
old->node.leaf_p = new_rght;
454
new->node.branches.b[EB_LEFT] = new_leaf;
455
new->node.branches.b[EB_RGHT] = old_leaf;
457
/* we may refuse to duplicate this key if the tree is
458
* tagged as containing only unique keys.
460
if ((new->key == old->key) && eb_gettag(root_right))
463
/* new->key >= old->key, new goes the right */
464
old->node.leaf_p = new_left;
465
new->node.leaf_p = new_rght;
466
new->node.branches.b[EB_LEFT] = old_leaf;
467
new->node.branches.b[EB_RGHT] = new_leaf;
469
if (new->key == old->key) {
471
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
478
/* OK we're walking down this link */
479
old = container_of(eb_untag(troot, EB_NODE),
480
struct eb32_node, node.branches);
482
/* Stop going down when we don't have common bits anymore. We
483
* also stop in front of a duplicates tree because it means we
484
* have to insert above.
487
if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
488
(((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
489
/* The tree did not contain the key, so we insert <new> before the node
490
* <old>, and set ->bit to designate the lowest bit position in <new>
491
* which applies to ->branches.b[].
493
eb_troot_t *new_left, *new_rght;
494
eb_troot_t *new_leaf, *old_node;
496
new_left = eb_dotag(&new->node.branches, EB_LEFT);
497
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
498
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
499
old_node = eb_dotag(&old->node.branches, EB_NODE);
501
new->node.node_p = old->node.node_p;
503
if ((s32)new->key < (s32)old->key) {
504
new->node.leaf_p = new_left;
505
old->node.node_p = new_rght;
506
new->node.branches.b[EB_LEFT] = new_leaf;
507
new->node.branches.b[EB_RGHT] = old_node;
509
else if ((s32)new->key > (s32)old->key) {
510
old->node.node_p = new_left;
511
new->node.leaf_p = new_rght;
512
new->node.branches.b[EB_LEFT] = old_node;
513
new->node.branches.b[EB_RGHT] = new_leaf;
517
ret = eb_insert_dup(&old->node, &new->node);
518
return container_of(ret, struct eb32_node, node);
524
root = &old->node.branches;
525
side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
526
troot = root->b[side];
529
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
530
* parent is already set to <new>, and the <root>'s branch is still in
531
* <side>. Update the root's leaf till we have it. Note that we can also
532
* find the side by checking the side of new->node.node_p.
535
/* We need the common higher bits between new->key and old->key.
536
* What differences are there between new->key and the node here ?
537
* NOTE that bit(new) is always < bit(root) because highest
538
* bit of new->key and old->key are identical here (otherwise they
539
* would sit on different branches).
541
// note that if EB_NODE_BITS > 1, we should check that it's still >= 0
542
new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
543
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
548
#endif /* _EB32_TREE_H */