123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)letis_empty=function[]->true|_->falseletrev_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.revletrecremovenb=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_sortedcomparenlletrecdrop_nnl=ifn<=0thenlelsematchlwith[]->[]|_::xs->drop_n(n-1)xsletselectnl=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 *)letshuffle?rng_statel=letrandint=matchrng_statewith|None->Random.int|Somerng_state->Random.State.intrng_stateinleta=Array.of_listlinletlen=Array.lengthainfori=lendownto2doletm=randintiinletn'=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)tailinfind0listletfilter_somel=(* Written like this (building backward and with a final List.rev)
so that it is tail-recursive *)letrecgoacc=function|[]->List.revacc|None::xs->goaccxs|Somex::xs->go(x::acc)xsingo[]lletrecfind_mapf=function|[]->None|x::l->(matchfxwithNone->find_mapfl|r->r)letfold_left_ifinitl=List.fold_left(fun(i,accu)x->letaccu=fiaccuxin(i+1,accu))(0,init)l|>snd