2023-04-23 14:49:09 +02:00
|
|
|
namespace Pathfinding;
|
|
|
|
|
|
|
|
public class RPriorityQueue<TKey, TPriority> where TKey : notnull
|
|
|
|
{
|
|
|
|
public Dictionary<TKey, TPriority> queue;
|
2023-05-17 19:00:47 +02:00
|
|
|
public int Count => queue.Count;
|
2023-04-23 14:49:09 +02:00
|
|
|
|
|
|
|
public RPriorityQueue()
|
|
|
|
{
|
|
|
|
queue = new();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Enqueue(TKey key, TPriority priority)
|
|
|
|
{
|
|
|
|
if (!queue.TryAdd(key, priority))
|
|
|
|
queue[key] = priority;
|
|
|
|
}
|
|
|
|
|
2023-05-17 19:01:00 +02:00
|
|
|
public TKey Dequeue()
|
2023-04-23 14:49:09 +02:00
|
|
|
{
|
2023-05-17 19:01:00 +02:00
|
|
|
TKey retKey = queue.MinBy(item => item.Value).Key;
|
2023-04-23 14:49:09 +02:00
|
|
|
queue.Remove(retKey);
|
|
|
|
return retKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Remove(IEnumerable<TKey> elements)
|
|
|
|
{
|
|
|
|
int before = Count;
|
|
|
|
queue = queue.Where(queueitem => !elements.Contains(queueitem.Key))
|
|
|
|
.ToDictionary(item => item.Key, item => item.Value);
|
|
|
|
return before - Count;
|
|
|
|
}
|
2023-04-24 19:37:25 +02:00
|
|
|
|
|
|
|
public int RemoveExcept(IEnumerable<TKey> exceptKeys)
|
|
|
|
{
|
|
|
|
int before = Count;
|
|
|
|
queue = queue.IntersectBy(exceptKeys, item => item.Key).ToDictionary(item => item.Key, item => item.Value);
|
|
|
|
return before - Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Clear()
|
|
|
|
{
|
|
|
|
int before = Count;
|
|
|
|
queue.Clear();
|
|
|
|
return before;
|
|
|
|
}
|
2023-04-23 14:49:09 +02:00
|
|
|
}
|