123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132(**************************************************************************)(* This file is part of BINSEC. *)(* *)(* Copyright (C) 2016-2026 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It 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. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file licenses/LGPLv2.1). *)(* *)(**************************************************************************)moduletypeS=sigtypeelttypetvalempty:tvalsingleton:elt->tvalis_empty:t->boolvallength:t->intvaladd:elt->t->tvalremove:t->tvalpop:t->elt*tvalpeek:t->eltvalmerge:t->t->tvaliter:(elt->unit)->t->unitvalfold:('a->elt->'a)->'a->t->'avalmap:(elt->elt)->t->tendmoduleMake(X:Sigs.COMPARABLE)=structtypet=Empty|HeapofX.t*tlisttypeelt=X.tletempty=Emptyletsingletonx=Heap(x,[])letis_emptyh=h=Emptyletmergeh1h2=match(h1,h2)with|Empty,_->h2|_,Empty->h1|Heap(x1,l1),Heap(x2,l2)->ifX.comparex1x2>0thenHeap(x1,h2::l1)elseHeap(x2,h1::l2)letrecmerge_pairsl=matchlwith|[]->Empty|[h]->h|h1::h2::hs->merge(mergeh1h2)(merge_pairshs)letaddxh=merge(Heap(x,[]))hletremoveh=matchhwithEmpty->failwith"remove"|Heap(_,l)->merge_pairslletpeekh=matchhwithEmpty->failwith"peek"|Heap(x,_)->xletpoph=tryletv=peekhin(v,removeh)withFailure_->failwith"pop"letrecfoldfacc=function|Empty->acc|Heap(x,l)->List.fold_left(foldf)(faccx)lletrecmapf=function|Empty->Empty|Heap(x,l)->letl'=List.map(mapf)linadd(fx)(merge_pairsl')letreciterf=function|Empty->()|Heap(x,l)->fx;List.iter(iterf)lletlengthh=fold(funacc_->acc+1)0hendmoduleCMake(X:Sigs.ANY)=structmoduleH=Make(structtypet=X.t*intletcompare(_,n1)(_,n2)=comparen1n2end)typeelt=X.ttypet=H.tletfront_idx=ref0letrear_idx=ref0letfrontx=incrfront_idx;(x,!front_idx)letrearx=decrrear_idx;(x,!rear_idx)letempty=H.emptyletis_empty=H.is_emptyletaddx=H.add(frontx)letremove=H.removeletconsx=addxletsingletonx=addxemptyletsnocx=H.add(rearx)letpoph=let(v,_),l=H.pophin(v,l)letpeekh=fst(H.peekh)letfoldfacch=H.fold(funacc(e,_)->facce)acchletiterfh=H.iter(fun(e,_)->fe)hletmapfh=H.map(fun(e,n)->(fe,n))hletmerge=H.mergeletlength=H.lengthend