1#ifndef __HASH_H_ 2#define __HASH_H_ 3 4/* 5 * Simple hash table implementation 6 * --kkourt@cslab.ece.ntua.gr 7 */ 8 9/* hash entry */ 10typedef struct hash_entry_st { 11 /* key, value for hash entry */ 12 unsigned long key; 13 unsigned long val; 14 /* pointer to next entry, if NULL this is the last one */ 15 struct hash_entry_st *next; 16 //char padding[40]; 17} hash_entry_t; 18 19typedef struct hash_st { 20 /* hash table: each entry is is a pointer to 21 * the head of a linked list of hash entries */ 22 hash_entry_t **table; 23 unsigned int size; /* number of slots */ 24} hash_t; 25 26/** 27 * hash_init: initialize a hash table 28 * size: size of the table 29 */ 30hash_t *hash_init(unsigned int size); 31 32#define HASH_ENTRY_NOTFOUND (~0UL) 33/** 34 * hash_lookup: find the value of the given key 35 * If entry exists return val, else return HASH_ENTRY_NOTFOUND 36 */ 37unsigned long hash_lookup(hash_t *hash, unsigned long key); 38 39/** 40 * hash_insert: insert a new value for the given key 41 * If an entry for the key exists, just replace it 42 */ 43void hash_insert(hash_t *hash, unsigned long key, unsigned long val); 44 45/** 46 * hash_delete: delete the entry of the given value 47 * If entry exists return old value, else return HASH_ENTRY_NOTFOUND 48 */ 49unsigned long hash_delete(hash_t *hash, unsigned long key); 50 51/** 52 * hash_destroy: destroy the hash 53 */ 54void hash_destroy(hash_t *hash); 55 56/** 57 * hash_swap: exchange the values of two keys. 58 * if an entry for either keys does not exist, return -1 59 * else return 0 60 */ 61int hash_swap(hash_t *hash, unsigned long key1, unsigned long key2); 62 63static inline unsigned long hash_fn(hash_t *hash, unsigned long key) 64{ 65 return (key % hash->size); 66} 67 68 69void hash_print(hash_t *hash); 70#endif 71