Skip to content

Commit e74c09b

Browse files
committed
correct zero-priority outcomes
1 parent 72e3791 commit e74c09b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/common/sum_tree.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,28 @@ function Base.empty!(t::SumTree)
131131
t
132132
end
133133

134+
function correct_priority(t::SumTree, leaf_ind)
135+
p = t.tree[leaf_ind]
136+
# walk backwards until p != 0 or until leftmost leaf reached
137+
tmp_ind = leaf_ind
138+
while p == 0f0 && (tmp_ind-1)*2 > length(t.tree)
139+
tmp_ind -= 1
140+
p = t.tree[tmp_ind]
141+
end
142+
# walk forwards until p != 0 or until rightmost leaf reached
143+
p == 0f0 && (tmp_ind = leaf_ind)
144+
while p == 0f0 && (tmp_ind - t.nparents) <= t.length
145+
tmp_ind += 1
146+
p = t.tree[tmp_ind]
147+
end
148+
return p, tmp_ind
149+
end
150+
151+
134152
function Base.get(t::SumTree, v)
135153
parent_ind = 1
136154
leaf_ind = parent_ind
137155
while true
138-
v = min(v, t.tree[parent_ind])
139156
left_child_ind = parent_ind * 2
140157
right_child_ind = left_child_ind + 1
141158
if left_child_ind > length(t.tree)
@@ -153,7 +170,7 @@ function Base.get(t::SumTree, v)
153170
if leaf_ind <= t.nparents
154171
leaf_ind += t.capacity
155172
end
156-
p = t.tree[leaf_ind]
173+
p, leaf_ind = correct_priority(t, leaf_ind)
157174
ind = leaf_ind - t.nparents
158175
real_ind = ind >= t.first ? ind - t.first + 1 : ind + t.capacity - t.first + 1
159176
real_ind, p

0 commit comments

Comments
 (0)