nicer API with and without seed

This commit is contained in:
Simon Cruanes 2026-03-14 04:04:32 +00:00
parent a3c061a388
commit 9865a26c91
2 changed files with 84 additions and 39 deletions

View file

@ -1,3 +1,4 @@
module Raw = struct
external hash_string : string -> (int64[@unboxed]) -> (int64[@unboxed])
= "caml_cc_xxhash_string_byte" "caml_cc_xxhash_string"
[@@noalloc]
@ -15,6 +16,17 @@ external hash_int32 :
external hash_int : (int[@untagged]) -> (int64[@unboxed]) -> (int64[@unboxed])
= "caml_cc_xxhash_int_byte" "caml_cc_xxhash_int"
[@@noalloc]
end
let[@inline] hash_bool b seed = hash_int (Bool.to_int b) seed
let[@inline] hash_char c seed = hash_int (Char.code c) seed
let[@inline] hash_string s = Raw.hash_string s 0L
let[@inline] hash_string_seed s seed = Raw.hash_string s seed
let[@inline] hash_int64 v = Raw.hash_int64 v 0L
let[@inline] hash_int64_seed v seed = Raw.hash_int64 v seed
let[@inline] hash_int32 v = Raw.hash_int32 v 0L
let[@inline] hash_int32_seed v seed = Raw.hash_int32 v seed
let[@inline] hash_int v = Raw.hash_int v 0L
let[@inline] hash_int_seed v seed = Raw.hash_int v seed
let[@inline] hash_bool b = Raw.hash_int (Bool.to_int b) 0L
let[@inline] hash_bool_seed b seed = Raw.hash_int (Bool.to_int b) seed
let[@inline] hash_char c = Raw.hash_int (Char.code c) 0L
let[@inline] hash_char_seed c seed = Raw.hash_int (Char.code c) seed

View file

@ -6,6 +6,8 @@
All functions use XXH64 and are noalloc in native code.
*)
(** Raw bindings with explicit seed argument. *)
module Raw : sig
external hash_string : string -> (int64[@unboxed]) -> (int64[@unboxed])
= "caml_cc_xxhash_string_byte" "caml_cc_xxhash_string"
[@@noalloc]
@ -26,11 +28,42 @@ external hash_int32 :
external hash_int : (int[@untagged]) -> (int64[@unboxed]) -> (int64[@unboxed])
= "caml_cc_xxhash_int_byte" "caml_cc_xxhash_int"
[@@noalloc]
(** [hash_int v seed] hashes [v] as a 64-bit integer with [seed].
Noalloc and untagged in native code. *)
(** [hash_int v seed] hashes [v] as a 64-bit integer with [seed]. Noalloc
and untagged in native code. *)
end
val hash_bool : bool -> int64 -> int64
(** [hash_bool b seed] hashes [b] as an integer (0 or 1) with [seed]. *)
val hash_string : string -> int64
(** [hash_string s] hashes [s] using XXH64 with seed [0L]. *)
val hash_char : char -> int64 -> int64
(** [hash_char c seed] hashes [c] as its character code with [seed]. *)
val hash_string_seed : string -> int64 -> int64
(** [hash_string_seed s seed] hashes [s] with an explicit seed. *)
val hash_int64 : int64 -> int64
(** [hash_int64 v] hashes the 8-byte representation of [v] with seed [0L]. *)
val hash_int64_seed : int64 -> int64 -> int64
(** [hash_int64_seed v seed] hashes [v] with an explicit seed. *)
val hash_int32 : int32 -> int64
(** [hash_int32 v] hashes the 4-byte representation of [v] with seed [0L]. *)
val hash_int32_seed : int32 -> int64 -> int64
(** [hash_int32_seed v seed] hashes [v] with an explicit seed. *)
val hash_int : int -> int64
(** [hash_int v] hashes [v] as a 64-bit integer with seed [0L]. *)
val hash_int_seed : int -> int64 -> int64
(** [hash_int_seed v seed] hashes [v] with an explicit seed. *)
val hash_bool : bool -> int64
(** [hash_bool b] hashes [b] as an integer (0 or 1) with seed [0L]. *)
val hash_bool_seed : bool -> int64 -> int64
(** [hash_bool_seed b seed] hashes [b] with an explicit seed. *)
val hash_char : char -> int64
(** [hash_char c] hashes [c] as its character code with seed [0L]. *)
val hash_char_seed : char -> int64 -> int64
(** [hash_char_seed c seed] hashes [c] with an explicit seed. *)