| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: assoc.c,v 1.3 2001/01/25 21:12:38 mdw Exp $ |
| 4 | * |
| 5 | * Assocation tables |
| 6 | * |
| 7 | * (c) 2000 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the mLib utilities library. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: assoc.c,v $ |
| 33 | * Revision 1.3 2001/01/25 21:12:38 mdw |
| 34 | * Use @ATOM_HASH@ rather than digging in the @atom@ structure. |
| 35 | * |
| 36 | * Revision 1.2 2001/01/21 19:04:59 mdw |
| 37 | * Fix bugs. |
| 38 | * |
| 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. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | /*----- Header files ------------------------------------------------------*/ |
| 46 | |
| 47 | #include "alloc.h" |
| 48 | #include "assoc.h" |
| 49 | #include "atom.h" |
| 50 | #include "hash.h" |
| 51 | |
| 52 | /*----- Main code ---------------------------------------------------------*/ |
| 53 | |
| 54 | /* --- @assoc_create@ --- * |
| 55 | * |
| 56 | * Arguments: @assoc_table *t@ = pointer to an association table |
| 57 | * |
| 58 | * Returns: --- |
| 59 | * |
| 60 | * Use: Creates a new association table. |
| 61 | */ |
| 62 | |
| 63 | void assoc_create(assoc_table *t) |
| 64 | { |
| 65 | hash_create(&t->t, SYM_INITSZ); |
| 66 | t->load = SYM_LIMIT(SYM_INITSZ); |
| 67 | } |
| 68 | |
| 69 | /* --- @assoc_destroy@ --- * |
| 70 | * |
| 71 | * Arguments: @assoc_table *t@ = pointer to an association table |
| 72 | * |
| 73 | * Returns: --- |
| 74 | * |
| 75 | * Use: Destroys an association table. |
| 76 | */ |
| 77 | |
| 78 | void assoc_destroy(assoc_table *t) |
| 79 | { |
| 80 | hash_iter i; |
| 81 | |
| 82 | HASH_MKITER(&i, &t->t); |
| 83 | for (;;) { |
| 84 | hash_base *p; |
| 85 | HASH_NEXT(&i, p); |
| 86 | if (!p) |
| 87 | break; |
| 88 | x_free(t->t.a, p); |
| 89 | } |
| 90 | hash_destroy(&t->t); |
| 91 | } |
| 92 | |
| 93 | /* --- @assoc_find@ --- * |
| 94 | * |
| 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 |
| 99 | * |
| 100 | * Returns: A pointer to the item located or null. |
| 101 | * |
| 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. |
| 108 | * |
| 109 | * All the atoms used in a particular table should |
| 110 | */ |
| 111 | |
| 112 | void *assoc_find(assoc_table *t, atom *a, size_t sz, unsigned *f) |
| 113 | { |
| 114 | hash_base **bin = HASH_BIN(&t->t, ATOM_HASH(a)), **p; |
| 115 | assoc_base *q; |
| 116 | |
| 117 | /* --- Try to find the association --- */ |
| 118 | |
| 119 | for (p = bin; *p; p = &(*p)->next) { |
| 120 | q = (assoc_base *)*p; |
| 121 | if (q->a == a) { |
| 122 | *p = q->b.next; |
| 123 | q->b.next = *bin; |
| 124 | *bin = &q->b; |
| 125 | if (f) *f = 1; |
| 126 | return (q); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* --- Failed to find a match --- */ |
| 131 | |
| 132 | if (f) *f = 0; |
| 133 | if (!sz) return (0); |
| 134 | |
| 135 | /* --- Make a new assoication --- */ |
| 136 | |
| 137 | q = x_alloc(t->t.a, sz); |
| 138 | q->a = a; |
| 139 | q->b.next = *bin; |
| 140 | q->b.hash = ATOM_HASH(a); |
| 141 | *bin = &q->b; |
| 142 | |
| 143 | /* --- Maybe extend the table --- */ |
| 144 | |
| 145 | if (t->load) |
| 146 | t->load--; |
| 147 | if (!t->load && hash_extend(&t->t)) |
| 148 | t->load = SYM_LIMIT(t->t.mask + 1); |
| 149 | return (q); |
| 150 | } |
| 151 | |
| 152 | /* --- @assoc_remove@ --- * |
| 153 | * |
| 154 | * Arguments: @assoc_table *t@ = pointer to an association table |
| 155 | * @void *p@ = pointer to a block to remove |
| 156 | * |
| 157 | * Returns: --- |
| 158 | * |
| 159 | * Use: Removes an association from a table. |
| 160 | */ |
| 161 | |
| 162 | void assoc_remove(assoc_table *t, void *p) |
| 163 | { |
| 164 | assoc_base *q = p; |
| 165 | hash_remove(&t->t, &q->b); |
| 166 | x_free(t->t.a, q); |
| 167 | t->load++; |
| 168 | } |
| 169 | |
| 170 | /* --- @assoc_mkiter@, @assoc_next@ --- * |
| 171 | * |
| 172 | * Arguments: @assoc_iter *i@ = pointer to an iterator |
| 173 | * @assoc_table *t@ = pointer to an association table |
| 174 | * |
| 175 | * Returns: Next association, or null, for @assoc_next@; nothing for |
| 176 | * @assoc_mkiter@. |
| 177 | * |
| 178 | * Use: Iterates over the associations in a table. |
| 179 | */ |
| 180 | |
| 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); } |
| 183 | |
| 184 | /*----- That's all, folks -------------------------------------------------*/ |