hashtable.h File Reference

Generic hashtable. More...

#include <inttypes.h>
#include "linked_list.h"

Go to the source code of this file.

Data Structures

struct  struct_hashtable_t
 A basic implementation of a hashtable. More...
struct  struct_hashtable_entry_t
 The structure of a hashtable's entry. More...

Defines

#define _TIFA_HASHTABLE_H_

Typedefs

typedef struct struct_hashtable_t hashtable_t
 Equivalent to struct struct_hashtable_t.
typedef struct
struct_hashtable_entry_t 
hashtable_entry_t
 Equivalent to struct struct_hashtable_entry_t.

Functions

hashtable_talloc_init_hashtable (uint32_t size, int(*cmp_func)(const void *const key_a, const void *const key_b), uint32_t(*hash_func)(const void *const key))
 Allocates and returns a new hashtable_t.
void free_hashtable (hashtable_t *htable)
 Clears a hashtable_t.
void add_entry_in_hashtable (hashtable_t *const htable, const void *const key, const void *const data)
 Adds an entry in a hashtable_t.
void * get_entry_in_hashtable (hashtable_t *const htable, const void *const key)
 Gets an entry's data from a hashtable_t.
void * remove_entry_in_hashtable (hashtable_t *const htable, const void *const key)
 Removes an entry from a hashtable_t.


Detailed Description

Generic hashtable.

Author:
Jerome Milan
Date:
Fri Jun 10 2011
Version:
2011-06-10
Yet another implementation of a generic hashtable.

Definition in file hashtable.h.


Define Documentation

#define _TIFA_HASHTABLE_H_

Standard include guard.

Definition at line 35 of file hashtable.h.


Function Documentation

void add_entry_in_hashtable ( hashtable_t *const   htable,
const void *const   key,
const void *const   data 
)

Adds an entry in a hashtable_t.

Creates an entry with its data field given by the pointer data and its key field given by the pointer key and adds it in a hashtable_t.

Warning:
This function does not copy the actual content of the variable referenced by key and data but merely copies these references in the hashtable_entry_t structure.
Parameters:
[in] htable A pointer to the hashtable_t.
[in] key A pointer to the key of the new entry.
[in] data A pointer to the data of the new entry.

hashtable_t* alloc_init_hashtable ( uint32_t  size,
int(*)(const void *const key_a, const void *const key_b)  cmp_func,
uint32_t(*)(const void *const key)  hash_func 
)

Allocates and returns a new hashtable_t.

Allocates and returns a pointer to a new hashtable_t with size allocated buckets, using the comparison function pointed by cmp_func and the hash function given by hash_func.

Note:
If size is not a power of two, the lowest power of two greater than size is used instead.
Parameters:
[in] size The number of allocated buckets.
[in] cmp_func A pointer to the comparison function.
[in] hash_func A pointer to the hash function.

void free_hashtable ( hashtable_t htable  ) 

Clears a hashtable_t.

Clears the hashtable_t pointed by htable.

Warning:
This function merely clears the memory used by the linked lists but does not frees the memory space used by the key and data pointers of the hashtable_entry_t.

Do not call free(htable) in client code after a call to free_hashtable(htable): it would result in an error.

Parameters:
[in] htable A pointer to the hashtable_t to clear.

Referenced by free_mpzpair_htable().

void* get_entry_in_hashtable ( hashtable_t *const   htable,
const void *const   key 
)

Gets an entry's data from a hashtable_t.

Gets the data field of the entry from the hashtable htable whose key is given by key.

Parameters:
[in] htable A pointer to the hashtable_t.
[in] key A pointer to the key of the entry to read.
Returns:
The data field of the entry whose key is given by key, if any.

NULL if no such entry is found in the hashtable.

void* remove_entry_in_hashtable ( hashtable_t *const   htable,
const void *const   key 
)

Removes an entry from a hashtable_t.

Removes the entry from the hashtable htable whose key is given by key and returns its data field.

Warning:
The key field of the removed entry is freed if a matching entry is found. The means that if key is a pointer to a structure containing some other pointers, all the memory may not be freed since the real type of key is not known by the hashtable. This is why it is strongly recommended to use only pointers to integers or strings as keys.
Parameters:
[in] htable A pointer to the hashtable_t.
[in] key A pointer to the key of the entry to remove.
Returns:
The data field of the removed entry.

NULL if no matching entry is found in the hashtable.


Generated on Fri Jun 17 11:10:12 2011 for TIFA by Doxygen 1.5.5