/*
*******************************************************************
*
* FILE NAME : hash.cpp
* ORIGINATOR : 王钊扬(micklongen)
* DATE OF ORIGIN : 2009-4-26
*
*******************************************************************
*/
#include "stdafx.h"
#include <stdio.h>
#include "list.h"
#include "hash.h"
static struct DListHeader * get_list_header(struct hash_struct_info *head, struct DListNode *src)
{
if (head == NULL)
{
printf("The hash_struct_info head is NULL.\n");
exit(1);
}
if (head->hash_list == NULL)
{
printf("The hash_list is NULL.\n");
exit(1);
}
if (src == NULL)
{
printf("The src is NULL.\n");
exit(1);
}
return &(head->hash_list[head->hash(src) % head->size]);
}
static struct DListNode * Hash_list_search(struct hash_struct_info *head, struct DListNode *src)
{
struct DListNode *plink;
struct DListHeader *header;
if (head == NULL)
{
printf("The hash_struct_info head is NULL.\n");
exit(1);
}
if (src == NULL)
{
printf("The src is NULL\n");
exit(1);
}
header = head->get_list_header(head, src);
plink = DList_get_next(header, &(header->head));
while(plink != NULL)
{
if (head->cmp((void *)plink, (void *)src) == 0)
break;
plink = DList_get_next(header, plink);
}
return plink;
}
static int Hash_list_insert(struct hash_struct_info *head, struct DListNode *src)
{
struct DListHeader *header;
if (head == NULL)
{
printf("The hash_struct_info head is NULL.\n");
exit(1);
}
if (src == NULL)
{
printf("The src is NULL\n");
exit(1);
}
header = head->get_list_header(head, src);
DList_add_tail(header, src);
return 0;
}
static struct DListNode * Hash_list_delete(struct hash_struct_info *head, struct DListNode *src)
{
struct DListHeader *header;
if (head == NULL)
{
printf("The hash_struct_info head is NULL.\n");
exit(1);
}
if (src == NULL)
{
printf("The src is NULL\n");
exit(1);
}
header = head->get_list_header(head, src);
if (header == NULL)
{
printf("The list header is NULL\n");
exit(1);
}
return DList_delete_node(header, src);
}
int hash_init(struct hash_struct_info *head, int (*hash)(void *), int (*cmp)(void*, void*))
{
int i;
if (head == NULL)
{
printf("The hash_struct_info head is NULL.\n");
exit(1);
}
for (i = 0; i < MAX_HASH_HEADER_LEN; ++i)
initialize_Dlist_header(&(head->hash_list[i]));
head->size = MAX_HASH_HEADER_LEN;
head->hash = hash;
head->cmp = cmp;
head->get_list_header = get_list_header;
head->insert_node = Hash_list_insert;
head->search_node = Hash_list_search;
head->delete_node = Hash_list_delete;
return 1;
}
int hash_change_function(struct hash_struct_info *head, int member, void (*handler)(void))
{
switch(member)
{
case HASH_MEMBER_CMP:
head->cmp = (int (__cdecl *)(void *, void *))handler;
break;
case HASH_MEMBER_HASH:
head->hash = (int (__cdecl *)(void *))handler;
break;
case HASH_MEMBER_GET_LIST_HEADER:
head->get_list_header = (struct DListHeader *(__cdecl *)(hash_struct_info *, DListNode *))handler;
break;
case HASH_MEMBER_SEARCH:
head->search_node = (struct DListNode *(__cdecl *)(hash_struct_info *, DListNode *))handler;
break;
case HASH_MEMBER_INSERT:
head->insert_node = (int (__cdecl *)(hash_struct_info *, DListNode *))handler;
break;
case HASH_MEMBER_DELETE:
head->delete_node = (struct DListNode * (__cdecl *)(hash_struct_info *, DListNode *))handler;
break;
default:
printf("Can't change the member within the hash_struct_info.\n");
break;
}
return 0;
}
|