@@ -10,12 +10,11 @@ tree Adjacency List that represents the given tree
10
10
Returns
11
11
positions co-ordinates of the layout
12
12
"""
13
-
14
13
module Buchheim
15
14
16
15
using GeometryTypes
17
16
18
- immutable Tree{A<: AbstractVector ,P<: AbstractVector ,F}
17
+ struct Tree{A<: AbstractVector ,P<: AbstractVector ,F}
19
18
nodes:: A
20
19
mod:: F
21
20
thread:: Vector{Int}
@@ -27,49 +26,50 @@ immutable Tree{A<:AbstractVector,P<:AbstractVector,F}
27
26
nodesize:: F
28
27
end
29
28
30
- function Tree {T} (tree:: AbstractVector{T} , nodesize)
31
- mod = zeros (length (tree))
32
- thread = zeros (Int,length (tree))
33
- prelim = zeros (length (tree))
34
- shift = zeros (length (tree))
35
- change = zeros (length (tree))
36
- ancestor = [i for i in 1 : length (tree)]
29
+ function Tree (tree:: AbstractVector , nodesize)
30
+ len = length (tree)
31
+ mod = zeros (len)
32
+ thread = zeros (Int, len)
33
+ prelim = zeros (len)
34
+ shift = zeros (len)
35
+ change = zeros (len)
36
+ ancestor = collect (1 : len)
37
37
nodes = copy (tree)
38
- positions = zeros (Point{2 ,Float64},length (tree) )
39
- t = Tree (nodes,mod,thread,ancestor,prelim,shift,change,positions,nodesize)
38
+ positions = zeros (Point{2 ,Float64}, len )
39
+ t = Tree (nodes, mod, thread, ancestor, prelim, shift, change, positions, nodesize)
40
40
return t
41
41
end
42
42
43
- function layout {T} (t:: AbstractVector{T} ; nodesize= ones (length (t)))
44
- layout! (t,nodesize)
43
+ function layout (t:: AbstractVector ; nodesize= ones (length (t)))
44
+ layout! (t, nodesize)
45
45
end
46
46
47
- function layout! {T} (t:: AbstractVector{T} , nodesize)
48
- tree = Tree (t,nodesize)
49
- first_walk (1 ,tree)
50
- second_walk (1 ,- tree. prelim[1 ],0.0 ,tree)
47
+ function layout! (t:: AbstractVector , nodesize)
48
+ tree = Tree (t, nodesize)
49
+ first_walk (1 , tree)
50
+ second_walk (1 , - tree. prelim[1 ], 0.0 , tree)
51
51
return tree. positions
52
52
end
53
53
54
- function parent {T} (v :: T , t:: Tree )
54
+ function parent (v, t:: Tree )
55
55
tree = t. nodes
56
56
for i in 1 : length (tree)
57
- y = find (x -> (x== v),tree[i])
58
- if length (y)!= 0
57
+ y = findall (x -> (x== v), tree[i])
58
+ if length (y) != 0
59
59
return i
60
60
end
61
61
end
62
62
return nothing
63
63
end
64
64
65
- function first_walk {T} (v :: T , t:: Tree )
65
+ function first_walk (v, t:: Tree )
66
66
prelim = t. prelim
67
67
mod = t. mod
68
68
tree = t. nodes
69
69
nodesize = t. nodesize
70
70
p = parent (v,t)
71
71
if p != nothing
72
- index = find (x -> (x== v),tree[p])[1 ]
72
+ index = findall (x -> (x== v), tree[p])[1 ]
73
73
else
74
74
index = 1
75
75
end
@@ -83,51 +83,51 @@ function first_walk{T}(v::T,t::Tree)
83
83
defaultAncestor = tree[v][1 ]
84
84
for w in tree[v]
85
85
first_walk (w,t)
86
- defaultAncestor = apportion (w,defaultAncestor,t)
86
+ defaultAncestor = apportion (w, defaultAncestor, t)
87
87
end
88
- execute_shifts (v,t)
88
+ execute_shifts (v, t)
89
89
midpoint = (prelim[tree[v][1 ]] + prelim[tree[v][end ]]) / 2
90
90
if index > 1
91
91
w = tree[p][index- 1 ]
92
- prelim[v] = prelim[w] + (nodesize[w]+ 1.0 )
92
+ prelim[v] = prelim[w] + (nodesize[w] + 1.0 )
93
93
mod[v] = prelim[v] - midpoint
94
94
else
95
95
prelim[v] = midpoint
96
96
end
97
97
end
98
98
end
99
99
100
- function apportion {T} (v:: T ,defaultAncestor:: T ,t:: Tree )
100
+ function apportion (v:: T , defaultAncestor:: T , t:: Tree ) where {T}
101
101
tree = t. nodes
102
102
ancestor = t. ancestor
103
103
prelim = t. prelim
104
104
mod = t. mod
105
105
thread = t. thread
106
- p = parent (v,t)
106
+ p = parent (v, t)
107
107
nodesize = t. nodesize
108
108
if p != nothing
109
- index = find (x-> (x== v),tree[p])[1 ]
109
+ index = findall (x-> (x== v), tree[p])[1 ]
110
110
else
111
111
index = 1
112
112
end
113
113
if index > 1
114
114
w = tree[p][index- 1 ]
115
115
v_in_right = v_out_right = v
116
116
v_in_left = w
117
- v_out_left = tree[parent (v_in_right,t)][1 ]
117
+ v_out_left = tree[parent (v_in_right, t)][1 ]
118
118
s_in_right = mod[v_in_right]
119
119
s_out_right = mod[v_out_right]
120
120
s_in_left = mod[v_in_left]
121
121
s_out_left = mod[v_out_left]
122
- while next_right (v_in_left,t)!= 0 && next_left (v_in_right,t)!= 0
123
- v_in_left = next_right (v_in_left,t)
124
- v_in_right = next_left (v_in_right,t)
125
- v_out_left = next_left (v_out_left,t)
126
- v_out_right = next_right (v_out_right,t)
122
+ while next_right (v_in_left, t)!= 0 && next_left (v_in_right, t)!= 0
123
+ v_in_left = next_right (v_in_left, t)
124
+ v_in_right = next_left (v_in_right, t)
125
+ v_out_left = next_left (v_out_left, t)
126
+ v_out_right = next_right (v_out_right, t)
127
127
ancestor[v_out_right] = v
128
128
shift = (prelim[v_in_left] + s_in_left) - (prelim[v_in_right] + s_in_right) + (nodesize[v_in_left])
129
129
if shift > 0
130
- move_subtree (find_ancestor (v_in_left,v, defaultAncestor,t),v,shift,t)
130
+ move_subtree (find_ancestor (v_in_left, v, defaultAncestor,t), v,shift, t)
131
131
s_in_right += shift
132
132
s_out_right += shift
133
133
end
@@ -136,12 +136,12 @@ function apportion{T}(v::T,defaultAncestor::T,t::Tree)
136
136
s_out_left += mod[v_out_left]
137
137
s_out_right += mod[v_out_right]
138
138
end
139
- if next_right (v_in_left,t) != 0 && next_right (v_out_right,t) == 0
140
- thread[v_out_right] = next_right (v_in_left,t)
139
+ if next_right (v_in_left, t) != 0 && next_right (v_out_right, t) == 0
140
+ thread[v_out_right] = next_right (v_in_left, t)
141
141
mod[v_out_right] += s_in_left - s_out_right
142
142
else
143
- if next_left (v_in_right,t)!= 0 next_left (v_out_left,t)== 0
144
- thread[v_out_left] = next_left (v_in_right,t)
143
+ if next_left (v_in_right,t) != 0 && next_left (v_out_left,t) == 0
144
+ thread[v_out_left] = next_left (v_in_right, t)
145
145
mod[v_out_left] += s_in_right - s_out_left
146
146
defaultAncestor = v
147
147
end
@@ -150,20 +150,20 @@ function apportion{T}(v::T,defaultAncestor::T,t::Tree)
150
150
return defaultAncestor
151
151
end
152
152
153
- function number {T} (v :: T , t:: Tree )
154
- p = parent (v,t)
155
- index = find (x -> (x== v),t. nodes[p])[1 ]
153
+ function number (v, t:: Tree )
154
+ p = parent (v, t)
155
+ index = findall (x -> (x== v), t. nodes[p])[1 ]
156
156
return index
157
157
end
158
158
159
- function move_subtree {T} (w_left:: T ,w_right:: T ,shift:: Float64 ,t:: Tree )
159
+ function move_subtree (w_left:: T , w_right:: T , shift:: Float64 , t:: Tree ) where {T}
160
160
change = t. change
161
161
prelim = t. prelim
162
162
tree = t. nodes
163
163
shifttree = t. shift
164
164
mod = t. mod
165
- n_wl = number (w_left,t)
166
- n_wr = number (w_right,t)
165
+ n_wl = number (w_left, t)
166
+ n_wr = number (w_right, t)
167
167
subtrees = n_wr - n_wl
168
168
change[w_right] -= shift / subtrees
169
169
shifttree[w_right] += shift
@@ -172,32 +172,32 @@ function move_subtree{T}(w_left::T,w_right::T,shift::Float64,t::Tree)
172
172
mod[w_right] += shift
173
173
end
174
174
175
- function second_walk {T} (v :: T , m:: Float64 ,depth:: Float64 ,t:: Tree )
175
+ function second_walk (v, m:: Float64 , depth:: Float64 , t:: Tree )
176
176
prelim = t. prelim
177
177
mod = t. mod
178
178
positions = t. positions
179
179
nodesize = t. nodesize
180
- positions[v] = Point (prelim[v]+ m,- depth)
181
- if length (t. nodes[v])!= 0
180
+ positions[v] = Point (prelim[v]+ m, - depth)
181
+ if length (t. nodes[v]) != 0
182
182
maxdist = maximum ([nodesize[i] for i in t. nodes[v]])
183
183
else
184
184
maxdist = 0
185
185
end
186
186
for w in t. nodes[v]
187
- second_walk (w,m+ mod[v],Float64 (depth+ 1 + maxdist),t)
187
+ second_walk (w, m+ mod[v], Float64 (depth + 1 + maxdist), t)
188
188
end
189
189
end
190
190
191
- function find_ancestor {T} (w:: T ,v:: T ,defaultAncestor:: T ,tree:: Tree )
191
+ function find_ancestor (w:: T , v:: T , defaultAncestor:: T , tree:: Tree ) where {T}
192
192
ancestor = tree. ancestor
193
- if ancestor[w] in tree. nodes[parent (v,tree)]
193
+ if ancestor[w] in tree. nodes[parent (v, tree)]
194
194
return ancestor[w]
195
195
else
196
196
return defaultAncestor
197
197
end
198
198
end
199
199
200
- function execute_shifts {T} (v :: T , t:: Tree )
200
+ function execute_shifts (v, t:: Tree )
201
201
tree = t. nodes
202
202
shift = t. shift
203
203
change = t. change
@@ -213,7 +213,7 @@ function execute_shifts{T}(v::T,t::Tree)
213
213
end
214
214
end
215
215
216
- function next_left {T} (v :: T , t:: Tree )
216
+ function next_left (v, t:: Tree )
217
217
tree = t. nodes
218
218
thread = t. thread
219
219
if length (tree[v]) != 0
@@ -223,7 +223,7 @@ function next_left{T}(v::T,t::Tree)
223
223
end
224
224
end
225
225
226
- function next_right {T} (v :: T , t:: Tree )
226
+ function next_right (v, t:: Tree )
227
227
tree = t. nodes
228
228
thread = t. thread
229
229
if length (tree[v]) != 0
0 commit comments