From 18020f74d43758be53351a05129f771099257c6b Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Sun, 3 Jan 2021 17:00:20 -0500 Subject: Add split function for ropes Ropes should be rebalanced at the end of this function once rope_rebalance() is implemented. --- structures/rope/rope.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/structures/rope/rope.c b/structures/rope/rope.c index 1f0a389..6e79b24 100644 --- a/structures/rope/rope.c +++ b/structures/rope/rope.c @@ -131,3 +131,68 @@ rope *node1, *node2; return tmp; } + +rope* rope_split(root, i) +rope **root; +size_t i; +{ + rope **tmp, *split, *cur; + + rope *orig = *root; + + if (!root ) + return NULL; + + if (!i) { + split = *root; + *root = NULL; + return split; + } + + + tmp = root; + printf("i: %d\n", i); + while (!(*tmp)->str) { + if ((*tmp)->len <= i && (*tmp)->right) { + i -= (*tmp)->len; + printf("i: %d\n", i); + tmp = &(*tmp)->right; + } else if ((*tmp)->left) { + tmp = &(*tmp)->left; + } + } + + if (i != 0) { + split = str_to_rope((*tmp)->str + i); + (*tmp)->str[i] = '\0'; + /*Maybe want to strdup this and free to reduce memory usage*/ + + cur = (*tmp)->parent; + *tmp = rope_concat(*tmp, split); + (*tmp)->parent = cur; + + split = (*tmp)->right; + (*tmp)->right = NULL; + } else { + split = *tmp; + } + + + for(cur = split; cur->parent; cur = cur->parent) { + if (cur->parent->right == cur) + continue; + + split = rope_concat(split, cur->parent->right); + cur->parent->right = NULL; + } + + if (cur && cur->right == split) + cur->right = NULL; + *root = cur; + + /*TODO should be rebalancing rather than just recalulating lengths*/ + rope_relen(split); + rope_relen(*root); + + return split; +} -- cgit v1.1