Skip to content

Commit

Permalink
remove Size() from API
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 20, 2023
1 parent ccb2de2 commit b98297c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 37 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
Expand Down
15 changes: 0 additions & 15 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 4 additions & 13 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand All @@ -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.
Expand Down Expand Up @@ -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.
//
Expand Down
20 changes: 12 additions & 8 deletions treap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 := `▼
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b98297c

Please sign in to comment.