1 | /*
|
---|
2 | * Red-black search tree support
|
---|
3 | *
|
---|
4 | * Copyright 2009 Henri Verbeet
|
---|
5 | * Copyright 2009 Andrew Riedi
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2.1 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Lesser General Public
|
---|
18 | * License along with this library; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
24 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
25 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
26 | * a choice of LGPL license versions is made available with the language indicating
|
---|
27 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
28 | * of the LGPL is applied is otherwise unspecified.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef __WINE_WINE_RBTREE_H
|
---|
32 | #define __WINE_WINE_RBTREE_H
|
---|
33 |
|
---|
34 | #define WINE_RB_ENTRY_VALUE(element, type, field) \
|
---|
35 | ((type *)((char *)(element) - FIELD_OFFSET(type, field)))
|
---|
36 |
|
---|
37 | struct wine_rb_entry
|
---|
38 | {
|
---|
39 | struct wine_rb_entry *left;
|
---|
40 | struct wine_rb_entry *right;
|
---|
41 | unsigned int flags;
|
---|
42 | };
|
---|
43 |
|
---|
44 | struct wine_rb_stack
|
---|
45 | {
|
---|
46 | struct wine_rb_entry ***entries;
|
---|
47 | size_t count;
|
---|
48 | size_t size;
|
---|
49 | };
|
---|
50 |
|
---|
51 | struct wine_rb_functions
|
---|
52 | {
|
---|
53 | void *(*alloc)(size_t size);
|
---|
54 | void *(*realloc)(void *ptr, size_t size);
|
---|
55 | void (*free)(void *ptr);
|
---|
56 | int (*compare)(const void *key, const struct wine_rb_entry *entry);
|
---|
57 | };
|
---|
58 |
|
---|
59 | struct wine_rb_tree
|
---|
60 | {
|
---|
61 | const struct wine_rb_functions *functions;
|
---|
62 | struct wine_rb_entry *root;
|
---|
63 | struct wine_rb_stack stack;
|
---|
64 | };
|
---|
65 |
|
---|
66 | typedef void (wine_rb_traverse_func_t)(struct wine_rb_entry *entry, void *context);
|
---|
67 |
|
---|
68 | #define WINE_RB_FLAG_RED 0x1
|
---|
69 | #define WINE_RB_FLAG_STOP 0x2
|
---|
70 | #define WINE_RB_FLAG_TRAVERSED_LEFT 0x4
|
---|
71 | #define WINE_RB_FLAG_TRAVERSED_RIGHT 0x8
|
---|
72 |
|
---|
73 | static inline void wine_rb_stack_clear(struct wine_rb_stack *stack)
|
---|
74 | {
|
---|
75 | stack->count = 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | static inline void wine_rb_stack_push(struct wine_rb_stack *stack, struct wine_rb_entry **entry)
|
---|
79 | {
|
---|
80 | stack->entries[stack->count++] = entry;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static inline int wine_rb_ensure_stack_size(struct wine_rb_tree *tree, size_t size)
|
---|
84 | {
|
---|
85 | struct wine_rb_stack *stack = &tree->stack;
|
---|
86 |
|
---|
87 | if (size > stack->size)
|
---|
88 | {
|
---|
89 | size_t new_size = stack->size << 1;
|
---|
90 | struct wine_rb_entry ***new_entries = tree->functions->realloc(stack->entries,
|
---|
91 | new_size * sizeof(*stack->entries));
|
---|
92 |
|
---|
93 | if (!new_entries) return -1;
|
---|
94 |
|
---|
95 | stack->entries = new_entries;
|
---|
96 | stack->size = new_size;
|
---|
97 | }
|
---|
98 |
|
---|
99 | return 0;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static inline int wine_rb_is_red(struct wine_rb_entry *entry)
|
---|
103 | {
|
---|
104 | return entry && (entry->flags & WINE_RB_FLAG_RED);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static inline void wine_rb_rotate_left(struct wine_rb_entry **entry)
|
---|
108 | {
|
---|
109 | struct wine_rb_entry *e = *entry;
|
---|
110 | struct wine_rb_entry *right = e->right;
|
---|
111 |
|
---|
112 | e->right = right->left;
|
---|
113 | right->left = e;
|
---|
114 | right->flags &= ~WINE_RB_FLAG_RED;
|
---|
115 | right->flags |= e->flags & WINE_RB_FLAG_RED;
|
---|
116 | e->flags |= WINE_RB_FLAG_RED;
|
---|
117 | *entry = right;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static inline void wine_rb_rotate_right(struct wine_rb_entry **entry)
|
---|
121 | {
|
---|
122 | struct wine_rb_entry *e = *entry;
|
---|
123 | struct wine_rb_entry *left = e->left;
|
---|
124 |
|
---|
125 | e->left = left->right;
|
---|
126 | left->right = e;
|
---|
127 | left->flags &= ~WINE_RB_FLAG_RED;
|
---|
128 | left->flags |= e->flags & WINE_RB_FLAG_RED;
|
---|
129 | e->flags |= WINE_RB_FLAG_RED;
|
---|
130 | *entry = left;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static inline void wine_rb_flip_color(struct wine_rb_entry *entry)
|
---|
134 | {
|
---|
135 | entry->flags ^= WINE_RB_FLAG_RED;
|
---|
136 | entry->left->flags ^= WINE_RB_FLAG_RED;
|
---|
137 | entry->right->flags ^= WINE_RB_FLAG_RED;
|
---|
138 | }
|
---|
139 |
|
---|
140 | static inline void wine_rb_fixup(struct wine_rb_stack *stack)
|
---|
141 | {
|
---|
142 | while (stack->count)
|
---|
143 | {
|
---|
144 | struct wine_rb_entry **entry = stack->entries[stack->count - 1];
|
---|
145 |
|
---|
146 | if ((*entry)->flags & WINE_RB_FLAG_STOP)
|
---|
147 | {
|
---|
148 | (*entry)->flags &= ~WINE_RB_FLAG_STOP;
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (wine_rb_is_red((*entry)->right) && !wine_rb_is_red((*entry)->left)) wine_rb_rotate_left(entry);
|
---|
153 | if (wine_rb_is_red((*entry)->left) && wine_rb_is_red((*entry)->left->left)) wine_rb_rotate_right(entry);
|
---|
154 | if (wine_rb_is_red((*entry)->left) && wine_rb_is_red((*entry)->right)) wine_rb_flip_color(*entry);
|
---|
155 | --stack->count;
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | static inline void wine_rb_move_red_left(struct wine_rb_entry **entry)
|
---|
160 | {
|
---|
161 | wine_rb_flip_color(*entry);
|
---|
162 | if (wine_rb_is_red((*entry)->right->left))
|
---|
163 | {
|
---|
164 | wine_rb_rotate_right(&(*entry)->right);
|
---|
165 | wine_rb_rotate_left(entry);
|
---|
166 | wine_rb_flip_color(*entry);
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | static inline void wine_rb_move_red_right(struct wine_rb_entry **entry)
|
---|
171 | {
|
---|
172 | wine_rb_flip_color(*entry);
|
---|
173 | if (wine_rb_is_red((*entry)->left->left))
|
---|
174 | {
|
---|
175 | wine_rb_rotate_right(entry);
|
---|
176 | wine_rb_flip_color(*entry);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | static inline void wine_rb_postorder(struct wine_rb_tree *tree, wine_rb_traverse_func_t *callback, void *context)
|
---|
181 | {
|
---|
182 | struct wine_rb_entry **entry;
|
---|
183 |
|
---|
184 | if (!tree->root) return;
|
---|
185 |
|
---|
186 | for (entry = &tree->root;;)
|
---|
187 | {
|
---|
188 | struct wine_rb_entry *e = *entry;
|
---|
189 |
|
---|
190 | if (e->left && !(e->flags & WINE_RB_FLAG_TRAVERSED_LEFT))
|
---|
191 | {
|
---|
192 | wine_rb_stack_push(&tree->stack, entry);
|
---|
193 | e->flags |= WINE_RB_FLAG_TRAVERSED_LEFT;
|
---|
194 | entry = &e->left;
|
---|
195 | continue;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (e->right && !(e->flags & WINE_RB_FLAG_TRAVERSED_RIGHT))
|
---|
199 | {
|
---|
200 | wine_rb_stack_push(&tree->stack, entry);
|
---|
201 | e->flags |= WINE_RB_FLAG_TRAVERSED_RIGHT;
|
---|
202 | entry = &e->right;
|
---|
203 | continue;
|
---|
204 | }
|
---|
205 |
|
---|
206 | e->flags &= ~(WINE_RB_FLAG_TRAVERSED_LEFT | WINE_RB_FLAG_TRAVERSED_RIGHT);
|
---|
207 | callback(e, context);
|
---|
208 |
|
---|
209 | if (!tree->stack.count) break;
|
---|
210 | entry = tree->stack.entries[--tree->stack.count];
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | static inline int wine_rb_init(struct wine_rb_tree *tree, const struct wine_rb_functions *functions)
|
---|
215 | {
|
---|
216 | tree->functions = functions;
|
---|
217 | tree->root = NULL;
|
---|
218 |
|
---|
219 | tree->stack.entries = functions->alloc(16 * sizeof(*tree->stack.entries));
|
---|
220 | if (!tree->stack.entries) return -1;
|
---|
221 | tree->stack.size = 16;
|
---|
222 | tree->stack.count = 0;
|
---|
223 |
|
---|
224 | return 0;
|
---|
225 | }
|
---|
226 |
|
---|
227 | static inline void wine_rb_for_each_entry(struct wine_rb_tree *tree, wine_rb_traverse_func_t *callback, void *context)
|
---|
228 | {
|
---|
229 | wine_rb_postorder(tree, callback, context);
|
---|
230 | }
|
---|
231 |
|
---|
232 | static inline void wine_rb_destroy(struct wine_rb_tree *tree, wine_rb_traverse_func_t *callback, void *context)
|
---|
233 | {
|
---|
234 | /* Note that we use postorder here because the callback will likely free the entry. */
|
---|
235 | if (callback) wine_rb_postorder(tree, callback, context);
|
---|
236 |
|
---|
237 | tree->root = NULL;
|
---|
238 | tree->functions->free(tree->stack.entries);
|
---|
239 | }
|
---|
240 |
|
---|
241 | static inline struct wine_rb_entry *wine_rb_get(const struct wine_rb_tree *tree, const void *key)
|
---|
242 | {
|
---|
243 | struct wine_rb_entry *entry = tree->root;
|
---|
244 | while (entry)
|
---|
245 | {
|
---|
246 | int c = tree->functions->compare(key, entry);
|
---|
247 | if (!c) return entry;
|
---|
248 | entry = c < 0 ? entry->left : entry->right;
|
---|
249 | }
|
---|
250 | return NULL;
|
---|
251 | }
|
---|
252 |
|
---|
253 | static inline int wine_rb_put(struct wine_rb_tree *tree, const void *key, struct wine_rb_entry *entry)
|
---|
254 | {
|
---|
255 | struct wine_rb_entry **parent = &tree->root;
|
---|
256 | size_t black_height = 1;
|
---|
257 |
|
---|
258 | while (*parent)
|
---|
259 | {
|
---|
260 | int c;
|
---|
261 |
|
---|
262 | if (!wine_rb_is_red(*parent)) ++black_height;
|
---|
263 |
|
---|
264 | wine_rb_stack_push(&tree->stack, parent);
|
---|
265 |
|
---|
266 | c = tree->functions->compare(key, *parent);
|
---|
267 | if (!c)
|
---|
268 | {
|
---|
269 | wine_rb_stack_clear(&tree->stack);
|
---|
270 | return -1;
|
---|
271 | }
|
---|
272 | else if (c < 0) parent = &(*parent)->left;
|
---|
273 | else parent = &(*parent)->right;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /* After insertion, the path length to any node should be <= (black_height + 1) * 2. */
|
---|
277 | if (wine_rb_ensure_stack_size(tree, black_height << 1) == -1)
|
---|
278 | {
|
---|
279 | wine_rb_stack_clear(&tree->stack);
|
---|
280 | return -1;
|
---|
281 | }
|
---|
282 |
|
---|
283 | entry->flags = WINE_RB_FLAG_RED;
|
---|
284 | entry->left = NULL;
|
---|
285 | entry->right = NULL;
|
---|
286 | *parent = entry;
|
---|
287 |
|
---|
288 | wine_rb_fixup(&tree->stack);
|
---|
289 | tree->root->flags &= ~WINE_RB_FLAG_RED;
|
---|
290 |
|
---|
291 | return 0;
|
---|
292 | }
|
---|
293 |
|
---|
294 | static inline void wine_rb_remove(struct wine_rb_tree *tree, const void *key)
|
---|
295 | {
|
---|
296 | struct wine_rb_entry **entry = &tree->root;
|
---|
297 |
|
---|
298 | while (*entry)
|
---|
299 | {
|
---|
300 | if (tree->functions->compare(key, *entry) < 0)
|
---|
301 | {
|
---|
302 | wine_rb_stack_push(&tree->stack, entry);
|
---|
303 | if (!wine_rb_is_red((*entry)->left) && !wine_rb_is_red((*entry)->left->left)) wine_rb_move_red_left(entry);
|
---|
304 | entry = &(*entry)->left;
|
---|
305 | }
|
---|
306 | else
|
---|
307 | {
|
---|
308 | if (wine_rb_is_red((*entry)->left)) wine_rb_rotate_right(entry);
|
---|
309 | if (!tree->functions->compare(key, *entry) && !(*entry)->right)
|
---|
310 | {
|
---|
311 | *entry = NULL;
|
---|
312 | break;
|
---|
313 | }
|
---|
314 | if (!wine_rb_is_red((*entry)->right) && !wine_rb_is_red((*entry)->right->left))
|
---|
315 | wine_rb_move_red_right(entry);
|
---|
316 | if (!tree->functions->compare(key, *entry))
|
---|
317 | {
|
---|
318 | struct wine_rb_entry **e = &(*entry)->right;
|
---|
319 | struct wine_rb_entry *m = *e;
|
---|
320 | while (m->left) m = m->left;
|
---|
321 |
|
---|
322 | wine_rb_stack_push(&tree->stack, entry);
|
---|
323 | (*entry)->flags |= WINE_RB_FLAG_STOP;
|
---|
324 |
|
---|
325 | while ((*e)->left)
|
---|
326 | {
|
---|
327 | wine_rb_stack_push(&tree->stack, e);
|
---|
328 | if (!wine_rb_is_red((*e)->left) && !wine_rb_is_red((*e)->left->left)) wine_rb_move_red_left(e);
|
---|
329 | e = &(*e)->left;
|
---|
330 | }
|
---|
331 | *e = NULL;
|
---|
332 | wine_rb_fixup(&tree->stack);
|
---|
333 |
|
---|
334 | *m = **entry;
|
---|
335 | *entry = m;
|
---|
336 |
|
---|
337 | break;
|
---|
338 | }
|
---|
339 | else
|
---|
340 | {
|
---|
341 | wine_rb_stack_push(&tree->stack, entry);
|
---|
342 | entry = &(*entry)->right;
|
---|
343 | }
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | wine_rb_fixup(&tree->stack);
|
---|
348 | if (tree->root) tree->root->flags &= ~WINE_RB_FLAG_RED;
|
---|
349 | }
|
---|
350 |
|
---|
351 | #endif /* __WINE_WINE_RBTREE_H */
|
---|