12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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. *)(* *)(*****************************************************************************)letbool=1letint8=1letuint8=1letchar=1letint16=2letuint16=2letuint30=4letuint32=4letuint64=8letint31=4letint32=4letint64=8letfloat=8typetag_size=[`Uint8|`Uint16]lettag_size=function`Uint8->uint8|`Uint16->uint16typesigned_integer=[`Int31|`Int16|`Int8]typeunsigned_integer=[`Uint30|`Uint16|`Uint8]typeinteger=[signed_integer|unsigned_integer]letsigned_range_to_sizeminmax:[>signed_integer]=ifmin>=~-128&&max<=127then`Int8elseifmin>=~-32_768&&max<=32_767then`Int16else`Int31(* max should be centered at zero *)letunsigned_range_to_sizemax:[>unsigned_integer]=assert(max>=0);ifmax<=255then`Uint8elseifmax<=65535then`Uint16else`Uint30letinteger_to_size=function|`Int31->int31|`Int16->int16|`Int8->int8|`Uint30->uint30|`Uint16->uint16|`Uint8->uint8letmax_int=function|`Uint30|`Int31->(1lsl30)-1|`Int16->(1lsl15)-1|`Int8->(1lsl7)-1|`Uint16->(1lsl16)-1|`Uint8->(1lsl8)-1letmin_int=function|`Uint8|`Uint16|`Uint30->0|`Int31->-(1lsl30)|`Int16->-(1lsl15)|`Int8->-(1lsl7)letrange_to_size~minimum~maximum:integer=ifminimum<0thensigned_range_to_sizeminimummaximumelseunsigned_range_to_size(maximum-minimum)letenum_sizearr=unsigned_range_to_size(Array.lengtharr)