Mercury
Classes | Macros | Typedefs | Functions
mercury_hash_table.h File Reference

Hash table. More...

#include "mercury_util_config.h"
Include dependency graph for mercury_hash_table.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  hg_hash_table_iter
 Definition of a hg_hash_table_iter. More...
 

Macros

#define HG_HASH_TABLE_NULL   ((void *) 0)
 A null HashTableValue. More...
 

Typedefs

typedef struct hg_hash_table hg_hash_table_t
 A hash table structure. More...
 
typedef struct hg_hash_table_iter hg_hash_table_iter_t
 Structure used to iterate over a hash table. More...
 
typedef struct hg_hash_table_entry hg_hash_table_entry_t
 Internal structure representing an entry in a hash table. More...
 
typedef void * hg_hash_table_key_t
 A key to look up a value in a hg_hash_table_t. More...
 
typedef void * hg_hash_table_value_t
 A value stored in a hg_hash_table_t. More...
 
typedef 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. More...
 
typedef 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. More...
 
typedef 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. More...
 
typedef 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. More...
 

Functions

HG_UTIL_EXPORT hg_hash_table_thg_hash_table_new (hg_hash_table_hash_func_t hash_func, hg_hash_table_equal_func_t equal_func)
 Create a new hash table. More...
 
HG_UTIL_EXPORT void hg_hash_table_free (hg_hash_table_t *hash_table)
 Destroy a hash table. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 

Detailed Description

Hash table.

A hash table stores a set of values which can be addressed by a key. Given the key, the corresponding value can be looked up quickly.

To create a hash table, use hg_hash_table_new. To destroy a hash table, use hg_hash_table_free.

To insert a value into a hash table, use hg_hash_table_insert.

To remove a value from a hash table, use hg_hash_table_remove.

To look up a value by its key, use hg_hash_table_lookup.

To iterate over all values in a hash table, use hg_hash_table_iterate to initialize a hg_hash_table_iter structure. Each value can then be read in turn using hg_hash_table_iter_next and hg_hash_table_iter_has_more.

Definition in file mercury_hash_table.h.

Macro Definition Documentation

#define HG_HASH_TABLE_NULL   ((void *) 0)

A null HashTableValue.

Definition at line 99 of file mercury_hash_table.h.

Typedef Documentation

typedef struct hg_hash_table hg_hash_table_t

A hash table structure.

Definition at line 59 of file mercury_hash_table.h.

Structure used to iterate over a hash table.

Definition at line 65 of file mercury_hash_table.h.

typedef struct hg_hash_table_entry hg_hash_table_entry_t

Internal structure representing an entry in a hash table.

Definition at line 71 of file mercury_hash_table.h.

typedef void* hg_hash_table_key_t

A key to look up a value in a hg_hash_table_t.

Definition at line 77 of file mercury_hash_table.h.

typedef void* hg_hash_table_value_t

A value stored in a hg_hash_table_t.

Definition at line 83 of file mercury_hash_table.h.

typedef 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.

Parameters
valueThe value to generate a hash value for.
Returns
The hash value.

Definition at line 109 of file mercury_hash_table.h.

typedef 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.

Returns
Non-zero if the two keys are equal, zero if the keys are not equal.

Definition at line 118 of file mercury_hash_table.h.

typedef 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.

Definition at line 125 of file mercury_hash_table.h.

typedef 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.

Definition at line 132 of file mercury_hash_table.h.

Function Documentation

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.

Parameters
hash_funcFunction used to generate hash keys for the keys used in the table.
equal_funcFunction used to test keys used in the table for equality.
Returns
A new hash table structure, or NULL if it was not possible to allocate the new hash table.
HG_UTIL_EXPORT void hg_hash_table_free ( hg_hash_table_t hash_table)

Destroy a hash table.

Parameters
hash_tableThe hash table to destroy.
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.

Parameters
hash_tableThe hash table.
key_free_funcFunction used to free keys.
value_free_funcFunction used to free values.
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.

Parameters
hash_tableThe hash table.
keyThe key for the new value.
valueThe value to insert.
Returns
Non-zero if the value was added successfully, or zero if it was not possible to allocate memory for the new entry.
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.

Parameters
hash_tableThe hash table.
keyThe key of the value to look up.
Returns
The value, or HASH_TABLE_NULL if there is no value with that key in the hash table.
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.

Parameters
hash_tableThe hash table.
keyThe key of the value to remove.
Returns
Non-zero if a key was removed, or zero if the specified key was not found in the 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.

Parameters
hash_tableThe hash table.
Returns
The number of entries in the 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.

Parameters
hash_tableThe hash table.
iterPointer to an iterator structure to initialise.
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.

Parameters
iteratorThe hash table iterator.
Returns
Zero if there are no more values to iterate over, non-zero if there are more values to iterate over.
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.

Parameters
iteratorThe hash table iterator.
Returns
The next key from the hash table, or HG_HASH_TABLE_NULL if there are no more keys to iterate over.