diff options
Diffstat (limited to 'src/post/goom/goomsl_hash.c')
-rw-r--r-- | src/post/goom/goomsl_hash.c | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/post/goom/goomsl_hash.c b/src/post/goom/goomsl_hash.c index b723726ca..af2ec44a6 100644 --- a/src/post/goom/goomsl_hash.c +++ b/src/post/goom/goomsl_hash.c @@ -4,10 +4,11 @@ static GoomHashEntry *entry_new(const char *key, HashValue value) { + int len = strlen(key); GoomHashEntry *entry = (GoomHashEntry*)malloc(sizeof(GoomHashEntry)); - entry->key = (char *)malloc(strlen(key)+1); - strcpy(entry->key,key); + entry->key = (char *)malloc(len+1); + memcpy(entry->key,key,len+1); entry->value = value; entry->lower = NULL; entry->upper = NULL; @@ -57,9 +58,10 @@ static HashValue *entry_get(GoomHashEntry *entry, const char *key) { return &(entry->value); } -GoomHash *goom_hash_new(void) { +GoomHash *goom_hash_new() { GoomHash *_this = (GoomHash*)malloc(sizeof(GoomHash)); _this->root = NULL; + _this->number_of_puts = 0; return _this; } @@ -69,6 +71,7 @@ void goom_hash_free(GoomHash *_this) { } void goom_hash_put(GoomHash *_this, const char *key, HashValue value) { + _this->number_of_puts += 1; if (_this->root == NULL) _this->root = entry_new(key,value); else @@ -98,4 +101,20 @@ void goom_hash_put_ptr(GoomHash *_this, const char *key, void *ptr) { goom_hash_put(_this,key,value); } +/* FOR EACH */ +static void _goom_hash_for_each(GoomHash *_this, GoomHashEntry *entry, GH_Func func) +{ + if (entry == NULL) return; + func(_this, entry->key, &(entry->value)); + _goom_hash_for_each(_this, entry->lower, func); + _goom_hash_for_each(_this, entry->upper, func); +} + +void goom_hash_for_each(GoomHash *_this, GH_Func func) { + _goom_hash_for_each(_this, _this->root, func); +} + +int goom_hash_number_of_puts(GoomHash *_this) { + return _this->number_of_puts; +} |