54e60502b10e569a28cc9f3b8364657c166dd599
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
77 private abstract class Leaf
80 # Encapsulated FlatString in the leaf node
85 private class StringLeaf
88 init(val
: FlatString) do
95 # Basic structure, binary tree with a root node.
97 # Also shared services by subsequent implementations.
101 # Root node, entry point of a Rope.
102 private var root
: RopeNode
104 # Cached version of self as a flat String
105 private var str_representation
: nullable NativeString = null
110 # Creates a new Rope with `s` as root
111 init from
(s
: String) do
112 if s
isa RopeString then root
= s
.root
else root
= new StringLeaf(s
.as(FlatString))
115 private init from_root
(r
: RopeNode)
120 redef fun length
do return root
.length
122 # Iterator on the nodes of the rope, in forward postfix order
123 private fun postfix
(from
: Int): Postfix do return new Postfix.from
(self, from
)
125 # Iterator on the leaves of the rope, forward order
126 private fun leaves
(from
: Int): LeavesIterator do return new LeavesIterator(self, from
)
128 # Iterator on the substrings from 0, in forward order
129 redef fun substrings
do return new SubstringsIterator(self, 0)
131 # Iterator on the substrings, starting at position `from`, in forward order
132 fun substrings_from
(from
: Int): IndexedIterator[Text] do return new SubstringsIterator(self, from
)
134 # Iterator on the nodes of the rope, in backwards postfix order
135 private fun reverse_postfix
(from
: Int): ReversePostfix do return new ReversePostfix.from
(self, from
)
137 # Iterator on the leaves of the rope, backwards order
138 private fun reverse_leaves
(from
: Int): ReverseLeavesIterator do return new ReverseLeavesIterator(self,from
)
140 # Iterator on the substrings, in reverse order
141 fun reverse_substrings
: IndexedIterator[Text] do return new ReverseSubstringsIterator(self, length-1
)
143 # Iterator on the substrings, in reverse order, starting iteration at position `from`
144 fun reverse_substrings_from
(from
: Int): IndexedIterator[Text] do return new ReverseSubstringsIterator(self, from
)
148 for i
in substrings
do
155 if str_representation
!= null then return str_representation
.as(not null)
157 var native_final_str
= calloc_string
(length
+ 1)
159 native_final_str
[length
] = '\0'
161 if self.length
== 0 then
162 str_representation
= native_final_str
163 return native_final_str
168 for i
in substrings
do
170 if str
isa FlatString then str
.items
.copy_to
(native_final_str
, str
.length
, str
.index_from
, offset
)
174 str_representation
= native_final_str
176 return native_final_str
179 # Path to the Leaf for `position`
180 private fun node_at
(position
: Int): Path
182 assert position
>= 0 and position
< length
183 return get_node_from
(root
.as(not null), 0, position
, new List[PathElement])
186 # Builds the path to Leaf at position `seek_pos`
187 private fun get_node_from
(node
: RopeNode, curr_pos
: Int, seek_pos
: Int, stack
: List[PathElement]): Path
190 if node
isa Leaf then return new Path(node
, seek_pos
- curr_pos
, stack
)
191 node
= node
.as(Concat)
193 if node
.left
!= null then
194 var next_pos
= curr_pos
+ node
.left
.length
195 stack
.add
(new PathElement(node
))
196 if next_pos
> seek_pos
then
197 stack
.last
.left
= true
198 return get_node_from
(node
.left
.as(not null), curr_pos
, seek_pos
, stack
)
200 stack
.last
.right
= true
201 return get_node_from
(node
.right
.as(not null), next_pos
, seek_pos
, stack
)
203 var vis
= new PathElement(node
)
206 return get_node_from
(node
.right
.as(not null), curr_pos
, seek_pos
, stack
)
212 if not o
isa Text then return false
213 if o
.length
!= self.length
then return false
214 var oit
= o
.chars
.iterator
215 for i
in self.chars
.iterator
do
216 if i
!= oit
.item
then return false
223 # Rope that cannot be modified
228 redef fun to_s
do return self
230 redef fun empty
do return once
new RopeString.from
("")
232 redef var chars
: SequenceRead[Char] = new RopeStringChars(self)
236 var ret
= empty
.as(RopeString)
237 for i
in substrings
do
238 ret
= ret
.prepend
(i
.reversed
.to_s
).as(RopeString)
246 for i
in substrings
do
255 for i
in substrings
do
261 redef fun +(o
) do return insert_at
(o
.to_s
, length
)
265 var ret
= new RopeString.from
("")
267 ret
= (ret
+ self).as(RopeString)
272 # Inserts a String `str` at position `pos`
273 redef fun insert_at
(str
, pos
)
275 if str
.length
== 0 then return self
276 if self.length
== 0 then return new RopeString.from
(str
)
278 assert pos
>= 0 and pos
<= length
280 if pos
== length
then return append
(str
).as(RopeString)
282 var path
= node_at
(pos
)
284 var last_concat
= new Concat
286 if path
.offset
== 0 then
287 last_concat
.right
= path
.leaf
288 if str
isa FlatString then last_concat
.left
= new StringLeaf(str
) else last_concat
.left
= str
.as(RopeString).root
289 else if path
.offset
== path
.leaf
.length
then
290 if str
isa FlatString then last_concat
.right
= new StringLeaf(str
) else last_concat
.right
= str
.as(RopeString).root
291 last_concat
.left
= path
.leaf
293 var s
= path
.leaf
.str
294 var l_half
= s
.substring
(0, s
.length
- path
.offset
)
295 var r_half
= s
.substring_from
(s
.length
- path
.offset
)
297 cct
.right
= new StringLeaf(r_half
.as(FlatString))
298 last_concat
.left
= new StringLeaf(l_half
.as(FlatString))
299 if str
isa FlatString then last_concat
.right
= new StringLeaf(str
) else last_concat
.right
= str
.as(RopeString).root
300 cct
.left
= last_concat
304 for i
in path
.stack
.reverse_iterator
do
307 nod
.right
= i
.node
.right
.as(not null)
308 nod
.left
= last_concat
310 nod
.left
= i
.node
.left
.as(not null)
311 nod
.right
= last_concat
316 return new RopeString.from_root
(last_concat
)
319 # Adds `s` at the beginning of self
320 redef fun prepend
(s
) do return insert_at
(s
, 0)
322 # Adds `s` at the end of self
325 if self.is_empty
then return s
326 return new RopeString.from_root
(append_to_path
(root
,s
))
329 # Builds a new path from root to the rightmost node with s appended
330 private fun append_to_path
(node
: RopeNode, s
: String): RopeNode
333 if node
isa Leaf then
335 if s
isa FlatString then cct
.right
= new StringLeaf(s
) else cct
.right
= s
.as(RopeString).root
336 else if node
isa Concat then
337 var right
= node
.right
338 if node
.left
!= null then cct
.left
= node
.left
.as(not null)
339 if right
== null then
340 if s
isa FlatString then cct
.right
= new StringLeaf(s
) else cct
.right
= s
.as(RopeString).root
342 cct
.right
= append_to_path
(right
, s
)
350 # var rope = new RopeString.from("abcd")
351 # assert rope.substring(1, 2) == "bc"
352 # assert rope.substring(-1, 2) == "a"
353 # assert rope.substring(1, 0) == ""
354 # assert rope.substring(2, 5) == "cd"
356 redef fun substring
(pos
, len
)
363 if pos
+ len
> length
then len
= length
- pos
365 if len
<= 0 then return new RopeString.from
("")
367 var path
= node_at
(pos
)
370 var offset
= path
.offset
372 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))
374 var nod
: RopeNode = lf
376 for i
in path
.stack
.reverse_iterator
do
377 if i
.right
then continue
381 if r
!= null then tmp
.right
= r
385 var ret
= new RopeString
388 path
= ret
.node_at
(len-1
)
391 nod
= new StringLeaf(path
.leaf
.str
.substring
(0, offset
+1).as(FlatString))
393 for i
in path
.stack
.reverse_iterator
do
394 if i
.left
then continue
398 if l
!= null then tmp
.left
= l
408 redef class FlatString
410 redef fun append
(s
) do return (new RopeString.from
(self)) + s
412 redef fun prepend
(s
) do return (new RopeString.from
(self)).prepend
(s
)
414 redef fun insert_at
(s
, pos
)
416 if pos
== 0 then return prepend
(s
)
417 if pos
== length
then return append
(s
)
419 var l
= substring
(0,pos
)
420 var r
= substring_from
(pos
)
421 var ret
: String = new RopeString.from
(l
)
428 private class RopeStringChars
429 super SequenceRead[Char]
435 assert pos
< tgt
.length
436 var path
= tgt
.node_at
(pos
)
437 return path
.leaf
.str
.chars
[path
.offset
]
440 redef fun iterator
do return iterator_from
(0)
442 redef fun iterator_from
(pos
) do return new RopeCharIterator(tgt
, pos
)
444 redef fun reverse_iterator
do return reverse_iterator_from
(tgt
.length-1
)
446 redef fun reverse_iterator_from
(pos
) do return new ReverseRopeCharIterator(tgt
, pos
)
449 # Used to iterate on a Rope
450 private class IteratorElement
461 # The node being visited
463 # If the node has a left child, was it visited ?
465 # If the node has a right child, was it visited ?
467 # Was the current node visited ?
471 # Simple Postfix iterator on the nodes of a Rope
472 private class Postfix
473 super IndexedIterator[RopeNode]
475 # Target Rope to iterate on
478 # Current position in Rope
482 var stack
= new List[IteratorElement]
484 init from
(tgt
: Rope, pos
: Int)
488 if pos
< 0 or pos
>= tgt
.length
then return
489 var path
= tgt
.node_at
(pos
)
490 self.pos
-= path
.offset
491 for i
in path
.stack
do
492 var item
= new IteratorElement(i
.node
)
494 if i
.right
then item
.right
= true
497 var item
= new IteratorElement(path
.leaf
)
505 return stack
.last
.node
508 redef fun is_ok
do return not stack
.is_empty
510 redef fun index
do return pos
513 if stack
.is_empty
then return
514 if pos
> target
.length-1
then
520 if lst
.node
isa Leaf then
521 pos
+= lst
.node
.length
530 if nod
isa Concat and nod
.left
!= null then
531 stack
.push
(new IteratorElement(nod
.left
.as(not null)))
536 if not lst
.right
then
539 if nod
isa Concat and nod
.right
!= null then
540 stack
.push
(new IteratorElement(nod
.right
.as(not null)))
549 # Iterates on the leaves (substrings) of the Rope
551 super IndexedIterator[Leaf]
553 private var nodes
: Postfix
555 init(tgt
: Rope, pos
: Int)
557 nodes
= tgt
.postfix
(pos
)
560 redef fun is_ok
do return nodes
.is_ok
565 return nodes
.item
.as(Leaf)
568 redef fun index
do return nodes
.index
574 if nodes
.is_ok
and nodes
.item
isa Leaf then break
579 # Uses the leaves and calculates a new substring on each iteration
580 class SubstringsIterator
581 super IndexedIterator[Text]
583 private var nodes
: IndexedIterator[Leaf]
585 # Current position in Rope
588 # Current substring, computed from the current Leaf and indexes
591 init(tgt
: Rope, pos
: Int)
593 nodes
= tgt
.leaves
(pos
)
595 if pos
< 0 or pos
>= tgt
.length
then return
599 # Compute the bounds of the current substring and makes the substring
600 private fun make_substring
602 substring
= nodes
.item
.str
604 var length
= substring
.length
605 if nodes
.index
< pos
then
606 min
= pos
- nodes
.index
608 substring
= substring
.substring
(min
, length
)
611 redef fun is_ok
do return nodes
.is_ok
619 redef fun index
do return pos
623 pos
+= substring
.length
625 if nodes
.is_ok
then make_substring
630 class RopeCharIterator
631 super IndexedIterator[Char]
633 var substrings
: IndexedIterator[Text]
639 var substr_iter
: IndexedIterator[Char]
641 init(tgt
: Rope, from
: Int)
643 substrings
= tgt
.substrings_from
(from
)
645 if not substrings
.is_ok
then
650 substr_iter
= substrings
.item
.chars
.iterator
653 redef fun item
do return substr_iter
.item
655 redef fun is_ok
do return pos
<= max
657 redef fun index
do return pos
662 if substr_iter
.is_ok
then
665 if not substr_iter
.is_ok
then
667 if substrings
.is_ok
then
668 substr_iter
= substrings
.item
.chars
.iterator
674 private class ReversePostfix
675 super IndexedIterator[RopeNode]
683 var stack
= new List[IteratorElement]
685 init from
(tgt
: Rope, pos
: Int)
689 if pos
< 0 or pos
>= tgt
.length
then return
690 var path
= tgt
.node_at
(pos
)
691 self.pos
-= path
.offset
692 for i
in path
.stack
do
693 var elemt
= new IteratorElement(i
.node
)
700 stack
.push
(new IteratorElement(path
.leaf
))
701 stack
.last
.done
= true
706 return stack
.last
.node
709 redef fun is_ok
do return not stack
.is_empty
711 redef fun index
do return pos
715 if stack
.is_empty
then return
726 if not lst
.right
then
727 var nod
= lst
.node
.as(Concat)
731 stack
.push
(new IteratorElement(rgt
))
737 var nod
= lst
.node
.as(Concat)
741 stack
.push
(new IteratorElement(lft
))
746 if lst
.node
isa Leaf then pos
-= lst
.node
.length
751 private class ReverseLeavesIterator
752 super IndexedIterator[Leaf]
754 var nodes
: ReversePostfix
756 init(tgt
: Rope, from
: Int)
758 nodes
= tgt
.reverse_postfix
(from
)
761 redef fun is_ok
do return nodes
.is_ok
765 return nodes
.item
.as(Leaf)
771 if nodes
.is_ok
then if nodes
.item
isa Leaf then break
775 redef fun index
do return nodes
.index
779 private class ReverseSubstringsIterator
780 super IndexedIterator[Text]
782 var leaves
: ReverseLeavesIterator
788 init(tgt
: Rope, from
: Int)
790 leaves
= tgt
.reverse_leaves
(from
)
792 if not leaves
.is_ok
then return
793 str
= leaves
.item
.str
799 if pos
>= (leaves
.index
+ str
.length
- 1) then return
800 str
= str
.substring
(0, (pos
- leaves
.index
+ 1))
803 redef fun is_ok
do return leaves
.is_ok
810 redef fun index
do return pos
815 if not leaves
.is_ok
then return
816 str
= leaves
.item
.str
821 private class ReverseRopeCharIterator
822 super IndexedIterator[Char]
824 var substrs
: IndexedIterator[Text]
828 var subiter
: IndexedIterator[Char]
830 init(tgt
: Rope, from
: Int)
832 substrs
= tgt
.reverse_substrings_from
(from
)
833 if not substrs
.is_ok
then
837 subiter
= substrs
.item
.chars
.reverse_iterator
841 redef fun is_ok
do return pos
>= 0
848 redef fun index
do return pos
852 if subiter
.is_ok
then subiter
.next
853 if not subiter
.is_ok
then
854 if substrs
.is_ok
then substrs
.next
855 if substrs
.is_ok
then subiter
= substrs
.item
.chars
.reverse_iterator