Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge branch 'aa/test-simulator' into feat/port-Caboose-main
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeanAsad committed Sep 18, 2023
2 parents 608a668 + ea1d62b commit 05c2b37
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions node_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,22 @@ func (nh *NodeHeap) PeekRandom() *Node {

func (nh *NodeHeap) TopN(n int) []*Node {
m := make([]*Node, 0, n)
temp := make([]*Node, 0, n)
nh.lk.Lock()
defer nh.lk.Unlock()

heap.Init(nh)
for i := 0; i < n && i < len(nh.Nodes); i++ {
node := nh.Nodes[i]
for i := 0; i < n && nh.Len() > 0; i++ {
item := heap.Pop(nh)
node := item.(*Node)
m = append(m, node)
temp = append(temp, node)
}

for _, node := range temp {
heap.Push(nh, node)
}

return m
}

Expand Down

0 comments on commit 05c2b37

Please sign in to comment.