Skip to content

Commit

Permalink
refactor: put Uint64Heap at the end of servicecheck.go
Browse files Browse the repository at this point in the history
Signed-off-by: Clément Nussbaumer <[email protected]>
  • Loading branch information
clementnuss committed Mar 12, 2024
1 parent 270e208 commit 4950dd6
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions internal/servicecheck/neighbours.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,6 @@ func (c *Checker) checkNeighbours(nh []*Neighbour) {
}
}

type Uint64Heap []uint64

func (h Uint64Heap) Len() int { return len(h) }
func (h Uint64Heap) Less(i, j int) bool { return h[i] > h[j] } // we want a max-heap, therefore the inversed condition
func (h Uint64Heap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }

func (h *Uint64Heap) Push(x any) {
*h = append(*h, x.(uint64))
}

func (h *Uint64Heap) Pop() any {
n := len(*h)
x := (*h)[n-1]
*h = (*h)[0 : n-1]

return x
}

func (c *Checker) filterNeighbours(nh []*Neighbour) []*Neighbour {
m := make(map[uint64]*Neighbour, c.NeighbourLimit+1)

Expand Down Expand Up @@ -155,3 +137,21 @@ func sha256Uint64(s string) uint64 {
h := sha256.Sum256([]byte(s))
return binary.BigEndian.Uint64(h[:8])
}

type Uint64Heap []uint64

func (h Uint64Heap) Len() int { return len(h) }
func (h Uint64Heap) Less(i, j int) bool { return h[i] > h[j] } // we want a max-heap, therefore the inversed condition
func (h Uint64Heap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }

func (h *Uint64Heap) Push(x any) {
*h = append(*h, x.(uint64))
}

func (h *Uint64Heap) Pop() any {
n := len(*h)
x := (*h)[n-1]
*h = (*h)[0 : n-1]

return x
}

0 comments on commit 4950dd6

Please sign in to comment.