From b98297c416953edfda0ead66ac157632df08247b Mon Sep 17 00:00:00 2001 From: Karl Gaissmaier Date: Fri, 20 Jan 2023 15:17:27 +0100 Subject: [PATCH] remove Size() from API --- README.md | 1 - bench_test.go | 15 --------------- helpers.go | 17 ++++------------- treap_test.go | 20 ++++++++++++-------- 4 files changed, 16 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index b09d680..496c859 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,6 @@ type Interface[T any] interface { func (t Tree[T]) Visit(start, stop T, visitFn func(item T) bool) func (t Tree[T]) Fprint(w io.Writer) error func (t Tree[T]) String() string - func (t Tree[T]) Size() int func (t Tree[T]) Min() (min T) func (t Tree[T]) Max() (max T) ``` diff --git a/bench_test.go b/bench_test.go index 50ea356..f94d30b 100644 --- a/bench_test.go +++ b/bench_test.go @@ -248,21 +248,6 @@ func BenchmarkIntersections(b *testing.B) { } } -func BenchmarkSize(b *testing.B) { - for n := 100; n <= 1_000_000; n *= 10 { - ivals := generateIvals(n) - tree := interval.NewTree(ivals...) - name := "In" + intMap[n] - - b.Run(name, func(b *testing.B) { - b.ResetTimer() - for n := 0; n < b.N; n++ { - _ = tree.Size() - } - }) - } -} - func BenchmarkMin(b *testing.B) { for n := 100; n <= 1_000_000; n *= 10 { ivals := generateIvals(n) diff --git a/helpers.go b/helpers.go index 2d31fb9..9c9d585 100644 --- a/helpers.go +++ b/helpers.go @@ -298,13 +298,14 @@ func (n *node[T]) pcmForNode(pcm parentChildsMap[T]) parentChildsMap[T] { // 0.x.y. In future versions this will be removed without increasing the main // semantic version, so please do not rely on it for now. // -func (t Tree[T]) Statistics() (maxDepth int, average, deviation float64) { +func (t Tree[T]) Statistics() (size int, maxDepth int, average, deviation float64) { // key is depth, value is the sum of nodes with this depth depths := make(map[int]int) - // get the depths + // get the depths, sum up the size t.root.traverse(inorder, 0, func(n *node[T], depth int) bool { depths[depth] += 1 + size += 1 return true }) @@ -326,7 +327,7 @@ func (t Tree[T]) Statistics() (maxDepth int, average, deviation float64) { variance = variance / float64(sum) deviation = math.Sqrt(variance) - return maxDepth, average, deviation + return size, maxDepth, average, deviation } // Min returns the min item in tree. @@ -355,16 +356,6 @@ func (t Tree[T]) Max() (max T) { return n.item } -// Size returns the number of items in tree. -func (t Tree[T]) Size() int { - size := 0 - t.root.traverse(inorder, 0, func(k *node[T], _ int) bool { - size++ - return true - }) - return size -} - // Visit traverses the tree with item >= start to item <= stop in ascending order, // or if start > stop, then the order is reversed. The visit function is called for each item. // diff --git a/treap_test.go b/treap_test.go index cc1ff7c..721402b 100644 --- a/treap_test.go +++ b/treap_test.go @@ -92,12 +92,12 @@ func TestNewTree(t *testing.T) { t.Errorf("CoverSCP(), got: %v, want: false", ok) } - if s := zeroTree.Insert(zeroItem); s.Size() != 1 { - t.Errorf("Insert(), got: %v, want: 1", s.Size()) + if size, _, _, _ := zeroTree.Insert(zeroItem).Statistics(); size != 1 { + t.Errorf("Insert(), got: %v, want: 1", size) } - if s := zeroTree.Clone(); s.Size() != 0 { - t.Errorf("Clone(), got: %v, want: 0", s.Size()) + if size, _, _, _ := zeroTree.Clone().Statistics(); size != 0 { + t.Errorf("Clone(), got: %v, want: 0", size) } if s := zeroTree.CoveredBy(zeroItem); s != nil { @@ -172,8 +172,8 @@ func TestTreeWithDups(t *testing.T) { } tree := interval.NewTree(is...) - if s := tree.Size(); s != 5 { - t.Errorf("Size() = %v, want 5", s) + if size, _, _, _ := tree.Statistics(); size != 5 { + t.Errorf("Size() = %v, want 5", size) } asStr := `▼ @@ -676,7 +676,7 @@ func TestVisit(t *testing.T) { return true }) - want = tree.Size() + want, _, _, _ = tree.Statistics() if len(collect) != want { t.Fatalf("Visit() descending, want: %d got: %v, %v", want, len(collect), collect) } @@ -790,7 +790,11 @@ func TestStatistics(t *testing.T) { t.Run(count, func(t *testing.T) { tree := interval.NewTree(generateIvals(n)...) - _, averageDepth, deviation := tree.Statistics() + size, _, averageDepth, deviation := tree.Statistics() + if size != n { + t.Fatalf("size, got: %d, want: %d", size, n) + } + t.Logf("stats: n=%d, averageDepth=%.4g, deviation=%.4g\n", n, averageDepth, deviation) maxAverageDepth := 2 * math.Log2(float64(n))