From 15de5dc2c5d82e816cd9c0a7c56b7e1d4165717f Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 6 Jul 2020 20:50:38 -0400 Subject: Add index function for maps --- collections/map/map.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'collections/map/map.c') diff --git a/collections/map/map.c b/collections/map/map.c index 7de58e4..0600c8b 100644 --- a/collections/map/map.c +++ b/collections/map/map.c @@ -105,6 +105,25 @@ void *key; return tmp; } +void* map_index(root, key) +map *root; +void *key; +{ + int cmp; + if (!root || !key) + return NULL; + + cmp = root->cmp(root->key, key); + + if (cmp < 0) + return map_index(root->left, key); + + if (cmp > 0) + return map_index(root->right, key); + + return root->val; +} + void map_clear(root) map *root; { -- cgit v1.1