hash_table.h
Go to the documentation of this file.
/*
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
Copyright (C) 2022 The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
typedef unsigned (*hash_func_t) (const char *key);
struct hash_table *hash_table_create(int buckets, hash_func_t func);
void hash_table_clear(struct hash_table *h, void (*delete_func)( void *) );
void hash_table_delete(struct hash_table *h);
int hash_table_size(struct hash_table *h);
double hash_table_load(struct hash_table *h);
int hash_table_insert(struct hash_table *h, const char *key, const void *value);
void *hash_table_lookup(struct hash_table *h, const char *key);
void *hash_table_remove(struct hash_table *h, const char *key);
void hash_table_firstkey(struct hash_table *h);
int hash_table_nextkey(struct hash_table *h, char **key, void **value);
void hash_table_randomkey(struct hash_table *h, int *offset_bookkeep);
int hash_table_nextkey_with_offset(struct hash_table *h, int offset_bookkeep, char **key, void **value);
int hash_table_fromkey(struct hash_table *h, const char *key);
unsigned hash_string(const char *s);
char **hash_table_keys_array(struct hash_table *h);
void hash_table_free_keys_array(char **keys);
#define HASH_TABLE_ITERATE( table, key, value ) hash_table_firstkey(table); while(hash_table_nextkey(table,&key,(void**)&value))
#define HASH_TABLE_ITERATE_RANDOM_START( table, offset_bookkeep, key, value ) hash_table_randomkey(table, &offset_bookkeep); while(hash_table_nextkey_with_offset(table, offset_bookkeep, &key, (void **)&value))
#define HASH_TABLE_ITERATE_FROM_KEY( table, iter_control, iter_count_var, key_start, key, value ) \
iter_control = 0; \
iter_count_var = 0; \
hash_table_fromkey(table, key_start); \
while(iter_count_var < hash_table_size(table) && (iter_count_var+=1 && (hash_table_nextkey(table, &key, (void **)&value) || (!iter_control && (iter_control+=1) && hash_table_fromkey(table, NULL) && hash_table_nextkey(table, &key, (void **)&value)))))
#endif
hash_table_firstkey
void hash_table_firstkey(struct hash_table *h)
Begin iteration over all keys.
hash_table_load
double hash_table_load(struct hash_table *h)
Return the proportion of elements vs buckets in the table.
hash_table_keys_array
char ** hash_table_keys_array(struct hash_table *h)
Return a NULL-termianted array with a copy of all the current keys.
hash_table_lookup
void * hash_table_lookup(struct hash_table *h, const char *key)
Look up a value by key.
hash_table_fromkey
int hash_table_fromkey(struct hash_table *h, const char *key)
Begin iteration at the given key.
hash_func_t
unsigned(* hash_func_t)(const char *key)
The type signature for a hash function given to hash_table_create.
Definition hash_table.h:41
hash_table_insert
int hash_table_insert(struct hash_table *h, const char *key, const void *value)
Insert a key and value.
hash_table_randomkey
void hash_table_randomkey(struct hash_table *h, int *offset_bookkeep)
Begin iteration over all keys from a random offset.
hash_string
unsigned hash_string(const char *s)
A default hash function.
hash_table_clear
void hash_table_clear(struct hash_table *h, void(*delete_func)(void *))
Remove all entries from an hash table.
hash_table_delete
void hash_table_delete(struct hash_table *h)
Delete a hash table.
hash_table_size
int hash_table_size(struct hash_table *h)
Count the entries in a hash table.
hash_table_free_keys_array
void hash_table_free_keys_array(char **keys)
Free an array generated from hash_table_free_keys_array.
hash_table_nextkey_with_offset
int hash_table_nextkey_with_offset(struct hash_table *h, int offset_bookkeep, char **key, void **value)
Continue iteration over all keys from an arbitray offset.
hash_table_remove
void * hash_table_remove(struct hash_table *h, const char *key)
Remove a value by key.
hash_table_create
struct hash_table * hash_table_create(int buckets, hash_func_t func)
Create a new hash table.
hash_table_nextkey
int hash_table_nextkey(struct hash_table *h, char **key, void **value)
Continue iteration over all keys.