libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
|
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include "sysmacros.h"
#include "types.h"
#include "list.h"
Go to the source code of this file.
Data Structures | |
struct | htbl_t |
struct | htbl2_t |
Macros | |
#define | HTBL_VALUE_MULTI(x) htbl_value_multi(x) |
Typedefs | |
typedef struct htbl2_multi_value_s | htbl2_multi_value_t |
Functions | |
void | htbl_create (htbl_t *htbl, size_t tbl_sz, size_t key_sz, bool_t multi_value) |
void | htbl_destroy (htbl_t *htbl) |
void | htbl2_create (htbl2_t *htbl, size_t tbl_sz, size_t key_sz, size_t value_sz, bool multi_value) |
void | htbl2_destroy (htbl2_t *htbl) |
void | htbl_empty (htbl_t *htbl, void(*func)(void *value, void *userinfo), void *userinfo) |
void | htbl2_empty (htbl2_t *htbl, size_t value_sz, void(*func)(void *value, void *userinfo), void *userinfo) |
size_t | htbl_count (const htbl_t *htbl) |
size_t | htbl2_count (const htbl2_t *htbl) |
void | htbl_set (htbl_t *htbl, const void *key, void *value) |
void | htbl2_set (htbl2_t *htbl, const void *key, size_t key_sz, void *value, size_t value_sz) |
void | htbl_remove (htbl_t *htbl, const void *key, bool_t nil_ok) |
void | htbl2_remove (htbl2_t *htbl, const void *key, size_t key_sz, bool nil_ok) |
void | htbl_remove_multi (htbl_t *htbl, const void *key, htbl_multi_value_t *list_item) |
void | htbl2_remove_multi (htbl2_t *htbl, const void *key, size_t key_sz, htbl2_multi_value_t *list_item) |
void * | htbl_lookup (const htbl_t *htbl, const void *key) |
void * | htbl2_lookup (const htbl2_t *htbl, const void *key, size_t key_sz, size_t value_sz) |
const list_t * | htbl_lookup_multi (const htbl_t *htbl, const void *key) |
const list_t * | htbl2_lookup_multi (const htbl2_t *htbl, const void *key, size_t key_sz) |
void * | htbl_value_multi (const htbl_multi_value_t *mv) |
void * | htbl2_value_multi (const htbl2_multi_value_t *mv, size_t value_sz) |
void | htbl_foreach (const htbl_t *htbl, void(*func)(const void *key, void *value, void *userinfo), void *userinfo) |
void | htbl2_foreach (const htbl2_t *htbl, size_t key_sz, size_t value_sz, void(*func)(const void *key, void *value, void *userinfo), void *userinfo) |
char * | htbl_dump (const htbl_t *htbl, bool_t printable_keys) |
static void | htbl_free (void *obj, void *unused) |
This module implements a simple general-purpose hash table.
Definition in file htbl.h.
#define HTBL_VALUE_MULTI | ( | x | ) | htbl_value_multi(x) |
Legacy backwards compatibility macro that just invokes htbl_value_multi().
void htbl2_create | ( | htbl2_t * | htbl, |
size_t | tbl_sz, | ||
size_t | key_sz, | ||
size_t | value_sz, | ||
bool | multi_value | ||
) |
void htbl2_empty | ( | htbl2_t * | htbl, |
size_t | value_sz, | ||
void(*)(void *value, void *userinfo) | func, | ||
void * | userinfo | ||
) |
void htbl2_foreach | ( | const htbl2_t * | htbl, |
size_t | key_sz, | ||
size_t | value_sz, | ||
void(*)(const void *key, void *value, void *userinfo) | func, | ||
void * | userinfo | ||
) |
void * htbl2_lookup | ( | const htbl2_t * | htbl, |
const void * | key, | ||
size_t | key_sz, | ||
size_t | value_sz | ||
) |
void htbl2_remove | ( | htbl2_t * | htbl, |
const void * | key, | ||
size_t | key_sz, | ||
bool | nil_ok | ||
) |
void htbl2_remove_multi | ( | htbl2_t * | htbl, |
const void * | key, | ||
size_t | key_sz, | ||
htbl2_multi_value_t * | list_item | ||
) |
void htbl2_set | ( | htbl2_t * | htbl, |
const void * | key, | ||
size_t | key_sz, | ||
void * | value, | ||
size_t | value_sz | ||
) |
void * htbl2_value_multi | ( | const htbl2_multi_value_t * | mv, |
size_t | value_sz | ||
) |
size_t htbl_count | ( | const htbl_t * | htbl | ) |
void htbl_create | ( | htbl_t * | htbl, |
size_t | tbl_sz, | ||
size_t | key_sz, | ||
bool_t | multi_value | ||
) |
Initializes a new hash table.
htbl | Pointer to the hash table which is to be initialized. |
tbl_sz | Hash table size. Once initialized, hash tables retain a fixed size and cannot be changed, so pick a size wisely here. The size affects the hash table's memory-vs-time performance. The bigger the hash table, the less of a chance of hash collisions requiring an extensive search, but that also uses more memory. A good rule of thumb is to size the hash table to double the maximum number of elements expected to be in the hash table. |
key_sz | Size of the hash keys to be used in the hash table. This will be the size of the key objects used in htbl_lookup(), htbl_set() and similar. |
multi_value | Controls whether the hash table will support duplicate values. This then changes how you need to look up entries inside of the hash table (see htbl_lookup() and htbl_lookup_multi()). |
void htbl_destroy | ( | htbl_t * | htbl | ) |
Deinitializes a hash table which was initialized using htbl_create(). The hash table must be empty at the time that htbl_destroy() is called. If you want to quickly empty the entire hash table, see htbl_empty().
char * htbl_dump | ( | const htbl_t * | htbl, |
bool_t | printable_keys | ||
) |
void htbl_empty | ( | htbl_t * | htbl, |
void(*)(void *value, void *userinfo) | func, | ||
void * | userinfo | ||
) |
Removes all entries from a hash table. This can be used to either reset the hash table to a clean, or to prepare the hash table for deinitialization via htbl_destroy().
func | Optional callback, which will be invoked for every element in the hash table, as it's being removed. You can use this to free memory of the table values, or to deinitialize them in some way. The first argument to the function will be the hash table value, while the second one will be the userinfo you provide in this call. If you do not want to use this callback, pass NULL here. If your values only need to be freed using the standard free() function, you can pass htbl_free() in this this argument and setting userinfo to NULL. This is a simple utility function which calls free() using your own heap allocator on every value it receives as part of the htbl_empty() call. |
userinfo | Optional argument, which will be passed to the func callback above in the second argument. |
void htbl_foreach | ( | const htbl_t * | htbl, |
void(*)(const void *key, void *value, void *userinfo) | func, | ||
void * | userinfo | ||
) |
Walks the hash table, iterating over each value in the table. It is safe to add or remove entries from the hash table from within the callback, although the walk may not pass over the newly added values.
htbl | The hash table to walk. |
func | A callback which will be invoked for every value stored in the hash table. The first and second arguments to the callback will be set to the key and value respectively. The third argument is the value of the userinfo parameter. |
userinfo | Optional parameter which will be passed on to the func argument in the third parameter. |
|
static |
Utility function that can be passed in the second argument of htbl_empty() if your values only require a standard C free()
call using your own heap allocator. You can use this as a shorthand to freeing all the values in a hash table, instead of having to write a custom callback every time.
free()
and does no other deinitialization of the memory pointed to by the hash table value. Do NOT use this if your values need more complex deinitialization. void * htbl_lookup | ( | const htbl_t * | htbl, |
const void * | key | ||
) |
Performs a hash table lookup. The hash table must have been initialized with multi_value
set to B_FALSE
in htbl_create().
key | The key under which the value was stored. This must point to a memory buffer of size key_sz as passed in the initial htbl_create() call. |
value
which was stored under the key, if it exists in the hash table. If no value was stored under key
, this returns NULL instead. Performs a hash table lookup. The hash table must have been initialized with multi_value
set to B_TRUE
in htbl_create().
key | The key under which the value was stored. This must point to a memory buffer of size key_sz as passed in the initial htbl_create() call. |
key
, this function returns a list_t
of htbl_multi_value_t
structures. You must not modify any of the fields of the returned structure, only access the value stored within using htbl_value_multi(). key
, returns NULL. void htbl_remove | ( | htbl_t * | htbl, |
const void * | key, | ||
bool_t | nil_ok | ||
) |
Removes a value from a hash table. If the hash table was created with multi_value
set to B_TRUE
in htbl_create(), then this will remove all values stored under key
. If you want more fine-grained control over which values to remove in a multi-value table, use htbl_remove_multi().
key | The key under which the value was stored. This must point to a memory buffer of size key_sz as passed in the initial htbl_create() call. |
nil_ok | If set to B_TRUE , the value not existing in the hash table is considered OK. If set to B_FALSE , if the value in the hash table doesn't exist, an assertion failure is triggered. |
void htbl_remove_multi | ( | htbl_t * | htbl, |
const void * | key, | ||
htbl_multi_value_t * | list_item | ||
) |
Removes a single value from a multi-value enabled hash table. The normal htbl_remove() function removes all values paired to a single key, so this function gives you finer control to only remove specific values.
key | The key for which to remove the value. |
list_item | The htbl_multi_value_t as contained in the list returned by htbl_lookup_multi(). |
void htbl_set | ( | htbl_t * | htbl, |
const void * | key, | ||
void * | value | ||
) |
Stores a new value in a hash table. If the value already exists, what happens depends on whether the hash table was created with multi_value
set to B_TRUE
or B_FALSE
in htbl_create():
key | The hash key under which to store the value. This must point to a memory buffer of size key_sz as passed in the initial htbl_create() call. The key itself is copied into the hash table, so you don't have to keep the memory pointed at by key allocated after the call to htbl_set() returns. |
value | The value which will be stored under key . Hash table values are stored by reference, so be sure not to de-allocate object pointed to by value , if you need to dereference the stored pointer. |
void * htbl_value_multi | ( | const htbl_multi_value_t * | mv | ) |