aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rwxr-xr-xinclude/hashfuncs.h33
-rwxr-xr-xinclude/strmap.h38
2 files changed, 71 insertions, 0 deletions
diff --git a/include/hashfuncs.h b/include/hashfuncs.h
new file mode 100755
index 0000000..da5d788
--- /dev/null
+++ b/include/hashfuncs.h
@@ -0,0 +1,33 @@
+#include <stdlib.h>
+
+/*
+ *
+ * 32 bit FNV-1a algorithm
+ * https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
+ *
+ */
+static size_t hash_key(const char *key, const size_t max_hash_value) {
+ uint32_t hash = 2166136261U; /* FNV-1a 32-bit offset basis */
+ /*
+ * We'll explicitly use unsigned chars here, since we're doing arithmetic. The
+ * C standard doesn't state whether an implementation must use signed or
+ * unsigned values for characters, so this conversion ensures we get
+ * correct behavior. If signed characters are used, we'll get overflows
+ * and other odd issues in the hash calculation.
+ */
+ const unsigned char *p = (unsigned char *) key;
+
+ while (*p) {
+ hash ^= *p++;
+ hash *= 16777619U; /* FNV-1a prime */
+ }
+
+ return hash % max_hash_value;
+}
+
+static size_t test_hash(const char *key, const size_t max_hash_value) {
+ (void) max_hash_value;
+
+ return key[0] % 3;
+}
+
diff --git a/include/strmap.h b/include/strmap.h
new file mode 100755
index 0000000..634da60
--- /dev/null
+++ b/include/strmap.h
@@ -0,0 +1,38 @@
+/*
+ * strmap.h
+ *
+ * A close-addressing based string -> string hash map
+ * for CISC 301: Operating Systems
+ */
+
+#ifndef H_STRMAP
+#define H_STRMAP
+
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct strmap strmap;
+typedef size_t (*hash_func) (const char *, size_t);
+
+typedef enum {
+ STRMAP_OK,
+ STRMAP_ERR,
+ STRMAP_NOTFOUND
+} strmap_status;
+
+/*
+ * Inserts a new string into a strmap, returning
+ */
+strmap_status strmap_put(strmap*, const char*, const char*);
+
+strmap_status strmap_get(const strmap*, const char*, const char**);
+
+strmap_status strmap_delete(strmap*, const char*);
+
+size_t strmap_size(const strmap*);
+
+strmap *strmap_create(hash_func);
+void strmap_destroy(strmap *);
+
+
+#endif