hash table implementation 哈希列表的代码实现

My implementation of Hash Table 


           In computing, a hash table (also hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.

Ideally, the hash function will assign each key to a unique bucket, but this situation is rarely achievable in practice (usually some keys will hash to the same bucket). Instead, most hash table designs assume that hash collisions—different keys that are assigned by the hash function to the same bucket—will occur and must be accommodated in some way.

In a well-dimensioned hash table, the average cost (number of instructions) for each lookup is independent of the number of elements stored in the table. Many hash table designs also allow arbitrary insertions and deletions of key-value pairs, at (amortized[2]) constant average cost per operation.[3][4]

In many situations, hash tables turn out to be more efficient than search trees or any other table lookup structure. For this reason, they are widely used in many kinds of computer software, particularly for associative arrays, database indexingcaches, and sets.

                                                    

C语言实现:

hash_table.h

#ifndef _HASH_TABLE_H
#define _HASH_TABLE_H 1

	#define NODE_NAME_SIZE 20
	struct node
	{
		int used;
		int index;
		char name[NODE_NAME_SIZE];//store the string what we interesting
	};

	#define ARRAY_SIZE 10
	#define TABLE_SIZE 40

	#define OPEN_SUCCESS   0
	#define OPEN_FAILED    1
	#define FREAD_FAILED   2
	#define FREAD_SUCCESS  3
	#define FWRITE_FAILED  4
	#define FWRITE_SUCCESS 5

	#define USED   1
	#define UNUSED 0

	#include "hash_function.h"
	#include "hash_insert_data.h"
	#include "init_table.h"
	#include "print_hash_table.h"

#endif
hash_table.c

/************************************************************
code writer: EOF
code date: 2014.03.06
e-mai: jasonleaster@gmail.com
code purpose:
	Just a test source file for hash table. I would be
happy, if my code will help you to know what is hash table.

If something wrong with my code, please touche me by e-mail

************************************************************/

#include <stdio.h>
#include "hash_table.h"
 
int main()
{
	int temp  = 0;
	int foo   = 0;
	int index = 0;

	char* people[ARRAY_SIZE] = {"jason",
			 	   "liuzijian",
			 	   "yunyun",
			 	   "foo",
			 	   "hello",
				   "world",
			           "dedo",
				   "an",
			 	   "god",
			 	   "tomato"};
	
	init_table("test.txt",TABLE_SIZE);
	
	for(temp = 0;temp < ARRAY_SIZE; temp++)
	{
		index = hash_function(people[temp]);

		foo  = hash_insert_data("test.txt",people[temp],index);
		if(foo != OPEN_SUCCESS)
		{
			printf("something wrong!\n");
			return 0;
		}
	}

	print_hash_table("test.txt",TABLE_SIZE);		

	return 0;
}



init_table.c

/************************************************************
code writer: EOF
code date: 2014.03.06
e-mai: jasonleaster@gmail.com
code purpose:
	Just a test source file for hash table. I would be
happy, if my code will help you to know what is hash table.

If something wrong with my code, please touch me by e-mail

************************************************************/

#include <stdio.h>
#include "hash_table.h"

int init_table(char* filename,int table_size)
{
	struct node temp_data;
	int temp = 0;

	FILE* p_file;
	
	p_file = fopen(filename,"a");
	//For special attention, access premission "a" is very improtant. I kill 5 hours with this fucking bug!!
	
	if(p_file == NULL)
	{
		printf("fopen error\n");
		return 0;
	}
	else
	{
		temp_data.used = UNUSED;
		temp_data.index = 0;
	}
	
	fseek(p_file,0,SEEK_SET);
	
	for(temp = 0;temp < TABLE_SIZE; temp++)
	{
	 	fseek(p_file,(temp)*sizeof(struct node),SEEK_SET);

		if(fwrite(&temp_data,sizeof(struct node),1,p_file) < 0)
		{
			printf("fwrite process\n");
			fclose(p_file);
			return 0;
		}
	}	

	fclose(p_file);

	return 0;
}
hash_function.c

/************************************************************
code writer: EOF
code date: 2014.03.06
e-mai: jasonleaster@gmail.com
code purpose:
	Just a implementation for hash_function.I would be
happy, if my code will help you to know what is hash table.

If something wrong with my code, please touche me by e-mail
************************************************************/
#include "hash_table.h"

int hash_function(char * string)
{
	int temp = 0;
	int key = 0;
	
	for(temp = 0; string[temp] != '\0';temp++)
	{
		key += string[temp];
	}
	
	key %= TABLE_SIZE;
	
	return key;//index value
}

print_hash_table.c

/************************************************************
code writer: EOF
code date: 2014.03.06
e-mai: jasonleaster@gmail.com
code purpose:
	Just a implementation for print_hash_table function .
I would be happy, if my code will help you to know 
what is hash table.

If something wrong with my code, please touche me by e-mail

***********************************************************/
#include "stdio.h"
#include "hash_table.h"

int print_hash_table(char* filename,int table_size)
{
	FILE* p_file = NULL;
	
	int temp = 0;
	struct node temp_node;

	p_file = fopen(filename,"r");
	
	if(p_file == NULL)
	{
		printf("fopen failed\n");
		return OPEN_FAILED;
	}

	for(temp = 0;temp < TABLE_SIZE; temp++)
	{
		fseek(p_file,temp*sizeof(struct node),SEEK_SET);

		if(fread(&temp_node,sizeof(struct node),1,p_file) <= 0)
		{
			printf("fread wrong!\n");
		}	

		if(temp_node.used == USED)
		{
			printf("index: %d name:%s\n",temp_node.index,temp_node.name);
		}
	}

	fclose(p_file);

	return OPEN_SUCCESS;
}


EOF

2014.03.07

于XTU 晚


2015.03.15

发现一个超级赞的Hash table的实现.

号称15行一个hash表


#include <stdlib.h>
#define SIZE 1024
static int (**hnew())[2] {
    return calloc(sizeof(int**), SIZE);
}
static void hdel(int (**e)[2]) {
    for (int i = 0; i < SIZE; i++) free(e[i]); free(e);
}
static int (**hget(int (**t)[2], int k))[2] {
    for (int h = k & (SIZE - 1); **t && ***t != k; h = ((h + 1) & (SIZE - 1)), t += h);
    return t;
}
static void hset(int (**t)[2], int k, int v) {
    for (int (**a)[2] = hget(t, k); !*a && (*a=malloc(sizeof(**t))); (**a)[0]=k,(**a)[1]=v);
}

// TEST DRIVER
#include <stdio.h>
int main() {
    int (**table)[2] = hnew();

    hset(table, 10, 20);
    hset(table, 20, 30);
    hset(table, 30, 40);

    int (**a)[2] = hget(table, 10);
    int (**b)[2] = hget(table, 20);
    int (**c)[2] = hget(table, 30);

    printf("%d:%d\n", (**a)[0], (**a)[1]);
    printf("%d:%d\n", (**b)[0], (**b)[1]);
    printf("%d:%d\n", (**c)[0], (**c)[1]);

    hdel(table);
    return 0;
}











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值