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

a simple hash table implementation More...

#include "hashtable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Functions

hash_table_element_thash_table_element_new ()
 
void hash_table_element_delete (hash_table_t *table, hash_table_element_t *element)
 
hash_table_thash_table_new (hash_table_mode_t mode)
 
void hash_table_delete (hash_table_t *table)
 
int hash_table_add (hash_table_t *table, void *key, size_t key_len, void *value, size_t value_len)
 
int hash_table_remove (hash_table_t *table, void *key, size_t key_len)
 
void * hash_table_lookup (hash_table_t *table, void *key, size_t key_len)
 
int hash_table_has_key (hash_table_t *table, void *key, size_t key_len)
 
size_t hash_table_get_keys (hash_table_t *table, void ***keys_ptr)
 
size_t hash_table_get_elements (hash_table_t *table, hash_table_element_t ***elements)
 
uint16_t hash_table_do_hash (void *key, size_t key_len, uint16_t max_key)
 
int hash_table_resize (hash_table_t *table, size_t len)
 
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

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

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