mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-05-05 08:54:22 -04:00
cchash64: add apply and combine*
This commit is contained in:
parent
e404dd26ab
commit
76d8ee79e1
2 changed files with 26 additions and 0 deletions
|
|
@ -10,6 +10,8 @@ let[@inline] finalize (s : state) : int = Hash_impl_.finalize s
|
|||
|
||||
type 'a t = state -> 'a -> state
|
||||
|
||||
let[@inline] apply h x = finalize64 (h seed x)
|
||||
let apply_int h x = Int64.to_int (finalize64 (h seed x))
|
||||
let[@inline] int s x = Hash_impl_.combine_int s x
|
||||
|
||||
let[@inline] bool s b =
|
||||
|
|
@ -77,6 +79,20 @@ let gen f s g =
|
|||
in
|
||||
aux s
|
||||
|
||||
let[@inline] combine2 a b =
|
||||
Hash_impl_.(finalize64 (combine_i64 (combine_i64 seed a) b))
|
||||
|
||||
let combine3 a b c =
|
||||
Hash_impl_.(
|
||||
let s = combine_i64 (combine_i64 seed a) b in
|
||||
finalize64 (combine_i64 s c))
|
||||
|
||||
let combine4 a b c d =
|
||||
Hash_impl_.(
|
||||
let s = combine_i64 (combine_i64 seed a) b in
|
||||
let s = combine_i64 s c in
|
||||
finalize64 (combine_i64 s d))
|
||||
|
||||
let array_comm f s a =
|
||||
let hashes = Array.map (fun x -> finalize64 (f seed x)) a in
|
||||
Array.sort Int64.compare hashes;
|
||||
|
|
|
|||
|
|
@ -37,6 +37,12 @@ type 'a t = state -> 'a -> state
|
|||
(** A hash combiner: takes the current state, mixes in a value, returns the
|
||||
updated state. *)
|
||||
|
||||
val apply : 'a t -> 'a -> int64
|
||||
(** Hash the input *)
|
||||
|
||||
val apply_int : 'a t -> 'a -> int
|
||||
(** Hash the input and truncate to [int] *)
|
||||
|
||||
val int : int t
|
||||
val bool : bool t
|
||||
val char : char t
|
||||
|
|
@ -66,6 +72,10 @@ val map : ('a -> 'b) -> 'b t -> 'a t
|
|||
val if_ : bool -> 'a t -> 'a t -> 'a t
|
||||
(** [if_ b t e] uses hasher [t] when [b] is true, [e] otherwise. *)
|
||||
|
||||
val combine2 : int64 -> int64 -> int64
|
||||
val combine3 : int64 -> int64 -> int64 -> int64
|
||||
val combine4 : int64 -> int64 -> int64 -> int64 -> int64
|
||||
|
||||
val poly : 'a t
|
||||
(** Uses [Hashtbl.hash] internally. *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue