diff --git a/CriticalPathAnalyzer/CriticalPathAnalyzer/src/server/tool/ServerPathTracer.cs b/CriticalPathAnalyzer/CriticalPathAnalyzer/src/server/tool/ServerPathTracer.cs index 156f670..b315556 100644 --- a/CriticalPathAnalyzer/CriticalPathAnalyzer/src/server/tool/ServerPathTracer.cs +++ b/CriticalPathAnalyzer/CriticalPathAnalyzer/src/server/tool/ServerPathTracer.cs @@ -133,6 +133,7 @@ namespace CriticalPathAnalyzer.Server.Tool { if (iterations % 1000 == 0) { CriticalPathAnalyzerServer.LoggerInstance.Info($"{iterations} propagation iterations"); } + ClusterNode node = clusterNodes[index]; foreach ((int nextNodeIndex, int delay) in node.NextNodes) { clusterNodes[nextNodeIndex].Time = node.Time + delay; @@ -169,6 +170,7 @@ namespace CriticalPathAnalyzer.Server.Tool { if (iterations % 1000 == 0) { CriticalPathAnalyzerServer.LoggerInstance.Info($"{iterations} loop iterations"); } + foreach ((int nextNodeIndex, int _) in node.NextNodes) { ClusterNode nextNode = clusterNodes[nextNodeIndex]; nextNode.PrevNodeIndexes.Remove(node.Index); @@ -250,24 +252,40 @@ namespace CriticalPathAnalyzer.Server.Tool { logger.Info($"Finished after {iterations} iterations"); } - private static HashSet GetClustersConnectedThroughRelays(Cluster cluster) { - var clusters = new HashSet() {cluster}; - foreach (InputPeg inputPeg in cluster.ConnectedInputs) { - LogicComponent component = inputPeg.LogicComponent; - if (component == null) { - // These are regular pegs, they are not attached to any logic component, skip them - continue; - } + private static HashSet GetClustersConnectedThroughRelays(Cluster origin) { + var clusters = new HashSet {origin}; + var queue = new Queue(); + queue.Enqueue(origin); + + while (queue.TryDequeue(out Cluster cluster)) { + foreach (InputPeg inputPeg in cluster.ConnectedInputs) { + LogicComponent component = inputPeg.LogicComponent; + if (component == null) { + // These are regular pegs, they are not attached to any logic component, skip them + continue; + } - if (component.GetType() == typeof(Relay)) { - // Collect all next clusters connected via a relay (from the control input side) - if (inputPeg.Address == component.Inputs[1].Address) { - // one of the sides - // TODO: need to recursively check for further relays - CollectPegClusters(component.Inputs[2].Address, clusters); - } else if (inputPeg.Address == component.Inputs[2].Address) { - // the other side - CollectPegClusters(component.Inputs[1].Address, clusters); + if (component.GetType() == typeof(Relay)) { + // Collect all next clusters connected via a relay (from the control input side) + if (inputPeg.Address == component.Inputs[1].Address) { + // one of the sides + if (component.Inputs[2].Address.IsInputAddress(out InputAddress inputAddress)) { + Cluster nextCluster = GetClusterAt(inputAddress); + if (clusters.Add(nextCluster)) { + // found a new cluster, continue searching + queue.Enqueue(nextCluster); + } + } + } else if (inputPeg.Address == component.Inputs[2].Address) { + // the other side + if (component.Inputs[1].Address.IsInputAddress(out InputAddress inputAddress)) { + Cluster nextCluster = GetClusterAt(inputAddress); + if (clusters.Add(nextCluster)) { + // found a new cluster, continue searching + queue.Enqueue(nextCluster); + } + } + } } } }