MCPcopy
hub / github.com/CodebuffAI/codebuff / siftDown

Method siftDown

common/src/util/min-heap.ts:36–63  ·  view source on GitHub ↗
(index: number)

Source from the content-addressed store, hash-verified

34 }
35
36 private siftDown(index: number): void {
37 while (true) {
38 let minIndex = index
39 const leftChild = this.getLeftChildIndex(index)
40 const rightChild = this.getRightChildIndex(index)
41
42 if (
43 leftChild < this.heap.length &&
44 this.heap[leftChild].score < this.heap[minIndex].score
45 ) {
46 minIndex = leftChild
47 }
48
49 if (
50 rightChild < this.heap.length &&
51 this.heap[rightChild].score < this.heap[minIndex].score
52 ) {
53 minIndex = rightChild
54 }
55
56 if (minIndex === index) {
57 break
58 }
59
60 this.swap(index, minIndex)
61 index = minIndex
62 }
63 }
64
65 insert(item: T, score: number): void {
66 this.heap.push({ item, score })

Callers 1

extractMinMethod · 0.95

Calls 3

getLeftChildIndexMethod · 0.95
getRightChildIndexMethod · 0.95
swapMethod · 0.95

Tested by

no test coverage detected