updated hashtable and started on basic hashtable testing
This commit is contained in:
parent
122eb1d351
commit
c344594af7
7 changed files with 453 additions and 187 deletions
|
|
@ -5,105 +5,115 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "arc/std/bool.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* @brief the arc hashtable data type
|
||||
* @brief a hashing function ptr
|
||||
*
|
||||
* @param[in] key value to hash
|
||||
*
|
||||
* @return uint32_t hashed value of the tkey
|
||||
*/
|
||||
typedef uint32_t (* ARC_Hashtable_HashFn)(void *key);
|
||||
|
||||
/**
|
||||
* @brief the hashtable key comparison callback function
|
||||
*
|
||||
* @param[in] key1 first key
|
||||
* @param[in] key2 second key
|
||||
*
|
||||
* @return ARC_True when keys match
|
||||
*/
|
||||
typedef ARC_Bool (* ARC_Hashtable_KeyCompareFn)(void *key1, void *key2);
|
||||
|
||||
/**
|
||||
* @brief a callback that cleans up a key and value's memory when it is removed from the hashtable
|
||||
*
|
||||
* @param[in] data the key to destroy
|
||||
*/
|
||||
typedef void (* ARC_Hashtable_DestroyKeyValueFn)(void *key, void *value);
|
||||
|
||||
/**
|
||||
* @brief a callback to be used by ARC_Hashtable_RunIteration
|
||||
*
|
||||
* @param[in] key a key at the current iteration
|
||||
* @param[in] value a value that matches the key at the current iteration
|
||||
*/
|
||||
typedef void (* ARC_Hashtable_IteratorFn)(void *key, void *value);
|
||||
|
||||
/**
|
||||
* @brief a resizable hashtable data type (will find next open slot before resizing)
|
||||
*/
|
||||
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
|
||||
* @note if the default hashing function is used (CRC32), then the key value needs to be a string or end in '\0'
|
||||
* @note an error will be thrown if the key is NULL
|
||||
*
|
||||
* @param[out] hashtable ARC_Hashtable to initialize
|
||||
* @param[in] hashFn a callback for a hashing function to be used, if set to NULL, CRC32 will be used
|
||||
* @param[in] compareFn a callback for checking keys, if set to NULL, addresses will be compared
|
||||
* @param[in] destroyKeyValueFn a callback to free the key and value
|
||||
*/
|
||||
void ARC_Hashtable_Create(ARC_Hashtable **htable, uint32_t bucketsize, ARC_Hashtable_Hash hash, ARC_Hashtable_KeyCompare compare);
|
||||
void ARC_Hashtable_Create(ARC_Hashtable **hashtable, ARC_Hashtable_HashFn *hashFn, ARC_Hashtable_KeyCompareFn *keyCompareFn, ARC_Hashtable_DestroyKeyValueFn *destroyKeyValueFn);
|
||||
|
||||
/**
|
||||
* @brief destroys ARC_Hashtable type
|
||||
* @brief destroys an ARC_Hashtable
|
||||
*
|
||||
* @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
|
||||
* @param[in] hashtable ARC_Hashtable to free
|
||||
*/
|
||||
void ARC_Hashtable_Destroy(ARC_Hashtable *htable, ARC_HashtableNode_DestroyExternal external, void *userdata);
|
||||
void ARC_Hashtable_Destroy(ARC_Hashtable *hashtable);
|
||||
|
||||
/**
|
||||
* @brief adds value to hastable
|
||||
* @brief adds a value at a key 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
|
||||
* @note this will error if you add more than 4,294,967,295 items (the max value of an unsigned int 32)
|
||||
*
|
||||
* @param[in] hashtable ARC_Hashtable to add to
|
||||
* @param[in] key key for node that is being added
|
||||
* @param[in] value value for node that is being added
|
||||
*/
|
||||
void ARC_Hashtable_Add(ARC_Hashtable *htable, void *key, size_t keysize, void *data);
|
||||
void ARC_Hashtable_Add(ARC_Hashtable *hashtable, void *key, void *value);
|
||||
|
||||
/**
|
||||
* @brief gets value from hashtable by key
|
||||
* @brief removes value from hashtable at a given 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
|
||||
* @param[in] hashtable ARC_Hashtable to remove from
|
||||
* @param[in] key key of data to remove from the hashtable
|
||||
*/
|
||||
void ARC_Hashtable_Get(ARC_Hashtable *htable, void *key, size_t keysize, void **data);
|
||||
void ARC_Hashtable_Remove(ARC_Hashtable *hashtable, void *key);
|
||||
|
||||
/**
|
||||
* @brief removes value from hashtable
|
||||
* @brief clears all values from a 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
|
||||
* @param[in] hashtable ARC_Hashtable to clear
|
||||
*/
|
||||
void ARC_Hashtable_Remove(ARC_Hashtable *htable, void *key, size_t keysize, ARC_HashtableNode_DestroyExternal external, void *userdata);
|
||||
void ARC_Hashtable_Clear(ARC_Hashtable *hashtable);
|
||||
|
||||
/**
|
||||
* @brief gets a value from hashtable by key
|
||||
*
|
||||
* @param[in] hashtable the hashtable to get the value from
|
||||
* @param[in] key the key to match against for a value
|
||||
*
|
||||
* @return the value if there is a key match, otherwise NULL
|
||||
*/
|
||||
void *ARC_Hashtable_Get(ARC_Hashtable *hashtable, void *key);
|
||||
|
||||
/**
|
||||
* @brief iterates through a hashtable passing available key value pairs to a callback
|
||||
*
|
||||
* @param[in] hashtable the hashtable to iterate through
|
||||
* @param[in] iteratorFn the callback which will can use the iterated key value pairs
|
||||
*/
|
||||
void ARC_Hashtable_RunIteration(ARC_Hashtable *hashtable, ARC_Hashtable_IteratorFn iteratorFn);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //ARC_STD_HASHTABLE_H_
|
||||
#endif //ARC_STD_HASHTABLE_H_
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue