-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticWireManager.swift
More file actions
60 lines (51 loc) · 1.61 KB
/
StaticWireManager.swift
File metadata and controls
60 lines (51 loc) · 1.61 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
//
// StaticWireManager.swift
// SimulatorLib
//
// Created by Bugen Zhao on 2020/4/25.
//
import Foundation
public class StaticWireManager {
var wires: [Wire] = []
var wiresDict: [WireName: Wire] = [:]
var checkpoint: [UInt64] = []
public func addWire(_ wire: Wire) {
if let exWire = wiresDict[wire.name] {
if exWire !== wire { fatalError(SimulatorError.WireManagerDuplicateNameError.rawValue) }
else { return }
}
wires.append(wire)
wiresDict[wire.name] = wire
}
public func clearCheckpoint() {
checkpoint.removeAll()
}
public func doCheckpoint() -> Bool {
var newCheckpoint = wires.map { $0.v }
defer { checkpoint = newCheckpoint }
return checkpoint == newCheckpoint
}
public func doPartialCheckpoint() -> Set<UnitName> {
var newCheckpoint = wires.map { $0.v }
defer { checkpoint = newCheckpoint }
var ret = Set<UnitName>()
for idx in 0..<checkpoint.count {
if checkpoint[idx] != newCheckpoint[idx] {
wires[idx].to.forEach { ret.insert($0) }
}
}
return ret
}
public func examine(verbose: Bool = true) -> Int {
return wires.reduce(0) { acc, wire in
if wire.from == nil {
if verbose { print("Warning: Wire \(wire.name): no from unit") }
return acc + 1
} else if wire.to.isEmpty {
if verbose { print("Warning: Wire \(wire.name): no to unit") }
return acc + 1
}
return acc
}
}
}