|
摘自PHP的HASH算法实现(1)
/* * 头文件 * 说明:此实现不保存对象,只保存对象的引用 */ #ifndef _HASH_H #define _HASH_H
#ifdef __cplusplus extern "C" { #endif typedef strUCt tagHASHBUCKET HASHBUCKET, *LPHASHBUCKET; typedef struct tagHASHTABLE HASHTABLE, *LPHASHTABLE; struct tagHASHBUCKET { unsigned long h; unsigned int key_length; void *data; LPHASHBUCKET next; LPHASHBUCKET previous; LPHASHBUCKET conflict_next; LPHASHBUCKET conflict_previous; char key[1]; }; typedef unsigned long (*hash_func_t)(char *, unsigned int); struct tagHASHTABLE { unsigned int table_size; unsigned int size_index; unsigned int elements; hash_func_t hash; LPHASHBUCKET p; LPHASHBUCKET head; LPHASHBUCKET tail; LPHASHBUCKET *buckets; }; extern int hash_create(LPHASHTABLE, unsigned int, hash_func_t); extern int hash_entry(LPHASHTABLE, char *, unsigned int, void *); extern int hash_find(LPHASHTABLE, char *, unsigned int, void **); extern int hash_update(LPHASHTABLE, char *, unsigned int, void *); extern int hash_remove(LPHASHTABLE, char *, unsigned int); extern int hash_destroy(LPHASHTABLE);
#ifdef __cplusplus }; #endif
#endif
/* * HASH实现 */ #include <stdlib.h> #include <memory.h> #include "main.h" static unsigned int size_table[] =
|