-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphCalculate.swift
More file actions
79 lines (62 loc) · 2.73 KB
/
GraphCalculate.swift
File metadata and controls
79 lines (62 loc) · 2.73 KB
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
//
// GraphCalculate.swift
//
//
// Created by Elliot Boschwitz on 6/8/24.
//
import Foundation
extension GraphCalculatable {
@MainActor
/// The main graph calculator. Shouldn't be called directly unless you know what you're doing.
public func calculate(from nodeIds: Set<Node.ID>,
willResetCyclePreferences: Bool = false) {
let graphState = self
var visitedNodes = Set<Node.ID>()
var queue = nodeIds
// Reset state on scheduled cycle nodes
self.topologicalData.nodesForNextGraphStep = .init()
while let nodeId = topologicalData.getNextNodeToCalculate(for: queue,
visitedNodes: visitedNodes,
willResetCyclePreferences: willResetCyclePreferences) {
queue.remove(nodeId)
guard !visitedNodes.contains(nodeId) else {
assertInDebug(self.topologicalData.cycleContains(nodeId))
#if DEV_DEBUG
// log("GraphState.calculate: scheduling cycle node \(nodeId)")
#endif
self.topologicalData.nodesForNextGraphStep.insert(nodeId)
continue
}
visitedNodes.insert(nodeId)
// Retrieve the node afresh everytime,
// since an upstream node's changes may have changed its inputs
guard let node = self.getNodeViewModel(id: nodeId) else {
// Not necessarily bad -- can happen if we deleted nodes but those nodes were still scheduled to run.
// fatalErrorIfDebug()
continue
}
let nextDownstreamNodes = self.calculateNode(node)
// Update queue set with changed downstream nodes
queue = nextDownstreamNodes.union(Set(queue))
}
// Check if preview layers need to be regenerated
if self.shouldResortPreviewLayers {
// log("EvalFlowResult.calculate: will re-sort preview layers")
graphState.updateOrderedPreviewLayers()
// Toggle back off
graphState.shouldResortPreviewLayers = false
}
}
@MainActor
public func calculate(_ id: Node.ID) {
self.topologicalData.nodesForNextGraphStep.insert(id)
}
@MainActor
public func calculate(_ idSet: Set<Node.ID>) {
self.topologicalData.nodesForNextGraphStep = self.topologicalData.nodesForNextGraphStep.union(idSet)
}
@MainActor
public func calculate(_ idList: [Node.ID]) {
self.calculate(Set(idList))
}
}