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
99 # Cached version of self as a flat String
100 private var str_representation
: nullable NativeString = null
105 # Creates a new Rope with `s` as root
106 init from
(s
: String) do
107 if s
isa RopeString then root
= s
.root
else root
= new Leaf(s
.as(FlatString))
110 private init from_root
(r
: RopeNode)
115 redef fun length
do return root
.length
117 # Iterator on the nodes of the rope, in forward postfix order
118 private fun postfix
(from
: Int): Postfix do return new Postfix.from
(self, from
)
120 # Iterator on the leaves of the rope, forward order
121 private fun leaves
(from
: Int): LeavesIterator do return new LeavesIterator(self, from
)
123 # Iterator on the substrings from 0, in forward order
124 fun substrings
: IndexedIterator[Text] do return new SubstringsIterator(self, 0)
126 # Iterator on the substrings, starting at position `from`, in forward order
127 fun substrings_from
(from
: Int): IndexedIterator[Text] do return new SubstringsIterator(self, from
)
131 for i
in substrings
do
138 if str_representation
!= null then return str_representation
.as(not null)
140 var native_final_str
= calloc_string
(length
+ 1)
142 native_final_str
[length
] = '\0'
144 if self.length
== 0 then
145 str_representation
= native_final_str
146 return native_final_str
151 for i
in substrings
do
153 if str
isa FlatString then str
.items
.copy_to
(native_final_str
, str
.length
, str
.index_from
, offset
)
157 str_representation
= native_final_str
159 return native_final_str
162 # Path to the Leaf for `position`
163 private fun node_at
(position
: Int): Path
165 assert position
>= 0 and position
< length
166 return get_node_from
(root
.as(not null), 0, position
, new List[PathElement])
169 # Builds the path to Leaf at position `seek_pos`
170 private fun get_node_from
(node
: RopeNode, curr_pos
: Int, seek_pos
: Int, stack
: List[PathElement]): Path
173 if node
isa Leaf then return new Path(node
, seek_pos
- curr_pos
, stack
)
174 node
= node
.as(Concat)
176 if node
.left
!= null then
177 var next_pos
= curr_pos
+ node
.left
.length
178 stack
.add
(new PathElement(node
))
179 if next_pos
> seek_pos
then
180 stack
.last
.left
= true
181 return get_node_from
(node
.left
.as(not null), curr_pos
, seek_pos
, stack
)
183 stack
.last
.right
= true
184 return get_node_from
(node
.right
.as(not null), next_pos
, seek_pos
, stack
)
186 var vis
= new PathElement(node
)
189 return get_node_from
(node
.right
.as(not null), curr_pos
, seek_pos
, stack
)
195 if not o
isa Text then return false
196 if o
.length
!= self.length
then return false
197 var oit
= o
.chars
.iterator
198 for i
in self.chars
.iterator
do
199 if i
!= oit
.item
then return false
206 # Rope that cannot be modified
211 redef fun to_s
do return self
213 redef fun empty
do return once
new RopeString.from
("")
217 var ret
= empty
.as(RopeString)
218 for i
in substrings
do
219 ret
= ret
.prepend
(i
.reversed
.to_s
).as(RopeString)
227 for i
in substrings
do
236 for i
in substrings
do
242 redef fun +(o
) do return insert_at
(o
.to_s
, length
)
246 var ret
= new RopeString.from
("")
248 ret
= (ret
+ self).as(RopeString)
253 # Inserts a String `str` at position `pos`
254 fun insert_at
(str
: String, pos
: Int): RopeString
256 if str
.length
== 0 then return self
257 if self.length
== 0 then return new RopeString.from
(str
)
259 assert pos
>= 0 and pos
<= length
261 if pos
== length
then return append
(str
).as(RopeString)
263 var path
= node_at
(pos
)
265 var last_concat
= new Concat
267 if path
.offset
== 0 then
268 last_concat
.right
= path
.leaf
269 if str
isa FlatString then last_concat
.left
= new Leaf(str
) else last_concat
.left
= str
.as(RopeString).root
270 else if path
.offset
== path
.leaf
.length
then
271 if str
isa FlatString then last_concat
.right
= new Leaf(str
) else last_concat
.right
= str
.as(RopeString).root
272 last_concat
.left
= path
.leaf
274 var s
= path
.leaf
.str
275 var l_half
= s
.substring
(0, s
.length
- path
.offset
)
276 var r_half
= s
.substring_from
(s
.length
- path
.offset
)
278 cct
.right
= new Leaf(r_half
)
279 last_concat
.left
= new Leaf(l_half
)
280 if str
isa FlatString then last_concat
.right
= new Leaf(str
) else last_concat
.right
= str
.as(RopeString).root
281 cct
.left
= last_concat
285 for i
in path
.stack
.reverse_iterator
do
288 nod
.right
= i
.node
.right
.as(not null)
289 nod
.left
= last_concat
291 nod
.left
= i
.node
.left
.as(not null)
292 nod
.right
= last_concat
297 return new RopeString.from_root
(last_concat
)
300 # Adds `s` at the beginning of self
301 fun prepend
(s
: String): String do return insert_at
(s
, 0)
303 # Adds `s` at the end of self
304 fun append
(s
: String): String
306 if self.is_empty
then return s
307 return new RopeString.from_root
(append_to_path
(root
,s
))
310 # Builds a new path from root to the rightmost node with s appended
311 private fun append_to_path
(node
: RopeNode, s
: String): RopeNode
314 if node
isa Leaf then
316 if s
isa FlatString then cct
.right
= new Leaf(s
) else cct
.right
= s
.as(RopeString).root
317 else if node
isa Concat then
318 var right
= node
.right
319 if node
.left
!= null then cct
.left
= node
.left
.as(not null)
320 if right
== null then
321 if s
isa FlatString then cct
.right
= new Leaf(s
) else cct
.right
= s
.as(RopeString).root
323 cct
.right
= append_to_path
(right
, s
)
331 # var rope = new RopeString.from("abcd")
332 # assert rope.substring(1, 2) == "bc"
333 # assert rope.substring(-1, 2) == "a"
334 # assert rope.substring(1, 0) == ""
335 # assert rope.substring(2, 5) == "cd"
337 redef fun substring
(pos
, len
)
344 if pos
+ len
> length
then len
= length
- pos
346 if len
<= 0 then return new RopeString.from
("")
348 var path
= node_at
(pos
)
351 var offset
= path
.offset
353 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
))
355 var nod
: RopeNode = lf
357 for i
in path
.stack
.reverse_iterator
do
358 if i
.right
then continue
362 if r
!= null then tmp
.right
= r
366 var ret
= new RopeString
369 path
= ret
.node_at
(len-1
)
372 nod
= new Leaf(path
.leaf
.str
.substring
(0, offset
+1))
374 for i
in path
.stack
.reverse_iterator
do
375 if i
.left
then continue
379 if l
!= null then tmp
.left
= l
389 # Used to iterate on a Rope
390 private class IteratorElement
401 # The node being visited
403 # If the node has a left child, was it visited ?
405 # If the node has a right child, was it visited ?
407 # Was the current node visited ?
411 # Simple Postfix iterator on the nodes of a Rope
412 private class Postfix
413 super IndexedIterator[RopeNode]
415 # Target Rope to iterate on
418 # Current position in Rope
422 var stack
= new List[IteratorElement]
424 init from
(tgt
: Rope, pos
: Int)
428 if pos
< 0 or pos
>= tgt
.length
then return
429 var path
= tgt
.node_at
(pos
)
430 self.pos
-= path
.offset
431 for i
in path
.stack
do
432 var item
= new IteratorElement(i
.node
)
434 if i
.right
then item
.right
= true
437 var item
= new IteratorElement(path
.leaf
)
445 return stack
.last
.node
448 redef fun is_ok
do return not stack
.is_empty
450 redef fun index
do return pos
453 if stack
.is_empty
then return
454 if pos
> target
.length-1
then
460 if lst
.node
isa Leaf then
461 pos
+= lst
.node
.length
470 if nod
isa Concat and nod
.left
!= null then
471 stack
.push
(new IteratorElement(nod
.left
.as(not null)))
476 if not lst
.right
then
479 if nod
isa Concat and nod
.right
!= null then
480 stack
.push
(new IteratorElement(nod
.right
.as(not null)))
489 # Iterates on the leaves (substrings) of the Rope
491 super IndexedIterator[Leaf]
493 private var nodes
: Postfix
495 init(tgt
: Rope, pos
: Int)
497 nodes
= tgt
.postfix
(pos
)
500 redef fun is_ok
do return nodes
.is_ok
505 return nodes
.item
.as(Leaf)
508 redef fun index
do return nodes
.index
514 if nodes
.is_ok
and nodes
.item
isa Leaf then break
519 # Uses the leaves and calculates a new substring on each iteration
520 class SubstringsIterator
521 super IndexedIterator[Text]
523 private var nodes
: IndexedIterator[Leaf]
525 # Current position in Rope
528 # Current substring, computed from the current Leaf and indexes
531 init(tgt
: Rope, pos
: Int)
533 nodes
= tgt
.leaves
(pos
)
535 if pos
< 0 or pos
>= tgt
.length
then return
539 # Compute the bounds of the current substring and makes the substring
540 private fun make_substring
542 substring
= nodes
.item
.str
544 var length
= substring
.length
545 if nodes
.index
< pos
then
546 min
= pos
- nodes
.index
548 substring
= substring
.substring
(min
, length
)
551 redef fun is_ok
do return nodes
.is_ok
559 redef fun index
do return pos
563 pos
+= substring
.length
565 if nodes
.is_ok
then make_substring
570 class RopeCharIterator
571 super IndexedIterator[Char]
573 var substrings
: IndexedIterator[Text]
579 var substr_iter
: IndexedIterator[Char]
581 init(tgt
: Rope, from
: Int)
583 substrings
= tgt
.substrings_from
(from
)
585 if not substrings
.is_ok
then
590 substr_iter
= substrings
.item
.chars
.iterator
593 redef fun item
do return substr_iter
.item
595 redef fun is_ok
do return pos
<= max
597 redef fun index
do return pos
602 if substr_iter
.is_ok
then
605 if not substr_iter
.is_ok
then
607 if substrings
.is_ok
then
608 substr_iter
= substrings
.item
.chars
.iterator