123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 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. *)(* *)(*****************************************************************************)moduleMake(K:Hashtbl.HashedType)(V:sigtypetend)=structmoduleCache=Aches.Vache.Map(Aches.Vache.FIFO_Precise)(Aches.Vache.Strong)(K)includeCachetypenonrect=V.ttletelementsq=Cache.fold(fun_xacc->x::acc)q[]letkeysq=Cache.fold(funk_acc->k::acc)q[]letbindingsq=Cache.fold(funkxacc->(k,x)::acc)q[](** [oldest_elements q n f] returns the (at most) [n] oldest elements of the
queue and calls [f] on the bindings for these elements. The elements are
returned from oldest to newest. *)letoldest_elementsqnaction=letexceptionElementsofV.tlistinletrev_elts=tryCache.fold_oldest_first(funkv(count,acc)->ifcount>=nthenraise(Elementsacc)else(actionkvq;(count+1,v::acc)))q(0,[])|>sndwithElementsacc->accinList.revrev_elts(* Redefining fold to have elements treated in order of oldest to newest *)letfoldfqacc=Cache.fold_oldest_firstfqaccletfold_sfqacc=letopenLwt.Syntaxinfold(funkvacc->let*accinfkvacc)q(Lwt.returnacc)letfold_es(typeerror)fqacc:(_,error)resultLwt.t=letopenLwt.SyntaxinletexceptionErroroferrorinLwt.try_bind(fun()->fold_s(funkvacc->let*res=fkvaccinmatchreswith|Okacc->Lwt.returnacc|Errore->Lwt.fail(Errore))qacc)Lwt.return_ok(functionErrore->Lwt.return_errore|e->Lwt.faile)letpeekq=matcholdest_elementsq1(fun___->())with|[]->None|[x]->Somex|_->assertfalselettakeq=matcholdest_elementsq1(funk_q->removeqk)with|[]->None|[x]->Somex|_->assertfalseletpeek_at_mostqn=oldest_elementsqn(fun___->())lettake_at_mostqn=(* Removing the keys during the fold does not work, accumulating the keys
then removing them does the trick. *)letkeys=ref[]inletvalues=oldest_elementsqn(funk__->keys:=k::!keys)inList.iter(removeq)!keys;valuesend