aboutsummaryrefslogtreecommitdiff
path: root/HashTable.c
diff options
context:
space:
mode:
Diffstat (limited to 'HashTable.c')
-rw-r--r--HashTable.c64
1 files changed, 39 insertions, 25 deletions
diff --git a/HashTable.c b/HashTable.c
index ed49ee7..0fed138 100644
--- a/HashTable.c
+++ b/HashTable.c
@@ -3,6 +3,8 @@
#include <stdio.h>
#include <string.h>
+/** Hashing function **/
+
// Fowler/Noll/Vo 32-bit hash implementation
// http://www.isthe.com/chongo/tech/comp/fnv/index.html
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash
@@ -20,6 +22,8 @@ static fnv_int fnv1_hash(const char* str) {
return hash;
}
+/** Metadata **/
+
struct HashTableMeta {
MemoryManager *mm;
usize size;
@@ -31,6 +35,8 @@ H_metadata(HashTable ht) {
return ht - sizeof(struct HashTableMeta);
}
+/** Helpers **/
+
static inline const char**
H_keys(HashTable ht) {
return ht;
@@ -64,6 +70,8 @@ H_find_hash(HashTable ht, const char *key, fnv_int *hash) {
return R_NotFound;
}
+/* Create */
+
HashTable
H_new(MemoryManager *mm, usize size) {
if (NULL == mm)
@@ -89,6 +97,8 @@ H_new(MemoryManager *mm, usize size) {
return start;
}
+/* Free */
+
Result
H_free(HashTable* ht) {
if (NULL == ht || NULL == *ht)
@@ -106,20 +116,6 @@ H_free(HashTable* ht) {
}
Result
-H_freevals(HashTable* ht) {
- if (NULL == ht || NULL == *ht)
- return R_NullArgument;
-
- struct HashTableMeta *htm = H_metadata(*ht);
-
- for (usize i = 0; i < htm->size; ++i) {
- htm->mm->free(*htm->mm, (void**)(H_vals(*ht) + i));
- }
-
- return R_Ok;
-}
-
-Result
H_freeat(HashTable ht, const char* key) {
if (NULL == ht || NULL == key)
return R_NullArgument;
@@ -136,20 +132,22 @@ H_freeat(HashTable ht, const char* key) {
return R_Ok;
}
-usize
-H_size(HashTable ht) {
- if (NULL == ht)
- return 0;
- return H_metadata(ht)->size;
-}
+Result
+H_freevals(HashTable* ht) {
+ if (NULL == ht || NULL == *ht)
+ return R_NullArgument;
-usize
-H_count(HashTable ht) {
- if (NULL == ht)
- return 0;
- return H_metadata(ht)->count;
+ struct HashTableMeta *htm = H_metadata(*ht);
+
+ for (usize i = 0; i < htm->size; ++i) {
+ htm->mm->free(*htm->mm, (void**)(H_vals(*ht) + i));
+ }
+
+ return R_Ok;
}
+/* Modify */
+
Result
H_insert(HashTable ht, const char *key, void *value) {
if (NULL == ht || NULL == key)
@@ -229,6 +227,22 @@ H_insertf(HashTable ht, const char *key, double value) {
return r;
}
+/* Get */
+
+usize
+H_size(HashTable ht) {
+ if (NULL == ht)
+ return 0;
+ return H_metadata(ht)->size;
+}
+
+usize
+H_count(HashTable ht) {
+ if (NULL == ht)
+ return 0;
+ return H_metadata(ht)->count;
+}
+
void*
H_get(HashTable ht, const char* key) {
if (NULL == ht || NULL == key)