chiark / gitweb /
Remove redundant initialization of `sub'.
[mLib] / assoc.c
1 /* -*-c-*-
2  *
3  * $Id: assoc.c,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.c,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 /*----- Header files ------------------------------------------------------*/
40
41 #include "alloc.h"
42 #include "assoc.h"
43 #include "atom.h"
44 #include "hash.h"
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 /* --- @assoc_create@ --- *
49  *
50  * Arguments:   @assoc_table *t@ = pointer to an association table
51  *
52  * Returns:     ---
53  *
54  * Use:         Creates a new association table.
55  */
56
57 void assoc_create(assoc_table *t)
58 {
59   hash_create(&t->t, SYM_INITSZ);
60   t->load = SYM_LIMIT(SYM_INITSZ);
61 }
62
63 /* --- @assoc_destroy@ --- *
64  *
65  * Arguments:   @assoc_table *t@ = pointer to an association table
66  *
67  * Returns:     ---
68  *
69  * Use:         Destroys an association table.
70  */
71
72 void assoc_destroy(assoc_table *t)
73 {
74   hash_iter i;
75
76   HASH_MKITER(&i, &t->t);
77   for (;;) {
78     hash_base *p;
79     HASH_NEXT(&i, p);
80     x_free(t->t.a, p);
81   }
82   hash_destroy(&t->t);
83 }
84
85 /* --- @assoc_find@ --- *
86  *
87  * Arguments:   @assoc_table *t@ = pointer to an association table
88  *              @atom *a@ = an atom to label the item
89  *              @size_t sz@ = size of block to allocate
90  *              @unsigned *f@ = pointer to `found' flag
91  *
92  * Returns:     A pointer to the item located or null.
93  *
94  * Use:         Looks up an atom in an association table.  If the atom is
95  *              found, the association is returned.  If not, and @sz@ is
96  *              zero, a null pointer is returned.  Otherwise, a block of size
97  *              @sz@ is allocated, its @assoc_base@ header is filled in, and
98  *              the pointer returned.  The flag @*f@ is cleared if the item
99  *              couldn't be found, or set if it was.
100  *
101  *              All the atoms used in a particular table should 
102  */
103
104 void *assoc_find(assoc_table *t, atom *a, size_t sz, unsigned *f)
105 {
106   hash_base **bin = HASH_BIN(&t->t, a->b.b.hash), **p;
107   assoc_base *q;
108
109   /* --- Try to find the association --- */
110
111   for (p = bin; *p; p = &(*p)->next) {
112     q = (assoc_base *)*p;
113     if (q->a == a) {
114       *p = q->b.next;
115       q->b.next = *bin;
116       *bin = &q->b;
117       if (f) *f = 1;
118       return (q);
119     }
120   }
121
122   /* --- Failed to find a match --- */
123
124   if (f) *f = 0;
125   if (!sz) return (0);
126
127   /* --- Make a new assoication --- */
128
129   q = x_alloc(t->t.a, sz);
130   q->b.next = *bin;
131   q->b.hash = ATOM_HASH(a);
132   *bin = &q->b;
133
134   /* --- Maybe extend the table --- */
135
136   if (t->load)
137     t->load--;
138   if (!t->load && hash_extend(&t->t))
139     t->load = SYM_LIMIT(t->t.mask + 1);
140   return (q);
141 }
142
143 /* --- @assoc_remove@ --- *
144  *
145  * Arguments:   @assoc_table *t@ = pointer to an association table
146  *              @void *p@ = pointer to a block to remove
147  *
148  * Returns:     ---
149  *
150  * Use:         Removes an association from a table.
151  */
152
153 void assoc_remove(assoc_table *t, void *p)
154 {
155   assoc_base *q = p;
156   hash_remove(&t->t, &q->b);
157   x_free(t->t.a, q);
158   t->load++;
159 }
160
161 /* --- @assoc_mkiter@, @assoc_next@ --- *
162  *
163  * Arguments:   @assoc_iter *i@ = pointer to an iterator
164  *              @assoc_table *t@ = pointer to an association table
165  *
166  * Returns:     Next association, or null, for @assoc_next@; nothing for
167  *              @assoc_mkiter@.
168  *
169  * Use:         Iterates over the associations in a table.
170  */
171
172 void assoc_mkiter(assoc_iter *i, assoc_table *t) { ASSOC_MKITER(i, t); }
173 void *assoc_next(assoc_iter *i) { void *p; ASSOC_NEXT(i, p); return (p); }
174
175 /*----- That's all, folks -------------------------------------------------*/