Class IterationGuard
- Namespace
- Support
- Assembly
- Support.dll
Provides methods that can help detect infinite loops in Debug builds.
public static class IterationGuard
- Inheritance
-
IterationGuard
- Inherited Members
Examples
Here is how the guard can be used in a real example:
public BoruvkasAlgorithm(IReadOnlyEdgeWeightedGraph<TWeight> graph)
{
var unionFind = new UnionFind(graph.VertexCount);
IterationGuard.Reset();
while (unionFind.ComponentCount > 1)
{
IterationGuard.Inc();
var minEdge = FindMinimumEdges(graph, unionFind);
AddMinimumEdgesToMst(minEdge, unionFind);
}
}
This algorithm relies in the fact that eventually there will only be one connected component. If the algorithm is not implemented correctly, the iteration limit will be exceeded and an exception will be thrown, allowing us to inspect the internal state and see what is going on.
Remarks
To use this feature:
- Call Reset(int) before the iteration starts, using a limit that makes sense for your tests.
- Call Inc() at the beginning of each iteration.
If the iteration limit is exceeded, an InvalidOperationException is thrown. This makes it easier to use the debugger to find the problem, since the code will break at the point where the iteration limit is exceeded.
All iteration guard calls use the same guard, so algorithms should not be run in parallel.
Methods
Inc()
[Conditional("DEBUG")]
public static void Inc()
Reset(int)
This resets the counter to zero and sets the limit to the specified value.
[Conditional("DEBUG")]
public static void Reset(int limit = 1000000)
Parameters
limit
int
Remarks
Call this before the iteration starts.