cctools
hash_table.h
Go to the documentation of this file.
1/*
2Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
3Copyright (C) 2022 The University of Notre Dame
4This software is distributed under the GNU General Public License.
5See the file COPYING for details.
6*/
7
8#ifndef HASH_TABLE_H
9#define HASH_TABLE_H
10
41typedef unsigned (*hash_func_t) (const char *key);
42
49struct hash_table *hash_table_create(int buckets, hash_func_t func);
50
56void hash_table_clear(struct hash_table *h, void (*delete_func)( void *) );
57
58
64void hash_table_delete(struct hash_table *h);
65
71int hash_table_size(struct hash_table *h);
72
79double hash_table_load(struct hash_table *h);
80
91int hash_table_insert(struct hash_table *h, const char *key, const void *value);
92
99void *hash_table_lookup(struct hash_table *h, const char *key);
100
107void *hash_table_remove(struct hash_table *h, const char *key);
108
116void hash_table_firstkey(struct hash_table *h);
117
129int hash_table_nextkey(struct hash_table *h, char **key, void **value);
130
139void hash_table_randomkey(struct hash_table *h, int *offset_bookkeep);
140
150int hash_table_nextkey_with_offset(struct hash_table *h, int offset_bookkeep, char **key, void **value);
151
161int hash_table_fromkey(struct hash_table *h, const char *key);
162
168unsigned hash_string(const char *s);
169
177char **hash_table_keys_array(struct hash_table *h);
178
179
184
199#define HASH_TABLE_ITERATE( table, key, value ) hash_table_firstkey(table); while(hash_table_nextkey(table,&key,(void**)&value))
200
201#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))
202
203#define HASH_TABLE_ITERATE_FROM_KEY( table, iter_control, iter_count_var, key_start, key, value ) \
204 iter_control = 0; \
205 iter_count_var = 0; \
206 hash_table_fromkey(table, key_start); \
207 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)))))
208
209#endif
void hash_table_firstkey(struct hash_table *h)
Begin iteration over all keys.
double hash_table_load(struct hash_table *h)
Return the proportion of elements vs buckets in the table.
char ** hash_table_keys_array(struct hash_table *h)
Return a NULL-termianted array with a copy of all the current keys.
void * hash_table_lookup(struct hash_table *h, const char *key)
Look up a value by key.
int hash_table_fromkey(struct hash_table *h, const char *key)
Begin iteration at the given key.
unsigned(* hash_func_t)(const char *key)
The type signature for a hash function given to hash_table_create.
Definition hash_table.h:41
int hash_table_insert(struct hash_table *h, const char *key, const void *value)
Insert a key and value.
void hash_table_randomkey(struct hash_table *h, int *offset_bookkeep)
Begin iteration over all keys from a random offset.
unsigned hash_string(const char *s)
A default hash function.
void hash_table_clear(struct hash_table *h, void(*delete_func)(void *))
Remove all entries from an hash table.
void hash_table_delete(struct hash_table *h)
Delete a hash table.
int hash_table_size(struct hash_table *h)
Count the entries in a hash table.
void hash_table_free_keys_array(char **keys)
Free an array generated from hash_table_free_keys_array.
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.
void * hash_table_remove(struct hash_table *h, const char *key)
Remove a value by key.
struct hash_table * hash_table_create(int buckets, hash_func_t func)
Create a new hash table.
int hash_table_nextkey(struct hash_table *h, char **key, void **value)
Continue iteration over all keys.