chiark / gitweb /
Improve type safety for sym_iter objects.
[mLib] / sym.h
diff --git a/sym.h b/sym.h
index 35a3fa6b05cfb8e6c56488b3b95992bd9aa759d9..1de62f1c7700419338daa5a55be326ba59b28bc7 100644 (file)
--- a/sym.h
+++ b/sym.h
@@ -1,36 +1,62 @@
 /* -*-c-*-
  *
- * $Id: sym.h,v 1.1 1998/06/17 23:44:42 mdw Exp $
+ * $Id: sym.h,v 1.9 1999/08/02 16:53:48 mdw Exp $
  *
  * Symbol table management
  *
  * (c) 1998 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------*
+/*----- Licensing notice --------------------------------------------------* 
  *
  * This file is part of the mLib utilities library.
  *
  * mLib is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
+ * 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 General Public License for more details.
- *
- * You should have received a copy of the GNU 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.
+ * 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.1  1998/06/17 23:44:42  mdw
- * Initial revision
+ * 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.
+ *
+ * Revision 1.7  1999/06/01 09:49:33  mdw
+ * Allow things to be looked up by just their caller-supplied hashes.  This
+ * actually needs to be thought through better.
+ *
+ * Revision 1.6  1999/05/26 21:08:31  mdw
+ * Rename symbols in line with newer conventions.
+ *
+ * Revision 1.5  1999/05/13 22:48:37  mdw
+ * Change `-ise' to `-ize' throughout.
+ *
+ * Revision 1.4  1999/05/06 19:51:35  mdw
+ * Reformatted the LGPL notice a little bit.
+ *
+ * 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
  *
  */
 
 
 #include <stddef.h>
 
+#ifndef BITS_H
+#  include "bits.h"
+#endif
+
+#ifndef HASH_H
+#  include "hash.h"
+#endif
+
 /*----- Type definitions --------------------------------------------------*/
 
 /* --- Symbol table --- *
@@ -55,9 +89,8 @@
  */
 
 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;
+  size_t load;
 } sym_table;
 
 /* --- A symbol table entry --- *
@@ -72,8 +105,7 @@ typedef struct sym_table {
 #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 */
+  hash_base b;
   union {
     char *p;                           /* Pointer to name string */
     char b[SYM_BUFSZ];                 /* Buffer containing a short name */
@@ -81,29 +113,32 @@ typedef struct sym_base {
   size_t len;                          /* Length of the symbol's name */
 } sym_base;
 
+/* --- A macro to pick a symbol's name out from the mess --- */
+
+#define SYM_NAME(sy)                                                   \
+  (((sym_base *)(sy))->len > SYM_BUFSZ ?                               \
+   ((sym_base *)(sy))->name.p :                                                \
+   ((sym_base *)(sy))->name.b)
+
 /* --- 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
  *
@@ -113,7 +148,7 @@ extern void sym_createTable(sym_table */*t*/);
  *             occupy.
  */
 
-extern void sym_destroyTable(sym_table */*t*/);
+extern void sym_destroy(sym_table */*t*/);
 
 /* --- @sym_find@ --- *
  *
@@ -131,7 +166,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
@@ -163,7 +198,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
@@ -174,7 +209,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@ --- *
  *
@@ -186,6 +223,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 -------------------------------------------------*/