基于锦标赛树的LAB-PQ实现
基于树存储的LAB-PQ,展示原理
def _mark(id, newflag):
t = T.leaf(id)
t.inQ = newflag
while t != T.root and
TestAndSet(t.parent.renew, newflag) :
t = t.parent
def _sync(t):
if t.is_leaf() :
return Q[t.id] if t.inQ else +inf
if t.renew == 0 :
return t.k
t.renew = 0
leftKey, rightKey = _sync(t.left),
_sync(t.right) #parallel
t.k = min(leftKey, rightKey)
return t.k
def _extract_from(theta, t):
if t.is_leaf() :
if Q[t.id] <= theta :
mark(t.id, 0)
return [t.id]
return []
if t.k > theta :
return []
leftSeq, rightSeq = _extractfrom(theta, t.left),
_extract_from(theta, t.right) #parallel
return leftSeq + rightSeq
def update(id):
_mark(id, 1)
def extract(theta):
_sync(T.root)
return _extract_from(theta, T.root)
具体实现时采用树状数组。