-
Notifications
You must be signed in to change notification settings - Fork 5
/
simpleweightededge.jl
122 lines (91 loc) · 3.22 KB
/
simpleweightededge.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
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
"""
AbstractSimpleWeightedEdge{T}
Abstract type for weighted edges with endpoints of type `T`.
"""
abstract type AbstractSimpleWeightedEdge{T} <: AbstractEdge{T} end
"""
SimpleWeightedEdge{T,U}
Concrete struct for a weighted edge with endpoints of type `T` and a weight of type `U<:Real`.
# Fields
- `src::T`: edge source
- `dst::T`: edge destination
- `weight::U`: edge weight
"""
struct SimpleWeightedEdge{T<:Integer,U<:Real} <: AbstractSimpleWeightedEdge{T}
src::T
dst::T
weight::U
end
"""
SimpleWeightedGraphEdge
Alias for `SimpleWeightedEdge`.
"""
const SimpleWeightedGraphEdge = SimpleWeightedEdge
"""
SimpleWeightedDiGraphEdge
Alias for `SimpleWeightedEdge`.
"""
const SimpleWeightedDiGraphEdge = SimpleWeightedEdge
"""
SimpleWeightedEdge((u, v))
Construct a `SimpleWeightedEdge` from `u` to `v` with a default weight of 1.0.
"""
SimpleWeightedEdge(t::NTuple{2}) = SimpleWeightedEdge(t[1], t[2], one(Float64))
function SimpleWeightedEdge{T,U}(t::NTuple{2}) where {T<:Integer,U<:Real}
return SimpleWeightedEdge(T(t[1]), T(t[2]), one(U))
end
"""
SimpleWeightedEdge(u => v)
Construct a `SimpleWeightedEdge` from `u` to `v` with a default weight of 1.0.
"""
SimpleWeightedEdge(p::Pair) = SimpleWeightedEdge(p.first, p.second, one(Float64))
function SimpleWeightedEdge{T,U}(p::Pair) where {T<:Integer,U<:Real}
return SimpleWeightedEdge(T(p.first), T(p.second), one(U))
end
"""
SimpleWeightedEdge((u, v, w))
Construct a `SimpleWeightedEdge` from `u` to `v` with a weight of `w`.
"""
SimpleWeightedEdge(t::NTuple{3}) = SimpleWeightedEdge(t[1], t[2], t[3])
function SimpleWeightedEdge{T,U}(t::NTuple{3}) where {T<:Integer,U<:Real}
return SimpleWeightedEdge(T(t[1]), T(t[2]), U(t[3]))
end
"""
SimpleWeightedEdge(u, v)
Construct a `SimpleWeightedEdge` from `u` to `v` with a default weight of 1.0.
"""
SimpleWeightedEdge(x, y) = SimpleWeightedEdge(x, y, one(Float64))
function SimpleWeightedEdge{T,U}(x, y) where {T<:Integer,U<:Real}
return SimpleWeightedEdge(x, y, one(U))
end
Base.eltype(::AbstractSimpleWeightedEdge{T}) where {T} = T
# Accessors
Graphs.src(e::AbstractSimpleWeightedEdge) = e.src
Graphs.dst(e::AbstractSimpleWeightedEdge) = e.dst
"""
weight(e)
Return the weight of a weighted edge.
"""
weight(e::AbstractSimpleWeightedEdge) = e.weight
# I/O
function Base.show(io::IO, e::AbstractSimpleWeightedEdge)
return print(io, "Edge $(e.src) => $(e.dst) with weight $(e.weight)")
end
# Conversions
Base.Tuple(e::AbstractSimpleWeightedEdge) = (src(e), dst(e), weight(e))
function (::Type{SimpleWeightedEdge{T,U}})(
e::AbstractSimpleWeightedEdge
) where {T<:Integer,U<:Real}
return SimpleWeightedEdge{T,U}(T(e.src), T(e.dst), U(e.weight))
end
# Convenience functions - note that these do not use weight.
Base.reverse(e::T) where {T<:AbstractSimpleWeightedEdge} = T(dst(e), src(e), weight(e))
function Base.:(==)(e1::AbstractSimpleWeightedEdge, e2::AbstractSimpleWeightedEdge)
return (src(e1) == src(e2) && dst(e1) == dst(e2))
end
function Base.:(==)(e1::AbstractSimpleWeightedEdge, e2::AbstractEdge)
return (src(e1) == src(e2) && dst(e1) == dst(e2))
end
function Base.:(==)(e1::AbstractEdge, e2::AbstractSimpleWeightedEdge)
return (src(e1) == src(e2) && dst(e1) == dst(e2))
end