1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include <stddef.h> /* for ptrdiff_t */
31 #endif /* __cplusplus */
33 typedef double *rb_key; /* key type ... double* is convenient for us,
34 but of course this could be cast to anything
35 desired (although void* would look more generic) */
37 typedef enum { RED, BLACK } rb_color;
38 typedef struct rb_node_s {
39 struct rb_node_s *p, *r, *l; /* parent, right, left */
40 rb_key k; /* key (and data) */
44 typedef int (*rb_compare)(rb_key k1, rb_key k2);
49 int N; /* number of nodes */
52 extern void rb_tree_init(rb_tree *t, rb_compare compare);
53 extern void rb_tree_destroy(rb_tree *t);
54 extern void rb_tree_destroy_with_keys(rb_tree *t);
55 extern rb_node *rb_tree_insert(rb_tree *t, rb_key k);
56 extern int rb_tree_check(rb_tree *t);
57 extern rb_node *rb_tree_find(rb_tree *t, rb_key k);
58 extern rb_node *rb_tree_find_le(rb_tree *t, rb_key k);
59 extern rb_node *rb_tree_find_lt(rb_tree *t, rb_key k);
60 extern rb_node *rb_tree_find_gt(rb_tree *t, rb_key k);
61 extern rb_node *rb_tree_resort(rb_tree *t, rb_node *n);
62 extern rb_node *rb_tree_min(rb_tree *t);
63 extern rb_node *rb_tree_max(rb_tree *t);
64 extern rb_node *rb_tree_succ(rb_node *n);
65 extern rb_node *rb_tree_pred(rb_node *n);
66 extern void rb_tree_shift_keys(rb_tree *t, ptrdiff_t kshift);
68 /* To change a key, use rb_tree_find+resort. Removing a node
69 currently wastes memory unless you change the allocation scheme
71 extern rb_node *rb_tree_remove(rb_tree *t, rb_node *n);
75 #endif /* __cplusplus */