123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129(**************************************************************************)(* *)(* Copyright (C) Jean-Christophe Filliatre *)(* *)(* This software is free software; you can redistribute it and/or *)(* modify it under the terms of the GNU Library General Public *)(* License version 2.1, with the special exception on linking *)(* described in file LICENSE. *)(* *)(* This software is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)(* *)(**************************************************************************)(*s Heaps *)moduletypeOrdered=sigtypetvalcompare:t->t->intendexceptionEmptymoduleMake(X:Ordered)=struct(* The heap is encoded in the array [data], where elements are stored
from [0] to [size - 1]. From an element stored at [i], the left
(resp. right) subtree, if any, is rooted at [2*i+1] (resp. [2*i+2]). *)typet={mutablesize:int;mutabledata:X.tarray;dummy:X.t;min_cap:int;(* minimal capacity, as given initially *)}(* invariant 0 <= size <= length data *)(* invariant data[size..] only contains dummy *)letcreate~dummyn=ifn<0||n>Sys.max_array_lengththeninvalid_arg"create";letn=max16nin{size=0;data=Array.makendummy;dummy=dummy;min_cap=n}letlengthh=h.sizeletis_emptyh=h.size=0(* [enlarge] doubles the size of [data] *)letenlargeh=letn=h.sizeinassert(n>0&&n=Array.lengthh.data);letn'=min(2*n)Sys.max_array_lengthinifn'=nthenfailwith"maximum capacity reached";letd=h.datainletd'=Array.maken'h.dummyinArray.blitd0d'0n;h.data<-d'letshrinkh=letn=Array.lengthh.datainletn'=maxh.min_cap(n/2)inassert(h.size<=n'&&n'<=n);ifn'<nthenbeginletd=h.datainletd'=Array.maken'h.dummyinArray.blitd0d'0h.size;h.data<-d'endletaddhx=letn=h.sizeinifn==Array.lengthh.datathenenlargeh;letd=h.datainletrecmoveupi=letfi=(i-1)/2inifi>0&&X.compared.(fi)x>0thenbegind.(i)<-d.(fi);moveupfiendelsed.(i)<-xinmoveupn;h.size<-n+1letminimumh=ifh.size<=0thenraiseEmpty;h.data.(0)letremoveh=ifh.size<=0thenraiseEmpty;letn=h.size-1inh.size<-n;letd=h.datainletx=d.(n)ind.(n)<-h.dummy;letrecmovedowni=letj=2*i+1inifj<nthenletj=letj'=j+1inifj'<n&&X.compared.(j')d.(j)<0thenj'elsejinifX.compared.(j)x<0thenbegind.(i)<-d.(j);movedownjendelsed.(i)<-xelsed.(i)<-xinmovedown0;if4*h.size<Array.lengthh.datathenshrinkhletpop_minimumh=letm=minimumhinremoveh;mletiterfh=letd=h.datainfori=0toh.size-1dofd.(i)doneletfoldfhx0=letn=h.sizeinletd=h.datainletrecfoldrecxi=ifi>=nthenxelsefoldrec(fd.(i)x)(succi)infoldrecx00end