C language implementation of hashtable sharing

  • 2020-04-02 02:08:18
  • OfStack

The header file hashtable. H


typedef struct _Bucket
{
    char *key;
    void *value;
    struct _Bucket *next;
} Bucket;

typedef struct _HashTable
{
    int size;
    int total;
    struct _Bucket *buckets;
} HashTable;

int hash_init(HashTable **ht);
int hash_find(HashTable *ht, char *key, void **result);
int hash_insert(HashTable *ht, char *key, void *value);
int hash_remove(HashTable *ht, char *key);
int hash_loop(HashTable *ht, void **result);
//int hash_index(HashTable *ht, char *key);
static unsigned int ELFHash(char *str, unsigned int length);

Hashtable. C


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hashtable.h"
#include "mempool.h"
#include "log.h"

#define SUCCESS 1
#define FAILED 0
#define HASH_LEN 5

int hash_init(HashTable **ht) {
    (*ht) = (HashTable *)malloc(sizeof(HashTable));
    if (NULL == ht) {
        write_log("HashTable init error");
        exit(1);
    }
    (*ht)->size = 0;
    (*ht)->total = HASH_LEN;
    Bucket *bucket = (Bucket *)malloc(sizeof(Bucket) * HASH_LEN);
    memset(bucket, 0, sizeof(sizeof(Bucket) * HASH_LEN));
    (*ht)->buckets = bucket;
    return SUCCESS;
}

int hash_insert(HashTable *ht, char *key, void *value) {
    if (ht->size >= ht->total) {
        ht->buckets = (Bucket *)realloc(ht->buckets, sizeof(Bucket) * (ht->size + HASH_LEN));
        ht->total = ht->size + HASH_LEN;
    }
    int index = hash_index(ht, key);
    Bucket *bucket = &ht->buckets[index];
    int _tmpindex;
    char _tmpindexstr[20];
    while (NULL != bucket->value) {

        while (NULL != bucket->next) {
            if (strcmp(key, bucket->key) == 0) {
                memset(bucket->value, 0, sizeof(bucket->value));
                memcpy(bucket->value, value, sizeof(value));
                return SUCCESS;
            }
            bucket = bucket->next;
        }

        do {
            _tmpindex = abs(rand() - index);
            sprintf(_tmpindexstr, "%d", _tmpindex);
            _tmpindex = hash_index(ht, _tmpindexstr);
        } while (_tmpindex == index || ht->buckets[_tmpindex].value != NULL);

        index = _tmpindex;
        bucket->next = &ht->buckets[index];
        bucket = bucket->next;
    }

    bucket->key = (char *)malloc(sizeof(key));
    bucket->value = (void *)malloc(sizeof(value));
    memcpy(bucket->key, key, sizeof(key));
    memcpy(bucket->value, value, sizeof(value));
    bucket->next = NULL;
    ht->size ++;

    return SUCCESS;
}

int hash_find(HashTable *ht, char *key, void **result) {
    int index = hash_index(ht, key);
    Bucket *bucket = &ht->buckets[index];
    if (NULL == bucket->value) {
        return FAILED;
    }

    while (strcmp(key, bucket->key)) {
        if (NULL != bucket->next) {
            bucket = bucket->next;
        } else {
            break;
        }
    }
    if (NULL == bucket->value || strcmp(key, bucket->key)) {
        return FAILED;
    }

    *result = bucket->value;
    return SUCCESS;

}

int hash_delete(HashTable *ht, char *key) {
    int index = hash_index(ht, key);
    Bucket *bucket = &ht->buckets[index];
    if (NULL == bucket->value) {
        return FAILED;
    }

    while (strcmp(key, bucket->key)) {
        if (NULL != bucket->next) {
            bucket = bucket->next;
        } else {
            break;
        }
    }

    if (NULL == bucket->value || strcmp(key, bucket->key)) {
        return FAILED;
    }

    memset(bucket, 0, sizeof(Bucket));
    ht->size --;
    return SUCCESS;
}

void hash_status(HashTable *ht) {
    printf("Total Size:tt%dn", ht->total);
    printf("Current Size:tt%dn", ht->size);
}

int hash_index(HashTable *ht, char *key) {
    return ELFHash(key, ht->total);
}

// ELF Hash Function
static unsigned int ELFHash(char *str, unsigned int length){
    unsigned int hash = 0;
    unsigned int x = 0;

    while (*str)
    {
        hash = (hash << 4) + (*str++);//Hash moved 4 bits to the left to store the current character ASCII into the hash lower four digits.
        if ((x = hash & 0xF0000000L) != 0)
        {
            //If the highest four digits are not 0, that means that there are more than 7 characters, and the eighth character is being saved. If it is not processed, when the next character is added, the first character will be moved out.
            //This processing, if applied to a string (a-z or a-z) will only affect 5-8 bits, otherwise it will affect 5-31 bits, because of the arithmetic shift used by C
            //Because 1-4 bits have just been stored to add new characters, it cannot>> 28
            hash ^= (x >> 24);
            //The above line of code does not affect X, X itself is the same as the hash with 4 bits high, and the following line of code &~ is zero for 28-31(4 bits high) bits.
            hash &= ~x;
        }
    }
    //Returns a number with a sign bit of zero, that is, the highest bit is discarded to avoid any impact outside the function. (we can consider that if there are only characters, the symbol bit cannot be negative.)
    return (hash & 0x7FFFFFFF) % length;
}

The key mapping USES the ELFHash algorithm


Related articles: