/* * hashtable.h * * Created on: 21.10.2017 * Author: julian */ #ifndef HASHTABLE_H_ #define HASHTABLE_H_ #include #include #include #include "strhash.h" #include #include typedef strhash_t entry_hash_t; typedef const char * entry_key_t; typedef struct entry * hashtable_iterator_t; // data deallocator typedef void(*hashtable_dealloc)(void*); extern int key_compare(entry_key_t one, entry_key_t two); extern entry_hash_t key_hash(entry_key_t key); struct entry { entry_hash_t hash; entry_key_t key; void * data; }; struct hashtable { struct entry * data; size_t len, count; hashtable_dealloc dealloc_data; }; extern void hashtable_init(struct hashtable * table); extern hashtable_iterator_t hashtable_end(struct hashtable * table); /** * Iterate of the hashmap. * * pass the return value to current to iterate over the entire map * pass NULL to get the beginning * * returns the next set entry in the table from current * returns hashtable_end() when there is nons * example loop: * hashmap_iterator_t current = hashtable_next(table, NULL); * for(;current != hashtable_end(table); current = hashtable_next(table, current)) {} * */ hashtable_iterator_t hashtable_next(struct hashtable * table, hashtable_iterator_t current); /** * returns the entry identified by the given hash * returns hashtable_end() if no entry matched */ hashtable_iterator_t hashtable_get_hash(struct hashtable * table, entry_hash_t hash); /** * returns the entry identified by the given key * returns hashtable_end() if no entry matched */ hashtable_iterator_t hashtable_get(struct hashtable * table, entry_key_t key); /** * delete all entries of the given table * * calls dealloc of the table for every data element, if it was set. */ void hashtable_clear(struct hashtable * table); /** * insert a new element in the table */ int hashtable_add(struct hashtable * table, struct entry entry); /** * resize the hashtable to a new physical size * can also try to make the hashtable smaller * * returns 0 on success * returns -1 on error (sets errno) * returns -2 if hashtable does not fit in new size. */ int hashtable_resize(struct hashtable * table, size_t newsize); /** * add new element in hashtable * * returns 0 on success * returns -1 on error */ int hashtable_add(struct hashtable * table, struct entry entry); /** * make a new entry that can be added out of key and data */ struct entry hashtable_make_entry(entry_key_t key, void * data); /** * remove one entry using the key * @return -1 on error * @return 1 on success * @return 0 on not found */ int hashtable_remove(struct hashtable * table, hashtable_iterator_t it); #endif /* HASHTABLE_H_ */