comparison src/redblack.h @ 125:e413158cae13

Add ushare project files.
author naoyan@johnstown.minaminoshima.org
date Sun, 03 Oct 2010 11:35:19 +0900
parents
children
comparison
equal deleted inserted replaced
124:9c7bc6c0327e 125:e413158cae13
1 /*
2 * RCS $Id: redblack.h,v 1.9 2003/10/24 01:31:21 damo Exp $
3 */
4
5 /*
6 Redblack balanced tree algorithm
7 Copyright (C) Damian Ivereigh 2000
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version. See the file COPYING for details.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 /* Header file for redblack.c, should be included by any code that
25 ** uses redblack.c since it defines the functions
26 */
27
28 /* Stop multiple includes */
29 #ifndef _REDBLACK_H
30
31 #ifndef RB_CUSTOMIZE
32 /*
33 * Without customization, the data member in the tree nodes is a void
34 * pointer, and you need to pass in a comparison function to be
35 * applied at runtime. With customization, you specify the data type
36 * as the macro RB_ENTRY(data_t) (has to be a macro because compilers
37 * gag on typdef void) and the name of the compare function as the
38 * value of the macro RB_CMP. Because the comparison function is
39 * compiled in, RB_CMP only needs to take two arguments. If your
40 * content type is not a pointer, define INLINE to get direct access.
41 */
42 #define rbdata_t void
43 #define RB_CMP(s, t, e) (*rbinfo->rb_cmp)(s, t, e)
44 #undef RB_INLINE
45 #define RB_ENTRY(name) rb##name
46 #endif /* RB_CUSTOMIZE */
47
48 #ifndef RB_STATIC
49 #define RB_STATIC
50 #endif
51
52 /* Modes for rblookup */
53 #define RB_NONE -1 /* None of those below */
54 #define RB_LUEQUAL 0 /* Only exact match */
55 #define RB_LUGTEQ 1 /* Exact match or greater */
56 #define RB_LULTEQ 2 /* Exact match or less */
57 #define RB_LULESS 3 /* Less than key (not equal to) */
58 #define RB_LUGREAT 4 /* Greater than key (not equal to) */
59 #define RB_LUNEXT 5 /* Next key after current */
60 #define RB_LUPREV 6 /* Prev key before current */
61 #define RB_LUFIRST 7 /* First key in index */
62 #define RB_LULAST 8 /* Last key in index */
63
64 /* For rbwalk - pinched from search.h */
65 typedef enum
66 {
67 preorder,
68 postorder,
69 endorder,
70 leaf
71 }
72 VISIT;
73
74 struct RB_ENTRY(lists) {
75 const struct RB_ENTRY(node) *rootp;
76 const struct RB_ENTRY(node) *nextp;
77 };
78
79 #define RBLIST struct RB_ENTRY(lists)
80
81 struct RB_ENTRY(tree) {
82 #ifndef RB_CUSTOMIZE
83 /* comparison routine */
84 int (*rb_cmp)(const void *, const void *, const void *);
85 /* config data to be passed to rb_cmp */
86 const void *rb_config;
87 /* root of tree */
88 #endif /* RB_CUSTOMIZE */
89 struct RB_ENTRY(node) *rb_root;
90 };
91
92 #ifndef RB_CUSTOMIZE
93 RB_STATIC struct RB_ENTRY(tree) *rbinit(int (*)(const void *, const void *, const void *),
94 const void *);
95 #else
96 RB_STATIC struct RB_ENTRY(tree) *RB_ENTRY(init)(void);
97 #endif /* RB_CUSTOMIZE */
98
99 #ifndef no_delete
100 RB_STATIC const RB_ENTRY(data_t) *RB_ENTRY(delete)(const RB_ENTRY(data_t) *, struct RB_ENTRY(tree) *);
101 #endif
102
103 #ifndef no_find
104 RB_STATIC const RB_ENTRY(data_t) *RB_ENTRY(find)(const RB_ENTRY(data_t) *, struct RB_ENTRY(tree) *);
105 #endif
106
107 #ifndef no_lookup
108 RB_STATIC const RB_ENTRY(data_t) *RB_ENTRY(lookup)(int, const RB_ENTRY(data_t) *, struct RB_ENTRY(tree) *);
109 #endif
110
111 #ifndef no_search
112 RB_STATIC const RB_ENTRY(data_t) *RB_ENTRY(search)(const RB_ENTRY(data_t) *, struct RB_ENTRY(tree) *);
113 #endif
114
115 #ifndef no_destroy
116 RB_STATIC void RB_ENTRY(destroy)(struct RB_ENTRY(tree) *);
117 #endif
118
119 #ifndef no_walk
120 RB_STATIC void RB_ENTRY(walk)(const struct RB_ENTRY(tree) *,
121 void (*)(const RB_ENTRY(data_t) *, const VISIT, const int, void *),
122 void *);
123 #endif
124
125 #ifndef no_readlist
126 RB_STATIC RBLIST *RB_ENTRY(openlist)(const struct RB_ENTRY(tree) *);
127 RB_STATIC const RB_ENTRY(data_t) *RB_ENTRY(readlist)(RBLIST *);
128 RB_STATIC void RB_ENTRY(closelist)(RBLIST *);
129 #endif
130
131 /* Some useful macros */
132 #define rbmin(rbinfo) RB_ENTRY(lookup)(RB_LUFIRST, NULL, (rbinfo))
133 #define rbmax(rbinfo) RB_ENTRY(lookup)(RB_LULAST, NULL, (rbinfo))
134
135 #define _REDBLACK_H
136 #endif /* _REDBLACK_H */
137
138 /*
139 *
140 * $Log: redblack.h,v $
141 * Revision 1.9 2003/10/24 01:31:21 damo
142 * Patches from Eric Raymond: %prefix is implemented.  Various other small
143 * changes avoid stepping on global namespaces and improve the documentation.
144 *
145 * Revision 1.8 2003/10/23 04:18:47 damo
146 * Fixed up the rbgen stuff ready for the 1.3 release
147 *
148 * Revision 1.7 2002/08/26 03:11:40 damo
149 * Fixed up a bunch of compiler warnings when compiling example4
150 *
151 * Tidies up the Makefile.am & Specfile.
152 *
153 * Renamed redblack to rbgen
154 *
155 * Revision 1.6 2002/08/26 01:03:35 damo
156 * Patch from Eric Raymond to change the way the library is used:-
157 *
158 * Eric's idea is to convert libredblack into a piece of in-line code
159 * generated by another program. This should be faster, smaller and easier
160 * to use.
161 *
162 * This is the first check-in of his code before I start futzing with it!
163 *
164 * Revision 1.5 2002/01/30 07:54:53 damo
165 * Fixed up the libtool versioning stuff (finally)
166 * Fixed bug 500600 (not detecting a NULL return from malloc)
167 * Fixed bug 509485 (no longer needs search.h)
168 * Cleaned up debugging section
169 * Allow multiple inclusions of redblack.h
170 * Thanks to Matthias Andree for reporting (and fixing) these
171 *
172 * Revision 1.4 2000/06/06 14:43:43 damo
173 * Added all the rbwalk & rbopenlist stuff. Fixed up malloc instead of sbrk.
174 * Added two new examples
175 *
176 * Revision 1.3 2000/05/24 06:45:27 damo
177 * Converted everything over to using const
178 * Added a new example1.c file to demonstrate the worst case scenario
179 * Minor fixups of the spec file
180 *
181 * Revision 1.2 2000/05/24 06:17:10 damo
182 * Fixed up the License (now the LGPL)
183 *
184 * Revision 1.1 2000/05/24 04:15:53 damo
185 * Initial import of files. Versions are now all over the place. Oh well
186 *
187 */
188