标签:csdn 目标 jsb 存储 log var 下列表 sig 算法
http://blog.csdn.net/myhaspl
private func findnode(val:Int)->Bool{//http://blog.csdn.net/myhaspl
//查找结点http://blog.csdn.net/myhaspl
if let mysltop = slinktop{
var mynode:skipLinkNode=mysltop
while true{
while true{
if let nextnd = mynode.nextnode {
let nodeval = nextnd.ndval
if nodeval < val{
mynode=nextnd
continue
}
if nodeval == val{
return true
}
}
break
}
if mynode.downnode == nil{
return false
}
else{
mynode = mynode.downnode!
}
}
}
else{
return false
}
}
....
....
....
private func deletenode(val:Int){
if let mysltop=slinktop{
var mynode:skipLinkNode=mysltop
while true{
while true{
if let nextnd = mynode.nextnode {
let nodeval = nextnd.ndval
if nodeval < val{
mynode=nextnd
continue
}
if nodeval == val{
//delete node from the level
mynode.nextnode=nextnd.nextnode
}
}
break
}
if mynode.downnode == nil{
//最底层http://blog.csdn.net/myhaspl
break
}
else{
mynode = mynode.downnode!
}
}
}
}
private func insertnode(val:Int){
//插入结点
let insertlv=getinsertlv()
let currtop=currlist(insertlv)
var mynode:skipLinkNode = currtop
var isfind:Bool=false
var searchnodes=[(skipLinkNode,skipLinkNode)]()
while true{
while let ntnode=mynode.nextnode{
if ntnode.ndval < val {
mynode = ntnode
}
else if ntnode.ndval == val {
isfind=true
searchnodes.append((ntnode,ntnode.nextnode!))
break
}
else{
searchnodes.append((mynode,ntnode))
break
}
}
if let dnnode=mynode.downnode {
mynode=dnnode
}
else{
break
}
}
var newnd:skipLinkNode?
var upnd:skipLinkNode?
var dnnd:skipLinkNode?
var prend:skipLinkNode
var ntnd:skipLinkNode
if !isfind {
for nodes in searchnodes{
(prend,ntnd)=nodes
upnd=newnd
newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val)
if upnd != nil{
upnd!.downnode=newnd
}
else{
dnnd = newnd!
}
}
if insertlv>slinklevel
{
addnewlevel(val,dnnode: dnnd!)
}
}
else{
let nodelist=searchnodes.last
(prend,ntnd)=nodelist!
newnd=createnode(prend,nextnd:ntnd,nodetype: ntype.node,val:val)
}
}
private func slinkstatus()->String{
var mystatus:String=""
var nownode:skipLinkNode
var i=slinklevel
while i>=0{
nownode=slist[i]
mystatus+="||top=>"
while true{
if let ntnode=nownode.nextnode {
if ntnode.ndtype != ntype.end {
mystatus+=String(ntnode.ndval)+"--"
}
else{
mystatus+="==>end||"
}
nownode=ntnode
}
else{
break
}
}
mystatus += "\n"
i-=1
}
return mystatus
}
底层是一个普通的有序链表。每一个更高层都充当以下列表的"高速跑道"。这里在层 i 中的元素按某个固定的概率 p 出如今层 i+1 中。平均起来,每一个元素都在 1/(1-p) 个列表中出现,而最高层的元素(一般是在跳跃列表前端的一个特殊的头元素)在 O(log1/pn) 个列表中出现。
通过跟踪起自目标直到到达在更高列表中出现的元素的反向查找路径,在每一个链表中预期的步数显而易见是 1/p。所以查找的整体代价是 O(log1/p n / p)。当p 是常数时是 O(log n)。通过选择不同 p 值。就能够在查找代价和存储代价之间作出权衡。
这里元素不多,体现不出优势,假设元素足够多,这样的索引结构就能体现出优势来了。
标签:csdn 目标 jsb 存储 log var 下列表 sig 算法
原文地址:http://www.cnblogs.com/yxysuanfa/p/7258982.html