diff options
Diffstat (limited to 'analyzer/structures')
| -rw-r--r-- | analyzer/structures/hashmap.c | 26 | ||||
| -rw-r--r-- | analyzer/structures/hashmap.h | 25 |
2 files changed, 44 insertions, 7 deletions
diff --git a/analyzer/structures/hashmap.c b/analyzer/structures/hashmap.c index 53fd5fc..0ef2ade 100644 --- a/analyzer/structures/hashmap.c +++ b/analyzer/structures/hashmap.c @@ -8,7 +8,7 @@ HashMap *hashmap_new( size_t key_size, size_t val_size, - size_t (*hash)(void *key), + uint32_t (*hash)(void *key), int (*compare)(void *key1, void *key2), void (*key_cleanup)(void *key), void (*val_cleanup)(void *val) @@ -26,7 +26,7 @@ HashMap *hashmap_new( } void hashmap_put(HashMap *map, void *key, void *val) { - size_t key_hash = map->hash(key); + size_t key_hash = map->hash(key) % HASHMAP_TABLE_SIZE; HashMapNode *node = map->table[key_hash]; // check if it exists @@ -56,7 +56,7 @@ void hashmap_put(HashMap *map, void *key, void *val) { } void *hashmap_get(HashMap *map, void *key) { - size_t key_hash = map->hash(key); + size_t key_hash = map->hash(key) % HASHMAP_TABLE_SIZE; HashMapNode *node = map->table[key_hash]; // check if it exists @@ -85,7 +85,7 @@ void free_node_without_next(HashMap *map, HashMapNode *node) { } void hashmap_delete(HashMap *map, void *key) { - size_t key_hash = map->hash(key); + size_t key_hash = map->hash(key) % HASHMAP_TABLE_SIZE; HashMapNode **p_node = &map->table[key_hash]; // check if it exists @@ -114,3 +114,21 @@ void hashmap_free(HashMap *map) { } free(map); } + +// FNV hash +uint32_t hash_str(void *str) { + char *s = str; + uint32_t hash = 2166136261; + while (*s) { + hash ^= (unsigned char)*s; + hash *= 16777619; + s++; + } + return hash; +} + +int compare_strs(void *str1, void *str2) { + char *s1 = str1; + char *s2 = str2; + return !strcmp(s1, s2); +} diff --git a/analyzer/structures/hashmap.h b/analyzer/structures/hashmap.h index 10637f1..7559010 100644 --- a/analyzer/structures/hashmap.h +++ b/analyzer/structures/hashmap.h @@ -2,6 +2,10 @@ // a simple implementation of hashmap using linked list #include <stddef.h> +#include <stdint.h> + +#ifndef HASHMAP_H +#define HASHMAP_H #define HASHMAP_TABLE_SIZE 128 @@ -15,7 +19,7 @@ typedef struct { HashMapNode *table[HASHMAP_TABLE_SIZE]; size_t key_size; size_t val_size; - size_t (*hash)(void *key); + uint32_t (*hash)(void *key); int (*compare)(void *key1, void *key2); void (*key_cleanup)(void *key); void (*val_cleanup)(void *val); @@ -29,7 +33,7 @@ typedef struct { // params: // - key - pointer to the key to hash // returns: -// hash of the key (size_t in [0; HASHMAP_TABLE_SIZE)) +// hash of the key // - compare - function for comparing keys // params: // - key1 - pointer to the first of the keys to compare @@ -49,7 +53,7 @@ typedef struct { extern HashMap *hashmap_new( size_t key_size, size_t val_size, - size_t (*hash)(void *key), + uint32_t (*hash)(void *key), int (*compare)(void *key1, void *key2), void (*key_cleanup)(void *key), void (*val_cleanup)(void *val) @@ -79,3 +83,18 @@ extern void hashmap_delete(HashMap *map, void *key); // free the hash map // - map - hash map to free extern void hashmap_free(HashMap *map); + +// hash a string +// - str - string to hash +// returns: +// hash of the string +extern uint32_t hash_str(void *str); + +// compare 2 strings +// - str1 - first string to compare +// - str2 - second string to compare +// returns: +// 1 if strings are identical, 0 otherwise +extern int compare_strs(void *str1, void *str2); + +#endif |
