Loading...
Searching...
No Matches
hashtable.h
Go to the documentation of this file.
#ifndef ARC_STD_HASHTABLE_H_
#define ARC_STD_HASHTABLE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
/**
* @brief the arc hashtable data type
*/
typedef struct ARC_Hashtable ARC_Hashtable;
/**
* @brief a node that contains a key-value reference along with a linked list like node
*/
typedef struct ARC_HashtableNode ARC_HashtableNode;
struct ARC_HashtableNode {
void *key;
size_t keysize;
void *data;
ARC_HashtableNode *node;
};
/**
* @brief a hashing function ptr
*
* @param key value to hash
* @param keysize should be sizeof(key) before key is a void ptr
* @param hashval value of hash, does not need to be within range of buckets
*/
typedef void (* ARC_Hashtable_Hash)(void *key, size_t *keysize, uint32_t *hashval);
/**
* @brief key comparison function ptr
*
* @param key1 first key
* @param key2 second key
*
* @return 0 when keys match
*/
typedef int8_t (* ARC_Hashtable_KeyCompare)(void *key1, size_t *key1size, void *key2, size_t *key2size);
/**
* @brief callback to allow memory freeing of nodes
*
* @param node node to be destroyed
* @param userdata any data the user wants to access in the callback
*/
typedef void (* ARC_HashtableNode_DestroyExternal)(ARC_HashtableNode *node, void *userdata);
/**
* @brief cteates ARC_Hashtable type
*
* @param htable where to store data
* @param bucketsize num of nodes to create in inital table
* @param hash hashing function to be used, if set to NULL, CRC32 will be used
* @param compare comparison functon for checking keys, if set to NULL, addresses will be compared
*/
void ARC_Hashtable_Create(ARC_Hashtable **htable, uint32_t bucketsize, ARC_Hashtable_Hash hash, ARC_Hashtable_KeyCompare compare);
/**
* @brief destroys ARC_Hashtable type
*
* @param htable htable that will be destroyed
* @param external function to allow external freeing of nodes, can be NULL
* @param userdata any data the user wants access to in the callback
*/
void ARC_Hashtable_Destroy(ARC_Hashtable *htable, ARC_HashtableNode_DestroyExternal external, void *userdata);
/**
* @brief adds value to hastable
*
* @param htable ARC_Hashtable to add to
* @param key key for node that is being added
* @param keysize sizeof key before it is passed into a void *
* @param data data for node that is being added
*/
void ARC_Hashtable_Add(ARC_Hashtable *htable, void *key, size_t keysize, void *data);
/**
* @brief gets value from hashtable by key
*
* @param htable table to get value from
* @param key key to get value from table
* @param keysize sizeof key before it is passed into a void *
* @param data data retrieved from table
*/
void ARC_Hashtable_Get(ARC_Hashtable *htable, void *key, size_t keysize, void **data);
/**
* @brief removes value from hashtable
*
* @param htable ARC_Hashtable to remove from
* @param key key of data to remove from hash table
* @param keysize sizeof key before it is passed into a void *
* @param external function to allow external freeing of data, can be NULL
* @param userdata any data the user wants access to in the callback
*/
void ARC_Hashtable_Remove(ARC_Hashtable *htable, void *key, size_t keysize, ARC_HashtableNode_DestroyExternal external, void *userdata);
#ifdef __cplusplus
}
#endif
#endif //ARC_STD_HASHTABLE_H_
ARC_Hashtable_KeyCompare
int8_t(* ARC_Hashtable_KeyCompare)(void *key1, size_t *key1size, void *key2, size_t *key2size)
key comparison function ptr
Definition hashtable.h:45
ARC_Hashtable_Destroy
void ARC_Hashtable_Destroy(ARC_Hashtable *htable, ARC_HashtableNode_DestroyExternal external, void *userdata)
destroys ARC_Hashtable type
ARC_HashtableNode_DestroyExternal
void(* ARC_HashtableNode_DestroyExternal)(ARC_HashtableNode *node, void *userdata)
callback to allow memory freeing of nodes
Definition hashtable.h:53
ARC_Hashtable_Get
void ARC_Hashtable_Get(ARC_Hashtable *htable, void *key, size_t keysize, void **data)
gets value from hashtable by key
ARC_Hashtable_Create
void ARC_Hashtable_Create(ARC_Hashtable **htable, uint32_t bucketsize, ARC_Hashtable_Hash hash, ARC_Hashtable_KeyCompare compare)
cteates ARC_Hashtable type
ARC_Hashtable
struct ARC_Hashtable ARC_Hashtable
the arc hashtable data type
Definition hashtable.h:14
ARC_Hashtable_Remove
void ARC_Hashtable_Remove(ARC_Hashtable *htable, void *key, size_t keysize, ARC_HashtableNode_DestroyExternal external, void *userdata)
removes value from hashtable
ARC_Hashtable_Add
void ARC_Hashtable_Add(ARC_Hashtable *htable, void *key, size_t keysize, void *data)
adds value to hastable
ARC_Hashtable_Hash
void(* ARC_Hashtable_Hash)(void *key, size_t *keysize, uint32_t *hashval)
a hashing function ptr
Definition hashtable.h:35
ARC_HashtableNode
Definition hashtable.h:20
ARC_HashtableNode::key
void * key
Definition hashtable.h:21
ARC_HashtableNode::node
ARC_HashtableNode * node
Definition hashtable.h:25
ARC_HashtableNode::keysize
size_t keysize
Definition hashtable.h:22
ARC_HashtableNode::data
void * data
Definition hashtable.h:24