-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_funs.jl
More file actions
175 lines (144 loc) · 4.09 KB
/
Copy pathnode_funs.jl
File metadata and controls
175 lines (144 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
isleaf(node::Node)
Test whether a node is a leaf or not.
"""
isleaf(node::Node) = isempty(children(node))
"""
isroot(node::Node)
Return `true` if `node` is the root node (meaning, it has no parent).
"""
isroot(node::Node) = parent(node) === nothing
"""
lastchild(node::Node)
Get the last child of `node`, or `nothing` if the node is a leaf.
"""
function lastchild(node::Node)
if isleaf(node)
return nothing
else
return last(children(node))
end
end
"""
addchild!(p::Node, id::Int, MTG<:AbstractNodeMTG, attributes)
addchild!(p::Node, MTG<:AbstractNodeMTG, attributes)
addchild!(p::Node, MTG<:AbstractNodeMTG)
addchild!(p::Node, child::Node; force=false)
Add a new child to a parent node (`p`), and add the parent node as the parent.
Returns the child node.
See also [`insert_child!`](@ref), or directly [`Node`](@ref) where we
can pass the parent, and it uses `addchild!` under the hood.
# Examples
```julia
# Create a root node:
mtg = MultiScaleTreeGraph.Node(
NodeMTG("/", "Plant", 1, 1),
Dict{Symbol,Any}()
)
roots = addchild!(
mtg,
NodeMTG("+", "RootSystem", 1, 2)
)
stem = addchild!(
mtg,
NodeMTG("+", "Stem", 1, 2)
)
phyto = addchild!(
stem,
NodeMTG("/", "Phytomer", 1, 3)
)
mtg
```
"""
function addchild!(p::Node, id::Int, MTG::M, attributes) where {M<:AbstractNodeMTG}
child = Node(id, p, MTG, attributes)
return child
end
function addchild!(p::Node, MTG::M, attributes) where {M<:AbstractNodeMTG}
child = Node(p, MTG, attributes)
return child
end
function addchild!(p::Node, MTG::M) where {M<:AbstractNodeMTG}
child = Node(p, MTG)
return child
end
@inline function _columnar_store_or_nothing(node::Node)
attrs = node_attributes(node)
attrs isa ColumnarAttrs || return nothing
return _store_for_node_attrs(attrs)
end
@inline function _maybe_recolumnarize_after_attach!(p::Node, child::Node, child_was_root::Bool)
child_was_root || return nothing
p_store = _columnar_store_or_nothing(p)
c_store = _columnar_store_or_nothing(child)
if p_store !== nothing && c_store !== nothing && p_store !== c_store
columnarize!(get_root(p))
end
return nothing
end
function addchild!(p::Node{N,A}, child::Node; force=false) where {N<:AbstractNodeMTG,A}
child_was_root = parent(child) === nothing
if child_was_root || force == true
reparent!(child, p)
elseif parent(child) != p && force == false
error("The node already has a parent. Hint: use `force=true` if needed.")
end
push!(children(p), child)
_maybe_recolumnarize_after_attach!(p, child, child_was_root)
return child
end
"""
Find the root node of a tree, given any node in the tree.
"""
function get_root(node::Node)
root = node
while !isroot(root)
root = parent(root)
end
return root
end
"""
siblings(node::Node)
Return the siblings of `node` as a vector of nodes (or `nothing` if non-existant).
"""
function siblings(node::Node)
# If there is no parent, no siblings, return nothing:
parent_ = parent(node)
parent_ === nothing && return nothing
all_siblings = children(parent_)
nsiblings = length(all_siblings)
nsiblings <= 1 && return similar(all_siblings, 0)
out = Vector{eltype(all_siblings)}(undef, nsiblings - 1)
j = 1
@inbounds for sibling in all_siblings
if sibling !== node
out[j] = sibling
j += 1
end
end
resize!(out, j - 1)
return out
end
"""
lastsibling(node::Node)
Return the last sibling of `node` (or `nothing` if non-existant).
"""
function lastsibling(node::Node)
# If there is no parent, no siblings, return nothing:
parent_ = parent(node)
parent_ === nothing && return nothing
return last(children(parent_))
end
"""
new_id(mtg)
new_id(mtg, max_id)
Make a new unique identifier by incrementing on the maximum node id.
Hint: prefer using `max_id = max_id(mtg)` and then `new_id(mtg, max_is)` for performance
if you do it repeatidely.
"""
function new_id(max_id::Int)
max_id + 1
end
function new_id(mtg::Node)
new_id(max_id(mtg))
end