📄 hash.h
字号:
#ifndef HASH_H#define HASH_H/** * hash.h -- a simple hash table implementation inspired by list.h * in the linux kernel * author: donald<donaldliew@gmail.com> * everyone is permitted to use and modify this file freely *//** * list.h modified based on <linux/list.h>, for more info. read any * linux source code analysis book */#include"list.h"#define MAX_KEY_LEN 40struct hash_table{ /** * the hash table data struct is nothing but a bunch of * buckets, which are simply linked lists */ struct list_head *buckets; /** * we store all entries in the hash table in the list 'all', * so that we can easily release them in a single iteration */ struct list_head all; unsigned int n; /* number of buckets */};/* * every hash table entry must include this struct as a member, see * struct list_head to comprend the mechanism better */struct hash_table_entry{ /**/ unsigned char key[MAX_KEY_LEN]; /* list of hash table entries in the same bucket */ struct list_head bucket; /* list of all hash table entries in the same hash table */ struct list_head all;};/* create an empty hash_table with specified number of buckets */struct hash_table *hash_table_create(int bucket_num);/* add an entry into existing hash table */void hash_table_add(struct hash_table *table, struct hash_table_entry* entry);/* retrieve an entry with specified key from existing hash table */struct hash_table_entry *hash_table_get(struct hash_table *table, const unsigned char *key);/* release an hash table, note that entries must be released manually */voidhash_table_free(struct hash_table *table);/* to comprehend how this works, refer to list_entry in list.h */#define hash_entry(ptr, type, member) \ ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))/* to comprehend how this works, refer to list_for_each in list.h */#define hash_for_each(pos, hash_table) \ for(pos=list_entry((hash_table)->all.next, struct hash_table_entry, all);\ pos!=list_entry(&(hash_table)->all, struct hash_table_entry, all);\ pos=list_entry(pos->all.next, struct hash_table_entry, all))#endif /* #ifndef HASH_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -