-
Notifications
You must be signed in to change notification settings - Fork 8
/
eb64tree.c
224 lines (202 loc) · 7.25 KB
/
eb64tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Elastic Binary Trees - exported functions for operations on 64bit nodes.
*
* Copyright (C) 2000-2015 Willy Tarreau - [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/* Consult eb64tree.h for more details about those functions */
#include "eb64tree.h"
struct eb64_node *eb64_insert(struct eb_root *root, struct eb64_node *new)
{
return __eb64_insert(root, new);
}
struct eb64_node *eb64i_insert(struct eb_root *root, struct eb64_node *new)
{
return __eb64i_insert(root, new);
}
struct eb64_node *eb64_lookup(struct eb_root *root, u64 x)
{
return __eb64_lookup(root, x);
}
struct eb64_node *eb64i_lookup(struct eb_root *root, s64 x)
{
return __eb64i_lookup(root, x);
}
/*
* Find the last occurrence of the highest key in the tree <root>, which is
* equal to or less than <x>. NULL is returned is no key matches.
*/
struct eb64_node *eb64_lookup_le(struct eb_root *root, u64 x)
{
struct eb64_node *node;
eb_troot_t *troot;
troot = root->b[EB_LEFT];
if (unlikely(troot == NULL))
return NULL;
while (1) {
if ((eb_gettag(troot) == EB_LEAF)) {
/* We reached a leaf, which means that the whole upper
* parts were common. We will return either the current
* node or its next one if the former is too small.
*/
node = container_of(eb_untag(troot, EB_LEAF),
struct eb64_node, node.branches);
if (node->key <= x)
return node;
/* return prev */
troot = node->node.leaf_p;
break;
}
node = container_of(eb_untag(troot, EB_NODE),
struct eb64_node, node.branches);
if (node->node.bit < 0) {
/* We're at the top of a dup tree. Either we got a
* matching value and we return the rightmost node, or
* we don't and we skip the whole subtree to return the
* prev node before the subtree. Note that since we're
* at the top of the dup tree, we can simply return the
* prev node without first trying to escape from the
* tree.
*/
if (node->key <= x) {
troot = node->node.branches.b[EB_RGHT];
while (eb_gettag(troot) != EB_LEAF)
troot = (eb_untag(troot, EB_NODE))->b[EB_RGHT];
return container_of(eb_untag(troot, EB_LEAF),
struct eb64_node, node.branches);
}
/* return prev */
troot = node->node.node_p;
break;
}
if (((x ^ node->key) >> node->node.bit) >= EB_NODE_BRANCHES) {
/* No more common bits at all. Either this node is too
* small and we need to get its highest value, or it is
* too large, and we need to get the prev value.
*/
if ((node->key >> node->node.bit) < (x >> node->node.bit)) {
troot = node->node.branches.b[EB_RGHT];
return eb64_entry(eb_walk_down(troot, EB_RGHT), struct eb64_node, node);
}
/* Further values will be too high here, so return the prev
* unique node (if it exists).
*/
troot = node->node.node_p;
break;
}
troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
}
/* If we get here, it means we want to report previous node before the
* current one which is not above. <troot> is already initialised to
* the parent's branches.
*/
while (eb_gettag(troot) == EB_LEFT) {
/* Walking up from left branch. We must ensure that we never
* walk beyond root.
*/
if (unlikely(eb_clrtag((eb_untag(troot, EB_LEFT))->b[EB_RGHT]) == NULL))
return NULL;
troot = (eb_root_to_node(eb_untag(troot, EB_LEFT)))->node_p;
}
/* Note that <troot> cannot be NULL at this stage */
troot = (eb_untag(troot, EB_RGHT))->b[EB_LEFT];
node = eb64_entry(eb_walk_down(troot, EB_RGHT), struct eb64_node, node);
return node;
}
/*
* Find the first occurrence of the lowest key in the tree <root>, which is
* equal to or greater than <x>. NULL is returned is no key matches.
*/
struct eb64_node *eb64_lookup_ge(struct eb_root *root, u64 x)
{
struct eb64_node *node;
eb_troot_t *troot;
troot = root->b[EB_LEFT];
if (unlikely(troot == NULL))
return NULL;
while (1) {
if ((eb_gettag(troot) == EB_LEAF)) {
/* We reached a leaf, which means that the whole upper
* parts were common. We will return either the current
* node or its next one if the former is too small.
*/
node = container_of(eb_untag(troot, EB_LEAF),
struct eb64_node, node.branches);
if (node->key >= x)
return node;
/* return next */
troot = node->node.leaf_p;
break;
}
node = container_of(eb_untag(troot, EB_NODE),
struct eb64_node, node.branches);
if (node->node.bit < 0) {
/* We're at the top of a dup tree. Either we got a
* matching value and we return the leftmost node, or
* we don't and we skip the whole subtree to return the
* next node after the subtree. Note that since we're
* at the top of the dup tree, we can simply return the
* next node without first trying to escape from the
* tree.
*/
if (node->key >= x) {
troot = node->node.branches.b[EB_LEFT];
while (eb_gettag(troot) != EB_LEAF)
troot = (eb_untag(troot, EB_NODE))->b[EB_LEFT];
return container_of(eb_untag(troot, EB_LEAF),
struct eb64_node, node.branches);
}
/* return next */
troot = node->node.node_p;
break;
}
if (((x ^ node->key) >> node->node.bit) >= EB_NODE_BRANCHES) {
/* No more common bits at all. Either this node is too
* large and we need to get its lowest value, or it is too
* small, and we need to get the next value.
*/
if ((node->key >> node->node.bit) > (x >> node->node.bit)) {
troot = node->node.branches.b[EB_LEFT];
return eb64_entry(eb_walk_down(troot, EB_LEFT), struct eb64_node, node);
}
/* Further values will be too low here, so return the next
* unique node (if it exists).
*/
troot = node->node.node_p;
break;
}
troot = node->node.branches.b[(x >> node->node.bit) & EB_NODE_BRANCH_MASK];
}
/* If we get here, it means we want to report next node after the
* current one which is not below. <troot> is already initialised
* to the parent's branches.
*/
while (eb_gettag(troot) != EB_LEFT)
/* Walking up from right branch, so we cannot be below root */
troot = (eb_root_to_node(eb_untag(troot, EB_RGHT)))->node_p;
/* Note that <troot> cannot be NULL at this stage */
troot = (eb_untag(troot, EB_LEFT))->b[EB_RGHT];
if (eb_clrtag(troot) == NULL)
return NULL;
node = eb64_entry(eb_walk_down(troot, EB_LEFT), struct eb64_node, node);
return node;
}