How to hash 10 data

Asked 2 years ago, Updated 2 years ago, 90 views

Literally, I want to hash 10 integers

I don't know because I still lack the concept of hashing

I was going to use Jenkins' hash function to do a hash.

// Robert Jenkins' 32bit hash function
unsigned __int32 hash(unsigned __int32 key)
{
    key += (key<<12);
    key ^= (key>>22);
    key += (key<<4);
    key ^= (key>>9);
    key += (key<<10);
    key ^= (key>>2);
    key += (key<<7);
    key ^= (key>>12);

    return key;
}

But when I put the value of 3 into the hash function and printed it out, it came out as 948077404.

Then, I want to make the hash table into an array, so should I make it with the array size of 948077404?

I only need to hash about 10 pieces of data. What should I do?

c hash

2022-09-22 19:53

1 Answers

Let's talk a little bit about the hash.

The hash function converts the variable size original data into a fixed size hash value.

The hash value resulting from this transformation contains the characteristics of the original data.

The advantage of converting this variable size data into fixed size data is that you can speed up comparison operations. The hash value is typically smaller than the original data, allowing you to quickly compare whether the original data is the same or different.

Use to quickly compare the original data. Being able to compare quickly means that the source can quickly find the same data. With these characteristics, you can quickly create maps, dictionaries, hash tables, sets, and more, which are data structures that allow you to find values (specific data that matches the key) using the key.

Of course, the original data and hash values cannot be matched 1:1 because the amount of information is reduced. Other source data can also have the same hash value, which is called a hash conflict.

Moving on, it's almost impossible to create an array that can hold all the hash values, and it's not good because it's freeing up memory for data that you don't want to use.

Once you've used 10 pieces of data, you can create an array of 10 elements.

If you do it in more detail, the code will be longer, so if you express it conceptually and concisely, it is as follows.

struct Node {
    unsigned __int32 key;
    void* value;
} } table[10];
memset(table, 0, sizeof(table));

// Add hash and value to table
table[0].key = hash(1); // 1 is an arbitrary key
table[0].value = malloc(sizeof(int); // any value
// ...

You can create a hash table in this way to put the key and the value. Of course, we don't actually make it as simple as above. It also increases the speed of searches by aligning the key .

You can then quickly find the original data you want to find by comparing the key as shown below.

int i, key;
void* value;

key = hash(1); // hash of the original data to be found
value = NULL;
for (i = 0; i < (sizeof(table) / sizeof(*table)); ++i) {
    if (table[i].key == key) {
        value = table[i].value;
        break;
    }
}

if (value != NULL) {
    // Found a value corresponding to the key
}

In addition, a hash table that simply uses 10 integers is not faster than an array made with 10 integers.


2022-09-22 19:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.