From f1459b57dfef0c5e1f86a539ab7ab81b51ea2292 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 11 Feb 2017 13:37:32 +0100 Subject: [PATCH] add `CCHash.{list,array}_comm` --- src/core/CCHash.ml | 9 +++++++++ src/core/CCHash.mli | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/core/CCHash.ml b/src/core/CCHash.ml index 7ac85632..c4741cf4 100644 --- a/src/core/CCHash.ml +++ b/src/core/CCHash.ml @@ -56,6 +56,15 @@ let if_ b then_ else_ h = let poly x = Hashtbl.hash x +let array_comm f a = + 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 (fun h->h) arr + +let list_comm f l = + let a = Array.of_list l in + array_comm f a + let seq f seq = let h = ref 0x43 in seq (fun x -> h := combine f !h x); diff --git a/src/core/CCHash.mli b/src/core/CCHash.mli index f7328b0a..34e22760 100644 --- a/src/core/CCHash.mli +++ b/src/core/CCHash.mli @@ -39,6 +39,16 @@ val if_ : bool -> 'a t -> 'a t -> 'a t val poly : 'a t (** the regular polymorphic hash function *) +val list_comm : 'a t -> 'a list t +(** Commutative version of {!list}. Lists that are equal up to permutation + will have the same hash. + @since NEXT_RELEASE *) + +val array_comm : 'a t -> 'a array t +(** Commutative version of {!array}. Arrays that are equal up to permutation + will have the same hash. + @since NEXT_RELEASE *) + (** {2 Base hash combinators} *) val combine : 'a t -> hash -> 'a -> hash