-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5901 from onflow/bastian/improve-migration-schedu…
…ling Improve scheduling in account-based migration
- Loading branch information
Showing
5 changed files
with
243 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package util | ||
|
||
import ( | ||
"container/heap" | ||
) | ||
|
||
// TopN keeps track of the top N elements. | ||
// Use Add to add elements to the list. | ||
type TopN[T any] struct { | ||
Tree []T | ||
N int | ||
IsLess func(T, T) bool | ||
} | ||
|
||
func NewTopN[T any](n int, isLess func(T, T) bool) *TopN[T] { | ||
return &TopN[T]{ | ||
Tree: make([]T, 0, n), | ||
N: n, | ||
IsLess: isLess, | ||
} | ||
} | ||
|
||
func (h *TopN[T]) Len() int { | ||
return len(h.Tree) | ||
} | ||
|
||
func (h *TopN[T]) Less(i, j int) bool { | ||
a := h.Tree[i] | ||
b := h.Tree[j] | ||
return h.IsLess(a, b) | ||
} | ||
|
||
func (h *TopN[T]) Swap(i, j int) { | ||
h.Tree[i], h.Tree[j] = | ||
h.Tree[j], h.Tree[i] | ||
} | ||
|
||
func (h *TopN[T]) Push(x interface{}) { | ||
h.Tree = append(h.Tree, x.(T)) | ||
} | ||
|
||
func (h *TopN[T]) Pop() interface{} { | ||
tree := h.Tree | ||
count := len(tree) | ||
lastIndex := count - 1 | ||
last := tree[lastIndex] | ||
var empty T | ||
tree[lastIndex] = empty | ||
h.Tree = tree[0:lastIndex] | ||
return last | ||
} | ||
|
||
// Add tries to add a value to the list. | ||
// If the list is full, it will return the smallest value and true. | ||
// If the list is not full, it will return the zero value and false. | ||
func (h *TopN[T]) Add(value T) (popped T, didPop bool) { | ||
heap.Push(h, value) | ||
if h.Len() > h.N { | ||
popped := heap.Pop(h).(T) | ||
return popped, true | ||
} | ||
var empty T | ||
return empty, false | ||
} |
Oops, something went wrong.