123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2018-2021 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)letmay_consxsx=matchxwithNone->xs|Somex->x::xsletrev_subln=ifn<0theninvalid_arg"Utils.rev_sub: `n` must be non-negative.";letrecappend_rev_subaccl=function|0->acc|n->(matchlwith|[]->acc|hd::tl->append_rev_sub(hd::acc)tl(n-1))inappend_rev_sub[]lnletsubln=rev_subln|>List.revletfirst_someo1o2=match(o1,o2)with(Some_,_)->o1|(None,o2)->o2letmerge_filter2?(finalize=List.rev)?(compare=compare)?(f=first_some)l1l2=letsort=List.sortcompareinletrecmerge_auxacc=function|([],[])->finalizeacc|(r1,[])->finalizeacc@List.filter_map(funx1->f(Somex1)None)r1|([],r2)->finalizeacc@List.filter_map(funx2->fNone(Somex2))r2|((h1::t1asr1),(h2::t2asr2))->ifcompareh1h2>0thenmerge_aux(may_consacc(fNone(Someh2)))(r1,t2)elseifcompareh1h2<0thenmerge_aux(may_consacc(f(Someh1)None))(t1,r2)else(* m1 = m2 *)merge_aux(may_consacc(f(Someh1)(Someh2)))(t1,t2)inmerge_aux[](sortl1,sortl2)letmerge2?finalize?compare?(f=funx1_x1->x1)l1l2=merge_filter2?finalize?compare~f:(funx1x2->match(x1,x2)with|(None,None)->assertfalse|(Somex1,None)->Somex1|(None,Somex2)->Somex2|(Somex1,Somex2)->Some(fx1x2))l1l2letrecremovenb=function|[]->[]|lwhennb<=0->l|_::tl->remove(nb-1)tlletrecrepeatnx=ifn<=0then[]elsex::repeat(predn)xletsplit_nnl=letrecloopaccn=function|[]->(l,[])|remwhenn<=0->(List.revacc,rem)|x::xs->loop(x::acc)(predn)xsinloop[]nllettake_n_unsortednl=fst(split_nnl)lettake_n_sorted(typea)comparenl=letmoduleB=Bounded_heap.Make(structtypet=aletcompare=compareend)inlett=B.createninList.iter(funx->B.insertxt)l;B.gettlettake_n?comparenl=matchcomparewith|None->take_n_unsortednl|Somecompare->take_n_sortedcomparenlletselectnl=letrecloopnacc=function|[]->invalid_arg"Utils.select"|x::xswhenn<=0->(x,List.rev_appendaccxs)|x::xs->loop(predn)(x::acc)xsinloopn[]lletshift=function[]->[]|hd::tl->tl@[hd]letrecproductab=matchawith|[]->[]|hd::tl->List.map(funx->(hd,x))b@producttlb(* Use Fisher-Yates shuffle as described by Knuth
https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle *)letshufflel=leta=Array.of_listlinletlen=Array.lengthainfori=lendownto2doletm=Random.intiinletn'=i-1inifm<>n'then(lettmp=a.(m)ina.(m)<-a.(n');a.(n')<-tmp)done;Array.to_listaletindex_of?(compare=Stdlib.compare)itemlist=letrecfindindex=function|[]->None|head::tail->ifcompareheaditem=0thenSomeindexelsefind(index+1)tailinfind0listletrecfind_mapf=function|[]->None|x::l->(matchfxwithNone->find_mapfl|r->r)