Portability | portable |
---|---|
Stability | provisional |
Maintainer | johan.tibell@gmail.com |
This module defines a class, Hashable
, for types that can be
converted to a hash value. This class exists for the benefit of
hashing-based data structures. The module provides instances for
basic types and a way to combine hash values.
The hash
function should be as collision-free as possible, which
means that the hash
function must map the inputs to the hash
values as evenly as possible.
Computing hash values
The class of types that can be converted to a hash value.
Minimal implementation: hash
or hashWithSalt
.
Return a hash value for the argument.
The general contract of hash
is:
- This integer need not remain consistent from one execution of an application to another execution of the same application.
- If two values are equal according to the
==
method, then applying thehash
method on each of the two values must produce the same integer result. - It is not required that if two values are unequal
according to the
==
method, then applying thehash
method on each of the two values must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal values may improve the performance of hashing-based data structures.
hashWithSalt :: Int -> a -> IntSource
Return a hash value for the argument, using the given salt.
This method can be used to compute different hash values for the same input by providing a different salt in each application of the method.
The contract for hashWithSalt
is the same as for hash
, with
the additional requirement that any instance that defines
hashWithSalt
must make use of the salt in its implementation.
Creating new instances
The functions below can be used when creating new instances of
Hashable
. For example, the hashWithSalt
method for many string-like
types can be defined in terms of either hashPtrWithSalt
or
hashByteArrayWithSalt
. Here's how you could implement an instance for
the ByteString
data type, from the bytestring
package:
import qualified Data.ByteString as B import qualified Data.ByteString.Internal as B import qualified Data.ByteString.Unsafe as B import Data.Hashable import Foreign.Ptr (castPtr) instance Hashable B.ByteString where hashWithSalt salt bs = B.inlinePerformIO $ B.unsafeUseAsCStringLen bs $ \(p, len) -> hashPtrWithSalt p (fromIntegral len) salt
The combine
function can be used to implement Hashable
instances for data types with more than one field, using this
recipe:
instance (Hashable a, Hashable b) => Hashable (Foo a b) where hashWithSalt s (Foo a b) = s `hashWithSalt` a `hashWithSalt` b
A nonzero seed is used so the hash value will be affected by initial fields whose hash value is zero. If no seed was provided, the overall hash value would be unaffected by any such initial fields, which could increase collisions. The value 17 is arbitrary.
Compute a hash value for the content of this pointer.
Compute a hash value for the content of this pointer, using an initial salt.
This function can for example be used to hash non-contiguous segments of memory as if they were one contiguous segment, by using the output of one hash as the salt for the next.
:: ByteArray# | data to hash |
-> Int | offset, in bytes |
-> Int | length, in bytes |
-> Int | hash value |
Compute a hash value for the content of this ByteArray#
,
beginning at the specified offset, using specified number of bytes.
Availability: GHC.
:: ByteArray# | data to hash |
-> Int | offset, in bytes |
-> Int | length, in bytes |
-> Int | salt |
-> Int | hash value |
Compute a hash value for the content of this ByteArray#
, using
an initial salt.
This function can for example be used to hash non-contiguous segments of memory as if they were one contiguous segment, by using the output of one hash as the salt for the next.
Availability: GHC.