45c1cbd0f8d084cc133e92f27b0beebfe53b0004
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 redef fun substrings
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
)
129 # Iterator on the nodes of the rope, in backwards postfix order
130 private fun reverse_postfix
(from
: Int): ReversePostfix do return new ReversePostfix.from
(self, from
)
132 # Iterator on the leaves of the rope, backwards order
133 private fun reverse_leaves
(from
: Int): ReverseLeavesIterator do return new ReverseLeavesIterator(self,from
)
135 # Iterator on the substrings, in reverse order
136 fun reverse_substrings
: IndexedIterator[Text] do return new ReverseSubstringsIterator(self, length-1
)
138 # Iterator on the substrings, in reverse order, starting iteration at position `from`
139 fun reverse_substrings_from
(from
: Int): IndexedIterator[Text] do return new ReverseSubstringsIterator(self, from
)
143 for i
in substrings
do
150 if str_representation
!= null then return str_representation
.as(not null)
152 var native_final_str
= calloc_string
(length
+ 1)
154 native_final_str
[length
] = '\0'
156 if self.length
== 0 then
157 str_representation
= native_final_str
158 return native_final_str
163 for i
in substrings
do
165 if str
isa FlatString then str
.items
.copy_to
(native_final_str
, str
.length
, str
.index_from
, offset
)
169 str_representation
= native_final_str
171 return native_final_str
174 # Path to the Leaf for `position`
175 private fun node_at
(position
: Int): Path
177 assert position
>= 0 and position
< length
178 return get_node_from
(root
.as(not null), 0, position
, new List[PathElement])
181 # Builds the path to Leaf at position `seek_pos`
182 private fun get_node_from
(node
: RopeNode, curr_pos
: Int, seek_pos
: Int, stack
: List[PathElement]): Path
185 if node
isa Leaf then return new Path(node
, seek_pos
- curr_pos
, stack
)
186 node
= node
.as(Concat)
188 if node
.left
!= null then
189 var next_pos
= curr_pos
+ node
.left
.length
190 stack
.add
(new PathElement(node
))
191 if next_pos
> seek_pos
then
192 stack
.last
.left
= true
193 return get_node_from
(node
.left
.as(not null), curr_pos
, seek_pos
, stack
)
195 stack
.last
.right
= true
196 return get_node_from
(node
.right
.as(not null), next_pos
, seek_pos
, stack
)
198 var vis
= new PathElement(node
)
201 return get_node_from
(node
.right
.as(not null), curr_pos
, seek_pos
, stack
)
207 if not o
isa Text then return false
208 if o
.length
!= self.length
then return false
209 var oit
= o
.chars
.iterator
210 for i
in self.chars
.iterator
do
211 if i
!= oit
.item
then return false
218 # Rope that cannot be modified
223 redef fun to_s
do return self
225 redef fun empty
do return once
new RopeString.from
("")
227 redef var chars
: SequenceRead[Char] = new RopeStringChars(self)
231 var ret
= empty
.as(RopeString)
232 for i
in substrings
do
233 ret
= ret
.prepend
(i
.reversed
.to_s
).as(RopeString)
241 for i
in substrings
do
250 for i
in substrings
do
256 redef fun +(o
) do return insert_at
(o
.to_s
, length
)
260 var ret
= new RopeString.from
("")
262 ret
= (ret
+ self).as(RopeString)
267 # Inserts a String `str` at position `pos`
268 redef fun insert_at
(str
, pos
)
270 if str
.length
== 0 then return self
271 if self.length
== 0 then return new RopeString.from
(str
)
273 assert pos
>= 0 and pos
<= length
275 if pos
== length
then return append
(str
).as(RopeString)
277 var path
= node_at
(pos
)
279 var last_concat
= new Concat
281 if path
.offset
== 0 then
282 last_concat
.right
= path
.leaf
283 if str
isa FlatString then last_concat
.left
= new Leaf(str
) else last_concat
.left
= str
.as(RopeString).root
284 else if path
.offset
== path
.leaf
.length
then
285 if str
isa FlatString then last_concat
.right
= new Leaf(str
) else last_concat
.right
= str
.as(RopeString).root
286 last_concat
.left
= path
.leaf
288 var s
= path
.leaf
.str
289 var l_half
= s
.substring
(0, s
.length
- path
.offset
)
290 var r_half
= s
.substring_from
(s
.length
- path
.offset
)
292 cct
.right
= new Leaf(r_half
.as(FlatString))
293 last_concat
.left
= new Leaf(l_half
.as(FlatString))
294 if str
isa FlatString then last_concat
.right
= new Leaf(str
) else last_concat
.right
= str
.as(RopeString).root
295 cct
.left
= last_concat
299 for i
in path
.stack
.reverse_iterator
do
302 nod
.right
= i
.node
.right
.as(not null)
303 nod
.left
= last_concat
305 nod
.left
= i
.node
.left
.as(not null)
306 nod
.right
= last_concat
311 return new RopeString.from_root
(last_concat
)
314 # Adds `s` at the beginning of self
315 redef fun prepend
(s
) do return insert_at
(s
, 0)
317 # Adds `s` at the end of self
320 if self.is_empty
then return s
321 return new RopeString.from_root
(append_to_path
(root
,s
))
324 # Builds a new path from root to the rightmost node with s appended
325 private fun append_to_path
(node
: RopeNode, s
: String): RopeNode
328 if node
isa Leaf then
330 if s
isa FlatString then cct
.right
= new Leaf(s
) else cct
.right
= s
.as(RopeString).root
331 else if node
isa Concat then
332 var right
= node
.right
333 if node
.left
!= null then cct
.left
= node
.left
.as(not null)
334 if right
== null then
335 if s
isa FlatString then cct
.right
= new Leaf(s
) else cct
.right
= s
.as(RopeString).root
337 cct
.right
= append_to_path
(right
, s
)
345 # var rope = new RopeString.from("abcd")
346 # assert rope.substring(1, 2) == "bc"
347 # assert rope.substring(-1, 2) == "a"
348 # assert rope.substring(1, 0) == ""
349 # assert rope.substring(2, 5) == "cd"
351 redef fun substring
(pos
, len
)
358 if pos
+ len
> length
then len
= length
- pos
360 if len
<= 0 then return new RopeString.from
("")
362 var path
= node_at
(pos
)
365 var offset
= path
.offset
367 if path
.leaf
.str
.length
- offset
> len
then lf
= new Leaf(lf
.str
.substring
(offset
,len
).as(FlatString)) else lf
= new Leaf(lf
.str
.substring_from
(offset
).as(FlatString))
369 var nod
: RopeNode = lf
371 for i
in path
.stack
.reverse_iterator
do
372 if i
.right
then continue
376 if r
!= null then tmp
.right
= r
380 var ret
= new RopeString
383 path
= ret
.node_at
(len-1
)
386 nod
= new Leaf(path
.leaf
.str
.substring
(0, offset
+1).as(FlatString))
388 for i
in path
.stack
.reverse_iterator
do
389 if i
.left
then continue
393 if l
!= null then tmp
.left
= l
403 redef class FlatString
405 redef fun append
(s
) do return (new RopeString.from
(self)) + s
407 redef fun prepend
(s
) do return (new RopeString.from
(self)).prepend
(s
)
409 redef fun insert_at
(s
, pos
)
411 if pos
== 0 then return prepend
(s
)
412 if pos
== length
then return append
(s
)
414 var l
= substring
(0,pos
)
415 var r
= substring_from
(pos
)
416 var ret
: String = new RopeString.from
(l
)
423 private class RopeStringChars
424 super SequenceRead[Char]
430 assert pos
< tgt
.length
431 var path
= tgt
.node_at
(pos
)
432 return path
.leaf
.str
.chars
[path
.offset
]
435 redef fun iterator
do return iterator_from
(0)
437 redef fun iterator_from
(pos
) do return new RopeCharIterator(tgt
, pos
)
439 redef fun reverse_iterator
do return reverse_iterator_from
(tgt
.length-1
)
441 redef fun reverse_iterator_from
(pos
) do return new ReverseRopeCharIterator(tgt
, pos
)
444 # Used to iterate on a Rope
445 private class IteratorElement
456 # The node being visited
458 # If the node has a left child, was it visited ?
460 # If the node has a right child, was it visited ?
462 # Was the current node visited ?
466 # Simple Postfix iterator on the nodes of a Rope
467 private class Postfix
468 super IndexedIterator[RopeNode]
470 # Target Rope to iterate on
473 # Current position in Rope
477 var stack
= new List[IteratorElement]
479 init from
(tgt
: Rope, pos
: Int)
483 if pos
< 0 or pos
>= tgt
.length
then return
484 var path
= tgt
.node_at
(pos
)
485 self.pos
-= path
.offset
486 for i
in path
.stack
do
487 var item
= new IteratorElement(i
.node
)
489 if i
.right
then item
.right
= true
492 var item
= new IteratorElement(path
.leaf
)
500 return stack
.last
.node
503 redef fun is_ok
do return not stack
.is_empty
505 redef fun index
do return pos
508 if stack
.is_empty
then return
509 if pos
> target
.length-1
then
515 if lst
.node
isa Leaf then
516 pos
+= lst
.node
.length
525 if nod
isa Concat and nod
.left
!= null then
526 stack
.push
(new IteratorElement(nod
.left
.as(not null)))
531 if not lst
.right
then
534 if nod
isa Concat and nod
.right
!= null then
535 stack
.push
(new IteratorElement(nod
.right
.as(not null)))
544 # Iterates on the leaves (substrings) of the Rope
546 super IndexedIterator[Leaf]
548 private var nodes
: Postfix
550 init(tgt
: Rope, pos
: Int)
552 nodes
= tgt
.postfix
(pos
)
555 redef fun is_ok
do return nodes
.is_ok
560 return nodes
.item
.as(Leaf)
563 redef fun index
do return nodes
.index
569 if nodes
.is_ok
and nodes
.item
isa Leaf then break
574 # Uses the leaves and calculates a new substring on each iteration
575 class SubstringsIterator
576 super IndexedIterator[Text]
578 private var nodes
: IndexedIterator[Leaf]
580 # Current position in Rope
583 # Current substring, computed from the current Leaf and indexes
586 init(tgt
: Rope, pos
: Int)
588 nodes
= tgt
.leaves
(pos
)
590 if pos
< 0 or pos
>= tgt
.length
then return
594 # Compute the bounds of the current substring and makes the substring
595 private fun make_substring
597 substring
= nodes
.item
.str
599 var length
= substring
.length
600 if nodes
.index
< pos
then
601 min
= pos
- nodes
.index
603 substring
= substring
.substring
(min
, length
)
606 redef fun is_ok
do return nodes
.is_ok
614 redef fun index
do return pos
618 pos
+= substring
.length
620 if nodes
.is_ok
then make_substring
625 class RopeCharIterator
626 super IndexedIterator[Char]
628 var substrings
: IndexedIterator[Text]
634 var substr_iter
: IndexedIterator[Char]
636 init(tgt
: Rope, from
: Int)
638 substrings
= tgt
.substrings_from
(from
)
640 if not substrings
.is_ok
then
645 substr_iter
= substrings
.item
.chars
.iterator
648 redef fun item
do return substr_iter
.item
650 redef fun is_ok
do return pos
<= max
652 redef fun index
do return pos
657 if substr_iter
.is_ok
then
660 if not substr_iter
.is_ok
then
662 if substrings
.is_ok
then
663 substr_iter
= substrings
.item
.chars
.iterator
669 private class ReversePostfix
670 super IndexedIterator[RopeNode]
678 var stack
= new List[IteratorElement]
680 init from
(tgt
: Rope, pos
: Int)
684 if pos
< 0 or pos
>= tgt
.length
then return
685 var path
= tgt
.node_at
(pos
)
686 self.pos
-= path
.offset
687 for i
in path
.stack
do
688 var elemt
= new IteratorElement(i
.node
)
695 stack
.push
(new IteratorElement(path
.leaf
))
696 stack
.last
.done
= true
701 return stack
.last
.node
704 redef fun is_ok
do return not stack
.is_empty
706 redef fun index
do return pos
710 if stack
.is_empty
then return
721 if not lst
.right
then
722 var nod
= lst
.node
.as(Concat)
726 stack
.push
(new IteratorElement(rgt
))
732 var nod
= lst
.node
.as(Concat)
736 stack
.push
(new IteratorElement(lft
))
741 if lst
.node
isa Leaf then pos
-= lst
.node
.length
746 private class ReverseLeavesIterator
747 super IndexedIterator[Leaf]
749 var nodes
: ReversePostfix
751 init(tgt
: Rope, from
: Int)
753 nodes
= tgt
.reverse_postfix
(from
)
756 redef fun is_ok
do return nodes
.is_ok
760 return nodes
.item
.as(Leaf)
766 if nodes
.is_ok
then if nodes
.item
isa Leaf then break
770 redef fun index
do return nodes
.index
774 private class ReverseSubstringsIterator
775 super IndexedIterator[Text]
777 var leaves
: ReverseLeavesIterator
783 init(tgt
: Rope, from
: Int)
785 leaves
= tgt
.reverse_leaves
(from
)
787 if not leaves
.is_ok
then return
788 str
= leaves
.item
.str
794 if pos
>= (leaves
.index
+ str
.length
- 1) then return
795 str
= str
.substring
(0, (pos
- leaves
.index
+ 1))
798 redef fun is_ok
do return leaves
.is_ok
805 redef fun index
do return pos
810 if not leaves
.is_ok
then return
811 str
= leaves
.item
.str
816 private class ReverseRopeCharIterator
817 super IndexedIterator[Char]
819 var substrs
: IndexedIterator[Text]
823 var subiter
: IndexedIterator[Char]
825 init(tgt
: Rope, from
: Int)
827 substrs
= tgt
.reverse_substrings_from
(from
)
828 if not substrs
.is_ok
then
832 subiter
= substrs
.item
.chars
.reverse_iterator
836 redef fun is_ok
do return pos
>= 0
843 redef fun index
do return pos
847 if subiter
.is_ok
then subiter
.next
848 if not subiter
.is_ok
then
849 if substrs
.is_ok
then substrs
.next
850 if substrs
.is_ok
then subiter
= substrs
.item
.chars
.reverse_iterator