blob: f15af38fde801f5ecd04bb21447ac6ae1052e9a5 [file] [log] [blame]
Willy Tarreauc2186022009-10-26 19:48:54 +01001/*
2 * Elastic Binary Trees - macros to manipulate String data nodes.
Willy Tarreau3a932442010-05-09 19:29:23 +02003 * Version 6.0
4 * (C) 2002-2010 - Willy Tarreau <w@1wt.eu>
Willy Tarreauc2186022009-10-26 19:48:54 +01005 *
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;
Willy Tarreau3a932442010-05-09 19:29:23 +020044 int bit;
45 int node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010046
47 troot = root->b[EB_LEFT];
48 if (unlikely(troot == NULL))
49 return NULL;
50
51 bit = 0;
52 while (1) {
53 if ((eb_gettag(troot) == EB_LEAF)) {
54 node = container_of(eb_untag(troot, EB_LEAF),
55 struct ebmb_node, node.branches);
Willy Tarreau4c848222009-10-29 12:00:11 +010056 if (strcmp((char *)node->key, x) == 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010057 return node;
58 else
59 return NULL;
60 }
61 node = container_of(eb_untag(troot, EB_NODE),
62 struct ebmb_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +020063 node_bit = node->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +010064
Willy Tarreau3a932442010-05-09 19:29:23 +020065 if (node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +010066 /* We have a dup tree now. Either it's for the same
67 * value, and we walk down left, or it's a different
68 * one and we don't have our key.
69 */
Willy Tarreau4c848222009-10-29 12:00:11 +010070 if (strcmp((char *)node->key, x) != 0)
Willy Tarreauc2186022009-10-26 19:48:54 +010071 return NULL;
72
73 troot = node->node.branches.b[EB_LEFT];
74 while (eb_gettag(troot) != EB_LEAF)
75 troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
76 node = container_of(eb_untag(troot, EB_LEAF),
77 struct ebmb_node, node.branches);
78 return node;
79 }
80
81 /* OK, normal data node, let's walk down */
82 bit = string_equal_bits(x, node->key, bit);
Willy Tarreau3a932442010-05-09 19:29:23 +020083 if (bit < node_bit)
Willy Tarreauc2186022009-10-26 19:48:54 +010084 return NULL; /* no more common bits */
85
Willy Tarreau3a932442010-05-09 19:29:23 +020086 troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
87 (~node_bit & 7)) & 1];
Willy Tarreauc2186022009-10-26 19:48:54 +010088 }
89}
90
91/* Insert ebmb_node <new> into subtree starting at node root <root>. Only
92 * new->key needs be set with the zero-terminated string key. The ebmb_node is
93 * returned. If root->b[EB_RGHT]==1, the tree may only contain unique keys. The
94 * caller is responsible for properly terminating the key with a zero.
95 */
96static forceinline struct ebmb_node *
97__ebst_insert(struct eb_root *root, struct ebmb_node *new)
98{
99 struct ebmb_node *old;
100 unsigned int side;
101 eb_troot_t *troot;
102 eb_troot_t *root_right = root;
103 int diff;
104 int bit;
Willy Tarreau3a932442010-05-09 19:29:23 +0200105 int old_node_bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100106
107 side = EB_LEFT;
108 troot = root->b[EB_LEFT];
109 root_right = root->b[EB_RGHT];
110 if (unlikely(troot == NULL)) {
111 /* Tree is empty, insert the leaf part below the left branch */
112 root->b[EB_LEFT] = eb_dotag(&new->node.branches, EB_LEAF);
113 new->node.leaf_p = eb_dotag(root, EB_LEFT);
114 new->node.node_p = NULL; /* node part unused */
115 return new;
116 }
117
118 /* The tree descent is fairly easy :
119 * - first, check if we have reached a leaf node
120 * - second, check if we have gone too far
121 * - third, reiterate
122 * Everywhere, we use <new> for the node node we are inserting, <root>
123 * for the node we attach it to, and <old> for the node we are
124 * displacing below <new>. <troot> will always point to the future node
125 * (tagged with its type). <side> carries the side the node <new> is
126 * attached to below its parent, which is also where previous node
127 * was attached.
128 */
129
130 bit = 0;
131 while (1) {
132 if (unlikely(eb_gettag(troot) == EB_LEAF)) {
133 eb_troot_t *new_left, *new_rght;
134 eb_troot_t *new_leaf, *old_leaf;
135
136 old = container_of(eb_untag(troot, EB_LEAF),
137 struct ebmb_node, node.branches);
138
139 new_left = eb_dotag(&new->node.branches, EB_LEFT);
140 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
141 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
142 old_leaf = eb_dotag(&old->node.branches, EB_LEAF);
143
144 new->node.node_p = old->node.leaf_p;
145
146 /* Right here, we have 3 possibilities :
147 * - the tree does not contain the key, and we have
148 * new->key < old->key. We insert new above old, on
149 * the left ;
150 *
151 * - the tree does not contain the key, and we have
152 * new->key > old->key. We insert new above old, on
153 * the right ;
154 *
155 * - the tree does contain the key, which implies it
156 * is alone. We add the new key next to it as a
157 * first duplicate.
158 *
159 * The last two cases can easily be partially merged.
160 */
161 bit = string_equal_bits(new->key, old->key, bit);
162 diff = cmp_bits(new->key, old->key, bit);
163
164 if (diff < 0) {
165 new->node.leaf_p = new_left;
166 old->node.leaf_p = new_rght;
167 new->node.branches.b[EB_LEFT] = new_leaf;
168 new->node.branches.b[EB_RGHT] = old_leaf;
169 } else {
170 /* we may refuse to duplicate this key if the tree is
171 * tagged as containing only unique keys.
172 */
173 if (diff == 0 && eb_gettag(root_right))
174 return old;
175
176 /* new->key >= old->key, new goes the right */
177 old->node.leaf_p = new_left;
178 new->node.leaf_p = new_rght;
179 new->node.branches.b[EB_LEFT] = old_leaf;
180 new->node.branches.b[EB_RGHT] = new_leaf;
181
182 if (diff == 0) {
183 new->node.bit = -1;
184 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
185 return new;
186 }
187 }
188 break;
189 }
190
191 /* OK we're walking down this link */
192 old = container_of(eb_untag(troot, EB_NODE),
193 struct ebmb_node, node.branches);
Willy Tarreau3a932442010-05-09 19:29:23 +0200194 old_node_bit = old->node.bit;
Willy Tarreauc2186022009-10-26 19:48:54 +0100195
196 /* Stop going down when we don't have common bits anymore. We
197 * also stop in front of a duplicates tree because it means we
198 * have to insert above. Note: we can compare more bits than
199 * the current node's because as long as they are identical, we
200 * know we descend along the correct side.
201 */
Willy Tarreau3a932442010-05-09 19:29:23 +0200202 if (old_node_bit < 0) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100203 /* we're above a duplicate tree, we must compare till the end */
204 bit = string_equal_bits(new->key, old->key, bit);
205 goto dup_tree;
206 }
Willy Tarreau3a932442010-05-09 19:29:23 +0200207 else if (bit < old_node_bit) {
Willy Tarreauc2186022009-10-26 19:48:54 +0100208 bit = string_equal_bits(new->key, old->key, bit);
209 }
210
Willy Tarreau3a932442010-05-09 19:29:23 +0200211 if (bit < old_node_bit) { /* we don't have all bits in common */
Willy Tarreauc2186022009-10-26 19:48:54 +0100212 /* The tree did not contain the key, so we insert <new> before the node
213 * <old>, and set ->bit to designate the lowest bit position in <new>
214 * which applies to ->branches.b[].
215 */
216 eb_troot_t *new_left, *new_rght;
217 eb_troot_t *new_leaf, *old_node;
218 dup_tree:
219 new_left = eb_dotag(&new->node.branches, EB_LEFT);
220 new_rght = eb_dotag(&new->node.branches, EB_RGHT);
221 new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
222 old_node = eb_dotag(&old->node.branches, EB_NODE);
223
224 new->node.node_p = old->node.node_p;
225
226 diff = cmp_bits(new->key, old->key, bit);
227 if (diff < 0) {
228 new->node.leaf_p = new_left;
229 old->node.node_p = new_rght;
230 new->node.branches.b[EB_LEFT] = new_leaf;
231 new->node.branches.b[EB_RGHT] = old_node;
232 }
233 else if (diff > 0) {
234 old->node.node_p = new_left;
235 new->node.leaf_p = new_rght;
236 new->node.branches.b[EB_LEFT] = old_node;
237 new->node.branches.b[EB_RGHT] = new_leaf;
238 }
239 else {
240 struct eb_node *ret;
241 ret = eb_insert_dup(&old->node, &new->node);
242 return container_of(ret, struct ebmb_node, node);
243 }
244 break;
245 }
246
247 /* walk down */
248 root = &old->node.branches;
Willy Tarreau3a932442010-05-09 19:29:23 +0200249 side = (new->key[old_node_bit >> 3] >> (~old_node_bit & 7)) & 1;
Willy Tarreauc2186022009-10-26 19:48:54 +0100250 troot = root->b[side];
251 }
252
253 /* Ok, now we are inserting <new> between <root> and <old>. <old>'s
254 * parent is already set to <new>, and the <root>'s branch is still in
255 * <side>. Update the root's leaf till we have it. Note that we can also
256 * find the side by checking the side of new->node.node_p.
257 */
258
259 /* We need the common higher bits between new->key and old->key.
260 * This number of bits is already in <bit>.
261 */
262 new->node.bit = bit;
263 root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
264 return new;
265}
266
Willy Tarreau9e2e39e2009-11-02 14:43:39 +0100267#endif /* _EBSTTREE_H */
268