blob: 87e0306e0de3c095cba6a48cbbb01beb63549687 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate String data nodes.
3 * Version 5.0
4 * (C) 2002-2009 - Willy Tarreau <w@1wt.eu>
5 *
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
21/* These functions and macros rely on Multi-Byte nodes */
22
Willy Tarreau9e2e39e2009-11-02 14:43:39 +010023#ifndef _EBSTTREE_H
24#define _EBSTTREE_H
25
Willy Tarreauc2186022009-10-26 19:48:54 +010026#include "ebtree.h"
27#include "ebmbtree.h"
28
29/* The following functions are not inlined by default. They are declared
30 * in ebsttree.c, which simply relies on their inline version.
31 */
32REGPRM2 struct ebmb_node *ebst_lookup(struct eb_root *root, const char *x);
33REGPRM2 struct ebmb_node *ebst_insert(struct eb_root *root, struct ebmb_node *new);
34
35/* Find the first occurence of a zero-terminated string <x> in the tree <root>.
36 * It's the caller's reponsibility to use this function only on trees which
37 * only contain zero-terminated strings. If none can be found, return NULL.
38 */
39static forceinline struct ebmb_node *__ebst_lookup(struct eb_root *root, const void *x)
40{
41 struct ebmb_node *node;
42 eb_troot_t *troot;
43 unsigned int bit;
44
45 troot = root->b[EB_LEFT];
46 if (unlikely(troot == NULL))
47 return NULL;
48
49 bit = 0;
50 while (1) {
51 if ((eb_gettag(troot) == EB_LEAF)) {
52 node = container_of(eb_untag(troot, EB_LEAF),
53 struct ebmb_node, node.branches);
Willy Tarreau4c848222009-10-29 12:00:11 +010054 if (strcmp((char *)node->key, x) == 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010055 return node;
56 else
57 return NULL;
58 }
59 node = container_of(eb_untag(troot, EB_NODE),
60 struct ebmb_node, node.branches);
61
62 if (node->node.bit < 0) {
63 /* We have a dup tree now. Either it's for the same
64 * value, and we walk down left, or it's a different
65 * one and we don't have our key.
66 */
Willy Tarreau4c848222009-10-29 12:00:11 +010067 if (strcmp((char *)node->key, x) != 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010068 return NULL;
69
70 troot = node->node.branches.b[EB_LEFT];
71 while (eb_gettag(troot) != EB_LEAF)
72 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
73 node = container_of(eb_untag(troot, EB_LEAF),
74 struct ebmb_node, node.branches);
75 return node;
76 }
77
78 /* OK, normal data node, let's walk down */
79 bit = string_equal_bits(x, node->key, bit);
80 if (bit < node->node.bit)
81 return NULL; /* no more common bits */
82
83 troot = node->node.branches.b[(((unsigned char*)x)[node->node.bit >> 3] >>
84 (~node->node.bit & 7)) & 1];
85 }
86}
87
88/* Insert ebmb_node <new> into subtree starting at node root <root>. Only
89 * new->key needs be set with the zero-terminated string key. The ebmb_node is
90 * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
91 * caller is responsible for properly terminating the key with a zero.
92 */
93static forceinline struct ebmb_node *
94__ebst_insert(struct eb_root *root, struct ebmb_node *new)
95{
96 struct ebmb_node *old;
97 unsigned int side;
98 eb_troot_t *troot;
99 eb_troot_t *root_right = root;
100 int diff;
101 int bit;
102
103 side = EB_LEFT;
104 troot = root->b[EB_LEFT];
105 root_right = root->b[EB_RGHT];
106 if (unlikely(troot == NULL)) {
107 /* Tree is empty, insert the leaf part below the left branch */
108 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
109 new->node.leaf_p = eb_dotag(root, EB_LEFT);
110 new->node.node_p = NULL; /* node part unused */
111 return new;
112 }
113
114 /* The tree descent is fairly easy :
115 * - first, check if we have reached a leaf node
116 * - second, check if we have gone too far
117 * - third, reiterate
118 * Everywhere, we use <new> for the node node we are inserting, <root>
119 * for the node we attach it to, and <old> for the node we are
120 * displacing below <new>. <troot> will always point to the future node
121 * (tagged with its type). <side> carries the side the node <new> is
122 * attached to below its parent, which is also where previous node
123 * was attached.
124 */
125
126 bit = 0;
127 while (1) {
128 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
129 eb_troot_t *new_left, *new_rght;
130 eb_troot_t *new_leaf, *old_leaf;
131
132 old = container_of(eb_untag(troot, EB_LEAF),
133 struct ebmb_node, node.branches);
134
135 new_left = eb_dotag(&new->node.branches, EB_LEFT);
136 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
137 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
138 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
139
140 new->node.node_p = old->node.leaf_p;
141
142 /* Right here, we have 3 possibilities :
143 * - the tree does not contain the key, and we have
144 * new->key < old->key. We insert new above old, on
145 * the left ;
146 *
147 * - the tree does not contain the key, and we have
148 * new->key > old->key. We insert new above old, on
149 * the right ;
150 *
151 * - the tree does contain the key, which implies it
152 * is alone. We add the new key next to it as a
153 * first duplicate.
154 *
155 * The last two cases can easily be partially merged.
156 */
157 bit = string_equal_bits(new->key, old->key, bit);
158 diff = cmp_bits(new->key, old->key, bit);
159
160 if (diff < 0) {
161 new->node.leaf_p = new_left;
162 old->node.leaf_p = new_rght;
163 new->node.branches.b[EB_LEFT] = new_leaf;
164 new->node.branches.b[EB_RGHT] = old_leaf;
165 } else {
166 /* we may refuse to duplicate this key if the tree is
167 * tagged as containing only unique keys.
168 */
169 if (diff == 0 && eb_gettag(root_right))
170 return old;
171
172 /* new->key >= old->key, new goes the right */
173 old->node.leaf_p = new_left;
174 new->node.leaf_p = new_rght;
175 new->node.branches.b[EB_LEFT] = old_leaf;
176 new->node.branches.b[EB_RGHT] = new_leaf;
177
178 if (diff == 0) {
179 new->node.bit = -1;
180 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
181 return new;
182 }
183 }
184 break;
185 }
186
187 /* OK we're walking down this link */
188 old = container_of(eb_untag(troot, EB_NODE),
189 struct ebmb_node, node.branches);
190
191 /* Stop going down when we don't have common bits anymore. We
192 * also stop in front of a duplicates tree because it means we
193 * have to insert above. Note: we can compare more bits than
194 * the current node's because as long as they are identical, we
195 * know we descend along the correct side.
196 */
197 if (old->node.bit < 0) {
198 /* we're above a duplicate tree, we must compare till the end */
199 bit = string_equal_bits(new->key, old->key, bit);
200 goto dup_tree;
201 }
202 else if (bit < old->node.bit) {
203 bit = string_equal_bits(new->key, old->key, bit);
204 }
205
206 if (bit < old->node.bit) { /* we don't have all bits in common */
207 /* The tree did not contain the key, so we insert <new> before the node
208 * <old>, and set ->bit to designate the lowest bit position in <new>
209 * which applies to ->branches.b[].
210 */
211 eb_troot_t *new_left, *new_rght;
212 eb_troot_t *new_leaf, *old_node;
213 dup_tree:
214 new_left = eb_dotag(&new->node.branches, EB_LEFT);
215 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
216 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
217 old_node = eb_dotag(&old->node.branches, EB_NODE);
218
219 new->node.node_p = old->node.node_p;
220
221 diff = cmp_bits(new->key, old->key, bit);
222 if (diff < 0) {
223 new->node.leaf_p = new_left;
224 old->node.node_p = new_rght;
225 new->node.branches.b[EB_LEFT] = new_leaf;
226 new->node.branches.b[EB_RGHT] = old_node;
227 }
228 else if (diff > 0) {
229 old->node.node_p = new_left;
230 new->node.leaf_p = new_rght;
231 new->node.branches.b[EB_LEFT] = old_node;
232 new->node.branches.b[EB_RGHT] = new_leaf;
233 }
234 else {
235 struct eb_node *ret;
236 ret = eb_insert_dup(&old->node, &new->node);
237 return container_of(ret, struct ebmb_node, node);
238 }
239 break;
240 }
241
242 /* walk down */
243 root = &old->node.branches;
244 side = (new->key[old->node.bit >> 3] >> (~old->node.bit & 7)) & 1;
245 troot = root->b[side];
246 }
247
248 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
249 * parent is already set to <new>, and the <root>'s branch is still in
250 * <side>. Update the root's leaf till we have it. Note that we can also
251 * find the side by checking the side of new->node.node_p.
252 */
253
254 /* We need the common higher bits between new->key and old->key.
255 * This number of bits is already in <bit>.
256 */
257 new->node.bit = bit;
258 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
259 return new;
260}
261
Willy Tarreau9e2e39e2009-11-02 14:43:39 +0100262#endif /* _EBSTTREE_H */
263