forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyedPriorityQueue.cs
295 lines (251 loc) · 8.54 KB
/
KeyedPriorityQueue.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
namespace DataStructures.Heaps
{
/// <summary>
/// Implements the Keyed Priority Queue Data Structure.
/// All nodes have: a Key, a Value, a Priority
/// <typeparam name="K">Node's Key type</typeparam>
/// <typeparam name="V">Node's Value type</typeparam>
/// <typeparam name="P">Node's Priority type</typeparam>
/// </summary>
public class PriorityQueue<K, V, P> where P : IComparable<P>
{
/// <summary>
/// Instance variables
/// </summary>
private BinaryMaxHeap<PriorityQueueNode<K, V, P>> _heap { get; set; }
private Comparer<PriorityQueueNode<K, V, P>> _priorityComparer { get; set; }
private Dictionary<K, int> _keysMap { get; set; }
/// <summary>
/// CONSTRUCTOR
/// </summary>
public PriorityQueue() : this(0, null) { }
/// <summary>
/// CONSTRUCTOR
/// </summary>
/// <param name="capacity">Capacity of priority queue.</param>
public PriorityQueue(int capacity) : this(capacity, null) { }
/// <summary>
/// CONSTRUCTOR
/// </summary>
/// <param name="capacity">Capacity of priority queue.</param>
/// <param name="priorityComparer">The node's priority comparer.</param>
public PriorityQueue(int capacity, Comparer<PriorityQueueNode<K, V, P>> priorityComparer)
{
if (capacity >= 0)
{
if (priorityComparer == null)
{
_priorityComparer = Comparer<PriorityQueueNode<K, V, P>>.Default;
}
else
{
_priorityComparer = priorityComparer;
}
_heap = new BinaryMaxHeap<PriorityQueueNode<K, V, P>>(capacity, this._priorityComparer);
_keysMap = new Dictionary<K, int>();
}
else
{
throw new ArgumentOutOfRangeException("Please provide a valid capacity.");
}
}
/// <summary>
/// Returns the count of elements in the queue.
/// </summary>
public int Count
{
get { return _heap.Count; }
}
/// <summary>
/// Checks if the queue is empty
/// <returns>True if queue is empty; false otherwise.</returns>
/// </summary>
public bool IsEmpty
{
get { return _heap.IsEmpty; }
}
/// <summary>
/// Returns an array of keys
/// </summary>
public K[] Keys
{
get
{
var keysArray = new K[_keysMap.Count];
_keysMap.Keys.CopyTo(keysArray, 0);
return keysArray;
}
}
/// <summary>
/// Returns the highest priority element.
/// </summary>
/// <returns>The at highest priority.</returns>
public V PeekAtHighestPriority()
{
if (_heap.IsEmpty)
{
throw new ArgumentOutOfRangeException("Queue is empty.");
}
return _heap.Peek().Value;
}
/// <summary>
/// Enqueue the specified key and value without priority.
/// </summary>
/// <param name="key">Key.</param>
/// <param name="value">Value.</param>
public void Enqueue(K key, V value)
{
Enqueue(key, value, default(P));
}
/// <summary>
/// Enqueue the specified key, value and priority.
/// </summary>
/// <param name="key">Key.</param>
/// <param name="value">Value.</param>
/// <param name="priority">Priority.</param>
public void Enqueue(K key, V value, P priority)
{
if (!_keysMap.ContainsKey(key))
{
_keysMap.Add(key, 1);
}
else
{
_keysMap[key] += 1;
}
var newNode = new PriorityQueueNode<K, V, P>(key, value, priority);
_heap.Add(newNode);
}
/// <summary>
/// Dequeue this instance.
/// </summary>
public V Dequeue()
{
if (_heap.IsEmpty)
{
throw new ArgumentOutOfRangeException("Queue is empty.");
}
var highest = _heap.Peek();
// Decrement the key's counter
_keysMap[highest.Key] = _keysMap[highest.Key] - 1;
if (_keysMap[highest.Key] == 0)
{
_keysMap.Remove(highest.Key);
}
_heap.RemoveMax();
return highest.Value;
}
/// <summary>
/// Sets the priority.
/// </summary>
/// <param name="key">Key.</param>
/// <param name="newPriority">New priority.</param>
public void SetPriority(K key, P newPriority)
{
// Handle boundaries errors
if (_heap.IsEmpty)
{
throw new ArgumentOutOfRangeException("Queue is empty.");
}
else if (!_keysMap.ContainsKey(key))
{
throw new KeyNotFoundException();
}
var keyComparer = Comparer<K>.Default;
for (int i = 0; i < _heap.Count; ++i)
{
if (keyComparer.Compare(_heap[i].Key, key) == 0)
{
_heap[i].Priority = newPriority;
break;
}
}
}
/// <summary>
/// Clear this priority queue.
/// </summary>
public void Clear()
{
_heap.Clear();
}
///// <summary>
///// Removes the node that has the specified key.
///// </summary>
///// <param name="key">Key.</param>
//public void Remove(K key)
//{
// if (_heap.IsEmpty)
// {
// throw new ArgumentOutOfRangeException ("Queue is empty.");
// }
//
// var keyComparer = Comparer<K>.Default;
//
// Predicate<PriorityQueueNode<K, V, P>> match =
// new Predicate<PriorityQueueNode<K, V, P>> (
// item => keyComparer.Compare(item.Key, key) == 0);
//
// _heap.RemoveAll (match);
//}
///// <summary>
///// Removes the node that has the specified key and value.
///// </summary>
///// <param name="key">Key.</param>
///// <param name="value">Value.</param>
//public void Remove(K key, V value)
//{
// if (_heap.IsEmpty)
// {
// throw new ArgumentOutOfRangeException ("Queue is empty.");
// }
//
// var keyComparer = Comparer<K>.Default;
// var valueComparer = Comparer<V>.Default;
//
// Predicate<PriorityQueueNode<K, V, P>> match =
// new Predicate<PriorityQueueNode<K, V, P>> (
// item =>
// keyComparer.Compare(item.Key, key) == 0 &&
// valueComparer.Compare(item.Value, value) == 0);
//
// _heap.RemoveAll (match);
//}
}
/// <summary>
/// The Priority-queue node.
/// </summary>
/// <typeparam name="K">Node's Key type</typeparam>
/// <typeparam name="V">Node's Value type</typeparam>
/// <typeparam name="P">Node's Priority type</typeparam>
public class PriorityQueueNode<K, V, P> : IComparable<PriorityQueueNode<K, V, P>> where P : IComparable<P>
{
public K Key { get; set; }
public V Value { get; set; }
public P Priority { get; set; }
public PriorityQueueNode() : this(default(K), default(V), default(P)) { }
public PriorityQueueNode(K key, V value, P priority)
{
this.Key = key;
this.Value = value;
this.Priority = priority;
}
public int CompareTo(PriorityQueueNode<K, V, P> other)
{
if (other == null)
return -1;
return this.Priority.CompareTo(other.Priority);
}
}//end-of-node-class
/// <summary>
/// Keyed Priority-queue node comparer.
/// </summary>
public class PriorityQueueNodeComparer<K, V, P> : IComparer<PriorityQueueNode<K, V, P>> where P : IComparable<P>
{
public int Compare(PriorityQueueNode<K, V, P> first, PriorityQueueNode<K, V, P> second)
{
return first.Priority.CompareTo(second.Priority);
}
}//end-of-comparer-class
}