Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file Belt_internalBuckets.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207[@@@ocaml.text" Adapted by Authors of BuckleScript 2017 "]moduleC=Belt_internalBucketsTypetype('a,'b)bucket={mutablekey:'a;mutablevalue:'b;mutablenext:('a,'b)bucketC.opt;}and('hash,'eq,'a,'b)t=('hash,'eq,('a,'b)bucket)C.containerletbucket:key:'a->value:'b->next:('a,'b)bucketC.opt->('a,'b)bucket=fun~key~value~next->{key;value;next}letkeySet:('a,'b)bucket->'a->unit=funov->o.key<-vletkey:('a,'b)bucket->'a=funo->o.keyletvalueSet:('a,'b)bucket->'b->unit=funov->o.value<-vletvalue:('a,'b)bucket->'b=funo->o.valueletnextSet:('a,'b)bucket->('a,'b)bucketC.opt->unit=funov->o.next<-vletnext:('a,'b)bucket->('a,'b)bucketC.opt=funo->o.nextmoduleA=Belt_Arrayletreccopy(x:_t):_t=C.container~hash:(C.hashx)~eq:(C.eqx)~size:(C.sizex)~buckets:(copyBuckets(C.bucketsx))andcopyBuckets(buckets:_bucketC.optarray)=letlen=A.lengthbucketsinletnewBuckets=iflen>0thenA.makeUninitializedUnsafelen(A.getUnsafebuckets0)else[||]infori=0tolen-1doA.setUnsafenewBucketsi(copyBucket(A.getUnsafebucketsi))done;newBucketsandcopyBucketc=matchC.toOptcwith|None->c|Somec->lethead=bucket~key:(keyc)~value:(valuec)~next:C.emptyOptincopyAuxCont(nextc)head;C.returnheadandcopyAuxContcprec=matchC.toOptcwith|None->()|Somenc->letncopy=bucket~key:(keync)~value:(valuenc)~next:C.emptyOptinnextSetprec(C.returnncopy);copyAuxCont(nextnc)ncopyletrecbucketLengthaccubuckets=matchC.toOptbucketswith|None->accu|Somecell->bucketLength(accu+1)(nextcell)letrecdo_bucket_iter~fbuckets=matchC.toOptbucketswith|None->()|Somecell->f(keycell)(valuecell);do_bucket_iter~f(nextcell)letforEachUhf=letd=C.bucketshinfori=0toA.lengthd-1dodo_bucket_iterf(A.getUnsafedi)doneletforEachhf=forEachUh(funab->fab)letrecdo_bucket_fold~fbaccu=matchC.toOptbwith|None->accu|Somecell->do_bucket_fold~f(nextcell)(faccu(keycell)(valuecell))letreduceUhinitf=letd=C.bucketshinletaccu=refinitinfori=0toA.lengthd-1doaccu:=do_bucket_fold~f(A.getUnsafedi)!accudone;!acculetreducehinitf=reduceUhinit(funabc->fabc)letgetMaxBucketLengthh=A.reduceU(C.bucketsh)0(funmb->letlen=bucketLength0binStdlib.maxmlen)letgetBucketHistogramh=letmbl=getMaxBucketLengthhinlethisto=A.makeByU(mbl+1)(fun_->0)inA.forEachU(C.bucketsh)(funb->letl=bucketLength0binA.setUnsafehistol(A.getUnsafehistol+1));histoletlogStatsh=lethistogram=getBucketHistogramhinPrintf.printf"{\n\tbindings: %d,\n\tbuckets: %d\n\thistogram: %s\n}"(C.sizeh)(A.length(C.bucketsh))(A.reduceUhistogram""(funaccx->acc^string_of_intx))letrecfilterMapInplaceBucketfhipreccell=letn=nextcellinmatchf(keycell)(valuecell)with|None->(C.sizeSeth(C.sizeh-1);matchC.toOptnwith|SomenextCell->filterMapInplaceBucketfhiprecnextCell|None->(matchC.toOptprecwith|None->A.setUnsafe(C.bucketsh)iprec|Somecell->nextSetcelln))|Somedata->(letbucket=C.returncellin(matchC.toOptprecwith|None->A.setUnsafe(C.bucketsh)ibucket|Somec->nextSetcellbucket);valueSetcelldata;matchC.toOptnwith|None->nextSetcelln|SomenextCell->filterMapInplaceBucketfhibucketnextCell)[@@ocaml.doc" iterate the Buckets, in place remove the elements "]letkeepMapInPlaceUhf=leth_buckets=C.bucketshinfori=0toA.lengthh_buckets-1doletv=A.getUnsafeh_bucketsiinmatchC.toOptvwith|None->()|Somev->filterMapInplaceBucketfhiC.emptyOptvdoneletkeepMapInPlacehf=keepMapInPlaceUh(funab->fab)letrecfillArrayiarrcell=A.setUnsafearri(keycell,valuecell);matchC.toOpt(nextcell)with|None->i+1|Somev->fillArray(i+1)arrvlettoArrayh=letd=C.bucketshinletcurrent=ref0inletarr=refNoneinfori=0toA.lengthd-1doletcell=A.getUnsafediinmatchC.toOptcellwith|None->()|Somecell->letarr=match!arrwith|None->leta=A.makeUninitializedUnsafe(C.sizeh)(keycell,valuecell)inarr:=Somea;a|Somearr->arrincurrent:=fillArray!currentarrcelldone;match!arrwithNone->[||]|Somearr->arrletrecfillArrayMapiarrcellf=A.setUnsafearri(fcell);matchC.toOpt(nextcell)with|None->i+1|Somev->fillArrayMap(i+1)arrvfletlinearhf=letd=C.bucketshinletcurrent=ref0inletarr=refNoneinfori=0toA.lengthd-1doletcell=A.getUnsafediinmatchC.toOptcellwith|None->()|Somecell->letarr=match!arrwith|None->leta=A.makeUninitializedUnsafe(C.sizeh)(fcell)inarr:=Somea;a|Somearr->arrincurrent:=fillArrayMap!currentarrcellfdone;match!arrwithNone->[||]|Somearr->arrletkeysToArrayh=linearh(funx->keyx)letvaluesToArrayh=linearh(funx->valuex)lettoArrayh=linearh(funx->(keyx,valuex))