Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320737
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类: C/C++

2009-05-10 14:01:46


/*
 *******************************************************************
 *
 * 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;
}

阅读(2477) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~