|
|
|
@ -6,9 +6,20 @@ package redblacktree
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var rng *rand.Rand
|
|
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
|
seed := time.Now().UTC().UnixNano()
|
|
|
|
|
rng = rand.New(rand.NewSource(seed))
|
|
|
|
|
os.Exit(m.Run())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRedBlackTreePut(t *testing.T) {
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
tree.Put(5, "e")
|
|
|
|
@ -19,7 +30,7 @@ func TestRedBlackTreePut(t *testing.T) {
|
|
|
|
|
tree.Put(1, "x")
|
|
|
|
|
tree.Put(2, "b")
|
|
|
|
|
tree.Put(1, "a") //overwrite
|
|
|
|
|
|
|
|
|
|
t.Log(tree)
|
|
|
|
|
if actualValue := tree.Size(); actualValue != 7 {
|
|
|
|
|
t.Errorf("Got %v expected %v", actualValue, 7)
|
|
|
|
|
}
|
|
|
|
@ -557,7 +568,15 @@ func TestRedBlackTreeIteratorLast(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func benchmarkGet(b *testing.B, tree *Tree, size int) {
|
|
|
|
|
func newRandomIntTree(size, randMax int) *Tree {
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
|
tree.Put(rng.Intn(randMax), nil)
|
|
|
|
|
}
|
|
|
|
|
return tree
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tree *Tree) benchmarkGet(b *testing.B, size int) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Get(n)
|
|
|
|
@ -565,147 +584,248 @@ func benchmarkGet(b *testing.B, tree *Tree, size int) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func benchmarkPut(b *testing.B, tree *Tree, size int) {
|
|
|
|
|
func (tree *Tree) benchmarkGetRandom(b *testing.B, size, randMax int) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Get(rng.Intn(randMax))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tree *Tree) benchmarkPut(b *testing.B, size int) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
tree.Empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tree *Tree) benchmarkPutRandom(b *testing.B, size, randMax int) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(rng.Intn(randMax), nil)
|
|
|
|
|
}
|
|
|
|
|
tree.Empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func benchmarkRemove(b *testing.B, tree *Tree, size int) {
|
|
|
|
|
func (tree *Tree) benchmarkPutAndRemove(b *testing.B, size int) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Remove(n)
|
|
|
|
|
}
|
|
|
|
|
tree.Empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tree *Tree) benchmarkPutAndRemoveRandom(b *testing.B, size int, randMax int) {
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(rng.Intn(randMax), nil)
|
|
|
|
|
}
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Remove(rng.Intn(randMax))
|
|
|
|
|
}
|
|
|
|
|
tree.Empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeGet100(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLGet100(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkGet(b, tree, size)
|
|
|
|
|
tree.benchmarkGet(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeGet1000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLGet1000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 1000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkGet(b, tree, size)
|
|
|
|
|
tree.benchmarkGet(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeGet10000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLGet10000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 10000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkGet(b, tree, size)
|
|
|
|
|
tree.benchmarkGet(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeGet100000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLGet100000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkGet(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLGetRandom100(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkGetRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLGetRandom1000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 1000
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkGetRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLGetRandom10000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 10000
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkGetRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLGetRandom100000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100000
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkGet(b, tree, size)
|
|
|
|
|
tree.benchmarkGetRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreePut100(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPut100(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkPut(b, tree, size)
|
|
|
|
|
tree.benchmarkPut(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreePut1000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPut1000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 1000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkPut(b, tree, size)
|
|
|
|
|
tree.benchmarkPut(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreePut10000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPut10000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 10000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkPut(b, tree, size)
|
|
|
|
|
tree.benchmarkPut(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreePut100000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPut100000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkPut(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLPutRandom100(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkPutRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLPutRandom1000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 1000
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkPutRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLPutRandom10000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 10000
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
tree.benchmarkPutRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkAVLPutRandom100000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100000
|
|
|
|
|
tree := newRandomIntTree(size, size*5)
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkPut(b, tree, size)
|
|
|
|
|
tree.benchmarkPutRandom(b, size, size*5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeRemove100(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPutAndRemove100(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkRemove(b, tree, size)
|
|
|
|
|
tree.benchmarkPutAndRemove(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeRemove1000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPutAndRemove1000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 1000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkRemove(b, tree, size)
|
|
|
|
|
tree.benchmarkPutAndRemove(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeRemove10000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPutAndRemove10000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 10000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkRemove(b, tree, size)
|
|
|
|
|
tree.benchmarkPutAndRemove(b, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkRedBlackTreeRemove100000(b *testing.B) {
|
|
|
|
|
func BenchmarkAVLPutAndRemove100000(b *testing.B) {
|
|
|
|
|
b.StopTimer()
|
|
|
|
|
size := 100000
|
|
|
|
|
tree := NewWithIntComparator()
|
|
|
|
|
for n := 0; n < size; n++ {
|
|
|
|
|
tree.Put(n, struct{}{})
|
|
|
|
|
tree.Put(n, nil)
|
|
|
|
|
}
|
|
|
|
|
b.StartTimer()
|
|
|
|
|
benchmarkRemove(b, tree, size)
|
|
|
|
|
tree.benchmarkPutAndRemove(b, size)
|
|
|
|
|
}
|
|
|
|
|