123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *)(* Copyright (c) 2021-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. *)(* *)(*****************************************************************************)openScript_typed_irletmakex=Map_tagxletget_module(Map_tagx)=xletempty_from:typeabc.(a,b)map->(a,c)map=fun(Map_tag(moduleBox))->Map_tag(modulestructtypekey=atypevalue=cmoduleOPS=Box.OPSletboxed=OPS.emptyletsize=0end)letempty:typeab.acomparable_ty->(a,b)map=funty->letmoduleOPS=structletkey_size=Gas_comparable_input_size.size_of_comparable_valuetyincludeMap.Make(structtypet=aletcompare=Script_comparable.compare_comparabletyend)endinMap_tag(modulestructtypekey=atypevalue=bmoduleOPS=OPSletboxed=OPS.emptyletsize=0end)letget:typekeyvalue.key->(key,value)map->valueoption=funk(Map_tag(moduleBox))->Box.OPS.findkBox.boxedletupdate:typeab.a->boption->(a,b)map->(a,b)map=funkv(Map_tag(moduleBox))->letboxed,size=letcontains=matchBox.OPS.findkBox.boxedwithNone->false|_->trueinmatchvwith|Somev->(Box.OPS.addkvBox.boxed,Box.size+ifcontainsthen0else1)|None->(Box.OPS.removekBox.boxed,Box.size-ifcontainsthen1else0)inMap_tag(modulestructtypekey=atypevalue=bmoduleOPS=Box.OPSletboxed=boxedletsize=sizeend)letmem:typekeyvalue.key->(key,value)map->bool=funk(Map_tag(moduleBox))->matchBox.OPS.findkBox.boxedwithNone->false|_->trueletfold:typekeyvalueacc.(key->value->acc->acc)->(key,value)map->acc->acc=funf(Map_tag(moduleBox))->Box.OPS.foldfBox.boxedletfold_es:typekeyvalueacc.(key->value->acc->acctzresultLwt.t)->(key,value)map->acc->acctzresultLwt.t=funf(Map_tag(moduleBox))->Box.OPS.fold_esfBox.boxedletsize:typekeyvalue.(key,value)map->Script_int.nScript_int.num=fun(Map_tag(moduleBox))->Script_int.(abs(of_intBox.size))letmap_es_in_context:typecontextkeyvaluevalue'.(context->key->value->(value'*context)tzresultLwt.t)->context->(key,value)map->((key,value')map*context)tzresultLwt.t=funfctxt(Map_tag(moduleBox))->Box.OPS.fold_es(funkeyvalue(map,ctxt)->fctxtkeyvalue>|=?fun(value,ctxt)->(Box.OPS.addkeyvaluemap,ctxt))Box.boxed(Box.OPS.empty,ctxt)>|=?fun(map,ctxt)->(Map_tag(modulestructtypekey=Box.keytypevalue=value'moduleOPS=Box.OPSletboxed=mapletsize=Box.sizeend),ctxt)