cedar-go
cedar-go copied to clipboard
插入操作进入死循环卡主
执行下面段代码,会卡在第4行(删除第4行前的一行或多行均不会出现卡主的情况):
myCedar.Insert([]byte("李沁"), 1)
myCedar.Delete([]byte("李沁"))
myCedar.Insert([]byte("测试"), 1)
myCedar.Insert([]byte("的淡"), 1) // 在该行卡主
myCedar.Insert([]byte("的"), 1)
经过断点调试可发现卡主的原因是程序在cedar.go源文件的236~237行处于死循环状态:
if hasChild && keepOrder {
c = &da.Ninfos[base^int(*c)].Sibling
for da.Ordered && *c != 0 && *c < label { // 第236行
c = &da.Ninfos[base^int(*c)].Sibling // 第237行
}
}
经过尝试,算法实现仍未看懂,不知该如何解决卡主的问题,若大神 @adamzy 有空,望不吝赐教!
我也碰到这个问题了。 我发现,执行 第二行,delete "李沁"的时候 da.Ninfos[ 0 ].Sibling 不为空,会存在 &da.Ninfos[base^int(*c)].Sibling == c 的场景,出现死循环。 我在执行删除的时候增加了几行代码,如果root节点没有子节点,da.Ninfos[ 0 ].Sibling = 0. 目前没有死循环了。
hasChild := da.Array[da.Array[0].base()^int(da.Ninfos[0].Child)].Check == 0
if !hasChild {
da.Ninfos[0].Child = 0
}