~ubuntu-branches/ubuntu/utopic/haproxy/utopic-proposed

« back to all changes in this revision

Viewing changes to ebtree/ebsttree.h

  • Committer: Bazaar Package Importer
  • Author(s): Christo Buschek
  • Date: 2011-03-11 12:41:59 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110311124159-9foyp4juf1ilqipo
Tags: 1.4.13-1
* New maintainer upload (Closes: #615246)
* New upstream release
* Standards-version goes 3.9.1 (no change)
* Added patch bashism (Closes: #581109)
* Added a README.source file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
                        return node;
79
79
                }
80
80
 
81
 
                /* OK, normal data node, let's walk down */
82
 
                bit = string_equal_bits(x, node->key, bit);
83
 
                if (bit < node_bit)
84
 
                        return NULL; /* no more common bits */
 
81
                /* OK, normal data node, let's walk down but don't compare data
 
82
                 * if we already reached the end of the key.
 
83
                 */
 
84
                if (likely(bit >= 0)) {
 
85
                        bit = string_equal_bits(x, node->key, bit);
 
86
                        if (likely(bit < node_bit)) {
 
87
                                if (bit >= 0)
 
88
                                        return NULL; /* no more common bits */
 
89
 
 
90
                                /* bit < 0 : we reached the end of the key. If we
 
91
                                 * are in a tree with unique keys, we can return
 
92
                                 * this node. Otherwise we have to walk it down
 
93
                                 * and stop comparing bits.
 
94
                                 */
 
95
                                if (eb_gettag(root->b[EB_RGHT]))
 
96
                                        return node;
 
97
                        }
 
98
                }
85
99
 
86
100
                troot = node->node.branches.b[(((unsigned char*)x)[node_bit >> 3] >>
87
101
                                               (~node_bit & 7)) & 1];
158
172
                         *
159
173
                         * The last two cases can easily be partially merged.
160
174
                         */
161
 
                        bit = string_equal_bits(new->key, old->key, bit);
 
175
                        if (bit >= 0)
 
176
                                bit = string_equal_bits(new->key, old->key, bit);
 
177
 
 
178
                        if (bit < 0) {
 
179
                                /* key was already there */
 
180
 
 
181
                                /* we may refuse to duplicate this key if the tree is
 
182
                                 * tagged as containing only unique keys.
 
183
                                 */
 
184
                                if (eb_gettag(root_right))
 
185
                                        return old;
 
186
 
 
187
                                /* new arbitrarily goes to the right and tops the dup tree */
 
188
                                old->node.leaf_p = new_left;
 
189
                                new->node.leaf_p = new_rght;
 
190
                                new->node.branches.b[EB_LEFT] = old_leaf;
 
191
                                new->node.branches.b[EB_RGHT] = new_leaf;
 
192
                                new->node.bit = -1;
 
193
                                root->b[side] = eb_dotag(&new->node.branches, EB_NODE);
 
194
                                return new;
 
195
                        }
 
196
 
162
197
                        diff = cmp_bits(new->key, old->key, bit);
163
 
 
164
198
                        if (diff < 0) {
 
199
                                /* new->key < old->key, new takes the left */
165
200
                                new->node.leaf_p = new_left;
166
201
                                old->node.leaf_p = new_rght;
167
202
                                new->node.branches.b[EB_LEFT] = new_leaf;
168
203
                                new->node.branches.b[EB_RGHT] = old_leaf;
169
204
                        } 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 */
 
205
                                /* new->key > old->key, new takes the right */
177
206
                                old->node.leaf_p = new_left;
178
207
                                new->node.leaf_p = new_rght;
179
208
                                new->node.branches.b[EB_LEFT] = old_leaf;
180
209
                                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
210
                        }
188
211
                        break;
189
212
                }
199
222
                 * the current node's because as long as they are identical, we
200
223
                 * know we descend along the correct side.
201
224
                 */
202
 
                if (old_node_bit < 0) {
203
 
                        /* 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
 
                }
207
 
                else if (bit < old_node_bit) {
208
 
                        bit = string_equal_bits(new->key, old->key, bit);
209
 
                }
 
225
                if (bit >= 0 && (bit < old_node_bit || old_node_bit < 0))
 
226
                        bit = string_equal_bits(new->key, old->key, bit);
210
227
 
211
 
                if (bit < old_node_bit) { /* we don't have all bits in common */
212
 
                        /* 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[].
 
228
                if (unlikely(bit < 0)) {
 
229
                        /* Perfect match, we must only stop on head of dup tree
 
230
                         * or walk down to a leaf.
 
231
                         */
 
232
                        if (old_node_bit < 0) {
 
233
                                /* We know here that string_equal_bits matched all
 
234
                                 * bits and that we're on top of a dup tree, then
 
235
                                 * we can perform the dup insertion and return.
 
236
                                 */
 
237
                                struct eb_node *ret;
 
238
                                ret = eb_insert_dup(&old->node, &new->node);
 
239
                                return container_of(ret, struct ebmb_node, node);
 
240
                        }
 
241
                        /* OK so let's walk down */
 
242
                }
 
243
                else if (bit < old_node_bit || old_node_bit < 0) {
 
244
                        /* The tree did not contain the key, or we stopped on top of a dup
 
245
                         * tree, possibly containing the key. In the former case, we insert
 
246
                         * <new> before the node <old>, and set ->bit to designate the lowest
 
247
                         * bit position in <new> which applies to ->branches.b[]. In the later
 
248
                         * case, we add the key to the existing dup tree. Note that we cannot
 
249
                         * enter here if we match an intermediate node's key that is not the
 
250
                         * head of a dup tree.
215
251
                         */
216
252
                        eb_troot_t *new_left, *new_rght;
217
253
                        eb_troot_t *new_leaf, *old_node;
218
 
                dup_tree:
 
254
 
219
255
                        new_left = eb_dotag(&new->node.branches, EB_LEFT);
220
256
                        new_rght = eb_dotag(&new->node.branches, EB_RGHT);
221
257
                        new_leaf = eb_dotag(&new->node.branches, EB_LEAF);
223
259
 
224
260
                        new->node.node_p = old->node.node_p;
225
261
 
 
262
                        /* we can never match all bits here */
226
263
                        diff = cmp_bits(new->key, old->key, bit);
227
264
                        if (diff < 0) {
228
265
                                new->node.leaf_p = new_left;
230
267
                                new->node.branches.b[EB_LEFT] = new_leaf;
231
268
                                new->node.branches.b[EB_RGHT] = old_node;
232
269
                        }
233
 
                        else if (diff > 0) {
 
270
                        else {
234
271
                                old->node.node_p = new_left;
235
272
                                new->node.leaf_p = new_rght;
236
273
                                new->node.branches.b[EB_LEFT] = old_node;
237
274
                                new->node.branches.b[EB_RGHT] = new_leaf;
238
275
                        }
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
276
                        break;
245
277
                }
246
278
 
258
290
 
259
291
        /* We need the common higher bits between new->key and old->key.
260
292
         * This number of bits is already in <bit>.
 
293
         * NOTE: we can't get here whit bit < 0 since we found a dup !
261
294
         */
262
295
        new->node.bit = bit;
263
296
        root->b[side] = eb_dotag(&new->node.branches, EB_NODE);