aboutsummaryrefslogtreecommitdiff
path: root/collections
diff options
context:
space:
mode:
authorTucker Evans <tucker@tuckerevans.com>2020-07-08 11:31:38 -0400
committerTucker Evans <tucker@tuckerevans.com>2020-07-08 11:31:38 -0400
commitaeced5737c8b8d87a794bbb9775dd2145c43cabc (patch)
tree93821c8a4550ac449c6fa0591278b3f4f2986b1e /collections
parent7ef7147d743b1ca1edb6d40b820c79b7527ab941 (diff)
Add check key ptr for map to ease memory management of keys
Diffstat (limited to 'collections')
-rw-r--r--collections/map/map.adoc37
-rw-r--r--collections/map/map.c19
-rw-r--r--collections/map/map.h1
3 files changed, 53 insertions, 4 deletions
diff --git a/collections/map/map.adoc b/collections/map/map.adoc
index 695f763..ce4999b 100644
--- a/collections/map/map.adoc
+++ b/collections/map/map.adoc
@@ -96,8 +96,37 @@ else
assert(map_size(dict) == 1);
----
-[[map_reset_key]]
-+void* map_reset_key(map *self, vaid *key)+
+[[map_check_key_ptr]]
++int map_check_key_ptr(map *self, void *key)+
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+This is used to check the key pointer for equivalent keys.
+It is provided to ease memory management, in combination with
+<<map_set_key,+map_set_key()+>>.
+
+Examples
+^^^^^^^^
+[source,c]
+----
+#include "map.h"
+#include <string.h>
+
+char *eq_key2
+
+map *dict = map_new((cmp_func) strcmp);
+
+map_insert(dict, strdup("ONE"), NULL);
+
+eq_key2 = strdup("ONE");
+
+/*Want to free key1 for some reason*/
+if (!map_check_key_ptr(dict, eq_key2)) {
+ free(map_set_key(dict, eq_key2));
+}
+
+----
+
+[[map_set_key]]
++void* map_set_key(map *self, void *key)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This replaces an equivalent key with the one passed in, the key that is
overwritten is return so the user can free it.
@@ -113,12 +142,12 @@ map *dict = map_new((cmp_func) strcmp);
map_insert(dict, strdup("ONE"), "VALUE");
assert(map_insert(dict, "ONE", "NEW_VALUE") < 0);
-free(map_reset_key(dict "ONE"));
+free(map_set_key(dict "ONE"));
assert(map_insert(dict, "ONE", "NEW_VALUE") == 0);
----
[[map_index]]
-+void* map_index(map *self, vaid *key)+
++void* map_index(map *self, void *key)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the value associated with +key+ (or equivalent).
diff --git a/collections/map/map.c b/collections/map/map.c
index 00f2225..66382ba 100644
--- a/collections/map/map.c
+++ b/collections/map/map.c
@@ -102,6 +102,25 @@ void *key, *val;
return -1;
}
+int map_check_key_ptr(root, key)
+map *root;
+void *key;
+{
+ int cmp;
+
+ if (!root || !key)
+ return 0;
+
+ cmp = root->cmp(root->key, key);
+
+ if (cmp < 0)
+ return map_check_key_ptr(root->left, key);
+ if (cmp > 0)
+ return map_check_key_ptr(root->right, key);
+
+ return root->key == key;
+}
+
void* map_set_key(root, key)
map *root;
void *key;
diff --git a/collections/map/map.h b/collections/map/map.h
index 9de3ba1..38e3e11 100644
--- a/collections/map/map.h
+++ b/collections/map/map.h
@@ -14,6 +14,7 @@ int map_size(map*);
int map_insert(map*, void*, void*);
void* map_index(map*, void*);
+int map_check_key_ptr(map*, void*);
void* map_set_key(map*, void*);