现在的位置: 首页 > 综合 > 正文

哈希表的实现

2013年09月09日 ⁄ 综合 ⁄ 共 2501字 ⁄ 字号 评论关闭

hash.h

#ifndef HASH_H
#define HASH_H				1

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <list.h>

#define NMN_USER_HASH_LIST_MAX		1024

struct user {
	struct hlist_node 	sibling;
	char			username[32];
/*
 *	char			userpwd[NMN_VALUE_MAX_LEN];
 */
	time_t			starttime;
	time_t			endtime;
};

extern int user_count;
extern int hash_test[NMN_USER_HASH_LIST_MAX];
extern struct hlist_head hash_listhead[NMN_USER_HASH_LIST_MAX];
/*
int str2user(struct user *user_entry, const char *username, const char *userpwd);
int user2str(char *buf, int buf_len, const struct user *user_entry);
//*/
int read_users_list(const char *file_path);
struct user *user_hash_get(const char *username);
//#undef HASH_LIST_MAX



#endif /* HASH_H */

hash.c

#include <hash.h>
struct hlist_head hash_listhead[NMN_USER_HASH_LIST_MAX];
int user_count;
static unsigned int bkdrhash(const char *str)
{
	unsigned int seed = 1313; //13 131 1313 13131 131313 etc...
	unsigned int hash = 0;
	while(*str) {
		hash = hash * seed + (unsigned int)(*str++);
	}
	return hash & 0x7FFFFFFF;
}
static int get_name_hash_index(const char *username)
{
	unsigned int key = bkdrhash(username);
	int index = key & (NMN_USER_HASH_LIST_MAX-1);
	return index;
}
static int user_hash_push(struct user *entry)
{
	int index = get_name_hash_index(entry->username);
	hlist_add_head(&entry->sibling, &hash_listhead[index]);
	return 0;
}
/*
static int user_cmp(const struct user *a, const struct user *b)
{
	return strcmp(a->username, b->username);
}
//*/
struct user *user_hash_get(const char *username)
{
	struct user *tpos;
	struct user *ret = NULL;
	struct hlist_node *pos;
	int index = get_name_hash_index(username);
	hlist_for_each_entry(tpos, pos, &hash_listhead[index], sibling) {
		fprintf(stdout, "username=%s...\n", tpos->username);
		if(0==strcmp(username, tpos->username)) {
			ret = tpos;
			break;
		}
	}
	return ret;
}
/*
static int line2user(struct user *entry, const char *line)
{
	sscanf(line, "%s Cleartext-Password := \"%[^\"]%*[ #\"]%ld-%ld",
	             entry->username, entry->userpwd, &entry->starttime, &entry->endtime);
	return 0;
}
//*/
/* without userpwd */
static int line2user(struct user *entry, const char *line)
{
	return sscanf(line, "%s Cleartext-Password := \"%*[^\"]%*[ #\"]%ld-%ld",
	             entry->username, &entry->starttime, &entry->endtime);
}
int read_users_list(const char *dir_path)
{
	int ret = 0;
	user_count = 0;
	char file_path[1024];
	snprintf(file_path, sizeof(file_path), "%s", dir_path);
	FILE *fp = fopen(file_path, "r");
	if(NULL==fp) {
		return -1;
	}
	char line[1024];
	struct user *entry;
	while(NULL != (fgets(line, sizeof(line), fp))) {
		if(('#'==*line) || (!strstr(line, "Cleartext-Password"))) {
			continue;
		}
		entry = (struct user *)malloc(sizeof(struct user));
		if(!entry) {
			ret = -1;
			break;
		}
		line2user(entry, line);
		user_hash_push(entry);
		++user_count;
	}
	fclose(fp);
	return ret;
}
/* 暂无释放链表方法实现 */

抱歉!评论已关闭.