1 | /*
|
---|
2 | * Copyright (c) 2008 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | /*
|
---|
36 | * This is an implementation of generic chained hash table.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <adt/hash_table.h>
|
---|
40 | #include <adt/list.h>
|
---|
41 | #include <unistd.h>
|
---|
42 | #include <malloc.h>
|
---|
43 | #include <assert.h>
|
---|
44 | #include <str.h>
|
---|
45 |
|
---|
46 | /** Create chained hash table.
|
---|
47 | *
|
---|
48 | * @param h Hash table structure. Will be initialized by this call.
|
---|
49 | * @param m Number of hash table buckets.
|
---|
50 | * @param max_keys Maximal number of keys needed to identify an item.
|
---|
51 | * @param op Hash table operations structure.
|
---|
52 | *
|
---|
53 | * @return True on success
|
---|
54 | *
|
---|
55 | */
|
---|
56 | bool hash_table_create(hash_table_t *h, hash_count_t m, hash_count_t max_keys,
|
---|
57 | hash_table_operations_t *op)
|
---|
58 | {
|
---|
59 | assert(h);
|
---|
60 | assert(op && op->hash && op->compare);
|
---|
61 | assert(max_keys > 0);
|
---|
62 |
|
---|
63 | h->entry = malloc(m * sizeof(list_t));
|
---|
64 | if (!h->entry)
|
---|
65 | return false;
|
---|
66 |
|
---|
67 | memset((void *) h->entry, 0, m * sizeof(list_t));
|
---|
68 |
|
---|
69 | hash_count_t i;
|
---|
70 | for (i = 0; i < m; i++)
|
---|
71 | list_initialize(&h->entry[i]);
|
---|
72 |
|
---|
73 | h->entries = m;
|
---|
74 | h->max_keys = max_keys;
|
---|
75 | h->op = op;
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /** Destroy a hash table instance.
|
---|
81 | *
|
---|
82 | * @param h Hash table to be destroyed.
|
---|
83 | *
|
---|
84 | */
|
---|
85 | void hash_table_destroy(hash_table_t *h)
|
---|
86 | {
|
---|
87 | assert(h);
|
---|
88 | assert(h->entry);
|
---|
89 |
|
---|
90 | free(h->entry);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** Insert item into a hash table.
|
---|
94 | *
|
---|
95 | * @param h Hash table.
|
---|
96 | * @param key Array of all keys necessary to compute hash index.
|
---|
97 | * @param item Item to be inserted into the hash table.
|
---|
98 | */
|
---|
99 | void hash_table_insert(hash_table_t *h, unsigned long key[], link_t *item)
|
---|
100 | {
|
---|
101 | assert(item);
|
---|
102 | assert(h && h->op && h->op->hash && h->op->compare);
|
---|
103 |
|
---|
104 | hash_index_t chain = h->op->hash(key);
|
---|
105 | assert(chain < h->entries);
|
---|
106 |
|
---|
107 | list_append(item, &h->entry[chain]);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Search hash table for an item matching keys.
|
---|
111 | *
|
---|
112 | * @param h Hash table.
|
---|
113 | * @param key Array of all keys needed to compute hash index.
|
---|
114 | *
|
---|
115 | * @return Matching item on success, NULL if there is no such item.
|
---|
116 | *
|
---|
117 | */
|
---|
118 | link_t *hash_table_find(hash_table_t *h, unsigned long key[])
|
---|
119 | {
|
---|
120 | assert(h && h->op && h->op->hash && h->op->compare);
|
---|
121 |
|
---|
122 | hash_index_t chain = h->op->hash(key);
|
---|
123 | assert(chain < h->entries);
|
---|
124 |
|
---|
125 | list_foreach(h->entry[chain], cur) {
|
---|
126 | if (h->op->compare(key, h->max_keys, cur)) {
|
---|
127 | /*
|
---|
128 | * The entry is there.
|
---|
129 | */
|
---|
130 | return cur;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | return NULL;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** Remove all matching items from hash table.
|
---|
138 | *
|
---|
139 | * For each removed item, h->remove_callback() is called.
|
---|
140 | *
|
---|
141 | * @param h Hash table.
|
---|
142 | * @param key Array of keys that will be compared against items of
|
---|
143 | * the hash table.
|
---|
144 | * @param keys Number of keys in the 'key' array.
|
---|
145 | *
|
---|
146 | */
|
---|
147 | void hash_table_remove(hash_table_t *h, unsigned long key[], hash_count_t keys)
|
---|
148 | {
|
---|
149 | assert(h && h->op && h->op->hash && h->op->compare &&
|
---|
150 | h->op->remove_callback);
|
---|
151 | assert(keys <= h->max_keys);
|
---|
152 |
|
---|
153 | if (keys == h->max_keys) {
|
---|
154 | /*
|
---|
155 | * All keys are known, hash_table_find() can be used to find the
|
---|
156 | * entry.
|
---|
157 | */
|
---|
158 |
|
---|
159 | link_t *cur = hash_table_find(h, key);
|
---|
160 | if (cur) {
|
---|
161 | list_remove(cur);
|
---|
162 | h->op->remove_callback(cur);
|
---|
163 | }
|
---|
164 |
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * Fewer keys were passed.
|
---|
170 | * Any partially matching entries are to be removed.
|
---|
171 | */
|
---|
172 | hash_index_t chain;
|
---|
173 | for (chain = 0; chain < h->entries; chain++) {
|
---|
174 | for (link_t *cur = h->entry[chain].head.next;
|
---|
175 | cur != &h->entry[chain].head;
|
---|
176 | cur = cur->next) {
|
---|
177 | if (h->op->compare(key, keys, cur)) {
|
---|
178 | link_t *hlp;
|
---|
179 |
|
---|
180 | hlp = cur;
|
---|
181 | cur = cur->prev;
|
---|
182 |
|
---|
183 | list_remove(hlp);
|
---|
184 | h->op->remove_callback(hlp);
|
---|
185 |
|
---|
186 | continue;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Apply function to all items in hash table.
|
---|
193 | *
|
---|
194 | * @param h Hash table.
|
---|
195 | * @param f Function to be applied.
|
---|
196 | * @param arg Argument to be passed to the function.
|
---|
197 | *
|
---|
198 | */
|
---|
199 | void hash_table_apply(hash_table_t *h, void (*f)(link_t *, void *), void *arg)
|
---|
200 | {
|
---|
201 | hash_index_t bucket;
|
---|
202 |
|
---|
203 | for (bucket = 0; bucket < h->entries; bucket++) {
|
---|
204 | list_foreach(h->entry[bucket], cur) {
|
---|
205 | f(cur, arg);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** @}
|
---|
211 | */
|
---|