2
* Elastic Binary Trees - macros and structures for operations on 32bit nodes.
3
* (C) 2002-2007 - Willy Tarreau <w@1wt.eu>
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.
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.
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
20
#ifndef _COMMON_EB32TREE_H
21
#define _COMMON_EB32TREE_H
26
/* Return the structure of type <type> whose member <member> points to <ptr> */
27
#define eb32_entry(ptr, type, member) container_of(ptr, type, member)
29
#define EB32_ROOT EB_ROOT
30
#define EB32_TREE_HEAD EB_TREE_HEAD
32
/* These types may sometimes already be defined */
33
typedef unsigned int u32;
34
typedef signed int s32;
36
/* This structure carries a node, a leaf, and a key. It must start with the
37
* eb_node so that it can be cast into an eb_node. We could also have put some
38
* sort of transparent union here to reduce the indirection level, but the fact
39
* is, the end user is not meant to manipulate internals, so this is pointless.
42
struct eb_node node; /* the tree node, must be at the beginning */
47
* Exported functions and macros.
48
* Many of them are always inlined because they are extremely small, and
49
* are generally called at most once or twice in a program.
52
/* Return leftmost node in the tree, or NULL if none */
53
static inline struct eb32_node *eb32_first(struct eb_root *root)
55
return eb32_entry(eb_first(root), struct eb32_node, node);
58
/* Return rightmost node in the tree, or NULL if none */
59
static inline struct eb32_node *eb32_last(struct eb_root *root)
61
return eb32_entry(eb_last(root), struct eb32_node, node);
64
/* Return next node in the tree, or NULL if none */
65
static inline struct eb32_node *eb32_next(struct eb32_node *eb32)
67
return eb32_entry(eb_next(&eb32->node), struct eb32_node, node);
70
/* Return previous node in the tree, or NULL if none */
71
static inline struct eb32_node *eb32_prev(struct eb32_node *eb32)
73
return eb32_entry(eb_prev(&eb32->node), struct eb32_node, node);
76
/* Return next node in the tree, skipping duplicates, or NULL if none */
77
static inline struct eb32_node *eb32_next_unique(struct eb32_node *eb32)
79
return eb32_entry(eb_next_unique(&eb32->node), struct eb32_node, node);
82
/* Return previous node in the tree, skipping duplicates, or NULL if none */
83
static inline struct eb32_node *eb32_prev_unique(struct eb32_node *eb32)
85
return eb32_entry(eb_prev_unique(&eb32->node), struct eb32_node, node);
88
/* Delete node from the tree if it was linked in. Mark the node unused. Note
89
* that this function relies on a non-inlined generic function: eb_delete.
91
static inline void eb32_delete(struct eb32_node *eb32)
93
eb_delete(&eb32->node);
97
* The following functions are not inlined by default. They are declared
98
* in eb32tree.c, which simply relies on their inline version.
100
REGPRM2 struct eb32_node *eb32_lookup(struct eb_root *root, u32 x);
101
REGPRM2 struct eb32_node *eb32i_lookup(struct eb_root *root, s32 x);
102
REGPRM2 struct eb32_node *eb32_insert(struct eb_root *root, struct eb32_node *new);
103
REGPRM2 struct eb32_node *eb32i_insert(struct eb_root *root, struct eb32_node *new);
106
* The following functions are less likely to be used directly, because their
107
* code is larger. The non-inlined version is preferred.
110
/* Delete node from the tree if it was linked in. Mark the node unused. */
111
static inline void __eb32_delete(struct eb32_node *eb32)
113
__eb_delete(&eb32->node);
117
* Find the first occurence of a key in the tree <root>. If none can be
118
* found, return NULL.
120
static inline struct eb32_node *__eb32_lookup(struct eb_root *root, u32 x)
122
struct eb32_node *node;
125
troot = root->b[EB_LEFT];
126
if (unlikely(troot == NULL))
130
if ((eb_gettag(troot) == EB_LEAF)) {
131
node = container_of(eb_untag(troot, EB_LEAF),
132
struct eb32_node, node.branches);
138
node = container_of(eb_untag(troot, EB_NODE),
139
struct eb32_node, node.branches);
141
if (x == node->key) {
142
/* Either we found the node which holds the key, or
143
* we have a dup tree. In the later case, we have to
144
* walk it down left to get the first entry.
146
if (node->node.bit < 0) {
147
troot = node->node.branches.b[EB_LEFT];
148
while (eb_gettag(troot) != EB_LEAF)
149
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
150
node = container_of(eb_untag(troot, EB_LEAF),
151
struct eb32_node, node.branches);
156
troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
161
* Find the first occurence of a signed key in the tree <root>. If none can
162
* be found, return NULL.
164
static inline struct eb32_node *__eb32i_lookup(struct eb_root *root, s32 x)
166
struct eb32_node *node;
168
u32 key = x ^ 0x80000000;
170
troot = root->b[EB_LEFT];
171
if (unlikely(troot == NULL))
175
if ((eb_gettag(troot) == EB_LEAF)) {
176
node = container_of(eb_untag(troot, EB_LEAF),
177
struct eb32_node, node.branches);
183
node = container_of(eb_untag(troot, EB_NODE),
184
struct eb32_node, node.branches);
186
if (x == node->key) {
187
/* Either we found the node which holds the key, or
188
* we have a dup tree. In the later case, we have to
189
* walk it down left to get the first entry.
191
if (node->node.bit < 0) {
192
troot = node->node.branches.b[EB_LEFT];
193
while (eb_gettag(troot) != EB_LEAF)
194
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
195
node = container_of(eb_untag(troot, EB_LEAF),
196
struct eb32_node, node.branches);
201
troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
205
/* Insert eb32_node <new> into subtree starting at node root <root>.
206
* Only new->key needs be set with the key. The eb32_node is returned.
208
static inline struct eb32_node *
209
__eb32_insert(struct eb_root *root, struct eb32_node *new) {
210
struct eb32_node *old;
213
u32 newkey; /* caching the key saves approximately one cycle */
216
troot = root->b[EB_LEFT];
217
if (unlikely(troot == NULL)) {
218
/* Tree is empty, insert the leaf part below the left branch */
219
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
220
new->node.leaf_p = eb_dotag(root, EB_LEFT);
221
new->node.node_p = NULL; /* node part unused */
225
/* The tree descent is fairly easy :
226
* - first, check if we have reached a leaf node
227
* - second, check if we have gone too far
229
* Everywhere, we use <new> for the node node we are inserting, <root>
230
* for the node we attach it to, and <old> for the node we are
231
* displacing below <new>. <troot> will always point to the future node
232
* (tagged with its type). <side> carries the side the node <new> is
233
* attached to below its parent, which is also where previous node
234
* was attached. <newkey> carries the key being inserted.
239
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
240
eb_troot_t *new_left, *new_rght;
241
eb_troot_t *new_leaf, *old_leaf;
243
old = container_of(eb_untag(troot, EB_LEAF),
244
struct eb32_node, node.branches);
246
new_left = eb_dotag(&new->node.branches, EB_LEFT);
247
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
248
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
249
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
251
new->node.node_p = old->node.leaf_p;
253
/* Right here, we have 3 possibilities :
254
- the tree does not contain the key, and we have
255
new->key < old->key. We insert new above old, on
258
- the tree does not contain the key, and we have
259
new->key > old->key. We insert new above old, on
262
- the tree does contain the key, which implies it
263
is alone. We add the new key next to it as a
266
The last two cases can easily be partially merged.
269
if (new->key < old->key) {
270
new->node.leaf_p = new_left;
271
old->node.leaf_p = new_rght;
272
new->node.branches.b[EB_LEFT] = new_leaf;
273
new->node.branches.b[EB_RGHT] = old_leaf;
275
/* new->key >= old->key, new goes the right */
276
old->node.leaf_p = new_left;
277
new->node.leaf_p = new_rght;
278
new->node.branches.b[EB_LEFT] = old_leaf;
279
new->node.branches.b[EB_RGHT] = new_leaf;
281
if (new->key == old->key) {
283
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
290
/* OK we're walking down this link */
291
old = container_of(eb_untag(troot, EB_NODE),
292
struct eb32_node, node.branches);
294
/* Stop going down when we don't have common bits anymore. We
295
* also stop in front of a duplicates tree because it means we
296
* have to insert above.
299
if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
300
(((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
301
/* The tree did not contain the key, so we insert <new> before the node
302
* <old>, and set ->bit to designate the lowest bit position in <new>
303
* which applies to ->branches.b[].
305
eb_troot_t *new_left, *new_rght;
306
eb_troot_t *new_leaf, *old_node;
308
new_left = eb_dotag(&new->node.branches, EB_LEFT);
309
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
310
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
311
old_node = eb_dotag(&old->node.branches, EB_NODE);
313
new->node.node_p = old->node.node_p;
315
if (new->key < old->key) {
316
new->node.leaf_p = new_left;
317
old->node.node_p = new_rght;
318
new->node.branches.b[EB_LEFT] = new_leaf;
319
new->node.branches.b[EB_RGHT] = old_node;
321
else if (new->key > old->key) {
322
old->node.node_p = new_left;
323
new->node.leaf_p = new_rght;
324
new->node.branches.b[EB_LEFT] = old_node;
325
new->node.branches.b[EB_RGHT] = new_leaf;
329
ret = eb_insert_dup(&old->node, &new->node);
330
return container_of(ret, struct eb32_node, node);
336
root = &old->node.branches;
337
side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
338
troot = root->b[side];
341
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
342
* parent is already set to <new>, and the <root>'s branch is still in
343
* <side>. Update the root's leaf till we have it. Note that we can also
344
* find the side by checking the side of new->node.node_p.
347
/* We need the common higher bits between new->key and old->key.
348
* What differences are there between new->key and the node here ?
349
* NOTE that bit(new) is always < bit(root) because highest
350
* bit of new->key and old->key are identical here (otherwise they
351
* would sit on different branches).
353
// note that if EB_NODE_BITS > 1, we should check that it's still >= 0
354
new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
355
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
360
/* Insert eb32_node <new> into subtree starting at node root <root>, using
361
* signed keys. Only new->key needs be set with the key. The eb32_node
364
static inline struct eb32_node *
365
__eb32i_insert(struct eb_root *root, struct eb32_node *new) {
366
struct eb32_node *old;
369
int newkey; /* caching the key saves approximately one cycle */
372
troot = root->b[EB_LEFT];
373
if (unlikely(troot == NULL)) {
374
/* Tree is empty, insert the leaf part below the left branch */
375
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
376
new->node.leaf_p = eb_dotag(root, EB_LEFT);
377
new->node.node_p = NULL; /* node part unused */
381
/* The tree descent is fairly easy :
382
* - first, check if we have reached a leaf node
383
* - second, check if we have gone too far
385
* Everywhere, we use <new> for the node node we are inserting, <root>
386
* for the node we attach it to, and <old> for the node we are
387
* displacing below <new>. <troot> will always point to the future node
388
* (tagged with its type). <side> carries the side the node <new> is
389
* attached to below its parent, which is also where previous node
390
* was attached. <newkey> carries a high bit shift of the key being
391
* inserted in order to have negative keys stored before positive
394
newkey = new->key + 0x80000000;
397
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
398
eb_troot_t *new_left, *new_rght;
399
eb_troot_t *new_leaf, *old_leaf;
401
old = container_of(eb_untag(troot, EB_LEAF),
402
struct eb32_node, node.branches);
404
new_left = eb_dotag(&new->node.branches, EB_LEFT);
405
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
406
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
407
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
409
new->node.node_p = old->node.leaf_p;
411
/* Right here, we have 3 possibilities :
412
- the tree does not contain the key, and we have
413
new->key < old->key. We insert new above old, on
416
- the tree does not contain the key, and we have
417
new->key > old->key. We insert new above old, on
420
- the tree does contain the key, which implies it
421
is alone. We add the new key next to it as a
424
The last two cases can easily be partially merged.
427
if ((s32)new->key < (s32)old->key) {
428
new->node.leaf_p = new_left;
429
old->node.leaf_p = new_rght;
430
new->node.branches.b[EB_LEFT] = new_leaf;
431
new->node.branches.b[EB_RGHT] = old_leaf;
433
/* new->key >= old->key, new goes the right */
434
old->node.leaf_p = new_left;
435
new->node.leaf_p = new_rght;
436
new->node.branches.b[EB_LEFT] = old_leaf;
437
new->node.branches.b[EB_RGHT] = new_leaf;
439
if (new->key == old->key) {
441
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
448
/* OK we're walking down this link */
449
old = container_of(eb_untag(troot, EB_NODE),
450
struct eb32_node, node.branches);
452
/* Stop going down when we don't have common bits anymore. We
453
* also stop in front of a duplicates tree because it means we
454
* have to insert above.
457
if ((old->node.bit < 0) || /* we're above a duplicate tree, stop here */
458
(((new->key ^ old->key) >> old->node.bit) >= EB_NODE_BRANCHES)) {
459
/* The tree did not contain the key, so we insert <new> before the node
460
* <old>, and set ->bit to designate the lowest bit position in <new>
461
* which applies to ->branches.b[].
463
eb_troot_t *new_left, *new_rght;
464
eb_troot_t *new_leaf, *old_node;
466
new_left = eb_dotag(&new->node.branches, EB_LEFT);
467
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
468
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
469
old_node = eb_dotag(&old->node.branches, EB_NODE);
471
new->node.node_p = old->node.node_p;
473
if ((s32)new->key < (s32)old->key) {
474
new->node.leaf_p = new_left;
475
old->node.node_p = new_rght;
476
new->node.branches.b[EB_LEFT] = new_leaf;
477
new->node.branches.b[EB_RGHT] = old_node;
479
else if ((s32)new->key > (s32)old->key) {
480
old->node.node_p = new_left;
481
new->node.leaf_p = new_rght;
482
new->node.branches.b[EB_LEFT] = old_node;
483
new->node.branches.b[EB_RGHT] = new_leaf;
487
ret = eb_insert_dup(&old->node, &new->node);
488
return container_of(ret, struct eb32_node, node);
494
root = &old->node.branches;
495
side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
496
troot = root->b[side];
499
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
500
* parent is already set to <new>, and the <root>'s branch is still in
501
* <side>. Update the root's leaf till we have it. Note that we can also
502
* find the side by checking the side of new->node.node_p.
505
/* We need the common higher bits between new->key and old->key.
506
* What differences are there between new->key and the node here ?
507
* NOTE that bit(new) is always < bit(root) because highest
508
* bit of new->key and old->key are identical here (otherwise they
509
* would sit on different branches).
511
// note that if EB_NODE_BITS > 1, we should check that it's still >= 0
512
new->node.bit = flsnz(new->key ^ old->key) - EB_NODE_BITS;
513
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
518
#endif /* _COMMON_EB32TREE_H */