-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo_hash_table_3.h
37 lines (30 loc) · 1.52 KB
/
demo_hash_table_3.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef __HASHTAB_H__
#define __HASHTAB_H__
typedef struct _hashtab_node {
void* key;
void* data;
struct _hashtab_node *next;
} hashtab_node;
typedef struct _hashtab {
hashtab_node **htables; // 哈希桶
int size; // 哈希桶的最大数量
int nel; // 哈希桶中元素的个数
int (*hash_value)(struct _hashtab *h, const void *key); // 哈希函数
int (*keycmp)(struct _hashtab *h, const void *key1, const void *key2); // 哈希key比较函数函数,当哈希数值一致时适用
void (*hash_node_free)(hashtab_node *node);
} hashtab;
#define HASHTAB_MAX_NODES (0xffffffff)
typedef int (*hash_key_func)(struct _hashtab *h, const void *key); // 哈希函数
typedef int (*keycmp_func)(struct _hashtab *h, const void *key1, const void *key2); // 哈希key比较函数,当哈希数值一致时候使用
typedef void (*hash_node_free_func)(hashtab_node *node);
// 根据当前结构体元素的地址,获取到结构体首地址
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE*)0)->MEMBER)
#define container(ptr, type, member) ({\
const typeof(((type *)0)->member) *__mptr = (ptr);\
(type *) ((char *)__mptr - offsetof(type, member));})
hashtab *hashtab_create(int size, hash_key_func hash_value, keycmp_func keycmp, hash_node_free_func hash_node_free);
void hashtab_destory(hashtab *h);
int hashtab_insert(hashtab *h, void *key, void *data);
hashtab_node *hashtab_delete(hashtab *h, void *key);
void *hashtab_search(hashtab* h, void *key);
#endif