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 return get_node_from
(root
.as(not null), 0, position
, new List[PathElement])
193 # Builds the path to Leaf at position `seek_pos`
194 private fun get_node_from
(node
: RopeNode, curr_pos
: Int, seek_pos
: Int, stack
: List[PathElement]): Path
197 if node
isa Leaf then return new Path(node
, seek_pos
- curr_pos
, stack
)
198 node
= node
.as(Concat)
200 if node
.left
!= null then
201 var next_pos
= curr_pos
+ node
.left
.length
202 stack
.add
(new PathElement(node
))
203 if next_pos
> seek_pos
then
204 stack
.last
.left
= true
205 return get_node_from
(node
.left
.as(not null), curr_pos
, seek_pos
, stack
)
207 stack
.last
.right
= true
208 return get_node_from
(node
.right
.as(not null), next_pos
, seek_pos
, stack
)
210 var vis
= new PathElement(node
)
213 return get_node_from
(node
.right
.as(not null), curr_pos
, seek_pos
, stack
)
219 if not o
isa Text then return false
220 if o
.length
!= self.length
then return false
221 var oit
= o
.chars
.iterator
222 for i
in self.chars
.iterator
do
223 if i
!= oit
.item
then return false
230 # Rope that cannot be modified
235 redef fun to_s
do return self
237 redef fun empty
do return once
new RopeString.from
("")
239 redef var chars
: SequenceRead[Char] = new RopeStringChars(self)
243 var ret
= empty
.as(RopeString)
244 for i
in substrings
do
245 ret
= ret
.prepend
(i
.reversed
.to_s
).as(RopeString)
253 for i
in substrings
do
262 for i
in substrings
do
268 redef fun +(o
) do return insert_at
(o
.to_s
, length
)
272 var ret
= new RopeString.from
("")
274 ret
= (ret
+ self).as(RopeString)
279 # Inserts a String `str` at position `pos`
280 redef fun insert_at
(str
, pos
)
282 if str
.length
== 0 then return self
283 if self.length
== 0 then return new RopeString.from
(str
)
285 assert pos
>= 0 and pos
<= length
287 if pos
== length
then return append
(str
).as(RopeString)
289 var path
= node_at
(pos
)
291 var last_concat
: Concat
293 if path
.offset
== 0 then
294 if str
isa FlatString then
295 last_concat
= new Concat(new StringLeaf(str
), path
.leaf
)
297 last_concat
= new Concat(str
.as(RopeString).root
, path
.leaf
)
299 else if path
.offset
== path
.leaf
.length
then
300 if str
isa FlatString then
301 last_concat
= new Concat(path
.leaf
, new StringLeaf(str
))
303 last_concat
= new Concat(path
.leaf
, str
.as(RopeString).root
)
306 var s
= path
.leaf
.str
307 var l_half
= s
.substring
(0, s
.length
- path
.offset
)
308 var r_half
= s
.substring_from
(s
.length
- path
.offset
)
310 var ll
= new StringLeaf(l_half
.as(FlatString))
311 if str
isa FlatString then
312 cct
= new Concat(ll
, new StringLeaf(str
))
314 cct
= new Concat(ll
, str
.as(RopeString).root
)
316 last_concat
= new Concat(cct
, new StringLeaf(r_half
.as(FlatString)))
319 for i
in path
.stack
.reverse_iterator
do
321 last_concat
= new Concat(last_concat
, i
.node
.right
)
323 last_concat
= new Concat(i
.node
.left
, last_concat
)
327 return new RopeString.from_root
(last_concat
)
330 # Adds `s` at the beginning of self
331 redef fun prepend
(s
) do return insert_at
(s
, 0)
333 # Adds `s` at the end of self
336 if self.is_empty
then return s
337 return new RopeString.from_root
(append_to_path
(root
,s
))
340 # Builds a new path from root to the rightmost node with s appended
341 private fun append_to_path
(node
: RopeNode, s
: String): RopeNode
344 if node
isa Leaf then
345 if s
isa FlatString then
346 cct
= new Concat(node
, new StringLeaf(s
))
348 cct
= new Concat(node
, s
.as(RopeString).root
)
351 var n
= node
.as(Concat)
353 var lft
= n
.left
.as(not null)
354 if right
== null then
355 if s
isa FlatString then
356 cct
= new Concat(lft
, new StringLeaf(s
))
358 cct
= new Concat(lft
, s
.as(RopeString).root
)
361 cct
= new Concat(lft
, append_to_path
(right
, s
))
369 # var rope = new RopeString.from("abcd")
370 # assert rope.substring(1, 2) == "bc"
371 # assert rope.substring(-1, 2) == "a"
372 # assert rope.substring(1, 0) == ""
373 # assert rope.substring(2, 5) == "cd"
375 redef fun substring
(pos
, len
)
382 if pos
+ len
> length
then len
= length
- pos
384 if len
<= 0 then return new RopeString.from
("")
386 var path
= node_at
(pos
)
389 var offset
= path
.offset
391 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))
393 var nod
: RopeNode = lf
395 for i
in path
.stack
.reverse_iterator
do
396 if i
.right
then continue
397 nod
= new Concat(nod
, i
.node
.right
)
400 var ret
= new RopeString
403 path
= ret
.node_at
(len-1
)
406 nod
= new StringLeaf(path
.leaf
.str
.substring
(0, offset
+1).as(FlatString))
408 for i
in path
.stack
.reverse_iterator
do
409 if i
.left
then continue
410 nod
= new Concat(i
.node
.left
, nod
)
419 redef class FlatString
421 redef fun append
(s
) do return (new RopeString.from
(self)) + s
423 redef fun prepend
(s
) do return (new RopeString.from
(self)).prepend
(s
)
425 redef fun insert_at
(s
, pos
)
427 if pos
== 0 then return prepend
(s
)
428 if pos
== length
then return append
(s
)
430 var l
= substring
(0,pos
)
431 var r
= substring_from
(pos
)
432 var ret
: String = new RopeString.from
(l
)
439 private class RopeStringChars
440 super SequenceRead[Char]
446 assert pos
< tgt
.length
447 var path
= tgt
.node_at
(pos
)
448 return path
.leaf
.str
.chars
[path
.offset
]
451 redef fun iterator
do return iterator_from
(0)
453 redef fun iterator_from
(pos
) do return new RopeCharIterator(tgt
, pos
)
455 redef fun reverse_iterator
do return reverse_iterator_from
(tgt
.length-1
)
457 redef fun reverse_iterator_from
(pos
) do return new ReverseRopeCharIterator(tgt
, pos
)
460 # Used to iterate on a Rope
461 private class IteratorElement
472 # The node being visited
474 # If the node has a left child, was it visited ?
476 # If the node has a right child, was it visited ?
478 # Was the current node visited ?
482 # Simple Postfix iterator on the nodes of a Rope
483 private class Postfix
484 super IndexedIterator[RopeNode]
486 # Target Rope to iterate on
489 # Current position in Rope
493 var stack
= new List[IteratorElement]
495 init from
(tgt
: Rope, pos
: Int)
499 if pos
< 0 or pos
>= tgt
.length
then return
500 var path
= tgt
.node_at
(pos
)
501 self.pos
-= path
.offset
502 for i
in path
.stack
do
503 var item
= new IteratorElement(i
.node
)
505 if i
.right
then item
.right
= true
508 var item
= new IteratorElement(path
.leaf
)
516 return stack
.last
.node
519 redef fun is_ok
do return not stack
.is_empty
521 redef fun index
do return pos
524 if stack
.is_empty
then return
525 if pos
> target
.length-1
then
531 if lst
.node
isa Leaf then
532 pos
+= lst
.node
.length
541 if nod
isa Concat and nod
.left
!= null then
542 stack
.push
(new IteratorElement(nod
.left
.as(not null)))
547 if not lst
.right
then
550 if nod
isa Concat and nod
.right
!= null then
551 stack
.push
(new IteratorElement(nod
.right
.as(not null)))
560 # Iterates on the leaves (substrings) of the Rope
562 super IndexedIterator[Leaf]
564 private var nodes
: Postfix
566 init(tgt
: Rope, pos
: Int)
568 nodes
= tgt
.postfix
(pos
)
571 redef fun is_ok
do return nodes
.is_ok
576 return nodes
.item
.as(Leaf)
579 redef fun index
do return nodes
.index
585 if nodes
.is_ok
and nodes
.item
isa Leaf then break
590 # Uses the leaves and calculates a new substring on each iteration
591 class SubstringsIterator
592 super IndexedIterator[Text]
594 private var nodes
: IndexedIterator[Leaf]
596 # Current position in Rope
599 # Current substring, computed from the current Leaf and indexes
602 init(tgt
: Rope, pos
: Int)
604 nodes
= tgt
.leaves
(pos
)
606 if pos
< 0 or pos
>= tgt
.length
then return
610 # Compute the bounds of the current substring and makes the substring
611 private fun make_substring
613 substring
= nodes
.item
.str
615 var length
= substring
.length
616 if nodes
.index
< pos
then
617 min
= pos
- nodes
.index
619 substring
= substring
.substring
(min
, length
)
622 redef fun is_ok
do return nodes
.is_ok
630 redef fun index
do return pos
634 pos
+= substring
.length
636 if nodes
.is_ok
then make_substring
641 class RopeCharIterator
642 super IndexedIterator[Char]
644 var substrings
: IndexedIterator[Text]
650 var substr_iter
: IndexedIterator[Char]
652 init(tgt
: Rope, from
: Int)
654 substrings
= tgt
.substrings_from
(from
)
656 if not substrings
.is_ok
then
661 substr_iter
= substrings
.item
.chars
.iterator
664 redef fun item
do return substr_iter
.item
666 redef fun is_ok
do return pos
<= max
668 redef fun index
do return pos
673 if substr_iter
.is_ok
then
676 if not substr_iter
.is_ok
then
678 if substrings
.is_ok
then
679 substr_iter
= substrings
.item
.chars
.iterator
685 private class ReversePostfix
686 super IndexedIterator[RopeNode]
694 var stack
= new List[IteratorElement]
696 init from
(tgt
: Rope, pos
: Int)
700 if pos
< 0 or pos
>= tgt
.length
then return
701 var path
= tgt
.node_at
(pos
)
702 self.pos
-= path
.offset
703 for i
in path
.stack
do
704 var elemt
= new IteratorElement(i
.node
)
711 stack
.push
(new IteratorElement(path
.leaf
))
712 stack
.last
.done
= true
717 return stack
.last
.node
720 redef fun is_ok
do return not stack
.is_empty
722 redef fun index
do return pos
726 if stack
.is_empty
then return
737 if not lst
.right
then
738 var nod
= lst
.node
.as(Concat)
742 stack
.push
(new IteratorElement(rgt
))
748 var nod
= lst
.node
.as(Concat)
752 stack
.push
(new IteratorElement(lft
))
757 if lst
.node
isa Leaf then pos
-= lst
.node
.length
762 private class ReverseLeavesIterator
763 super IndexedIterator[Leaf]
765 var nodes
: ReversePostfix
767 init(tgt
: Rope, from
: Int)
769 nodes
= tgt
.reverse_postfix
(from
)
772 redef fun is_ok
do return nodes
.is_ok
776 return nodes
.item
.as(Leaf)
782 if nodes
.is_ok
then if nodes
.item
isa Leaf then break
786 redef fun index
do return nodes
.index
790 private class ReverseSubstringsIterator
791 super IndexedIterator[Text]
793 var leaves
: ReverseLeavesIterator
799 init(tgt
: Rope, from
: Int)
801 leaves
= tgt
.reverse_leaves
(from
)
803 if not leaves
.is_ok
then return
804 str
= leaves
.item
.str
810 if pos
>= (leaves
.index
+ str
.length
- 1) then return
811 str
= str
.substring
(0, (pos
- leaves
.index
+ 1))
814 redef fun is_ok
do return leaves
.is_ok
821 redef fun index
do return pos
826 if not leaves
.is_ok
then return
827 str
= leaves
.item
.str
832 private class ReverseRopeCharIterator
833 super IndexedIterator[Char]
835 var substrs
: IndexedIterator[Text]
839 var subiter
: IndexedIterator[Char]
841 init(tgt
: Rope, from
: Int)
843 substrs
= tgt
.reverse_substrings_from
(from
)
844 if not substrs
.is_ok
then
848 subiter
= substrs
.item
.chars
.reverse_iterator
852 redef fun is_ok
do return pos
>= 0
859 redef fun index
do return pos
863 if subiter
.is_ok
then subiter
.next
864 if not subiter
.is_ok
then
865 if substrs
.is_ok
then substrs
.next
866 if substrs
.is_ok
then subiter
= substrs
.item
.chars
.reverse_iterator