fix IndexRange
This commit is contained in:
parent
d0996f7aaf
commit
ba7263c3f9
28
avl/avl.go
28
avl/avl.go
|
@ -272,34 +272,6 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
}
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
|
||||
// const il = 0
|
||||
// const ir = 1
|
||||
|
||||
// if result[1] == nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
// l := result[1].children[il]
|
||||
// r := result[1].children[ir]
|
||||
|
||||
// if l == nil {
|
||||
// result[0] = nil
|
||||
// } else {
|
||||
// for l.children[ir] != nil {
|
||||
// l = l.children[ir]
|
||||
// }
|
||||
// result[0] = l
|
||||
// }
|
||||
|
||||
// if r == nil {
|
||||
// result[2] = nil
|
||||
// } else {
|
||||
// for r.children[il] != nil {
|
||||
// r = r.children[il]
|
||||
// }
|
||||
// result[2] = r
|
||||
// }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
158
vbt/iterator.go
158
vbt/iterator.go
|
@ -46,63 +46,6 @@ func (iter *Iterator) Right() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
iter.curPushPrevStack(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
|
||||
for i := 0; i < idx; i++ {
|
||||
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return nil
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
if i == idx-1 {
|
||||
return iter.cur
|
||||
}
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *Iterator) Prev() (result bool) {
|
||||
|
||||
if iter.dir > -1 {
|
||||
if iter.dir == 1 && iter.cur != nil {
|
||||
iter.tstack.Clear()
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
iter.up = iter.getPrevUp(iter.cur)
|
||||
}
|
||||
iter.dir = -1
|
||||
}
|
||||
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return false
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
func GetNext(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
|
@ -135,16 +78,15 @@ func GetNext(cur *Node, idx int) *Node {
|
|||
|
||||
func (iter *Iterator) Next() (result bool) {
|
||||
|
||||
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||
if iter.dir > -1 {
|
||||
if iter.dir == 1 && iter.cur != nil {
|
||||
iter.tstack.Clear()
|
||||
iter.curPushNextStack(iter.cur) // 把当前cur计算的逆向回朔
|
||||
iter.up = iter.getNextUp(iter.cur) // cur 寻找下个要计算up
|
||||
iter.curPushNextStack(iter.cur)
|
||||
iter.up = iter.getNextUp(iter.cur)
|
||||
}
|
||||
iter.dir = 1
|
||||
iter.dir = -1
|
||||
}
|
||||
|
||||
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return false
|
||||
|
@ -159,6 +101,64 @@ func (iter *Iterator) Next() (result bool) {
|
|||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
func GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
iter.curPushPrevStack(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
|
||||
for i := 0; i < idx; i++ {
|
||||
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return nil
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
if i == idx-1 {
|
||||
return iter.cur
|
||||
}
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *Iterator) Prev() (result bool) {
|
||||
|
||||
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||
iter.tstack.Clear()
|
||||
iter.curPushPrevStack(iter.cur) // 把当前cur计算的逆向回朔
|
||||
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
|
||||
}
|
||||
iter.dir = 1
|
||||
}
|
||||
|
||||
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return false
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
// 如果再次计算的栈为空, 则只能返回false
|
||||
return false
|
||||
}
|
||||
|
@ -170,7 +170,7 @@ func getRelationship(cur *Node) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
func (iter *Iterator) getNextUp(cur *Node) *Node {
|
||||
func (iter *Iterator) getPrevUp(cur *Node) *Node {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
|
||||
return cur.parent
|
||||
|
@ -180,19 +180,19 @@ func (iter *Iterator) getNextUp(cur *Node) *Node {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) curPushNextStack(cur *Node) {
|
||||
next := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
||||
func (iter *Iterator) curPushPrevStack(cur *Node) {
|
||||
Prev := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
||||
|
||||
if next != nil {
|
||||
iter.tstack.Push(next)
|
||||
for next.children[1] != nil {
|
||||
next = next.children[1]
|
||||
iter.tstack.Push(next) // 入栈 用于回溯
|
||||
if Prev != nil {
|
||||
iter.tstack.Push(Prev)
|
||||
for Prev.children[1] != nil {
|
||||
Prev = Prev.children[1]
|
||||
iter.tstack.Push(Prev) // 入栈 用于回溯
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) getPrevUp(cur *Node) *Node {
|
||||
func (iter *Iterator) getNextUp(cur *Node) *Node {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
|
||||
return cur.parent
|
||||
|
@ -202,14 +202,14 @@ func (iter *Iterator) getPrevUp(cur *Node) *Node {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) curPushPrevStack(cur *Node) {
|
||||
prev := cur.children[1]
|
||||
func (iter *Iterator) curPushNextStack(cur *Node) {
|
||||
next := cur.children[1]
|
||||
|
||||
if prev != nil {
|
||||
iter.tstack.Push(prev)
|
||||
for prev.children[0] != nil {
|
||||
prev = prev.children[0]
|
||||
iter.tstack.Push(prev)
|
||||
if next != nil {
|
||||
iter.tstack.Push(next)
|
||||
for next.children[0] != nil {
|
||||
next = next.children[0]
|
||||
iter.tstack.Push(next)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
161
vbt/vbt.go
161
vbt/vbt.go
|
@ -113,7 +113,7 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
|||
iter := NewIterator(n)
|
||||
result = make([]interface{}, 0, idx1-idx2)
|
||||
for i := idx2; i <= idx1; i++ {
|
||||
if iter.Next() {
|
||||
if iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
|
@ -134,7 +134,7 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
|||
iter := NewIterator(n)
|
||||
result = make([]interface{}, 0, idx2-idx1)
|
||||
for i := idx1; i <= idx2; i++ {
|
||||
if iter.Prev() {
|
||||
if iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
|
@ -264,7 +264,7 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
|||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(min)
|
||||
for iter.Prev() {
|
||||
for iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == max {
|
||||
break
|
||||
|
@ -289,7 +289,7 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
|||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(max)
|
||||
for iter.Next() {
|
||||
for iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == min {
|
||||
break
|
||||
|
@ -338,6 +338,15 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
n = n.children[1]
|
||||
lastc = c
|
||||
case 0:
|
||||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
|
@ -347,147 +356,55 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
switch lastc {
|
||||
case 1:
|
||||
const il = 0
|
||||
const ir = 1
|
||||
|
||||
if result[1] == nil {
|
||||
result[0] = last
|
||||
|
||||
parent := last
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[2] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
if result[1] != nil {
|
||||
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
} else {
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[0] = result[1].parent
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[0] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
|
||||
parent := result[1].parent
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[2] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[2] = r
|
||||
}
|
||||
result[0] = last
|
||||
result[2] = GetNext(last, 1)
|
||||
}
|
||||
|
||||
case -1:
|
||||
|
||||
const il = 1
|
||||
const ir = 0
|
||||
|
||||
if result[1] == nil {
|
||||
result[2] = last
|
||||
|
||||
parent := last
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[0] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if result[1] != nil {
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
} else {
|
||||
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[2] = result[1].parent
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[2] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
|
||||
parent := result[1].parent
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 compare 后左右的关系
|
||||
result[0] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[0] = r
|
||||
|
||||
}
|
||||
result[2] = last
|
||||
result[0] = GetPrev(last, 1)
|
||||
}
|
||||
case 0:
|
||||
|
||||
const il = 0
|
||||
const ir = 1
|
||||
case 0:
|
||||
|
||||
if result[1] == nil {
|
||||
return
|
||||
}
|
||||
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[0] = nil
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[0] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
result[2] = nil
|
||||
} else {
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[2] = r
|
||||
}
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
||||
func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.compare(key, n.value); c {
|
||||
switch c := tree.compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
n = n.children[1]
|
||||
case 0:
|
||||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
|
@ -496,9 +413,9 @@ func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
func (tree *Tree) Put(key, value interface{}) {
|
||||
func (tree *Tree) Put(key interface{}) {
|
||||
|
||||
node := &Node{value: value, size: 1}
|
||||
node := &Node{value: key, size: 1}
|
||||
if tree.root == nil {
|
||||
tree.root = node
|
||||
return
|
||||
|
|
125
vbt/vbt_test.go
125
vbt/vbt_test.go
|
@ -64,7 +64,7 @@ func TestIndexRange(t *testing.T) {
|
|||
tree := New(compare.Int)
|
||||
l := []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15}
|
||||
for _, v := range l {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
// [3 7 14 14 14 15 16 17 20 21 30 40 40 40 40 50]
|
||||
// t.Error(tree.Values(), tree.Size())
|
||||
|
@ -124,7 +124,7 @@ func TestIndexRange(t *testing.T) {
|
|||
func TestGetAround(t *testing.T) {
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{7, 14, 14, 14, 16, 17, 20, 30, 21, 40, 50, 3, 40, 40, 40, 15} {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
var Result string
|
||||
|
@ -202,7 +202,7 @@ func TestPutComparatorRandom(t *testing.T) {
|
|||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
content += spew.Sprint(v) + ","
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
godsavl.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
@ -222,7 +222,7 @@ func TestPutComparatorRandom(t *testing.T) {
|
|||
func TestGet(t *testing.T) {
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
for _, v := range []int{2383, 7666, 3055, 39016, 57092, 27897, 36513, 1562, 22574, 23202} {
|
||||
|
@ -240,7 +240,7 @@ func TestGet(t *testing.T) {
|
|||
func TestGetRange(t *testing.T) {
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
// t.Error(tree.debugString())
|
||||
|
@ -297,7 +297,7 @@ func TestGetRange(t *testing.T) {
|
|||
func TestTravalsal(t *testing.T) {
|
||||
tree := New(compare.Int)
|
||||
for _, v := range []int{5, 6, 8, 10, 13, 17, 1, 2, 40, 30} {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
i := 0
|
||||
|
@ -330,7 +330,7 @@ ALL:
|
|||
if _, ok := m[v]; !ok {
|
||||
m[v] = v
|
||||
l = append(l, v)
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
@ -368,7 +368,7 @@ ALL:
|
|||
if _, ok := m[v]; !ok {
|
||||
l = append(l, v)
|
||||
m[v] = v
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
gods.Put(v, v)
|
||||
}
|
||||
}
|
||||
|
@ -428,7 +428,7 @@ func BenchmarkIndexRange(b *testing.B) {
|
|||
b.N = len(l)
|
||||
|
||||
for _, v := range l {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -466,7 +466,7 @@ func BenchmarkIterator(b *testing.B) {
|
|||
b.N = len(l)
|
||||
|
||||
for _, v := range l {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -492,7 +492,7 @@ func BenchmarkRemove(b *testing.B) {
|
|||
|
||||
b.N = len(l)
|
||||
for _, v := range l {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -546,7 +546,7 @@ func BenchmarkGet(b *testing.B) {
|
|||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
for i := 0; i < b.N; i++ {
|
||||
tree.Put(l[i], i)
|
||||
tree.Put(l[i])
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
@ -617,66 +617,13 @@ func BenchmarkPut(b *testing.B) {
|
|||
for i := 0; i < execCount; i++ {
|
||||
tree := New(compare.Int)
|
||||
for _, v := range l {
|
||||
tree.Put(v, v)
|
||||
tree.Put(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutStable(t *testing.T) {
|
||||
|
||||
// l := []int{14, 18, 20, 21, 22, 23, 19}
|
||||
// for n := 0; n < 1000000; n++ {
|
||||
// var l []int
|
||||
// l = append(l, 60, 5, 15)
|
||||
// for i := 0; len(l) < 11; i++ {
|
||||
// l = append(l, randomdata.Number(3, 69))
|
||||
// }
|
||||
|
||||
// tree := New(compare.Int)
|
||||
// for _, v := range l {
|
||||
// tree.Put(v, v)
|
||||
// }
|
||||
// result := tree.getArountNode(60)
|
||||
|
||||
// if result[2] == nil && tree.indexNode(-1) != result[1] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// result = tree.getArountNode(5)
|
||||
// if result[0] == nil && tree.indexNode(0) != result[1] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// result = tree.getArountNode(2)
|
||||
// if result[2] == nil && tree.indexNode(0) != result[2] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// result = tree.getArountNode(70)
|
||||
// if result[0] == nil && tree.indexNode(-1) != result[0] {
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(result)
|
||||
// }
|
||||
|
||||
// }
|
||||
// for _, v := range []int{10, 0, 9, 5, -11, -10, -1, -5} {
|
||||
// t.Error(tree.Index(v))
|
||||
// // t.Error(tree.debugString())
|
||||
// }
|
||||
|
||||
// tree.RemoveIndex(4)
|
||||
// t.Error(tree.Index(4))
|
||||
// t.Error(tree.Values())
|
||||
// t.Error(tree.debugString())
|
||||
// t.Error(len(l), tree.debugString(), "\n", "-----------") // 3 6(4)
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkIndex(b *testing.B) {
|
||||
|
@ -685,56 +632,14 @@ func BenchmarkIndex(b *testing.B) {
|
|||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
for i := 0; i < b.N; i++ {
|
||||
tree.Put(l[i], i)
|
||||
tree.Put(l[i])
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
b.StartTimer()
|
||||
|
||||
b.N = 1000000
|
||||
|
||||
var result [50]interface{}
|
||||
for n := 0; n < b.N; n++ {
|
||||
i := 0
|
||||
tree.Traversal(func(v interface{}) bool {
|
||||
result[i] = v
|
||||
i++
|
||||
if i < 50 {
|
||||
return true
|
||||
}
|
||||
log.Print(i)
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkTraversal(b *testing.B) {
|
||||
tree := New(compare.Int)
|
||||
|
||||
l := loadTestData()
|
||||
b.N = len(l)
|
||||
for i := 0; i < b.N; i++ {
|
||||
tree.Put(l[i], i)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
b.StartTimer()
|
||||
|
||||
execCount := 10
|
||||
b.N = len(l) * execCount
|
||||
|
||||
for n := 0; n < execCount; n++ {
|
||||
i := 0
|
||||
var result []interface{}
|
||||
tree.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
i++
|
||||
if i >= 50 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
tree.Index(i)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -46,63 +46,6 @@ func (iter *Iterator) Right() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
iter.curPushPrevStack(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
|
||||
for i := 0; i < idx; i++ {
|
||||
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return nil
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
if i == idx-1 {
|
||||
return iter.cur
|
||||
}
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *Iterator) Prev() (result bool) {
|
||||
|
||||
if iter.dir > -1 {
|
||||
if iter.dir == 1 && iter.cur != nil {
|
||||
iter.tstack.Clear()
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
iter.up = iter.getPrevUp(iter.cur)
|
||||
}
|
||||
iter.dir = -1
|
||||
}
|
||||
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return false
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
func GetNext(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
|
@ -135,16 +78,15 @@ func GetNext(cur *Node, idx int) *Node {
|
|||
|
||||
func (iter *Iterator) Next() (result bool) {
|
||||
|
||||
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||
if iter.dir > -1 {
|
||||
if iter.dir == 1 && iter.cur != nil {
|
||||
iter.tstack.Clear()
|
||||
iter.curPushNextStack(iter.cur) // 把当前cur计算的逆向回朔
|
||||
iter.up = iter.getNextUp(iter.cur) // cur 寻找下个要计算up
|
||||
iter.curPushNextStack(iter.cur)
|
||||
iter.up = iter.getNextUp(iter.cur)
|
||||
}
|
||||
iter.dir = 1
|
||||
iter.dir = -1
|
||||
}
|
||||
|
||||
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return false
|
||||
|
@ -159,6 +101,64 @@ func (iter *Iterator) Next() (result bool) {
|
|||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
func GetPrev(cur *Node, idx int) *Node {
|
||||
|
||||
iter := NewIterator(cur)
|
||||
iter.curPushPrevStack(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
|
||||
for i := 0; i < idx; i++ {
|
||||
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return nil
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
if i == idx-1 {
|
||||
return iter.cur
|
||||
}
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return cur
|
||||
}
|
||||
|
||||
func (iter *Iterator) Prev() (result bool) {
|
||||
|
||||
if iter.dir < 1 { // 非 1(next 方向定义 -1 为 prev)
|
||||
if iter.dir == -1 && iter.cur != nil { // 如果上次为prev方向, 则清空辅助计算的栈
|
||||
iter.tstack.Clear()
|
||||
iter.curPushPrevStack(iter.cur) // 把当前cur计算的逆向回朔
|
||||
iter.up = iter.getPrevUp(iter.cur) // cur 寻找下个要计算up
|
||||
}
|
||||
iter.dir = 1
|
||||
}
|
||||
|
||||
// 如果栈空了, 把up的递归计算入栈, 重新计算 下次的up值
|
||||
if iter.tstack.Size() == 0 {
|
||||
if iter.up == nil {
|
||||
return false
|
||||
}
|
||||
iter.tstack.Push(iter.up)
|
||||
iter.up = iter.getPrevUp(iter.up)
|
||||
}
|
||||
|
||||
if v, ok := iter.tstack.Pop(); ok {
|
||||
iter.cur = v.(*Node)
|
||||
iter.curPushPrevStack(iter.cur)
|
||||
return true
|
||||
}
|
||||
|
||||
// 如果再次计算的栈为空, 则只能返回false
|
||||
return false
|
||||
}
|
||||
|
@ -170,7 +170,7 @@ func getRelationship(cur *Node) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
func (iter *Iterator) getNextUp(cur *Node) *Node {
|
||||
func (iter *Iterator) getPrevUp(cur *Node) *Node {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 1 { // next 在 降序 小值. 如果child在右边, parent 比 child 小, parent才有效, 符合降序
|
||||
return cur.parent
|
||||
|
@ -180,19 +180,19 @@ func (iter *Iterator) getNextUp(cur *Node) *Node {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) curPushNextStack(cur *Node) {
|
||||
next := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
||||
func (iter *Iterator) curPushPrevStack(cur *Node) {
|
||||
Prev := cur.children[0] // 当前的左然后向右找, 找到最大, 就是最接近cur 并且小于cur的值
|
||||
|
||||
if next != nil {
|
||||
iter.tstack.Push(next)
|
||||
for next.children[1] != nil {
|
||||
next = next.children[1]
|
||||
iter.tstack.Push(next) // 入栈 用于回溯
|
||||
if Prev != nil {
|
||||
iter.tstack.Push(Prev)
|
||||
for Prev.children[1] != nil {
|
||||
Prev = Prev.children[1]
|
||||
iter.tstack.Push(Prev) // 入栈 用于回溯
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iter *Iterator) getPrevUp(cur *Node) *Node {
|
||||
func (iter *Iterator) getNextUp(cur *Node) *Node {
|
||||
for cur.parent != nil {
|
||||
if getRelationship(cur) == 0 { // Prev 在 降序 大值. 如果child在左边, parent 比 child 大, parent才有效 , 符合降序
|
||||
return cur.parent
|
||||
|
@ -202,14 +202,14 @@ func (iter *Iterator) getPrevUp(cur *Node) *Node {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (iter *Iterator) curPushPrevStack(cur *Node) {
|
||||
prev := cur.children[1]
|
||||
func (iter *Iterator) curPushNextStack(cur *Node) {
|
||||
next := cur.children[1]
|
||||
|
||||
if prev != nil {
|
||||
iter.tstack.Push(prev)
|
||||
for prev.children[0] != nil {
|
||||
prev = prev.children[0]
|
||||
iter.tstack.Push(prev)
|
||||
if next != nil {
|
||||
iter.tstack.Push(next)
|
||||
for next.children[0] != nil {
|
||||
next = next.children[0]
|
||||
iter.tstack.Push(next)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
175
vbtkey/vbtkey.go
175
vbtkey/vbtkey.go
|
@ -26,12 +26,12 @@ func (n *Node) String() string {
|
|||
}
|
||||
|
||||
type Tree struct {
|
||||
root *Node
|
||||
comparator compare.Compare
|
||||
root *Node
|
||||
compare compare.Compare
|
||||
}
|
||||
|
||||
func New(comparator compare.Compare) *Tree {
|
||||
return &Tree{comparator: comparator}
|
||||
func New(compare compare.Compare) *Tree {
|
||||
return &Tree{compare: compare}
|
||||
}
|
||||
|
||||
func (tree *Tree) String() string {
|
||||
|
@ -114,7 +114,7 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
|||
iter := NewIterator(n)
|
||||
result = make([]interface{}, 0, idx1-idx2)
|
||||
for i := idx2; i <= idx1; i++ {
|
||||
if iter.Next() {
|
||||
if iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
|
@ -135,7 +135,7 @@ func (tree *Tree) IndexRange(idx1, idx2 int) (result []interface{}, ok bool) { /
|
|||
iter := NewIterator(n)
|
||||
result = make([]interface{}, 0, idx2-idx1)
|
||||
for i := idx1; i <= idx2; i++ {
|
||||
if iter.Prev() {
|
||||
if iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
} else {
|
||||
ok = false
|
||||
|
@ -243,7 +243,7 @@ func (tree *Tree) Values() []interface{} {
|
|||
}
|
||||
|
||||
func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
||||
c := tree.comparator(k2, k1)
|
||||
c := tree.compare(k2, k1)
|
||||
switch c {
|
||||
case 1:
|
||||
|
||||
|
@ -265,7 +265,7 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
|||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(min)
|
||||
for iter.Prev() {
|
||||
for iter.Next() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == max {
|
||||
break
|
||||
|
@ -290,7 +290,7 @@ func (tree *Tree) GetRange(k1, k2 interface{}) (result []interface{}) {
|
|||
result = make([]interface{}, 0, 16)
|
||||
|
||||
iter := NewIterator(max)
|
||||
for iter.Next() {
|
||||
for iter.Prev() {
|
||||
result = append(result, iter.Value())
|
||||
if iter.cur == min {
|
||||
break
|
||||
|
@ -330,7 +330,7 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
|
||||
for n := tree.root; n != nil; {
|
||||
last = n
|
||||
c := tree.comparator(key, n.key)
|
||||
c := tree.compare(key, n.value)
|
||||
switch c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
|
@ -339,159 +339,76 @@ func (tree *Tree) getArountNode(key interface{}) (result [3]*Node) {
|
|||
n = n.children[1]
|
||||
lastc = c
|
||||
case 0:
|
||||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
result[1] = n
|
||||
n = nil
|
||||
default:
|
||||
panic("Get comparator only is allowed in -1, 0, 1")
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
|
||||
switch lastc {
|
||||
case 1:
|
||||
const il = 0
|
||||
const ir = 1
|
||||
|
||||
if result[1] == nil {
|
||||
result[0] = last
|
||||
|
||||
parent := last
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 comparator 后左右的关系
|
||||
result[2] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
if result[1] != nil {
|
||||
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
} else {
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[0] = result[1].parent
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[0] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
|
||||
parent := result[1].parent
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 comparator 后左右的关系
|
||||
result[2] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[2] = r
|
||||
}
|
||||
result[0] = last
|
||||
result[2] = GetNext(last, 1)
|
||||
}
|
||||
|
||||
case -1:
|
||||
|
||||
const il = 1
|
||||
const ir = 0
|
||||
|
||||
if result[1] == nil {
|
||||
result[2] = last
|
||||
|
||||
parent := last
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 comparator 后左右的关系
|
||||
result[0] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if result[1] != nil {
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
} else {
|
||||
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[2] = result[1].parent
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[2] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
|
||||
parent := result[1].parent
|
||||
for ; parent != nil && parent.parent != nil; parent = parent.parent {
|
||||
child := getRelationship(parent)
|
||||
if child == (-lastc+2)/2 { // child 与 comparator 后左右的关系
|
||||
result[0] = parent.parent
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[0] = r
|
||||
|
||||
}
|
||||
result[2] = last
|
||||
result[0] = GetPrev(last, 1)
|
||||
}
|
||||
case 0:
|
||||
|
||||
const il = 0
|
||||
const ir = 1
|
||||
case 0:
|
||||
|
||||
if result[1] == nil {
|
||||
return
|
||||
}
|
||||
|
||||
l := result[1].children[il]
|
||||
r := result[1].children[ir]
|
||||
|
||||
if l == nil {
|
||||
result[0] = nil
|
||||
} else {
|
||||
for l.children[ir] != nil {
|
||||
l = l.children[ir]
|
||||
}
|
||||
result[0] = l
|
||||
}
|
||||
|
||||
if r == nil {
|
||||
result[2] = nil
|
||||
} else {
|
||||
for r.children[il] != nil {
|
||||
r = r.children[il]
|
||||
}
|
||||
result[2] = r
|
||||
}
|
||||
result[0] = GetPrev(result[1], 1)
|
||||
result[2] = GetNext(result[1], 1)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (tree *Tree) GetNode(key interface{}) (*Node, bool) {
|
||||
func (tree *Tree) GetNode(value interface{}) (*Node, bool) {
|
||||
|
||||
for n := tree.root; n != nil; {
|
||||
switch c := tree.comparator(key, n.key); c {
|
||||
switch c := tree.compare(value, n.value); c {
|
||||
case -1:
|
||||
n = n.children[0]
|
||||
case 1:
|
||||
n = n.children[1]
|
||||
case 0:
|
||||
iter := NewIterator(n)
|
||||
iter.Prev()
|
||||
for iter.Prev() {
|
||||
if tree.compare(iter.cur.value, n.value) == 0 {
|
||||
n = iter.cur
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
return n, true
|
||||
default:
|
||||
panic("Get comparator only is allowed in -1, 0, 1")
|
||||
panic("Get compare only is allowed in -1, 0, 1")
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
@ -534,7 +451,7 @@ func (tree *Tree) Put(key, value interface{}) {
|
|||
|
||||
cur.size++
|
||||
parent = cur
|
||||
c := tree.comparator(key, cur.key)
|
||||
c := tree.compare(key, cur.key)
|
||||
child = (c + 2) / 2
|
||||
cur = cur.children[child]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user