diff options
Diffstat (limited to 'src/hashtable.c')
| -rw-r--r-- | src/hashtable.c | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/hashtable.c b/src/hashtable.c new file mode 100644 index 0000000..9716c25 --- /dev/null +++ b/src/hashtable.c | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | /* | ||
| 2 | * hashtable.c | ||
| 3 | * really simple hash table implementation | ||
| 4 | * | ||
| 5 | * Copyright (c) 2011 Nikias Bassen, All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | #include "hashtable.h" | ||
| 22 | |||
| 23 | hashtable_t* hash_table_new(hash_func_t hash_func, compare_func_t compare_func) | ||
| 24 | { | ||
| 25 | hashtable_t* ht = (hashtable_t*)malloc(sizeof(hashtable_t)); | ||
| 26 | int i; | ||
| 27 | for (i = 0; i < 256; i++) { | ||
| 28 | ht->entries[i] = NULL; | ||
| 29 | } | ||
| 30 | ht->count = 0; | ||
| 31 | ht->hash_func = hash_func; | ||
| 32 | ht->compare_func = compare_func; | ||
| 33 | return ht; | ||
| 34 | } | ||
| 35 | |||
| 36 | void hash_table_destroy(hashtable_t *ht) | ||
| 37 | { | ||
| 38 | if (!ht) return; | ||
| 39 | |||
| 40 | int i = 0; | ||
| 41 | for (i = 0; i < 256; i++) { | ||
| 42 | if (ht->entries[i]) { | ||
| 43 | hashentry_t* e = ht->entries[i]; | ||
| 44 | while (e) { | ||
| 45 | free(e->value); | ||
| 46 | hashentry_t* old = e; | ||
| 47 | e = e->next; | ||
| 48 | free(old); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | free(ht); | ||
| 53 | } | ||
| 54 | |||
| 55 | void hash_table_insert(hashtable_t* ht, void *key, void *value) | ||
| 56 | { | ||
| 57 | if (!ht || !key) return; | ||
| 58 | int i; | ||
| 59 | |||
| 60 | unsigned int hash = ht->hash_func(key); | ||
| 61 | |||
| 62 | int idx0 = hash & 0xFF; | ||
| 63 | |||
| 64 | // get the idx0 list | ||
| 65 | hashentry_t* e = ht->entries[idx0]; | ||
| 66 | while (e) { | ||
| 67 | if (ht->compare_func(e->key, key)) { | ||
| 68 | // element already present. replace value. | ||
| 69 | e->value = value; | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | e = e->next; | ||
| 73 | } | ||
| 74 | |||
| 75 | // if we get here, the element is not yet in the list. | ||
| 76 | |||
| 77 | // make a new entry. | ||
| 78 | hashentry_t* entry = (hashentry_t*)malloc(sizeof(hashentry_t)); | ||
| 79 | entry->key = key; | ||
| 80 | entry->value = value; | ||
| 81 | if (!ht->entries[idx0]) { | ||
| 82 | // first entry | ||
| 83 | entry->next = NULL; | ||
| 84 | } else { | ||
| 85 | // add to list | ||
| 86 | entry->next = ht->entries[idx0]; | ||
| 87 | } | ||
| 88 | ht->entries[idx0] = entry; | ||
| 89 | ht->count++; | ||
| 90 | } | ||
| 91 | |||
| 92 | void* hash_table_lookup(hashtable_t* ht, void *key) | ||
| 93 | { | ||
| 94 | if (!ht || !key) return NULL; | ||
| 95 | unsigned int hash = ht->hash_func(key); | ||
| 96 | |||
| 97 | int idx0 = hash & 0xFF; | ||
| 98 | |||
| 99 | hashentry_t* e = ht->entries[idx0]; | ||
| 100 | while (e) { | ||
| 101 | if (ht->compare_func(e->key, key)) { | ||
| 102 | return e->value; | ||
| 103 | } | ||
| 104 | e = e->next; | ||
| 105 | } | ||
| 106 | return NULL; | ||
| 107 | } | ||
