lib/standard/ropes: Nodes for Ropes.
authorLucas Bajolet <r4pass@hotmail.com>
Wed, 4 Jun 2014 20:31:54 +0000 (16:31 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Wed, 4 Jun 2014 20:31:54 +0000 (16:31 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/ropes.nit

index 54226d5..b5697bd 100644 (file)
@@ -17,3 +17,50 @@ module ropes
 
 intrude import string
 
+# A node for a Rope
+private abstract class RopeNode
+       # Length of the node
+       var length = 0
+end
+
+# Node that represents a concatenation between two nodes (of any RopeNode type)
+private class Concat
+       super RopeNode
+
+       # Left child of the node
+       var _left: nullable RopeNode = null
+       # Right child of the node
+       var _right: nullable RopeNode = null
+
+       fun left: nullable RopeNode do return _left
+       fun right: nullable RopeNode do return _right
+
+       fun left=(l: RopeNode)
+       do
+               _left = l
+               length = l.length
+               if _right != null then length += _right.length
+       end
+
+       fun right=(r: RopeNode)
+       do
+               _right = r
+               length = r.length
+               if _left != null then length += _left.length
+       end
+end
+
+# Leaf of a Rope, contains a FlatString
+private class Leaf
+       super RopeNode
+
+       # Encapsulated FlatString in the leaf node
+       var str: FlatString
+
+       init(val: FlatString) do
+               self.str = val
+               length = str.length
+       end
+
+end
+