-
Notifications
You must be signed in to change notification settings - Fork 703
Expand file tree
/
Copy pathIndexedPriorityQueue.cs
More file actions
323 lines (270 loc) · 10.5 KB
/
Copy pathIndexedPriorityQueue.cs
File metadata and controls
323 lines (270 loc) · 10.5 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Garnet.common.Collections
{
/// <summary>
/// In-place updatable min-heap. With methods to access priority in constant time.
/// </summary>
public class IndexedPriorityQueue<TElement, TPriority>
{
// element -> index in heap
private readonly Dictionary<TElement, int> _index;
private const int DefaultCapacity = 4;
// binary heap
private (TElement element, TPriority priority)[] _heap = [];
private int _count;
/// <summary>
/// Raw heap access to do iteration really fast
/// </summary>
public (TElement element, TPriority priority)[] RawHeap => _heap;
/// <summary>
/// Number of elements in the priority queue.
/// </summary>
public int Count => _count;
/// <summary>
/// Creates an IndexedPriorityQueue using the default equality comparer for elements.
/// </summary>
public IndexedPriorityQueue() : this(null) { }
/// <summary>
/// Creates an IndexedPriorityQueue using the specified equality comparer for elements.
/// </summary>
public IndexedPriorityQueue(IEqualityComparer<TElement> comparer)
{
_index = new Dictionary<TElement, int>(comparer);
}
/// <summary>
/// Determines whether the specified element exists in the priority queue.
/// </summary>
/// <param name="element">The element to look up.</param>
/// <returns><see langword="true"/> if the element exists; otherwise, <see langword="false"/>.</returns>
public bool Exists(TElement element) => _index.ContainsKey(element);
/// <summary>
/// O(log N) - Enqueue or update the priority of a key
/// </summary>
/// <param name="element"></param>
/// <param name="value"></param>
public void EnqueueOrUpdate(TElement element, TPriority value)
{
if (_index.TryGetValue(element, out int idxInHeap))
{
_index[element] = UpdateHeap(idxInHeap, value);
return;
}
_index[element] = InsertIntoHeap(element, value);
}
/// <summary>
/// O(log N) - Dequeue Key with Lowest Priority
/// </summary>
/// <returns>Element with lowest priority</returns>
public TElement Dequeue()
{
if (_count == 0)
throw new InvalidOperationException("The queue is empty.");
return DequeueFromHeap();
}
///
/// <summary>
/// O(1) - Try to peek at the element with the lowest priority
/// </summary>
/// <param name="key">The element with the lowest priority</param>
/// <param name="value">The priority of the element</param>
/// <returns>True if the queue is not empty, otherwise false</returns>
public bool TryPeek(out TElement key, out TPriority value)
{
if (_count == 0)
{
key = default!;
value = default!;
return false;
}
(key, value) = _heap[0];
return true;
}
/// <summary>
/// O(log N) - Change the priority of an element
/// </summary>
/// <param name="key">The element whose priority is to be changed</param>
/// <param name="newValue">The new priority value</param>
public void ChangePriority(TElement key, TPriority newValue) => _index[key] = UpdateHeap(_index[key], newValue);
/// <summary>
/// O(1) - Get the priority of an element
/// </summary>
/// <param name="key">The element whose priority is to be retrieved</param>
/// <param name="value">The priority of the element</param>
public void GetPriority(TElement key, out TPriority value) => value = _heap[_index[key]].priority;
/// <summary>
/// O(1) - Try to get the priority of an element. Returns false if the element is not in the queue.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool TryGetPriority(TElement key, out TPriority value)
{
if (_index.TryGetValue(key, out int idxInHeap))
{
value = _heap[idxInHeap].priority;
return true;
}
value = default!;
return false;
}
/// <summary>
/// O(log N) - Try to remove an element from the queue. Returns false if the element is not in the queue.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool TryRemove(TElement key)
{
if (!_index.TryGetValue(key, out int idxInHeap))
{
return false;
}
_index.Remove(key);
_count--;
if (idxInHeap != _count)
{
_heap[idxInHeap] = _heap[_count];
_index[_heap[idxInHeap].element] = idxInHeap;
// Try sifting down, if it doesn't move then try sifting up
if (SiftDown(idxInHeap) == idxInHeap)
{
SiftUp(idxInHeap);
}
}
_heap[_count] = default;
if (_heap.Length > DefaultCapacity && _count < _heap.Length / 2)
{
Shrink();
}
return true;
}
// helper - methods
private int InsertIntoHeap(TElement key, TPriority value)
{
if (_count == _heap.Length)
{
Grow(_count + 1);
}
_heap[_count] = (key, value);
_index[key] = _count;
_count++;
return SiftUp(_count - 1);
}
private TElement DequeueFromHeap()
{
TElement element = _heap[0].element;
_index.Remove(element);
_count--;
if (_count > 0)
{
_heap[0] = _heap[_count];
_heap[_count] = default;
_index[_heap[0].element] = 0;
SiftDown(0);
}
else
{
_heap[0] = default;
}
if (_heap.Length > DefaultCapacity && _count < _heap.Length / 2)
{
Shrink();
}
return element;
}
private int UpdateHeap(int idxInHeap, TPriority newValue)
{
TPriority oldValue = _heap[idxInHeap].priority;
TElement element = _heap[idxInHeap].element;
_heap[idxInHeap] = (element, newValue);
int cmp = Comparer<TPriority>.Default.Compare(newValue, oldValue);
if (cmp < 0)
{
// new priority is smaller – sift up
return SiftUp(idxInHeap);
}
else if (cmp > 0)
{
// new priority is larger – sift down
return SiftDown(idxInHeap);
}
return idxInHeap;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int SiftUp(int currIdx)
{
var entry = _heap[currIdx];
while (currIdx > 0)
{
int parentIdx = GetParentIndex(currIdx);
if (Comparer<TPriority>.Default.Compare(_heap[parentIdx].priority, entry.priority) <= 0)
break;
_heap[currIdx] = _heap[parentIdx];
_index[_heap[currIdx].element] = currIdx;
currIdx = parentIdx;
}
_heap[currIdx] = entry;
_index[entry.element] = currIdx;
return currIdx;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int SiftDown(int currIdx)
{
var entry = _heap[currIdx];
while (true)
{
int smallerChildIdx = GetLeftChildIndex(currIdx);
if (smallerChildIdx >= _count)
break;
int rightChildIdx = smallerChildIdx + 1;
if (rightChildIdx < _count && Comparer<TPriority>.Default.Compare(_heap[rightChildIdx].priority, _heap[smallerChildIdx].priority) < 0)
smallerChildIdx = rightChildIdx;
if (Comparer<TPriority>.Default.Compare(entry.priority, _heap[smallerChildIdx].priority) <= 0)
break;
_heap[currIdx] = _heap[smallerChildIdx];
_index[_heap[currIdx].element] = currIdx;
currIdx = smallerChildIdx;
}
_heap[currIdx] = entry;
_index[entry.element] = currIdx;
return currIdx;
}
private int GetParentIndex(int i) => (i - 1) / 2;
private int GetLeftChildIndex(int i) => (2 * i) + 1;
/// <summary>
/// Grows the priority queue to match the specified min capacity.
/// </summary>
private void Grow(int minCapacity)
{
Debug.Assert(_heap.Length < minCapacity);
const int GrowFactor = 2;
const int MinimumGrow = 4;
int newcapacity = GrowFactor * _heap.Length;
// Allow the queue to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _heap.Length overflowed thanks to the (uint) cast
if ((uint)newcapacity > Array.MaxLength) newcapacity = Array.MaxLength;
// Ensure minimum growth is respected.
newcapacity = Math.Max(newcapacity, _heap.Length + MinimumGrow);
// If the computed capacity is still less than specified, set to the original argument.
// Capacities exceeding Array.MaxLength will be surfaced as OutOfMemoryException by Array.Resize.
if (newcapacity < minCapacity) newcapacity = minCapacity;
Array.Resize(ref _heap, newcapacity);
}
/// <summary>
/// Shrinks the backing array when more than half the space is unoccupied.
/// </summary>
private void Shrink()
{
int newCapacity = _heap.Length / 2;
newCapacity = Math.Max(newCapacity, DefaultCapacity);
if (newCapacity < _heap.Length)
{
Array.Resize(ref _heap, newCapacity);
}
}
}
}