Mstdlib-1.24.0
Hashtable - String/Void Pointer

Typedefs

typedef struct M_hash_strvp M_hash_strvp_t
 
typedef struct M_hash_strvp_enum M_hash_strvp_enum_t
 

Enumerations

enum  M_hash_strvp_flags_t {
  M_HASH_STRVP_NONE = 0 ,
  M_HASH_STRVP_CASECMP = 1 << 0 ,
  M_HASH_STRVP_KEYS_UPPER = 1 << 1 ,
  M_HASH_STRVP_KEYS_LOWER = 1 << 2 ,
  M_HASH_STRVP_KEYS_ORDERED = 1 << 3 ,
  M_HASH_STRVP_KEYS_SORTASC = 1 << 4 ,
  M_HASH_STRVP_KEYS_SORTDESC = 1 << 5 ,
  M_HASH_STRVP_MULTI_VALUE = 1 << 6 ,
  M_HASH_STRVP_MULTI_GETLAST = 1 << 7 ,
  M_HASH_STRVP_STATIC_SEED = 1 << 8
}
 

Functions

M_hash_strvp_tM_hash_strvp_create (size_t size, M_uint8 fillpct, M_uint32 flags, void(*destroy_func)(void *)) M_MALLOC_ALIASED
 
void M_hash_strvp_destroy (M_hash_strvp_t *h, M_bool destroy_vals) M_FREE(1)
 
M_bool M_hash_strvp_insert (M_hash_strvp_t *h, const char *key, void *value)
 
M_bool M_hash_strvp_remove (M_hash_strvp_t *h, const char *key, M_bool destroy_vals)
 
M_bool M_hash_strvp_get (const M_hash_strvp_t *h, const char *key, void **value)
 
void * M_hash_strvp_get_direct (const M_hash_strvp_t *h, const char *key)
 
M_bool M_hash_strvp_is_multi (const M_hash_strvp_t *h)
 
M_bool M_hash_strvp_multi_len (const M_hash_strvp_t *h, const char *key, size_t *len)
 
M_bool M_hash_strvp_multi_get (const M_hash_strvp_t *h, const char *key, size_t idx, void **value)
 
void * M_hash_strvp_multi_get_direct (const M_hash_strvp_t *h, const char *key, size_t idx)
 
M_bool M_hash_strvp_multi_remove (M_hash_strvp_t *h, const char *key, size_t idx, M_bool destroy_vals)
 
M_uint32 M_hash_strvp_size (const M_hash_strvp_t *h)
 
size_t M_hash_strvp_num_collisions (const M_hash_strvp_t *h)
 
size_t M_hash_strvp_num_expansions (const M_hash_strvp_t *h)
 
size_t M_hash_strvp_num_keys (const M_hash_strvp_t *h)
 
size_t M_hash_strvp_enumerate (const M_hash_strvp_t *h, M_hash_strvp_enum_t **hashenum)
 
M_bool M_hash_strvp_enumerate_next (const M_hash_strvp_t *h, M_hash_strvp_enum_t *hashenum, const char **key, void **value)
 
void M_hash_strvp_enumerate_free (M_hash_strvp_enum_t *hashenum)
 
void M_hash_strvp_merge (M_hash_strvp_t **dest, M_hash_strvp_t *src) M_FREE(2)
 

Detailed Description

Hashtable, meant for storing string keys and void pointer values.

References to the data will always be read-only. All keys will be duplicated by the hashtable. Values will not be duplicated.

Typedef Documentation

◆ M_hash_strvp_t

typedef struct M_hash_strvp M_hash_strvp_t

◆ M_hash_strvp_enum_t

typedef struct M_hash_strvp_enum M_hash_strvp_enum_t

Enumeration Type Documentation

◆ M_hash_strvp_flags_t

Flags for controlling the behavior of the hashtable.

Enumerator
M_HASH_STRVP_NONE 

Case sensitive single value (new values replace).

M_HASH_STRVP_CASECMP 

Key compare is case insensitive.

M_HASH_STRVP_KEYS_UPPER 

Keys will be upper cased before being inserted. Should be used in conjunction with M_HASH_STRVP_CASECMP.

M_HASH_STRVP_KEYS_LOWER 

Keys will be lower cased before being inserted. Should be used in conjunction with M_HASH_STRVP_CASECMP.

M_HASH_STRVP_KEYS_ORDERED 

Keys should be ordered. Default is insertion order unless the sorted option is specified.

M_HASH_STRVP_KEYS_SORTASC 

When the keys are ordered sort them using the key_equality function.

M_HASH_STRVP_KEYS_SORTDESC 

When the keys are ordered sort them using the key_equality function.

M_HASH_STRVP_MULTI_VALUE 

Allow keys to contain multiple values. Sorted in insertion order another sorting is specified.

M_HASH_STRVP_MULTI_GETLAST 

When using get and get_direct function get the last value from the list when allowing multiple values. The default is to get the first value.

M_HASH_STRVP_STATIC_SEED 

Use a static seed for hash function initialization. This greatly reduces the security of the hashtable and removes collision attack protections. This should only be used as a performance optimization when creating millions of hashtables with static data specifically for quick look up. DO NOT use this flag with any hashtable that could store user generated data! Be very careful about duplicating a hashtable that was created with this flag. All duplicates will use the static seed.

Function Documentation

◆ M_hash_strvp_create()

M_hash_strvp_t * M_hash_strvp_create ( size_t  size,
M_uint8  fillpct,
M_uint32  flags,
void(*)(void *)  destroy_func 
)

Create a new hashtable.

The hashtable will pre-allocate an array of buckets based on the rounded up size specified. Any hash collisions will result in those collisions being chained together via a linked list. The hashtable will auto-expand by a power of 2 when the fill percentage specified is reached. All key entries are compared in a case-insensitive fashion, and are duplicated internally. Values are duplicated. Case is preserved for both keys and values.

Parameters
[in]sizeSize of the hash table. If not specified as a power of 2, will be rounded up to the nearest power of 2.
[in]fillpctThe maximum fill percentage before the hash table is expanded. If 0 is specified, the hashtable will never expand, otherwise the value must be between 1 and 99 (recommended: 75).
[in]flagsM_hash_strvp_flags_t flags for modifying behavior.
[in]destroy_funcThe function to be called to destroy value when the hashtable itself is destroyed. Can be NULL.
Returns
Allocated hashtable.
See also
M_hash_strvp_destroy

◆ M_hash_strvp_destroy()

void M_hash_strvp_destroy ( M_hash_strvp_t h,
M_bool  destroy_vals 
)

Destroy the hashtable.

Parameters
[in]hHashtable to destroy
[in]destroy_valsM_TRUE if the values held by the hashtable should be destroyed. This will almost always be M_TRUE. This should only be set to M_FALSE when all values held by the hashtable are being managed externally.

◆ M_hash_strvp_insert()

M_bool M_hash_strvp_insert ( M_hash_strvp_t h,
const char *  key,
void *  value 
)

Insert an entry into the hashtable.

Parameters
[in,out]hHashtable being referenced.
[in]keyKey to insert. A NULL or empty string is explicity disallowed.
[in]valueValue to insert into hashtable. Value will not be duplicated. The hashtable will take ownership of the value. Maybe NULL.
Returns
M_TRUE on success, or M_FALSE on failure.

◆ M_hash_strvp_remove()

M_bool M_hash_strvp_remove ( M_hash_strvp_t h,
const char *  key,
M_bool  destroy_vals 
)

Remove an entry from the hashtable.

Parameters
[in,out]hHashtable being referenced.
[in]keyKey to remove from the hashtable. A NULL or empty string is explicitly disallowed.
[in]destroy_valsM_TRUE if the value held by the hashtable should be destroyed. This will almost always be M_TRUE. This should only be set to M_FALSE when the value held by the hashtable is being managed externally.
Returns
M_TRUE on success, or M_FALSE if key does not exist.

◆ M_hash_strvp_get()

M_bool M_hash_strvp_get ( const M_hash_strvp_t h,
const char *  key,
void **  value 
)

Retrieve the value for a key from the hashtable.

Parameters
[in]hHashtable being referenced.
[in]keyKey for value. A NULL or empty string is explicitly disallowed.
[out]valuePointer to value stored in the hashtable. Optional, pass NULL if not needed.
Returns
M_TRUE if value retrieved, M_FALSE if key does not exist.

◆ M_hash_strvp_get_direct()

void * M_hash_strvp_get_direct ( const M_hash_strvp_t h,
const char *  key 
)

Retrieve the value for a key from the hashtable, and return it directly as the return value.

This cannot be used if you need to differentiate between a key that doesn't exist vs a key with a NULL value.

Parameters
[in]hHashtable being referenced.
[in]keyKey for value to retrieve from the hashtable. A NULL or empty string is explicitly disallowed.
Returns
NULL if key doesn't exist or NULL value on file, otherwise the value.

◆ M_hash_strvp_is_multi()

M_bool M_hash_strvp_is_multi ( const M_hash_strvp_t h)

Wether the hashtable a multi value table.

Parameters
[in]hHashtable being referenced.
Returns
M_TRUE if a multi value hashtable.

◆ M_hash_strvp_multi_len()

M_bool M_hash_strvp_multi_len ( const M_hash_strvp_t h,
const char *  key,
size_t *  len 
)

Get the number of values for a given key.

Parameters
[in]hHashtable being referenced.
[in]keyKey for value to retrieve.
[out]lenThe number of values.
Returns
M_TRUE if length is retrieved, M_FALSE if key does not exist.

◆ M_hash_strvp_multi_get()

M_bool M_hash_strvp_multi_get ( const M_hash_strvp_t h,
const char *  key,
size_t  idx,
void **  value 
)

Retrieve the value for a key from the given index when supporting muli-values.

Parameters
[in]hHashtable being referenced.
[in]keyKey for value to retrieve.
[in]idxThe index the value resides at.
[out]valuePointer to value stored. Optional, pass NULL if not needed.
Returns
M_TRUE if value retrieved, M_FALSE if key does not exist

◆ M_hash_strvp_multi_get_direct()

void * M_hash_strvp_multi_get_direct ( const M_hash_strvp_t h,
const char *  key,
size_t  idx 
)

Retrieve the value for a key from the given index when supporting muli-values.

Parameters
[in]hHashtable being referenced.
[in]keyKey for value to retrieve.
[in]idxThe index the value resides at.
Returns
M_TRUE if value retrieved, M_FALSE if key does not exist.

◆ M_hash_strvp_multi_remove()

M_bool M_hash_strvp_multi_remove ( M_hash_strvp_t h,
const char *  key,
size_t  idx,
M_bool  destroy_vals 
)

Remove a value from the hashtable when supporting muli-values.

If all values have been removed then the key will be removed.

Parameters
[in,out]hHashtable being referenced
[in]keyKey for value to retrieve.
[in]idxThe index the value resides at.
[in]destroy_valsM_TRUE if the value held by the hashtable should be destroyed. This will almost always be M_TRUE. This should only be set to M_FALSE when the value held by the hashtable is being managed externally.
Returns
M_TRUE if the value was removed, M_FALSE if key does not exist.

◆ M_hash_strvp_size()

M_uint32 M_hash_strvp_size ( const M_hash_strvp_t h)

Retrieve the current size (number of buckets/slots, not necessarily used).

Parameters
[in]hHashtable being referenced.
Returns
Size of the hashtable

◆ M_hash_strvp_num_collisions()

size_t M_hash_strvp_num_collisions ( const M_hash_strvp_t h)

Retrieve the number of collisions for hashtable entries that has occurred since creation.

Parameters
[in]hHashtable being referenced.
Returns
Number of collisions.

◆ M_hash_strvp_num_expansions()

size_t M_hash_strvp_num_expansions ( const M_hash_strvp_t h)

Retrieve the number of expansions/rehashes since creation.

Parameters
[in]hHashtable being referenced.
Returns
number of expansions/rehashes.

◆ M_hash_strvp_num_keys()

size_t M_hash_strvp_num_keys ( const M_hash_strvp_t h)

Retrieve the number of entries in the hashtable.

This is the number of keys stored.

Parameters
[in]hHashtable being referenced.
Returns
number of entries in the hashtable.

◆ M_hash_strvp_enumerate()

size_t M_hash_strvp_enumerate ( const M_hash_strvp_t h,
M_hash_strvp_enum_t **  hashenum 
)

Start an enumeration of the keys within a hashtable.

Parameters
[in]hHashtable being referenced.
[out]hashenumOutputs an initialized state variable for starting an enumeration.
Returns
Number of items in the hashtable
See also
M_hash_strvp_enumerate_free

◆ M_hash_strvp_enumerate_next()

M_bool M_hash_strvp_enumerate_next ( const M_hash_strvp_t h,
M_hash_strvp_enum_t hashenum,
const char **  key,
void **  value 
)

Retrieve the next item from a hashtable enumeration.

If multi-value, keys will appear multiple times as each value will be retrieved individually.

Parameters
[in]hHashtable being referenced.
[in,out]hashenumState variable for tracking the enumeration process.
[out]keyValue of next enumerated key. Optional, pass NULL if not needed.
[out]valueValue of next enumerated value. Optional, pass NULL if not needed.
Returns
M_TRUE if enumeration succeeded, M_FALSE if no more keys.

◆ M_hash_strvp_enumerate_free()

void M_hash_strvp_enumerate_free ( M_hash_strvp_enum_t hashenum)

Destroy an enumeration state.

Parameters
[in]hashenumEnumeration to destroy.

◆ M_hash_strvp_merge()

void M_hash_strvp_merge ( M_hash_strvp_t **  dest,
M_hash_strvp_t src 
)

Merge two hashtables together.

The second (src) hashtable will be destroyed automatically upon completion of this function. Any key/value pointers for the hashtable will be directly copied over to the destination hashtable, they will not be duplicated. Any keys which exist in 'dest' that also exist in 'src' will be overwritten by the 'src' value.

If dest and src are multi-value, all values from src will be copied into dest and the values from dest will not be removed. If dest is not multi-value and src is, then only the last value in src will be present in dest. If dest is multi-value and src is not, then the value from src will be added to dest.

Parameters
[in,out]destPointer by reference to the hashtable receiving the key/value pairs. if dest is NULL, the src address will simply be copied to dest.
[in,out]srcPointer to the hashtable giving up its key/value pairs.