Skip to content

container/heap: use sort.Interface where possible #65967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/container/heap/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Interface interface {
// Init is idempotent with respect to the heap invariants
// and may be called whenever the heap invariants may have been invalidated.
// The complexity is O(n) where n = h.Len().
func Init(h Interface) {
func Init(h sort.Interface) {
// heapify
n := h.Len()
for i := n/2 - 1; i >= 0; i-- {
Expand Down Expand Up @@ -80,13 +80,13 @@ func Remove(h Interface, i int) any {
// Changing the value of the element at index i and then calling Fix is equivalent to,
// but less expensive than, calling [Remove](h, i) followed by a Push of the new value.
// The complexity is O(log n) where n = h.Len().
func Fix(h Interface, i int) {
func Fix(h sort.Interface, i int) {
if !down(h, i, h.Len()) {
up(h, i)
}
}

func up(h Interface, j int) {
func up(h sort.Interface, j int) {
for {
i := (j - 1) / 2 // parent
if i == j || !h.Less(j, i) {
Expand All @@ -97,7 +97,7 @@ func up(h Interface, j int) {
}
}

func down(h Interface, i0, n int) bool {
func down(h sort.Interface, i0, n int) bool {
i := i0
for {
j1 := 2*i + 1
Expand Down