-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move heap code to internal for better testing
- Loading branch information
jmacd
committed
Nov 16, 2019
1 parent
b0dd5c7
commit f53b1a8
Showing
3 changed files
with
133 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2019, LightStep Inc. | ||
|
||
package internal | ||
|
||
type Vsample struct { | ||
Sample interface{} | ||
Weight float64 | ||
} | ||
|
||
type SampleHeap []Vsample | ||
|
||
func (sh *SampleHeap) Push(v Vsample) { | ||
l := append(*sh, v) | ||
n := len(l) - 1 | ||
|
||
// This copies the body of heap.up(). | ||
j := n | ||
for { | ||
i := (j - 1) / 2 // parent | ||
if i == j || l[j].Weight >= l[i].Weight { | ||
break | ||
} | ||
l[i], l[j] = l[j], l[i] | ||
j = i | ||
} | ||
|
||
*sh = l | ||
} | ||
|
||
func (sh *SampleHeap) Pop() Vsample { | ||
l := *sh | ||
n := len(l) - 1 | ||
result := l[0] | ||
l[0] = l[n] | ||
l = l[:n] | ||
|
||
// This copies the body of heap.down(). | ||
i := 0 | ||
for { | ||
j1 := 2*i + 1 | ||
if j1 >= n || j1 < 0 { // j1 < 0 after int overflow | ||
break | ||
} | ||
j := j1 // left child | ||
if j2 := j1 + 1; j2 < n && l[j2].Weight < l[j1].Weight { | ||
j = j2 // = 2*i + 2 // right child | ||
} | ||
if l[j].Weight >= l[i].Weight { | ||
break | ||
} | ||
l[i], l[j] = l[j], l[i] | ||
i = j | ||
} | ||
|
||
*sh = l | ||
return result | ||
} |
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,58 @@ | ||
// Copyright 2019, LightStep Inc. | ||
|
||
package internal_test | ||
|
||
import ( | ||
"container/heap" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/lightstep/varopt/internal" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type simpleHeap []float64 | ||
|
||
func (s *simpleHeap) Len() int { | ||
return len(*s) | ||
} | ||
|
||
func (s *simpleHeap) Swap(i, j int) { | ||
(*s)[i], (*s)[j] = (*s)[j], (*s)[i] | ||
} | ||
|
||
func (s *simpleHeap) Less(i, j int) bool { | ||
return (*s)[i] < (*s)[j] | ||
} | ||
|
||
func (s *simpleHeap) Push(x interface{}) { | ||
*s = append(*s, x.(float64)) | ||
} | ||
|
||
func (s *simpleHeap) Pop() interface{} { | ||
old := *s | ||
n := len(old) | ||
x := old[n-1] | ||
*s = old[0 : n-1] | ||
return x | ||
} | ||
|
||
func TestLargeHeap(t *testing.T) { | ||
var L internal.SampleHeap | ||
var S simpleHeap | ||
|
||
for i := 0; i < 1e6; i++ { | ||
v := rand.NormFloat64() | ||
L.Push(internal.Vsample{Weight: v}) | ||
heap.Push(&S, v) | ||
} | ||
|
||
for len(S) > 0 { | ||
v1 := heap.Pop(&S).(float64) | ||
v2 := L.Pop().Weight | ||
|
||
require.Equal(t, v1, v2) | ||
} | ||
|
||
require.Equal(t, 0, len(L)) | ||
} |
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