-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGraph+ConvenientInitializers.swift
102 lines (90 loc) · 2.92 KB
/
Graph+ConvenientInitializers.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
//extension Graph: ExpressibleByArrayLiteral where NodeValue: Identifiable, NodeValue.ID == NodeID
//{
// public init(arrayLiteral elements: NodeValue...)
// {
// self.init(values: elements)
// }
//}
extension Graph: ExpressibleByArrayLiteral where NodeID == NodeValue
{
public init(arrayLiteral elements: NodeValue...)
{
self.init(values: elements)
}
}
extension Graph: ExpressibleByDictionaryLiteral
{
public init(dictionaryLiteral elements: (NodeID, NodeValue)...)
{
self.init(valuesByID: .init(uniqueKeysWithValues: elements))
}
}
public extension Graph
{
init(values: some Sequence<NodeValue>)
where NodeValue: Identifiable, NodeValue.ID == NodeID
{
self.init(valuesByID: .init(values: values))
}
/**
Uses the `NodeValue.ID` of a value as the ``GraphNode/id`` for its corresponding node
*/
init(values: some Sequence<NodeValue>,
edges: some Sequence<(NodeID, NodeID)>)
where NodeValue: Identifiable, NodeValue.ID == NodeID
{
self.init(valuesByID: .init(values: values), edges: edges)
}
init(values: some Sequence<NodeValue>,
edgeTuples: some Sequence<(NodeID, NodeID)>)
where NodeValue: Identifiable, NodeValue.ID == NodeID
{
self.init(valuesByID: .init(values: values), edges: edgeTuples)
}
/**
Uses the `NodeValue.ID` of a value as the ``GraphNode/id`` for its corresponding node
*/
init(values: some Sequence<NodeValue>,
edges: some Sequence<Edge>)
where NodeValue: Identifiable, NodeValue.ID == NodeID
{
self.init(valuesByID: .init(values: values), edges: edges)
}
init(values: some Sequence<NodeValue>)
where NodeID == NodeValue
{
self.init(valuesByID: .init(values: values))
}
/**
Uses a `NodeValue` itself as the ``GraphNode/id`` for its corresponding node
*/
init(values: some Sequence<NodeValue>,
edges: some Sequence<(NodeID, NodeID)>)
where NodeID == NodeValue
{
self.init(valuesByID: .init(values: values),
edges: edges)
}
/**
Uses a `NodeValue` itself as the ``GraphNode/id`` for its corresponding node
*/
init(values: some Sequence<NodeValue>,
edges: some Sequence<Edge>)
where NodeID == NodeValue
{
self.init(valuesByID: .init(values: values), edges: edges)
}
init(valuesByID: Dictionary<NodeID, NodeValue>)
{
self.init(valuesByID: valuesByID, edges: [Edge]())
}
/**
Create a `Graph` that determines ``GraphNode/id``s for new `NodeValue`s via the given closure
*/
init(valuesByID: Dictionary<NodeID, NodeValue>,
edges: some Sequence<(NodeID, NodeID)>)
{
let actualEdges = edges.map { Edge(from: $0.0, to: $0.1) }
self.init(valuesByID: valuesByID, edges: actualEdges)
}
}