From ca9d5447e09f6fa032e55bf28989b02d60e2bfa8 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 29 Nov 2019 14:02:36 -0600 Subject: [PATCH] fix(heap): handle case with one element properly --- src/core/Heap.ml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/Heap.ml b/src/core/Heap.ml index 08aab303..12fa608c 100644 --- a/src/core/Heap.ml +++ b/src/core/Heap.ml @@ -126,10 +126,11 @@ module Make(Elt : RANKED) = struct let remove_min ({heap} as s) = if Vec.size heap=0 then raise Not_found; let x = Vec.get heap 0 in - Elt.set_idx x _absent_index; let new_hd = Vec.pop heap in (* new head *) Vec.set heap 0 new_hd; 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 *) if Vec.size heap > 1 then ( percolate_down s new_hd;