mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
perf(cchash): improve a bit commutative hashing of arrays/lists
This commit is contained in:
parent
40133ee511
commit
e63383174e
1 changed files with 13 additions and 4 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue