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

A simple hash with an numerical key implementation. More...

Data Structures

struct  hash_num_entry_s
 This structure represents an entry within a hash table. More...
 
struct  hash_num_s
 This represents a hash table. More...
 

Typedefs

typedef struct hash_num_entry_s HASH_NUM_ENTRY_T
 This structure represents an entry within a hash table.
 
typedef struct hash_num_s HASH_NUM_T
 This represents a hash table.
 

Functions

HASH_NUM_Thash_num_new (const unsigned long slots)
 Create a new hash with numerical keys. More...
 
HASH_NUM_Tunsync_hash_num_new (const unsigned long slots)
 Create a new unsynchronized hash with numerical keys. More...
 
void hash_num_clear (HASH_NUM_T *hash, void(*val_free_fn)(void *))
 Clears all keys and values from a hash. More...
 
void hash_num_free (HASH_NUM_T *hash, void(*val_free_fn)(void *))
 Frees memory associated with a hash. More...
 
void * hash_num_add (HASH_NUM_T *hash, unsigned long key, const void *val)
 Add a value to a hash with the given key. More...
 
void * hash_num_del (HASH_NUM_T *hash, unsigned long key)
 Remove a value from the hashmap. More...
 
void * hash_num_get (const HASH_NUM_T *hash, unsigned long key)
 Get a value from the hashmap. More...
 
unsigned long * hash_num_keys (const HASH_NUM_T *hash)
 Obtains all keys currently in the hashmap. More...
 
HASH_NUM_Thash_num_dup (const HASH_NUM_T *src, void *(*fn)())
 Create a deep copy of a hash hash_num_free should be called on the pointer when no longer needed. More...
 
HASH_NUM_Thash_num_dup_strval (const HASH_NUM_T *src)
 Create a deep copy of a hash, assuming that the values are NULL-terminated strings. More...
 

Detailed Description

A simple hash with an numerical key implementation.

Copyright © 2020 - 2023 DiffusionData Ltd., All Rights Reserved.

Use is subject to licence terms.

NOTICE: All information contained herein is, and remains the property of DiffusionData. The intellectual and technical concepts contained herein are proprietary to DiffusionData 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_num_add ( HASH_NUM_T hash,
unsigned long  key,
const void *  val 
)

Add a value to a hash with the given key.

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

Parameters
hashThe hash 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 hash the previous value is returned.
NULLIf the key cannot be found in the hash.
void hash_num_clear ( HASH_NUM_T hash,
void(*)(void *)  val_free_fn 
)

Clears all keys and values from a hash.

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

Parameters
hashThe hash to 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_num_del ( HASH_NUM_T hash,
unsigned long  key 
)

Remove a value from the hashmap.

Parameters
hashThe hash 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_NUM_T* hash_num_dup ( const HASH_NUM_T src,
void *(*)()  fn 
)

Create a deep copy of a hash hash_num_free should be called on the pointer when no longer needed.

Parameters
srcThe hash to copy.
fnThe function used to copy the hash value.
Returns
A copy of the hashmap.
HASH_NUM_T* hash_num_dup_strval ( const HASH_NUM_T src)

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

Parameters
srcThe hash to copy.
Returns
A copy of the hashmap.
void hash_num_free ( HASH_NUM_T hash,
void(*)(void *)  val_free_fn 
)

Frees memory associated with a hash.

This function can free all memory associated with a hash.

Parameters
hashThe hash to 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_num_get ( const HASH_NUM_T hash,
unsigned long  key 
)

Get a value from the hashmap.

Parameters
hashThe hash 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.
unsigned long* hash_num_keys ( const HASH_NUM_T hash)

Obtains all keys currently in the hashmap.

Parameters
hashThe hash to be inspected.
Returns
A array of all keys in the hashmap. Call free() on the array when it is no longer required;
HASH_NUM_T* hash_num_new ( const unsigned long  slots)

Create a new hash with numerical keys.

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_NUM_T *Returns a pointer to a HASH_NUM_T structure.
NULLIf the hash cannot be created.
HASH_NUM_T* unsync_hash_num_new ( const unsigned long  slots)

Create a new unsynchronized hash with numerical keys.

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_NUM_T *Returns a pointer to a HASH_NUM_T structure.
NULLIf the hash cannot be created.