C语言的hash表实现:uthash

jopen 10年前

uthash是一个C语言的hash表实现。它以宏定义的方式实现hash表,不仅加快了运行的速度,而且与关键类型无关的优点。

uthash使用起来十分方便,只要将头文件uthash.h包含进去就可以使用。

uthash支持如下平台:

  • Linux
  • Mac OS X
  • Windows using vs2008 and vs 2010
  • Solaris
  • OpenBSD

Example 1. Adding an item to a hash.
#include "uthash.h"    struct my_struct {      int id;            /* we'll use this field as the key */      char name[10];                   UT_hash_handle hh; /* makes this structure hashable */  };    struct my_struct *users = NULL;    void add_user(struct my_struct *s) {      HASH_ADD_INT( users, id, s );      }
Example 2. Looking up an item in a hash.
struct my_struct *find_user(int user_id) {      struct my_struct *s;        HASH_FIND_INT( users, &user_id, s );        return s;  }
Example 3. Deleting an item from a hash.
void delete_user(struct my_struct *user) {      HASH_DEL( users, user);    }

项目主页:http://www.open-open.com/lib/view/home/1386236285626