chiark / gitweb /
Include `crc32.h' for @CRC32@ macro.
[mLib] / assoc.h
1 /* -*-c-*-
2  *
3  * $Id: assoc.h,v 1.1 2001/01/20 11:50:58 mdw Exp $
4  *
5  * Assocation tables
6  *
7  * (c) 2000 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: assoc.h,v $
33  * Revision 1.1  2001/01/20 11:50:58  mdw
34  * Hash tables indexed by atoms, to avoid expense of hashing keys on each
35  * lookup, and to reduce storage used by key texts.
36  *
37  */
38
39 #ifndef MLIB_ASSOC_H
40 #define MLIB_ASSOC_H
41
42 #ifdef __cplusplus
43   extern "C" {
44 #endif
45
46 /*----- Header files ------------------------------------------------------*/
47
48 #ifndef MLIB_ATOM_H
49 #  include "atom.h"
50 #endif
51
52 #ifndef MLIB_HASH_H
53 #  include "hash.h"
54 #endif
55
56 /*----- Data structures ---------------------------------------------------*/
57
58 typedef struct assoc_table {
59   hash_table t;
60   size_t load;
61 } assoc_table;
62
63 typedef struct assoc_base {
64   hash_base b;
65   atom *a;
66 } assoc_base;
67
68 typedef struct { hash_iter i; } assoc_iter;
69
70 /*----- Functions provided ------------------------------------------------*/
71
72 /* --- @assoc_create@ --- *
73  *
74  * Arguments:   @assoc_table *t@ = pointer to an association table
75  *
76  * Returns:     ---
77  *
78  * Use:         Creates a new association table.
79  */
80
81 extern void assoc_create(assoc_table */*t*/);
82
83 /* --- @assoc_destroy@ --- *
84  *
85  * Arguments:   @assoc_table *t@ = pointer to an association table
86  *
87  * Returns:     ---
88  *
89  * Use:         Destroys an association table.
90  */
91
92 extern void assoc_destroy(assoc_table */*t*/);
93
94 /* --- @assoc_find@ --- *
95  *
96  * Arguments:   @assoc_table *t@ = pointer to an association table
97  *              @atom *a@ = an atom to label the item
98  *              @size_t sz@ = size of block to allocate
99  *              @unsigned *f@ = pointer to `found' flag
100  *
101  * Returns:     A pointer to the item located or null.
102  *
103  * Use:         Looks up an atom in an association table.  If the atom is
104  *              found, the association is returned.  If not, and @sz@ is
105  *              zero, a null pointer is returned.  Otherwise, a block of size
106  *              @sz@ is allocated, its @assoc_base@ header is filled in, and
107  *              the pointer returned.  The flag @*f@ is cleared if the item
108  *              couldn't be found, or set if it was.
109  *
110  *              All the atoms used in a particular table should 
111  */
112
113 extern void *assoc_find(assoc_table */*t*/, atom */*a*/,
114                         size_t /*sz*/, unsigned */*f*/);
115
116 /* --- @assoc_remove@ --- *
117  *
118  * Arguments:   @assoc_table *t@ = pointer to an association table
119  *              @void *p@ = pointer to a block to remove
120  *
121  * Returns:     ---
122  *
123  * Use:         Removes an association from a table.
124  */
125
126 extern void assoc_remove(assoc_table */*t*/, void */*p*/);
127
128 /* --- @assoc_mkiter@, @assoc_next@ --- *
129  *
130  * Arguments:   @assoc_iter *i@ = pointer to an iterator
131  *              @assoc_table *t@ = pointer to an association table
132  *
133  * Returns:     Next association, or null, for @assoc_next@; nothing for
134  *              @assoc_mkiter@.
135  *
136  * Use:         Iterates over the associations in a table.
137  */
138
139 #define ASSOC_MKITER(i_, t_) HASH_MKITER(&(i_)->i, &(t_)->t)
140
141 #define ASSOC_NEXT(i_, p) do {                                          \
142   hash_base *_q;                                                        \
143   HASH_NEXT(&(i_)->i, _q);                                              \
144   (p) = (void *)_q;                                                     \
145 } while (0)
146
147 extern void assoc_mkiter(assoc_iter */*i*/, assoc_table */*t*/);
148 extern void *assoc_next(assoc_iter */*i*/);
149
150 /*----- That's all, folks -------------------------------------------------*/
151
152 #ifdef __cplusplus
153   }
154 #endif
155
156 #endif