C語言的hash表實現:uthash
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.
Example 3. Deleting an item from a hash.struct my_struct *find_user(int user_id) { struct my_struct *s; HASH_FIND_INT( users, &user_id, s ); return s; }
void delete_user(struct my_struct *user) { HASH_DEL( users, user); }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!