9aef172835283a719ce30cc6dfbc2431c21b2fd4
[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
93 */
94
95 void *assoc_find(assoc_table *t, atom *a, size_t sz, unsigned *f)
96 {
97 hash_base **bin = HASH_BIN(&t->t, ATOM_HASH(a)), **p;
98 assoc_base *q;
99
100 /* --- Try to find the association --- */
101
102 for (p = bin; *p; p = &(*p)->next) {
103 q = (assoc_base *)*p;
104 if (q->a == a) {
105 *p = q->b.next;
106 q->b.next = *bin;
107 *bin = &q->b;
108 if (f) *f = 1;
109 return (q);
110 }
111 }
112
113 /* --- Failed to find a match --- */
114
115 if (f) *f = 0;
116 if (!sz) return (0);
117
118 /* --- Make a new assoication --- */
119
120 q = x_alloc(t->t.a, sz);
121 q->a = a;
122 q->b.next = *bin;
123 q->b.hash = ATOM_HASH(a);
124 *bin = &q->b;
125
126 /* --- Maybe extend the table --- */
127
128 if (t->load)
129 t->load--;
130 if (!t->load && hash_extend(&t->t))
131 t->load = SYM_LIMIT(t->t.mask + 1);
132 return (q);
133 }
134
135 /* --- @assoc_remove@ --- *
136 *
137 * Arguments: @assoc_table *t@ = pointer to an association table
138 * @void *p@ = pointer to a block to remove
139 *
140 * Returns: ---
141 *
142 * Use: Removes an association from a table.
143 */
144
145 void assoc_remove(assoc_table *t, void *p)
146 {
147 assoc_base *q = p;
148 hash_remove(&t->t, &q->b);
149 x_free(t->t.a, q);
150 t->load++;
151 }
152
153 /* --- @assoc_mkiter@, @assoc_next@ --- *
154 *
155 * Arguments: @assoc_iter *i@ = pointer to an iterator
156 * @assoc_table *t@ = pointer to an association table
157 *
158 * Returns: Next association, or null, for @assoc_next@; nothing for
159 * @assoc_mkiter@.
160 *
161 * Use: Iterates over the associations in a table.
162 */
163
164 void assoc_mkiter(assoc_iter *i, assoc_table *t) { ASSOC_MKITER(i, t); }
165 void *assoc_next(assoc_iter *i) { void *p; ASSOC_NEXT(i, p); return (p); }
166
167 /*----- That's all, folks -------------------------------------------------*/