ht.h (1814B)
1 #ifndef HT_H 2 #define HT_H 3 4 #include <stack_sd.h> 5 6 #define HT_DEFAULT_SIZE 255 7 #define HT_DEFAULT_STACK_SIZE 2 8 #define HT_LOOKUP_NOT_FOUND (STACK_SD_LOOKUP_NOT_FOUND) // == -1 9 10 typedef struct { 11 size_t nelts; 12 size_t nalloc; 13 14 stack_sd_t *entries; 15 } ht_t; 16 17 /** 18 * Intitialize a hash table. 19 * 20 * This initializes a hash table of <<alloc>> buckets, at the address 21 * pointed to by <<t>>. Note that this memory needs to be cleared. 22 * 23 * @param t pointer to the ht_t to be initialized 24 * @param alloc the number of buckets to allocate for 25 */ 26 void ht_init_size(ht_t *t, const size_t alloc); 27 28 /** 29 * Initialize a hash table. 30 * 31 * This initializes a hash table of the default size which is 32 * HT_DEFAULT_SIZE number of buckets. Note that this allocates memory 33 * that needs to be ht_clear'd. 34 * 35 * @param t pointer to the th to initialized 36 */ 37 void ht_init(ht_t *t); 38 39 /** 40 * Clear a stack. 41 * 42 * @param t the stack to clear 43 */ 44 void ht_clear(ht_t *t); 45 46 /** 47 * Lookup a sd_t in the hashtable. 48 * 49 * Returns a nonnegative value if the sd_t is in the hash table (by value 50 * comparison rather than pointer comparison of data). The value is the 51 * index of the stack_sd_t in the hash bucket where the sd_t is found. If 52 * the sd_t cannot be found in the stack the value 53 * HT_LOOKUP_NOT_FOUND == -1 54 * is returned. 55 * 56 * @param t pointer to the hash table too look up in 57 * @param d the sd_t to look up 58 */ 59 int ht_lookup(ht_t *t, sd_t d); 60 61 /** 62 * Insert a as into the hashtable. 63 * 64 * Inserts a sd_t into the hash table. Note that this does not copy the 65 * sd_t data, but only pushes the pointer onto the stack. The sd_t should 66 * therefore not be cleared until ht_t has been cleared. 67 * 68 * @param t pointer to the hash table to insert into 69 * @param d the sd_t to insert 70 */ 71 void ht_insert(ht_t *t, sd_t d); 72 73 #endif