X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/c846879ccf3e86ea293c157f4aa2ff8716fb5b4c..d4efbcd93c940ad522fcf8c601ec1829d2e0b10d:/sym.h diff --git a/sym.h b/sym.h index 5c87b4a..7a40b74 100644 --- a/sym.h +++ b/sym.h @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: sym.h,v 1.3 1999/05/05 18:50:31 mdw Exp $ + * $Id: sym.h,v 1.14 2004/04/08 01:36:13 mdw Exp $ * * Symbol table management * * (c) 1998 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,33 +15,20 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with mLib; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -/*----- Revision history --------------------------------------------------* - * - * $Log: sym.h,v $ - * Revision 1.3 1999/05/05 18:50:31 mdw - * Change licensing conditions to LGPL. - * - * Revision 1.2 1998/11/26 19:27:34 mdw - * Move SYM_NAME into the header file. Fix bugs. - * - * Revision 1.1.1.1 1998/06/17 23:44:42 mdw - * Initial version of mLib * + * You should have received a copy of the GNU Library General Public + * License along with mLib; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + * MA 02111-1307, USA. */ -#ifndef SYM_H -#define SYM_H +#ifndef MLIB_SYM_H +#define MLIB_SYM_H #ifdef __cplusplus extern "C" { @@ -51,6 +38,39 @@ #include +#ifndef MLIB_BITS_H +# include "bits.h" +#endif + +#ifndef MLIB_HASH_H +# include "hash.h" +#endif + +#ifndef MLIB_SUB_H +# include "sub.h" +#endif + +/*----- Tuning parameters -------------------------------------------------*/ + +/* --- Initial hash table size --- * + * + * This is the initial @mask@ value. It must be of the form %$2^n - 1$%, + * so that it can be used to mask of the bottom bits of a hash value. + */ + +#define SYM_INITSZ 32 /* Size of a new hash table */ + +/* --- Maximum load factor --- * + * + * This parameter controls how much the table has to be loaded before the + * table is extended. The number of elements %$n$%, the number of bins %$b$% + * and the limit %$l$% satisfy the relation %$n < bl$%; if a new item is + * added to the table and this relation is found to be false, the table is + * doubled in size. + */ + +#define SYM_LIMIT(n) ((n) * 2) /* Load factor for growing table */ + /*----- Type definitions --------------------------------------------------*/ /* --- Symbol table --- * @@ -61,9 +81,9 @@ */ typedef struct sym_table { - unsigned long mask; /* Bit mask for hashing purposes */ - size_t c; /* Down counter for growing table */ - struct sym_base **a; /* Array of hash bins */ + hash_table t; + subarena *s; + size_t load; } sym_table; /* --- A symbol table entry --- * @@ -75,48 +95,41 @@ typedef struct sym_table { * sufficient to allow me to manipulate such objects. */ -#define SYM_BUFSZ 16 /* Size of local string buffer */ - typedef struct sym_base { - struct sym_base *next; /* Next symbol in hash bin */ - unsigned long hash; /* Hash value for symbol's name */ - union { - char *p; /* Pointer to name string */ - char b[SYM_BUFSZ]; /* Buffer containing a short name */ - } name; /* Name of this symbol */ + hash_base b; /* Base structure */ + char *name; /* Pointer to name string */ size_t len; /* Length of the symbol's name */ } sym_base; -/* --- A macro to pick a symbol's name out from the mess --- */ +/* --- Macros for picking out useful information --- * + * + * Note that @SYM_LEN@ returns the size of the symbol key. For textual keys, + * this will include the terminating null. + */ -#define SYM_NAME(sy) \ - (((sym_base *)(sy))->len > SYM_BUFSZ ? \ - ((sym_base *)(sy))->name.p : \ - ((sym_base *)(sy))->name.b) +#define SYM_NAME(sy) ((const char *)(((sym_base *)(sy))->name)) +#define SYM_LEN(sy) (((sym_base *)(sy))->len + 0) +#define SYM_HASH(sy) (((sym_base *)(sy))->b.hash + 0) /* --- An iterator block --- */ -typedef struct sym_iter { - sym_table *t; /* Symbol table being iterated */ - sym_base *n; /* Address of next item to return */ - size_t i; /* Index of next hash bin to use */ -} sym_iter; +typedef struct { hash_iter i; } sym_iter; /*----- External functions ------------------------------------------------*/ -/* --- @sym_createTable@ --- * +/* --- @sym_create@ --- * * - * Arguments: @sym_table *t@ = symbol table to initialise + * Arguments: @sym_table *t@ = symbol table to initialize * * Returns: --- * - * Use: Initialises the given symbol table. Raises @EXC_NOMEM@ if + * Use: Initializes the given symbol table. Raises @EXC_NOMEM@ if * there isn't enough memory. */ -extern void sym_createTable(sym_table */*t*/); +extern void sym_create(sym_table */*t*/); -/* --- @sym_destroyTable@ --- * +/* --- @sym_destroy@ --- * * * Arguments: @sym_table *t@ = pointer to symbol table in question * @@ -126,12 +139,12 @@ extern void sym_createTable(sym_table */*t*/); * occupy. */ -extern void sym_destroyTable(sym_table */*t*/); +extern void sym_destroy(sym_table */*t*/); /* --- @sym_find@ --- * * * Arguments: @sym_table *t@ = pointer to symbol table in question - * @const char *n@ = pointer to symbol table to look up + * @const char *n@ = pointer to symbol name to look up * @long l@ = length of the name string or negative to measure * @size_t sz@ = size of desired symbol object, or zero * @unsigned *f@ = pointer to a flag, or null. @@ -144,7 +157,7 @@ extern void sym_destroyTable(sym_table */*t*/); * may be given, in which case the name may contain arbitrary * binary data, or it may be given as a negative number, in * which case the length of the name is calculated as - * @strlen(n)@. + * @strlen(n) + 1@. * * The return value is the address of a pointer to a @sym_base@ * block (which may have other things on the end, as above). If @@ -164,7 +177,7 @@ extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/, /* --- @sym_remove@ --- * * - * Arguments: @sym_table *i@ = pointer to a symbol table object + * Arguments: @sym_table *t@ = pointer to a symbol table object * @void *b@ = pointer to symbol table entry * * Returns: --- @@ -176,7 +189,7 @@ extern void *sym_find(sym_table */*t*/, const char */*n*/, long /*l*/, extern void sym_remove(sym_table */*t*/, void */*b*/); -/* --- @sym_createIter@ --- * +/* --- @sym_mkiter@ --- * * * Arguments: @sym_iter *i@ = pointer to an iterator object * @sym_table *t@ = pointer to a symbol table object @@ -187,7 +200,9 @@ extern void sym_remove(sym_table */*t*/, void */*b*/); * iterate through a symbol table. */ -extern void sym_createIter(sym_iter */*i*/, sym_table */*t*/); +#define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t) + +extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/); /* --- @sym_next@ --- * * @@ -199,6 +214,12 @@ extern void sym_createIter(sym_iter */*i*/, sym_table */*t*/); * returned in any particular order. */ +#define SYM_NEXT(i_, p) do { \ + hash_base *_q; \ + HASH_NEXT(&(i_)->i, _q); \ + (p) = (void *)_q; \ +} while (0) + extern void *sym_next(sym_iter */*i*/); /*----- That's all, folks -------------------------------------------------*/