diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-06 15:49:49 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-08 10:56:36 -0400 |
commit | 50846316de122801e07511d4e3771a4561efa5ad (patch) | |
tree | 8f3edaafdeac9551781d823ac20f5c869a1d3c08 | |
parent | cf427c05b05e0409cfdeb6bc30b69070dd1700c3 (diff) |
Add size function for maps
-rw-r--r-- | collections/map/map.adoc | 21 | ||||
-rw-r--r-- | collections/map/map.c | 9 | ||||
-rw-r--r-- | collections/map/map.h | 3 |
3 files changed, 32 insertions, 1 deletions
diff --git a/collections/map/map.adoc b/collections/map/map.adoc index fc6b8b7..837a08a 100644 --- a/collections/map/map.adoc +++ b/collections/map/map.adoc @@ -1,7 +1,7 @@ Map === Tucker Evans -v0.1, 2020-07-06 +v0.2, 2020-07-06 A basic map implemented in an AVL tree. @@ -50,3 +50,22 @@ Examples map *dict = map_new((cmp_func) strcmp); ---- + +[[map_size]] ++map_size(map *self)+ +~~~~~~~~~~~~~~~~~~~~~ +Returns the number of key, value pairs stored in map +self+. + +Examples +^^^^^^^^ +[source,c] +---- +#include "map.h" +#include <string.h> + +map *dict = map_new((cmp_func) strcmp); + +assert(map_size(dict) == 0); +map_set(dict, "ONE", NULL); +assert(map_size(dict) == 1); +---- diff --git a/collections/map/map.c b/collections/map/map.c index 803b5ad..712a895 100644 --- a/collections/map/map.c +++ b/collections/map/map.c @@ -25,3 +25,12 @@ cmp_func cmp; return tmp; } + +int map_size(root) +map *root; +{ + if (!root || !root->key) + return 0; + + return map_size(root->left) + map_size(root->right) + 1; +} diff --git a/collections/map/map.h b/collections/map/map.h index 8aad2f4..ef4b0c8 100644 --- a/collections/map/map.h +++ b/collections/map/map.h @@ -7,4 +7,7 @@ typedef int (*cmp_func)(void*, void*); /*constructors*/ map* map_new(cmp_func); +/*management*/ +int map_size(map*); + #endif |