1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it if you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or as a part of
11 # Nit implementation of the Ropes (see Ropes : An Alternative to Strings,
12 # SOFTWARE - PRACTICE AND EXPERIENCE, VOL. 25(12), 1315 - 1330 (DECEMBER 1995)
13 # Hans. J Boehm, Russ Atkinson and Michael Plass)
15 # A rope is a kind of string but instead of being flat, it relies on a binary tree structure to store data.
20 # Used when searching for a particular node
21 # Returns the path to the node from the root of the rope
22 # Also, the node and the offset for seeked position in the rope
28 # Stack of the nodes traversed, and the path used
29 var stack
: List[PathElement]
32 # An element for a Path, has the concat node and whether or not
33 # left or right child was visited.
34 private class PathElement
37 # Was the left child visited ?
39 # Was the right child visited ?
44 private abstract class RopeNode
49 # Node that represents a concatenation between two nodes (of any RopeNode type)
53 # Left child of the node
54 var _left
: nullable RopeNode = null
55 # Right child of the node
56 var _right
: nullable RopeNode = null
58 fun left
: nullable RopeNode do return _left
59 fun right
: nullable RopeNode do return _right
61 fun left
=(l
: RopeNode)
65 if _right
!= null then length
+= _right
.length
68 fun right
=(r
: RopeNode)
72 if _left
!= null then length
+= _left
.length
76 # Leaf of a Rope, contains a FlatString
80 # Encapsulated FlatString in the leaf node
83 init(val
: FlatString) do
90 # Basic structure, binary tree with a root node.
92 # Also shared services by subsequent implementations.
96 # Root node, entry point of a Rope.
97 private var root
: RopeNode
102 # Creates a new Rope with `s` as root
103 init from
(s
: String) do
104 if s
isa RopeString then root
= s
.root
else root
= new Leaf(s
.as(FlatString))
107 private init from_root
(r
: RopeNode)
112 redef fun length
do return root
.length
114 # Iterator on the nodes of the rope, in forward postfix order
115 private fun postfix
(from
: Int): Postfix do return new Postfix.from
(self, from
)
117 # Iterator on the leaves of the rope, forward order
118 private fun leaves
(from
: Int): LeavesIterator do return new LeavesIterator(self, from
)
120 # Iterator on the substrings from 0, in forward order
121 fun substrings
: IndexedIterator[Text] do return new SubstringsIterator(self, 0)
123 # Iterator on the substrings, starting at position `from`, in forward order
124 fun substrings_from
(from
: Int): IndexedIterator[Text] do return new SubstringsIterator(self, from
)
126 # Path to the Leaf for `position`
127 private fun node_at
(position
: Int): Path
129 assert position
>= 0 and position
< length
130 return get_node_from
(root
.as(not null), 0, position
, new List[PathElement])
133 # Builds the path to Leaf at position `seek_pos`
134 private fun get_node_from
(node
: RopeNode, curr_pos
: Int, seek_pos
: Int, stack
: List[PathElement]): Path
137 if node
isa Leaf then return new Path(node
, seek_pos
- curr_pos
, stack
)
138 node
= node
.as(Concat)
140 if node
.left
!= null then
141 var next_pos
= curr_pos
+ node
.left
.length
142 stack
.add
(new PathElement(node
))
143 if next_pos
> seek_pos
then
144 stack
.last
.left
= true
145 return get_node_from
(node
.left
.as(not null), curr_pos
, seek_pos
, stack
)
147 stack
.last
.right
= true
148 return get_node_from
(node
.right
.as(not null), next_pos
, seek_pos
, stack
)
150 var vis
= new PathElement(node
)
153 return get_node_from
(node
.right
.as(not null), curr_pos
, seek_pos
, stack
)
159 # Rope that cannot be modified
164 redef fun to_s
do return self
166 # Inserts a String `str` at position `pos`
167 fun insert_at
(str
: String, pos
: Int): RopeString
169 if str
.length
== 0 then return self
170 if self.length
== 0 then return new RopeString.from
(str
)
172 assert pos
>= 0 and pos
<= length
174 if pos
== length
then return append
(str
).as(RopeString)
176 var path
= node_at
(pos
)
178 var last_concat
= new Concat
180 if path
.offset
== 0 then
181 last_concat
.right
= path
.leaf
182 if str
isa FlatString then last_concat
.left
= new Leaf(str
) else last_concat
.left
= str
.as(RopeString).root
183 else if path
.offset
== path
.leaf
.length
then
184 if str
isa FlatString then last_concat
.right
= new Leaf(str
) else last_concat
.right
= str
.as(RopeString).root
185 last_concat
.left
= path
.leaf
187 var s
= path
.leaf
.str
188 var l_half
= s
.substring
(0, s
.length
- path
.offset
)
189 var r_half
= s
.substring_from
(s
.length
- path
.offset
)
191 cct
.right
= new Leaf(r_half
)
192 last_concat
.left
= new Leaf(l_half
)
193 if str
isa FlatString then last_concat
.right
= new Leaf(str
) else last_concat
.right
= str
.as(RopeString).root
194 cct
.left
= last_concat
198 for i
in path
.stack
.reverse_iterator
do
201 nod
.right
= i
.node
.right
.as(not null)
202 nod
.left
= last_concat
204 nod
.left
= i
.node
.left
.as(not null)
205 nod
.right
= last_concat
210 return new RopeString.from_root
(last_concat
)
213 # Adds `s` at the end of self
214 fun append
(s
: String): String
216 if self.is_empty
then return s
217 return new RopeString.from_root
(append_to_path
(root
,s
))
220 # Builds a new path from root to the rightmost node with s appended
221 private fun append_to_path
(node
: RopeNode, s
: String): RopeNode
224 if node
isa Leaf then
226 if s
isa FlatString then cct
.right
= new Leaf(s
) else cct
.right
= s
.as(RopeString).root
227 else if node
isa Concat then
228 var right
= node
.right
229 if node
.left
!= null then cct
.left
= node
.left
.as(not null)
230 if right
== null then
231 if s
isa FlatString then cct
.right
= new Leaf(s
) else cct
.right
= s
.as(RopeString).root
233 cct
.right
= append_to_path
(right
, s
)
241 # var rope = new RopeString.from("abcd")
242 # assert rope.substring(1, 2) == "bc"
243 # assert rope.substring(-1, 2) == "a"
244 # assert rope.substring(1, 0) == ""
245 # assert rope.substring(2, 5) == "cd"
247 redef fun substring
(pos
, len
)
254 if pos
+ len
> length
then len
= length
- pos
256 if len
<= 0 then return new RopeString.from
("")
258 var path
= node_at
(pos
)
261 var offset
= path
.offset
263 if path
.leaf
.str
.length
- offset
> len
then lf
= new Leaf(lf
.str
.substring
(offset
,len
)) else lf
= new Leaf(lf
.str
.substring_from
(offset
))
265 var nod
: RopeNode = lf
267 for i
in path
.stack
.reverse_iterator
do
268 if i
.right
then continue
272 if r
!= null then tmp
.right
= r
276 var ret
= new RopeString
279 path
= ret
.node_at
(len-1
)
282 nod
= new Leaf(path
.leaf
.str
.substring
(0, offset
+1))
284 for i
in path
.stack
.reverse_iterator
do
285 if i
.left
then continue
289 if l
!= null then tmp
.left
= l
299 # Used to iterate on a Rope
300 private class IteratorElement
311 # The node being visited
313 # If the node has a left child, was it visited ?
315 # If the node has a right child, was it visited ?
317 # Was the current node visited ?
321 # Simple Postfix iterator on the nodes of a Rope
322 private class Postfix
323 super IndexedIterator[RopeNode]
325 # Target Rope to iterate on
328 # Current position in Rope
332 var stack
= new List[IteratorElement]
334 init from
(tgt
: Rope, pos
: Int)
338 if pos
< 0 or pos
>= tgt
.length
then return
339 var path
= tgt
.node_at
(pos
)
340 self.pos
-= path
.offset
341 for i
in path
.stack
do
342 var item
= new IteratorElement(i
.node
)
344 if i
.right
then item
.right
= true
347 var item
= new IteratorElement(path
.leaf
)
355 return stack
.last
.node
358 redef fun is_ok
do return not stack
.is_empty
360 redef fun index
do return pos
363 if stack
.is_empty
then return
364 if pos
> target
.length-1
then
370 if lst
.node
isa Leaf then
371 pos
+= lst
.node
.length
380 if nod
isa Concat and nod
.left
!= null then
381 stack
.push
(new IteratorElement(nod
.left
.as(not null)))
386 if not lst
.right
then
389 if nod
isa Concat and nod
.right
!= null then
390 stack
.push
(new IteratorElement(nod
.right
.as(not null)))
399 # Iterates on the leaves (substrings) of the Rope
401 super IndexedIterator[Leaf]
403 private var nodes
: Postfix
405 init(tgt
: Rope, pos
: Int)
407 nodes
= tgt
.postfix
(pos
)
410 redef fun is_ok
do return nodes
.is_ok
415 return nodes
.item
.as(Leaf)
418 redef fun index
do return nodes
.index
424 if nodes
.is_ok
and nodes
.item
isa Leaf then break
429 # Uses the leaves and calculates a new substring on each iteration
430 class SubstringsIterator
431 super IndexedIterator[Text]
433 private var nodes
: IndexedIterator[Leaf]
435 # Current position in Rope
438 # Current substring, computed from the current Leaf and indexes
441 init(tgt
: Rope, pos
: Int)
443 nodes
= tgt
.leaves
(pos
)
445 if pos
< 0 or pos
>= tgt
.length
then return
449 # Compute the bounds of the current substring and makes the substring
450 private fun make_substring
452 substring
= nodes
.item
.str
454 var length
= substring
.length
455 if nodes
.index
< pos
then
456 min
= pos
- nodes
.index
458 substring
= substring
.substring
(min
, length
)
461 redef fun is_ok
do return nodes
.is_ok
469 redef fun index
do return pos
473 pos
+= substring
.length
475 if nodes
.is_ok
then make_substring