Skip to content

Commit

Permalink
impl. NewTreeConcurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Feb 5, 2023
1 parent 4fc58fb commit 89d0e1f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ To apply this library to types of one-dimensional intervals, you must provide a
type Tree[T any] struct{ ... }
func NewTree[T any](cmp func(a, b T) (ll, rr, lr, rl int), items ...T) Tree[T]

func NewTreeConcurrent[T any](jobs int, 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
39 changes: 26 additions & 13 deletions treap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"
"math/rand"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -65,6 +66,22 @@ func equalStatistics(t1, t2 interval.Tree[uintInterval]) bool {
return a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2
}

func equalsSizeAndOrder[T any](t1, t2 interval.Tree[T]) bool {
var t1InOrder []T
t1.Visit(t1.Min(), t1.Max(), func(item T) bool {
t1InOrder = append(t1InOrder, item)
return true
})

var t2InOrder []T
t2.Visit(t2.Min(), t2.Max(), func(item T) bool {
t2InOrder = append(t2InOrder, item)
return true
})

return reflect.DeepEqual(t1InOrder, t2InOrder)
}

func TestNewTree(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -169,33 +186,29 @@ func TestNewTreeConcurrent(t *testing.T) {
tree1 := interval.NewTree(cmpUintInterval, ivals[0])
tree2 := interval.NewTreeConcurrent(1, cmpUintInterval, ivals[0])

if !equalStatistics(tree1, tree2) {
t.Fatal("New() differs with NewConcurrent(), statistics differ")
if !equalsSizeAndOrder(tree1, tree2) {
t.Fatal("New() differs with NewConcurrent()")
}

tree1 = interval.NewTree(cmpUintInterval, ivals[:2]...)
tree2 = interval.NewTreeConcurrent(2, cmpUintInterval, ivals[:2]...)

if !equalStatistics(tree1, tree2) {
t.Fatal("New() differs with NewConcurrent(), statistics differ")
if !equalsSizeAndOrder(tree1, tree2) {
t.Fatal("New() differs with NewConcurrent()")
}

tree1 = interval.NewTree(cmpUintInterval, ivals[:30_000]...)
tree2 = interval.NewTreeConcurrent(3, cmpUintInterval, ivals[:30_000]...)

if !equalStatistics(tree1, tree2) {
t.Log(tree1.Statistics())
t.Log(tree2.Statistics())
t.Fatal("New() differs with NewConcurrent(), statistics differ")
if !equalsSizeAndOrder(tree1, tree2) {
t.Fatal("New() differs with NewConcurrent()")
}

tree1 = interval.NewTree(cmpUintInterval, ivals...)
tree2 = interval.NewTreeConcurrent(4, cmpUintInterval, ivals...)
tree2 = interval.NewTreeConcurrent(runtime.NumCPU(), cmpUintInterval, ivals...)

if !equalStatistics(tree1, tree2) {
t.Log(tree1.Statistics())
t.Log(tree2.Statistics())
t.Fatal("New() differs with NewConcurrent(), statistics differ")
if !equalsSizeAndOrder(tree1, tree2) {
t.Fatal("New() differs with NewConcurrent()")
}
}

Expand Down

0 comments on commit 89d0e1f

Please sign in to comment.