From 61ab39873f66649129872485ed721911999060b1 Mon Sep 17 00:00:00 2001 From: mescalinum Date: Sat, 15 Oct 2011 12:26:33 +0000 Subject: move hashtable functions outside svn path=/trunk/externals/loaders/tclpd/; revision=15602 --- hashtable.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 hashtable.h (limited to 'hashtable.h') diff --git a/hashtable.h b/hashtable.h new file mode 100644 index 0000000..1c3fea7 --- /dev/null +++ b/hashtable.h @@ -0,0 +1,43 @@ +#ifndef HASHTABLE_H_INCLUDED +#define HASHTABLE_H_INCLUDED + +#include +#include +#include + +typedef struct list_node { + const char* k; + void* v; + struct list_node* next; +} list_node_t; + +typedef struct hash_table { + list_node_t** t; + size_t sz; +} hash_table_t; + +uint32_t hash_str(const char *s); +list_node_t* list_add(list_node_t* head, const char* k, void* v); +list_node_t* list_remove(list_node_t* head, const char* k); +void* list_get(list_node_t* head, const char* k); + +hash_table_t* hashtable_new(size_t size); +void hash_table_free(hash_table_t* ht); + +static inline void hashtable_add(hash_table_t* ht, const char* name, void* c) { + uint32_t h = hash_str(name) % ht->sz; + ht->t[h] = list_add(ht->t[h], name, (void*)c); +} + +static inline void hashtable_remove(hash_table_t* ht, const char* name) { + uint32_t h = hash_str(name) % ht->sz; + ht->t[h] = list_remove(ht->t[h], name); +} + +static inline void* hashtable_get(hash_table_t* ht, const char* name) { + uint32_t h = hash_str(name) % ht->sz; + return list_get(ht->t[h], name); +} + +#endif // HASHTABLE_H_INCLUDED + -- cgit v1.2.1