Implementing a Simple Hastable in C Language (4)

Time:2019-1-15

Implementing a Simple Hastable in C Language (4)

We explained in the previous chapter.Hash tableMost Importanthash functionAnd with pseudo code and C language, we implemented our ownhash functionhash functionincollisionIt’s inevitable when it happens.collisionHow can we deal with it effectively? Let’s talk about this chapter.

Handling collisions

hash functionMapping infinite inputs to finite outputs occurs when different inputs are mapped to the same output.collision, each.Hash tableThey all use different methods to deal with it.collision

Our hash table will use a dual hashing technique called open address to handle conflicts. Double hashing uses two hash functions to calculate occurrencecollisionIndex of post-stored records.

double hashing

WheniHappencollisionThen we use the following method to obtain the index:

index = hash_a(string) + i * hash_b(string) % num_buckets

When it doesn’t happencollisionAt that time,i=0So the index ishash_aValue, occurrencecollisionLater,hash_aThe result needs to go through once.hash_bProcessing.

hash_bPossible return0Reduce item 2 to0This leads toHash tableInsert multiple records into the samebucketIn Chinese, we can be inhash_bAfter the result1To deal with this situation, make sure it never does0

index = (hash_a(string) + i * (hash_b(string) + 1)) % num_buckets

Algorithm implementation

// hash_table.c
static int ht_get_hash(const char* s, const int num_buckets, const int attempt) {
    const int hash_a = ht_hash(s, HT_PRIME_1, num_buckets);
    const int hash_b = ht_hash(s, HT_PRIME_2, num_buckets);
    return (hash_a + (attempt * (hash_b + 1))) % num_buckets;
}

Chapter 1: hash functions
Next Chapter: Completing the Hash Table API