Difference between hashed value, and key-value?

Viewed 49

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?

2 Answers

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.

https://en.wikipedia.org/wiki/Hash_function

So, you provide an input, which is presumably the key and get the result of the hashing function, which is the hash.

  • I used the word presumably above, because it is up to you what your algorithm considers to be a key and what it considers to be a value, but, in general, the result of a hashing function is a value that can be used to index a hash table.

Here's a picture taken from wikipedia

enter image description here

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.

Related