2
* Elastic Binary Trees - macros to manipulate String data 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
21
/* These functions and macros rely on Multi-Byte nodes */
29
/* The following functions are not inlined by default. They are declared
30
* in ebsttree.c, which simply relies on their inline version.
32
REGPRM2 struct ebmb_node *ebst_lookup(struct eb_root *root, const char *x);
33
REGPRM3 struct ebmb_node *ebst_lookup_len(struct eb_root *root, const char *x, unsigned int len);
34
REGPRM2 struct ebmb_node *ebst_insert(struct eb_root *root, struct ebmb_node *new);
36
/* Find the first occurence of a zero-terminated string <x> in the tree <root>.
37
* It's the caller's reponsibility to use this function only on trees which
38
* only contain zero-terminated strings. If none can be found, return NULL.
40
static forceinline struct ebmb_node *__ebst_lookup(struct eb_root *root, const void *x)
42
struct ebmb_node *node;
46
troot = root->b[EB_LEFT];
47
if (unlikely(troot == NULL))
52
if ((eb_gettag(troot) == EB_LEAF)) {
53
node = container_of(eb_untag(troot, EB_LEAF),
54
struct ebmb_node, node.branches);
55
if (strcmp((char *)node->key, x) == 0)
60
node = container_of(eb_untag(troot, EB_NODE),
61
struct ebmb_node, node.branches);
63
if (node->node.bit < 0) {
64
/* We have a dup tree now. Either it's for the same
65
* value, and we walk down left, or it's a different
66
* one and we don't have our key.
68
if (strcmp((char *)node->key, x) != 0)
71
troot = node->node.branches.b[EB_LEFT];
72
while (eb_gettag(troot) != EB_LEAF)
73
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
74
node = container_of(eb_untag(troot, EB_LEAF),
75
struct ebmb_node, node.branches);
79
/* OK, normal data node, let's walk down */
80
bit = string_equal_bits(x, node->key, bit);
81
if (bit < node->node.bit)
82
return NULL; /* no more common bits */
84
troot = node->node.branches.b[(((unsigned char*)x)[node->node.bit >> 3] >>
85
(~node->node.bit & 7)) & 1];
89
/* Insert ebmb_node <new> into subtree starting at node root <root>. Only
90
* new->key needs be set with the zero-terminated string key. The ebmb_node is
91
* returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
92
* caller is responsible for properly terminating the key with a zero.
94
static forceinline struct ebmb_node *
95
__ebst_insert(struct eb_root *root, struct ebmb_node *new)
97
struct ebmb_node *old;
100
eb_troot_t *root_right = root;
105
troot = root->b[EB_LEFT];
106
root_right = root->b[EB_RGHT];
107
if (unlikely(troot == NULL)) {
108
/* Tree is empty, insert the leaf part below the left branch */
109
root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
110
new->node.leaf_p = eb_dotag(root, EB_LEFT);
111
new->node.node_p = NULL; /* node part unused */
115
/* The tree descent is fairly easy :
116
* - first, check if we have reached a leaf node
117
* - second, check if we have gone too far
119
* Everywhere, we use <new> for the node node we are inserting, <root>
120
* for the node we attach it to, and <old> for the node we are
121
* displacing below <new>. <troot> will always point to the future node
122
* (tagged with its type). <side> carries the side the node <new> is
123
* attached to below its parent, which is also where previous node
129
if (unlikely(eb_gettag(troot) == EB_LEAF)) {
130
eb_troot_t *new_left, *new_rght;
131
eb_troot_t *new_leaf, *old_leaf;
133
old = container_of(eb_untag(troot, EB_LEAF),
134
struct ebmb_node, node.branches);
136
new_left = eb_dotag(&new->node.branches, EB_LEFT);
137
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
138
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
139
old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
141
new->node.node_p = old->node.leaf_p;
143
/* Right here, we have 3 possibilities :
144
* - the tree does not contain the key, and we have
145
* new->key < old->key. We insert new above old, on
148
* - the tree does not contain the key, and we have
149
* new->key > old->key. We insert new above old, on
152
* - the tree does contain the key, which implies it
153
* is alone. We add the new key next to it as a
156
* The last two cases can easily be partially merged.
158
bit = string_equal_bits(new->key, old->key, bit);
159
diff = cmp_bits(new->key, old->key, bit);
162
new->node.leaf_p = new_left;
163
old->node.leaf_p = new_rght;
164
new->node.branches.b[EB_LEFT] = new_leaf;
165
new->node.branches.b[EB_RGHT] = old_leaf;
167
/* we may refuse to duplicate this key if the tree is
168
* tagged as containing only unique keys.
170
if (diff == 0 && eb_gettag(root_right))
173
/* new->key >= old->key, new goes the right */
174
old->node.leaf_p = new_left;
175
new->node.leaf_p = new_rght;
176
new->node.branches.b[EB_LEFT] = old_leaf;
177
new->node.branches.b[EB_RGHT] = new_leaf;
181
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
188
/* OK we're walking down this link */
189
old = container_of(eb_untag(troot, EB_NODE),
190
struct ebmb_node, node.branches);
192
/* Stop going down when we don't have common bits anymore. We
193
* also stop in front of a duplicates tree because it means we
194
* have to insert above. Note: we can compare more bits than
195
* the current node's because as long as they are identical, we
196
* know we descend along the correct side.
198
if (old->node.bit < 0) {
199
/* we're above a duplicate tree, we must compare till the end */
200
bit = string_equal_bits(new->key, old->key, bit);
203
else if (bit < old->node.bit) {
204
bit = string_equal_bits(new->key, old->key, bit);
207
if (bit < old->node.bit) { /* we don't have all bits in common */
208
/* The tree did not contain the key, so we insert <new> before the node
209
* <old>, and set ->bit to designate the lowest bit position in <new>
210
* which applies to ->branches.b[].
212
eb_troot_t *new_left, *new_rght;
213
eb_troot_t *new_leaf, *old_node;
215
new_left = eb_dotag(&new->node.branches, EB_LEFT);
216
new_rght = eb_dotag(&new->node.branches, EB_RGHT);
217
new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
218
old_node = eb_dotag(&old->node.branches, EB_NODE);
220
new->node.node_p = old->node.node_p;
222
diff = cmp_bits(new->key, old->key, bit);
224
new->node.leaf_p = new_left;
225
old->node.node_p = new_rght;
226
new->node.branches.b[EB_LEFT] = new_leaf;
227
new->node.branches.b[EB_RGHT] = old_node;
230
old->node.node_p = new_left;
231
new->node.leaf_p = new_rght;
232
new->node.branches.b[EB_LEFT] = old_node;
233
new->node.branches.b[EB_RGHT] = new_leaf;
237
ret = eb_insert_dup(&old->node, &new->node);
238
return container_of(ret, struct ebmb_node, node);
244
root = &old->node.branches;
245
side = (new->key[old->node.bit >> 3] >> (~old->node.bit & 7)) & 1;
246
troot = root->b[side];
249
/* Ok, now we are inserting <new> between <root> and <old>. <old>'s
250
* parent is already set to <new>, and the <root>'s branch is still in
251
* <side>. Update the root's leaf till we have it. Note that we can also
252
* find the side by checking the side of new->node.node_p.
255
/* We need the common higher bits between new->key and old->key.
256
* This number of bits is already in <bit>.
259
root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
263
#endif /* _EBSTTREE_H */