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 come from the
96 void *assoc_find(assoc_table
*t
, atom
*a
, size_t sz
, unsigned *f
)
98 hash_base
**bin
= HASH_BIN(&t
->t
, ATOM_HASH(a
)), **p
;
101 /* --- Try to find the association --- */
103 for (p
= bin
; *p
; p
= &(*p
)->next
) {
104 q
= (assoc_base
*)*p
;
114 /* --- Failed to find a match --- */
119 /* --- Make a new association --- */
121 q
= x_alloc(t
->t
.a
, sz
);
124 q
->b
.hash
= ATOM_HASH(a
);
127 /* --- Maybe extend the table --- */
131 if (!t
->load
&& hash_extend(&t
->t
))
132 t
->load
= SYM_LIMIT(t
->t
.mask
+ 1);
136 /* --- @assoc_remove@ --- *
138 * Arguments: @assoc_table *t@ = pointer to an association table
139 * @void *p@ = pointer to a block to remove
143 * Use: Removes an association from a table.
146 void assoc_remove(assoc_table
*t
, void *p
)
149 hash_remove(&t
->t
, &q
->b
);
154 /* --- @assoc_mkiter@, @assoc_next@ --- *
156 * Arguments: @assoc_iter *i@ = pointer to an iterator
157 * @assoc_table *t@ = pointer to an association table
159 * Returns: Next association, or null, for @assoc_next@; nothing for
162 * Use: Iterates over the associations in a table.
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
); }
168 /*----- That's all, folks -------------------------------------------------*/