in csharp/src/Containers/Heap.cs [246:287]
public TValue Pop()
{
version++;
if (count == 0) throw new Exception("Heap is empty");
if (isHeap)
{
TValue returnValue = elements[0];
TValue 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) < 0) swapIndex = left;
}
else
{
if (comparer.Compare(elements[left], elements[right]) < 0)
{
if (comparer.Compare(elements[left], value) < 0) swapIndex = left;
}
else if (comparer.Compare(elements[right], value) < 0) swapIndex = right;
}
if (swapIndex == current) break;
elements[current] = elements[swapIndex];
current = swapIndex;
}
elements[current] = value;
return returnValue;
}
else
{
count--;
return elements[count];
}
}