Categories &

Functions List

Function Reference: keyHash

datatypes: key = keyHash (X)
datatypes: key = keyHash (X, base)

Generate a hash code for an array.

key = keyHash (X) generates a uint64 scalar that represents the input X, which may be a numeric, logical, character, cell, struct or function handle array. keyHash utilizes the 64-bit FNV-1a variant of the Fowler-Noll-Vo non-cryptographic hash function.

A cell array is keyed on its elements, each hashed in turn, so a cell may hold any type that keyHash accepts, nested to any depth. A struct is keyed on its field names and their values; the order in which its fields were created is not part of the key, so two structs differing only in field order are the same key. A function handle is keyed on its text alone: two named handles for the same function are one key, while distinct anonymous handles never match even when written identically, so nothing a handle captures can be observed through keyMatch.

key = keyHash (X, base) also generates a 64-bit hash code using base as the offset basis for the FNV-1a hash algorithm. base must be a uint64 integer type scalar. Use this syntax to cascade keyHash on multiple objects for which a single hash code is required.

keyMatch decides key identity and keyHash agrees with it: two values that keyMatch reports as the same key always have the same hash code. The converse does not hold, since distinct keys are permitted to share a hash code. Values that differ only in a representation that keyMatch ignores therefore hash alike, so -0 hashes as 0, and every NaN hashes alike whatever produced it.

Elements of a cell array of character vectors are hashed together with their lengths, so that the boundaries between them are part of the key and {'ab', 'c'} does not hash as {'a', 'bc'}.

Note that unlike MATLAB, this implementation does not use any random seed. As a result, keyHash will always generate the exact same hash key for any particular input across different workers and Octave sessions. Hash codes are not stable across package versions, however: those produced before version 1.3.1 are invalid and must be recomputed.

Source Code: keyHash