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