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_ext(unsigned int minsize, float maxloadfactor,
45 unsigned int (*hashf) (void*),
46 int (*eqf) (void*, void*),
47 void (*freekfn) (void*),
48 void (*freevfn) (void*))
51 unsigned int i, size = 1u;
53 /* Check requested hashtable isn't too large */
54 if (minsize > (1u << 31))
57 /* Enforce size as power of 2 */
58 while (size < minsize)
61 h = (struct hashtable *)malloc(sizeof(struct hashtable));
66 h->table = (struct entry **)malloc(sizeof(struct entry*) * size);
75 for (i = 0; i < size; i++)
78 h->tablelength = size;
82 h->loadlimit = (unsigned int) ((float)size * maxloadfactor);
90 create_hashtable(unsigned int (*hashf) (void*),
91 int (*eqf) (void*, void*),
92 void (*freekfn) (void*),
93 void (*freevfn) (void*))
95 return create_hashtable_ext(16, 0.75, hashf, eqf, freekfn, freevfn);
98 /*****************************************************************************/
100 hash(struct hashtable *h, void *k)
102 /* Aim to protect against poor hash functions by adding logic here
103 * - logic taken from java 1.4 hashtable source */
105 unsigned int i = h->hashfn(k);
108 i ^= ((i >> 14) | (i << 18)); /* >>> */
110 i ^= ((i >> 10) | (i << 22)); /* >>> */
115 /*****************************************************************************/
117 indexFor(unsigned int tablelength, unsigned int hashvalue)
119 /* Only works if tablelength == 2^N */
120 return (hashvalue & (tablelength - 1u));
123 /*****************************************************************************/
125 hashtable_expand(struct hashtable *h)
127 /* Double the size of the table to accomodate more entries */
128 struct entry **newtable;
131 unsigned int newsize, i, index;
133 /* Check we're not hitting max capacity */
134 if (0 == (newsize = (h->tablelength << 1)))
137 newtable = (struct entry **)malloc(sizeof(struct entry*) * newsize);
139 if (newtable != NULL)
141 memset(newtable, 0, newsize * sizeof(struct entry *));
143 /* This algorithm is not 'stable'. ie. it reverses the list
144 * when it transfers entries between the tables */
145 for (i = 0; i < h->tablelength; i++)
147 while ((e = h->table[i]) != NULL)
149 h->table[i] = e->next;
150 index = indexFor(newsize, e->h);
151 e->next = newtable[index];
159 else /* Plan B: realloc instead */
161 newtable = (struct entry **)
162 realloc(h->table, newsize * sizeof(struct entry *));
164 if (newtable == NULL)
169 for (i = h->tablelength; i < newsize; i++)
172 for (i = 0; i < h->tablelength; i++)
174 for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE)
176 index = indexFor(newsize, e->h);
185 e->next = newtable[index];
192 h->tablelength = newsize;
198 /*****************************************************************************/
200 hashtable_count(struct hashtable *h)
202 return h->entrycount;
205 /*****************************************************************************/
207 hashtable_insert(struct hashtable *h, void *k, void *v)
209 /* This method allows duplicate keys - but they shouldn't be used */
213 if (++(h->entrycount) > h->loadlimit)
215 /* Ignore the return value. If expand fails, we should
216 * still try cramming just this value into the existing table
217 * -- we may not have memory for a larger table, but one more
218 * element may be ok. Next time we insert, we'll try expanding again.*/
223 e = (struct entry *)malloc(sizeof(struct entry));
233 index = indexFor(h->tablelength, e->h);
236 e->next = h->table[index];
242 /*****************************************************************************/
244 hashtable_change(struct hashtable *h, void *k, void *v)
247 unsigned int hashvalue, index;
249 hashvalue = hash(h, k);
250 index = indexFor(h->tablelength, hashvalue);
255 /* Check hash value to short circuit heavier comparison */
256 if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
258 if (h->freevfn != NULL)
271 /*****************************************************************************/
272 int /* checks if key exists */
273 hashtable_exists(struct hashtable *h, void *k)
276 unsigned int hashvalue, index;
278 hashvalue = hash(h, k);
279 index = indexFor(h->tablelength, hashvalue);
284 /* Check hash value to short circuit heavier comparison */
285 if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
294 /*****************************************************************************/
295 void * /* returns value associated with key */
296 hashtable_search(struct hashtable *h, void *k)
299 unsigned int hashvalue, index;
301 hashvalue = hash(h, k);
302 index = indexFor(h->tablelength, hashvalue);
307 /* Check hash value to short circuit heavier comparison */
308 if ((hashvalue == e->h) && (h->eqfn(k, e->k)))
317 /*****************************************************************************/
318 void * /* returns value associated with key */
319 hashtable_remove(struct hashtable *h, void *k)
321 /* TODO: consider compacting the table when the load factor drops enough,
322 * or provide a 'compact' method. */
327 unsigned int index = indexFor(h->tablelength, hash(h, k));
329 pE = &(h->table[index]);
334 if (h->eqfn(k, e->k))
339 if (h->freekfn != NULL)
341 if (h->freevfn != NULL)
357 /*****************************************************************************/
360 hashtable_destroy(struct hashtable *h)
364 struct entry **table = h->table;
366 for (i = 0; i < h->tablelength; i++)
374 if (h->freekfn != NULL)
376 if (h->freevfn != NULL)
387 /*****************************************************************************/
388 /* hashtable_iterator - iterator constructor */
390 struct hashtable_itr *
391 hashtable_iterator(struct hashtable *h)
393 unsigned int i, tablelength;
394 struct hashtable_itr *itr = (struct hashtable_itr *)
395 malloc(sizeof(struct hashtable_itr));
402 tablelength = h->tablelength;
403 itr->index = tablelength;
405 if (0 == h->entrycount)
408 for (i = 0; i < tablelength; i++)
410 if (h->table[i] != NULL)
412 itr->e = h->table[i];
422 /*****************************************************************************/
423 /* key - return the key of the (key, value) pair at the current position */
426 hashtable_iterator_key(struct hashtable_itr *itr)
428 if (itr == NULL || itr->e == NULL)
434 /*****************************************************************************/
435 /* value - return the value of the (key, value) pair at the current position */
438 hashtable_iterator_value(struct hashtable_itr *itr)
440 if (itr == NULL || itr->e == NULL)
446 /*****************************************************************************/
447 /* advance - advance the iterator to the next element
448 * returns zero if advanced to end of table */
451 hashtable_iterator_advance(struct hashtable_itr *itr)
453 unsigned int j, tablelength;
454 struct entry **table;
458 return 0; /* stupidity check */
468 tablelength = itr->h->tablelength;
469 if (tablelength <= (j = ++(itr->index)))
476 table = itr->h->table;
477 while ((next = table[j]) == NULL)
479 if (++j >= tablelength)
481 itr->index = tablelength;
493 /*****************************************************************************/
494 /* call function for all hashtable entries */
496 hashtable_foreach(struct hashtable *h, hashtable_fn fn, void *userdata)
501 if (hashtable_count(h) == 0)
504 struct hashtable_itr *itr = hashtable_iterator(h);
508 fn(hashtable_iterator_key(itr), hashtable_iterator_value(itr), userdata);
510 while (hashtable_iterator_advance(itr));
515 /*****************************************************************************/
516 /* call function for all hashtable entries and remove them, if function returned 1 */
518 hashtable_foreach_remove(struct hashtable *h, hashtable_remove_fn fn, void *userdata)
523 if (hashtable_count(h) == 0)
526 struct hashtable *remove = create_hashtable(h->hashfn, h->eqfn, NULL, NULL);
527 struct hashtable_itr *itr = hashtable_iterator(h);
531 if (fn(hashtable_iterator_key(itr), hashtable_iterator_value(itr), userdata))
532 hashtable_insert(remove, hashtable_iterator_key(itr), "1");
534 while (hashtable_iterator_advance(itr));
538 unsigned int num_removed = 0;
540 if (hashtable_count(remove) > 0)
542 struct hashtable_itr *itr_remove = hashtable_iterator(remove);
546 hashtable_remove(h, hashtable_iterator_key(itr_remove));
549 while (hashtable_iterator_advance(itr_remove));
554 hashtable_destroy(remove);
559 /*****************************************************************************/
560 /* remove_all hashtable entries */
562 hashtable_remove_all(struct hashtable *h)
564 /* TODO: this function should directly remove all hashtable entries */
569 if (hashtable_count(h) == 0)
572 struct hashtable *remove = create_hashtable(h->hashfn, h->eqfn, NULL, NULL);
573 struct hashtable_itr *itr = hashtable_iterator(h);
577 hashtable_insert(remove, hashtable_iterator_key(itr), "1");
579 while (hashtable_iterator_advance(itr));
583 struct hashtable_itr *itr_remove = hashtable_iterator(remove);
584 unsigned int num_removed = 0;
588 hashtable_remove(h, hashtable_iterator_key(itr_remove));
591 while (hashtable_iterator_advance(itr_remove));
595 hashtable_destroy(remove);