1 #include "goomsl_hash.h"
6 entry_new (const char *key, HashValue value)
9 int len = strlen (key);
10 GoomHashEntry *entry = (GoomHashEntry *) malloc (sizeof (GoomHashEntry));
12 entry->key = (char *) malloc (len + 1);
13 memcpy (entry->key, key, len + 1);
22 entry_free (GoomHashEntry * entry)
25 entry_free (entry->lower);
26 entry_free (entry->upper);
33 entry_put (GoomHashEntry * entry, const char *key, HashValue value)
35 int cmp = strcmp (key, entry->key);
40 if (entry->upper == NULL)
41 entry->upper = entry_new (key, value);
43 entry_put (entry->upper, key, value);
45 if (entry->lower == NULL)
46 entry->lower = entry_new (key, value);
48 entry_put (entry->lower, key, value);
53 entry_get (GoomHashEntry * entry, const char *key)
60 cmp = strcmp (key, entry->key);
62 return entry_get (entry->upper, key);
64 return entry_get (entry->lower, key);
66 return &(entry->value);
72 GoomHash *_this = (GoomHash *) malloc (sizeof (GoomHash));
75 _this->number_of_puts = 0;
80 goom_hash_free (GoomHash * _this)
82 entry_free (_this->root);
87 goom_hash_put (GoomHash * _this, const char *key, HashValue value)
89 _this->number_of_puts += 1;
90 if (_this->root == NULL)
91 _this->root = entry_new (key, value);
93 entry_put (_this->root, key, value);
97 goom_hash_get (GoomHash * _this, const char *key)
101 return entry_get (_this->root, key);
105 goom_hash_put_int (GoomHash * _this, const char *key, int i)
110 goom_hash_put (_this, key, value);
114 goom_hash_put_float (GoomHash * _this, const char *key, float f)
119 goom_hash_put (_this, key, value);
123 goom_hash_put_ptr (GoomHash * _this, const char *key, void *ptr)
128 goom_hash_put (_this, key, value);
134 _goom_hash_for_each (GoomHash * _this, GoomHashEntry * entry, GH_Func func)
138 func (_this, entry->key, &(entry->value));
139 _goom_hash_for_each (_this, entry->lower, func);
140 _goom_hash_for_each (_this, entry->upper, func);
144 goom_hash_for_each (GoomHash * _this, GH_Func func)
146 _goom_hash_for_each (_this, _this->root, func);
150 goom_hash_number_of_puts (GoomHash * _this)
152 return _this->number_of_puts;