aboutsummaryrefslogtreecommitdiff
path: root/src/shared/util/hashmap.h
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-10-18 21:34:55 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-10-18 21:34:55 -0400
commit33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904 (patch)
tree18d24b74e48d8b7e9cb656272df7a48b28d3b6c8 /src/shared/util/hashmap.h
parent76b4fddee6106b60dbc6da6d7bcef61b42a3c310 (diff)
downloadbrokengine-33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904.tar.gz
brokengine-33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904.tar.bz2
brokengine-33e6b9627e6a46d388d46f2c5b4d15ba7e9f9904.zip
Started refactoring
* Finished a basic server * Changed from ZMQ to nanomsg (basically the same though) * Moved some repeated code from the client/server directories into "shared" * Edited makefile to reflect new dependencies
Diffstat (limited to 'src/shared/util/hashmap.h')
-rw-r--r--src/shared/util/hashmap.h81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/shared/util/hashmap.h b/src/shared/util/hashmap.h
new file mode 100644
index 0000000..16c76dd
--- /dev/null
+++ b/src/shared/util/hashmap.h
@@ -0,0 +1,81 @@
+/*
+ * Generic hashmap manipulation functions
+ *
+ * Originally by Elliot C Back - http://elliottback.com/wp/hashmap-implementation-in-c/
+ *
+ * Modified by Pete Warden to fix a serious performance problem, support strings as keys
+ * and removed thread synchronization - http://petewarden.typepad.com
+ */
+#ifndef __HASHMAP_H__
+#define __HASHMAP_H__
+
+#define MAP_MISSING -3 /* No such element */
+#define MAP_FULL -2 /* Hashmap is full */
+#define MAP_OMEM -1 /* Out of Memory */
+#define MAP_OK 0 /* OK */
+
+/*
+ * any_t is a pointer. This allows you to put arbitrary structures in
+ * the hashmap.
+ */
+typedef void *any_t;
+
+/*
+ * PFany is a pointer to a function that can take two any_t arguments
+ * and return an integer. Returns status code..
+ */
+typedef int (*PFany)(any_t, any_t);
+
+/*
+ * map_t is a pointer to an internally maintained data structure.
+ * Clients of this package do not need to know how hashmaps are
+ * represented. They see and manipulate only map_t's.
+ */
+typedef any_t map_t;
+
+/*
+ * Return an empty hashmap. Returns NULL if empty.
+*/
+extern map_t hashmap_new();
+
+/*
+ * Iteratively call f with argument (item, data) for
+ * each element data in the hashmap. The function must
+ * return a map status code. If it returns anything other
+ * than MAP_OK the traversal is terminated. f must
+ * not reenter any hashmap functions, or deadlock may arise.
+ */
+extern int hashmap_iterate(map_t in, PFany f, any_t item);
+
+/*
+ * Add an element to the hashmap. Return MAP_OK or MAP_OMEM.
+ */
+extern int hashmap_put(map_t in, char* key, any_t value);
+
+/*
+ * Get an element from the hashmap. Return MAP_OK or MAP_MISSING.
+ */
+extern int hashmap_get(map_t in, char* key, any_t *arg);
+
+/*
+ * Remove an element from the hashmap. Return MAP_OK or MAP_MISSING.
+ */
+extern int hashmap_remove(map_t in, char* key);
+
+/*
+ * Get any element. Return MAP_OK or MAP_MISSING.
+ * remove - should the element be removed from the hashmap
+ */
+extern int hashmap_get_one(map_t in, any_t *arg, int remove);
+
+/*
+ * Free the hashmap
+ */
+extern void hashmap_free(map_t in);
+
+/*
+ * Get the current size of a hashmap
+ */
+extern int hashmap_length(map_t in);
+
+#endif //__HASHMAP_H__