Hashtable sharing _c language implemented by C language

Source: Internet
Author: User

Header file Hashtable.h

Copy Code code as follows:

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

Copy Code code as follows:

#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:\t\t%d\n", ht->total);
printf ("Current size:\t\t%d\n", 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 Left 4 digits, the current character ASCII into the hash low four bits.
if ((x = hash & 0xf0000000l)!= 0)
{
If the highest four digits are not 0, then the character is more than 7, now the 8th character is stored, if not processed, and then the next character, the first character will be removed, so the following processing.
The processing, if the string (A-Z or a-Z) will affect only 5-8 bits, otherwise affects 5-31 bit, because the C language uses the arithmetic shift
Because 1-4 bits just stored the new add to the character, so can't >>28
Hash ^= (x >> 24);
The above line of code does not affect X, itself x and hash of the high 4-bit the same, the following line of code &~ that is 28-31 (high 4 bit) bit zero.
Hash &= ~x;
}
}
Returns a number with a sign bit of 0, that is, discarding the highest bit so as not to have an effect outside the function. (We can consider that if there are only characters, the sign bit cannot be negative)
Return (hash & 0x7fffffff)% length;
}

Where key is mapped using the Elfhash algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.