mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
feat(hash): add bytes
This commit is contained in:
parent
795ae5c546
commit
13429e5e88
2 changed files with 5 additions and 3 deletions
|
|
@ -86,14 +86,15 @@ let int32 (x:int32) = Hashtbl.hash x (* TODO: FNV *)
|
||||||
let int64 (x:int64) = Hashtbl.hash x (* TODO: FNV *)
|
let int64 (x:int64) = Hashtbl.hash x (* TODO: FNV *)
|
||||||
let nativeint (x:nativeint) = Hashtbl.hash x
|
let nativeint (x:nativeint) = Hashtbl.hash x
|
||||||
|
|
||||||
let string (x:string) =
|
let bytes (x:bytes) =
|
||||||
let h = ref fnv_offset_basis in
|
let h = ref fnv_offset_basis in
|
||||||
for i = 0 to String.length x - 1 do
|
for i = 0 to Bytes.length x - 1 do
|
||||||
h := Int64.(mul !h fnv_prime);
|
h := Int64.(mul !h fnv_prime);
|
||||||
let byte = Char.code (String.unsafe_get x i) in
|
let byte = Char.code (Bytes.unsafe_get x i) in
|
||||||
h := Int64.(logxor !h (of_int byte));
|
h := Int64.(logxor !h (of_int byte));
|
||||||
done;
|
done;
|
||||||
Int64.to_int !h land max_int
|
Int64.to_int !h land max_int
|
||||||
|
let string (x:string) = bytes (Bytes.unsafe_of_string x)
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
int 42 >= 0
|
int 42 >= 0
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ val slice : string -> int -> int t
|
||||||
(** [slice s i len state] hashes the slice [i, …, i+len-1] of [s]
|
(** [slice s i len state] hashes the slice [i, …, i+len-1] of [s]
|
||||||
into [state]. *)
|
into [state]. *)
|
||||||
|
|
||||||
|
val bytes : bytes t
|
||||||
val string : string t
|
val string : string t
|
||||||
|
|
||||||
val list : 'a t -> 'a list t
|
val list : 'a t -> 'a list t
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue