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