HashingSourceCommon tools to write custom hash-functions for our various types: Use hash2, hash3, hash4 or hash5 to create the hash of a tuple, hash_list to create the hash of a list and hash_sum to create the hash of a sum type.
For example, one might write a hash function for the given type as follows
type t =
| A
| B of int
| C of char * int
| D of int list
| E of { x: int; mutable y : int }
(* mutable values should NOT be included in hash *)
let hash x =
let total = 5 in (* 5 total constructors *)
match x with
| A -> hash_sum ~total ~nb:0 0 (* hash for constant is 0 *)
| B i -> hash_sum ~total ~nb:1 i
| C(c,i) -> hash_sum ~total ~nb:2 @@ hash2 (int_of_char c) i
| D l -> hash_sum ~total ~nb:3 @@ hash_list Fun.id l
| E {x;_} -> hash_sum ~total ~nb:4 xFast hash function (for recursive subcalls), lots of collisions, only examines constructor tag
hash_sum ~nb ~total value is used to generate a unique hash for sum types.
hash of a pair
hash of a triplet
hash of a quadruplet
hash of a quintuplet
hash a list, using the given function to hash its elements