cchash64: add apply and combine*

This commit is contained in:
Simon Cruanes 2026-03-19 21:05:23 -04:00
parent e404dd26ab
commit 76d8ee79e1
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 26 additions and 0 deletions

View file

@ -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;

View file

@ -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. *)