aboutsummaryrefslogtreecommitdiff
path: root/collections/map/map.c
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2020-07-24 15:38:15 -0400
committerTucker Evans <tuckerevans24@gmail.com>2020-07-24 15:56:31 -0400
commitbc540d32e8d2ff16ffe688b02f18d565651e4b10 (patch)
treeaefaefaeb7451086eacfde86d5d523798bd1964e /collections/map/map.c
parent1cd08117d9506d72bf4bf87a12ad4720d0892407 (diff)
Add swap function for maps
Diffstat (limited to 'collections/map/map.c')
-rw-r--r--collections/map/map.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/collections/map/map.c b/collections/map/map.c
index 5ad0b65..b0e095e 100644
--- a/collections/map/map.c
+++ b/collections/map/map.c
@@ -497,6 +497,55 @@ void *key;
return ret;
}
+void map_swap(root, ikey, jkey)
+map *root;
+void *ikey, *jkey;
+{
+ struct map_node *tmp, *inode, *jnode;
+ void *tmp_val;
+ int cmp;
+
+ tmp = root->root;
+
+searchi:
+ if (!tmp)
+ return;
+
+ cmp = root->cmp(ikey, tmp->key);
+ if (cmp < 0) {
+ tmp = tmp->left;
+ goto searchi;
+ }
+ if (cmp > 0) {
+ tmp = tmp->right;
+ goto searchi;
+ }
+
+ inode = tmp;
+
+ tmp = root->root;
+searchj:
+ if (!tmp)
+ return;
+
+ cmp = root->cmp(jkey, tmp->key);
+ if (cmp < 0) {
+ tmp = tmp->left;
+ goto searchj;
+ }
+ if (cmp > 0) {
+ tmp = tmp->right;
+ goto searchj;
+ }
+
+ jnode = tmp;
+ tmp_val = jnode->val;
+ jnode->val = inode->val;
+ inode->val = tmp_val;
+
+ return;
+}
+
void map_clear_aux(root)
struct map_node *root;
{