This module implements efficient computations of hash values for diverse Nim types. All the procs are based on these two building blocks: the !& proc used to start or mix a hash value, and the !$ proc used to finish the hash value. 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): THash = ## Computes a THash from `x`. var h: THash = 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): THash = ## Computes a THash from `x`. var h: THash = 0 h = h !& hash(x.foo) h = h !& hash(x.bar) result = !$h
Types
THash = int
- a hash value; hash tables using these values should always have a size of a power of two and can use the and operator instead of mod for truncation of the hash value. Source
Procs
proc `!&`(h: THash; val: int): THash {.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
proc `!$`(h: THash): THash {.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
proc hashData(data: pointer; size: int): THash {.raises: [], tags: [].}
- hashes an array of bytes of size size Source
proc hash(x: pointer): THash {.inline, raises: [], tags: [].}
- efficient hashing of pointers Source
proc hash[T](x: T): THash {.inline.}
- efficient hashing of proc vars; closures are supported too. Source
proc hash(x: int): THash {.inline, raises: [], tags: [].}
- efficient hashing of integers Source
proc hash(x: int64): THash {.inline, raises: [], tags: [].}
- efficient hashing of integers Source
proc hash(x: char): THash {.inline, raises: [], tags: [].}
- efficient hashing of characters Source
proc hash(x: string): THash {.raises: [], tags: [].}
- efficient hashing of strings Source
proc hashIgnoreStyle(x: string): THash {.raises: [], tags: [].}
- efficient hashing of strings; style is ignored Source
proc hashIgnoreCase(x: string): THash {.raises: [], tags: [].}
- efficient hashing of strings; case is ignored Source
proc hash[T](x: T): THash
- efficient hashing of tuples. Source
proc hash(x: float): THash {.inline, raises: [], tags: [].}
- Source
proc hash[A](x: openArray[A]): THash
- Source
proc hash[A](x: set[A]): THash
- Source