Skip to content

Commit

Permalink
API change, NewTree
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 27, 2023
1 parent dcc730e commit 680da66
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 57 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ To apply this library to types of one-dimensional intervals, you must provide a
import "github.com/gaissmai/interval"

type Tree[T any] struct{ ... }
func NewTree[T any](cmp func(a, b T) (ll, rr, lr, rl int)) Tree[T]
func NewTree[T any](cmp func(a, b T) (ll, rr, lr, rl int), items ...T) Tree[T]

func (t Tree[T]) Insert(items ...T) Tree[T]
func (t Tree[T]) Delete(item T) (Tree[T], bool)
Expand Down
42 changes: 20 additions & 22 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ var intMap = map[int]string{
1_000_000: "1_000_000",
}

var tree = interval.NewTree(compareIval)

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

probe := generateIvals(1)[0]
name := "Into" + intMap[n]
Expand All @@ -37,7 +35,7 @@ func BenchmarkInsert(b *testing.B) {

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

Expand All @@ -55,7 +53,7 @@ func BenchmarkDelete(b *testing.B) {
ivals := generateIvals(n)
probe := ivals[rand.Intn(len(ivals))]

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

b.Run(name, func(b *testing.B) {
Expand All @@ -72,7 +70,7 @@ func BenchmarkDeleteMutable(b *testing.B) {
ivals := generateIvals(n)
probe := ivals[rand.Intn(len(ivals))]

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

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

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

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

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

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

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

b.Run(name, func(b *testing.B) {
Expand All @@ -131,7 +129,7 @@ func BenchmarkUnionMutable(b *testing.B) {
func BenchmarkIntersects(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := tree.Insert(ivals...)
tree := interval.NewTree(compareIval, ivals...)
probe := ivals[rand.Intn(len(ivals))]
name := "In" + intMap[n]

Expand All @@ -147,7 +145,7 @@ func BenchmarkIntersects(b *testing.B) {
func BenchmarkFind(b *testing.B) {
for n := 1; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := tree.Insert(ivals...)
tree := interval.NewTree(compareIval, ivals...)
probe := ivals[rand.Intn(len(ivals))]
name := "In" + intMap[n]

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

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

Expand All @@ -177,7 +175,7 @@ func BenchmarkCoverLCP(b *testing.B) {

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

Expand All @@ -192,7 +190,7 @@ func BenchmarkCoverSCP(b *testing.B) {

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

Expand All @@ -207,7 +205,7 @@ func BenchmarkCoveredBy(b *testing.B) {

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

Expand All @@ -222,7 +220,7 @@ func BenchmarkCovers(b *testing.B) {

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

Expand All @@ -237,7 +235,7 @@ func BenchmarkPrecededBy(b *testing.B) {

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

Expand All @@ -252,7 +250,7 @@ func BenchmarkPrecedes(b *testing.B) {

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

Expand All @@ -268,7 +266,7 @@ func BenchmarkIntersections(b *testing.B) {
func BenchmarkMin(b *testing.B) {
for n := 100; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := tree.Insert(ivals...)
tree := interval.NewTree(compareIval, ivals...)
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand All @@ -283,7 +281,7 @@ func BenchmarkMin(b *testing.B) {
func BenchmarkMax(b *testing.B) {
for n := 100; n <= 1_000_000; n *= 10 {
ivals := generateIvals(n)
tree := tree.Insert(ivals...)
tree := interval.NewTree(compareIval, ivals...)
name := "In" + intMap[n]

b.Run(name, func(b *testing.B) {
Expand Down
13 changes: 6 additions & 7 deletions example_period_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func compareIval(p, q Ival) (ll, rr, lr, rl int) {
}

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

tree1.Fprint(os.Stdout)
// Output:
Expand All @@ -53,7 +52,7 @@ func ExampleNewTree() {
}

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

fmt.Println("\nInterval with max value in tree:")
Expand All @@ -71,7 +70,7 @@ func ExampleTree_Max() {
}

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

item := Ival{3, 4}
Expand All @@ -94,7 +93,7 @@ func ExampleTree_Covers() {
}

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

item := Ival{3, 10}
Expand All @@ -117,7 +116,7 @@ func ExampleTree_CoveredBy() {
}

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

item := Ival{6, 6}
Expand All @@ -139,7 +138,7 @@ func ExampleTree_Precedes_period() {
}

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

Expand Down
10 changes: 4 additions & 6 deletions example_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func compareTval(p, q Tval) (ll, rr, lr, rl int) {
cmpTime(p.death, q.birth)
}

var timeTree = interval.NewTree(compareTval)

// example data
var physicists = []Tval{
mkTval(1473, 1543, "Kopernikus"),
Expand Down Expand Up @@ -82,11 +80,11 @@ var physicists = []Tval{
}

func ExampleTree_Precedes_time() {
tree := timeTree.Insert(physicists...)
tree := interval.NewTree(compareTval, physicists...)
tree.Fprint(os.Stdout)

precedes := tree.Precedes(mkTval(1643, 1727, "Newton"))
tree = timeTree.Insert(precedes...)
tree = interval.NewTree(compareTval, precedes...)

fmt.Println("\nPrecedes Newton:")
tree.Fprint(os.Stdout)
Expand Down Expand Up @@ -131,11 +129,11 @@ func ExampleTree_Precedes_time() {
}

func ExampleTree_PrecededBy_time() {
tree := timeTree.Insert(physicists...)
tree := interval.NewTree(compareTval, physicists...)
tree.Fprint(os.Stdout)

precededBy := tree.PrecededBy(mkTval(1643, 1727, "Newton"))
tree = timeTree.Insert(precededBy...)
tree = interval.NewTree(compareTval, precededBy...)

fmt.Println("\nPrecededBy Newton:")
tree.Fprint(os.Stdout)
Expand Down
10 changes: 8 additions & 2 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Tree[T any] struct {
cmp func(T, T) (ll, rr, lr, rl int)
}

// NewTree initializes the interval tree with the interval comparison function for type T.
// NewTree initializes the interval tree with the compare function and items from type T.
//
// cmp(a, b T) (ll, rr, lr, rl int)
//
Expand All @@ -43,9 +43,15 @@ type Tree[T any] struct {
// lr: left point interval a compared with right point interval b (-1, 0, +1)
// rl: right point interval a compared with left point interval b (-1, 0, +1)
//
func NewTree[T any](cmp func(a, b T) (ll, rr, lr, rl int)) Tree[T] {
func NewTree[T any](cmp func(a, b T) (ll, rr, lr, rl int), items ...T) Tree[T] {
var t Tree[T]
t.cmp = cmp

// mutable insert
for i := range items {
t.root = t.root.insert(t.makeNode(items[i]), false, &t)
}

return t
}

Expand Down
Loading

0 comments on commit 680da66

Please sign in to comment.