1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
13 * Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk>
15 * Permission is hereby granted, free of charge, to any person obtaining a copy
16 * of this software and associated documentation files (the "Software"), to
17 * deal in the Software without restriction, including without limitation the
18 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 * sell copies of the Software, and to permit persons to whom the Software is
20 * furnished to do so, subject to the following conditions:
22 * The above copyright notice and this permission notice shall be included in
23 * all copies of the Software and its documentation and acknowledgment shall be
24 * given in the documentation and software packages that this Software was
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
31 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 /*****************************************************************************/
44 create_hashtable(unsigned int minsize, float maxloadfactor,
45 unsigned int (*hashf) (void*),
46 int (*eqf) (void*,void*))
49 unsigned int i, size = 1u;
51 /* Check requested hashtable isn't too large */
52 if (minsize > (1u << 31))
55 /* Enforce size as power of 2 */
56 while (size < minsize)
59 h = (struct hashtable *)malloc(sizeof(struct hashtable));
64 h->table = (struct entry **)malloc(sizeof(struct entry*) * size);
73 for (i=0; i < size; i++)
76 h->tablelength = size;
80 h->loadlimit = (unsigned int) ((float)size * maxloadfactor);
85 /*****************************************************************************/
87 hash(struct hashtable *h, void *k)
89 /* Aim to protect against poor hash functions by adding logic here
90 * - logic taken from java 1.4 hashtable source */
92 unsigned int i = h->hashfn(k);
95 i ^= ((i >> 14) | (i << 18)); /* >>> */
97 i ^= ((i >> 10) | (i << 22)); /* >>> */
102 /*****************************************************************************/
104 indexFor(unsigned int tablelength, unsigned int hashvalue)
106 /* Only works if tablelength == 2^N */
107 return (hashvalue & (tablelength - 1u));
110 /*****************************************************************************/
112 hashtable_expand(struct hashtable *h)
114 /* Double the size of the table to accomodate more entries */
115 struct entry **newtable;
118 unsigned int newsize, i, index;
120 /* Check we're not hitting max capacity */
121 if (0 == (newsize = (h->tablelength << 1)))
124 newtable = (struct entry **)malloc(sizeof(struct entry*) * newsize);
126 if (newtable != NULL)
128 memset(newtable, 0, newsize * sizeof(struct entry *));
130 /* This algorithm is not 'stable'. ie. it reverses the list
131 * when it transfers entries between the tables */
132 for (i = 0; i < h->tablelength; i++)
134 while ((e = h->table[i]) != NULL)
136 h->table[i] = e->next;
137 index = indexFor(newsize,e->h);
138 e->next = newtable[index];
146 else /* Plan B: realloc instead */
148 newtable = (struct entry **)
149 realloc(h->table, newsize * sizeof(struct entry *));
151 if (newtable == NULL)
156 for (i = h->tablelength; i < newsize; i++)
159 for (i = 0; i < h->tablelength; i++)
161 for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE)
163 index = indexFor(newsize,e->h);
172 e->next = newtable[index];
179 h->tablelength = newsize;
185 /*****************************************************************************/
187 hashtable_count(struct hashtable *h)
189 return h->entrycount;
192 /*****************************************************************************/
194 hashtable_insert(struct hashtable *h, void *k, void *v)
196 /* This method allows duplicate keys - but they shouldn't be used */
200 if (++(h->entrycount) > h->loadlimit)
202 /* Ignore the return value. If expand fails, we should
203 * still try cramming just this value into the existing table
204 * -- we may not have memory for a larger table, but one more
205 * element may be ok. Next time we insert, we'll try expanding again.*/
210 e = (struct entry *)malloc(sizeof(struct entry));
220 index = indexFor(h->tablelength,e->h);
223 e->next = h->table[index];
229 /*****************************************************************************/
231 hashtable_change(struct hashtable *h, void *k, void *v)
234 unsigned int hashvalue, index;
236 hashvalue = hash(h,k);
237 index = indexFor(h->tablelength,hashvalue);
242 /* Check hash value to short circuit heavier comparison */
243 if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
257 /*****************************************************************************/
258 void * /* returns value associated with key */
259 hashtable_search(struct hashtable *h, void *k)
262 unsigned int hashvalue, index;
264 hashvalue = hash(h,k);
265 index = indexFor(h->tablelength,hashvalue);
270 /* Check hash value to short circuit heavier comparison */
271 if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
280 /*****************************************************************************/
281 void * /* returns value associated with key */
282 hashtable_remove(struct hashtable *h, void *k)
284 /* TODO: consider compacting the table when the load factor drops enough,
285 * or provide a 'compact' method. */
290 unsigned int index = indexFor(h->tablelength,hash(h,k));
292 pE = &(h->table[index]);
297 if (h->eqfn(k, e->k))
315 /*****************************************************************************/
318 hashtable_destroy(struct hashtable *h, int free_values)
322 struct entry **table = h->table;
324 for (i = 0; i < h->tablelength; i++)
346 /*****************************************************************************/
347 /* hashtable_iterator - iterator constructor */
349 struct hashtable_itr *
350 hashtable_iterator(struct hashtable *h)
352 unsigned int i, tablelength;
353 struct hashtable_itr *itr = (struct hashtable_itr *)
354 malloc(sizeof(struct hashtable_itr));
361 tablelength = h->tablelength;
362 itr->index = tablelength;
364 if (0 == h->entrycount)
367 for (i = 0; i < tablelength; i++)
369 if (h->table[i] != NULL)
371 itr->e = h->table[i];
381 /*****************************************************************************/
382 /* key - return the key of the (key,value) pair at the current position */
385 hashtable_iterator_key(struct hashtable_itr *i)
390 /*****************************************************************************/
391 /* value - return the value of the (key,value) pair at the current position */
394 hashtable_iterator_value(struct hashtable_itr *i)
399 /*****************************************************************************/
400 /* advance - advance the iterator to the next element
401 * returns zero if advanced to end of table */
404 hashtable_iterator_advance(struct hashtable_itr *itr)
406 unsigned int j,tablelength;
407 struct entry **table;
411 return 0; /* stupidity check */
421 tablelength = itr->h->tablelength;
422 if (tablelength <= (j = ++(itr->index)))
429 table = itr->h->table;
430 while ((next = table[j]) == NULL)
432 if (++j >= tablelength)
434 itr->index = tablelength;