-
Notifications
You must be signed in to change notification settings - Fork 671
/
heap.go
24 lines (20 loc) · 963 Bytes
/
heap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validator
import "container/heap"
// EndTimeHeap orders validators by EndTime from earliest to latest.
type EndTimeHeap []*Validator
func (h *EndTimeHeap) Len() int { return len(*h) }
func (h *EndTimeHeap) Less(i, j int) bool { return (*h)[i].EndTime().Before((*h)[j].EndTime()) }
func (h *EndTimeHeap) Swap(i, j int) { (*h)[i], (*h)[j] = (*h)[j], (*h)[i] }
func (h *EndTimeHeap) Add(validator *Validator) { heap.Push(h, validator) }
func (h *EndTimeHeap) Peek() *Validator { return (*h)[0] }
func (h *EndTimeHeap) Remove() *Validator { return heap.Pop(h).(*Validator) }
func (h *EndTimeHeap) Push(x interface{}) { *h = append(*h, x.(*Validator)) }
func (h *EndTimeHeap) Pop() interface{} {
newLen := len(*h) - 1
val := (*h)[newLen]
(*h)[newLen] = nil
*h = (*h)[:newLen]
return val
}