From 6f96cec5ae1a07580a2d0da7f8a3c6a7ce01284e Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Wed, 4 Jun 2014 16:31:54 -0400 Subject: [PATCH] lib/standard/ropes: Nodes for Ropes. Signed-off-by: Lucas Bajolet --- lib/standard/ropes.nit | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/lib/standard/ropes.nit b/lib/standard/ropes.nit index 54226d5..b5697bd 100644 --- a/lib/standard/ropes.nit +++ b/lib/standard/ropes.nit @@ -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 + -- 1.7.9.5