1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Manipulation and presentation of ordered trees.
20 # Generic structure to manage and display an ordered tree
22 # Ordered tree are tree where the elements of a same parent are in a specific order
24 # Elements of the trees are added with the `add` method that takes a parent and
26 # If the parent is `null`, then the element is considered a root.
29 # var t = new OrderedTree[String]
31 # t.add("root", "child1")
32 # t.add("root", "child2")
33 # t.add("child1", "grand-child")
34 # assert t.length == 4
37 # By default, the elements with a same parent
38 # are visited in the order they are added.
41 # assert t.to_a == ["root", "child1", "grand-child", "child2"]
42 # assert t.write_to_string == """
50 # The `sort_with` method can be used reorder elements
53 # t.add("root", "aaa")
54 # assert t.to_a == ["root", "child1", "grand-child", "child2", "aaa"]
55 # t.sort_with(alpha_comparator)
56 # assert t.to_a == ["root", "aaa", "child1", "grand-child", "child2"]
59 # This class can be used as it to work with generic trees but can also be specialized to provide more specific
60 # behavior or display. It is why the internal attributes are mutable.
61 class OrderedTree[E
: Object]
65 # The roots of the tree (in sequence)
66 var roots
= new Array[E
]
68 # The branches of the trees.
69 # For each element, the ordered array of its direct sub-elements.
70 var sub
= new HashMap[E
, Array[E
]]
72 # Add a new element `e` in the tree.
73 # `p` is the parent of `e`.
74 # if `p` is null, then `e` is a root element.
75 fun add
(p
: nullable E
, e
: E
)
79 else if sub
.has_key
(p
) then
86 # Append all nodes `es` as children of `p`.
87 fun add_all
(p
: nullable E
, es
: Collection[E
])
89 for e
in es
do add
(p
, e
)
92 # print the full tree on `o`
93 # Write a ASCII-style tree and use the `display` method to label elements
94 redef fun write_to
(stream
: Writer)
97 stream
.write display
(r
)
99 sub_write_to
(stream
, r
, "")
103 private fun sub_write_to
(o
: Writer, e
: E
, prefix
: String)
105 if not sub
.has_key
(e
) then return
110 o
.write
"{prefix}|--{display(e2)}\n"
111 sub_write_to
(o
, e2
, prefix
+"| ")
113 o
.write
"{prefix}`--{display(e2)}\n"
114 sub_write_to
(o
, e2
, prefix
+" ")
119 # Sort roots and other elements using a comparator method
120 # This method basically sorts roots then each group of children
121 fun sort_with
(comparator
: Comparator)
123 comparator
.sort
(roots
)
124 for a
in sub
.values
do
129 # How to display a specific element of the tree
130 # By defaut, uses `to_s`
132 # Subclasses should redefine this method to provide a specific output
133 fun display
(e
: E
): String do return e
.to_s
135 # Get an array of the contained elements
138 # var tree = new OrderedTree[Int]
139 # tree.add_all(null, [1, 2])
140 # tree.add_all(1, [11, 12])
141 # tree.add_all(11, [111, 112])
142 # tree.add_all(12, [121, 122])
143 # tree.add_all(2, [21, 22])
144 # assert tree.to_a == [1, 11, 111, 112, 12, 121, 122, 2, 21, 22]
145 redef fun to_a
: Array[E
] do
146 var res
= new Array[E
]
147 for r
in roots
do sub_to_a
(r
, res
)
151 private fun sub_to_a
(e
: E
, res
: Array[E
]) do
153 if sub
.has_key
(e
) then for e2
in sub
[e
] do sub_to_a
(e2
, res
)
156 # var tree = new OrderedTree[Int]
157 # assert tree.is_empty
159 # assert not tree.is_empty
160 redef fun is_empty
: Bool do return roots
.is_empty
162 # var tree = new OrderedTree[Int]
165 # assert tree.first == 1
166 redef fun first
do return roots
.first
168 # var tree = new OrderedTree[Int]
169 # tree.add_all(null, [1, 2])
170 # tree.add_all(1, [11, 12])
171 # tree.add_all(11, [111, 112])
172 # tree.add_all(12, [121, 122])
173 # tree.add_all(2, [21, 22])
174 # var order = [1, 11, 111, 112, 12, 121, 122, 2, 21, 22]
175 # assert tree.iterator.to_a == order
176 redef fun iterator
do return new OrderedTreeIterator[E
](self)
179 # An Iterator over an OrderedTree
180 private class OrderedTreeIterator[E
: Object]
183 var tree
: OrderedTree[E
]
185 var iterators
= new Array[Iterator[E
]]
188 if not tree
.is_empty
then
189 iterators
.add tree
.roots
.iterator
193 redef fun is_ok
do return not iterators
.is_empty
197 return iterators
.last
.item
202 if tree
.sub
.has_key
(item
) then
203 iterators
.add tree
.sub
[item
].iterator
206 while is_ok
and not iterators
.last
.is_ok
do
208 if is_ok
and iterators
.last
.is_ok
then
215 redef fun iterator
do return new OrderedTreeIterator[E
](tree
)