准备修改benmark test的一些方式, 达到最佳

This commit is contained in:
huangsimin 2019-04-09 10:39:14 +08:00
parent fc49f20936
commit 1f3df9205f
18 changed files with 147 additions and 270 deletions

View File

@ -53,14 +53,14 @@ func (tree *Tree) Size() int {
return tree.size return tree.size
} }
func (tree *Tree) Remove(key interface{}) *Node { func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok { if n, ok := tree.GetNode(key); ok {
tree.size-- tree.size--
if tree.size == 0 { if tree.size == 0 {
tree.root = nil tree.root = nil
return n return n.value, true
} }
left := getHeight(n.children[0]) left := getHeight(n.children[0])
@ -70,7 +70,7 @@ func (tree *Tree) Remove(key interface{}) *Node {
p := n.parent p := n.parent
p.children[getRelationship(n)] = nil p.children[getRelationship(n)] = nil
tree.fixRemoveHeight(p) tree.fixRemoveHeight(p)
return n return n.value, true
} }
var cur *Node var cur *Node
@ -111,10 +111,10 @@ func (tree *Tree) Remove(key interface{}) *Node {
tree.fixRemoveHeight(cparent) tree.fixRemoveHeight(cparent)
} }
return cur return cur.value, true
} }
return nil return nil, false
} }
// Values 返回先序遍历的值 // Values 返回先序遍历的值
@ -153,7 +153,6 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
// iter := NewIterator(min)
tree.iter.SetNode(min) tree.iter.SetNode(min)
iter := tree.iter iter := tree.iter
for iter.Next() { for iter.Next() {
@ -180,7 +179,6 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
// iter := NewIterator(max)
tree.iter.SetNode(max) tree.iter.SetNode(max)
iter := tree.iter iter := tree.iter
for iter.Prev() { for iter.Prev() {
@ -232,7 +230,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
n = n.children[1] n = n.children[1]
lastc = c lastc = c
case 0: case 0:
// iter := NewIterator(n)
tree.iter.SetNode(n) tree.iter.SetNode(n)
iter := tree.iter iter := tree.iter
iter.Prev() iter.Prev()
@ -292,10 +290,8 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
case 1: case 1:
n = n.children[1] n = n.children[1]
case 0: case 0:
// iter := NewIterator(n)
tree.iter.SetNode(n) tree.iter.SetNode(n)
iter := tree.iter iter := tree.iter
iter.Prev() iter.Prev()
for iter.Prev() { for iter.Prev() {

View File

@ -5,7 +5,6 @@ import (
"encoding/gob" "encoding/gob"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"testing" "testing"
"474420502.top/eson/structure/compare" "474420502.top/eson/structure/compare"
@ -15,36 +14,6 @@ import (
"github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/trees/redblacktree"
) )
const CompartorSize = 100
const NumberMax = 600
func Save(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
//fmt.Println(userBytes)
var l []int
// m := make(map[int]int)
for i := 0; len(l) < CompartorSize; i++ {
v := randomdata.Number(0, NumberMax)
// if _, ok := m[v]; !ok {
// m[v] = v
l = append(l, v)
// }
}
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
encoder.Encode(l)
lbytes := result.Bytes()
f.Write(lbytes)
}
func loadTestData() []int { func loadTestData() []int {
data, err := ioutil.ReadFile("../l.log") data, err := ioutil.ReadFile("../l.log")
if err != nil { if err != nil {

View File

@ -28,10 +28,12 @@ type Tree struct {
root *Node root *Node
size int size int
Compare compare.Compare Compare compare.Compare
iter *Iterator
} }
func New(Compare compare.Compare) *Tree { func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare} return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
} }
func (tree *Tree) String() string { func (tree *Tree) String() string {
@ -52,14 +54,14 @@ func (tree *Tree) Size() int {
return tree.size return tree.size
} }
func (tree *Tree) Remove(key interface{}) *Node { func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok { if n, ok := tree.GetNode(key); ok {
tree.size-- tree.size--
if tree.size == 0 { if tree.size == 0 {
tree.root = nil tree.root = nil
return n return n.value, true
} }
left := getHeight(n.children[0]) left := getHeight(n.children[0])
@ -69,7 +71,7 @@ func (tree *Tree) Remove(key interface{}) *Node {
p := n.parent p := n.parent
p.children[getRelationship(n)] = nil p.children[getRelationship(n)] = nil
tree.fixRemoveHeight(p) tree.fixRemoveHeight(p)
return n return n.value, true
} }
var cur *Node var cur *Node
@ -109,11 +111,10 @@ func (tree *Tree) Remove(key interface{}) *Node {
} else { } else {
tree.fixRemoveHeight(cparent) tree.fixRemoveHeight(cparent)
} }
return cur.value, true
return cur
} }
return nil return nil, false
} }
// Values 返回先序遍历的值 // Values 返回先序遍历的值
@ -152,7 +153,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
iter := NewIterator(min) tree.iter.SetNode(min)
iter := tree.iter
for iter.Next() { for iter.Next() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == max { if iter.cur == max {
@ -177,7 +179,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
iter := NewIterator(max) tree.iter.SetNode(max)
iter := tree.iter
for iter.Prev() { for iter.Prev() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == min { if iter.cur == min {
@ -239,21 +242,21 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
if result[1] != nil { if result[1] != nil {
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} else { } else {
result[0] = last result[0] = last
result[2] = GetNext(last, 1) result[2] = tree.iter.GetNext(last, 1)
} }
case -1: case -1:
if result[1] != nil { if result[1] != nil {
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} else { } else {
result[2] = last result[2] = last
result[0] = GetPrev(last, 1) result[0] = tree.iter.GetPrev(last, 1)
} }
case 0: case 0:
@ -261,8 +264,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
if result[1] == nil { if result[1] == nil {
return return
} }
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} }
return return
} }
@ -543,10 +546,6 @@ func (tree *Tree) rlrotate(cur *Node) {
cur.children[r] = mov cur.children[r] = mov
mov.parent = cur mov.parent = cur
// movparent.size = getChildrenSumSize(movparent) + 1
// mov.size = getChildrenSumSize(mov) + 1
// cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1 mov.height = getMaxChildrenHeight(mov) + 1
movparent.height = getMaxChildrenHeight(movparent) + 1 movparent.height = getMaxChildrenHeight(movparent) + 1
cur.height = getMaxChildrenHeight(cur) + 1 cur.height = getMaxChildrenHeight(cur) + 1
@ -620,9 +619,6 @@ func (tree *Tree) rrotate(cur *Node) {
// 连接转移后的节点 由于mov只是与cur交换值,parent不变 // 连接转移后的节点 由于mov只是与cur交换值,parent不变
cur.children[r] = mov cur.children[r] = mov
// mov.size = getChildrenSumSize(mov) + 1
// cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1 mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1 cur.height = getMaxChildrenHeight(cur) + 1
} }
@ -659,9 +655,6 @@ func (tree *Tree) lrotateex(cur *Node) {
cur.children[r] = mov cur.children[r] = mov
// mov.size = getChildrenSumSize(mov) + 1
// cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1 mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1 cur.height = getMaxChildrenHeight(cur) + 1
} }
@ -694,9 +687,6 @@ func (tree *Tree) lrotate(cur *Node) {
cur.children[r] = mov cur.children[r] = mov
// mov.size = getChildrenSumSize(mov) + 1
// cur.size = getChildrenSumSize(cur) + 1
mov.height = getMaxChildrenHeight(mov) + 1 mov.height = getMaxChildrenHeight(mov) + 1
cur.height = getMaxChildrenHeight(cur) + 1 cur.height = getMaxChildrenHeight(cur) + 1
} }

View File

@ -9,6 +9,7 @@ type Iterator struct {
up *Node up *Node
cur *Node cur *Node
tstack *lastack.Stack tstack *lastack.Stack
// curnext *Node
} }
func initIterator(avltree *Tree) *Iterator { func initIterator(avltree *Tree) *Iterator {
@ -23,6 +24,18 @@ func NewIterator(n *Node) *Iterator {
return iter return iter
} }
func NewIteratorWithCap(n *Node, cap int) *Iterator {
iter := &Iterator{tstack: lastack.NewWithCap(cap)}
iter.up = n
return iter
}
func (iter *Iterator) SetNode(n *Node) {
iter.up = n
iter.dir = 0
iter.tstack.Clear()
}
func (iter *Iterator) Value() interface{} { func (iter *Iterator) Value() interface{} {
return iter.cur.value return iter.cur.value
} }
@ -45,9 +58,10 @@ func (iter *Iterator) Right() bool {
return false return false
} }
func GetNext(cur *Node, idx int) *Node { func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
iter := NewIterator(cur) // iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushNextStack(iter.up) iter.curPushNextStack(iter.up)
iter.up = iter.getNextUp(iter.up) iter.up = iter.getNextUp(iter.up)
@ -102,9 +116,10 @@ func (iter *Iterator) Next() (result bool) {
return false return false
} }
func GetPrev(cur *Node, idx int) *Node { func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
iter := NewIterator(cur) // iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushPrevStack(iter.up) iter.curPushPrevStack(iter.up)
iter.up = iter.getPrevUp(iter.up) iter.up = iter.getPrevUp(iter.up)

View File

@ -28,10 +28,12 @@ type Tree struct {
root *Node root *Node
size int size int
Compare compare.Compare Compare compare.Compare
iter *Iterator
} }
func New(Compare compare.Compare) *Tree { func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare} return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
} }
func (tree *Tree) String() string { func (tree *Tree) String() string {
@ -52,14 +54,14 @@ func (tree *Tree) Size() int {
return tree.size return tree.size
} }
func (tree *Tree) Remove(key interface{}) *Node { func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok { if n, ok := tree.GetNode(key); ok {
tree.size-- tree.size--
if tree.size == 0 { if tree.size == 0 {
tree.root = nil tree.root = nil
return n return n.value, true
} }
left := getHeight(n.children[0]) left := getHeight(n.children[0])
@ -69,7 +71,7 @@ func (tree *Tree) Remove(key interface{}) *Node {
p := n.parent p := n.parent
p.children[getRelationship(n)] = nil p.children[getRelationship(n)] = nil
tree.fixRemoveHeight(p) tree.fixRemoveHeight(p)
return n return n.value, true
} }
var cur *Node var cur *Node
@ -110,10 +112,10 @@ func (tree *Tree) Remove(key interface{}) *Node {
tree.fixRemoveHeight(cparent) tree.fixRemoveHeight(cparent)
} }
return cur return cur.value, true
} }
return nil return nil, false
} }
// Values 返回先序遍历的值 // Values 返回先序遍历的值
@ -152,7 +154,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
iter := NewIterator(min) tree.iter.SetNode(min)
iter := tree.iter
for iter.Next() { for iter.Next() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == max { if iter.cur == max {
@ -177,7 +180,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
iter := NewIterator(max) tree.iter.SetNode(max)
iter := tree.iter
for iter.Prev() { for iter.Prev() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == min { if iter.cur == min {
@ -227,7 +231,9 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
n = n.children[1] n = n.children[1]
lastc = c lastc = c
case 0: case 0:
iter := NewIterator(n)
tree.iter.SetNode(n)
iter := tree.iter
iter.Prev() iter.Prev()
for iter.Prev() { for iter.Prev() {
if tree.Compare(iter.cur.value, n.value) == 0 { if tree.Compare(iter.cur.value, n.value) == 0 {
@ -248,21 +254,21 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
if result[1] != nil { if result[1] != nil {
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} else { } else {
result[0] = last result[0] = last
result[2] = GetNext(last, 1) result[2] = tree.iter.GetNext(last, 1)
} }
case -1: case -1:
if result[1] != nil { if result[1] != nil {
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} else { } else {
result[2] = last result[2] = last
result[0] = GetPrev(last, 1) result[0] = tree.iter.GetPrev(last, 1)
} }
case 0: case 0:
@ -270,8 +276,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
if result[1] == nil { if result[1] == nil {
return return
} }
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} }
return return
} }
@ -285,7 +291,8 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
case 1: case 1:
n = n.children[1] n = n.children[1]
case 0: case 0:
iter := NewIterator(n) tree.iter.SetNode(n)
iter := tree.iter
iter.Prev() iter.Prev()
for iter.Prev() { for iter.Prev() {
if tree.Compare(iter.cur.key, n.key) == 0 { if tree.Compare(iter.cur.key, n.key) == 0 {

View File

@ -16,36 +16,6 @@ import (
"github.com/emirpasic/gods/utils" "github.com/emirpasic/gods/utils"
) )
const CompartorSize = 100
const NumberMax = 600
func Save(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
//fmt.Println(userBytes)
var l []int
// m := make(map[int]int)
for i := 0; len(l) < CompartorSize; i++ {
v := randomdata.Number(0, NumberMax)
// if _, ok := m[v]; !ok {
// m[v] = v
l = append(l, v)
// }
}
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
encoder.Encode(l)
lbytes := result.Bytes()
f.Write(lbytes)
}
func loadTestData() []int { func loadTestData() []int {
data, err := ioutil.ReadFile("../l.log") data, err := ioutil.ReadFile("../l.log")
if err != nil { if err != nil {

View File

@ -24,6 +24,18 @@ func NewIterator(n *Node) *Iterator {
return iter return iter
} }
func NewIteratorWithCap(n *Node, cap int) *Iterator {
iter := &Iterator{tstack: lastack.NewWithCap(cap)}
iter.up = n
return iter
}
func (iter *Iterator) SetNode(n *Node) {
iter.up = n
iter.dir = 0
iter.tstack.Clear()
}
func (iter *Iterator) Value() interface{} { func (iter *Iterator) Value() interface{} {
return iter.cur.value return iter.cur.value
} }
@ -46,9 +58,10 @@ func (iter *Iterator) Right() bool {
return false return false
} }
func GetNext(cur *Node, idx int) *Node { func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
iter := NewIterator(cur) // iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushNextStack(iter.up) iter.curPushNextStack(iter.up)
iter.up = iter.getNextUp(iter.up) iter.up = iter.getNextUp(iter.up)
@ -103,9 +116,10 @@ func (iter *Iterator) Next() (result bool) {
return false return false
} }
func GetPrev(cur *Node, idx int) *Node { func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
iter := NewIterator(cur) // iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushPrevStack(iter.up) iter.curPushPrevStack(iter.up)
iter.up = iter.getPrevUp(iter.up) iter.up = iter.getPrevUp(iter.up)

View File

@ -28,10 +28,12 @@ type Tree struct {
root *Node root *Node
size int size int
Compare compare.Compare Compare compare.Compare
iter *Iterator
} }
func New(Compare compare.Compare) *Tree { func New(Compare compare.Compare) *Tree {
return &Tree{Compare: Compare} return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)}
} }
func (tree *Tree) String() string { func (tree *Tree) String() string {
@ -52,14 +54,14 @@ func (tree *Tree) Size() int {
return tree.size return tree.size
} }
func (tree *Tree) Remove(key interface{}) *Node { func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok { if n, ok := tree.GetNode(key); ok {
tree.size-- tree.size--
if tree.size == 0 { if tree.size == 0 {
tree.root = nil tree.root = nil
return n return n.value, true
} }
left := getHeight(n.children[0]) left := getHeight(n.children[0])
@ -69,7 +71,7 @@ func (tree *Tree) Remove(key interface{}) *Node {
p := n.parent p := n.parent
p.children[getRelationship(n)] = nil p.children[getRelationship(n)] = nil
tree.fixRemoveHeight(p) tree.fixRemoveHeight(p)
return n return n.value, true
} }
var cur *Node var cur *Node
@ -110,10 +112,10 @@ func (tree *Tree) Remove(key interface{}) *Node {
tree.fixRemoveHeight(cparent) tree.fixRemoveHeight(cparent)
} }
return cur return cur.value, true
} }
return nil return nil, false
} }
// Values 返回先序遍历的值 // Values 返回先序遍历的值
@ -152,7 +154,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
iter := NewIterator(min) tree.iter.SetNode(min)
iter := tree.iter
for iter.Next() { for iter.Next() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == max { if iter.cur == max {
@ -177,7 +180,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
result = make([]interface{}, 0, 16) result = make([]interface{}, 0, 16)
iter := NewIterator(max) tree.iter.SetNode(max)
iter := tree.iter
for iter.Prev() { for iter.Prev() {
result = append(result, iter.Value()) result = append(result, iter.Value())
if iter.cur == min { if iter.cur == min {
@ -227,7 +231,9 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
n = n.children[1] n = n.children[1]
lastc = c lastc = c
case 0: case 0:
iter := NewIterator(n)
tree.iter.SetNode(n)
iter := tree.iter
iter.Prev() iter.Prev()
for iter.Prev() { for iter.Prev() {
if tree.Compare(iter.cur.value, n.value) == 0 { if tree.Compare(iter.cur.value, n.value) == 0 {
@ -248,21 +254,21 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
if result[1] != nil { if result[1] != nil {
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} else { } else {
result[0] = last result[0] = last
result[2] = GetNext(last, 1) result[2] = tree.iter.GetNext(last, 1)
} }
case -1: case -1:
if result[1] != nil { if result[1] != nil {
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} else { } else {
result[2] = last result[2] = last
result[0] = GetPrev(last, 1) result[0] = tree.iter.GetPrev(last, 1)
} }
case 0: case 0:
@ -270,8 +276,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
if result[1] == nil { if result[1] == nil {
return return
} }
result[0] = GetPrev(result[1], 1) result[0] = tree.iter.GetPrev(result[1], 1)
result[2] = GetNext(result[1], 1) result[2] = tree.iter.GetNext(result[1], 1)
} }
return return
} }
@ -285,7 +291,9 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
case 1: case 1:
n = n.children[1] n = n.children[1]
case 0: case 0:
iter := NewIterator(n)
tree.iter.SetNode(n)
iter := tree.iter
iter.Prev() iter.Prev()
for iter.Prev() { for iter.Prev() {
if tree.Compare(iter.cur.key, n.key) == 0 { if tree.Compare(iter.cur.key, n.key) == 0 {

View File

@ -24,6 +24,18 @@ func NewIterator(n *Node) *Iterator {
return iter return iter
} }
func NewIteratorWithCap(n *Node, cap int) *Iterator {
iter := &Iterator{tstack: lastack.NewWithCap(cap)}
iter.up = n
return iter
}
func (iter *Iterator) SetNode(n *Node) {
iter.up = n
iter.dir = 0
iter.tstack.Clear()
}
func (iter *Iterator) Value() interface{} { func (iter *Iterator) Value() interface{} {
return iter.cur.value return iter.cur.value
} }
@ -46,9 +58,10 @@ func (iter *Iterator) Right() bool {
return false return false
} }
func GetNext(cur *Node, idx int) *Node { func (iter *Iterator) GetNext(cur *Node, idx int) *Node {
iter := NewIterator(cur) // iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushNextStack(iter.up) iter.curPushNextStack(iter.up)
iter.up = iter.getNextUp(iter.up) iter.up = iter.getNextUp(iter.up)
@ -103,9 +116,10 @@ func (iter *Iterator) Next() (result bool) {
return false return false
} }
func GetPrev(cur *Node, idx int) *Node { func (iter *Iterator) GetPrev(cur *Node, idx int) *Node {
iter := NewIterator(cur) // iter := NewIterator(cur)
iter.SetNode(cur)
iter.curPushPrevStack(iter.up) iter.curPushPrevStack(iter.up)
iter.up = iter.getPrevUp(iter.up) iter.up = iter.getPrevUp(iter.up)

View File

@ -1,9 +1 @@
package structure package structure
type IteratorNext interface {
Next() bool
}
type IteratorPrev interface {
Prev() bool
}

View File

@ -67,11 +67,11 @@ func (pq *PriorityQueue) GetRange(k1, k2 interface{}) []interface{} {
return pq.queue.GetRange(k1, k2) return pq.queue.GetRange(k1, k2)
} }
func (pq *PriorityQueue) RemoveIndex(idx int) (*Node, bool) { func (pq *PriorityQueue) RemoveIndex(idx int) (interface{}, bool) {
return pq.queue.RemoveIndex(idx) return pq.queue.RemoveIndex(idx)
} }
func (pq *PriorityQueue) Remove(key interface{}) (*Node, bool) { func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) {
return pq.queue.Remove(key) return pq.queue.Remove(key)
} }

View File

@ -156,11 +156,11 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) {
return nil, false return nil, false
} }
func (tree *vbTree) RemoveIndex(idx int) (*Node, bool) { func (tree *vbTree) RemoveIndex(idx int) (interface{}, bool) {
n := tree.indexNode(idx) n := tree.indexNode(idx)
if n != nil { if n != nil {
tree.removeNode(n) tree.removeNode(n)
return n, true return n.value, true
} }
return nil, false return nil, false
} }
@ -229,11 +229,11 @@ func (tree *vbTree) removeNode(n *Node) {
return return
} }
func (tree *vbTree) Remove(key interface{}) (*Node, bool) { func (tree *vbTree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok { if n, ok := tree.GetNode(key); ok {
tree.removeNode(n) tree.removeNode(n)
return n, true return n.value, true
} }
// return nil // return nil
return nil, false return nil, false

View File

@ -5,7 +5,6 @@ import (
"encoding/gob" "encoding/gob"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"testing" "testing"
"474420502.top/eson/structure/compare" "474420502.top/eson/structure/compare"
@ -17,38 +16,6 @@ import (
"github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/trees/redblacktree"
) )
const CompareSize = 1000000
const NumberMax = 50000000
func Save(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
var l []int
// for i := 0; len(l) < 1000; i++ {
// v := randomdata.Number(0, 65535)
// l = append(l, v)
// }
m := make(map[int]int)
for i := 0; len(l) < CompareSize; i++ {
v := randomdata.Number(0, NumberMax)
if _, ok := m[v]; !ok {
m[v] = v
l = append(l, v)
}
}
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
encoder.Encode(l)
lbytes := result.Bytes()
f.Write(lbytes)
}
func loadTestData() []int { func loadTestData() []int {
data, err := ioutil.ReadFile("../l.log") data, err := ioutil.ReadFile("../l.log")
if err != nil { if err != nil {

1
test.sh Normal file
View File

@ -0,0 +1 @@
cd avl ; go test -v

View File

@ -154,11 +154,11 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
return nil, false return nil, false
} }
func (tree *Tree) RemoveIndex(idx int) (*Node, bool) { func (tree *Tree) RemoveIndex(idx int) (interface{}, bool) {
n := tree.indexNode(idx) n := tree.indexNode(idx)
if n != nil { if n != nil {
tree.removeNode(n) tree.removeNode(n)
return n, true return n.value, true
} }
return nil, false return nil, false
} }
@ -221,11 +221,11 @@ func (tree *Tree) removeNode(n *Node) {
return return
} }
func (tree *Tree) Remove(key interface{}) (*Node, bool) { func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok { if n, ok := tree.GetNode(key); ok {
tree.removeNode(n) tree.removeNode(n)
return n, true return n.value, true
} }
// return nil // return nil
return nil, false return nil, false

View File

@ -5,7 +5,6 @@ import (
"encoding/gob" "encoding/gob"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"testing" "testing"
"474420502.top/eson/structure/compare" "474420502.top/eson/structure/compare"
@ -17,38 +16,6 @@ import (
"github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/trees/redblacktree"
) )
const CompareSize = 1000000
const NumberMax = 50000000
func TestSave(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
var l []int
// for i := 0; len(l) < 1000; i++ {
// v := randomdata.Number(0, 65535)
// l = append(l, v)
// }
m := make(map[int]int)
for i := 0; len(l) < CompareSize; i++ {
v := randomdata.Number(0, NumberMax)
if _, ok := m[v]; !ok {
m[v] = v
l = append(l, v)
}
}
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
encoder.Encode(l)
lbytes := result.Bytes()
f.Write(lbytes)
}
func loadTestData() []int { func loadTestData() []int {
data, err := ioutil.ReadFile("../l.log") data, err := ioutil.ReadFile("../l.log")
if err != nil { if err != nil {

View File

@ -155,11 +155,11 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
return nil, false return nil, false
} }
func (tree *Tree) RemoveIndex(idx int) (*Node, bool) { func (tree *Tree) RemoveIndex(idx int) (interface{}, bool) {
n := tree.indexNode(idx) n := tree.indexNode(idx)
if n != nil { if n != nil {
tree.removeNode(n) tree.removeNode(n)
return n, true return n.value, true
} }
return nil, false return nil, false
} }
@ -222,11 +222,11 @@ func (tree *Tree) removeNode(n *Node) {
return return
} }
func (tree *Tree) Remove(key interface{}) (*Node, bool) { func (tree *Tree) Remove(key interface{}) (interface{}, bool) {
if n, ok := tree.GetNode(key); ok { if n, ok := tree.GetNode(key); ok {
tree.removeNode(n) tree.removeNode(n)
return n, true return n.value, true
} }
// return nil // return nil
return nil, false return nil, false

View File

@ -5,7 +5,6 @@ import (
"encoding/gob" "encoding/gob"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"testing" "testing"
"474420502.top/eson/structure/compare" "474420502.top/eson/structure/compare"
@ -17,38 +16,6 @@ import (
"github.com/emirpasic/gods/trees/redblacktree" "github.com/emirpasic/gods/trees/redblacktree"
) )
const CompareSize = 1000000
const NumberMax = 50000000
func TestSave(t *testing.T) {
f, err := os.OpenFile("../l.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
log.Println(err)
}
var l []int
// for i := 0; len(l) < 1000; i++ {
// v := randomdata.Number(0, 65535)
// l = append(l, v)
// }
//m := make(map[int]int)
for i := 0; len(l) < CompareSize; i++ {
v := randomdata.Number(0, NumberMax)
// if _, ok := m[v]; !ok {
// m[v] = v
l = append(l, v)
// }
}
var result bytes.Buffer
encoder := gob.NewEncoder(&result)
encoder.Encode(l)
lbytes := result.Bytes()
f.Write(lbytes)
}
func loadTestData() []int { func loadTestData() []int {
data, err := ioutil.ReadFile("../l.log") data, err := ioutil.ReadFile("../l.log")
if err != nil { if err != nil {