Skip to content

Commit

Permalink
Revert unrelated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Apr 6, 2021
1 parent 5fb47c2 commit 27f083f
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions packages/scheduler/src/SchedulerMinHeap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,65 @@ export function pop(heap: Heap): Node | null {
return null;
}
const first = heap[0];

const last = heap.pop();

if (last !== first) {
heap[0] = last;
siftDown(heap, last, 0);
}

return first;
}

function siftUp(heap, node, i) {
let index = i;

while (index > 0) {
const parentIndex = (index - 1) >>> 1;
const parent = heap[parentIndex];

if (compare(parent, node) < 0) {
break;
if (compare(parent, node) > 0) {
// The parent is larger. Swap positions.
heap[parentIndex] = node;
heap[index] = parent;
index = parentIndex;
} else {
// The parent is smaller. Exit.
return;
}
swap(heap, parentIndex, index);
index = parentIndex;
}
}

function siftDown(heap, node, i) {
let index = i;
const halfLength = heap.length >>> 1;

const length = heap.length;
const halfLength = length >>> 1;
while (index < halfLength) {
let bestIndex = index * 2 + 1;
const rightIndex = index * 2 + 2;

// If the right node is smaller, swap with the smaller of those.
if (
heap.length > rightIndex &&
compare(heap[rightIndex], heap[bestIndex]) < 0
) {
bestIndex = rightIndex;
}

if (compare(node, heap[bestIndex]) < 0) {
break;
const leftIndex = (index + 1) * 2 - 1;
const left = heap[leftIndex];
const rightIndex = leftIndex + 1;
const right = heap[rightIndex];

// If the left or right node is smaller, swap with the smaller of those.
if (compare(left, node) < 0) {
if (rightIndex < length && compare(right, left) < 0) {
heap[index] = right;
heap[rightIndex] = node;
index = rightIndex;
} else {
heap[index] = left;
heap[leftIndex] = node;
index = leftIndex;
}
} else {
if (rightIndex < length && compare(right, node) < 0) {
heap[index] = right;
heap[rightIndex] = node;
index = rightIndex;
} else {
// Neither child is smaller. Exit.
return;
}
}

swap(heap, bestIndex, index);
index = bestIndex;
}
}

function swap(heap, left, right) {
const item = heap[left];
heap[left] = heap[right];
heap[right] = item;
}

function compare(a, b) {
// Compare sort index first, then task id.
const diff = a.sortIndex - b.sortIndex;
Expand Down

0 comments on commit 27f083f

Please sign in to comment.