-# /!\ Non Thread-safe /!\
-#
-class BufferRope
- super Rope
-
- var is_dirty: Bool = false
-
- init
- do
- super
- end
-
- init with_string(str)
- do
- super
- end
-
- ############################################################################
- # Tree Balancing Methods #
- ############################################################################
-
- # Performs a right rotation on a node of the Rope
- #
- # Root Pivot
- # / \ / \
- # Pivot Leaf3 => Leaf1 Root
- # / \ / \
- # Leaf1 Leaf2 Leaf2 Leaf3
- private fun rotate_right(root: ConcatNode)
- do
- assert root.left_child != null
- var pivot = root.left_child.as(ConcatNode)
- var root_new_left = pivot.right_child
- var root_parent = root.parent
-
- root.left_child = root_new_left
- pivot.right_child = root
- if root_parent == null then
- self.parent_node = pivot
- pivot.parent = null
- return
- end
-
- if root_parent.left_child == root then
- root_parent.left_child = pivot
- else
- root_parent.right_child = pivot
- end
-
- root.update_data
- pivot.update_data
- root_parent.update_data
- end
-
- # Performs a left rotation on a node of the Rope
- #
- # Root Pivot
- # / \ / \
- # Leaf1 Pivot => Root Leaf3
- # / \ / \
- # Leaf2 Leaf3 Leaf1 Leaf2
- private fun rotate_left(root: ConcatNode)
- do
- assert root.right_child != null
- var pivot = root.right_child.as(ConcatNode)
- var root_new_right = pivot.left_child
- var root_parent = root.parent
-
- root.right_child = root_new_right
- pivot.left_child = root
- if root_parent == null then
- self.parent_node = pivot
- pivot.parent = null
- return
- end
-
- if root_parent.left_child == root then
- root_parent.left_child = pivot
- else
- root_parent.right_child = pivot
- end
-
- root.update_data
- pivot.update_data
- root_parent.update_data
- end
-
- # Shortcut method to balance a node and its parents
- # based on the rules of the AVL Tree
- private fun balance_from_node(parent_node: nullable ConcatNode)
- do
- while parent_node != null do
- parent_node.update_data
- var node_balance = parent_node.balance_factor
- if node_balance < -1 or node_balance > 1 then
- balance_node(parent_node)
- end
- parent_node = parent_node.parent
- end
- end
-
- # Performs rotations to balance a node according to AVL Tree rules
- private fun balance_node(node: ConcatNode)
- do
- var balance_factor = node.balance_factor
- if balance_factor < -1 then
- var right_balance = node.right_child.balance_factor
- if right_balance < 0 then
- rotate_left(node)
- else
- rotate_right(node.right_child.as(ConcatNode))
- rotate_left(node)
- end
- else
- var left_balance = node.left_child.balance_factor
- if left_balance > 0 then
- rotate_right(node)
- else
- rotate_left(node.left_child.as(ConcatNode))
- rotate_right(node)
- end
- end
- end
-
- ############################################################################
- # BufferRope exclusive Methods #
- ############################################################################
-
- # Appends a new Collection[Char] at the end of the current rope
- fun append(str: String): BufferRope
- do
- var last_node = parent_node
-
- while last_node isa ConcatNode and last_node.right_child != null do
- last_node = last_node.right_child.as(not null)
- end
-
- if last_node isa ConcatNode then
- last_node.right_child = new LeafNode(str.to_s)
- else if last_node isa LeafNode then
- var last_node_parent = last_node.parent
- var new_concat = new ConcatNode
- last_node_parent.right_child = new_concat
- new_concat.left_child = last_node
- new_concat.right_child = new LeafNode(str.to_s)
- last_node = new_concat
- else
- print "Fatal Error, please report to the developers for more insight."
- abort
- end
-
- balance_from_node(last_node)
-
- is_dirty = true
-
- return self
- end
-
- # Variatic function to append several collections of Chars
- fun append_multi(strs: String...): BufferRope
- do
- for i in strs do
- append(i)
- end
- return self
- end
-
- # Adds a new Collection[Char] at the beginning of the rope
- fun prepend(str: String): BufferRope
- do
- var curr_node = parent_node
-
- while curr_node isa ConcatNode and curr_node.left_child != null do
- curr_node = curr_node.left_child.as(not null)
- end
-
- if curr_node isa ConcatNode then
- curr_node.left_child = new LeafNode(str.to_s)
- else if curr_node isa LeafNode then
- var parent = curr_node.parent
- var new_concat = new ConcatNode
- var new_leaf = new LeafNode(str.to_s)
- new_concat.left_child = new_leaf
- new_concat.right_child = curr_node
- parent.left_child = new_concat
- curr_node = new_concat
- else
- print "Fatal Error"
- abort
- end
-
- balance_from_node(curr_node)
-
- is_dirty = true
-
- return self
- end
-
- # Variatic function to prepend several collections of Chars
- fun prepend_multi(strs: String...): BufferRope
- do
- for i in strs do
- prepend(i)
- end
- return self
- end
-
- # Adds the content of `str` after self, does not create a new rope object
- fun concat(str: Rope): Rope
- do
- var other_iter = new DFSRopeLeafIterator(str)
-
- var modif_list = new List[String]
-
- while other_iter.is_ok do
- modif_list.push(other_iter.item.value)
- other_iter.next
- end
-
- while modif_list.length > 0 do
- self.append(modif_list.shift)
- end
-
- if not is_dirty then is_dirty = true
-
- return self
- end
-
- # Returns the content of the current BufferRope object as an ImmutableRope
- fun freeze: ImmutableRope
- do
- var buffer_rope = new BufferRope
- var new_rope = new ImmutableRope
-
- var iter = new DFSRopeLeafIterator(self)
-
- while iter.is_ok do
- buffer_rope.append(iter.item.value)
- iter.next
- end
-
- new_rope.parent_node = buffer_rope.parent_node
-
- if not is_dirty then new_rope.str_representation = self.str_representation
-
- return new_rope
- end
-
- # Unsafe method to convert self as an ImmutableRope
- #
- # To be used internally only
- private fun to_immutable: ImmutableRope
- do
- var immutable_self = new ImmutableRope
- immutable_self.parent_node = self.parent_node
- return immutable_self
- end
-
- ############################################################################
- # Rope refined Methods #
- ############################################################################
-
- redef fun subrope(index_from: Int, count: Int): BufferRope
- do
- return super.as(BufferRope)
- end