diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-08 11:15:18 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-08 11:15:18 -0400 |
commit | 78fa3e40d3c2f0d302f4779ca35352f5f68fba0b (patch) | |
tree | c240ee4f32a8aad69baa1b784d8242e4601620d7 /collections/map/map.adoc | |
parent | 1ec57ef8df39b2ad7af98e2e65d93a60e7851586 (diff) |
Fix map insert error returns
Makes inserting an equivalent key an error, set_val should be used.
Diffstat (limited to 'collections/map/map.adoc')
-rw-r--r-- | collections/map/map.adoc | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/collections/map/map.adoc b/collections/map/map.adoc index da0ebaf..27278b8 100644 --- a/collections/map/map.adoc +++ b/collections/map/map.adoc @@ -73,11 +73,9 @@ assert(map_size(dict) == 1); [[map_insert]] +int map_insert(map *self, void *key, void *value)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Inserts item +value+ for +key+ into +self+ -This returns an int indicating a successful insertion; currently the only -two potential errors are caused by a NULL map or an equivalent key with a -different pointer, this is in order to prevent a memory leak. -<<reset_key,+map_reset_key()+>> can be used to fix this issue. +Inserts a new item +value+ for +key+ into +self+ +This returns an int indicating a successful insertion; providing a NULL +self+ +or a key that is already in +self+ will return -1 otherwise 0 is returned. Examples ^^^^^^^^ @@ -85,11 +83,14 @@ Examples ---- #include "map.h" #include <string.h> +#include <stdio.h> map *dict = map_new((cmp_func) strcmp); -map_insert(dict, "ONE", NULL); -assert(map_size(dict) == 1); +if (map_insert(dict, "ONE", NULL) < 0) + printf("Failed to insert {\"ONE\": NULL}\n"); +else + assert(map_size(dict) == 1); ---- [[map_reset_key]] |