12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182# 1 "src/base/misc/owl_countmin_table.ml"(* A module type for the tables used to implement the CountMin sketch. *)moduletypeSig=sig(** {5 Type definition} *)typet(** The type of count-min tables *)(** {5 Core functions} *)valinit:int->int->t(**
[init l w] generates a table with length [l] and width [w],
all counters initialized to 0.
*)valincr:int->int->t->unit(**
[incr i j t] increments the counter at length index [i]
and width index [j] in table [t].
*)valget:int->int->t->int(**
[get i j t] gets the value of the counter at length index [i]
and width index [j] in table [t].
*)valclone:t->t(**
[clone t] returns a new table with the same contents as [t].
*)valmerge:t->t->t(**
[merge t1 t2] merges tables [t1] and [t2] element-wise. If [t1] and [t2]
have the same dimensions, returns a new table whose elements are the sums of corresponding
elements from [t1] and [t2]. If dimensions do not match, raises [INVALID_ARGUMENT].
*)end(* Implementation of the CountMin sketch table using OCaml native arrays *)moduleNative:Sig=structtypet=intarrayarrayletinitlw=Array.make_matrixlw0letincrijt=t.(i).(j)<-t.(i).(j)+1letgetijt=t.(i).(j)letclone=Array.map(Array.map(funx->x))letmerget1t2=tryArray.map2(Array.map2(+))t1t2with|Invalid_argument_->raise(Owl_exception.INVALID_ARGUMENT"Cannot merge countmin tables of different dimensions")end(* Implementation of the CountMin sketch table using Owl ndarrays *)moduleOwl:Sig=structtypet=Owl_base_dense_ndarray_s.arrletinitlw=Owl_base_dense_ndarray_s.zeros[|l;w|]letincrijt=Owl_base_dense_ndarray_s.(sett[|i;j|](gett[|i;j|]+.1.))letgetijt=Owl_base_dense_ndarray_s.gett[|i;j|]|>int_of_floatletclonet=Owl_base_dense_ndarray_s.copytletmerget1t2=letopenOwl_base_dense_ndarray_sinifshapet1=shapet2thenaddt1t2elseraise(Owl_exception.INVALID_ARGUMENT"Cannot merge countmin tables of different dimensions")end