Potential crasher fix. (Never triggered in UQM atm)
Also a small optimisation. git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3585 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
@@ -103,6 +103,8 @@ Heap_removeByIndex(Heap *heap, size_t i) {
|
|||||||
// Restore the heap invariant. We're shifting entries into the
|
// Restore the heap invariant. We're shifting entries into the
|
||||||
// gap that was created until we find the place where we can
|
// gap that was created until we find the place where we can
|
||||||
// insert the last entry.
|
// insert the last entry.
|
||||||
|
HeapValue *lastEntry = heap->entries[heap->numEntries];
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
size_t childI = i * 2 + 1;
|
size_t childI = i * 2 + 1;
|
||||||
// The two children are childI and 'childI + 1'.
|
// The two children are childI and 'childI + 1'.
|
||||||
@@ -114,7 +116,7 @@ Heap_removeByIndex(Heap *heap, size_t i) {
|
|||||||
// There is no left child either.
|
// There is no left child either.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (heap->comparator(heap->entries[childI + 1],
|
if (heap->comparator(heap->entries[childI + 1],
|
||||||
heap->entries[childI]) < 0) {
|
heap->entries[childI]) < 0) {
|
||||||
// The right child is the child with the lowest value.
|
// The right child is the child with the lowest value.
|
||||||
@@ -123,8 +125,7 @@ Heap_removeByIndex(Heap *heap, size_t i) {
|
|||||||
}
|
}
|
||||||
// childI is now the child with the lowest value.
|
// childI is now the child with the lowest value.
|
||||||
|
|
||||||
if (heap->comparator(heap->entries[heap->numEntries],
|
if (heap->comparator(lastEntry, heap->entries[childI]) <= 0) {
|
||||||
heap->entries[childI]) <= 0) {
|
|
||||||
// The last entry goes here.
|
// The last entry goes here.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -138,7 +139,7 @@ Heap_removeByIndex(Heap *heap, size_t i) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fill the gap with the last entry.
|
// Fill the gap with the last entry.
|
||||||
heap->entries[i] = heap->entries[heap->numEntries];
|
heap->entries[i] = lastEntry;
|
||||||
heap->entries[i]->index = i;
|
heap->entries[i]->index = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user