CAFE
Computational Analysis of gene Family Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hashtable.h File Reference

a simple hash table implementation More...

#include <sys/types.h>
#include <stdint.h>

Go to the source code of this file.

Data Structures

struct  hash_table_element
 stores an hash table element for use in the hash table More...
 
struct  hash_table
 identifies the hashtable for which operations are to be performed More...
 

Macros

#define HASH_LEN   table->key_num
 
#define HASH(x, y)   hash_table_do_hash(x,y,HASH_LEN)
 
#define hash_table_element_s   sizeof(hash_table_element_t)
 
#define hash_table_s   sizeof(hash_table_t)
 
#define HT_ADD(table, key, value)   hash_table_add(table, (void *) key, sizeof(*key), (void *) value, sizeof(*value))
 
#define HT_REMOVE(table, key)   hash_table_remove(table, key, sizeof(*key))
 
#define HT_LOOKUP(table, key)   hash_table_lookup(table, key, sizeof(*key))
 
#define HT_HAS_KEY(table, key)   hash_table_has_key(table, key, sizeof(*key))
 

Typedefs

typedef struct hash_table_element hash_table_element_t
 
typedef enum hash_table_mode hash_table_mode_t
 
typedef struct hash_table hash_table_t
 

Enumerations

enum  hash_table_mode { MODE_COPY, MODE_VALUEREF, MODE_ALLREF }
 

Functions

hash_table_element_thash_table_element_new ()
 
void hash_table_element_delete (hash_table_t *, hash_table_element_t *)
 
uint16_t hash_table_do_hash (void *key, size_t key_len, uint16_t max_key)
 
hash_table_thash_table_new (hash_table_mode_t)
 
void hash_table_delete (hash_table_t *)
 
int hash_table_add (hash_table_t *, void *, size_t, void *, size_t)
 
int hash_table_remove (hash_table_t *, void *, size_t)
 
void * hash_table_lookup (hash_table_t *, void *, size_t)
 
int hash_table_has_key (hash_table_t *, void *, size_t)
 
size_t hash_table_get_keys (hash_table_t *, void ***)
 
size_t hash_table_get_elements (hash_table_t *, hash_table_element_t ***)
 
int hash_table_resize (hash_table_t *, size_t)
 
int hash_table_iterate (hash_table_t *table, int(*fct)(void *user, void *value, void *key, size_t key_len), void *user)
 

Detailed Description

a simple hash table implementation

License GPLv3+

Author
Ankur Shrivastava

Macro Definition Documentation

#define HASH (   x,
 
)    hash_table_do_hash(x,y,HASH_LEN)
#define HASH_LEN   table->key_num
#define hash_table_element_s   sizeof(hash_table_element_t)
#define hash_table_s   sizeof(hash_table_t)
#define HT_ADD (   table,
  key,
  value 
)    hash_table_add(table, (void *) key, sizeof(*key), (void *) value, sizeof(*value))

macro to add a key - value pair to the hash table

Note
use this macro when size of key and/or value can be given by sizeof
Parameters
tablehash table to add element to
keypointer to the key for the hash table
valuepointer to the value to be added against the key
Returns
0 on sucess
-1 when no memory
#define HT_HAS_KEY (   table,
  key 
)    hash_table_has_key(table, key, sizeof(*key))

macro to look if the exists in the hash table

Note
use this macro when size of key and/or value can be given by sizeof
Parameters
keypointer to key to be looked for
Returns
0 when key is not found
1 when key is found
#define HT_LOOKUP (   table,
  key 
)    hash_table_lookup(table, key, sizeof(*key))

macro to lookup a key in a particular table

Parameters
tabletable to look key in
keypointer to key to be looked for
Returns
NULL when key is not found in the hash table
void* pointer to the value in the table
#define HT_REMOVE (   table,
  key 
)    hash_table_remove(table, key, sizeof(*key))

macro to remove an hash table element (for a given key) from a given hash table

Note
use this macro when size of key and/or value can be given by sizeof
Parameters
tablehash table from which element has to be removed
keypointer to the key which has to be removed
Returns
0 on sucess
-1 when key is not found

Typedef Documentation

typedef struct hash_table hash_table_t

Enumeration Type Documentation

defines the mode of operation of hash table

Enumerator
MODE_COPY 

copy mode here values as well as key is copied

MODE_VALUEREF 

value reference mode, here ONLY key is copies and value is always referred

MODE_ALLREF 

in this mode all keys and values are referred

Function Documentation

int hash_table_add ( hash_table_t table,
void *  key,
size_t  key_len,
void *  value,
size_t  value_len 
)

Function to add a key - value pair to the hash table, use HT_ADD macro

Parameters
tablehash table to add element to
keypointer to the key for the hash table
key_lenlength of the key in bytes
valuepointer to the value to be added against the key
value_lenlength of the value in bytes
Returns
0 on sucess
-1 when no memory
void hash_table_delete ( hash_table_t table)

Function to delete the hash table

Parameters
tablehash table to be deleted
uint16_t hash_table_do_hash ( void *  key,
size_t  key_len,
uint16_t  max_key 
)

Function that returns a hash value for a given key and key_len

Parameters
keypointer to the key
key_lenlength of the key
max_keymax value of the hash to be returned by the function
Returns
hash value belonging to [0, max_key)
void hash_table_element_delete ( hash_table_t table,
hash_table_element_t element 
)

Function to delete an hash table element

Parameters
tabletable from which element has to be deleted
elementhash table element to be deleted
hash_table_element_t* hash_table_element_new ( )

Function to create a now hash_table element

Returns
hash_table_element_t object when success
NULL when no memory
size_t hash_table_get_elements ( hash_table_t table,
hash_table_element_t ***  elements 
)

Function to get all elements (key - value pairs) from the given hash table

Parameters
tablehash table from which elements have to be retrieved
elementsa pointer to an array of hash_table_element_t pointer (malloced by function)
Returns
1 when no memory
count of elements
size_t hash_table_get_keys ( hash_table_t table,
void ***  keys_ptr 
)

Function to return all the keys in a given hash table

Parameters
tablehash table from which key are to be reterived
keysa void** pointer where keys are filled in (memory allocated internally and must be freed)
Returns
total number of keys filled in keys
int hash_table_has_key ( hash_table_t table,
void *  key,
size_t  key_len 
)

Function to look if the exists in the hash table

Parameters
keypointer to key to be looked for
key_lensize of the key to be searched
Returns
0 when key is not found
1 when key is found
int hash_table_iterate ( hash_table_t table,
int(*)(void *user, void *value, void *key, size_t key_len)  fct,
void *  user 
)

Function to iterate through all elements of the hashtable

Parameters
tablehash table to be iterated
fctpointer to a function returning 1 if the element has to be removed
userarbitrary user pointer passed to the fct callback
Returns
0 when success
void* hash_table_lookup ( hash_table_t table,
void *  key,
size_t  key_len 
)

Function to lookup a key in a particular table

Note
use this macro when size of key and/or value can be given by sizeof
Parameters
tabletable to look key in
keypointer to key to be looked for
key_lensize of the key to be searched
Returns
NULL when key is not found in the hash table
void* pointer to the value in the table

Function to lookup a key in a particular table

Parameters
tabletable to look key in
keypointer to key to be looked for
key_lensize of the key to be searched
Returns
NULL when key is not found in the hash table
void* pointer to the value in the table
hash_table_t* hash_table_new ( hash_table_mode_t  mode)

Fuction to create a new hash table

Parameters
modehash_table_mode which the hash table should follow
Returns
hash_table_t object which references the hash table
NULL when no memory
int hash_table_remove ( hash_table_t table,
void *  key,
size_t  key_len 
)

Function to remove an hash table element (for a given key) from a given hash table

Parameters
tablehash table from which element has to be removed
keypointer to the key which has to be removed
key_lensize of the key in bytes
Returns
0 on sucess
-1 when key is not found
int hash_table_resize ( hash_table_t table,
size_t  len 
)

Function to resize the hash table store house

Parameters
tablehash table to be resized
lennew length of the hash table
Returns
-1 when no elements in hash table
-2 when no emmory for new store house
0 when sucess