Chinaunix首页 | 论坛 | 博客
  • 博客访问: 123454
  • 博文数量: 20
  • 博客积分: 1627
  • 博客等级: 上尉
  • 技术积分: 383
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-25 16:11
文章分类

全部博文(20)

文章存档

2012年(5)

2011年(2)

2010年(12)

2009年(1)

我的朋友

分类: C/C++

2010-04-08 22:49:59

    在linux下用c,一直想找个很好用的hash库,前段时间也找到了linxu自带的hcreate,发现key只能是数组,而且好像没有删除节点功能,只能放弃使用,太不友好了。呵呵。

    后来又找了一个glib提供的哈希库,g_hash_table_xxx系列函数,用起来还行。凑合了,不过要链接glib的库,也不是很理想,不过万一没有其他的用,也忍了,呵呵。

    不过最近同事推荐了一个uthash的库,很好用哦。而且不是库,是和内核的list.h一样,是提供一个头文件给你,你只需要包含这个头文件就好了。key值也可以是int, str等。用起来很方便。这里提供uthash的地址,大家可以下来用用。呵呵。


下面是我的测试代码,以备后用。
myhash.c

#include "uthash.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/*这个uthash必须构造一个结构体*/
struct packet
{
        int key;       /*这个是用来做hash的key值*/
        char msg[10];
        UT_hash_handle hh; /*这个结构是uthash的结构体,里面包含next,prev,hash值等信息*/
};

int main()
{
        struct packet *pkt, *tmp;
        int i;

        struct packet *hash_packet = NULL; /*必须初始化为NULL*/
       

        /*打印这个hash的节点数*/
        printf ("hash count = %d \n", HASH_COUNT(hash_packet));


        /*往hash中添加节点*/

        for (i=0; i<10; i++)
        {
                pkt = (struct packet *)malloc(sizeof(struct packet));
                pkt->key=i;
                sprintf (pkt->msg, "i=%d", i);

                HASH_FIND_INT(hash_packet, &i, tmp);
                if (tmp != NULL)
                {
                        printf ("The key(%d) exists in hash. \n", i);
                        continue;
                }
                HASH_ADD_INT(hash_packet, key, pkt);
                printf ("insert item. key=%d,value=%p \n", i, pkt);
        }
        printf ("hash count = %d \n", HASH_COUNT(hash_packet));


        /*通过key查找*/
        for (i=0; i<13; i++)
        {
                HASH_FIND_INT(hash_packet, &i, tmp);
                if (tmp == NULL)
                {
                        printf ("find not item. key=%d,value=%p \n", i, tmp);
                        continue;
                }
                printf ("find item. key=%d,value=%p \n", i, tmp);
        }

        printf ("hash count = %d \n", HASH_COUNT(hash_packet));


        /*遍历这个hash表*/
        for (tmp=hash_packet; tmp != NULL; tmp=tmp->hh.next)
                printf (" %d => %s \n", tmp->key, tmp->msg);
       
/*删除节点*/
        for (i=0; i<13; i++)
        {
                HASH_FIND_INT(hash_packet, &i, tmp);
                if (tmp == NULL)
                {
                        printf ("find not item. key=%d,value=%p \n", i, tmp);
                        continue;
                }

                /*删除节点不会释放你的空间必须自己释放*/
                HASH_DEL(hash_packet, tmp);
                free(tmp);
                printf ("delete itme. key=%d,value=%p \n", i, tmp);
        }
        printf ("hash count = %d \n", HASH_COUNT(hash_packet));
        return 0;
}


makefile

CC=gcc
CFLAGS=-Wall -g -I.

all: depend myhash

depend:
        $(CC) -MM *.c > .depend
myhash: myhash.o .depend
        $(CC) -o $@ $<

ctags:
        ctags *.c *.h
clean:
        rm -f *.o myhash

re:
        make clean
        make


就写这么多吧。有什么问题去看他提供的文档。里面很详细,也说了很多需要注意的地方。祝大家使用愉快。
阅读(8348) | 评论(0) | 转发(0) |
0

上一篇:glib hash的使用

下一篇:内核list.h使用

给主人留下些什么吧!~~