From af1c42d4bf235779d05c135a87f2c95c83df6940 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 8 Jul 2020 14:34:24 -0400 Subject: Fix separate base map type from tree structure (nodes) This was initially needed so we can rotate on the root node, also should cut down on memory usage (albeit by a tiny amount) by not copying the comparison function pointer with every node. It feels like a more clean solution anyways. ---------------------------------------------------------------------- Squashed commit of the following: commit 056e73216cd850ae563fb6da27657cf17d0514b6 Author: Tucker Evans Date: Wed Jul 8 14:33:39 2020 -0400 Fix map free types with auxiliary function for map trees commit 1014cb41b38e987062040afd338f34a12fc0e7bc Author: Tucker Evans Date: Wed Jul 8 14:33:03 2020 -0400 Fix map clear types with auxiliary function for map trees commit 913d1f5e4358e7a8a3057027698320050a1ea472 Author: Tucker Evans Date: Wed Jul 8 14:28:54 2020 -0400 Fix index map types with auxiliary function for map trees commit 8d76fc0b4abcd49f67f7bff55a4da83b7ab457d6 Author: Tucker Evans Date: Wed Jul 8 14:27:42 2020 -0400 Fix map set key types with auxilary function for map trees commit fec252a5214ab02dccfe90bfe8b548e0e872f6ef Author: Tucker Evans Date: Wed Jul 8 14:22:59 2020 -0400 Fix check key ptr types with auxiliary function for map tree commit 9170db7f6aaefa1b0824e0b4a2a4acc73865c151 Author: Tucker Evans Date: Wed Jul 8 14:17:22 2020 -0400 Fix change set val types with auxilary function for map trees commit 9be9d3d5fd07541fbe6d4c1ec97be9c943b1455d Author: Tucker Evans Date: Wed Jul 8 14:04:00 2020 -0400 Fix map insert types with auxiliary function for map trees commit 2a561c8320f6be8b415d62e1175ef4ff4f845962 Author: Tucker Evans Date: Wed Jul 8 14:00:15 2020 -0400 Fix map size types with auxiliary function for map trees commit 361645cbb4578900d6b3d32a84b9a2b94716d5d1 Author: Tucker Evans Date: Wed Jul 8 13:59:32 2020 -0400 Fix change new function for map type change commit 9a99bc4149307a1fa012bcb98bedf2a8569a822c Author: Tucker Evans Date: Wed Jul 8 13:58:57 2020 -0400 Fix internal functions for change in map type commit 0335d8fd4f7aaf2e24e282bd39d02bc2714b0061 Author: Tucker Evans Date: Wed Jul 8 13:53:44 2020 -0400 Add struct to hold metadata/root of tree for map This was needed so we can rotate on the root node, also should cut down on memory usage (albeit by a tiny amount) by not copying the comparison function pointer with every node. --- collections/map/map.adoc | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'collections/map/map.adoc') diff --git a/collections/map/map.adoc b/collections/map/map.adoc index ce4999b..09dfde7 100644 --- a/collections/map/map.adoc +++ b/collections/map/map.adoc @@ -1,7 +1,7 @@ Map === Tucker Evans -v0.5, 2020-07-06 +v0.6, 2020-07-06 A basic map implemented in an AVL tree. @@ -96,6 +96,25 @@ else assert(map_size(dict) == 1); ---- +[[map_set_val]] ++void* map_set_val(map *self, void *key, void *val)+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This replaces the value stored at +key+ (or equivalent) with +val+, the value +that is overwritten is return so the user can free it. + +Examples +^^^^^^^^ +[source,c] +---- +#include "map.h" +#include + +map *dict = map_new((cmp_func) strcmp); + +map_insert(dict, strdup("ONE"), strdup("VALUE")); +free(map_set_val(dict, "ONE", "NEW_VALUE")); +---- + [[map_check_key_ptr]] +int map_check_key_ptr(map *self, void *key)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -122,7 +141,6 @@ eq_key2 = strdup("ONE"); if (!map_check_key_ptr(dict, eq_key2)) { free(map_set_key(dict, eq_key2)); } - ---- [[map_set_key]] @@ -140,10 +158,14 @@ Examples map *dict = map_new((cmp_func) strcmp); -map_insert(dict, strdup("ONE"), "VALUE"); -assert(map_insert(dict, "ONE", "NEW_VALUE") < 0); -free(map_set_key(dict "ONE")); -assert(map_insert(dict, "ONE", "NEW_VALUE") == 0); +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_index]] -- cgit v1.1