Skip to content

Commit 1210901

Browse files
committed
fix: Exception thrown when heapify empty queue (#32)
1 parent 80a28ed commit 1210901

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

Runtime/UnsafeMinPriorityQueue.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,7 @@ public void EnqueueRange(ReadOnlySpan<T> elements)
145145
{
146146
nodes.AddRangeNoResize(ptr, elements.Length);
147147
}
148-
if (Count > 1)
149-
{
150-
Heapify();
151-
}
148+
Heapify();
152149
}
153150
else
154151
{
@@ -180,7 +177,7 @@ private void RemoveRootNode()
180177
/// Gets the index of an element's parent.
181178
/// </summary>
182179
[return: AssumeRange(0, (int.MaxValue - 1) >> Log2Arity)]
183-
private static int GetParentIndex(int index) => (index - 1) >> Log2Arity;
180+
private static int GetParentIndex([AssumeRange(1, int.MaxValue)] int index) => (index - 1) >> Log2Arity;
184181

185182
/// <summary>
186183
/// Gets the index of the first child of an element.
@@ -192,6 +189,11 @@ private void RemoveRootNode()
192189
/// </summary>
193190
internal void Heapify()
194191
{
192+
if(nodes.Length <= 1)
193+
{
194+
return;
195+
}
196+
195197
// Leaves of the tree are in fact 1-element heaps, for which there
196198
// is no need to correct them. The heap property needs to be restored
197199
// only for higher nodes, starting from the first node that has children.

0 commit comments

Comments
 (0)