-
Notifications
You must be signed in to change notification settings - Fork 663
/
heap.go
76 lines (64 loc) · 1.33 KB
/
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txheap
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/heap"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)
type Heap interface {
Add(tx *txs.Tx)
Get(txID ids.ID) *txs.Tx
List() []*txs.Tx
Remove(txID ids.ID) *txs.Tx
Peek() *txs.Tx
RemoveTop() *txs.Tx
Len() int
}
type heapTx struct {
tx *txs.Tx
age int
}
type txHeap struct {
heap heap.Map[ids.ID, heapTx]
currentAge int
}
func (h *txHeap) Add(tx *txs.Tx) {
txID := tx.ID()
if h.heap.Contains(txID) {
return
}
htx := heapTx{
tx: tx,
age: h.currentAge,
}
h.currentAge++
h.heap.Push(txID, htx)
}
func (h *txHeap) Get(txID ids.ID) *txs.Tx {
got, _ := h.heap.Get(txID)
return got.tx
}
func (h *txHeap) List() []*txs.Tx {
heapTxs := heap.MapValues(h.heap)
res := make([]*txs.Tx, 0, len(heapTxs))
for _, tx := range heapTxs {
res = append(res, tx.tx)
}
return res
}
func (h *txHeap) Remove(txID ids.ID) *txs.Tx {
removed, _ := h.heap.Remove(txID)
return removed.tx
}
func (h *txHeap) Peek() *txs.Tx {
_, peeked, _ := h.heap.Peek()
return peeked.tx
}
func (h *txHeap) RemoveTop() *txs.Tx {
_, popped, _ := h.heap.Pop()
return popped.tx
}
func (h *txHeap) Len() int {
return h.heap.Len()
}