diff options
Diffstat (limited to 'structures')
| -rw-r--r-- | structures/rope/rope.c | 65 | 
1 files changed, 65 insertions, 0 deletions
| 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; +} | 
