blob: 6fa6412f2d16b07cd498ecafcaad8ce6b6cbe761 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate String data nodes.
Willy Tarreauc9a31da2009-12-14 12:40:27 +01003 * Version 5.1
Willy Tarreauc2186022009-10-26 19:48:54 +01004 * (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);
Willy Tarreauc9a31da2009-12-14 12:40:27 +010033REGPRM3 struct ebmb_node *ebst_lookup_len(struct eb_root *root, const char *x, unsigned int len);
Willy Tarreauc2186022009-10-26 19:48:54 +010034REGPRM2 struct ebmb_node *ebst_insert(struct eb_root *root, struct ebmb_node *new);
35
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.
39 */
40static forceinline struct ebmb_node *__ebst_lookup(struct eb_root *root, const void *x)
41{
42 struct ebmb_node *node;
43 eb_troot_t *troot;
44 unsigned int bit;
45
46 troot = root->b[EB_LEFT];
47 if (unlikely(troot == NULL))
48 return NULL;
49
50 bit = 0;
51 while (1) {
52 if ((eb_gettag(troot) == EB_LEAF)) {
53 node = container_of(eb_untag(troot, EB_LEAF),
54 struct ebmb_node, node.branches);
Willy Tarreau4c848222009-10-29 12:00:11 +010055 if (strcmp((char *)node->key, x) == 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010056 return node;
57 else
58 return NULL;
59 }
60 node = container_of(eb_untag(troot, EB_NODE),
61 struct ebmb_node, node.branches);
62
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.
67 */
Willy Tarreau4c848222009-10-29 12:00:11 +010068 if (strcmp((char *)node->key, x) != 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010069 return NULL;
70
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);
76 return node;
77 }
78
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 */
83
84 troot = node->node.branches.b[(((unsigned char*)x)[node->node.bit >> 3] >>
85 (~node->node.bit & 7)) & 1];
86 }
87}
88
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.
93 */
94static forceinline struct ebmb_node *
95__ebst_insert(struct eb_root *root, struct ebmb_node *new)
96{
97 struct ebmb_node *old;
98 unsigned int side;
99 eb_troot_t *troot;
100 eb_troot_t *root_right = root;
101 int diff;
102 int bit;
103
104 side = EB_LEFT;
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 */
112 return new;
113 }
114
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
118 * - third, reiterate
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
124 * was attached.
125 */
126
127 bit = 0;
128 while (1) {
129 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
130 eb_troot_t *new_left, *new_rght;
131 eb_troot_t *new_leaf, *old_leaf;
132
133 old = container_of(eb_untag(troot, EB_LEAF),
134 struct ebmb_node, node.branches);
135
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);
140
141 new->node.node_p = old->node.leaf_p;
142
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
146 * the left ;
147 *
148 * - the tree does not contain the key, and we have
149 * new->key > old->key. We insert new above old, on
150 * the right ;
151 *
152 * - the tree does contain the key, which implies it
153 * is alone. We add the new key next to it as a
154 * first duplicate.
155 *
156 * The last two cases can easily be partially merged.
157 */
158 bit = string_equal_bits(new->key, old->key, bit);
159 diff = cmp_bits(new->key, old->key, bit);
160
161 if (diff < 0) {
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;
166 } else {
167 /* we may refuse to duplicate this key if the tree is
168 * tagged as containing only unique keys.
169 */
170 if (diff == 0 && eb_gettag(root_right))
171 return old;
172
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;
178
179 if (diff == 0) {
180 new->node.bit = -1;
181 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
182 return new;
183 }
184 }
185 break;
186 }
187
188 /* OK we're walking down this link */
189 old = container_of(eb_untag(troot, EB_NODE),
190 struct ebmb_node, node.branches);
191
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.
197 */
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);
201 goto dup_tree;
202 }
203 else if (bit < old->node.bit) {
204 bit = string_equal_bits(new->key, old->key, bit);
205 }
206
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[].
211 */
212 eb_troot_t *new_left, *new_rght;
213 eb_troot_t *new_leaf, *old_node;
214 dup_tree:
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);
219
220 new->node.node_p = old->node.node_p;
221
222 diff = cmp_bits(new->key, old->key, bit);
223 if (diff < 0) {
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;
228 }
229 else if (diff > 0) {
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;
234 }
235 else {
236 struct eb_node *ret;
237 ret = eb_insert_dup(&old->node, &new->node);
238 return container_of(ret, struct ebmb_node, node);
239 }
240 break;
241 }
242
243 /* walk down */
244 root = &old->node.branches;
245 side = (new->key[old->node.bit >> 3] >> (~old->node.bit & 7)) & 1;
246 troot = root->b[side];
247 }
248
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.
253 */
254
255 /* We need the common higher bits between new->key and old->key.
256 * This number of bits is already in <bit>.
257 */
258 new->node.bit = bit;
259 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
260 return new;
261}
262
Willy Tarreau9e2e39e2009-11-02 14:43:39 +0100263#endif /* _EBSTTREE_H */
264