libdatatypes 0.3.2
Abstract datatypes for C.
Loading...
Searching...
No Matches
hashtable.c File Reference

Generic hashtable. More...

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

Go to the source code of this file.

Functions

HashTablehashtable_new (size_t size, HashFunc hash_func, EqualFunc compare_keys, FreeFunc free_key, FreeFunc free_value)
 
void hashtable_init (HashTable *table, size_t size, HashFunc hash_func, EqualFunc compare_keys, FreeFunc free_key, FreeFunc free_value)
 
void hashtable_destroy (HashTable *table)
 
void hashtable_free (HashTable *table)
 
void hashtable_clear (HashTable *table)
 
HashTableInsertResult hashtable_set (HashTable *table, void *key, void *value, bool overwrite_key)
 
void hashtable_remove (HashTable *table, const void *key)
 
HashTablePairhashtable_lookup (HashTable *table, const void *key)
 
void * hashtable_pair_get_key (const HashTablePair *pair)
 
void * hashtable_pair_get_value (const HashTablePair *pair)
 
void hashtable_pair_set_value (HashTablePair *pair, void *value)
 
bool hashtable_key_exists (const HashTable *table, const void *key)
 
size_t hashtable_count (const HashTable *table)
 
void hashtable_iter_init (const HashTable *table, HashTableIter *iter)
 
bool hashtable_iter_next (HashTableIter *iter)
 
void * hashtable_iter_get_key (const HashTableIter *iter)
 
void * hashtable_iter_get_value (const HashTableIter *iter)
 

Detailed Description

Generic hashtable.

Author
Sebastian Fedrau sebas.nosp@m.tian.nosp@m..fedr.nosp@m.au@g.nosp@m.mail..nosp@m.com

Definition in file hashtable.c.

Function Documentation

◆ hashtable_clear()

void hashtable_clear ( HashTable table)
Parameters
tablea HashTable

Removes all elements from the HashTable.

Definition at line 218 of file hashtable.c.

◆ hashtable_count()

size_t hashtable_count ( const HashTable table)
Parameters
tablea HashTable
Returns
number of stored elements

Gets the number of stored elements.

Definition at line 481 of file hashtable.c.

◆ hashtable_destroy()

void hashtable_destroy ( HashTable table)
Parameters
tablea HashTable

Frees all keys, values and the table pointer.

Definition at line 167 of file hashtable.c.

◆ hashtable_free()

void hashtable_free ( HashTable table)
Parameters
tablea HashTable

Frees all keys and values without freeing the table pointer.

Definition at line 208 of file hashtable.c.

◆ hashtable_init()

void hashtable_init ( HashTable table,
size_t  size,
HashFunc  hash_func,
EqualFunc  compare_keys,
FreeFunc  free_key,
FreeFunc  free_value 
)
Parameters
tablea HashTable
sizesize of the hash table (number of buckets), HASHTABLE_AUTO_RESIZE to grow automatically
hash_funcfunction to create hash from a key
compare_keysfunction to check equality of two keys
free_keyfunction to free keys or NULL
free_valuefunction to free values or NULL

Initializes a HashTable.

Definition at line 120 of file hashtable.c.

◆ hashtable_iter_get_key()

void * hashtable_iter_get_key ( const HashTableIter iter)
Parameters
itera HashTableIter
Returns
key of current element

Retrieves the key of the current element.

Definition at line 537 of file hashtable.c.

◆ hashtable_iter_get_value()

void * hashtable_iter_get_value ( const HashTableIter iter)
Parameters
itera HashTableIter
Returns
value of current element

Retrieves the value of the current element.

Definition at line 545 of file hashtable.c.

◆ hashtable_iter_init()

void hashtable_iter_init ( const HashTable table,
HashTableIter iter 
)
Parameters
tablea HashTable
iteran uninitialized HashTableIter

Initializes a key/value pair iterator and associates it with the table. Modifying the table while using the iterator might lead to undefined behaviour.

Definition at line 489 of file hashtable.c.

◆ hashtable_iter_next()

bool hashtable_iter_next ( HashTableIter iter)
Parameters
itera HashTableIter
Returns
false if end of the HashTable has been reached

Goes to next element of the HashTable.

Definition at line 513 of file hashtable.c.

◆ hashtable_key_exists()

bool hashtable_key_exists ( const HashTable table,
const void *  key 
)
Parameters
tablea HashTable
keykey to test
Returns
true if given key does exist

Checks if a key does exist.

Definition at line 470 of file hashtable.c.

◆ hashtable_lookup()

HashTablePair * hashtable_lookup ( HashTable table,
const void *  key 
)
Parameters
tablea HashTable
keykey to lookup
Returns
the found key-value pair or NULL

Looks up a key-value pair in the HashTable.

Definition at line 416 of file hashtable.c.

◆ hashtable_new()

HashTable * hashtable_new ( size_t  size,
HashFunc  hash_func,
EqualFunc  compare_keys,
FreeFunc  free_key,
FreeFunc  free_value 
)
Parameters
sizesize of the hash table (number of buckets), HASHTABLE_AUTO_RESIZE to grow automatically
hash_funcfunction to create hash from a key
compare_keysfunction to check equality of two keys
free_keyfunction to free keys or NULL
free_valuefunction to free values or NULL
Returns
a new HashTable

Creates a new HashTable.

Definition at line 100 of file hashtable.c.

◆ hashtable_pair_get_key()

void * hashtable_pair_get_key ( const HashTablePair pair)
Parameters
paira key-value pair
Returns
key of the pair

Retrieves the key of a key-value pair.

Definition at line 435 of file hashtable.c.

◆ hashtable_pair_get_value()

void * hashtable_pair_get_value ( const HashTablePair pair)
Parameters
paira key-value pair
Returns
value of the pair

Retrieves the value of a key-value pair.

Definition at line 445 of file hashtable.c.

◆ hashtable_pair_set_value()

void hashtable_pair_set_value ( HashTablePair pair,
void *  value 
)
Parameters
paira HashTablePair
valuenew value to set

Overwrites the value of a key-value pair.

Definition at line 455 of file hashtable.c.

◆ hashtable_remove()

void hashtable_remove ( HashTable table,
const void *  key 
)
Parameters
tablea HashTable
keykey of the element to remove

Removes an element from the HashTable.

Definition at line 367 of file hashtable.c.

◆ hashtable_set()

HashTableInsertResult hashtable_set ( HashTable table,
void *  key,
void *  value,
bool  overwrite_key 
)
Parameters
tablea HashTable
keykey to insert
valuethe value to associate with the key
overwrite_keytrue to overwrite already existing keys
Returns
type of the performed insert operation

Inserts a new key and value in the HashTable. If overwrite_key is set an existing key is freed using the specified free_key function before it gets replaced.

Definition at line 308 of file hashtable.c.