-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_funs.jl
More file actions
208 lines (174 loc) · 5.42 KB
/
Copy pathnode_funs.jl
File metadata and controls
208 lines (174 loc) · 5.42 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
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
function _subtree_has_node_id_conflict(node::Node, store::MTGAttributeStore)
nid = node_id(node)
if nid <= length(store.node_bucket) && store.node_bucket[nid] != 0
return true
end
for ch in children(node)
_subtree_has_node_id_conflict(ch, store) && return true
end
return false
end
function _merge_columnar_subtree_into_store!(node::Node, target_store::MTGAttributeStore)
attrs = node_attributes(node)
attrs isa ColumnarAttrs || return false
snapshot = Dict{Symbol,Any}(pairs(attrs))
_add_node_with_attrs!(target_store, node_id(node), symbol(node), snapshot)
attrs.ref.store = target_store
attrs.ref.node_id = node_id(node)
empty!(attrs.staged)
for ch in children(node)
_merge_columnar_subtree_into_store!(ch, target_store) || return false
end
return true
end
function _try_merge_root_subtree_store!(child::Node, parent_store::MTGAttributeStore, child_store::MTGAttributeStore)
parent_store === child_store && return true
_subtree_has_node_id_conflict(child, parent_store) && return false
return _merge_columnar_subtree_into_store!(child, parent_store)
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
# Fast path: re-bind only the attached subtree into the parent store.
# Fallback to full re-columnarization when IDs collide.
_try_merge_root_subtree_store!(child, 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