diff options
author | Tucker Evans <tucker@tuckerevans.com> | 2020-07-06 19:47:22 -0400 |
---|---|---|
committer | Tucker Evans <tucker@tuckerevans.com> | 2020-07-08 11:02:40 -0400 |
commit | 51d368c20e1d93970a94cb0d2aff10654922bb37 (patch) | |
tree | 2903118e96ca69a499e91e63c5ee6c217b47e44d /collections/map/map.c | |
parent | 0cca188d0ccbfe3d87805c74b63fef72a58a2b84 (diff) |
Add insert function for maps
Diffstat (limited to 'collections/map/map.c')
-rw-r--r-- | collections/map/map.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/collections/map/map.c b/collections/map/map.c index 1b6cbaa..83a446d 100644 --- a/collections/map/map.c +++ b/collections/map/map.c @@ -45,6 +45,43 @@ map *root; return map_size(root->left) + map_size(root->right) + 1; } +int map_insert(root, key, val) +map *root; +void *key, *val; +{ + int cmp; + + if (!root) + return -1; + + if (!key) { + root->key = key; + root->val = val; + return 0; + } + + cmp = root->cmp(root->key, key); + + if (cmp == 0 && root->key == key) { + root->val = val; + } else if (cmp < 0) { + + if (!root->left) + root->left = map_new_from_parent(root); + + map_insert(root->left, key, val); + } else if (cmp > 0) { + if (!root->right) + root->right = map_new_from_parent(root); + + map_insert(root->right, key, val); + } else { + return -1; + } + + return 0; +} + void map_clear(root) map *root; { |