Skip to content

Commit

Permalink
fix: fix heap test
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Jun 30, 2024
1 parent c8b1720 commit a0e53b5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
6 changes: 3 additions & 3 deletions go/heap/example_intheap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func (h *IntHeap) Pop() int {
// and removes them in order of priority.
func Example_intHeap() {
h := &IntHeap{2, 1, 5}
heap.Init[int](h)
heap.Push[int](h, 3)
heap.Init(h)
heap.Push(h, 3)
fmt.Printf("minimum: %d\n", (*h)[0])
for h.Len() > 0 {
fmt.Printf("%d ", heap.Pop[int](h))
fmt.Printf("%d ", heap.Pop(h))
}
// Output:
// minimum: 1
Expand Down
8 changes: 4 additions & 4 deletions go/heap/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import "sort"
// for a type using the routines in this package.
// Any type that implements it may be used as a
// min-heap with the following invariants (established after
// Init has been called or if the data is empty or sorted):
// [Init] has been called or if the data is empty or sorted):
//
// !h.Less(j, i) for 0 <= i < h.Len() and 2*i+1 <= j <= 2*i+2 and j < h.Len()
//
// Note that Push and Pop in this interface are for package heap's
// Note that [Push] and [Pop] in this interface are for package heap's
// implementation to call. To add and remove things from the heap,
// use heap.Push and heap.Pop.
// use [heap.Push] and [heap.Pop].
type Interface[T any] interface {
sort.Interface
Push(x T) // add x as element Len()
Expand Down Expand Up @@ -78,7 +78,7 @@ func Remove[T any](h Interface[T], i int) T {

// Fix re-establishes the heap ordering after the element at index i has changed its value.
// 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.
// 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[T any](h Interface[T], i int) {
if !down(h, i, h.Len()) {
Expand Down
30 changes: 15 additions & 15 deletions go/heap/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestInit0(t *testing.T) {
for i := 20; i > 0; i-- {
h.Push(0) // all elements are the same
}
Init[int](h)
Init(h)
h.verify(t, 0)

for i := 1; h.Len() > 0; i++ {
Expand All @@ -75,11 +75,11 @@ func TestInit1(t *testing.T) {
for i := 20; i > 0; i-- {
h.Push(i) // all elements are different
}
Init[int](h)
Init(h)
h.verify(t, 0)

for i := 1; h.Len() > 0; i++ {
x := Pop[int](h)
x := Pop(h)
h.verify(t, 0)
if x != i {
t.Errorf("%d.th pop got %d; want %d", i, x, i)
Expand All @@ -94,18 +94,18 @@ func Test(t *testing.T) {
for i := 20; i > 10; i-- {
h.Push(i)
}
Init[int](h)
Init(h)
h.verify(t, 0)

for i := 10; i > 0; i-- {
Push[int](h, i)
Push(h, i)
h.verify(t, 0)
}

for i := 1; h.Len() > 0; i++ {
x := Pop[int](h)
x := Pop(h)
if i < 20 {
Push[int](h, 20+i)
Push(h, 20+i)
}
h.verify(t, 0)
if x != i {
Expand All @@ -123,7 +123,7 @@ func TestRemove0(t *testing.T) {

for h.Len() > 0 {
i := h.Len() - 1
x := Remove[int](h, i)
x := Remove(h, i)
if x != i {
t.Errorf("Remove(%d) got %d; want %d", i, x, i)
}
Expand All @@ -139,7 +139,7 @@ func TestRemove1(t *testing.T) {
h.verify(t, 0)

for i := 0; h.Len() > 0; i++ {
x := Remove[int](h, 0)
x := Remove(h, 0)
if x != i {
t.Errorf("Remove(0) got %d; want %d", x, i)
}
Expand All @@ -158,7 +158,7 @@ func TestRemove2(t *testing.T) {

m := make(map[int]bool)
for h.Len() > 0 {
m[Remove[int](h, (h.Len()-1)/2)] = true
m[Remove(h, (h.Len()-1)/2)] = true
h.verify(t, 0)
}

Expand All @@ -177,10 +177,10 @@ func BenchmarkDup(b *testing.B) {
h := make(myHeap, 0, n)
for i := 0; i < b.N; i++ {
for j := 0; j < n; j++ {
Push[int](&h, 0) // all elements are the same
Push(&h, 0) // all elements are the same
}
for h.Len() > 0 {
Pop[int](&h)
Pop(&h)
}
}
}
Expand All @@ -190,15 +190,15 @@ func TestFix(t *testing.T) {
h.verify(t, 0)

for i := 200; i > 0; i -= 10 {
Push[int](h, i)
Push(h, i)
}
h.verify(t, 0)

if (*h)[0] != 10 {
t.Fatalf("Expected head to be 10, was %d", (*h)[0])
}
(*h)[0] = 210
Fix[int](h, 0)
Fix(h, 0)
h.verify(t, 0)

for i := 100; i > 0; i-- {
Expand All @@ -208,7 +208,7 @@ func TestFix(t *testing.T) {
} else {
(*h)[elem] /= 2
}
Fix[int](h, elem)
Fix(h, elem)
h.verify(t, 0)
}
}

0 comments on commit a0e53b5

Please sign in to comment.