perf(cchash): improve a bit commutative hashing of arrays/lists

This commit is contained in:
Simon Cruanes 2022-06-07 16:20:26 -04:00
parent 40133ee511
commit e63383174e
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -134,14 +134,23 @@ let if_ b then_ else_ h =
let poly x = Hashtbl.hash x let poly x = Hashtbl.hash x
let array_of_hashes_ arr =
Array.sort CCInt.compare arr; (* sort the hashes, so their order does not matter *)
Array.fold_left combine2 0x42 arr
let array_comm f a = let array_comm f a =
let arr = Array.init (Array.length a) (fun i -> f a.(i)) in let arr = Array.init (Array.length a) (fun i -> f a.(i)) in
Array.sort CCInt.compare arr; (* sort the hashes, so their order does not matter *) array_of_hashes_ arr
array (fun h->h) arr
let list_comm f l = let list_comm f l =
let a = Array.of_list l in let arr = Array.make (List.length l) 0 in
array_comm f a List.iteri (fun i x -> arr.(i) <- f x) l;
array_of_hashes_ arr
(*$T
list_comm int [1;2] = list_comm int [2;1]
list_comm int [1;2] <> list_comm int [2;3]
*)
let iter f seq = let iter f seq =
let h = ref 0x43 in let h = ref 0x43 in