mirror of
https://github.com/c-cube/sidekick.git
synced 2026-01-29 04:44:52 -05:00
fix heap
This commit is contained in:
parent
0266a39b04
commit
99fed971d6
1 changed files with 17 additions and 12 deletions
|
|
@ -124,17 +124,22 @@ module Make(Elt : RANKED) = struct
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let remove_min ({heap} as s) =
|
let remove_min ({heap} as s) =
|
||||||
if Vec.size heap=0 then raise Not_found;
|
match Vec.size heap with
|
||||||
let x = Vec.get heap 0 in
|
| 0 -> raise Not_found
|
||||||
let new_hd = Vec.pop heap in (* new head *)
|
| 1 ->
|
||||||
Vec.set heap 0 new_hd;
|
let x = Vec.pop heap in
|
||||||
Elt.set_idx new_hd 0;
|
Elt.set_idx x _absent_index;
|
||||||
(* remove [x]. do it after [new_hd.idx<-0] in case [x==new_hd] *)
|
x
|
||||||
Elt.set_idx x _absent_index;
|
| _ ->
|
||||||
(* enforce heap property again *)
|
let x = Vec.get heap 0 in
|
||||||
if Vec.size heap > 1 then (
|
let new_hd = Vec.pop heap in (* heap.last() *)
|
||||||
percolate_down s new_hd;
|
Vec.set heap 0 new_hd;
|
||||||
);
|
Elt.set_idx x _absent_index;
|
||||||
x
|
Elt.set_idx new_hd 0;
|
||||||
|
(* enforce heap property again *)
|
||||||
|
if Vec.size heap > 1 then (
|
||||||
|
percolate_down s new_hd;
|
||||||
|
);
|
||||||
|
x
|
||||||
|
|
||||||
end [@@inline]
|
end [@@inline]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue