This module implements efficient computations of hash values for diverse Nim types. All the procs are based on these two building blocks:
If you want to implement hash procs for your custom types you will end up writing the following kind of skeleton of code:
proc hash(x: Something): Hash = ## Computes a Hash from `x`. var h: Hash = 0 # Iterate over parts of `x`. for xAtom in x: # Mix the atom with the partial hash. h = h !& xAtom # Finish the hash. result = !$h
If your custom types contain fields for which there already is a hash proc, like for example objects made up of strings, you can simply hash together the hash value of the individual fields:
proc hash(x: Something): Hash = ## Computes a Hash from `x`. var h: Hash = 0 h = h !& hash(x.foo) h = h !& hash(x.bar) result = !$h
Procs
proc `!&`(h: Hash; val: int): Hash {.
inline, raises: [], tags: [].}- mixes a hash value h with val to produce a new hash value. This is only needed if you need to implement a hash proc for a new datatype. Source Edit
proc `!$`(h: Hash): Hash {.
inline, raises: [], tags: [].}- finishes the computation of the hash value. This is only needed if you need to implement a hash proc for a new datatype. Source Edit
proc hashData(data: pointer; size: int): Hash {.
raises: [], tags: [].}- hashes an array of bytes of size size Source Edit
proc hash(x: pointer): Hash {.
inline, raises: [], tags: [].}- efficient hashing of pointers Source Edit
proc hash[T: proc](x: T): Hash {.
inline.}- efficient hashing of proc vars; closures are supported too. Source Edit
proc hash(x: int): Hash {.
inline, raises: [], tags: [].}- efficient hashing of integers Source Edit
proc hash(x: int64): Hash {.
inline, raises: [], tags: [].}- efficient hashing of int64 integers Source Edit
proc hash(x: char): Hash {.
inline, raises: [], tags: [].}- efficient hashing of characters Source Edit
proc hash[T: Ordinal](x: T): Hash {.
inline.}- efficient hashing of other ordinal types (e.g., enums) Source Edit
proc hash(x: string): Hash {.
raises: [], tags: [].}- efficient hashing of strings Source Edit
proc hash(sBuf: string; sPos, ePos: int): Hash {.
raises: [], tags: [].}-
efficient hashing of a string buffer, from starting position sPos to ending position ePos
hash(myStr, 0, myStr.high) is equivalent to hash(myStr)
Source Edit proc hashIgnoreStyle(x: string): Hash {.
raises: [], tags: [].}- efficient hashing of strings; style is ignored Source Edit
proc hashIgnoreStyle(sBuf: string; sPos, ePos: int): Hash {.
raises: [], tags: [].}-
efficient hashing of a string buffer, from starting position sPos to ending position ePos; style is ignored
hashIgnoreStyle(myBuf, 0, myBuf.high) is equivalent to hashIgnoreStyle(myBuf)
Source Edit proc hashIgnoreCase(x: string): Hash {.
raises: [], tags: [].}- efficient hashing of strings; case is ignored Source Edit
proc hashIgnoreCase(sBuf: string; sPos, ePos: int): Hash {.
raises: [], tags: [].}-
efficient hashing of a string buffer, from starting position sPos to ending position ePos; case is ignored
hashIgnoreCase(myBuf, 0, myBuf.high) is equivalent to hashIgnoreCase(myBuf)
Source Edit proc hash(x: float): Hash {.
inline, raises: [], tags: [].}- efficient hashing of floats. Source Edit
proc hash[T: tuple](x: T): Hash
- efficient hashing of tuples. Source Edit
proc hash[A](x: openArray[A]): Hash
- efficient hashing of arrays and sequences. Source Edit
proc hash[A](aBuf: openArray[A]; sPos, ePos: int): Hash
-
efficient hashing of portions of arrays and sequences.
hash(myBuf, 0, myBuf.high) is equivalent to hash(myBuf)
Source Edit proc hash[A](x: set[A]): Hash
- efficient hashing of sets. Source Edit