aboutsummaryrefslogtreecommitdiff
path: root/collections/map/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'collections/map/map.c')
-rw-r--r--collections/map/map.c37
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;
{