I'm trying to understand something that feels pretty basic but I cannot wrap my head around
we have a key value, character, string, whatever
, and we want to send it to our hashing function
, which gives some index.
My question is, is the result of the hashing function
the value
in a key-value pair ?
Or is the value separate from the result of the hashing function?
CodePudding user response:
A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes. The values are usually used to index a fixed-size table called a hash table.
CodePudding user response:
Does this help?
EDIT: Conforming to standard nomenclature...
typedef struct {
uint32_t hash;
char key[32 1]; // emphasize null terminate string
} kv_t;
kv_t node;
strncpy( node.key, "Hello World!", sizeof node.key );
node.key[ sizeof node.key - 1 ] = '\0';
node.hash = hash( node.key);
Note: there is both a "key value" and a "hash value"... Referring to "value" is ambiguous and should be avoided.
In a perfect world, the hashing function would generate a unique hash value for each of the 'keys'. The world isn't perfect.
The above struct allows one to create and insert "key-value" pairs in a contiguous (growing) array of pairs that can later be "binary searched". Sparsely filled arrays may provide faster access, but be costly in terms of memory (unused 'slots'). Everything is a trade-off.