Skip to content
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

Move simple into subpackage #6

Merged
merged 1 commit into from
Nov 8, 2019
Merged
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ jobs:
echo 'export GOPATH="$HOME/go"' >> $BASH_ENV
source $BASH_ENV
- checkout
- run: go test
- run: go test -v ./...

workflows:
version: 2
16 changes: 9 additions & 7 deletions simple.go → simple/simple.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright 2019, LightStep Inc.

package varopt
package simple

import (
"math/rand"

"github.com/lightstep/varopt"
)

// Simple implements unweighted reservoir sampling using Algorithm R
@@ -12,13 +14,13 @@ import (
type Simple struct {
capacity int
observed int
buffer []Sample
buffer []varopt.Sample
rnd *rand.Rand
}

// NewSimple returns a simple reservoir sampler with given capacity
// New returns a simple reservoir sampler with given capacity
// (i.e., reservoir size) and random number generator.
func NewSimple(capacity int, rnd *rand.Rand) *Simple {
func New(capacity int, rnd *rand.Rand) *Simple {
return &Simple{
capacity: capacity,
rnd: rnd,
@@ -27,11 +29,11 @@ func NewSimple(capacity int, rnd *rand.Rand) *Simple {

// Add considers a new observation for the sample. Items have unit
// weight.
func (s *Simple) Add(span Sample) {
func (s *Simple) Add(span varopt.Sample) {
s.observed++

if s.buffer == nil {
s.buffer = make([]Sample, 0, s.capacity)
s.buffer = make([]varopt.Sample, 0, s.capacity)
}

if len(s.buffer) < s.capacity {
@@ -47,7 +49,7 @@ func (s *Simple) Add(span Sample) {
}

// Get returns the i'th selected item from the sample.
func (s *Simple) Get(i int) Sample {
func (s *Simple) Get(i int) varopt.Sample {
return s.buffer[i]
}

41 changes: 0 additions & 41 deletions simple_test.go

This file was deleted.

11 changes: 6 additions & 5 deletions varopt_test.go
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import (
"testing"

"github.com/lightstep/varopt"
"github.com/lightstep/varopt/simple"
"github.com/stretchr/testify/require"
)

@@ -106,15 +107,15 @@ func testUnbiased(t *testing.T, bbr, bsr float64) {

for _, blockList := range blockLists {
for _, block := range blockList {
simple := varopt.NewSimple(sampleSize, rnd)
ss := simple.New(sampleSize, rnd)

for _, s := range block {
simple.Add(s)
ss.Add(s)
}

weight := simple.Weight()
for i := 0; i < simple.Size(); i++ {
vsample.Add(simple.Get(i), weight)
weight := ss.Weight()
for i := 0; i < ss.Size(); i++ {
vsample.Add(ss.Get(i), weight)
}
}
}