2 * Copyright (C) 2000, Matias Atria
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 DviFree2Func free_func;
28 #define MDVI_ASSOC_SIZE 31
30 static void assoc_free(DviHashKey key, void *ptr)
32 DviAssoc *assoc = (DviAssoc *)ptr;
34 DEBUG((4, "Destroying association `%s'\n", (char *)key));
36 assoc->free_func(key, assoc->data);
40 int mdvi_assoc_put(DviContext *dvi, char *key, void *data, DviFree2Func f)
45 if(dvi->assoc.buckets == NULL) {
46 mdvi_hash_create(&dvi->assoc, MDVI_ASSOC_SIZE);
47 dvi->assoc.hash_free = assoc_free;
49 assoc = xalloc(DviAssoc);
53 ok = mdvi_hash_add(&dvi->assoc, MDVI_KEY(key),
54 assoc, MDVI_HASH_UNIQUE);
62 void *mdvi_assoc_get(DviContext *dvi, char *key)
66 if(dvi->assoc.buckets == NULL)
68 assoc = (DviAssoc *)mdvi_hash_lookup(&dvi->assoc, MDVI_KEY(key));
69 return assoc ? assoc->data : NULL;
72 void *mdvi_assoc_del(DviContext *dvi, char *key)
77 if(dvi->assoc.buckets == NULL)
79 assoc = mdvi_hash_remove(&dvi->assoc, MDVI_KEY(key));
87 void mdvi_assoc_free(DviContext *dvi, char *key)
89 if(dvi->assoc.buckets) {
90 /* this will call `assoc_free' */
91 mdvi_hash_destroy_key(&dvi->assoc, MDVI_KEY(key));
95 void mdvi_assoc_flush(DviContext *dvi)
97 if(dvi->assoc.buckets)
98 mdvi_hash_reset(&dvi->assoc, 0);