rb.h

Mon, 17 Jan 2005 22:02:09 +0100

author
tuomov
date
Mon, 17 Jan 2005 22:02:09 +0100
changeset 85
9f94b2e96e3b
parent 73
401322031c7e
permissions
-rw-r--r--

trunk: changeset 1934
Fixed everything that requires locale stuff to check CF_NO_LOCALE.

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

mercurial