mirror of
https://github.com/c-cube/sidekick.git
synced 2025-12-09 20:55:39 -05:00
fix(heap): handle case with one element properly
This commit is contained in:
parent
87a2936f75
commit
ca9d5447e0
1 changed files with 2 additions and 1 deletions
|
|
@ -126,10 +126,11 @@ 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;
|
if Vec.size heap=0 then raise Not_found;
|
||||||
let x = Vec.get heap 0 in
|
let x = Vec.get heap 0 in
|
||||||
Elt.set_idx x _absent_index;
|
|
||||||
let new_hd = Vec.pop heap in (* new head *)
|
let new_hd = Vec.pop heap in (* new head *)
|
||||||
Vec.set heap 0 new_hd;
|
Vec.set heap 0 new_hd;
|
||||||
Elt.set_idx new_hd 0;
|
Elt.set_idx new_hd 0;
|
||||||
|
(* remove [x]. do it after [new_hd.idx<-0] in case [x==new_hd] *)
|
||||||
|
Elt.set_idx x _absent_index;
|
||||||
(* enforce heap property again *)
|
(* enforce heap property again *)
|
||||||
if Vec.size heap > 1 then (
|
if Vec.size heap > 1 then (
|
||||||
percolate_down s new_hd;
|
percolate_down s new_hd;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue