aboutsummaryrefslogtreecommitdiff
path: root/hashtable.c
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.c
parent2e927c34a68671d7f090cbfafe70e87cac21375a (diff)
move hashtable functions outside
svn path=/trunk/externals/loaders/tclpd/; revision=15602
Diffstat (limited to 'hashtable.c')
-rw-r--r--hashtable.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/hashtable.c b/hashtable.c
new file mode 100644
index 0000000..ae32d38
--- /dev/null
+++ b/hashtable.c
@@ -0,0 +1,77 @@
+#include "hashtable.h"
+
+uint32_t hash_str(const char *s) {
+ const unsigned char *p = (const unsigned char *)s;
+ uint32_t h = 5381;
+
+ while (*p) {
+ h *= 33;
+ h ^= *p++;
+ }
+
+ return h ^ (h >> 16);
+}
+
+list_node_t* list_add(list_node_t* head, const char* k, void* v) {
+ list_node_t* n = (list_node_t*)malloc(sizeof(list_node_t));
+ n->next = head;
+ n->k = strdup(k);
+ n->v = v;
+ return n;
+}
+
+list_node_t* list_remove(list_node_t* head, const char* k) {
+ list_node_t* tmp;
+
+ // head remove
+ while(head && strcmp(head->k, k) == 0) {
+ tmp = head;
+ head = head->next;
+ free(tmp->k);
+ free(tmp);
+ }
+
+ list_node_t* p = head;
+
+ // normal (non-head) remove
+ while(p->next) {
+ if(strcmp(p->next->k, k) == 0)
+ {
+ tmp = p->next;
+ p->next = p->next->next;
+ free(tmp);
+ continue;
+ }
+ p = p->next;
+ }
+
+ return head;
+}
+
+void* list_get(list_node_t* head, const char* k) {
+ while(head) {
+ if(strcmp(head->k, k) == 0) {
+ return head->v;
+ }
+ head = head->next;
+ }
+ return (void*)0;
+}
+
+hash_table_t* hashtable_new(size_t size) {
+ hash_table_t* ht = NULL;
+ if(size > 0) {
+ ht = (hash_table_t*)malloc(sizeof(hash_table_t));
+ ht->sz = size;
+ ht->t = (list_node_t**)malloc(sizeof(list_node_t*) * size);
+ }
+ return ht;
+}
+
+void hashtable_free(hash_table_t* ht) {
+ if(ht) {
+ free(ht->t);
+ free(ht);
+ }
+}
+