From 76d8ee79e15cfffe165fa9a07774f71840097542 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 19 Mar 2026 21:05:23 -0400 Subject: [PATCH] cchash64: add apply and combine* --- src/core/CCHash64.ml | 16 ++++++++++++++++ src/core/CCHash64.mli | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/core/CCHash64.ml b/src/core/CCHash64.ml index 79838e31..4cabb288 100644 --- a/src/core/CCHash64.ml +++ b/src/core/CCHash64.ml @@ -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; diff --git a/src/core/CCHash64.mli b/src/core/CCHash64.mli index 6c31e192..2909e824 100644 --- a/src/core/CCHash64.mli +++ b/src/core/CCHash64.mli @@ -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. *)