Diffusion C API  6.7.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hash.h File Reference

A simple hashmap implementation. More...

Data Structures

struct  hash_entry_s
 This structure represents an entry within a hash table. More...
 
struct  hash_s
 This represents a hash table. More...
 

Typedefs

typedef struct hash_entry_s HASH_ENTRY_T
 This structure represents an entry within a hash table.
 
typedef struct hash_s HASH_T
 This represents a hash table.
 

Functions

HASH_Thash_new (const unsigned long slots)
 Create a new hashmap with the given number of slots. More...
 
HASH_Tunsync_hash_new (const unsigned long slots)
 Create a new unsynchronized hashmap with the given number of slots. More...
 
void hash_clear (HASH_T *hash, void(*key_free_fn)(void *), void(*val_free_fn)(void *))
 Clears all keys and values from a hashmap. More...
 
void hash_free (HASH_T *hash, void(*key_free_fn)(void *), void(*val_free_fn)(void *))
 Frees memory (including keys) associated with a hashmap. More...
 
void * hash_add (HASH_T *hash, const char *key, const void *val)
 Add a value to a hashmap with the given key. More...
 
void * hash_del (HASH_T *hash, const char *key)
 Remove a value from the hashmap. More...
 
void * hash_get (const HASH_T *hash, const char *key)
 Get a value from the hashmap. More...
 
char ** hash_keys (const HASH_T *hash)
 Obtains all keys currently in the hashmap. More...
 
HASH_Thash_dup (const HASH_T *src, void *(*dup_fn)(void *))
 Create a deep copy of a hashmap, where dup_fn() is the function used to duplicated the hash value. More...
 
HASH_Thash_dup_strval (const HASH_T *src)
 Create a deep copy of a hashmap, assuming that the values are NULL-terminated strings. More...
 

Detailed Description

A simple hashmap implementation.

Copyright © 2014, 2021 Push Technology Ltd., All Rights Reserved.

Use is subject to license terms.

NOTICE: All information contained herein is, and remains the property of Push Technology. The intellectual and technical concepts contained herein are proprietary to Push Technology and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or copyright law.

Function Documentation

void* hash_add ( HASH_T hash,
const char *  key,
const void *  val 
)

Add a value to a hashmap with the given key.

If the hashmap already contains the key, the value is replaced and the old value returned. In this case, the key in the hashmap is reused; you may need to free() the key that was passed.

Parameters
hashThe hashmap to which the key/value pair will be added.
keyThe key under which to store the value.
valThe value stored under the key.
Return values
void *If the key already exists in the hashmap the previous value is returned.
NULLIf the key cannot be found in the hashmap.
void hash_clear ( HASH_T hash,
void(*)(void *)  key_free_fn,
void(*)(void *)  val_free_fn 
)

Clears all keys and values from a hashmap.

Frees all the keys and values in a HASH_T, but does not free the hash itself.

Parameters
hashThe hashmap to be freed.
key_free_fnA function to be used to free memory associated with the key, or NULL if the keys should not be freed.
val_free_fnA function to be used to free memory associated with the value, or NULL if the values should not be freed.
void* hash_del ( HASH_T hash,
const char *  key 
)

Remove a value from the hashmap.

Parameters
hashThe hashmap from which the key/value pair will be removed.
keyThe key for the entry which is to be removed.
Return values
void *The value which was removed.
NULLIf the key was not found.
HASH_T* hash_dup ( const HASH_T src,
void *(*)(void *)  dup_fn 
)

Create a deep copy of a hashmap, where dup_fn() is the function used to duplicated the hash value.

(Hash keys are assumed to be strings). hash_free should be called on the pointer when no longer needed.

Parameters
srcThe hashmap to copy.
dup_fnThe function used to copy the hash value.
Returns
A copy of the hashmap.
HASH_T* hash_dup_strval ( const HASH_T src)

Create a deep copy of a hashmap, assuming that the values are NULL-terminated strings.

Parameters
srcThe hashmap to copy.
Returns
A copy of the hashmap.
void hash_free ( HASH_T hash,
void(*)(void *)  key_free_fn,
void(*)(void *)  val_free_fn 
)

Frees memory (including keys) associated with a hashmap.

This function can free all memory associated with a hashmap.

Parameters
hashThe hashmap to be freed.
key_free_fnA function to be used to free memory associated with the key, or NULL if the keys should not be freed.
val_free_fnA function to be used to free memory associated with the value, or NULL if the values should not be freed.
void* hash_get ( const HASH_T hash,
const char *  key 
)

Get a value from the hashmap.

Parameters
hashThe hashmap to be searched for the key.
keyThe key for which the value is to be returned.
Return values
void *The value in the hashmap associated with the key, or NULL if not found.
NULLIf the key was not found.
char** hash_keys ( const HASH_T hash)

Obtains all keys currently in the hashmap.

Parameters
hashThe hashmap to be inspected.
Returns
A NULL-terminated array of all keys in the hashmap. Call free() on the array when it is no longer required; keys are pointers to the value in the hash and should not be freed.
HASH_T* hash_new ( const unsigned long  slots)

Create a new hashmap with the given number of slots.

Parameters
slotsThe number of slots available in the hashmap. Keys hash to a slot, and if a slot already contains a key which yields the same hash, it is chained to other entries in the bucket.
Return values
HASH_T *Returns a pointer to a HASH_T structure.
NULLIf the hash cannot be created.
HASH_T* unsync_hash_new ( const unsigned long  slots)

Create a new unsynchronized hashmap with the given number of slots.

Parameters
slotsThe number of slots available in the hashmap. Keys hash to a slot, and if a slot already contains a key which yields the same hash, it is chained to other entries in the bucket.
Return values
HASH_T *Returns a pointer to a HASH_T structure.
NULLIf the hash cannot be created.