Skip to content

Commit

Permalink
Fix removing elements from Imperative priority queue
Browse files Browse the repository at this point in the history
The queue is implemented using heap tree saved as an array.
This is pretty standard.
To do a removal you replace the element to remove with the last element and
adjust the heap order.
This can result in having to move the moved element up or down base its the value.
Code only moved down, so also move it up if needed.

Signed-off-by: Frediano Ziglio <[email protected]>
  • Loading branch information
freddy77 committed Dec 3, 2024
1 parent cd98e9d commit 414deef
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ocaml/libs/xapi-stdext/lib/xapi-stdext-threads/ipq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,17 @@ let remove h s =
if s < 0 || s >= h.size then
invalid_arg (Printf.sprintf "%s: index %d out of bounds" __FUNCTION__ s) ;
let n = h.size - 1 in
h.size <- n ;
let d = h.data in
let x = d.(n) in
(* moving [x] up in the heap *)
let rec moveup i =
let fi = (i - 1) / 2 in
if i > 0 && Mtime.is_later d.(fi).time ~than:x.time then (
d.(i) <- d.(fi) ;
moveup fi
) else
d.(i) <- x
in
(* moving [x] down in the heap *)
let rec movedown i =
let j = (2 * i) + 1 in
Expand All @@ -86,7 +94,13 @@ let remove h s =
else
d.(i) <- x
in
movedown s
if s = n then
()
else if Mtime.is_later d.(s).time ~than:x.time then
moveup s
else
movedown s ;
h.size <- n

let find h ev =
let rec iter n =
Expand Down

0 comments on commit 414deef

Please sign in to comment.