chiark / gitweb /
Version bump.
[mLib] / sym.h
diff --git a/sym.h b/sym.h
index edd3284b174f2ffed15f022427a03eba11fad920..db49ff5625121408ed124fc3ab83bfbe16377667 100644 (file)
--- a/sym.h
+++ b/sym.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: sym.h,v 1.8 1999/08/02 14:45:48 mdw Exp $
+ * $Id: sym.h,v 1.12 2001/01/20 11:49:37 mdw Exp $
  *
  * Symbol table management
  *
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: sym.h,v $
+ * Revision 1.12  2001/01/20 11:49:37  mdw
+ * Export tuning parameters from header file, for the benefit of other
+ * hashtable implementations.  Change the storage of symbol names: store
+ * the name after the allocated symbol block in all cases.  This replaces
+ * the previous complicated and slightly wasteful arrangement.
+ *
+ * Revision 1.11  2000/06/17 10:37:39  mdw
+ * Add support for arena management.
+ *
+ * Revision 1.10  1999/12/10 23:42:04  mdw
+ * Change header file guard names.
+ *
+ * Revision 1.9  1999/08/02 16:53:48  mdw
+ * Improve type safety for sym_iter objects.
+ *
  * Revision 1.8  1999/08/02 14:45:48  mdw
  * Break low-level hashtable code out from sym.
  *
@@ -57,8 +72,8 @@
  *
  */
 
-#ifndef SYM_H
-#define SYM_H
+#ifndef MLIB_SYM_H
+#define MLIB_SYM_H
 
 #ifdef __cplusplus
   extern "C" {
 
 #include <stddef.h>
 
-#ifndef BITS_H
+#ifndef MLIB_BITS_H
 #  include "bits.h"
 #endif
 
-#ifndef HASH_H
+#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 --- *
 
 typedef struct sym_table {
   hash_table t;
+  subarena *s;
   size_t load;
 } sym_table;
 
@@ -99,27 +140,25 @@ 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 {
-  hash_base b;
-  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 hash_iter sym_iter;
+typedef struct { hash_iter i; } sym_iter;
 
 /*----- External functions ------------------------------------------------*/
 
@@ -183,7 +222,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:    ---
@@ -206,7 +245,7 @@ extern void sym_remove(sym_table */*t*/, void */*b*/);
  *             iterate through a symbol table.
  */
 
-#define SYM_MKITER(i, t) HASH_MKITER((i), &(t)->t)
+#define SYM_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t)
 
 extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/);
 
@@ -220,9 +259,9 @@ extern void sym_mkiter(sym_iter */*i*/, sym_table */*t*/);
  *             returned in any particular order.
  */
 
-#define SYM_NEXT(i, p) do {                                            \
+#define SYM_NEXT(i_, p) do {                                           \
   hash_base *_q;                                                       \
-  HASH_NEXT((i), _q);                                                  \
+  HASH_NEXT(&(i_)->i, _q);                                             \
   (p) = (void *)_q;                                                    \
 } while (0)