/
types.jl
58 lines (52 loc) · 1.7 KB
/
types.jl
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
"""
Edge
Directed edge from `parent` to `child`, both of type `Int` (for indices).
Field `length` can be missing.
"""
mutable struct Edge
parent::Int
child::Int
length::Union{Missing,Float64}
end
# default edge length is missing:
Edge(parent, child) = Edge(parent, child, missing)
function Base.show(io::IO, edge::Edge)
str = "edge from $(edge.parent) to $(edge.child)"
str *= (ismissing(edge.length) ? "" : ", length $(edge.length)")
str *= "\n"
print(io, str)
end
"""
Tree{T}(edge, label, foo)
Tree (or directed graph) described by its list of `edge`s.
`label` is a dictionary whose values (node labels) are of type `T`,
such that labels could be strings, symbols, integers etc., but they
should be of the **same type** for all nodes in the tree.
Field `foo` is just to illustrate the difference between
`mutable struct` and `struct`.
"""
struct Tree{T}
edge::Vector{Edge}
label::Dict{Int64,T}
foo::Int
end
# if no arguments at all: create empty tree with String labels, and foo=0
Tree() = Tree{String}(Vector{Edge}(), Dict{Int64,String}(), 0)
# given edges only: declare labels of type String, foo = 0
Tree(edge::Vector{Edge}) = Tree{String}(edge, Dict{Int64,String}(), 0)
# given edges and labels: extract type of labels to get T and create the tree
Tree(edge::Vector{Edge}, label::AbstractDict, foo=0::Int) =
Tree{eltype(values(label))}(edge, label, foo)
function Base.show(io::IO, tree::Tree{T}) where T
str = "parent -> child:"
for e in tree.edge
str *= "\n$(e.parent) $(e.child)"
end
if !isempty(tree.label)
str *= "\nlabels:"
for (ind,lab) in tree.label
str *= "\n$ind: $lab"
end
end
print(io, str)
end