Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file indexable.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)typeindex_only=Index_onlytypevalue_only=Value_onlytypeunknown=Unknowntype(_,'a)t=|Value:'a->(value_only,'a)t|Hidden_value:'a->(unknown,'a)t|Index:int32->(index_only,'a)t|Hidden_index:int32->(unknown,'a)ttypeerror+=Index_cannot_be_negativeofint32let()=letopenData_encodinginregister_error_kind`Permanent~id:"indexable.index_cannot_be_negative"~title:"Index of values cannot be negative"~description:"A negative integer cannot be used as an index for a value."~pp:(funppfwrong_id->Format.fprintfppf"%ld cannot be used as an index because it is negative."wrong_id)(obj1(req"wrong_index"int32))(functionIndex_cannot_be_negativewrong_id->Somewrong_id|_->None)(funwrong_id->Index_cannot_be_negativewrong_id)type'avalue=(value_only,'a)ttype'aindex=(index_only,'a)ttype'aeither=(unknown,'a)tletvalue:'a->'avalue=funv->Valuevletfrom_value:'a->'aeither=funv->Hidden_valuevletindex:int32->'aindextzresult=funi->ifCompare.Int32.(0l<=i)thenok(Indexi)elseerror(Index_cannot_be_negativei)letfrom_index:int32->'aeithertzresult=funi->ifCompare.Int32.(0l<=i)thenok(Hidden_indexi)elseerror(Index_cannot_be_negativei)letindex_exn:int32->'aindex=funi->matchindexiwith|Okx->x|Error_->raise(Invalid_argument"Indexable.index_exn")letfrom_index_exn:int32->'aeither=funi->matchfrom_indexiwith|Okx->x|Error_->raise(Invalid_argument"Indexable.from_index_exn")letdestruct:typestatea.(state,a)t->(aindex,a)Either.t=function|Hidden_valuex|Valuex->Rightx|Hidden_indexx|Indexx->Left(Indexx)letforget:typestatea.(state,a)t->(unknown,a)t=function|Hidden_valuex|Valuex->Hidden_valuex|Hidden_indexx|Indexx->Hidden_indexxletto_int32=functionIndexx->xletto_value=functionValuex->xletis_value_e:error:'trace->('state,'a)t->('a,'trace)result=fun~errorv->matchdestructvwithLeft_->Result.errorerror|Rightv->Result.okvletcompactval_encoding=Data_encoding.Compact.(conv(functionHidden_indexx->Either.Leftx|Hidden_valuex->Rightx)(functionLeftx->Hidden_indexx|Rightx->Hidden_valuex)@@or_int32~int32_title:"index"~alt_title:"value"val_encoding)letencoding:'aData_encoding.t->'aeitherData_encoding.t=funval_encoding->Data_encoding.Compact.make~tag_size:`Uint8@@compactval_encodingletpp:typestatea.(Format.formatter->a->unit)->Format.formatter->(state,a)t->unit=funppvfmt->function|Hidden_indexx|Indexx->Format.(fprintffmt"#%ld"x)|Hidden_valuex|Valuex->Format.(fprintffmt"%a"ppvx)letin_memory_size:typestatea.(a->Cache_memory_helpers.sint)->(state,a)t->Cache_memory_helpers.sint=funims->letopenCache_memory_helpersinfunction|Hidden_valuex|Valuex->header_size+!word_size+!imsx|Hidden_index_|Index_->header_size+!word_size+!int32_sizeletsize:typestatea.(a->int)->(state,a)t->int=funs->function|Hidden_valuex|Valuex->1+sx|Hidden_index_|Index_->(* tag + int32 *)1+4letcompare:typestatestate'a.(a->a->int)->(state,a)t->(state',a)t->int=funcxy->match(x,y)with|((Hidden_indexx|Indexx),(Hidden_indexy|Indexy))->Compare.Int32.comparexy|((Hidden_valuex|Valuex),(Hidden_valuey|Valuey))->cxy|((Hidden_index_|Index_),(Hidden_value_|Value_))->-1|((Hidden_value_|Value_),(Hidden_index_|Index_))->1letcompare_valuesc:'avalue->'avalue->int=fun(Valuex)(Valuey)->cxyletcompare_indexes:'aindex->'aindex->int=fun(Indexx)(Indexy)->Compare.Int32.comparexymoduletypeVALUE=sigtypetvalencoding:tData_encoding.tvalcompare:t->t->intvalpp:Format.formatter->t->unitendmoduleMake(V:VALUE)=structtypenonrec'statet=('state,V.t)ttypenonrecindex=V.tindextypenonrecvalue=V.tvaluetypenonreceither=V.teitherletvalue=valueletindex=indexletindex_exn=index_exnletcompact=compactV.encodingletencoding=encodingV.encodingletindex_encoding:indexData_encoding.t=Data_encoding.(conv(fun(Indexx)->x)(funx->Indexx)Data_encoding.int32)letvalue_encoding:valueData_encoding.t=Data_encoding.(conv(fun(Valuex)->x)(funx->Valuex)V.encoding)letpp:Format.formatter->'statet->unit=funfmtx->ppV.ppfmtxletcompare_values=compare_valuesV.compareletcompare_indexes=compare_indexesletcompare:'statet->'state't->int=funxy->compareV.comparexyend