Mstdlib-1.24.0
Hashtable - String/Index

Typedefs

typedef struct M_hash_stridx M_hash_stridx_t
 
typedef struct M_hash_stridx_enum M_hash_stridx_enum_t
 

Enumerations

enum  M_hash_stridx_flags_t {
  M_HASH_STRIDX_NONE = 0 ,
  M_HASH_STRIDX_CASECMP = 1 << 0 ,
  M_HASH_STRIDX_KEYS_UPPER = 1 << 1 ,
  M_HASH_STRIDX_KEYS_LOWER = 1 << 2 ,
  M_HASH_STRIDX_KEYS_ORDERED = 1 << 3 ,
  M_HASH_STRIDX_KEYS_SORTASC = 1 << 4 ,
  M_HASH_STRIDX_KEYS_SORTDESC = 1 << 5 ,
  M_HASH_STRIDX_MULTI_VALUE = 1 << 6 ,
  M_HASH_STRIDX_MULTI_GETLAST = 1 << 7 ,
  M_HASH_STRIDX_STATIC_SEED = 1 << 8
}
 

Functions

M_hash_stridx_tM_hash_stridx_create (size_t size, M_uint8 fillpct, M_uint32 flags) M_MALLOC
 
void M_hash_stridx_destroy (M_hash_stridx_t *h) M_FREE(1)
 
M_bool M_hash_stridx_insert (M_hash_stridx_t *h, const char *key, size_t value)
 
M_bool M_hash_stridx_remove (M_hash_stridx_t *h, const char *key)
 
M_bool M_hash_stridx_get (const M_hash_stridx_t *h, const char *key, size_t *value)
 
size_t M_hash_stridx_get_direct (const M_hash_stridx_t *h, const char *key)
 
M_bool M_hash_stridx_is_multi (const M_hash_stridx_t *h)
 
M_bool M_hash_stridx_multi_len (const M_hash_stridx_t *h, const char *key, size_t *len)
 
M_bool M_hash_stridx_multi_get (const M_hash_stridx_t *h, const char *key, size_t idx, size_t *value)
 
size_t M_hash_stridx_multi_get_direct (const M_hash_stridx_t *h, const char *key, size_t idx)
 
M_bool M_hash_stridx_multi_remove (M_hash_stridx_t *h, const char *key, size_t idx)
 
M_uint32 M_hash_stridx_size (const M_hash_stridx_t *h)
 
size_t M_hash_stridx_num_collisions (const M_hash_stridx_t *h)
 
size_t M_hash_stridx_num_expansions (const M_hash_stridx_t *h)
 
size_t M_hash_stridx_num_keys (const M_hash_stridx_t *h)
 
size_t M_hash_stridx_enumerate (const M_hash_stridx_t *h, M_hash_stridx_enum_t **hashenum)
 
M_bool M_hash_stridx_enumerate_next (const M_hash_stridx_t *h, M_hash_stridx_enum_t *hashenum, const char **key, size_t *value)
 
void M_hash_stridx_enumerate_free (M_hash_stridx_enum_t *hashenum)
 
void M_hash_stridx_merge (M_hash_stridx_t **dest, M_hash_stridx_t *src) M_FREE(2)
 
M_hash_stridx_tM_hash_stridx_duplicate (const M_hash_stridx_t *h) M_MALLOC
 

Detailed Description

Hashtable, meant for storing string keys and index values. This is primarily used for something like dereferencing a CSV column name to its position.

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

Typedef Documentation

◆ M_hash_stridx_t

typedef struct M_hash_stridx M_hash_stridx_t

◆ M_hash_stridx_enum_t

typedef struct M_hash_stridx_enum M_hash_stridx_enum_t

Enumeration Type Documentation

◆ M_hash_stridx_flags_t

Flags for controlling the behavior of the hashtable.

Enumerator
M_HASH_STRIDX_NONE 

Case sensitive single value (new values replace).

M_HASH_STRIDX_CASECMP 

Key compare is case insensitive.

M_HASH_STRIDX_KEYS_UPPER 

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

M_HASH_STRIDX_KEYS_LOWER 

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

M_HASH_STRIDX_KEYS_ORDERED 

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

M_HASH_STRIDX_KEYS_SORTASC 

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

M_HASH_STRIDX_KEYS_SORTDESC 

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

M_HASH_STRIDX_MULTI_VALUE 

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

M_HASH_STRIDX_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_STRIDX_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_stridx_create()

M_hash_stridx_t * M_hash_stridx_create ( size_t  size,
M_uint8  fillpct,
M_uint32  flags 
)

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_stridx_flags_t flags for modifying behavior.
Returns
Allocated hashtable.
See also
M_hash_stridx_destroy

◆ M_hash_stridx_destroy()

void M_hash_stridx_destroy ( M_hash_stridx_t h)

Destroy the hashtable.

Parameters
[in]hHashtable to destroy

◆ M_hash_stridx_insert()

M_bool M_hash_stridx_insert ( M_hash_stridx_t h,
const char *  key,
size_t  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.
Returns
M_TRUE on success, or M_FALSE on failure.

◆ M_hash_stridx_remove()

M_bool M_hash_stridx_remove ( M_hash_stridx_t h,
const char *  key 
)

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.
Returns
M_TRUE on success, or M_FALSE if key does not exist.

◆ M_hash_stridx_get()

M_bool M_hash_stridx_get ( const M_hash_stridx_t h,
const char *  key,
size_t *  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_stridx_get_direct()

size_t M_hash_stridx_get_direct ( const M_hash_stridx_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 0 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_stridx_is_multi()

M_bool M_hash_stridx_is_multi ( const M_hash_stridx_t h)

Wether the hashtable a multi value table.

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

◆ M_hash_stridx_multi_len()

M_bool M_hash_stridx_multi_len ( const M_hash_stridx_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_stridx_multi_get()

M_bool M_hash_stridx_multi_get ( const M_hash_stridx_t h,
const char *  key,
size_t  idx,
size_t *  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_stridx_multi_get_direct()

size_t M_hash_stridx_multi_get_direct ( const M_hash_stridx_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_stridx_multi_remove()

M_bool M_hash_stridx_multi_remove ( M_hash_stridx_t h,
const char *  key,
size_t  idx 
)

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.
Returns
M_TRUE if the value was removed, M_FALSE if key does not exist.

◆ M_hash_stridx_size()

M_uint32 M_hash_stridx_size ( const M_hash_stridx_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_stridx_num_collisions()

size_t M_hash_stridx_num_collisions ( const M_hash_stridx_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_stridx_num_expansions()

size_t M_hash_stridx_num_expansions ( const M_hash_stridx_t h)

Retrieve the number of expansions/rehashes since creation.

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

◆ M_hash_stridx_num_keys()

size_t M_hash_stridx_num_keys ( const M_hash_stridx_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_stridx_enumerate()

size_t M_hash_stridx_enumerate ( const M_hash_stridx_t h,
M_hash_stridx_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_stridx_enumerate_free

◆ M_hash_stridx_enumerate_next()

M_bool M_hash_stridx_enumerate_next ( const M_hash_stridx_t h,
M_hash_stridx_enum_t hashenum,
const char **  key,
size_t *  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_stridx_enumerate_free()

void M_hash_stridx_enumerate_free ( M_hash_stridx_enum_t hashenum)

Destroy an enumeration state.

Parameters
[in]hashenumEnumeration to destroy.

◆ M_hash_stridx_merge()

void M_hash_stridx_merge ( M_hash_stridx_t **  dest,
M_hash_stridx_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.

◆ M_hash_stridx_duplicate()

M_hash_stridx_t * M_hash_stridx_duplicate ( const M_hash_stridx_t h)

Duplicate an existing hashtable.

Copying all keys and values.

Parameters
[in]hHashtable to be copied.
Returns
Duplicated hashtable.