Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 10, 2023
1 parent fc7bcdc commit a38dee7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 87 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,11 @@ goos: linux
goarch: amd64
pkg: github.com/gaissmai/interval
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
BenchmarkShortestIn1-8 190550422 6.2 ns/op 0 B/op 0 allocs/op
BenchmarkShortestIn10-8 20306680 53.1 ns/op 0 B/op 0 allocs/op
BenchmarkShortestIn100-8 5832771 197.6 ns/op 0 B/op 0 allocs/op
BenchmarkShortestIn1_000-8 3890366 420.6 ns/op 0 B/op 0 allocs/op
BenchmarkShortestIn10_000-8 3306588 425.0 ns/op 0 B/op 0 allocs/op
BenchmarkShortestIn100_000-8 2542274 668.7 ns/op 0 B/op 0 allocs/op
BenchmarkShortestIn1_000_000-8 1763833 995.5 ns/op 0 B/op 0 allocs/op
BenchmarkShortest/In100-8 5974706 193.1 ns/op 0 B/op 0 allocs/op
BenchmarkShortest/In1_000-8 4827165 248.6 ns/op 0 B/op 0 allocs/op
BenchmarkShortest/In10_000-8 2477786 482.7 ns/op 0 B/op 0 allocs/op
BenchmarkShortest/In100_000-8 4965262 242.0 ns/op 0 B/op 0 allocs/op
BenchmarkShortest/In1_000_000-8 2285011 527.3 ns/op 0 B/op 0 allocs/op
```

... and so the benchmark for `Largest()`:
Expand All @@ -168,11 +166,9 @@ goos: linux
goarch: amd64
pkg: github.com/gaissmai/interval
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
BenchmarkLargestIn1-8 67820457 17.6 ns/op 0 B/op 0 allocs/op
BenchmarkLargestIn10-8 25577661 113.0 ns/op 0 B/op 0 allocs/op
BenchmarkLargestIn100-8 32839588 63.8 ns/op 0 B/op 0 allocs/op
BenchmarkLargestIn1_000-8 16319536 115.9 ns/op 0 B/op 0 allocs/op
BenchmarkLargestIn10_000-8 14414833 90.5 ns/op 0 B/op 0 allocs/op
BenchmarkLargestIn100_000-8 8502637 161.6 ns/op 0 B/op 0 allocs/op
BenchmarkLargestIn1_000_000-8 7168594 387.4 ns/op 0 B/op 0 allocs/op
BenchmarkLargest/In100-8 14285817 77.25 ns/op 0 B/op 0 allocs/op
BenchmarkLargest/In1_000-8 12253501 95.82 ns/op 0 B/op 0 allocs/op
BenchmarkLargest/In10_000-8 8254034 145.5 ns/op 0 B/op 0 allocs/op
BenchmarkLargest/In100_000-8 6909498 174.2 ns/op 0 B/op 0 allocs/op
BenchmarkLargest/In1_000_000-8 6963052 160.7 ns/op 0 B/op 0 allocs/op
```
26 changes: 24 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,33 @@ import (
"os"

"github.com/gaissmai/interval"
"github.com/gaissmai/interval/internal/period"
)

// fmt.Stringer for formattting, not required for interval.Interface
func (p Ival) String() string {
return fmt.Sprintf("%d...%d", p[0], p[1])
}

// little helper, compare two ints
func cmp(a, b int) int {
switch {
case a == b:
return 0
case a < b:
return -1
}
return 1
}

// example interval
type Ival [2]int

// implement interval.Interface
func (p Ival) CompareLower(q Ival) int { return cmp(p[0], q[0]) }
func (p Ival) CompareUpper(q Ival) int { return cmp(p[1], q[1]) }

// example data
var periods = []period.Ival{
var periods = []Ival{
{3, 4},
{2, 9},
{7, 9},
Expand Down
26 changes: 0 additions & 26 deletions internal/period/period.go

This file was deleted.

89 changes: 44 additions & 45 deletions treap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"testing"

"github.com/gaissmai/interval"
"github.com/gaissmai/interval/internal/period"
)

// test data
var ps = []period.Ival{
var ps = []Ival{
{0, 6},
{0, 5},
{1, 8},
Expand All @@ -29,24 +28,24 @@ var ps = []period.Ival{
}

// random test data
func generateIvals(n int) []period.Ival {
is := make([]period.Ival, n)
func generateIvals(n int) []Ival {
is := make([]Ival, n)
for i := 0; i < n; i++ {
a := rand.Int()
b := rand.Int()
if a > b {
a, b = b, a
}
is[i] = period.Ival{a, b}
is[i] = Ival{a, b}
}
return is
}

func TestNewTree(t *testing.T) {
t.Parallel()

var zeroItem period.Ival
var zeroTree interval.Tree[period.Ival]
var zeroItem Ival
var zeroTree interval.Tree[Ival]

w := new(strings.Builder)
if err := zeroTree.Fprint(w); err != nil {
Expand Down Expand Up @@ -102,8 +101,8 @@ func TestNewTree(t *testing.T) {
t.Errorf("Max(), got: %v, want: %v", s, zeroItem)
}

var items []period.Ival
zeroTree.Visit(zeroItem, zeroItem, func(item period.Ival) bool {
var items []Ival
zeroTree.Visit(zeroItem, zeroItem, func(item Ival) bool {
items = append(items, item)
return true
})
Expand All @@ -115,7 +114,7 @@ func TestNewTree(t *testing.T) {
func TestTreeWithDups(t *testing.T) {
t.Parallel()

is := []period.Ival{
is := []Ival{
{0, 100},
{41, 102},
{42, 67},
Expand Down Expand Up @@ -160,7 +159,7 @@ func TestImmutable(t *testing.T) {
t.Fatal("Delete changed receiver")
}

item := period.Ival{-111, 666}
item := Ival{-111, 666}
_ = tree1.Insert(item)
if !reflect.DeepEqual(tree1, tree2) {
t.Fatal("Insert changed receiver")
Expand Down Expand Up @@ -207,58 +206,58 @@ func TestLookup(t *testing.T) {
// │ └─ 6...7
// └─ 7...9

item := period.Ival{0, 5}
item := Ival{0, 5}
if got, _ := tree.Shortest(item); got != item {
t.Errorf("Shortest(%v) = %v, want %v", item, got, item)
}

item = period.Ival{5, 5}
want := period.Ival{4, 8}
item = Ival{5, 5}
want := Ival{4, 8}
if got, _ := tree.Shortest(item); got != want {
t.Errorf("Shortest(%v) = %v, want %v", item, got, want)
}

item = period.Ival{8, 9}
want = period.Ival{7, 9}
item = Ival{8, 9}
want = Ival{7, 9}
if got, _ := tree.Shortest(item); got != want {
t.Errorf("Shortest(%v) = %v, want %v", item, got, want)
}

item = period.Ival{3, 8}
want = period.Ival{2, 8}
item = Ival{3, 8}
want = Ival{2, 8}
if got, _ := tree.Shortest(item); got != want {
t.Errorf("Shortest(%v) = %v, want %v", item, got, want)
}

item = period.Ival{19, 55}
item = Ival{19, 55}
if got, ok := tree.Shortest(item); ok {
t.Errorf("Shortest(%v) = %v, want %v", item, got, !ok)
}

item = period.Ival{-19, 0}
item = Ival{-19, 0}
if got, ok := tree.Shortest(item); ok {
t.Errorf("Shortest(%v) = %v, want %v", item, got, !ok)
}

item = period.Ival{8, 8}
want = period.Ival{1, 8}
item = Ival{8, 8}
want = Ival{1, 8}
if got, _ := tree.Largest(item); got != want {
t.Errorf("Largest(%v) = %v, want %v", item, got, want)
}

item = period.Ival{3, 6}
want = period.Ival{0, 6}
item = Ival{3, 6}
want = Ival{0, 6}
if got, _ := tree.Largest(item); got != want {
t.Errorf("Largest(%v) = %v, want %v", item, got, want)
}

item = period.Ival{3, 7}
want = period.Ival{1, 8}
item = Ival{3, 7}
want = Ival{1, 8}
if got, _ := tree.Largest(item); got != want {
t.Errorf("Largest(%v) = %v, want %v", item, got, want)
}

item = period.Ival{0, 7}
item = Ival{0, 7}
if _, ok := tree.Largest(item); ok {
t.Errorf("Largest(%v) = %v, want %v", item, ok, false)
}
Expand All @@ -269,35 +268,35 @@ func TestLookup(t *testing.T) {
func TestSuperset(t *testing.T) {
t.Parallel()

is := []period.Ival{
is := []Ival{
{1, 100},
{45, 120},
{46, 80},
}

tree := interval.NewTree(is...)

item := period.Ival{0, 6}
item := Ival{0, 6}
if got, ok := tree.Largest(item); ok {
t.Errorf("Largest(%v) = %v, want %v", item, got, !ok)
}

item = period.Ival{99, 200}
item = Ival{99, 200}
if got, ok := tree.Largest(item); ok {
t.Errorf("Largest(%v) = %v, want %v", item, got, !ok)
}

item = period.Ival{1, 100}
item = Ival{1, 100}
if got, ok := tree.Largest(item); got != is[0] || !ok {
t.Errorf("Largest(%v) = %v, want %v", item, got, is[0])
}

item = period.Ival{46, 80}
item = Ival{46, 80}
if got, ok := tree.Largest(item); got != is[0] || !ok {
t.Errorf("Largest(%v) = %v, want %v", item, got, is[0])
}

item = period.Ival{47, 62}
item = Ival{47, 62}
if got, ok := tree.Largest(item); got != is[0] || !ok {
t.Errorf("Largest(%v) = %v, want %v", item, got, is[0])
}
Expand All @@ -307,9 +306,9 @@ func TestVisit(t *testing.T) {
t.Parallel()
tree := interval.NewTree(ps...)

var collect []period.Ival
var collect []Ival
want := 4
tree.Visit(tree.Min(), tree.Max(), func(item period.Ival) bool {
tree.Visit(tree.Min(), tree.Max(), func(item Ival) bool {
collect = append(collect, item)
return len(collect) != want
})
Expand All @@ -320,7 +319,7 @@ func TestVisit(t *testing.T) {

collect = nil
want = 9
tree.Visit(tree.Min(), tree.Max(), func(item period.Ival) bool {
tree.Visit(tree.Min(), tree.Max(), func(item Ival) bool {
collect = append(collect, item)
return len(collect) != want
})
Expand All @@ -331,14 +330,14 @@ func TestVisit(t *testing.T) {

collect = nil
want = 2
tree.Visit(tree.Max(), tree.Min(), func(item period.Ival) bool {
tree.Visit(tree.Max(), tree.Min(), func(item Ival) bool {
collect = append(collect, item)
return len(collect) != want
})

collect = nil
want = 5
tree.Visit(tree.Max(), tree.Min(), func(item period.Ival) bool {
tree.Visit(tree.Max(), tree.Min(), func(item Ival) bool {
collect = append(collect, item)
return len(collect) != want
})
Expand All @@ -347,20 +346,20 @@ func TestVisit(t *testing.T) {
func TestMinMax(t *testing.T) {
t.Parallel()
tree := interval.NewTree(ps...)
want := period.Ival{0, 6}
want := Ival{0, 6}
if tree.Min() != want {
t.Fatalf("Min(), want: %v, got: %v", want, tree.Min())
}

want = period.Ival{7, 9}
want = Ival{7, 9}
if tree.Max() != want {
t.Fatalf("Max(), want: %v, got: %v", want, tree.Max())
}
}

func TestUnion(t *testing.T) {
t.Parallel()
tree := interval.NewTree[period.Ival]()
tree := interval.NewTree[Ival]()

for i := range ps {
b := interval.NewTree(ps[i])
Expand Down Expand Up @@ -437,10 +436,10 @@ func TestRandom(t *testing.T) {
t.Run(tname, func(t *testing.T) {
t.Parallel()
var (
shortest period.Ival
largest period.Ival
subsets []period.Ival
supersets []period.Ival
shortest Ival
largest Ival
subsets []Ival
supersets []Ival
ok bool
)
if shortest, ok = tree.Shortest(item); !ok {
Expand Down

0 comments on commit a38dee7

Please sign in to comment.