diff --git a/avl/avl.go b/avl/avl.go index e67cc2f..53edc30 100644 --- a/avl/avl.go +++ b/avl/avl.go @@ -53,14 +53,14 @@ func (tree *Tree) Size() int { 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 { tree.size-- if tree.size == 0 { tree.root = nil - return n + return n.value, true } left := getHeight(n.children[0]) @@ -70,7 +70,7 @@ func (tree *Tree) Remove(key interface{}) *Node { p := n.parent p.children[getRelationship(n)] = nil tree.fixRemoveHeight(p) - return n + return n.value, true } var cur *Node @@ -111,10 +111,10 @@ func (tree *Tree) Remove(key interface{}) *Node { tree.fixRemoveHeight(cparent) } - return cur + return cur.value, true } - return nil + return nil, false } // Values 返回先序遍历的值 @@ -153,7 +153,6 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - // iter := NewIterator(min) tree.iter.SetNode(min) iter := tree.iter for iter.Next() { @@ -180,7 +179,6 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - // iter := NewIterator(max) tree.iter.SetNode(max) iter := tree.iter for iter.Prev() { @@ -232,7 +230,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { n = n.children[1] lastc = c case 0: - // iter := NewIterator(n) + tree.iter.SetNode(n) iter := tree.iter iter.Prev() @@ -292,10 +290,8 @@ func (tree *Tree) GetNode(value interface{}) (*Node, bool) { case 1: n = n.children[1] case 0: - // iter := NewIterator(n) tree.iter.SetNode(n) - iter := tree.iter iter.Prev() for iter.Prev() { diff --git a/avl/avl_test.go b/avl/avl_test.go index 86da345..bcd6423 100644 --- a/avl/avl_test.go +++ b/avl/avl_test.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "io/ioutil" "log" - "os" "testing" "474420502.top/eson/structure/compare" @@ -15,36 +14,6 @@ import ( "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 { data, err := ioutil.ReadFile("../l.log") if err != nil { diff --git a/avldup/avldup.go b/avldup/avldup.go index 8f811b1..bf22d0b 100644 --- a/avldup/avldup.go +++ b/avldup/avldup.go @@ -28,10 +28,12 @@ type Tree struct { root *Node size int Compare compare.Compare + + iter *Iterator } func New(Compare compare.Compare) *Tree { - return &Tree{Compare: Compare} + return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } func (tree *Tree) String() string { @@ -52,14 +54,14 @@ func (tree *Tree) Size() int { 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 { tree.size-- if tree.size == 0 { tree.root = nil - return n + return n.value, true } left := getHeight(n.children[0]) @@ -69,7 +71,7 @@ func (tree *Tree) Remove(key interface{}) *Node { p := n.parent p.children[getRelationship(n)] = nil tree.fixRemoveHeight(p) - return n + return n.value, true } var cur *Node @@ -109,11 +111,10 @@ func (tree *Tree) Remove(key interface{}) *Node { } else { tree.fixRemoveHeight(cparent) } - - return cur + return cur.value, true } - return nil + return nil, false } // Values 返回先序遍历的值 @@ -152,7 +153,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - iter := NewIterator(min) + tree.iter.SetNode(min) + iter := tree.iter for iter.Next() { result = append(result, iter.Value()) if iter.cur == max { @@ -177,7 +179,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - iter := NewIterator(max) + tree.iter.SetNode(max) + iter := tree.iter for iter.Prev() { result = append(result, iter.Value()) if iter.cur == min { @@ -239,21 +242,21 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { if result[1] != nil { - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } else { result[0] = last - result[2] = GetNext(last, 1) + result[2] = tree.iter.GetNext(last, 1) } case -1: if result[1] != nil { - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } else { result[2] = last - result[0] = GetPrev(last, 1) + result[0] = tree.iter.GetPrev(last, 1) } case 0: @@ -261,8 +264,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { if result[1] == nil { return } - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } return } @@ -543,10 +546,6 @@ func (tree *Tree) rlrotate(cur *Node) { cur.children[r] = mov mov.parent = cur - // movparent.size = getChildrenSumSize(movparent) + 1 - // mov.size = getChildrenSumSize(mov) + 1 - // cur.size = getChildrenSumSize(cur) + 1 - mov.height = getMaxChildrenHeight(mov) + 1 movparent.height = getMaxChildrenHeight(movparent) + 1 cur.height = getMaxChildrenHeight(cur) + 1 @@ -620,9 +619,6 @@ func (tree *Tree) rrotate(cur *Node) { // 连接转移后的节点 由于mov只是与cur交换值,parent不变 cur.children[r] = mov - // mov.size = getChildrenSumSize(mov) + 1 - // cur.size = getChildrenSumSize(cur) + 1 - mov.height = getMaxChildrenHeight(mov) + 1 cur.height = getMaxChildrenHeight(cur) + 1 } @@ -659,9 +655,6 @@ func (tree *Tree) lrotateex(cur *Node) { cur.children[r] = mov - // mov.size = getChildrenSumSize(mov) + 1 - // cur.size = getChildrenSumSize(cur) + 1 - mov.height = getMaxChildrenHeight(mov) + 1 cur.height = getMaxChildrenHeight(cur) + 1 } @@ -694,9 +687,6 @@ func (tree *Tree) lrotate(cur *Node) { cur.children[r] = mov - // mov.size = getChildrenSumSize(mov) + 1 - // cur.size = getChildrenSumSize(cur) + 1 - mov.height = getMaxChildrenHeight(mov) + 1 cur.height = getMaxChildrenHeight(cur) + 1 } diff --git a/avldup/iterator.go b/avldup/iterator.go index 303ce57..3fe6b63 100644 --- a/avldup/iterator.go +++ b/avldup/iterator.go @@ -9,6 +9,7 @@ type Iterator struct { up *Node cur *Node tstack *lastack.Stack + // curnext *Node } func initIterator(avltree *Tree) *Iterator { @@ -23,6 +24,18 @@ func NewIterator(n *Node) *Iterator { 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{} { return iter.cur.value } @@ -45,9 +58,10 @@ func (iter *Iterator) Right() bool { 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.up = iter.getNextUp(iter.up) @@ -102,9 +116,10 @@ func (iter *Iterator) Next() (result bool) { 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.up = iter.getPrevUp(iter.up) diff --git a/avlkey/avlkey.go b/avlkey/avlkey.go index 7ae1f41..1489c83 100644 --- a/avlkey/avlkey.go +++ b/avlkey/avlkey.go @@ -28,10 +28,12 @@ type Tree struct { root *Node size int Compare compare.Compare + + iter *Iterator } func New(Compare compare.Compare) *Tree { - return &Tree{Compare: Compare} + return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } func (tree *Tree) String() string { @@ -52,14 +54,14 @@ func (tree *Tree) Size() int { 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 { tree.size-- if tree.size == 0 { tree.root = nil - return n + return n.value, true } left := getHeight(n.children[0]) @@ -69,7 +71,7 @@ func (tree *Tree) Remove(key interface{}) *Node { p := n.parent p.children[getRelationship(n)] = nil tree.fixRemoveHeight(p) - return n + return n.value, true } var cur *Node @@ -110,10 +112,10 @@ func (tree *Tree) Remove(key interface{}) *Node { tree.fixRemoveHeight(cparent) } - return cur + return cur.value, true } - return nil + return nil, false } // Values 返回先序遍历的值 @@ -152,7 +154,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - iter := NewIterator(min) + tree.iter.SetNode(min) + iter := tree.iter for iter.Next() { result = append(result, iter.Value()) if iter.cur == max { @@ -177,7 +180,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - iter := NewIterator(max) + tree.iter.SetNode(max) + iter := tree.iter for iter.Prev() { result = append(result, iter.Value()) if iter.cur == min { @@ -227,7 +231,9 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { n = n.children[1] lastc = c case 0: - iter := NewIterator(n) + + tree.iter.SetNode(n) + iter := tree.iter iter.Prev() for iter.Prev() { 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 { - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } else { result[0] = last - result[2] = GetNext(last, 1) + result[2] = tree.iter.GetNext(last, 1) } case -1: if result[1] != nil { - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } else { result[2] = last - result[0] = GetPrev(last, 1) + result[0] = tree.iter.GetPrev(last, 1) } case 0: @@ -270,8 +276,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { if result[1] == nil { return } - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } return } @@ -285,7 +291,8 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) { case 1: n = n.children[1] case 0: - iter := NewIterator(n) + tree.iter.SetNode(n) + iter := tree.iter iter.Prev() for iter.Prev() { if tree.Compare(iter.cur.key, n.key) == 0 { diff --git a/avlkey/avlkey_test.go b/avlkey/avlkey_test.go index 8581387..ad9da8f 100644 --- a/avlkey/avlkey_test.go +++ b/avlkey/avlkey_test.go @@ -16,36 +16,6 @@ import ( "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 { data, err := ioutil.ReadFile("../l.log") if err != nil { diff --git a/avlkey/iterator.go b/avlkey/iterator.go index b8173b2..555efa8 100644 --- a/avlkey/iterator.go +++ b/avlkey/iterator.go @@ -24,6 +24,18 @@ func NewIterator(n *Node) *Iterator { 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{} { return iter.cur.value } @@ -46,9 +58,10 @@ func (iter *Iterator) Right() bool { 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.up = iter.getNextUp(iter.up) @@ -103,9 +116,10 @@ func (iter *Iterator) Next() (result bool) { 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.up = iter.getPrevUp(iter.up) diff --git a/avlkeydup/avlkeydup.go b/avlkeydup/avlkeydup.go index 7c0a857..c113f56 100644 --- a/avlkeydup/avlkeydup.go +++ b/avlkeydup/avlkeydup.go @@ -28,10 +28,12 @@ type Tree struct { root *Node size int Compare compare.Compare + + iter *Iterator } func New(Compare compare.Compare) *Tree { - return &Tree{Compare: Compare} + return &Tree{Compare: Compare, iter: NewIteratorWithCap(nil, 16)} } func (tree *Tree) String() string { @@ -52,14 +54,14 @@ func (tree *Tree) Size() int { 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 { tree.size-- if tree.size == 0 { tree.root = nil - return n + return n.value, true } left := getHeight(n.children[0]) @@ -69,7 +71,7 @@ func (tree *Tree) Remove(key interface{}) *Node { p := n.parent p.children[getRelationship(n)] = nil tree.fixRemoveHeight(p) - return n + return n.value, true } var cur *Node @@ -110,10 +112,10 @@ func (tree *Tree) Remove(key interface{}) *Node { tree.fixRemoveHeight(cparent) } - return cur + return cur.value, true } - return nil + return nil, false } // Values 返回先序遍历的值 @@ -152,7 +154,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - iter := NewIterator(min) + tree.iter.SetNode(min) + iter := tree.iter for iter.Next() { result = append(result, iter.Value()) if iter.cur == max { @@ -177,7 +180,8 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) { result = make([]interface{}, 0, 16) - iter := NewIterator(max) + tree.iter.SetNode(max) + iter := tree.iter for iter.Prev() { result = append(result, iter.Value()) if iter.cur == min { @@ -227,7 +231,9 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { n = n.children[1] lastc = c case 0: - iter := NewIterator(n) + + tree.iter.SetNode(n) + iter := tree.iter iter.Prev() for iter.Prev() { 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 { - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } else { result[0] = last - result[2] = GetNext(last, 1) + result[2] = tree.iter.GetNext(last, 1) } case -1: if result[1] != nil { - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } else { result[2] = last - result[0] = GetPrev(last, 1) + result[0] = tree.iter.GetPrev(last, 1) } case 0: @@ -270,8 +276,8 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) { if result[1] == nil { return } - result[0] = GetPrev(result[1], 1) - result[2] = GetNext(result[1], 1) + result[0] = tree.iter.GetPrev(result[1], 1) + result[2] = tree.iter.GetNext(result[1], 1) } return } @@ -285,7 +291,9 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) { case 1: n = n.children[1] case 0: - iter := NewIterator(n) + + tree.iter.SetNode(n) + iter := tree.iter iter.Prev() for iter.Prev() { if tree.Compare(iter.cur.key, n.key) == 0 { diff --git a/avlkeydup/iterator.go b/avlkeydup/iterator.go index ac617a1..ffa1a24 100644 --- a/avlkeydup/iterator.go +++ b/avlkeydup/iterator.go @@ -24,6 +24,18 @@ func NewIterator(n *Node) *Iterator { 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{} { return iter.cur.value } @@ -46,9 +58,10 @@ func (iter *Iterator) Right() bool { 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.up = iter.getNextUp(iter.up) @@ -103,9 +116,10 @@ func (iter *Iterator) Next() (result bool) { 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.up = iter.getPrevUp(iter.up) diff --git a/interface.go b/interface.go index a80b419..6fe4564 100644 --- a/interface.go +++ b/interface.go @@ -1,9 +1 @@ package structure - -type IteratorNext interface { - Next() bool -} - -type IteratorPrev interface { - Prev() bool -} diff --git a/priority_queue/priority_queue.go b/priority_queue/priority_queue.go index 49fa0d1..18bfd14 100644 --- a/priority_queue/priority_queue.go +++ b/priority_queue/priority_queue.go @@ -67,11 +67,11 @@ func (pq *PriorityQueue) GetRange(k1, k2 interface{}) []interface{} { 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) } -func (pq *PriorityQueue) Remove(key interface{}) (*Node, bool) { +func (pq *PriorityQueue) Remove(key interface{}) (interface{}, bool) { return pq.queue.Remove(key) } diff --git a/priority_queue/vbt.go b/priority_queue/vbt.go index df8c88d..fab0650 100644 --- a/priority_queue/vbt.go +++ b/priority_queue/vbt.go @@ -156,11 +156,11 @@ func (tree *vbTree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { return nil, false } -func (tree *vbTree) RemoveIndex(idx int) (*Node, bool) { +func (tree *vbTree) RemoveIndex(idx int) (interface{}, bool) { n := tree.indexNode(idx) if n != nil { tree.removeNode(n) - return n, true + return n.value, true } return nil, false } @@ -229,11 +229,11 @@ func (tree *vbTree) removeNode(n *Node) { return } -func (tree *vbTree) Remove(key interface{}) (*Node, bool) { +func (tree *vbTree) Remove(key interface{}) (interface{}, bool) { if n, ok := tree.GetNode(key); ok { tree.removeNode(n) - return n, true + return n.value, true } // return nil return nil, false diff --git a/priority_queue/vbt_test.go b/priority_queue/vbt_test.go index d00734e..f366461 100644 --- a/priority_queue/vbt_test.go +++ b/priority_queue/vbt_test.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "io/ioutil" "log" - "os" "testing" "474420502.top/eson/structure/compare" @@ -17,38 +16,6 @@ import ( "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 { data, err := ioutil.ReadFile("../l.log") if err != nil { diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..c3874fe --- /dev/null +++ b/test.sh @@ -0,0 +1 @@ +cd avl ; go test -v diff --git a/vbt/vbt.go b/vbt/vbt.go index 4dbef38..4ce84c7 100644 --- a/vbt/vbt.go +++ b/vbt/vbt.go @@ -154,11 +154,11 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { / return nil, false } -func (tree *Tree) RemoveIndex(idx int) (*Node, bool) { +func (tree *Tree) RemoveIndex(idx int) (interface{}, bool) { n := tree.indexNode(idx) if n != nil { tree.removeNode(n) - return n, true + return n.value, true } return nil, false } @@ -221,11 +221,11 @@ func (tree *Tree) removeNode(n *Node) { return } -func (tree *Tree) Remove(key interface{}) (*Node, bool) { +func (tree *Tree) Remove(key interface{}) (interface{}, bool) { if n, ok := tree.GetNode(key); ok { tree.removeNode(n) - return n, true + return n.value, true } // return nil return nil, false diff --git a/vbt/vbt_test.go b/vbt/vbt_test.go index 1c59614..751a8ad 100644 --- a/vbt/vbt_test.go +++ b/vbt/vbt_test.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "io/ioutil" "log" - "os" "testing" "474420502.top/eson/structure/compare" @@ -17,38 +16,6 @@ import ( "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 { data, err := ioutil.ReadFile("../l.log") if err != nil { diff --git a/vbtkey/vbtkey.go b/vbtkey/vbtkey.go index 78501ee..18da20b 100644 --- a/vbtkey/vbtkey.go +++ b/vbtkey/vbtkey.go @@ -155,11 +155,11 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { / return nil, false } -func (tree *Tree) RemoveIndex(idx int) (*Node, bool) { +func (tree *Tree) RemoveIndex(idx int) (interface{}, bool) { n := tree.indexNode(idx) if n != nil { tree.removeNode(n) - return n, true + return n.value, true } return nil, false } @@ -222,11 +222,11 @@ func (tree *Tree) removeNode(n *Node) { return } -func (tree *Tree) Remove(key interface{}) (*Node, bool) { +func (tree *Tree) Remove(key interface{}) (interface{}, bool) { if n, ok := tree.GetNode(key); ok { tree.removeNode(n) - return n, true + return n.value, true } // return nil return nil, false diff --git a/vbtkey/vbtkey_test.go b/vbtkey/vbtkey_test.go index a4379d2..fda305e 100644 --- a/vbtkey/vbtkey_test.go +++ b/vbtkey/vbtkey_test.go @@ -5,7 +5,6 @@ import ( "encoding/gob" "io/ioutil" "log" - "os" "testing" "474420502.top/eson/structure/compare" @@ -17,38 +16,6 @@ import ( "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 { data, err := ioutil.ReadFile("../l.log") if err != nil {