fix IndexRange
This commit is contained in:
@@ -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]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user