Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file skip_list_repr.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)moduletypeS=sigtype('content,'ptr)cellvalequal:('content->'content->bool)->('ptr->'ptr->bool)->('content,'ptr)cell->('content,'ptr)cell->boolvalencoding:'ptrData_encoding.t->'contentData_encoding.t->('content,'ptr)cellData_encoding.tvalindex:(_,_)cell->intvalcontent:('content,'ptr)cell->'contentvalback_pointer:('content,'ptr)cell->int->'ptroptionvalback_pointers:('content,'ptr)cell->'ptrlistvalgenesis:'content->('content,'ptr)cellvalnext:prev_cell:('content,'ptr)cell->prev_cell_ptr:'ptr->'content->('content,'ptr)cellvalback_path:deref:('ptr->('content,'ptr)celloption)->cell_ptr:'ptr->target_index:int->'ptrlistoptionvalvalid_back_path:equal_ptr:('ptr->'ptr->bool)->deref:('ptr->('content,'ptr)celloption)->cell_ptr:'ptr->target_ptr:'ptr->'ptrlist->boolendmoduleMake(Parameters:sigvalbasis:intend):S=structlet()=assert(Compare.Int.(Parameters.basis>=2))openParameters(*
A cell of a skip list with some [`content] and backpointers of
type [`ptr].
Invariants
----------
- back_pointers[i]
= Some (pointer to (index - (index mod (basis ** i)) - 1))
(for all i < length back_pointers)
- length back_pointers = log basis index
Notes
-----
- The [index] field is not strictly required but helps in making
the data structure more robust. Indeed, otherwise, we should
also ask the client to provide the index of the cell to be
built, which can be error-prone.
- The back pointers of a node are chosen from the back pointers of
its predecessor (except for the genesis node) and a pointer to this
predecessor. This locality makes the insertion of new nodes very
efficient in practice.
*)type('content,'ptr)cell={content:'content;back_pointers:'ptroptionFallbackArray.t;index:int;}letequalequal_contentequal_ptrcell1cell2=letequal_back_pointersb1b2=letopenFallbackArrayinCompare.Int.(lengthb1=lengthb2)&&fst@@fold(fun(equal,i)h1->(equal&&Option.equalequal_ptrh1(getb2i),i+1))b1(true,0)inlet{content;back_pointers;index}=cell1inequal_contentcontentcell2.content&&Compare.Int.equalindexcell2.index&&equal_back_pointersback_pointerscell2.back_pointersletindexcell=cell.indexletback_pointers_to_lista=FallbackArray.fold(funl->function|Someptr->ptr::l|None->(* By [cell] invariants. *)assertfalse)a[]|>List.revletencodingptr_encodingcontent_encoding=letof_list=FallbackArray.of_list~fallback:None~proj:(func->Somec)inletto_list=back_pointers_to_listinletopenData_encodinginconv(fun{index;content;back_pointers}->(index,content,to_listback_pointers))(fun(index,content,back_pointers)->{index;content;back_pointers=of_listback_pointers})(obj3(req"index"int31)(req"content"content_encoding)(req"back_pointers"(listptr_encoding)))letcontentcell=cell.contentletback_pointerscell=back_pointers_to_listcell.back_pointersletgenesiscontent={index=0;content;back_pointers=FallbackArray.make0None}letback_pointercelli=FallbackArray.getcell.back_pointersi(* Precondition: i < length cell.back_pointers *)letback_pointer_unsafecelli=matchFallbackArray.getcell.back_pointersiwith|Someptr->ptr|None->(* By precondition and invariants of cells. *)assertfalseletnext~prev_cell~prev_cell_ptrcontent=letindex=prev_cell.index+1inletback_pointers=letrecauxpoweraccui=ifCompare.Int.(index<power)thenList.revaccuelseletback_pointer_i=ifCompare.Int.(indexmodpower=0)thenprev_cell_ptrelse(* The following call is valid because of
- [i < List.length prev_cell.back_pointer]
because [log_basis index = log_basis prev_cell.index]
- the invariants of [prev_cell] *)back_pointer_unsafeprev_celliinletaccu=back_pointer_i::accuinaux(power*basis)accu(i+1)inaux1[]0inletback_pointers=FallbackArray.of_list~fallback:None~proj:Option.someback_pointersin{index;content;back_pointers}letbest_skipcelltarget_index=letindex=cell.indexinletrecauxidxpowbest_idx=ifCompare.Int.(idx>=FallbackArray.lengthcell.back_pointers)thenbest_idxelseletidx_index=index-(indexmodpow)-1inifCompare.Int.(idx_index<target_index)thenbest_idxelseaux(idx+1)(basis*pow)(Someidx)inaux01Noneletback_path~deref~cell_ptr~target_index=letrecauxpathptr=letpath=ptr::pathinOption.bind(derefptr)@@funcell->letindex=cell.indexinifCompare.Int.(target_index=index)thenSome(List.revpath)elseifCompare.Int.(target_index>index)thenNoneelseOption.bind(best_skipcelltarget_index)@@funbest_idx->Option.bind(back_pointercellbest_idx)@@funptr->auxpathptrinaux[]cell_ptrletmemequalxl=letopenFallbackArrayinletn=lengthlinletrecauxidx=ifCompare.Int.(idx>=n)thenfalseelsematchFallbackArray.getlidxwith|None->aux(idx+1)|Somey->ifequalxythentrueelseaux(idx+1)inaux0letassume_someof=matchowithNone->false|Somex->fxletvalid_back_path~equal_ptr~deref~cell_ptr~target_ptrpath=assume_some(dereftarget_ptr)@@funtarget->assume_some(derefcell_ptr)@@funcell->lettarget_index=indextargetandcell_index=indexcellinletrecvalid_pathindexcell_ptrpath=match(cell_ptr,path)with|final_cell,[]->equal_ptrtarget_ptrfinal_cell&&Compare.Int.(index=target_index)|cell_ptr,cell_ptr'::path->assume_some(derefcell_ptr)@@funcell->assume_some(derefcell_ptr')@@funcell'->memequal_ptrcell_ptr'cell.back_pointers&&assume_some(best_skipcelltarget_index)@@funbest_idx->assume_some(back_pointercellbest_idx)@@funbest_ptr->letminimal=equal_ptrbest_ptrcell_ptr'inletindex'=cell'.indexinminimal&&valid_pathindex'cell_ptr'pathinmatchpathwith|[]->false|first_cell_ptr::path->equal_ptrfirst_cell_ptrcell_ptr&&valid_pathcell_indexcell_ptrpathend