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
48 # Transforms the current node to a Leaf.
49 # This might be costly to invoke since this produces a FlatString concatenation.
50 # Can be used internally to limit the growth of the Rope when working with small leaves.
51 fun to_leaf
: Leaf is abstract
54 # Node that represents a concatenation between two nodes (of any RopeNode type)
58 # Left child of the node
59 var left
: nullable RopeNode
60 # Right child of the node
61 var right
: nullable RopeNode
63 init(l
: nullable RopeNode, r
: nullable RopeNode)
67 if l
!= null then length
+= l
.length
68 if r
!= null then length
+= r
.length
74 if right
== null then return new StringLeaf("".as(FlatString))
77 if right
== null then return left
.as(not null).to_leaf
78 return new StringLeaf((left
.to_leaf
.str
.as(FlatString) + right
.to_leaf
.str
.as(FlatString)).as(FlatString))
82 # Leaf of a Rope, contains a FlatString
83 private abstract class Leaf
86 # Encapsulated FlatString in the leaf node
91 private class StringLeaf
94 init(val
: FlatString) do
99 redef fun to_leaf
do return self
102 # Basic structure, binary tree with a root node.
104 # Also shared services by subsequent implementations.
108 # Root node, entry point of a Rope.
109 private var root
: RopeNode
111 # Cached version of self as a flat String
112 private var str_representation
: nullable NativeString = null
117 # Creates a new Rope with `s` as root
118 init from
(s
: String) do
119 if s
isa RopeString then root
= s
.root
else root
= new StringLeaf(s
.as(FlatString))
122 private init from_root
(r
: RopeNode)
127 redef fun length
do return root
.length
129 # Iterator on the nodes of the rope, in forward postfix order
130 private fun postfix
(from
: Int): Postfix do return new Postfix.from
(self, from
)
132 # Iterator on the leaves of the rope, forward order
133 private fun leaves
(from
: Int): LeavesIterator do return new LeavesIterator(self, from
)
135 # Iterator on the substrings from 0, in forward order
136 redef fun substrings
do return new SubstringsIterator(self, 0)
138 # Iterator on the substrings, starting at position `from`, in forward order
139 fun substrings_from
(from
: Int): IndexedIterator[Text] do return new SubstringsIterator(self, from
)
141 # Iterator on the nodes of the rope, in backwards postfix order
142 private fun reverse_postfix
(from
: Int): ReversePostfix do return new ReversePostfix.from
(self, from
)
144 # Iterator on the leaves of the rope, backwards order
145 private fun reverse_leaves
(from
: Int): ReverseLeavesIterator do return new ReverseLeavesIterator(self,from
)
147 # Iterator on the substrings, in reverse order
148 fun reverse_substrings
: IndexedIterator[Text] do return new ReverseSubstringsIterator(self, length-1
)
150 # Iterator on the substrings, in reverse order, starting iteration at position `from`
151 fun reverse_substrings_from
(from
: Int): IndexedIterator[Text] do return new ReverseSubstringsIterator(self, from
)
155 for i
in substrings
do
162 if str_representation
!= null then return str_representation
.as(not null)
164 var native_final_str
= calloc_string
(length
+ 1)
166 native_final_str
[length
] = '\0'
168 if self.length
== 0 then
169 str_representation
= native_final_str
170 return native_final_str
175 for i
in substrings
do
177 if str
isa FlatString then str
.items
.copy_to
(native_final_str
, str
.length
, str
.index_from
, offset
)
181 str_representation
= native_final_str
183 return native_final_str
186 # Path to the Leaf for `position`
187 private fun node_at
(position
: Int): Path
189 assert position
>= 0 and position
<= length
190 if position
== length
then
191 var st
= new List[PathElement]
192 stack_to_end
(root
,st
)
193 if not st
.is_empty
then
195 var lf
= lst
.node
.right
197 return new Path(lf
.as(Leaf), lf
.length
, st
)
200 return new Path(lf
.as(Leaf), lf
.length
, st
)
203 return new Path(root
.as(Leaf), length
, st
)
206 return get_node_from
(root
, 0, position
, new List[PathElement])
209 # Special case for when the required pos is length
210 private fun stack_to_end
(nod
: RopeNode, st
: List[PathElement])
212 if nod
isa Leaf then return
213 var n
= nod
.as(Concat)
215 var ele
= new PathElement(n
)
224 # Builds the path to Leaf at position `seek_pos`
225 private fun get_node_from
(node
: RopeNode, curr_pos
: Int, seek_pos
: Int, stack
: List[PathElement]): Path
228 if node
isa Leaf then return new Path(node
, seek_pos
- curr_pos
, stack
)
229 node
= node
.as(Concat)
231 if node
.left
!= null then
232 var next_pos
= curr_pos
+ node
.left
.length
233 stack
.add
(new PathElement(node
))
234 if next_pos
> seek_pos
then
235 stack
.last
.left
= true
236 return get_node_from
(node
.left
.as(not null), curr_pos
, seek_pos
, stack
)
238 stack
.last
.right
= true
239 return get_node_from
(node
.right
.as(not null), next_pos
, seek_pos
, stack
)
241 var vis
= new PathElement(node
)
244 return get_node_from
(node
.right
.as(not null), curr_pos
, seek_pos
, stack
)
250 if not o
isa Text then return false
251 if o
.length
!= self.length
then return false
252 var oit
= o
.chars
.iterator
253 for i
in self.chars
.iterator
do
254 if i
!= oit
.item
then return false
261 # Rope that cannot be modified
266 redef fun to_s
do return self
268 redef fun empty
do return once
new RopeString.from
("")
270 redef var chars
: SequenceRead[Char] = new RopeStringChars(self)
275 for i
in substrings
do
276 ret
= i
.as(String).reversed
+ ret
284 for i
in substrings
do
285 ret
+= i
.as(String).to_upper
293 for i
in substrings
do
294 ret
+= i
.as(String).to_lower
299 redef fun +(o
) do return insert_at
(o
.to_s
, length
)
303 var ret
= new RopeString.from
("")
305 ret
= (ret
+ self).as(RopeString)
310 # Inserts a String `str` at position `pos`
311 redef fun insert_at
(str
, pos
)
313 if str
.length
== 0 then return self
314 if self.length
== 0 then return new RopeString.from
(str
)
316 assert pos
>= 0 and pos
<= length
318 var path
= node_at
(pos
)
320 var last_concat
: Concat
322 if path
.offset
== 0 then
323 if str
isa FlatString then
324 last_concat
= new Concat(new StringLeaf(str
), path
.leaf
)
326 last_concat
= new Concat(str
.as(RopeString).root
, path
.leaf
)
328 else if path
.offset
== path
.leaf
.length
then
329 if str
isa FlatString then
330 last_concat
= new Concat(path
.leaf
, new StringLeaf(str
))
332 last_concat
= new Concat(path
.leaf
, str
.as(RopeString).root
)
335 var s
= path
.leaf
.str
336 var l_half
= s
.substring
(0, s
.length
- path
.offset
)
337 var r_half
= s
.substring_from
(s
.length
- path
.offset
)
339 var ll
= new StringLeaf(l_half
.as(FlatString))
340 if str
isa FlatString then
341 cct
= new Concat(ll
, new StringLeaf(str
))
343 cct
= new Concat(ll
, str
.as(RopeString).root
)
345 last_concat
= new Concat(cct
, new StringLeaf(r_half
.as(FlatString)))
348 for i
in path
.stack
.reverse_iterator
do
350 last_concat
= new Concat(last_concat
, i
.node
.right
)
352 last_concat
= new Concat(i
.node
.left
, last_concat
)
356 return new RopeString.from_root
(last_concat
)
361 # var rope = new RopeString.from("abcd")
362 # assert rope.substring(1, 2) == "bc"
363 # assert rope.substring(-1, 2) == "a"
364 # assert rope.substring(1, 0) == ""
365 # assert rope.substring(2, 5) == "cd"
367 redef fun substring
(pos
, len
)
374 if pos
+ len
> length
then len
= length
- pos
376 if len
<= 0 then return new RopeString.from
("")
378 var path
= node_at
(pos
)
381 var offset
= path
.offset
383 if path
.leaf
.str
.length
- offset
> len
then lf
= new StringLeaf(lf
.str
.substring
(offset
,len
).as(FlatString)) else lf
= new StringLeaf(lf
.str
.substring_from
(offset
).as(FlatString))
385 var nod
: RopeNode = lf
387 for i
in path
.stack
.reverse_iterator
do
388 if i
.right
then continue
389 nod
= new Concat(nod
, i
.node
.right
)
392 var ret
= new RopeString
395 path
= ret
.node_at
(len-1
)
398 nod
= new StringLeaf(path
.leaf
.str
.substring
(0, offset
+1).as(FlatString))
400 for i
in path
.stack
.reverse_iterator
do
401 if i
.left
then continue
402 nod
= new Concat(i
.node
.left
, nod
)
411 redef class FlatString
413 redef fun insert_at
(s
, pos
)
417 var r
= new RopeString.from
(s
)
420 if pos
== length
then
421 var r
= new RopeString.from
(self)
425 var l
= substring
(0,pos
)
426 var r
= substring_from
(pos
)
427 var ret
: String = new RopeString.from
(l
)
434 private class RopeStringChars
435 super SequenceRead[Char]
441 assert pos
< tgt
.length
442 var path
= tgt
.node_at
(pos
)
443 return path
.leaf
.str
.chars
[path
.offset
]
446 redef fun iterator
do return iterator_from
(0)
448 redef fun iterator_from
(pos
) do return new RopeCharIterator(tgt
, pos
)
450 redef fun reverse_iterator
do return reverse_iterator_from
(tgt
.length-1
)
452 redef fun reverse_iterator_from
(pos
) do return new ReverseRopeCharIterator(tgt
, pos
)
455 # Used to iterate on a Rope
456 private class IteratorElement
467 # The node being visited
469 # If the node has a left child, was it visited ?
471 # If the node has a right child, was it visited ?
473 # Was the current node visited ?
477 # Simple Postfix iterator on the nodes of a Rope
478 private class Postfix
479 super IndexedIterator[RopeNode]
481 # Target Rope to iterate on
484 # Current position in Rope
488 var stack
= new List[IteratorElement]
490 init from
(tgt
: Rope, pos
: Int)
494 if pos
< 0 or pos
>= tgt
.length
then return
495 var path
= tgt
.node_at
(pos
)
496 self.pos
-= path
.offset
497 for i
in path
.stack
do
498 var item
= new IteratorElement(i
.node
)
500 if i
.right
then item
.right
= true
503 var item
= new IteratorElement(path
.leaf
)
511 return stack
.last
.node
514 redef fun is_ok
do return not stack
.is_empty
516 redef fun index
do return pos
519 if stack
.is_empty
then return
520 if pos
> target
.length-1
then
526 if lst
.node
isa Leaf then
527 pos
+= lst
.node
.length
536 if nod
isa Concat and nod
.left
!= null then
537 stack
.push
(new IteratorElement(nod
.left
.as(not null)))
542 if not lst
.right
then
545 if nod
isa Concat and nod
.right
!= null then
546 stack
.push
(new IteratorElement(nod
.right
.as(not null)))
555 # Iterates on the leaves (substrings) of the Rope
557 super IndexedIterator[Leaf]
559 private var nodes
: Postfix
561 init(tgt
: Rope, pos
: Int)
563 nodes
= tgt
.postfix
(pos
)
566 redef fun is_ok
do return nodes
.is_ok
571 return nodes
.item
.as(Leaf)
574 redef fun index
do return nodes
.index
580 if nodes
.is_ok
and nodes
.item
isa Leaf then break
585 # Uses the leaves and calculates a new substring on each iteration
586 class SubstringsIterator
587 super IndexedIterator[Text]
589 private var nodes
: IndexedIterator[Leaf]
591 # Current position in Rope
594 # Current substring, computed from the current Leaf and indexes
597 init(tgt
: Rope, pos
: Int)
599 nodes
= tgt
.leaves
(pos
)
601 if pos
< 0 or pos
>= tgt
.length
then return
605 # Compute the bounds of the current substring and makes the substring
606 private fun make_substring
608 substring
= nodes
.item
.str
610 var length
= substring
.length
611 if nodes
.index
< pos
then
612 min
= pos
- nodes
.index
614 substring
= substring
.substring
(min
, length
)
617 redef fun is_ok
do return nodes
.is_ok
625 redef fun index
do return pos
629 pos
+= substring
.length
631 if nodes
.is_ok
then make_substring
636 class RopeCharIterator
637 super IndexedIterator[Char]
639 var substrings
: IndexedIterator[Text]
645 var substr_iter
: IndexedIterator[Char]
647 init(tgt
: Rope, from
: Int)
649 substrings
= tgt
.substrings_from
(from
)
651 if not substrings
.is_ok
then
656 substr_iter
= substrings
.item
.chars
.iterator
659 redef fun item
do return substr_iter
.item
661 redef fun is_ok
do return pos
<= max
663 redef fun index
do return pos
668 if substr_iter
.is_ok
then
671 if not substr_iter
.is_ok
then
673 if substrings
.is_ok
then
674 substr_iter
= substrings
.item
.chars
.iterator
680 private class ReversePostfix
681 super IndexedIterator[RopeNode]
689 var stack
= new List[IteratorElement]
691 init from
(tgt
: Rope, pos
: Int)
695 if pos
< 0 or pos
>= tgt
.length
then return
696 var path
= tgt
.node_at
(pos
)
697 self.pos
-= path
.offset
698 for i
in path
.stack
do
699 var elemt
= new IteratorElement(i
.node
)
706 stack
.push
(new IteratorElement(path
.leaf
))
707 stack
.last
.done
= true
712 return stack
.last
.node
715 redef fun is_ok
do return not stack
.is_empty
717 redef fun index
do return pos
721 if stack
.is_empty
then return
732 if not lst
.right
then
733 var nod
= lst
.node
.as(Concat)
737 stack
.push
(new IteratorElement(rgt
))
743 var nod
= lst
.node
.as(Concat)
747 stack
.push
(new IteratorElement(lft
))
752 if lst
.node
isa Leaf then pos
-= lst
.node
.length
757 private class ReverseLeavesIterator
758 super IndexedIterator[Leaf]
760 var nodes
: ReversePostfix
762 init(tgt
: Rope, from
: Int)
764 nodes
= tgt
.reverse_postfix
(from
)
767 redef fun is_ok
do return nodes
.is_ok
771 return nodes
.item
.as(Leaf)
777 if nodes
.is_ok
then if nodes
.item
isa Leaf then break
781 redef fun index
do return nodes
.index
785 private class ReverseSubstringsIterator
786 super IndexedIterator[Text]
788 var leaves
: ReverseLeavesIterator
794 init(tgt
: Rope, from
: Int)
796 leaves
= tgt
.reverse_leaves
(from
)
798 if not leaves
.is_ok
then return
799 str
= leaves
.item
.str
805 if pos
>= (leaves
.index
+ str
.length
- 1) then return
806 str
= str
.substring
(0, (pos
- leaves
.index
+ 1))
809 redef fun is_ok
do return leaves
.is_ok
816 redef fun index
do return pos
821 if not leaves
.is_ok
then return
822 str
= leaves
.item
.str
827 private class ReverseRopeCharIterator
828 super IndexedIterator[Char]
830 var substrs
: IndexedIterator[Text]
834 var subiter
: IndexedIterator[Char]
836 init(tgt
: Rope, from
: Int)
838 substrs
= tgt
.reverse_substrings_from
(from
)
839 if not substrs
.is_ok
then
843 subiter
= substrs
.item
.chars
.reverse_iterator
847 redef fun is_ok
do return pos
>= 0
854 redef fun index
do return pos
858 if subiter
.is_ok
then subiter
.next
859 if not subiter
.is_ok
then
860 if substrs
.is_ok
then substrs
.next
861 if substrs
.is_ok
then subiter
= substrs
.item
.chars
.reverse_iterator