5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
37 /* --- @assoc_create@ --- *
39 * Arguments: @assoc_table *t@ = pointer to an association table
43 * Use: Creates a new association table.
46 void assoc_create(assoc_table *t)
48 hash_create(&t->t, SYM_INITSZ);
49 t->load = SYM_LIMIT(SYM_INITSZ);
52 /* --- @assoc_destroy@ --- *
54 * Arguments: @assoc_table *t@ = pointer to an association table
58 * Use: Destroys an association table.
61 void assoc_destroy(assoc_table *t)
65 HASH_MKITER(&i, &t->t);
76 /* --- @assoc_find@ --- *
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
83 * Returns: A pointer to the item located or null.
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.
92 * All the atoms used in a particular table should
95 void *assoc_find(assoc_table *t, atom *a, size_t sz, unsigned *f)
97 hash_base **bin = HASH_BIN(&t->t, ATOM_HASH(a)), **p;
100 /* --- Try to find the association --- */
102 for (p = bin; *p; p = &(*p)->next) {
103 q = (assoc_base *)*p;
113 /* --- Failed to find a match --- */
118 /* --- Make a new assoication --- */
120 q = x_alloc(t->t.a, sz);
123 q->b.hash = ATOM_HASH(a);
126 /* --- Maybe extend the table --- */
130 if (!t->load && hash_extend(&t->t))
131 t->load = SYM_LIMIT(t->t.mask + 1);
135 /* --- @assoc_remove@ --- *
137 * Arguments: @assoc_table *t@ = pointer to an association table
138 * @void *p@ = pointer to a block to remove
142 * Use: Removes an association from a table.
145 void assoc_remove(assoc_table *t, void *p)
148 hash_remove(&t->t, &q->b);
153 /* --- @assoc_mkiter@, @assoc_next@ --- *
155 * Arguments: @assoc_iter *i@ = pointer to an iterator
156 * @assoc_table *t@ = pointer to an association table
158 * Returns: Next association, or null, for @assoc_next@; nothing for
161 * Use: Iterates over the associations in a table.
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); }
167 /*----- That's all, folks -------------------------------------------------*/