123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Trili Tech <contact@trili.tech> *)(* *)(* 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. *)(* *)(*****************************************************************************)moduletypeKeyS=sigincludeMap.OrderedTypevalto_string:t->stringendmoduletypeS=sigtypekeytype'aproducer=key->'aLwt.tmoduleMap:Map.Swithtypekey=keytype'atvalorigin:'at->Tezos_tree_encoding.wrapped_treeoptionvalstring_of_key:key->stringvalpp:(Format.formatter->'a->unit)->Format.formatter->'at->unitvalto_string:('a->string)->'at->stringvalcreate:?values:'aMap.t->?produce_value:'aproducer->?origin:Tezos_tree_encoding.wrapped_tree->unit->'atvalget:key->'at->'aLwt.tvalset:key->'a->'at->'atvalremove:key->'at->'atvaldup:'at->'atvalloaded_bindings:'at->(key*'aoption)listendexceptionUnexpectedAccessmoduleMake(Key:KeyS):Swithtypekey=Key.t=structmoduleMap=Map.Make(Key)typekey=Key.ttype'aproducer=key->'aLwt.ttype'at={origin:Tezos_tree_encoding.wrapped_treeoption;produce_value:'aproducer;mutablevalues:'aoptionMap.t;}letorigin{origin;_}=originletstring_of_key=Key.to_stringletpppp_value=letpp_valuesfmtvalues=Map.bindingsvalues|>Format.fprintffmt"@[<hv>%a@]"(Format.pp_print_list~pp_sep:(funppf()->Format.fprintfppf";@ ")(funppf(k,v)->Format.fprintfppf"%s => %a"(Key.to_stringk)(Format.pp_print_optionpp_value)v))infunfmtmap->Format.fprintffmt"@[<hv 2>{ values = @[<hv 2>[ %a ]@] }@]"pp_valuesmap.valuesletto_stringshow_valuemap=letpp_valuefmtvalue=Format.pp_print_stringfmt(show_valuevalue)inFormat.asprintf"%a"(pppp_value)mapletdef_produce_value_=raiseUnexpectedAccessletcreate?(values=Map.empty)?(produce_value=def_produce_value)?origin()=letvalues=Map.mapOption.somevaluesin{produce_value;values;origin}letgetkeymap=letopenLwt.SyntaxinmatchMap.find_optkeymap.valueswith|None->(* Need to create the missing key-value association. *)let+value=map.produce_valuekeyinmap.values<-Map.addkey(Somevalue)map.values;value|SomeNone->(* The key was removed *)raiseUnexpectedAccess|Some(Somevalue)->Lwt.returnvalueletsetkeyvaluemap={mapwithvalues=Map.addkey(Somevalue)map.values}letremovekeymap={mapwithvalues=Map.addkeyNonemap.values}letdup{origin;produce_value;values}={origin;produce_value;values}letloaded_bindingsm=Map.bindingsm.valuesendmoduleLwtIntMap=Make(Int)moduleLwtInt32Map=Make(Int32)moduleLwtInt64Map=Make(Int64)moduleMutable=structmoduletypeS=sigtypekeymoduleMap:Swithtypekey=keytype'atvalof_immutable:'aMap.t->'atvalcreate:?values:'aMap.Map.t->?produce_value:'aMap.producer->?origin:Tezos_tree_encoding.wrapped_tree->unit->'atvalget:key->'at->'aLwt.tvalset:key->'a->'at->unitvalremove:key->'at->unitvalsnapshot:'at->'aMap.tendmoduleMake(Key:KeyS):Swithtypekey=Key.t=structmoduleMap=Make(Key)typekey=Map.keytype'at='aMap.trefletof_immutable=refletcreate?values?produce_value?originunit=of_immutable(Map.create?values?produce_value?originunit)letgetkeymap_ref=Map.getkey!map_refletsetkeyvaluemap_ref=map_ref:=Map.setkeyvalue!map_refletremovekeymap_ref=map_ref:=Map.removekey!map_refletsnapshotmap_ref=!map_refendmoduleLwtIntMap=Make(Int)moduleLwtInt32Map=Make(Int32)moduleLwtInt64Map=Make(Int64)end