rb.h

Wed, 10 Mar 2004 17:46:49 +0100

author
tuomov
date
Wed, 10 Mar 2004 17:46:49 +0100
changeset 67
b53529762874
parent 66
42085c134e29
child 68
7a97a1769840
permissions
-rw-r--r--

trunk: changeset 1370
- Added red-black tree routines for direct pointer comparison.

- Made keys to red-black tree routines const.

65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
1 /*
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
2 Generic C code for red-black trees.
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
3 Copyright (C) 2000 James S. Plank
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
4
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
7 License as published by the Free Software Foundation; either
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
9
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
13 Lesser General Public License for more details.
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
14
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
18 */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
19
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
20 /* Revision 1.2. Jim Plank */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
21
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
22 /* Original code by Jim Plank (plank@cs.utk.edu) */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
23 /* modified for THINK C 6.0 for Macintosh by Chris Bartley */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
24
66
42085c134e29 trunk: changeset 1369
tuomov
parents: 65
diff changeset
25 typedef struct rb_status {
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
26 unsigned red : 1 ;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
27 unsigned internal : 1 ;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
28 unsigned left : 1 ;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
29 unsigned root : 1 ;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
30 unsigned head : 1 ;
66
42085c134e29 trunk: changeset 1369
tuomov
parents: 65
diff changeset
31 } Rb_status;
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
32
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
33 /* Main rb_node. You only ever use the fields
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
34
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
35 c.list.flink
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
36 c.list.blink
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
37
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
38 k.key or k.ikey
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
39 v.val
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
40 */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
41
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
42
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
43 typedef struct rb_node {
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
44 union {
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
45 struct {
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
46 struct rb_node *flink;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
47 struct rb_node *blink;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
48 } list;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
49 struct {
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
50 struct rb_node *left;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
51 struct rb_node *right;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
52 } child;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
53 } c;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
54 union {
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
55 struct rb_node *parent;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
56 struct rb_node *root;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
57 } p;
66
42085c134e29 trunk: changeset 1369
tuomov
parents: 65
diff changeset
58 Rb_status s;
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
59 union {
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
60 int ikey;
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
61 const void *key;
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
62 struct rb_node *lext;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
63 } k;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
64 union {
66
42085c134e29 trunk: changeset 1369
tuomov
parents: 65
diff changeset
65 void *val;
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
66 struct rb_node *rext;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
67 } v;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
68 } *Rb_node;
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
69
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
70
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
71 typedef int Rb_compfn(const void *, const void *);
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
72
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
73
66
42085c134e29 trunk: changeset 1369
tuomov
parents: 65
diff changeset
74 extern Rb_node make_rb(); /* Creates a new rb-tree */
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
75
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
76 /* Creates a node with key key and val val and inserts it into the tree.
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
77 rb_insert uses strcmp() as comparison funcion. rb_inserti uses <>=,
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
78 rb_insertg uses func() */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
79
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
80 extern Rb_node rb_insert(Rb_node tree, const char *key, void *val);
66
42085c134e29 trunk: changeset 1369
tuomov
parents: 65
diff changeset
81 extern Rb_node rb_inserti(Rb_node tree, int ikey, void *val);
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
82 extern Rb_node rb_insertp(Rb_node tree, const void *key, void *val);
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
83 extern Rb_node rb_insertg(Rb_node tree, const void *key, void *val,
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
84 Rb_compfn *func);
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
85
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
86
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
87 /* returns an external node in t whose value is equal
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
88 k or whose value is the smallest value greater than k. */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
89
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
90 extern Rb_node rb_find_key(Rb_node root, const char *key);
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
91 extern Rb_node rb_find_ikey(Rb_node root, int ikey);
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
92 extern Rb_node rb_find_pkey(Rb_node root, const void *key);
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
93 extern Rb_node rb_find_gkey(Rb_node root, const void *key, Rb_compfn *func);
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
94
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
95
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
96 /* Works just like the find_key versions only it returns whether or not
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
97 it found the key in the integer variable found */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
98
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
99 extern Rb_node rb_find_key_n(Rb_node root, const char *key, int *found);
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
100 extern Rb_node rb_find_ikey_n(Rb_node root, int ikey, int *found);
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
101 extern Rb_node rb_find_pkey_n(Rb_node root, const void *key, int *found);
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
102 extern Rb_node rb_find_gkey_n(Rb_node root, const void *key,
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
103 Rb_compfn *func, int *found);
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
104
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
105
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
106 /* Creates a node with key key and val val and inserts it into the
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
107 tree before/after node nd. Does not check to ensure that you are
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
108 keeping the correct order */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
109
67
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
110 extern Rb_node rb_insert_b(Rb_node nd, const void *key, void *val);
b53529762874 trunk: changeset 1370
tuomov
parents: 66
diff changeset
111 extern Rb_node rb_insert_a(Rb_node nd, const void *key, void *val);
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
112
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
113
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
114 extern void rb_delete_node(Rb_node node); /* Deletes and frees a node (but
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
115 not the key or val) */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
116 extern void rb_free_tree(Rb_node root); /* Deletes and frees an entire tree */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
117
66
42085c134e29 trunk: changeset 1369
tuomov
parents: 65
diff changeset
118 extern void *rb_val(Rb_node node); /* Returns node->v.val -- this is to shut
65
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
119 lint up */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
120
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
121 extern int rb_nblack(Rb_node n); /* returns # of black nodes in path from
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
122 n to the root */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
123 int rb_plength(Rb_node n); /* returns the # of nodes in path from
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
124 n to the root */
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
125
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
126 #define rb_first(n) (n->c.list.flink)
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
127 #define rb_last(n) (n->c.list.blink)
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
128 #define rb_next(n) (n->c.list.flink)
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
129 #define rb_prev(n) (n->c.list.blink)
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
130 #define rb_empty(t) (t->c.list.flink == t)
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
131 #ifndef rb_nil
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
132 #define rb_nil(t) (t)
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
133 #endif
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
134
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
135 #define rb_traverse(ptr, lst) \
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
136 for(ptr = rb_first(lst); ptr != rb_nil(lst); ptr = rb_next(ptr))
58e382ae97cd trunk: changeset 1368
tuomov
parents:
diff changeset
137

mercurial