Skip to content

Commit 5e6fd7f

Browse files
committed
refactor: use TimeSpan and named constant in SearchManager
1 parent 5aec425 commit 5e6fd7f

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

Alligator.Solver/Algorithms/SearchManager.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ namespace Alligator.Solver.Algorithms
44
{
55
internal class SearchManager : ISearchManager
66
{
7+
private const int TimeBudgetCheckInterval = 1024;
8+
79
private readonly Stopwatch stopwatch = new();
8-
private readonly long timeLimitMs;
10+
private readonly TimeSpan timeBudget;
911
private int nodeCount;
1012

1113
public int MaxDepth { get; }
@@ -15,23 +17,23 @@ internal class SearchManager : ISearchManager
1517
public SearchManager(int maxDepth, TimeSpan? timeBudget = null)
1618
{
1719
MaxDepth = maxDepth;
18-
timeLimitMs = (long)(timeBudget?.TotalMilliseconds ?? 0);
20+
this.timeBudget = timeBudget ?? TimeSpan.Zero;
1921
}
2022

2123
public void StartSearch()
2224
{
2325
nodeCount = 0;
2426
IsAborted = false;
25-
if (timeLimitMs > 0)
27+
if (timeBudget > TimeSpan.Zero)
2628
{
2729
stopwatch.Restart();
2830
}
2931
}
3032

3133
public void CheckTimeBudget()
3234
{
33-
if (timeLimitMs > 0 && (++nodeCount & 0x3FF) == 0
34-
&& stopwatch.ElapsedMilliseconds >= timeLimitMs)
35+
if (timeBudget > TimeSpan.Zero && ++nodeCount % TimeBudgetCheckInterval == 0
36+
&& stopwatch.Elapsed >= timeBudget)
3537
{
3638
IsAborted = true;
3739
}

0 commit comments

Comments
 (0)