Associative arrays/Creation/C: Difference between revisions
Content added Content deleted
(→From Scratch: remove hash_free function) |
(→From Scratch: include headers, use hash_t instead of Hash as struct name) |
||
Line 7: | Line 7: | ||
A hash table can be implemented with the following. Because of this example's simplicity, it comes with some restrictions on use and capabilities: It can't be resized automatically, if you try to insert more values than its capacity it will freeze, the hashing function is very simple, etc. All are fixable with additional logic or using a library: |
A hash table can be implemented with the following. Because of this example's simplicity, it comes with some restrictions on use and capabilities: It can't be resized automatically, if you try to insert more values than its capacity it will freeze, the hashing function is very simple, etc. All are fixable with additional logic or using a library: |
||
<lang c> |
<lang c>#include <stdio.h> |
||
#include <stdlib.h> |
|||
typedef struct { |
|||
int size; |
int size; |
||
void **keys; |
void **keys; |
||
void **values; |
void **values; |
||
} |
} hash_t; |
||
hash_t *hash_new (int size) { |
|||
hash_t *h = calloc(1, sizeof (hash_t)); |
|||
h->keys = calloc(size, sizeof (void *)); |
|||
h->values = calloc(size, sizeof (void *)); |
|||
h->size = size; |
|||
return |
return h; |
||
} |
} |
||
int hash_index ( |
int hash_index (hash_t *h, void *key) { |
||
int |
int i = (int) key % h->size; |
||
while ( |
while (h->keys[i] && h->keys[i] != key) |
||
i = (i + 1) % h->size; |
|||
return |
return i; |
||
} |
} |
||
void hash_insert ( |
void hash_insert (hash_t *h, void *key, void *value) { |
||
int |
int i = hash_index(h, key); |
||
h->keys[i] = key; |
|||
h->values[i] = value; |
|||
} |
} |
||
void *hash_lookup ( |
void *hash_lookup (hash_t *h, void *key) { |
||
int |
int i = hash_index(h, key); |
||
return |
return h->values[i]; |
||
} |
} |
||
int main () { |
int main () { |
||
hash_t *h = hash_new(15); |
|||
hash_insert( |
hash_insert(h, "hello", "world"); |
||
hash_insert( |
hash_insert(h, "a", "b"); |
||
printf("hello => %s\n", hash_lookup( |
printf("hello => %s\n", hash_lookup(h, "hello")); |
||
printf("herp => %s\n", hash_lookup( |
printf("herp => %s\n", hash_lookup(h, "herp")); |
||
printf("a => %s\n", hash_lookup( |
printf("a => %s\n", hash_lookup(h, "a")); |
||
return 0; |
return 0; |
||
}</lang> |
}</lang> |