Skip to content

Commit

Permalink
better API
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 10, 2023
1 parent 476ed17 commit b1c81b9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 108 deletions.
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ goos: linux
goarch: amd64
pkg: github.com/gaissmai/interval
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
BenchmarkInsertInto1-8 8506729 174.8 ns/op 128 B/op 2 allocs/op
BenchmarkInsertInto10-8 3443077 304.3 ns/op 256 B/op 4 allocs/op
BenchmarkInsertInto100-8 2419617 836.3 ns/op 640 B/op 10 allocs/op
BenchmarkInsertInto1_000-8 2900678 713.0 ns/op 512 B/op 8 allocs/op
BenchmarkInsertInto10_000-8 1222190 964.9 ns/op 768 B/op 12 allocs/op
BenchmarkInsertInto100_000-8 1019144 1246.0 ns/op 832 B/op 13 allocs/op
BenchmarkInsertInto1_000_000-8 881776 1423.0 ns/op 896 B/op 14 allocs/op
BenchmarkInsert/Into1-8 8728209 142 ns/op 128 B/op 2 allocs/op
BenchmarkInsert/Into10-8 2740953 391 ns/op 320 B/op 5 allocs/op
BenchmarkInsert/Into100-8 889261 1610 ns/op 896 B/op 14 allocs/op
BenchmarkInsert/Into1_000-8 601810 2081 ns/op 1088 B/op 17 allocs/op
BenchmarkInsert/Into10_000-8 754924 1334 ns/op 960 B/op 15 allocs/op
BenchmarkInsert/Into100_000-8 388801 2921 ns/op 1728 B/op 27 allocs/op
BenchmarkInsert/Into1_000_000-8 363061 4081 ns/op 2304 B/op 36 allocs/op
```

### Delete
Expand All @@ -132,13 +132,12 @@ goos: linux
goarch: amd64
pkg: github.com/gaissmai/interval
cpu: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
BenchmarkDeleteFrom1-8 17313007 59.5 ns/op 64 B/op 1 allocs/op
BenchmarkDeleteFrom10-8 9414926 251.1 ns/op 192 B/op 3 allocs/op
BenchmarkDeleteFrom100-8 2230638 504.0 ns/op 448 B/op 7 allocs/op
BenchmarkDeleteFrom1_000-8 1000000 1157.0 ns/op 832 B/op 13 allocs/op
BenchmarkDeleteFrom10_000-8 545694 2207.0 ns/op 1792 B/op 28 allocs/op
BenchmarkDeleteFrom100_000-8 418015 2515.0 ns/op 1856 B/op 29 allocs/op
BenchmarkDeleteFrom1_000_000-8 1081098 1504.0 ns/op 960 B/op 15 allocs/op
BenchmarkDelete/DeleteFrom10-8 8288145 149 ns/op 128 B/op 2 allocs/op
BenchmarkDelete/DeleteFrom100-8 1034215 1097 ns/op 960 B/op 15 allocs/op
BenchmarkDelete/DeleteFrom1_000-8 343502 3019 ns/op 2176 B/op 34 allocs/op
BenchmarkDelete/DeleteFrom10_000-8 543692 2128 ns/op 1728 B/op 27 allocs/op
BenchmarkDelete/DeleteFrom100_000-8 375445 3058 ns/op 2048 B/op 32 allocs/op
BenchmarkDelete/DeleteFrom1_000_000-8 266654 5381 ns/op 3200 B/op 50 allocs/op
```

### Lookup
Expand Down
9 changes: 6 additions & 3 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package interval_test

import (
"testing"

"github.com/gaissmai/interval"
)

var intMap = map[int]string{
1: "1",
10: "10",
Expand All @@ -10,8 +16,6 @@ var intMap = map[int]string{
1_000_000: "1_000_000",
}

/*
func BenchmarkInsert(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
tree := interval.NewTree(generateIvals(n)...)
Expand Down Expand Up @@ -131,4 +135,3 @@ func BenchmarkSupersets(b *testing.B) {
})
}
}
*/
30 changes: 16 additions & 14 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func covers[T Interface[T]](a, b T) bool {

// traverse the BST in some order, call the visitor function for each node.
// Prematurely stop traversion if visitor function returns false.
func (t *Node[T]) traverse(order traverseOrder, depth int, visitFn func(n *Node[T], depth int) bool) bool {
func (t *node[T]) traverse(order traverseOrder, depth int, visitFn func(n *node[T], depth int) bool) bool {
if t == nil {
return true
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func (t Tree[T]) Fprint(w io.Writer) error {
var pcm parentChildsMap[T]

// init map
pcm.pcMap = make(map[*Node[T]][]*Node[T])
pcm.pcMap = make(map[*node[T]][]*node[T])

pcm = t.root.buildParentChildsMap(pcm)

Expand All @@ -136,7 +136,7 @@ func (t Tree[T]) Fprint(w io.Writer) error {
return walkAndStringify(w, pcm, nil, "")
}

func walkAndStringify[T Interface[T]](w io.Writer, pcm parentChildsMap[T], parent *Node[T], pad string) error {
func walkAndStringify[T Interface[T]](w io.Writer, pcm parentChildsMap[T], parent *node[T], pad string) error {
// the prefix (pad + glyphe) is already printed on the line on upper level
if parent != nil {
if _, err := fmt.Fprintf(w, "%v\n", parent.item); err != nil {
Expand Down Expand Up @@ -206,7 +206,7 @@ func (t Tree[T]) FprintBST(w io.Writer) error {
}

// preorderStringify, traverse the tree, stringify the nodes in preorder
func (t *Node[T]) preorderStringify(w io.Writer, pad string) error {
func (t *node[T]) preorderStringify(w io.Writer, pad string) error {
// stringify this node
if _, err := fmt.Fprintf(w, "%v [prio:%.4g] [%p|l:%p|r:%p]\n", t.item, t.prio, t, t.left, t.right); err != nil {
return err
Expand Down Expand Up @@ -265,12 +265,12 @@ func (t *Node[T]) preorderStringify(w io.Writer, pad string) error {
// └─ 7...9
//
type parentChildsMap[T Interface[T]] struct {
pcMap map[*Node[T]][]*Node[T] // parent -> []child map
stack []*Node[T] // just needed for the algo
pcMap map[*node[T]][]*node[T] // parent -> []child map
stack []*node[T] // just needed for the algo
}

// buildParentChildsMap, in-order traversal
func (t *Node[T]) buildParentChildsMap(pcm parentChildsMap[T]) parentChildsMap[T] {
func (t *node[T]) buildParentChildsMap(pcm parentChildsMap[T]) parentChildsMap[T] {
if t == nil {
return pcm
}
Expand All @@ -286,7 +286,7 @@ func (t *Node[T]) buildParentChildsMap(pcm parentChildsMap[T]) parentChildsMap[T
}

// pcmForNode, find parent in stack, remove items from stack, put this item on stack.
func (t *Node[T]) pcmForNode(pcm parentChildsMap[T]) parentChildsMap[T] {
func (t *node[T]) pcmForNode(pcm parentChildsMap[T]) parentChildsMap[T] {
// if this item is covered by a prev item on stack
for j := len(pcm.stack) - 1; j >= 0; j-- {

Expand Down Expand Up @@ -320,12 +320,14 @@ func (t *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 *Node[T]) Statistics() (maxDepth int, average, deviation float64) {
func (t Tree[T]) Statistics() (maxDepth int, average, deviation float64) {
n := t.root

// key is depth, value is the sum of nodes with this depth
depths := make(map[int]int)

// get the depths
t.traverse(inorder, 0, func(t *Node[T], depth int) bool {
n.traverse(inorder, 0, func(t *node[T], depth int) bool {
depths[depth] += 1
return true
})
Expand Down Expand Up @@ -379,9 +381,9 @@ func (t Tree[T]) Max() (max T) {

// Size returns the number of items in tree.
func (t Tree[T]) Size() int {
var size int
n := t.root
n.traverse(inorder, 0, func(k *Node[T], _ int) bool {
size := 0
n.traverse(inorder, 0, func(k *node[T], _ int) bool {
size++
return true
})
Expand Down Expand Up @@ -418,7 +420,7 @@ func (t Tree[T]) Visit(start, stop T, visitFn func(t T) bool) {

span := join(mid1, join(l, mid2, immutable), immutable)

span.traverse(order, 0, func(t *Node[T], dummy int) bool {
span.traverse(order, 0, func(t *node[T], dummy int) bool {
return visitFn(t.item)
})
}
Expand All @@ -429,7 +431,7 @@ func (t Tree[T]) Clone() Tree[T] {
return t
}

func (n *Node[T]) clone() *Node[T] {
func (n *node[T]) clone() *node[T] {
if n == nil {
return n
}
Expand Down
Loading

0 comments on commit b1c81b9

Please sign in to comment.