c_cpp C中的哈希映射实现,带有djb2哈希函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C中的哈希映射实现,带有djb2哈希函数相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Implementation for hash table

#define Map_SIZED(n) (sizeof(struct Map) + (sizeof(struct MapSlot) * n))



unsigned long
hash(void *object)
{
	unsigned char* strRep = object;
    unsigned long hash = 5381;
    int c;
 
    while ((c = *strRep++))
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
 
    return hash;
}



struct MapSlot
{
	unsigned long key;
	void* item;
	struct MapSlot* next;
	int info; // for type
};


struct Map
{
	unsigned size;
	struct MapSlot slots[0];
};

struct Map* Map_new(unsigned size)
{
	struct Map* newmap = malloc(Map_SIZED(size));
	memset(newmap, 0, Map_SIZED(size));
	newmap->size = size;
	return newmap;
}

struct MapSlot* MapSlot_new(unsigned long hashKey, void* value, int info)
{
	struct MapSlot* newslot = malloc(sizeof(struct MapSlot));
	newslot->key = hashKey;
	newslot->item = value;
	newslot->info = info;
	return newslot;
}

// Inserts into hash map
// returns 0 if the key is already present (doesn't allow overwrite)
int Map_insert(struct Map* table, const char* key, void* value, int info)
{
	unsigned long hashed = hash((void*)key);
	struct MapSlot* index = table->slots + (hashed % table->size);
	if(!(index->key))
	{
		index->key = hashed;
		index->item = value;
		index->info = info;
		return 1;
	}
	else if (index->key == hashed)
	{
		return 0;
	}
	else
	{
		while(index->next != NULL) index = index->next;
		index->next = MapSlot_new(hashed, value, info);
		return 1;

	}
}

struct MapSlot* Map_get(struct Map* table, const char* key)
{
	unsigned long hashed = hash((void*)key);
	struct MapSlot* index = table->slots + (hashed % table->size);
	if(!(index->key)) return NULL;
	else if(index->key == hashed) return index;
	else
	{
		while(index != NULL && index->key != hashed)
		{
			index = index->next;
		}
		return index;
	}
}


int main(int argc, char const *argv[])
{
	const char* sample = "Hello World!";
	const char* sample2 = "Hello Wolf!";
	struct Map* dict = Map_new(1000);
	Map_insert(dict, "Foo", (void*)sample, 1);
	Map_insert(dict, "Doo", (void*)sample2, 1);
	const char* got = (const char*)(Map_get(dict, "Doo")->item);
	printf("%s\n", got);
	return 0;
}

以上是关于c_cpp C中的哈希映射实现,带有djb2哈希函数的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 在C中进行djb2哈希测试,使控制台能够测试哈希值

c_cpp 使用exists方法实现哈希映射

c_cpp c中的快速哈希表实现。

Dan Bernstein 的 Djb2 哈希函数:当我们只能乘以 33 时,为啥还要使用按位运算符?

djb2:一个产生简单的随机分布的哈希函数

c_cpp 为字符串实现哈希表和哈希函数