00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __HASH_TABLE_H
00027 #define __HASH_TABLE_H
00028
00029 typedef struct ht_statistic {
00030 int find_cnt;
00032 int cmp_cnt;
00034 int nocmp_cnt;
00036 int missed_cnt;
00037 } ht_statistic_t;
00038
00039 typedef const void* ht_key_t;
00040 typedef void* ht_data_t;
00041
00042 typedef unsigned int (*hash_func_t)(ht_key_t k);
00043 typedef int (*key_cmp_func_t)(ht_key_t a, ht_key_t b);
00044
00045 typedef struct ht_element {
00046 ht_key_t key;
00047 ht_data_t data;
00048 struct ht_element *next;
00049 } ht_element_t;
00050
00051 typedef struct ht_cslot {
00052 ht_element_t *first;
00053 ht_element_t *last;
00054 int cnt;
00055 } ht_cslot_t;
00056
00057 typedef struct hash_table {
00058 hash_func_t hash;
00059 key_cmp_func_t cmp;
00060 ht_cslot_t *cslots;
00061 int size;
00062
00063 int find_cnt;
00064 int cmp_cnt;
00065 int nocmp_cnt;
00066 int missed_cnt;
00067 } hash_table_t;
00068
00069 int ht_init(hash_table_t *ht, hash_func_t hash_func, key_cmp_func_t cmp_keys, int size);
00070 void ht_destroy(hash_table_t *ht);
00071 int ht_add(hash_table_t *ht, ht_key_t key, ht_data_t data);
00072 ht_data_t ht_remove(hash_table_t *ht, ht_key_t key);
00073 ht_data_t ht_find(hash_table_t *ht, ht_key_t key);
00074 void ht_get_statistic(hash_table_t *ht, ht_statistic_t *s);
00075 void ht_clear_statistic(hash_table_t *ht);
00076
00077
00078 typedef struct {
00079 hash_table_t *ht;
00080 int slot_pos;
00081 ht_element_t *current;
00082 } ht_traversal_info_t;
00083
00084 ht_element_t *get_first_ht_element(hash_table_t *ht, ht_traversal_info_t *info);
00085 ht_element_t *get_next_ht_element(ht_traversal_info_t *info);
00086
00087
00088 unsigned int rshash(const char* str, unsigned int len);
00089
00090 #endif