From 78fa3e40d3c2f0d302f4779ca35352f5f68fba0b Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 8 Jul 2020 11:15:18 -0400 Subject: Fix map insert error returns Makes inserting an equivalent key an error, set_val should be used. --- collections/map/map.adoc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'collections/map/map.adoc') 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. -<> 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 +#include 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]] -- cgit v1.1