Skip to content

Commit

Permalink
Replace try catch with the try pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAIBot committed Aug 11, 2021
1 parent bc39ba3 commit 7834659
Showing 1 changed file with 65 additions and 62 deletions.
127 changes: 65 additions & 62 deletions ChiselDebug/ChiselDebug/Routing/SimpleRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,81 +38,81 @@ public bool IsReadyToRoute()
return MissingNodeIO.Count == 0;
}

public List<WirePath> PathLines(PlacementInfo placements, CancellationToken cancelToken)
public bool TryPathLines(PlacementInfo placements, CancellationToken cancelToken, out List<WirePath> wiresPaths)
{
try
Dictionary<Line, int> repathCounter = new Dictionary<Line, int>();

List<LineInfo> sortedLines = new List<LineInfo>();
foreach ((IOInfo start, IOInfo end) in Connections.GetAllConnectionLines(placements))
{
Dictionary<Line, int> repathCounter = new Dictionary<Line, int>();

List<LineInfo> sortedLines = new List<LineInfo>();
foreach ((IOInfo start, IOInfo end) in Connections.GetAllConnectionLines(placements))
{
LineInfo line = new LineInfo(start, end);
sortedLines.Add(line);
repathCounter.Add(line.GetLine(), 0);
}
LineInfo line = new LineInfo(start, end);
sortedLines.Add(line);
repathCounter.Add(line.GetLine(), 0);
}

Queue<LineInfo> linesPriority = new Queue<LineInfo>(sortedLines.OrderBy(x => x.GetScore()));
Queue<LineInfo> linesPriority = new Queue<LineInfo>(sortedLines.OrderBy(x => x.GetScore()));

RouterBoard board = new RouterBoard(placements.SpaceNeeded);
board.PrepareBoard(placements.UsedSpace.Values.ToList());
board.CreateCheckpoint();
RouterBoard board = new RouterBoard(placements.SpaceNeeded);
board.PrepareBoard(placements.UsedSpace.Values.ToList());
board.CreateCheckpoint();

List<WirePath> paths = new List<WirePath>();
while (linesPriority.Count > 0)
List<WirePath> paths = new List<WirePath>();
while (linesPriority.Count > 0)
{
if (cancelToken.IsCancellationRequested)
{
if (cancelToken.IsCancellationRequested)
{
return new List<WirePath>();
}
wiresPaths = null;
return false;
}

(IOInfo start, IOInfo end) = linesPriority.Dequeue();
(IOInfo start, IOInfo end) = linesPriority.Dequeue();

Rectangle? startRect = null;
Rectangle? endRect = null;
if (placements.UsedSpace.ContainsKey(start.Node))
{
startRect = placements.UsedSpace[start.Node];
}
if (placements.UsedSpace.ContainsKey(end.Node))
{
endRect = placements.UsedSpace[end.Node];
}
Rectangle? startRect = null;
Rectangle? endRect = null;
if (placements.UsedSpace.ContainsKey(start.Node))
{
startRect = placements.UsedSpace[start.Node];
}
if (placements.UsedSpace.ContainsKey(end.Node))
{
endRect = placements.UsedSpace[end.Node];
}

WirePath path = PathLine(board, start, end, startRect, endRect, paths);
Debug.Assert(path.StartIO == start && path.EndIO == end);
WirePath path;
if (!TryPathLine(board, start, end, startRect, endRect, paths, out path))
{
Debug.WriteLine("Failed to find a path:");
wiresPaths = null;
return false;
}

List<WirePath> needsRepathing = new List<WirePath>();
foreach (var oldPath in paths)
Debug.Assert(path.StartIO == start && path.EndIO == end);
List<WirePath> needsRepathing = new List<WirePath>();
foreach (var oldPath in paths)
{
if (!path.CanCoexist(oldPath))
{
if (!path.CanCoexist(oldPath))
{
needsRepathing.Add(oldPath);
}
needsRepathing.Add(oldPath);
}
foreach (var repath in needsRepathing)
}
foreach (var repath in needsRepathing)
{
LineInfo line = new LineInfo(repath.StartIO, repath.EndIO);
if (repathCounter[line.GetLine()]++ >= 20)
{
LineInfo line = new LineInfo(repath.StartIO, repath.EndIO);
if (repathCounter[line.GetLine()]++ >= 20)
{
continue;
}
continue;
}

paths.Remove(repath);
paths.Remove(repath);

linesPriority.Enqueue(line);
}
paths.Add(path);
linesPriority.Enqueue(line);
}

RefineWirePaths(board, paths);
return paths;
}
catch (Exception e)
{
Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
throw;
paths.Add(path);
}

RefineWirePaths(board, paths);
wiresPaths = paths;
return true;
}

private readonly struct MoveData
Expand All @@ -131,7 +131,7 @@ public MoveData(MoveDirs dir)
}
}

private WirePath PathLine(RouterBoard board, IOInfo start, IOInfo end, Rectangle? startRect, Rectangle? endRect, List<WirePath> allPaths)
private bool TryPathLine(RouterBoard board, IOInfo start, IOInfo end, Rectangle? startRect, Rectangle? endRect, List<WirePath> allPaths, out WirePath wirePath)
{
board.ReloadCheckpoint();

Expand Down Expand Up @@ -208,15 +208,17 @@ private WirePath PathLine(RouterBoard board, IOInfo start, IOInfo end, Rectangle

if (current == relativeStart)
{
return board.GetPath(relativeEnd, relativeStart, start, end, false);
wirePath = board.GetPath(relativeEnd, relativeStart, start, end, false);
return true;
}

ScorePath currentScorePath = board.GetCellScorePath(currentIndex);
MoveDirs allowedMoves = board.GetCellMoves(currentIndex);

if (allowedMoves.HasFlag(MoveDirs.FriendWire) && canEndEarly)
{
return board.GetPath(relativeEnd, current, start, end, true);
wirePath = board.GetPath(relativeEnd, current, start, end, true);
return true;
}

bool onEnemyWire = allowedMoves.HasFlag(MoveDirs.EnemyWire);
Expand Down Expand Up @@ -249,7 +251,8 @@ private WirePath PathLine(RouterBoard board, IOInfo start, IOInfo end, Rectangle

Debug.WriteLine(board.BoardAllowedMovesToString(relativeEnd, relativeStart));
Debug.WriteLine(board.BoardStateToString(relativeEnd, relativeStart));
throw new Exception("Failed to find a path.");
wirePath = null;
return false;
}

private void RefineWirePaths(RouterBoard board, List<WirePath> paths)
Expand Down

0 comments on commit 7834659

Please sign in to comment.