aboutsummaryrefslogtreecommitdiff
path: root/hashtable.h
diff options
context:
space:
mode:
authormescalinum <mescalinum@users.sourceforge.net>2011-10-15 12:26:33 +0000
committermescalinum <mescalinum@users.sourceforge.net>2011-10-15 12:26:33 +0000
commit61ab39873f66649129872485ed721911999060b1 (patch)
treea023c3b18216cc7bec8ef5f0cb035328b0574d57 /hashtable.h
parent2e927c34a68671d7f090cbfafe70e87cac21375a (diff)
move hashtable functions outside
svn path=/trunk/externals/loaders/tclpd/; revision=15602
Diffstat (limited to 'hashtable.h')
-rw-r--r--hashtable.h43
1 files changed, 43 insertions, 0 deletions
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 <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+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
+