chiark / gitweb /
New program to make fixed tables for universal hashing.
[mLib] / sym.h
1 /* -*-c-*-
2  *
3  * $Id: sym.h,v 1.12 2001/01/20 11:49:37 mdw Exp $
4  *
5  * Symbol table management
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  * 
19  * mLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mLib; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------*
31  *
32  * $Log: sym.h,v $
33  * Revision 1.12  2001/01/20 11:49:37  mdw
34  * Export tuning parameters from header file, for the benefit of other
35  * hashtable implementations.  Change the storage of symbol names: store
36  * the name after the allocated symbol block in all cases.  This replaces
37  * the previous complicated and slightly wasteful arrangement.
38  *
39  * Revision 1.11  2000/06/17 10:37:39  mdw
40  * Add support for arena management.
41  *
42  * Revision 1.10  1999/12/10 23:42:04  mdw
43  * Change header file guard names.
44  *
45  * Revision 1.9  1999/08/02 16:53:48  mdw
46  * Improve type safety for sym_iter objects.
47  *
48  * Revision 1.8  1999/08/02 14:45:48  mdw
49  * Break low-level hashtable code out from sym.
50  *
51  * Revision 1.7  1999/06/01 09:49:33  mdw
52  * Allow things to be looked up by just their caller-supplied hashes.  This
53  * actually needs to be thought through better.
54  *
55  * Revision 1.6  1999/05/26 21:08:31  mdw
56  * Rename symbols in line with newer conventions.
57  *
58  * Revision 1.5  1999/05/13 22:48:37  mdw
59  * Change `-ise' to `-ize' throughout.
60  *
61  * Revision 1.4  1999/05/06 19:51:35  mdw
62  * Reformatted the LGPL notice a little bit.
63  *
64  * Revision 1.3  1999/05/05 18:50:31  mdw
65  * Change licensing conditions to LGPL.
66  *
67  * Revision 1.2  1998/11/26 19:27:34  mdw
68  * Move SYM_NAME into the header file.  Fix bugs.
69  *
70  * Revision 1.1.1.1  1998/06/17 23:44:42  mdw
71  * Initial version of mLib
72  *
73  */
74
75 #ifndef MLIB_SYM_H
76 #define MLIB_SYM_H
77
78 #ifdef __cplusplus
79   extern "C" {
80 #endif
81
82 /*----- Required headers --------------------------------------------------*/
83
84 #include <stddef.h>
85
86 #ifndef MLIB_BITS_H
87 #  include "bits.h"
88 #endif
89
90 #ifndef MLIB_HASH_H
91 #  include "hash.h"
92 #endif
93
94 #ifndef MLIB_SUB_H
95 #  include "sub.h"
96 #endif
97
98 /*----- Tuning parameters -------------------------------------------------*/
99
100 /* --- Initial hash table size --- *
101  *
102  * This is the initial @mask@ value.  It must be of the form %$2^n - 1$%,
103  * so that it can be used to mask of the bottom bits of a hash value.
104  */
105
106 #define SYM_INITSZ 32                   /* Size of a new hash table */
107
108 /* --- Maximum load factor --- *
109  *
110  * This parameter controls how much the table has to be loaded before the
111  * table is extended.  The number of elements %$n$%, the number of bins %$b$%
112  * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is
113  * added to the table and this relation is found to be false, the table is
114  * doubled in size.
115  */
116
117 #define SYM_LIMIT(n) ((n) * 2)          /* Load factor for growing table */
118
119 /*----- Type definitions --------------------------------------------------*/
120
121 /* --- Symbol table --- *
122  *
123  * A @sym_table@ contains the information needed to manage a symbol table.
124  * Users shouldn't fiddle with this information directly, but it needs to be
125  * here so that objects of the correct type can be created.
126  */
127
128 typedef struct sym_table {
129   hash_table t;
130   subarena *s;
131   size_t load;
132 } sym_table;
133
134 /* --- A symbol table entry --- *
135  *
136  * I don't care what actually gets stored in symbol entries because I don't
137  * create them: that's the responsibility of my client.  All I care about
138  * here is that whatever gets passed to me is a structure whose first member
139  * is a @sym_base@.  The ANSI guarantees about structure layout are
140  * sufficient to allow me to manipulate such objects.
141  */
142
143 typedef struct sym_base {
144   hash_base b;                          /* Base structure */
145   char *name;                           /* Pointer to name string */
146   size_t len;                           /* Length of the symbol's name */
147 } sym_base;
148
149 /* --- Macros for picking out useful information --- *
150  *
151  * Note that @SYM_LEN@ returns the size of the symbol key.  For textual keys,
152  * this will include the terminating null.
153  */
154
155 #define SYM_NAME(sy) ((const char *)(((sym_base *)(sy))->name))
156 #define SYM_LEN(sy) (((sym_base *)(sy))->len + 0)
157 #define SYM_HASH(sy) (((sym_base *)(sy))->b.hash + 0)
158
159 /* --- An iterator block --- */
160
161 typedef struct { hash_iter i; } sym_iter;
162
163 /*----- External functions ------------------------------------------------*/
164
165 /* --- @sym_create@ --- *
166  *
167  * Arguments:   @sym_table *t@ = symbol table to initialize
168  *
169  * Returns:     ---
170  *
171  * Use:         Initializes the given symbol table.  Raises @EXC_NOMEM@ if
172  *              there isn't enough memory.
173  */
174
175 extern void sym_create(sym_table */*t*/);
176
177 /* --- @sym_destroy@ --- *
178  *
179  * Arguments:   @sym_table *t@ = pointer to symbol table in question
180  *
181  * Returns:     ---
182  *
183  * Use:         Destroys a symbol table, freeing all the memory it used to
184  *              occupy.
185  */
186
187 extern void sym_destroy(sym_table */*t*/);
188
189 /* --- @sym_find@ --- *
190  *
191  * Arguments:   @sym_table *t@ = pointer to symbol table in question
192  *              @const char *n@ = pointer to symbol table to look up
193  *              @long l@ = length of the name string or negative to measure
194  *              @size_t sz@ = size of desired symbol object, or zero
195  *              @unsigned *f@ = pointer to a flag, or null.
196  *
197  * Returns:     The address of a @sym_base@ structure, or null if not found
198  *              and @sz@ is zero.
199  *
200  * Use:         Looks up a symbol in a given symbol table.  The name is
201  *              passed by the address of its first character.  The length
202  *              may be given, in which case the name may contain arbitrary
203  *              binary data, or it may be given as a negative number, in
204  *              which case the length of the name is calculated as
205  *              @strlen(n) + 1@.
206  *
207  *              The return value is the address of a pointer to a @sym_base@
208  *              block (which may have other things on the end, as above).  If
209  *              the symbol could be found, the return value points to the
210  *              symbol block.  If the symbol wasn't there, then if @sz@ is
211  *              nonzero, a new symbol is created and its address is returned;
212  *              otherwise a null pointer is returned.  The exception
213  *              @EXC_NOMEM@ is raised if the block can't be allocated.
214  *
215  *              The value of @*f@ indicates whether a new symbol entry was
216  *              created: a nonzero value indicates that an old value was
217  *              found.
218  */
219
220 extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/,
221                       size_t /*sz*/, unsigned */*f*/);
222
223 /* --- @sym_remove@ --- *
224  *
225  * Arguments:   @sym_table *t@ = pointer to a symbol table object
226  *              @void *b@ = pointer to symbol table entry
227  *
228  * Returns:     ---
229  *
230  * Use:         Removes the object from the symbol table.  The space occupied
231  *              by the object and its name is freed; anything else attached
232  *              to the entry should already be gone by this point.
233  */
234
235 extern void sym_remove(sym_table */*t*/, void */*b*/);
236
237 /* --- @sym_mkiter@ --- *
238  *
239  * Arguments:   @sym_iter *i@ = pointer to an iterator object
240  *              @sym_table *t@ = pointer to a symbol table object
241  *
242  * Returns:     ---
243  *
244  * Use:         Creates a new symbol table iterator which may be used to
245  *              iterate through a symbol table.
246  */
247
248 #define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t)
249
250 extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/);
251
252 /* --- @sym_next@ --- *
253  *
254  * Arguments:   @sym_iter *i@ = pointer to iterator object
255  *
256  * Returns:     Pointer to the next symbol found, or null when finished.
257  *
258  * Use:         Returns the next symbol from the table.  Symbols are not
259  *              returned in any particular order.
260  */
261
262 #define SYM_NEXT(i_, p) do {                                            \
263   hash_base *_q;                                                        \
264   HASH_NEXT(&(i_)->i, _q);                                              \
265   (p) = (void *)_q;                                                     \
266 } while (0)
267
268 extern void *sym_next(sym_iter */*i*/);
269
270 /*----- That's all, folks -------------------------------------------------*/
271
272 #ifdef __cplusplus
273   }
274 #endif
275
276 #endif