aboutsummaryrefslogtreecommitdiff
path: root/collections/map
diff options
context:
space:
mode:
Diffstat (limited to 'collections/map')
-rw-r--r--collections/map/map.adoc20
-rw-r--r--collections/map/map.c19
-rw-r--r--collections/map/map.h1
3 files changed, 39 insertions, 1 deletions
diff --git a/collections/map/map.adoc b/collections/map/map.adoc
index 2822b79..da0ebaf 100644
--- a/collections/map/map.adoc
+++ b/collections/map/map.adoc
@@ -1,7 +1,7 @@
Map
===
Tucker Evans
-v0.4, 2020-07-06
+v0.5, 2020-07-06
A basic map implemented in an AVL tree.
@@ -113,6 +113,24 @@ free(map_reset_key(dict "ONE"));
assert(map_insert(dict, "ONE", "NEW_VALUE") == 0);
----
+[[map_index]]
++void* map_index(map *self, vaid *key)+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Returns the value associated with +key+ (or equivalent).
+
+Examples
+^^^^^^^^
+[source,c]
+----
+#include "map.h"
+#include <string.h>
+
+map *dict = map_new((cmp_func) strcmp, sizeof(char*));
+
+map_insert(dict, strdup("ONE"), "VALUE");
+assert(strcmp(map_index(dict, "ONE"), "VALUE") == 0);
+----
+
[[map_clear]]
+void map_clear(map *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/collections/map/map.c b/collections/map/map.c
index 7de58e4..0600c8b 100644
--- a/collections/map/map.c
+++ b/collections/map/map.c
@@ -105,6 +105,25 @@ void *key;
return tmp;
}
+void* map_index(root, key)
+map *root;
+void *key;
+{
+ int cmp;
+ if (!root || !key)
+ return NULL;
+
+ cmp = root->cmp(root->key, key);
+
+ if (cmp < 0)
+ return map_index(root->left, key);
+
+ if (cmp > 0)
+ return map_index(root->right, key);
+
+ return root->val;
+}
+
void map_clear(root)
map *root;
{
diff --git a/collections/map/map.h b/collections/map/map.h
index a5fc816..d1a0012 100644
--- a/collections/map/map.h
+++ b/collections/map/map.h
@@ -13,6 +13,7 @@ int map_size(map*);
/*data*/
int map_insert(map*, void*, void*);
void* map_reset_key(map*, void*);
+void* map_index(map*, void*);
/*memory*/
void map_clear(map*);