struct/assoc.[ch]: Fix some commentary typos.
[mLib] / struct / assoc.c
1 /* -*-c-*-
2 *
3 * Assocation tables
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "alloc.h"
31 #include "assoc.h"
32 #include "atom.h"
33 #include "hash.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 /* --- @assoc_create@ --- *
38 *
39 * Arguments: @assoc_table *t@ = pointer to an association table
40 *
41 * Returns: ---
42 *
43 * Use: Creates a new association table.
44 */
45
46 void assoc_create(assoc_table *t)
47 {
48 hash_create(&t->t, SYM_INITSZ);
49 t->load = SYM_LIMIT(SYM_INITSZ);
50 }
51
52 /* --- @assoc_destroy@ --- *
53 *
54 * Arguments: @assoc_table *t@ = pointer to an association table
55 *
56 * Returns: ---
57 *
58 * Use: Destroys an association table.
59 */
60
61 void assoc_destroy(assoc_table *t)
62 {
63 hash_iter i;
64
65 HASH_MKITER(&i, &t->t);
66 for (;;) {
67 hash_base *p;
68 HASH_NEXT(&i, p);
69 if (!p)
70 break;
71 x_free(t->t.a, p);
72 }
73 hash_destroy(&t->t);
74 }
75
76 /* --- @assoc_find@ --- *
77 *
78 * Arguments: @assoc_table *t@ = pointer to an association table
79 * @atom *a@ = an atom to label the item
80 * @size_t sz@ = size of block to allocate
81 * @unsigned *f@ = pointer to `found' flag
82 *
83 * Returns: A pointer to the item located or null.
84 *
85 * Use: Looks up an atom in an association table. If the atom is
86 * found, the association is returned. If not, and @sz@ is
87 * zero, a null pointer is returned. Otherwise, a block of size
88 * @sz@ is allocated, its @assoc_base@ header is filled in, and
89 * the pointer returned. The flag @*f@ is cleared if the item
90 * couldn't be found, or set if it was.
91 *
92 * All the atoms used in a particular table should come from the
93 * same atom table.
94 */
95
96 void *assoc_find(assoc_table *t, atom *a, size_t sz, unsigned *f)
97 {
98 hash_base **bin = HASH_BIN(&t->t, ATOM_HASH(a)), **p;
99 assoc_base *q;
100
101 /* --- Try to find the association --- */
102
103 for (p = bin; *p; p = &(*p)->next) {
104 q = (assoc_base *)*p;
105 if (q->a == a) {
106 *p = q->b.next;
107 q->b.next = *bin;
108 *bin = &q->b;
109 if (f) *f = 1;
110 return (q);
111 }
112 }
113
114 /* --- Failed to find a match --- */
115
116 if (f) *f = 0;
117 if (!sz) return (0);
118
119 /* --- Make a new association --- */
120
121 q = x_alloc(t->t.a, sz);
122 q->a = a;
123 q->b.next = *bin;
124 q->b.hash = ATOM_HASH(a);
125 *bin = &q->b;
126
127 /* --- Maybe extend the table --- */
128
129 if (t->load)
130 t->load--;
131 if (!t->load && hash_extend(&t->t))
132 t->load = SYM_LIMIT(t->t.mask + 1);
133 return (q);
134 }
135
136 /* --- @assoc_remove@ --- *
137 *
138 * Arguments: @assoc_table *t@ = pointer to an association table
139 * @void *p@ = pointer to a block to remove
140 *
141 * Returns: ---
142 *
143 * Use: Removes an association from a table.
144 */
145
146 void assoc_remove(assoc_table *t, void *p)
147 {
148 assoc_base *q = p;
149 hash_remove(&t->t, &q->b);
150 x_free(t->t.a, q);
151 t->load++;
152 }
153
154 /* --- @assoc_mkiter@, @assoc_next@ --- *
155 *
156 * Arguments: @assoc_iter *i@ = pointer to an iterator
157 * @assoc_table *t@ = pointer to an association table
158 *
159 * Returns: Next association, or null, for @assoc_next@; nothing for
160 * @assoc_mkiter@.
161 *
162 * Use: Iterates over the associations in a table.
163 */
164
165 void assoc_mkiter(assoc_iter *i, assoc_table *t) { ASSOC_MKITER(i, t); }
166 void *assoc_next(assoc_iter *i) { void *p; ASSOC_NEXT(i, p); return (p); }
167
168 /*----- That's all, folks -------------------------------------------------*/