123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778openCore(* a single 63 bit chunk of the array, bounds checking is left to the main module. We can
only use 62 bits, because of the sign bit *)moduleInt63_chunk:sigtypetvalempty:tvalget:t->int->boolvalset:t->int->bool->tend=structopenInt63typet=Int63.tletempty=zeroletgetti=bit_andt(shift_leftonei)>zeroletsettiv=ifvthenbit_ort(shift_leftonei)elsebit_andt(bit_xorminus_one(shift_leftonei));;endtypet={data:Int63_chunk.tArray.t;length:int}(* We can't use the sign bit, so we only get to use 62 bits *)letbits_per_bucket=62letcreatesz=ifsz<0||sz>Array.max_length*bits_per_buckettheninvalid_argf"invalid size"();{data=Array.create~len:(1+(sz/bits_per_bucket))Int63_chunk.empty;length=sz};;letlengtht=t.lengthletbucketi=i/bits_per_bucketletindexi=imodbits_per_bucketletbounds_checkti=ifi<0||i>=t.lengththeninvalid_argf"Bitarray: out of bounds"();;letgetti=bounds_checkti;Int63_chunk.gett.data.(bucketi)(indexi);;letsettiv=bounds_checkti;letbucket=bucketiint.data.(bucket)<-Int63_chunk.sett.data.(bucket)(indexi)v;;letcleart=Array.fillt.data~pos:0~len:(Array.lengtht.data)Int63_chunk.emptyletfold=letreclooptn~init~f=ifn<t.lengththenloopt(n+1)~init:(finit(gettn))~felseinitinfunt~init~f->loopt0~init~f;;letitert~f=foldt~init:()~f:(fun_v->fv)letsexp_of_tt=Array.sexp_of_tBool.sexp_of_t(Array.initt.length~f:(funi->getti));;lett_of_sexpsexp=leta=Array.t_of_sexpBool.t_of_sexpsexpinlett=create(Array.lengtha)inArray.iteria~f:(funiv->settiv);t;;