123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960# 1 "src/base/misc/owl_countmin_sketch.ml"(* Functor to make the CountMin sketch using a specified underlying table *)moduleMake(T:Owl_countmin_table.Sig):Owl_countmin_sketch_sig.Sig=struct(* the type of sketches *)type'asketch={tbl:T.t;w:int;hash_fns:(int*int*int)array}(* the prime number to use for hashing *)letbigprime=(1lsl31)-1(* Calculate (a*x + b) mod (2^31 - 1) *)lethash31xab=letresult=(a*x)+bin((resultlsr31)+result)landbigprimeletinit_lwlw=letgen_rand_int_=Owl_base_stats_dist_uniform.uniform_int_rvs((1lsl30)-1)inletgen_rand_pairi=i,gen_rand_int(),gen_rand_int()in{tbl=T.initlw;w;hash_fns=Array.initlgen_rand_pair}letinit~epsilon~delta=(* set l = log (1/delta) and w = 1/epsilon *)init_lwOwl_base_maths.(1./.delta|>log2|>ceil|>int_of_float)Owl_base_maths.(1./.epsilon|>ceil|>int_of_float)letincrsx=letxh=Hashtbl.hashxinletiterfn(i,ai,bi)=T.incri(hash31xhaibimods.w)s.tblinArray.iteriterfns.hash_fnsletcountsx=letxh=Hashtbl.hashxinletfoldfnprv(i,ai,bi)=T.geti(hash31xhaibimods.w)s.tbl|>minprvinArray.fold_leftfoldfnmax_ints.hash_fnsletinit_from{tbl=_;w;hash_fns}=letl=Array.lengthhash_fnsin{tbl=T.initlw;w;hash_fns=Array.copyhash_fns}letmerges1s2=letopenOwl_exceptioninletexn=INVALID_ARGUMENT"Attempt to merge non-mergeable count-min sketches"incheck(s1.w=s2.w)exn;check(s1.hash_fns=s2.hash_fns)exn;try{tbl=T.merges1.tbls2.tbl;w=s1.w;hash_fns=s1.hash_fns}with|INVALID_ARGUMENT_->raiseexnendmoduleNative=Make(Owl_countmin_table.Native)moduleOwl=Make(Owl_countmin_table.Owl)