|
| 1 | +#include <stdlib.h> |
| 2 | +#include "datastructures.h" |
| 3 | + |
| 4 | +#define _XOPEN_SOURCE 500 /* Enable certain library functions (strdup) on linux. See feature_test_macros(7) */ |
| 5 | + |
| 6 | +#include <stdlib.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <limits.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +struct entry_s { |
| 12 | + void *key; |
| 13 | + size_t key_len; |
| 14 | + void *value; |
| 15 | + size_t value_len; |
| 16 | + struct entry_s *next; |
| 17 | +}; |
| 18 | + |
| 19 | +typedef struct entry_s entry_t; |
| 20 | + |
| 21 | +struct hashtable_s { |
| 22 | + int size; |
| 23 | + struct entry_s **table; |
| 24 | +}; |
| 25 | + |
| 26 | +typedef struct hashtable_s hashtable_t; |
| 27 | + |
| 28 | + |
| 29 | +/* Create a new hashtable. */ |
| 30 | +hashtable_t *ht_create( int size ) |
| 31 | +{ |
| 32 | + hashtable_t *hashtable = NULL; |
| 33 | + int i; |
| 34 | + size_t table_size = sizeof(hashtable_t) + (size * sizeof(entry_t *)); |
| 35 | + |
| 36 | + if( size < 1 ) return NULL; |
| 37 | + |
| 38 | + /* Allocate the table and the entry pointers. */ |
| 39 | + if((hashtable = calloc(1, table_size)) == NULL ) { |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + /* point to the entry table which is within the block of memory we just allocated. */ |
| 44 | + hashtable->table = (void *)(hashtable + 1); |
| 45 | + |
| 46 | + hashtable->size = size; |
| 47 | + |
| 48 | + return hashtable; |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +void ht_destroy(hastable_t *hashtable) |
| 53 | +{ |
| 54 | + if(!hashtable) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + /* walk down each bin and free everything we find */ |
| 59 | + for(size_t bin=0; bin<hashtable->size; bin++) { |
| 60 | + entry_t *curr = hashtable->table[bin]; |
| 61 | + |
| 62 | + while(curr) { |
| 63 | + entry_t *next = curr->next; |
| 64 | + free(curr); |
| 65 | + curr = next; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + free(hashtable); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +/* Hash a string for a particular hash table. */ |
| 75 | +static int ht_hash(hashtable_t *hashtable, void *key_arg, size_t key_len) |
| 76 | +{ |
| 77 | + size_t hash, i; |
| 78 | + int8_t *key = key_arg; |
| 79 | + |
| 80 | + // http://en.wikipedia.org/wiki/Jenkins_hash_function |
| 81 | + for ( hash = i = 0; i < key_len; ++i ) { |
| 82 | + hash += key[i], hash += ( hash << 10 ), hash ^= ( hash >> 6 ); |
| 83 | + } |
| 84 | + hash += ( hash << 3 ), hash ^= ( hash >> 11 ), hash += ( hash << 15 ); |
| 85 | + |
| 86 | + return hash % hashtable->size; |
| 87 | +} |
| 88 | + |
| 89 | +/* Create a key-value pair. */ |
| 90 | +static entry_t *ht_newpair( void *key, size_t key_len, void *value, size_t value_len) |
| 91 | +{ |
| 92 | + entry_t *newpair; |
| 93 | + size_t data_len = sizeof(entry_t) + key_len + value_len; |
| 94 | + |
| 95 | + if( ( newpair = malloc( data_len ) ) == NULL ) { |
| 96 | + return NULL; |
| 97 | + } |
| 98 | + |
| 99 | + /* stitch up pointers */ |
| 100 | + newpair->key = (int8_t *)(newpair + 1); |
| 101 | + newpair->value = newpair->key + key_len; |
| 102 | + newpair->next = NULL; |
| 103 | + |
| 104 | + newpair->key_len = key_len; |
| 105 | + newpair->value_len = value_len; |
| 106 | + |
| 107 | + memcpy(newpair->key, key, key_len); |
| 108 | + memcpy(newpair->value, value, value_len); |
| 109 | + |
| 110 | + return newpair; |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +/* Retrieve a value from a hash table. */ |
| 115 | +void *ht_get(hashtable_t *hashtable, void *key, size_t key_len) |
| 116 | +{ |
| 117 | + int bin = 0; |
| 118 | + entry_t *pair; |
| 119 | + |
| 120 | + bin = ht_hash(hashtable, key, key_len); |
| 121 | + |
| 122 | + /* Step through the bin, looking for our value. */ |
| 123 | + pair = hashtable->table[bin]; |
| 124 | + |
| 125 | + while(pair != NULL && memcmp(key, key_len, pair->key, pair->key_len) > 0 ) { |
| 126 | + pair = pair->next; |
| 127 | + } |
| 128 | + |
| 129 | + /* Did we actually find anything? */ |
| 130 | + if( pair != NULL || memcmp(key, key_len, pair->key, pair->key_len) == 0 ) { |
| 131 | + return pair->value; |
| 132 | + } else { |
| 133 | + return NULL; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +void ht_remove(hashtable_t *hashtable, void *key, size_t key_len) |
| 139 | +{ |
| 140 | + int bin = 0; |
| 141 | + entry_t *curr; |
| 142 | + entry_t *last = NULL; |
| 143 | + |
| 144 | + bin = ht_hash(hashtable, key, key_len); |
| 145 | + |
| 146 | + /* Step through the bin, looking for our value. */ |
| 147 | + curr = hashtable->table[bin]; |
| 148 | + |
| 149 | + while(curr != NULL && memcmp(key, key_len, curr->key, curr->key_len) > 0 ) { |
| 150 | + last = curr; |
| 151 | + curr = curr->next; |
| 152 | + } |
| 153 | + |
| 154 | + /* Did we actually find anything? */ |
| 155 | + if(curr != NULL || memcmp(key, key_len, curr->key, curr->key_len) == 0 ) { |
| 156 | + if(!last) { |
| 157 | + /* head of the list */ |
| 158 | + hashtable->table[bin] = curr->next; |
| 159 | + } else { |
| 160 | + last->next = curr->next; |
| 161 | + } |
| 162 | + |
| 163 | + free(curr); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +/* Insert a key-value pair into a hash table. */ |
| 169 | +int ht_set( hashtable_t *hashtable, void *key, size_t key_len, void *value, size_t value_len) |
| 170 | +{ |
| 171 | + int bin = 0; |
| 172 | + entry_t *newpair = NULL; |
| 173 | + |
| 174 | + newpair = ht_newpair(key, key_len, value, value_len); |
| 175 | + |
| 176 | + if(!newpair) { |
| 177 | + return 0; |
| 178 | + } |
| 179 | + |
| 180 | + /* make sure that there is no existing entry with the key */ |
| 181 | + ht_remove(hashtable, key, key_len); |
| 182 | + |
| 183 | + /* what bin is the entry going to be in? */ |
| 184 | + bin = ht_hash(hashtable, key, key_len); |
| 185 | + |
| 186 | + next = hashtable->table[bin]; |
| 187 | + |
| 188 | + /* find where the new entry should go */ |
| 189 | + while(next != NULL && memcmp(newpair->key, newpair->key_len, next->key, next->key_len) > 0 ) { |
| 190 | + last = next; |
| 191 | + next = next->next; |
| 192 | + } |
| 193 | + |
| 194 | + /* three cases, we are at the start of the list, at the end of the list, or somewhere in the middle */ |
| 195 | + if(next == hashtable->table[bin]) { |
| 196 | + /* we're at the start of the linked list in this bin. */ |
| 197 | + newpair->next = next; |
| 198 | + hashtable->table[bin] = newpair; |
| 199 | + } else if(next == NULL) { |
| 200 | + /* we're at the end of the linked list. */ |
| 201 | + last->next = newpair; |
| 202 | + } else { |
| 203 | + /* we're in the middle of the list. */ |
| 204 | + newpair->next = next; |
| 205 | + last->next = newpair; |
| 206 | + } |
| 207 | + |
| 208 | + return 1; |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +int main( int argc, char **argv ) { |
| 214 | + |
| 215 | + hashtable_t *hashtable = ht_create( 65536 ); |
| 216 | + |
| 217 | + ht_set( hashtable, "key1", "inky" ); |
| 218 | + ht_set( hashtable, "key2", "pinky" ); |
| 219 | + ht_set( hashtable, "key3", "blinky" ); |
| 220 | + ht_set( hashtable, "key4", "floyd" ); |
| 221 | + |
| 222 | + printf( "%s\n", ht_get( hashtable, "key1" ) ); |
| 223 | + printf( "%s\n", ht_get( hashtable, "key2" ) ); |
| 224 | + printf( "%s\n", ht_get( hashtable, "key3" ) ); |
| 225 | + printf( "%s\n", ht_get( hashtable, "key4" ) ); |
| 226 | + |
| 227 | + return 0; |
| 228 | +} |
0 commit comments