Skip to content

Commit

Permalink
Merge pull request #284 from vaskoz/day132
Browse files Browse the repository at this point in the history
Day132
  • Loading branch information
vaskoz committed Jan 6, 2019
2 parents 55052f0 + f56e4e5 commit c7bdb45
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 28 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go:
before_install:
- go get github.com/mattn/goveralls
script:
- go get ./...
- go test -cover -v -race ./...
- go test -bench=. ./...
- $GOPATH/bin/goveralls -service=travis-ci
12 changes: 6 additions & 6 deletions day132/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ type HitCounter interface {
// Record stores a timestamp.
Record(uint64)
// Total returns number of recorded events.
Total() uint64
Total() int
// Range returns the total counts inclusive.
Range(lower, upper uint64) uint64
Range(lower, upper uint64) int
}

// NewHitCounter returns a new instance of a HitCounter.
Expand All @@ -16,7 +16,7 @@ func NewHitCounter() HitCounter {
}

type hitCounter struct {
total uint64
total int
timestamps []uint64
}

Expand All @@ -25,12 +25,12 @@ func (hc *hitCounter) Record(ts uint64) {
hc.timestamps = append(hc.timestamps, ts)
}

func (hc *hitCounter) Total() uint64 {
func (hc *hitCounter) Total() int {
return hc.total
}

func (hc *hitCounter) Range(lower, upper uint64) uint64 {
var count uint64
func (hc *hitCounter) Range(lower, upper uint64) int {
var count int
for _, ts := range hc.timestamps {
if ts >= lower && ts <= upper {
count++
Expand Down
33 changes: 33 additions & 0 deletions day132/problem_faster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day132

import "github.com/algds/llrb"

// NewFasterHitCounter returns a new instance of a HitCounter.
func NewFasterHitCounter() HitCounter {
return &fasterHitCounter{timestamps: llrb.New(func(a, b llrb.Key) int {
aui := a.(uint64)
bui := b.(uint64)
if aui < bui {
return -1
} else if aui == bui {
return 0
}
return 1
})}
}

type fasterHitCounter struct {
timestamps llrb.Tree
}

func (fhc *fasterHitCounter) Record(ts uint64) {
llrb.Insert(fhc.timestamps, ts, ts)
}

func (fhc *fasterHitCounter) Total() int {
return fhc.timestamps.Size()
}

func (fhc *fasterHitCounter) Range(lower, upper uint64) int {
return llrb.RangeCount(fhc.timestamps, lower, upper)
}
29 changes: 29 additions & 0 deletions day132/problem_faster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package day132

import "testing"

func TestFasterHitCounter(t *testing.T) {
t.Parallel()
hc := NewFasterHitCounter()
for i := uint64(0); i < uint64(1000); i++ {
hc.Record(i)
}
if total := hc.Total(); total != 1000 {
t.Errorf("Expected 1000 got %v", total)
}
if size := hc.Range(500, 10000); size != 500 {
t.Errorf("Expected 500 got %v", size)
}
}

func BenchmarkFasterHitCounter(b *testing.B) {
hc := NewFasterHitCounter()
for i := uint64(0); i < uint64(100000); i++ {
hc.Record(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
hc.Total()
hc.Range(631, 632)
}
}
33 changes: 11 additions & 22 deletions day132/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,25 @@ import "testing"
func TestHitCounter(t *testing.T) {
t.Parallel()
hc := NewHitCounter()
hc.Record(10)
hc.Record(15)
hc.Record(20)
hc.Record(30)
if total := hc.Total(); total != 4 {
t.Errorf("Total should be 4 but got %v", total)
for i := uint64(0); i < uint64(1000); i++ {
hc.Record(i)
}
if r := hc.Range(0, 10); r != 1 {
t.Errorf("Expected 1 got %v", r)
if total := hc.Total(); total != 1000 {
t.Errorf("Expected 1000 got %v", total)
}
if r := hc.Range(10, 30); r != 4 {
t.Errorf("Expected 4 got %v", r)
}
if r := hc.Range(11, 29); r != 2 {
t.Errorf("Expected 2 got %v", r)
if size := hc.Range(500, 10000); size != 500 {
t.Errorf("Expected 500 got %v", size)
}
}

func BenchmarkHitCounter(b *testing.B) {
hc := NewHitCounter()
hc.Record(10)
hc.Record(15)
hc.Record(20)
hc.Record(30)

for i := uint64(0); i < uint64(100000); i++ {
hc.Record(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {

hc.Total()
hc.Range(0, 10)
hc.Range(10, 30)
hc.Range(11, 29)
hc.Range(631, 632)
}
}

0 comments on commit c7bdb45

Please sign in to comment.