private static TrackID?DescribeBranch(Junction.Branch startBranch) { if (descriptions.TryGetValue(startBranch, out var trackID)) { return(trackID); } // Main.DebugLog($"Starting BFS at {startBranch.track.logicTrack.ID}"); var visited = new HashSet <RailTrack>(); var queue = new Queue <Junction.Branch>(GetNextBranches(startBranch)); Junction.Branch?branch; while (queue.Count > 0) { branch = queue.Dequeue(); // Main.DebugLog($"Examining {branch}, track={branch.track}, logicTrack={branch.track.logicTrack}, ID={branch.track.logicTrack.ID}"); trackID = branch.track.logicTrack.ID; if (!trackID.IsGeneric()) { descriptions[startBranch] = trackID; return(trackID); } // avoid cycles if (visited.Contains(branch.track)) { continue; } visited.Add(branch.track); foreach (var next in GetNextBranches(branch)) { queue.Enqueue(next); } } // Main.DebugLog($"BFS ended without result"); descriptions[startBranch] = null; // Main.DebugLog($"returning null"); return(null); }
private static IEnumerable <Junction.Branch> GetNextBranches(Junction.Branch start) => (start.first ? start.track.GetAllOutBranches() : start.track.GetAllInBranches()) ?? Enumerable.Empty <Junction.Branch>();