Skip to content

Commit

Permalink
naming
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 28, 2023
1 parent 3e59cee commit b2ea9cb
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 220 deletions.
70 changes: 35 additions & 35 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var intMap = map[int]string{

func BenchmarkInsert(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)

probe := generateIvals(1)[0]
probe := genUintIvals(1)[0]
name := "Into" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -35,8 +35,8 @@ func BenchmarkInsert(b *testing.B) {

func BenchmarkInsertMutable(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
probe := genUintIvals(1)[0]
name := "Into" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -50,10 +50,10 @@ func BenchmarkInsertMutable(b *testing.B) {

func BenchmarkDelete(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
ivals := genUintIvals(n)
probe := ivals[rand.Intn(len(ivals))]

tree := interval.NewTree(compareIval, ivals...)
tree := interval.NewTree(cmpUintInterval, ivals...)
name := "DeleteFrom" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -67,10 +67,10 @@ func BenchmarkDelete(b *testing.B) {

func BenchmarkDeleteMutable(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
ivals := genUintIvals(n)
probe := ivals[rand.Intn(len(ivals))]

tree := interval.NewTree(compareIval, generateIvals(n)...)
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
name := "DeleteFrom" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -84,7 +84,7 @@ func BenchmarkDeleteMutable(b *testing.B) {

func BenchmarkClone(b *testing.B) {
for n := 10; n <= 10_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
name := intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -97,9 +97,9 @@ func BenchmarkClone(b *testing.B) {
}

func BenchmarkUnionImmutable(b *testing.B) {
this100_000 := interval.NewTree(compareIval, generateIvals(100_000)...)
this100_000 := interval.NewTree(cmpUintInterval, genUintIvals(100_000)...)
for n := 10; n <= 100_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
name := "size100_000with" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -113,8 +113,8 @@ func BenchmarkUnionImmutable(b *testing.B) {

func BenchmarkUnionMutable(b *testing.B) {
for n := 10; n <= 100_000; n *= 10 {
this100_000 := interval.NewTree(compareIval, generateIvals(100_000)...)
tree := interval.NewTree(compareIval, generateIvals(n)...)
this100_000 := interval.NewTree(cmpUintInterval, genUintIvals(100_000)...)
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
name := "size100_000with" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -128,8 +128,8 @@ func BenchmarkUnionMutable(b *testing.B) {

func BenchmarkIntersects(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := interval.NewTree(compareIval, ivals...)
ivals := genUintIvals(n)
tree := interval.NewTree(cmpUintInterval, ivals...)
probe := ivals[rand.Intn(len(ivals))]
name := "In" + intMap[n]

Expand All @@ -144,8 +144,8 @@ func BenchmarkIntersects(b *testing.B) {

func BenchmarkFind(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := interval.NewTree(compareIval, ivals...)
ivals := genUintIvals(n)
tree := interval.NewTree(cmpUintInterval, ivals...)
probe := ivals[rand.Intn(len(ivals))]
name := "In" + intMap[n]

Expand All @@ -160,8 +160,8 @@ func BenchmarkFind(b *testing.B) {

func BenchmarkCoverLCP(b *testing.B) {
for n := 100; n <= 1_000_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
probe := genUintIvals(1)[0]
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -175,8 +175,8 @@ func BenchmarkCoverLCP(b *testing.B) {

func BenchmarkCoverSCP(b *testing.B) {
for n := 100; n <= 1_000_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
probe := genUintIvals(1)[0]
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -190,8 +190,8 @@ func BenchmarkCoverSCP(b *testing.B) {

func BenchmarkCoveredBy(b *testing.B) {
for n := 100; n <= 100_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
probe := genUintIvals(1)[0]
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -205,8 +205,8 @@ func BenchmarkCoveredBy(b *testing.B) {

func BenchmarkCovers(b *testing.B) {
for n := 100; n <= 100_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
probe := genUintIvals(1)[0]
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -220,8 +220,8 @@ func BenchmarkCovers(b *testing.B) {

func BenchmarkPrecededBy(b *testing.B) {
for m := 100; m <= 10_000; m *= 10 {
tree := interval.NewTree(compareIval, generateIvals(m)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(m)...)
probe := genUintIvals(1)[0]
name := "In" + intMap[m]

b.Run(name, func(b *testing.B) {
Expand All @@ -235,8 +235,8 @@ func BenchmarkPrecededBy(b *testing.B) {

func BenchmarkPrecedes(b *testing.B) {
for m := 100; m <= 10_000; m *= 10 {
tree := interval.NewTree(compareIval, generateIvals(m)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(m)...)
probe := genUintIvals(1)[0]
name := "In" + intMap[m]

b.Run(name, func(b *testing.B) {
Expand All @@ -250,8 +250,8 @@ func BenchmarkPrecedes(b *testing.B) {

func BenchmarkIntersections(b *testing.B) {
for n := 100; n <= 10_000; n *= 10 {
tree := interval.NewTree(compareIval, generateIvals(n)...)
probe := generateIvals(1)[0]
tree := interval.NewTree(cmpUintInterval, genUintIvals(n)...)
probe := genUintIvals(1)[0]
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -265,8 +265,8 @@ func BenchmarkIntersections(b *testing.B) {

func BenchmarkMin(b *testing.B) {
for n := 100; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := interval.NewTree(compareIval, ivals...)
ivals := genUintIvals(n)
tree := interval.NewTree(cmpUintInterval, ivals...)
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -280,8 +280,8 @@ func BenchmarkMin(b *testing.B) {

func BenchmarkMax(b *testing.B) {
for n := 100; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := interval.NewTree(compareIval, ivals...)
ivals := genUintIvals(n)
tree := interval.NewTree(cmpUintInterval, ivals...)
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand Down
62 changes: 28 additions & 34 deletions example_period_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,15 @@ import (
)

// example interval
type Ival [2]uint
type uintInterval [2]uint

// example data
var periods = []Ival{
{3, 4},
{2, 9},
{7, 9},
{3, 5},
}

// fmt.Stringer for formattting, not required
func (p Ival) String() string {
return fmt.Sprintf("%d...%d", p[0], p[1])
// cmp function for uintInterval
func cmpUintInterval(p, q uintInterval) (ll, rr, lr, rl int) {
return cmpUint(p[0], q[0]), cmpUint(p[1], q[1]), cmpUint(p[0], q[1]), cmpUint(p[1], q[0])
}

// little helper
func cmp(a, b uint) int {
func cmpUint(a, b uint) int {
switch {
case a == b:
return 0
Expand All @@ -34,14 +26,21 @@ func cmp(a, b uint) int {
return 1
}

// cmp function for type Ival
func compareIval(p, q Ival) (ll, rr, lr, rl int) {
return cmp(p[0], q[0]), cmp(p[1], q[1]), cmp(p[0], q[1]), cmp(p[1], q[0])
// example data
var periods = []uintInterval{
{3, 4},
{2, 9},
{7, 9},
{3, 5},
}

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

func ExampleNewTree() {
tree1 := interval.NewTree(compareIval, periods...)

tree1 := interval.NewTree(cmpUintInterval, periods...)
tree1.Fprint(os.Stdout)
// Output:
// ▼
Expand All @@ -52,12 +51,11 @@ func ExampleNewTree() {
}

func ExampleTree_Max() {
tree1 := interval.NewTree(compareIval, periods...)
tree1 := interval.NewTree(cmpUintInterval, periods...)
tree1.Fprint(os.Stdout)

fmt.Println("\nInterval with max value in tree:")
fmt.Println(tree1.Max())

// Output:
// ▼
// └─ 2...9
Expand All @@ -70,15 +68,14 @@ func ExampleTree_Max() {
}

func ExampleTree_Covers() {
tree1 := interval.NewTree(compareIval, periods...)
tree1 := interval.NewTree(cmpUintInterval, periods...)
tree1.Fprint(os.Stdout)

item := Ival{3, 4}
item := uintInterval{3, 4}
fmt.Printf("\nCovers for item: %v\n", item)
for _, p := range tree1.Covers(item) {
fmt.Println(p)
}

// Output:
// ▼
// └─ 2...9
Expand All @@ -93,15 +90,14 @@ func ExampleTree_Covers() {
}

func ExampleTree_CoveredBy() {
tree1 := interval.NewTree(compareIval, periods...)
tree1 := interval.NewTree(cmpUintInterval, periods...)
tree1.Fprint(os.Stdout)

item := Ival{3, 10}
item := uintInterval{3, 10}
fmt.Printf("\nCoveredBy item: %v\n", item)
for _, p := range tree1.CoveredBy(item) {
fmt.Println(p)
}

// Output:
// ▼
// └─ 2...9
Expand All @@ -116,15 +112,14 @@ func ExampleTree_CoveredBy() {
}

func ExampleTree_Precedes_period() {
tree1 := interval.NewTree(compareIval, periods...)
tree1 := interval.NewTree(cmpUintInterval, periods...)
tree1.Fprint(os.Stdout)

item := Ival{6, 6}
item := uintInterval{6, 6}
fmt.Printf("\nPrecedes item: %v\n", item)
for _, p := range tree1.Precedes(item) {
fmt.Println(p)
}

// Output:
// ▼
// └─ 2...9
Expand All @@ -138,13 +133,13 @@ func ExampleTree_Precedes_period() {
}

func ExampleTree_Visit() {
tree1 := interval.NewTree(compareIval, periods...)
tree1 := interval.NewTree(cmpUintInterval, periods...)
fmt.Println("parent/child printing")
tree1.Fprint(os.Stdout)

start := Ival{3, 5}
stop := Ival{7, 9}
visitFn := func(item Ival) bool {
start := uintInterval{3, 5}
stop := uintInterval{7, 9}
visitFn := func(item uintInterval) bool {
fmt.Printf("%v\n", item)
return true
}
Expand All @@ -154,7 +149,6 @@ func ExampleTree_Visit() {

fmt.Println("visit descending")
tree1.Visit(stop, start, visitFn)

// Output:
// parent/child printing
// ▼
Expand Down
Loading

0 comments on commit b2ea9cb

Please sign in to comment.