blob: f0c79303574306bbec8534d6c92d80bf2507dddb [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
Willy Tarreauf56fd8a2007-11-19 18:43:04 +010020#ifndef _COMMON_EB32TREE_H
21#define _COMMON_EB32TREE_H
22
Willy Tarreaue6d2e4d2007-11-15 23:56:17 +010023#include "ebtree.h"
24
25
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)
28
29#define EB32_ROOT EB_ROOT
30#define EB32_TREE_HEAD EB_TREE_HEAD
31
32/* These types may sometimes already be defined */
33typedef unsigned int u32;
34typedef signed int s32;
35
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.
40 */
41struct eb32_node {
42 struct eb_node node; /* the tree node, must be at the beginning */
43 u32 key;
44};
45
46/*
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.
50 */
51
52/* Return leftmost node in the tree, or NULL if none */
53static inline struct eb32_node *eb32_first(struct eb_root *root)
54{
55 return eb32_entry(eb_first(root), struct eb32_node, node);
56}
57
58/* Return rightmost node in the tree, or NULL if none */
59static inline struct eb32_node *eb32_last(struct eb_root *root)
60{
61 return eb32_entry(eb_last(root), struct eb32_node, node);
62}
63
64/* Return next node in the tree, or NULL if none */
65static inline struct eb32_node *eb32_next(struct eb32_node *eb32)
66{
67 return eb32_entry(eb_next(&eb32->node), struct eb32_node, node);
68}
69
70/* Return previous node in the tree, or NULL if none */
71static inline struct eb32_node *eb32_prev(struct eb32_node *eb32)
72{
73 return eb32_entry(eb_prev(&eb32->node), struct eb32_node, node);
74}
75
76/* Return next node in the tree, skipping duplicates, or NULL if none */
77static inline struct eb32_node *eb32_next_unique(struct eb32_node *eb32)
78{
79 return eb32_entry(eb_next_unique(&eb32->node), struct eb32_node, node);
80}
81
82/* Return previous node in the tree, skipping duplicates, or NULL if none */
83static inline struct eb32_node *eb32_prev_unique(struct eb32_node *eb32)
84{
85 return eb32_entry(eb_prev_unique(&eb32->node), struct eb32_node, node);
86}
87
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.
90 */
91static inline void eb32_delete(struct eb32_node *eb32)
92{
93 eb_delete(&eb32->node);
94}
95
96/*
97 * The following functions are not inlined by default. They are declared
98 * in eb32tree.c, which simply relies on their inline version.
99 */
100REGPRM2 struct eb32_node *eb32_lookup(struct eb_root *root, u32 x);
101REGPRM2 struct eb32_node *eb32i_lookup(struct eb_root *root, s32 x);
102REGPRM2 struct eb32_node *eb32_insert(struct eb_root *root, struct eb32_node *new);
103REGPRM2 struct eb32_node *eb32i_insert(struct eb_root *root, struct eb32_node *new);
104
105/*
106 * The following functions are less likely to be used directly, because their
107 * code is larger. The non-inlined version is preferred.
108 */
109
110/* Delete node from the tree if it was linked in. Mark the node unused. */
111static inline void __eb32_delete(struct eb32_node *eb32)
112{
113 __eb_delete(&eb32->node);
114}
115
116/*
117 * Find the first occurence of a key in the tree <root>. If none can be
118 * found, return NULL.
119 */
120static inline struct eb32_node *__eb32_lookup(struct eb_root *root, u32 x)
121{
122 struct eb32_node *node;
123 eb_troot_t *troot;
124
125 troot = root->b[EB_LEFT];
126 if (unlikely(troot == NULL))
127 return NULL;
128
129 while (1) {
130 if ((eb_gettag(troot) == EB_LEAF)) {
131 node = container_of(eb_untag(troot, EB_LEAF),
132 struct eb32_node, node.branches);
133 if (node->key == x)
134 return node;
135 else
136 return NULL;
137 }
138 node = container_of(eb_untag(troot, EB_NODE),
139 struct eb32_node, node.branches);
140
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.
145 */
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);
152 }
153 return node;
154 }
155
156 troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
157 }
158}
159
160/*
161 * Find the first occurence of a signed key in the tree <root>. If none can
162 * be found, return NULL.
163 */
164static inline struct eb32_node *__eb32i_lookup(struct eb_root *root, s32 x)
165{
166 struct eb32_node *node;
167 eb_troot_t *troot;
168 u32 key = x ^ 0x80000000;
169
170 troot = root->b[EB_LEFT];
171 if (unlikely(troot == NULL))
172 return NULL;
173
174 while (1) {
175 if ((eb_gettag(troot) == EB_LEAF)) {
176 node = container_of(eb_untag(troot, EB_LEAF),
177 struct eb32_node, node.branches);
178 if (node->key == x)
179 return node;
180 else
181 return NULL;
182 }
183 node = container_of(eb_untag(troot, EB_NODE),
184 struct eb32_node, node.branches);
185
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.
190 */
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);
197 }
198 return node;
199 }
200
201 troot = node->node.branches.b[(key >> node->node.bit) & EB_NODE_BRANCH_MASK];
202 }
203}
204
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.
207 */
208static inline struct eb32_node *
209__eb32_insert(struct eb_root *root, struct eb32_node *new) {
210 struct eb32_node *old;
211 unsigned int side;
212 eb_troot_t *troot;
213 u32 newkey; /* caching the key saves approximately one cycle */
214
215 side = EB_LEFT;
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 */
222 return new;
223 }
224
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
228 * - third, reiterate
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.
235 */
236 newkey = new->key;
237
238 while (1) {
239 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
240 eb_troot_t *new_left, *new_rght;
241 eb_troot_t *new_leaf, *old_leaf;
242
243 old = container_of(eb_untag(troot, EB_LEAF),
244 struct eb32_node, node.branches);
245
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);
250
251 new->node.node_p = old->node.leaf_p;
252
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
256 the left ;
257
258 - the tree does not contain the key, and we have
259 new->key > old->key. We insert new above old, on
260 the right ;
261
262 - the tree does contain the key, which implies it
263 is alone. We add the new key next to it as a
264 first duplicate.
265
266 The last two cases can easily be partially merged.
267 */
268
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;
274 } else {
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;
280
281 if (new->key == old->key) {
282 new->node.bit = -1;
283 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
284 return new;
285 }
286 }
287 break;
288 }
289
290 /* OK we're walking down this link */
291 old = container_of(eb_untag(troot, EB_NODE),
292 struct eb32_node, node.branches);
293
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.
297 */
298
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[].
304 */
305 eb_troot_t *new_left, *new_rght;
306 eb_troot_t *new_leaf, *old_node;
307
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);
312
313 new->node.node_p = old->node.node_p;
314
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;
320 }
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;
326 }
327 else {
328 struct eb_node *ret;
329 ret = eb_insert_dup(&old->node, &new->node);
330 return container_of(ret, struct eb32_node, node);
331 }
332 break;
333 }
334
335 /* walk down */
336 root = &old->node.branches;
337 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
338 troot = root->b[side];
339 }
340
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.
345 */
346
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).
352 */
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);
356
357 return new;
358}
359
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
362 * is returned
363 */
364static inline struct eb32_node *
365__eb32i_insert(struct eb_root *root, struct eb32_node *new) {
366 struct eb32_node *old;
367 unsigned int side;
368 eb_troot_t *troot;
369 int newkey; /* caching the key saves approximately one cycle */
370
371 side = EB_LEFT;
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 */
378 return new;
379 }
380
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
384 * - third, reiterate
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
392 * ones.
393 */
394 newkey = new->key + 0x80000000;
395
396 while (1) {
397 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
398 eb_troot_t *new_left, *new_rght;
399 eb_troot_t *new_leaf, *old_leaf;
400
401 old = container_of(eb_untag(troot, EB_LEAF),
402 struct eb32_node, node.branches);
403
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);
408
409 new->node.node_p = old->node.leaf_p;
410
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
414 the left ;
415
416 - the tree does not contain the key, and we have
417 new->key > old->key. We insert new above old, on
418 the right ;
419
420 - the tree does contain the key, which implies it
421 is alone. We add the new key next to it as a
422 first duplicate.
423
424 The last two cases can easily be partially merged.
425 */
426
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;
432 } else {
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;
438
439 if (new->key == old->key) {
440 new->node.bit = -1;
441 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
442 return new;
443 }
444 }
445 break;
446 }
447
448 /* OK we're walking down this link */
449 old = container_of(eb_untag(troot, EB_NODE),
450 struct eb32_node, node.branches);
451
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.
455 */
456
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[].
462 */
463 eb_troot_t *new_left, *new_rght;
464 eb_troot_t *new_leaf, *old_node;
465
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);
470
471 new->node.node_p = old->node.node_p;
472
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;
478 }
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;
484 }
485 else {
486 struct eb_node *ret;
487 ret = eb_insert_dup(&old->node, &new->node);
488 return container_of(ret, struct eb32_node, node);
489 }
490 break;
491 }
492
493 /* walk down */
494 root = &old->node.branches;
495 side = (newkey >> old->node.bit) & EB_NODE_BRANCH_MASK;
496 troot = root->b[side];
497 }
498
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.
503 */
504
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).
510 */
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);
514
515 return new;
516}
Willy Tarreauf56fd8a2007-11-19 18:43:04 +0100517
518#endif /* _COMMON_EB32TREE_H */