-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.cs
46 lines (38 loc) · 1.03 KB
/
PriorityQueue.cs
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
namespace rac.so.bytehaven
{
public class PriorityQueue<T>
{
private readonly BinaryHeapWithMap<T> heap;
public PriorityQueue(int capacity, bool isMaxQueue = false)
{
Func<int, int, int> comparer = isMaxQueue ? (a, b) => b - a : (a, b) => a - b;
heap = new BinaryHeapWithMap<T>(capacity, comparer);
}
public void Enqueue(int priority, T value)
{
heap.Push(value, priority);
}
public (T Value, int priority) Dequeue()
{
return heap.Pop();
}
public (T Value, int priority) Peek()
{
return heap.Peek();
}
public bool Remove(T item)
{
return heap.Remove(item);
}
public void SetPriority(T item, int priority)
{
Remove(item);
Enqueue(priority, item);
}
public bool Contains(T item)
{
return heap.Contains(item);
}
public int Count => heap.Count;
}
}