forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontier.cs
More file actions
85 lines (69 loc) · 1.94 KB
/
Frontier.cs
File metadata and controls
85 lines (69 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace BotSharp.Core.Rules.Engines;
/// <summary>
/// Abstraction over the data structure that drives graph traversal order.
/// Stack → DFS, Queue → BFS. Swap the frontier mid-traversal to switch strategy.
/// </summary>
public interface IFrontier<T>
{
void Add(T item);
T Remove();
int Count { get; }
/// <summary>
/// Drain every remaining item into <paramref name="other"/>, preserving order.
/// </summary>
void DrainTo(IFrontier<T> other);
}
/// <summary>
/// LIFO frontier – produces depth-first traversal.
/// </summary>
public sealed class StackFrontier<T> : IFrontier<T>
{
private readonly Stack<T> _stack = new();
public int Count => _stack.Count;
public void Add(T item) => _stack.Push(item);
public T Remove() => _stack.Pop();
public void DrainTo(IFrontier<T> other)
{
// Pop gives items in priority order (highest-weight first).
var items = new List<T>();
while (_stack.Count > 0)
{
items.Add(_stack.Pop());
}
if (other is StackFrontier<T>)
{
items.Reverse();
}
foreach (var item in items)
{
other.Add(item);
}
}
}
/// <summary>
/// FIFO frontier – produces breadth-first traversal.
/// </summary>
public sealed class QueueFrontier<T> : IFrontier<T>
{
private readonly Queue<T> _queue = new();
public int Count => _queue.Count;
public void Add(T item) => _queue.Enqueue(item);
public T Remove() => _queue.Dequeue();
public void DrainTo(IFrontier<T> other)
{
// Dequeue gives items in priority order (highest-weight first).
var items = new List<T>();
while (_queue.Count > 0)
{
items.Add(_queue.Dequeue());
}
if (other is StackFrontier<T>)
{
items.Reverse();
}
foreach (var item in items)
{
other.Add(item);
}
}
}