X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=util.c;h=6853393e0487f384dfe602d331db5027b345a785;hb=0df394dcbcaf874f780b0d88cd4a76cff0569a6e;hp=5157d1cd1027027e1f6e32cdbad25e30af6cf23a;hpb=32c928ab6d527199b4404f9789ae8e7a4818e06f;p=xonotic%2Fgmqcc.git diff --git a/util.c b/util.c index 5157d1c..6853393 100644 --- a/util.c +++ b/util.c @@ -526,7 +526,7 @@ typedef struct hash_node_t { } hash_node_t; -size_t _util_hthash(hash_table_t *ht, const char *key) { +size_t util_hthash(hash_table_t *ht, const char *key) { uint64_t val = 0; size_t len = strlen(key); size_t itr = 0; @@ -584,20 +584,18 @@ hash_table_t *util_htnew(size_t size) { return hashtable; } -void util_htset(hash_table_t *ht, const char *key, void *value) { - size_t bin = 0; +void util_htseth(hash_table_t *ht, const char *key, size_t bin, void *value) { hash_node_t *newnode = NULL; hash_node_t *next = NULL; hash_node_t *last = NULL; - bin = _util_hthash(ht, key); next = ht->table[bin]; - while (next && next->key && strcmp(key, next->key)) + while (next && next->key && strcmp(key, next->key) > 0) last = next, next = next->next; /* already in table, do a replace */ - if (next && next->key && !strcmp(key, next->key)) { + if (next && next->key && strcmp(key, next->key) == 0) { next->value = value; } else { /* not found, grow a pair man :P */ @@ -614,8 +612,11 @@ void util_htset(hash_table_t *ht, const char *key, void *value) { } } -void *util_htget(hash_table_t *ht, const char *key) { - size_t bin = _util_hthash(ht, key); +void util_htset(hash_table_t *ht, const char *key, void *value) { + util_htseth(ht, key, util_hthash(ht, key), value); +} + +void *util_htgeth(hash_table_t *ht, const char *key, size_t bin) { hash_node_t *pair = ht->table[bin]; while (pair && pair->key && strcmp(key, pair->key) > 0) @@ -627,6 +628,10 @@ void *util_htget(hash_table_t *ht, const char *key) { return pair->value; } +void *util_htget(hash_table_t *ht, const char *key) { + return util_htgeth(ht, key, util_hthash(ht, key)); +} + /* * Free all allocated data in a hashtable, this is quite the amount * of work. @@ -635,12 +640,15 @@ void util_htdel(hash_table_t *ht) { size_t i = 0; for (; i < ht->size; i++) { hash_node_t *n = ht->table[i]; + hash_node_t *p; /* free in list */ while (n) { if (n->key) mem_d(n->key); + p = n; n = n->next; + mem_d(p); } }