3 * $Id: assoc.c,v 1.3 2001/01/25 21:12:38 mdw Exp $
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 2001/01/25 21:12:38 mdw
34 * Use @ATOM_HASH@ rather than digging in the @atom@ structure.
36 * Revision 1.2 2001/01/21 19:04:59 mdw
39 * Revision 1.1 2001/01/20 11:50:58 mdw
40 * Hash tables indexed by atoms, to avoid expense of hashing keys on each
41 * lookup, and to reduce storage used by key texts.
45 /*----- Header files ------------------------------------------------------*/
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @assoc_create@ --- *
56 * Arguments: @assoc_table *t@ = pointer to an association table
60 * Use: Creates a new association table.
63 void assoc_create(assoc_table *t)
65 hash_create(&t->t, SYM_INITSZ);
66 t->load = SYM_LIMIT(SYM_INITSZ);
69 /* --- @assoc_destroy@ --- *
71 * Arguments: @assoc_table *t@ = pointer to an association table
75 * Use: Destroys an association table.
78 void assoc_destroy(assoc_table *t)
82 HASH_MKITER(&i, &t->t);
93 /* --- @assoc_find@ --- *
95 * Arguments: @assoc_table *t@ = pointer to an association table
96 * @atom *a@ = an atom to label the item
97 * @size_t sz@ = size of block to allocate
98 * @unsigned *f@ = pointer to `found' flag
100 * Returns: A pointer to the item located or null.
102 * Use: Looks up an atom in an association table. If the atom is
103 * found, the association is returned. If not, and @sz@ is
104 * zero, a null pointer is returned. Otherwise, a block of size
105 * @sz@ is allocated, its @assoc_base@ header is filled in, and
106 * the pointer returned. The flag @*f@ is cleared if the item
107 * couldn't be found, or set if it was.
109 * All the atoms used in a particular table should
112 void *assoc_find(assoc_table *t, atom *a, size_t sz, unsigned *f)
114 hash_base **bin = HASH_BIN(&t->t, ATOM_HASH(a)), **p;
117 /* --- Try to find the association --- */
119 for (p = bin; *p; p = &(*p)->next) {
120 q = (assoc_base *)*p;
130 /* --- Failed to find a match --- */
135 /* --- Make a new assoication --- */
137 q = x_alloc(t->t.a, sz);
140 q->b.hash = ATOM_HASH(a);
143 /* --- Maybe extend the table --- */
147 if (!t->load && hash_extend(&t->t))
148 t->load = SYM_LIMIT(t->t.mask + 1);
152 /* --- @assoc_remove@ --- *
154 * Arguments: @assoc_table *t@ = pointer to an association table
155 * @void *p@ = pointer to a block to remove
159 * Use: Removes an association from a table.
162 void assoc_remove(assoc_table *t, void *p)
165 hash_remove(&t->t, &q->b);
170 /* --- @assoc_mkiter@, @assoc_next@ --- *
172 * Arguments: @assoc_iter *i@ = pointer to an iterator
173 * @assoc_table *t@ = pointer to an association table
175 * Returns: Next association, or null, for @assoc_next@; nothing for
178 * Use: Iterates over the associations in a table.
181 void assoc_mkiter(assoc_iter *i, assoc_table *t) { ASSOC_MKITER(i, t); }
182 void *assoc_next(assoc_iter *i) { void *p; ASSOC_NEXT(i, p); return (p); }
184 /*----- That's all, folks -------------------------------------------------*/