From b2ea9cb5cd4f2425b9886ab3946992c8ed0c94e3 Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Sat, 28 Jan 2023 15:19:49 +0100 Subject: [PATCH] naming --- bench_test.go | 70 +++++++-------- example_period_test.go | 62 ++++++-------- example_time_test.go | 110 ++++++++++++------------ treap_test.go | 190 ++++++++++++++++++++--------------------- 4 files changed, 212 insertions(+), 220 deletions(-) diff --git a/bench_test.go b/bench_test.go index fb07c5d..39b64fa 100644 --- a/bench_test.go +++ b/bench_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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] @@ -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] @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { diff --git a/example_period_test.go b/example_period_test.go index 8a3eda8..16627fa 100644 --- a/example_period_test.go +++ b/example_period_test.go @@ -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 @@ -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: // ▼ @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 } @@ -154,7 +149,6 @@ func ExampleTree_Visit() { fmt.Println("visit descending") tree1.Visit(stop, start, visitFn) - // Output: // parent/child printing // ▼ diff --git a/example_time_test.go b/example_time_test.go index 809cb17..6bdb3b2 100644 --- a/example_time_test.go +++ b/example_time_test.go @@ -9,11 +9,26 @@ import ( "github.com/gaissmai/interval" ) +// example time period interval +type timeInterval struct { + birth time.Time + death time.Time + name string +} + +// cmp func for timeInterval +func cmpTimeInterval(p, q timeInterval) (ll, rr, lr, rl int) { + return cmpTime(p.birth, q.birth), + cmpTime(p.death, q.death), + cmpTime(p.birth, q.death), + cmpTime(p.death, q.birth) +} + // little helper -func mkTval(i, j int, s string) Tval { +func makeTimeInterval(i, j int, s string) timeInterval { t1, _ := time.Parse("2006", strconv.Itoa(i)) t2, _ := time.Parse("2006", strconv.Itoa(j)) - return Tval{birth: t1, death: t2, name: s} + return timeInterval{birth: t1, death: t2, name: s} } // little helper @@ -27,68 +42,52 @@ func cmpTime(a, b time.Time) int { return 0 } -// example time period interval -type Tval struct { - birth time.Time - death time.Time - name string +// example data +var physicists = []timeInterval{ + makeTimeInterval(1473, 1543, "Kopernikus"), + makeTimeInterval(1544, 1603, "Gilbert"), + makeTimeInterval(1564, 1642, "Galilei"), + makeTimeInterval(1571, 1630, "Kepler"), + makeTimeInterval(1623, 1662, "Pascal"), + makeTimeInterval(1629, 1695, "Huygens"), + makeTimeInterval(1643, 1727, "Newton"), + makeTimeInterval(1700, 1782, "Bernoulli"), + makeTimeInterval(1777, 1855, "Gauss"), + makeTimeInterval(1707, 1783, "Euler"), + makeTimeInterval(1731, 1810, "Cavendish"), + makeTimeInterval(1736, 1813, "Lagrange"), + makeTimeInterval(1736, 1806, "Coulomb"), + makeTimeInterval(1745, 1827, "Volta"), + makeTimeInterval(1749, 1827, "Laplace"), + makeTimeInterval(1768, 1830, "Fourier"), + makeTimeInterval(1773, 1829, "Young"), + makeTimeInterval(1775, 1836, "Ampère"), + makeTimeInterval(1788, 1827, "Fresnel"), + makeTimeInterval(1791, 1867, "Faraday"), + makeTimeInterval(1796, 1832, "Carnot"), + makeTimeInterval(1805, 1865, "Hamilton"), + makeTimeInterval(1818, 1889, "Joule"), + makeTimeInterval(1821, 1894, "Helholtz"), + makeTimeInterval(1822, 1888, "Clausius"), + makeTimeInterval(1824, 1887, "Kirchhoff"), + makeTimeInterval(1824, 1907, "Kelvin"), + makeTimeInterval(1831, 1879, "Maxwell"), } // String, implements fmt.Stringer for nice formattting -func (p Tval) String() string { +func (p timeInterval) String() string { return fmt.Sprintf("%s...%s (%s)", p.birth.Format("2006"), p.death.Format("2006"), p.name) } -// cmp func for type Tval -func compareTval(p, q Tval) (ll, rr, lr, rl int) { - return cmpTime(p.birth, q.birth), - cmpTime(p.death, q.death), - cmpTime(p.birth, q.death), - cmpTime(p.death, q.birth) -} - -// example data -var physicists = []Tval{ - mkTval(1473, 1543, "Kopernikus"), - mkTval(1544, 1603, "Gilbert"), - mkTval(1564, 1642, "Galilei"), - mkTval(1571, 1630, "Kepler"), - mkTval(1623, 1662, "Pascal"), - mkTval(1629, 1695, "Huygens"), - mkTval(1643, 1727, "Newton"), - mkTval(1700, 1782, "Bernoulli"), - mkTval(1777, 1855, "Gauss"), - mkTval(1707, 1783, "Euler"), - mkTval(1731, 1810, "Cavendish"), - mkTval(1736, 1813, "Lagrange"), - mkTval(1736, 1806, "Coulomb"), - mkTval(1745, 1827, "Volta"), - mkTval(1749, 1827, "Laplace"), - mkTval(1768, 1830, "Fourier"), - mkTval(1773, 1829, "Young"), - mkTval(1775, 1836, "Ampère"), - mkTval(1788, 1827, "Fresnel"), - mkTval(1791, 1867, "Faraday"), - mkTval(1796, 1832, "Carnot"), - mkTval(1805, 1865, "Hamilton"), - mkTval(1818, 1889, "Joule"), - mkTval(1821, 1894, "Helholtz"), - mkTval(1822, 1888, "Clausius"), - mkTval(1824, 1887, "Kirchhoff"), - mkTval(1824, 1907, "Kelvin"), - mkTval(1831, 1879, "Maxwell"), -} - func ExampleTree_Precedes_time() { - tree := interval.NewTree(compareTval, physicists...) + tree := interval.NewTree(cmpTimeInterval, physicists...) tree.Fprint(os.Stdout) - precedes := tree.Precedes(mkTval(1643, 1727, "Newton")) - tree = interval.NewTree(compareTval, precedes...) + precedes := tree.Precedes(makeTimeInterval(1643, 1727, "Newton")) + tree = interval.NewTree(cmpTimeInterval, precedes...) fmt.Println("\nPrecedes Newton:") tree.Fprint(os.Stdout) - // Output: // ▼ // ├─ 1473...1543 (Kopernikus) @@ -129,15 +128,14 @@ func ExampleTree_Precedes_time() { } func ExampleTree_PrecededBy_time() { - tree := interval.NewTree(compareTval, physicists...) + tree := interval.NewTree(cmpTimeInterval, physicists...) tree.Fprint(os.Stdout) - precededBy := tree.PrecededBy(mkTval(1643, 1727, "Newton")) - tree = interval.NewTree(compareTval, precededBy...) + precededBy := tree.PrecededBy(makeTimeInterval(1643, 1727, "Newton")) + tree = interval.NewTree(cmpTimeInterval, precededBy...) fmt.Println("\nPrecededBy Newton:") tree.Fprint(os.Stdout) - // Output: // ▼ // ├─ 1473...1543 (Kopernikus) diff --git a/treap_test.go b/treap_test.go index 620dfff..41daa2a 100644 --- a/treap_test.go +++ b/treap_test.go @@ -12,7 +12,7 @@ import ( ) // test data -var ps = []Ival{ +var ps = []uintInterval{ {0, 6}, {0, 5}, {1, 8}, @@ -26,29 +26,29 @@ var ps = []Ival{ {7, 9}, } -func mkIval(a, b uint) Ival { +func makeUintIval(a, b uint) uintInterval { if a > b { a, b = b, a } - return Ival{a, b} + return uintInterval{a, b} } // random test data -func generateIvals(n int) []Ival { - is := make([]Ival, n) +func genUintIvals(n int) []uintInterval { + is := make([]uintInterval, n) for i := 0; i < n; i++ { a := rand.Int() b := rand.Int() - is[i] = mkIval(uint(a), uint(b)) + is[i] = makeUintIval(uint(a), uint(b)) } return is } -func equals(a, b Ival) bool { +func equals(a, b uintInterval) bool { return a[0] == b[0] && a[1] == b[1] } -func equalStatistics(t1, t2 interval.Tree[Ival]) bool { +func equalStatistics(t1, t2 interval.Tree[uintInterval]) bool { a1, b1, c1, d1 := t1.Statistics() a2, b2, c2, d2 := t2.Statistics() return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2 @@ -57,8 +57,8 @@ func equalStatistics(t1, t2 interval.Tree[Ival]) bool { func TestNewTree(t *testing.T) { t.Parallel() - var zeroItem Ival - tree := interval.NewTree(compareIval) + var zeroItem uintInterval + tree := interval.NewTree(cmpUintInterval) if tree.String() != "" { t.Errorf("String() = %v, want \"\"", "") @@ -134,8 +134,8 @@ func TestNewTree(t *testing.T) { t.Errorf("Max(), got: %v, want: %v", s, zeroItem) } - var items []Ival - tree.Visit(zeroItem, zeroItem, func(item Ival) bool { + var items []uintInterval + tree.Visit(zeroItem, zeroItem, func(item uintInterval) bool { items = append(items, item) return true }) @@ -147,7 +147,7 @@ func TestNewTree(t *testing.T) { func TestTreeWithDups(t *testing.T) { t.Parallel() - is := []Ival{ + is := []uintInterval{ {0, 100}, {41, 102}, {41, 102}, @@ -173,7 +173,7 @@ func TestTreeWithDups(t *testing.T) { {3, 13}, } - tree1 := interval.NewTree(compareIval, is...) + tree1 := interval.NewTree(cmpUintInterval, is...) if size, _, _, _ := tree1.Statistics(); size != 5 { t.Errorf("Size() = %v, want 5", size) } @@ -192,7 +192,7 @@ func TestTreeWithDups(t *testing.T) { func TestImmutable(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) + tree1 := interval.NewTree(cmpUintInterval, ps...) if _, ok := tree1.Delete(tree1.Min()); !ok { t.Fatal("Delete, could not delete min item") @@ -201,7 +201,7 @@ func TestImmutable(t *testing.T) { t.Fatal("Delete changed receiver") } - item := Ival{111, 666} + item := uintInterval{111, 666} _ = tree1.Insert(item) if _, ok := tree1.Find(item); ok { @@ -210,7 +210,7 @@ func TestImmutable(t *testing.T) { } func TestMutable(t *testing.T) { - tree1 := interval.NewTree(compareIval, ps...) + tree1 := interval.NewTree(cmpUintInterval, ps...) clone := tree1.Clone() if !equalStatistics(tree1, clone) { @@ -233,14 +233,14 @@ func TestMutable(t *testing.T) { } // reset - tree1 = interval.NewTree(compareIval, ps...) + tree1 = interval.NewTree(cmpUintInterval, ps...) clone = tree1.Clone() if !equalStatistics(tree1, clone) { t.Fatalf("Clone, something wrong, statistics differs") } - item := Ival{111, 666} + item := uintInterval{111, 666} (&tree1).InsertMutable(item) if _, ok := tree1.Find(item); !ok { @@ -251,15 +251,15 @@ func TestMutable(t *testing.T) { func TestFind(t *testing.T) { t.Parallel() - ivals := generateIvals(100_00) - tree1 := interval.NewTree(compareIval, ivals...) + ivals := genUintIvals(100_00) + tree1 := interval.NewTree(cmpUintInterval, ivals...) for _, ival := range ivals { item, ok := tree1.Find(ival) if ok != true { t.Errorf("Find(%v) = %v, want %v", item, ok, true) } - ll, rr, _, _ := compareIval(item, ival) + ll, rr, _, _ := cmpUintInterval(item, ival) if ll != 0 || rr != 0 { t.Errorf("Find(%v) = %v, want %v", ival, item, ival) } @@ -271,7 +271,7 @@ func TestLookup(t *testing.T) { for i := 0; i < 100; i++ { // bring some variance into the Treap due to the prio randomness - tree1 := interval.NewTree(compareIval, ps...) + tree1 := interval.NewTree(cmpUintInterval, ps...) // ▼ // ├─ 0...6 @@ -286,58 +286,58 @@ func TestLookup(t *testing.T) { // │ └─ 6...7 // └─ 7...9 - item := Ival{0, 5} + item := uintInterval{0, 5} if got, _ := tree1.CoverLCP(item); got != item { t.Errorf("CoverLCP(%v) = %v, want %v", item, got, item) } - item = Ival{5, 5} - want := Ival{4, 8} + item = uintInterval{5, 5} + want := uintInterval{4, 8} if got, _ := tree1.CoverLCP(item); got != want { t.Errorf("CoverLCP(%v) = %v, want %v", item, got, want) } - item = Ival{8, 9} - want = Ival{7, 9} + item = uintInterval{8, 9} + want = uintInterval{7, 9} if got, _ := tree1.CoverLCP(item); got != want { t.Errorf("CoverLCP(%v) = %v, want %v", item, got, want) } - item = Ival{3, 8} - want = Ival{2, 8} + item = uintInterval{3, 8} + want = uintInterval{2, 8} if got, _ := tree1.CoverLCP(item); got != want { t.Errorf("CoverLCP(%v) = %v, want %v", item, got, want) } - item = Ival{19, 55} + item = uintInterval{19, 55} if got, ok := tree1.CoverLCP(item); ok { t.Errorf("CoverLCP(%v) = %v, want %v", item, got, !ok) } - item = Ival{0, 19} + item = uintInterval{0, 19} if got, ok := tree1.CoverLCP(item); ok { t.Errorf("CoverLCP(%v) = %v, want %v", item, got, !ok) } - item = Ival{7, 7} - want = Ival{1, 8} + item = uintInterval{7, 7} + want = uintInterval{1, 8} if got, _ := tree1.CoverSCP(item); got != want { t.Errorf("CoverSCP(%v) = %v, want %v", item, got, want) } - item = Ival{3, 6} - want = Ival{0, 6} + item = uintInterval{3, 6} + want = uintInterval{0, 6} if got, _ := tree1.CoverSCP(item); got != want { t.Errorf("CoverSCP(%v) = %v, want %v", item, got, want) } - item = Ival{3, 7} - want = Ival{1, 8} + item = uintInterval{3, 7} + want = uintInterval{1, 8} if got, _ := tree1.CoverSCP(item); got != want { t.Errorf("CoverSCP(%v) = %v, want %v", item, got, want) } - item = Ival{0, 7} + item = uintInterval{0, 7} if _, ok := tree1.CoverSCP(item); ok { t.Errorf("CoverSCP(%v) = %v, want %v", item, ok, false) } @@ -348,8 +348,8 @@ func TestLookup(t *testing.T) { func TestCoveredBy(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) - var want []Ival + tree1 := interval.NewTree(cmpUintInterval, ps...) + var want []uintInterval // ▼ // ├─ 0...6 @@ -364,8 +364,8 @@ func TestCoveredBy(t *testing.T) { // │ └─ 6...7 // └─ 7...9 - item := Ival{0, 6} - want = []Ival{{0, 6}, {0, 5}, {1, 5}, {1, 4}} + item := uintInterval{0, 6} + want = []uintInterval{{0, 6}, {0, 5}, {1, 5}, {1, 4}} covered := tree1.CoveredBy(item) if !reflect.DeepEqual(covered, want) { @@ -373,7 +373,7 @@ func TestCoveredBy(t *testing.T) { } // ### - item = Ival{3, 6} + item = uintInterval{3, 6} want = nil covered = tree1.CoveredBy(item) @@ -382,8 +382,8 @@ func TestCoveredBy(t *testing.T) { } // ### - item = Ival{3, 11} - want = []Ival{{4, 8}, {6, 7}, {7, 9}} + item = uintInterval{3, 11} + want = []uintInterval{{4, 8}, {6, 7}, {7, 9}} covered = tree1.CoveredBy(item) if !reflect.DeepEqual(covered, want) { @@ -394,8 +394,8 @@ func TestCoveredBy(t *testing.T) { func TestCovers(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) - var want []Ival + tree1 := interval.NewTree(cmpUintInterval, ps...) + var want []uintInterval // ▼ // ├─ 0...6 @@ -410,8 +410,8 @@ func TestCovers(t *testing.T) { // │ └─ 6...7 // └─ 7...9 - item := Ival{0, 6} - want = []Ival{{0, 6}} + item := uintInterval{0, 6} + want = []uintInterval{{0, 6}} covers := tree1.Covers(item) if !reflect.DeepEqual(covers, want) { @@ -419,8 +419,8 @@ func TestCovers(t *testing.T) { } // ### - item = Ival{3, 7} - want = []Ival{{1, 8}, {1, 7}, {2, 8}, {2, 7}} + item = uintInterval{3, 7} + want = []uintInterval{{1, 8}, {1, 7}, {2, 8}, {2, 7}} covers = tree1.Covers(item) if !reflect.DeepEqual(covers, want) { @@ -428,7 +428,7 @@ func TestCovers(t *testing.T) { } // ### - item = Ival{3, 11} + item = uintInterval{3, 11} want = nil covers = tree1.Covers(item) @@ -440,7 +440,7 @@ func TestCovers(t *testing.T) { func TestIntersects(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) + tree1 := interval.NewTree(cmpUintInterval, ps...) // ▼ // ├─ 0...6 @@ -455,7 +455,7 @@ func TestIntersects(t *testing.T) { // │ └─ 6...7 // └─ 7...9 - item := Ival{7, 7} + item := uintInterval{7, 7} want := true got := tree1.Intersects(item) @@ -463,7 +463,7 @@ func TestIntersects(t *testing.T) { t.Fatalf("Intersects(%v), got: %v, want: %v", item, got, want) } - item = Ival{9, 17} + item = uintInterval{9, 17} want = true got = tree1.Intersects(item) @@ -471,7 +471,7 @@ func TestIntersects(t *testing.T) { t.Fatalf("Intersects(%v), got: %v, want: %v", item, got, want) } - item = Ival{1, 1} + item = uintInterval{1, 1} want = true got = tree1.Intersects(item) @@ -479,7 +479,7 @@ func TestIntersects(t *testing.T) { t.Fatalf("Intersects(%v), got: %v, want: %v", item, got, want) } - item = Ival{10, 12} + item = uintInterval{10, 12} want = false got = tree1.Intersects(item) @@ -491,8 +491,8 @@ func TestIntersects(t *testing.T) { func TestIntersections(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) - var want []Ival + tree1 := interval.NewTree(cmpUintInterval, ps...) + var want []uintInterval // ▼ // ├─ 0...6 @@ -507,8 +507,8 @@ func TestIntersections(t *testing.T) { // │ └─ 6...7 // └─ 7...9 - item := Ival{7, 7} - want = []Ival{{1, 8}, {1, 7}, {2, 8}, {2, 7}, {4, 8}, {6, 7}, {7, 9}} + item := uintInterval{7, 7} + want = []uintInterval{{1, 8}, {1, 7}, {2, 8}, {2, 7}, {4, 8}, {6, 7}, {7, 9}} intersections := tree1.Intersections(item) if !reflect.DeepEqual(intersections, want) { @@ -516,8 +516,8 @@ func TestIntersections(t *testing.T) { } // ### - item = Ival{8, 10} - want = []Ival{{1, 8}, {2, 8}, {4, 8}, {7, 9}} + item = uintInterval{8, 10} + want = []uintInterval{{1, 8}, {2, 8}, {4, 8}, {7, 9}} intersections = tree1.Intersections(item) if !reflect.DeepEqual(intersections, want) { @@ -525,7 +525,7 @@ func TestIntersections(t *testing.T) { } // ### - item = Ival{10, 15} + item = uintInterval{10, 15} want = nil intersections = tree1.Intersections(item) @@ -537,8 +537,8 @@ func TestIntersections(t *testing.T) { func TestPrecedes(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) - var want []Ival + tree1 := interval.NewTree(cmpUintInterval, ps...) + var want []uintInterval // ▼ // ├─ 0...6 @@ -553,8 +553,8 @@ func TestPrecedes(t *testing.T) { // │ └─ 6...7 // └─ 7...9 - item := Ival{7, 7} - want = []Ival{{0, 6}, {0, 5}, {1, 5}, {1, 4}} + item := uintInterval{7, 7} + want = []uintInterval{{0, 6}, {0, 5}, {1, 5}, {1, 4}} precedes := tree1.Precedes(item) if !reflect.DeepEqual(precedes, want) { @@ -562,8 +562,8 @@ func TestPrecedes(t *testing.T) { } // ### - item = Ival{5, 10} - want = []Ival{{1, 4}} + item = uintInterval{5, 10} + want = []uintInterval{{1, 4}} precedes = tree1.Precedes(item) if !reflect.DeepEqual(precedes, want) { @@ -571,7 +571,7 @@ func TestPrecedes(t *testing.T) { } // ### - item = Ival{0, 9} + item = uintInterval{0, 9} want = nil precedes = tree1.Precedes(item) @@ -583,8 +583,8 @@ func TestPrecedes(t *testing.T) { func TestPrecededBy(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) - var want []Ival + tree1 := interval.NewTree(cmpUintInterval, ps...) + var want []uintInterval // ▼ // ├─ 0...6 @@ -599,8 +599,8 @@ func TestPrecededBy(t *testing.T) { // │ └─ 6...7 // └─ 7...9 - item := Ival{4, 4} - want = []Ival{{6, 7}, {7, 9}} + item := uintInterval{4, 4} + want = []uintInterval{{6, 7}, {7, 9}} precedes := tree1.PrecededBy(item) if !reflect.DeepEqual(precedes, want) { @@ -608,8 +608,8 @@ func TestPrecededBy(t *testing.T) { } // ### - item = Ival{1, 2} - want = []Ival{{4, 8}, {6, 7}, {7, 9}} + item = uintInterval{1, 2} + want = []uintInterval{{4, 8}, {6, 7}, {7, 9}} precedes = tree1.PrecededBy(item) if !reflect.DeepEqual(precedes, want) { @@ -617,7 +617,7 @@ func TestPrecededBy(t *testing.T) { } // ### - item = Ival{0, 7} + item = uintInterval{0, 7} want = nil precedes = tree1.PrecededBy(item) @@ -628,11 +628,11 @@ func TestPrecededBy(t *testing.T) { func TestVisit(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) + tree1 := interval.NewTree(cmpUintInterval, ps...) - var collect []Ival + var collect []uintInterval want := 4 - tree1.Visit(tree1.Min(), tree1.Max(), func(item Ival) bool { + tree1.Visit(tree1.Min(), tree1.Max(), func(item uintInterval) bool { collect = append(collect, item) return len(collect) != want }) @@ -643,7 +643,7 @@ func TestVisit(t *testing.T) { collect = nil want = 9 - tree1.Visit(tree1.Max(), tree1.Min(), func(item Ival) bool { + tree1.Visit(tree1.Max(), tree1.Min(), func(item uintInterval) bool { collect = append(collect, item) return true }) @@ -655,7 +655,7 @@ func TestVisit(t *testing.T) { collect = nil want = 2 - tree1.Visit(tree1.Max(), tree1.Min(), func(item Ival) bool { + tree1.Visit(tree1.Max(), tree1.Min(), func(item uintInterval) bool { collect = append(collect, item) return len(collect) != want }) @@ -663,13 +663,13 @@ func TestVisit(t *testing.T) { func TestMinMax(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) - want := Ival{0, 6} + tree1 := interval.NewTree(cmpUintInterval, ps...) + want := uintInterval{0, 6} if tree1.Min() != want { t.Fatalf("Min(), want: %v, got: %v", want, tree1.Min()) } - want = Ival{7, 9} + want = uintInterval{7, 9} if tree1.Max() != want { t.Fatalf("Max(), want: %v, got: %v", want, tree1.Max()) } @@ -677,7 +677,7 @@ func TestMinMax(t *testing.T) { func TestUnion(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval) + tree1 := interval.NewTree(cmpUintInterval) for i := range ps { b := tree1.Insert(ps[i]) @@ -712,7 +712,7 @@ func TestUnion(t *testing.T) { t.Errorf("String()\nwant:\n%sgot:\n%s", asStr, tree1.String()) } - ps2 := []Ival{ + ps2 := []uintInterval{ {7, 60}, {8, 50}, {9, 80}, @@ -760,7 +760,7 @@ func TestStatistics(t *testing.T) { for n := 10_000; n <= 1_000_000; n *= 10 { count := strconv.Itoa(n) t.Run(count, func(t *testing.T) { - tree1 := interval.NewTree(compareIval, generateIvals(n)...) + tree1 := interval.NewTree(cmpUintInterval, genUintIvals(n)...) size, _, averageDepth, deviation := tree1.Statistics() if size != n { @@ -784,7 +784,7 @@ func TestStatistics(t *testing.T) { func TestPrintBST(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, ps...) + tree1 := interval.NewTree(cmpUintInterval, ps...) w := new(strings.Builder) _ = tree1.FprintBST(w) @@ -798,11 +798,11 @@ func TestPrintBST(t *testing.T) { func TestMatch(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, generateIvals(100_000)...) + tree1 := interval.NewTree(cmpUintInterval, genUintIvals(100_000)...) n := 100 for i := 0; i < n; i++ { - probe := generateIvals(100_000)[0] + probe := genUintIvals(100_000)[0] t.Run(probe.String(), func(t *testing.T) { tree1 := tree1.Insert(probe) @@ -849,11 +849,11 @@ func TestMatch(t *testing.T) { func TestMissing(t *testing.T) { t.Parallel() - tree1 := interval.NewTree(compareIval, generateIvals(100_000)...) + tree1 := interval.NewTree(cmpUintInterval, genUintIvals(100_000)...) n := 100 for i := 0; i < n; i++ { - probe := generateIvals(100_000)[0] + probe := genUintIvals(100_000)[0] t.Run(probe.String(), func(t *testing.T) { tree1 := tree1.Insert(probe)