in csharp/src/Containers/HeapWithAttachments.cs [279:320]
public HeapEntry Pop()
{
version++;
if (count == 0) throw new Exception("Heap is empty");
if (isHeap)
{
HeapEntry returnValue = elements[0];
HeapEntry value = elements[count - 1];
count--;
int current = 0;
while (current < count)
{
int left = (current << 1) + 1;
int right = (current << 1) + 2;
int swapIndex = current;
if (left >= count) swapIndex = current;
else
if (right >= count)
{
if (comparer.Compare(elements[left].Value, value.Value) < 0) swapIndex = left;
}
else
{
if (comparer.Compare(elements[left].Value, elements[right].Value) < 0)
{
if (comparer.Compare(elements[left].Value, value.Value) < 0) swapIndex = left;
}
else if (comparer.Compare(elements[right].Value, value.Value) < 0) swapIndex = right;
}
if (swapIndex == current) break;
elements[current] = elements[swapIndex];
current = swapIndex;
}
elements[current] = value;
return returnValue;
}
else
{
count--;
return elements[count];
}
}