-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGraph+EdgeAccess.swift
133 lines (115 loc) · 3.58 KB
/
Graph+EdgeAccess.swift
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
import SwiftyToolz
public extension Graph
{
/**
Removes the corresponding ``GraphEdge``, see ``Graph/removeEdge(with:)``
*/
@discardableResult
mutating func removeEdge(from originID: NodeID,
to destinationID: NodeID) -> Edge?
{
removeEdge(with: .init(originID, destinationID))
}
/**
Removes the ``GraphEdge`` with the given ID, also removing it from node neighbour caches
*/
@discardableResult
mutating func removeEdge(with id: Edge.ID) -> Edge?
{
// remove from node caches
nodesByID[id.originID]?.descendantIDs -= id.destinationID
nodesByID[id.destinationID]?.ancestorIDs -= id.originID
// remove edge itself
return edgesByID.removeValue(forKey: id)
}
/**
Add weight to the existing edge or create the edge with that weight
- Returns: The new weight of the edge with the given ID
*/
@discardableResult
mutating func add(_ weight: EdgeWeight,
toEdgeFrom originID: NodeID,
to destinationID: NodeID) -> EdgeWeight
{
add(weight, toEdgeWith: .init(originID, destinationID))
}
/**
Add weight to the existing edge or create the edge with that weight
- Returns: The new weight of the edge with the given ID
*/
@discardableResult
mutating func add(_ weight: EdgeWeight, toEdgeWith id: Edge.ID) -> EdgeWeight
{
if let existingWeight = edgesByID[id]?.weight
{
edgesByID[id]?.weight += weight
return existingWeight + weight
}
else
{
insertEdge(from: id.originID,
to: id.destinationID,
weight: weight)
return weight
}
}
@discardableResult
mutating func insertEdge(from originID: NodeID,
to destinationID: NodeID,
weight: EdgeWeight = 1) -> Edge
{
let edge = Edge(from: originID, to: destinationID, weight: weight)
insert(edge)
return edge
}
mutating func insert(_ edge: Edge)
{
if !contains(edge.id)
{
// update node caches because edge does not exist yet
nodesByID[edge.originID]?.descendantIDs += edge.destinationID
nodesByID[edge.destinationID]?.ancestorIDs += edge.originID
}
edgesByID.insert(edge)
}
/**
The ``GraphEdge`` between the corresponding nodes if it exists, otherwise `nil`
*/
func edge(from originID: NodeID, to destinationID: NodeID) -> Edge?
{
edge(with: .init(originID, destinationID))
}
/**
The ``GraphEdge`` with the given ID if the edge exists, otherwise `nil`
*/
func edge(with id: Edge.ID) -> Edge?
{
edgesByID[id]
}
func containsEdge(from originID: NodeID,
to destinationID: NodeID) -> Bool
{
contains(.init(originID, destinationID))
}
/**
Whether the `Graph` contains a ``GraphEdge`` with the given ``GraphEdge/id-swift.property``
*/
func contains(_ edgeID: Edge.ID) -> Bool
{
edgesByID.keys.contains(edgeID)
}
/**
All ``GraphEdge``s of the `Graph`
*/
var edges: some Collection<Edge>
{
edgesByID.values
}
/**
All ``GraphEdge/id-swift.property``s of the ``GraphEdge``s of the `Graph`
*/
var edgeIDs: some Collection<Edge.ID>
{
edgesByID.keys
}
}