Mercury
mercury_hash_table.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2005-2008, Simon Howard
4 
5 Permission to use, copy, modify, and/or distribute this software
6 for any purpose with or without fee is hereby granted, provided
7 that the above copyright notice and this permission notice appear
8 in all copies.
9 
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
14 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17 CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 
19  */
20 
46 #ifndef HG_HASH_TABLE_H
47 #define HG_HASH_TABLE_H
48 
49 #include "mercury_util_config.h"
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
59 typedef struct hg_hash_table hg_hash_table_t;
60 
66 
71 typedef struct hg_hash_table_entry hg_hash_table_entry_t;
72 
77 typedef void *hg_hash_table_key_t;
78 
83 typedef void *hg_hash_table_value_t;
84 
92  unsigned int next_chain;
93 };
94 
99 #define HG_HASH_TABLE_NULL ((void *) 0)
100 
109 typedef unsigned int (*hg_hash_table_hash_func_t)(hg_hash_table_key_t value);
110 
118 typedef int (*hg_hash_table_equal_func_t)(hg_hash_table_key_t value1, hg_hash_table_key_t value2);
119 
125 typedef void (*hg_hash_table_key_free_func_t)(hg_hash_table_key_t value);
126 
132 typedef void (*hg_hash_table_value_free_func_t)(hg_hash_table_value_t value);
133 
145 HG_UTIL_EXPORT hg_hash_table_t *
147  hg_hash_table_equal_func_t equal_func);
148 
154 HG_UTIL_EXPORT void
156 
165 HG_UTIL_EXPORT void
167  hg_hash_table_key_free_func_t key_free_func,
168  hg_hash_table_value_free_func_t value_free_func);
169 
181 HG_UTIL_EXPORT int
183  hg_hash_table_key_t key,
184  hg_hash_table_value_t value);
185 
194 HG_UTIL_EXPORT hg_hash_table_value_t
196  hg_hash_table_key_t key);
197 
206 HG_UTIL_EXPORT int
207 hg_hash_table_remove(hg_hash_table_t *hash_table, hg_hash_table_key_t key);
208 
215 HG_UTIL_EXPORT unsigned int
217 
225 HG_UTIL_EXPORT void
227 
237 HG_UTIL_EXPORT int
239 
248 HG_UTIL_EXPORT hg_hash_table_value_t
250 
251 #ifdef __cplusplus
252 }
253 #endif
254 
255 #endif /* HG_HASH_TABLE_H */
256 
hg_hash_table_entry_t * next_entry
void * hg_hash_table_value_t
A value stored in a hg_hash_table_t.
void(* hg_hash_table_value_free_func_t)(hg_hash_table_value_t value)
Type of function used to free values when entries are removed from a hash table.
HG_UTIL_EXPORT hg_hash_table_value_t hg_hash_table_lookup(hg_hash_table_t *hash_table, hg_hash_table_key_t key)
Look up a value in a hash table by key.
unsigned int(* hg_hash_table_hash_func_t)(hg_hash_table_key_t value)
Hash function used to generate hash values for keys used in a hash table.
HG_UTIL_EXPORT int hg_hash_table_iter_has_more(hg_hash_table_iter_t *iterator)
Determine if there are more keys in the hash table to iterate over.
void * hg_hash_table_key_t
A key to look up a value in a hg_hash_table_t.
int(* hg_hash_table_equal_func_t)(hg_hash_table_key_t value1, hg_hash_table_key_t value2)
Function used to compare two keys for equality.
void(* hg_hash_table_key_free_func_t)(hg_hash_table_key_t value)
Type of function used to free keys when entries are removed from a hash table.
HG_UTIL_EXPORT hg_hash_table_value_t hg_hash_table_iter_next(hg_hash_table_iter_t *iterator)
Using a hash table iterator, retrieve the next key.
HG_UTIL_EXPORT hg_hash_table_t * hg_hash_table_new(hg_hash_table_hash_func_t hash_func, hg_hash_table_equal_func_t equal_func)
Create a new hash table.
HG_UTIL_EXPORT void hg_hash_table_register_free_functions(hg_hash_table_t *hash_table, hg_hash_table_key_free_func_t key_free_func, hg_hash_table_value_free_func_t value_free_func)
Register functions used to free the key and value when an entry is removed from a hash table...
HG_UTIL_EXPORT void hg_hash_table_iterate(hg_hash_table_t *hash_table, hg_hash_table_iter_t *iter)
Initialise a HashTableIterator to iterate over a hash table.
struct hg_hash_table hg_hash_table_t
A hash table structure.
HG_UTIL_EXPORT int hg_hash_table_insert(hg_hash_table_t *hash_table, hg_hash_table_key_t key, hg_hash_table_value_t value)
Insert a value into a hash table, overwriting any existing entry using the same key.
HG_UTIL_EXPORT int hg_hash_table_remove(hg_hash_table_t *hash_table, hg_hash_table_key_t key)
Remove a value from a hash table.
struct hg_hash_table_entry hg_hash_table_entry_t
Internal structure representing an entry in a hash table.
HG_UTIL_EXPORT unsigned int hg_hash_table_num_entries(hg_hash_table_t *hash_table)
Retrieve the number of entries in a hash table.
HG_UTIL_EXPORT void hg_hash_table_free(hg_hash_table_t *hash_table)
Destroy a hash table.
hg_hash_table_t * hash_table
Definition of a hg_hash_table_iter.