-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGraph+NodeAccess.swift
72 lines (63 loc) · 1.6 KB
/
Graph+NodeAccess.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
public extension Graph
{
/// Remove the node with the given ID also removing its in- and outgoing edges
@discardableResult
mutating func removeNode(with nodeID: NodeID) -> Node?
{
guard let node = nodesByID.removeValue(forKey: nodeID) else
{
return nil
}
for ancestorID in node.ancestorIDs
{
removeEdge(with: .init(ancestorID, nodeID))
}
for descendantID in node.descendantIDs
{
removeEdge(with: .init(nodeID, descendantID))
}
return node
}
/**
All source nodes of the `Graph`, see ``GraphNode/isSource``
*/
var sources: some Collection<Node>
{
nodesByID.values.filter { $0.isSource }
}
/**
All sink nodes of the `Graph`, see ``GraphNode/isSink``
*/
var sinks: some Collection<Node>
{
nodesByID.values.filter { $0.isSink }
}
/**
Whether the `Graph` contains a ``GraphNode`` with the given ``GraphNode/id``
*/
func contains(_ nodeID: NodeID) -> Bool
{
nodesByID.keys.contains(nodeID)
}
/**
``GraphNode`` with the given ``GraphNode/id`` if one exists, otherwise `nil`
*/
func node(with id: NodeID) -> Node?
{
nodesByID[id]
}
/**
All ``GraphNode``s of the `Graph`
*/
var nodes: some Collection<Node>
{
nodesByID.values
}
/**
The ``GraphNode/id``s of all ``GraphNode``s of the `Graph`
*/
var nodeIDs: some Collection<NodeID>
{
nodesByID.keys
}
}