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