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

Commit

Permalink
fix: top N selection
Browse files Browse the repository at this point in the history
  • Loading branch information
AmeanAsad committed Sep 18, 2023
1 parent 61c82da commit ea1d62b
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 ea1d62b

Please sign in to comment.