5 #ifndef CGDS_HASH_TABLE_H
6 #define CGDS_HASH_TABLE_H
10 #include "cgds/safe_alloc.h"
11 #include "cgds/types.h"
14 * @brief Cell of a dictionary.
16 typedef struct HashCell
{
17 char* key
; ///< Key (as a string).
18 void* data
; ///< Generic data contained in this cell.
19 struct HashCell
* next
; ///< Pointer to next cell in the list.
23 * @brief Generic dictionary string --> any data.
25 typedef struct HashTable
{
26 UInt size
; ///< Count elements in the dictionary.
27 size_t dataSize
; ///< Size of a dict cell element in bytes.
28 size_t hashSize
; ///< (Maximum) Number of stored hash keys.
29 HashCell
** head
; ///< Pointers to the first cell in a list.
33 * @brief Initialize an empty dictionary.
36 HashTable
* hashTable
, ///< "this" pointer.
37 size_t dataSize
, ///< Size in bytes of a dictionary element.
38 size_t hashSize
///< (Maximum) Number of stored hash keys.
42 * @brief Return an allocated and initialized dictionary.
44 HashTable
* _hashtable_new(
45 size_t dataSize
, ///< Size in bytes of a dictionary element.
46 size_t hashSize
///< (Maximum) Number of stored hash keys.
50 * @brief Return an allocated and initialized vector.
51 * @param type Type of a vector element (int, char*, ...).
53 * Usage: HashTable* hashtable_new(<Type> type, UInt hash_size)
55 #define hashtable_new(type, hsize) \
56 _hashtable_new(sizeof(type), hsize)
59 * @brief Copy constructor (shallow copy, ok for basic types).
61 HashTable
* hashtable_copy(
62 HashTable
* hashTable
///< "this" pointer.
66 * @brief Check if the dictionary is empty.
69 HashTable
* hastTable
///< "this" pointer.
73 * @brief Return current size.
76 HashTable
* hastTable
///< "this" pointer.
80 * @brief Lookup element of given key.
83 HashTable
* hashTable
, ///< "this" pointer.
84 char* key
///< Key of the element to retrieve.
88 * @brief Lookup element of given key.
89 * @param hashTable "this" pointer.
90 * @param key Key of the element to retrieve..
91 * @param data 'out' variable (ptr) to contain the result.
93 * Usage: void hashtable_get(HashTable* hashTable, char* key, void data)
95 #define hashtable_get(hashTable, key, data) \
97 data = (typeof(data))_hashtable_get(hashTable, key); \
101 * @brief Add the entry (key, value) to dictionary.
104 HashTable
* hashTable
, ///< "this" pointer.
105 char* key
, ///< Key of the element to add or modify.
106 void* data
///< Pointer to new data at given key.
110 * @brief Add the entry (key, value) to dictionary.
111 * @param hashTable "this" pointer.
112 * @param key Key of the element to add or modify.
113 * @param data New data at given key.
115 * Usage: void hashtable_set(HashTable* hashTable, char* key, void data)
117 #define hashtable_set(hashTable, key, data) \
119 typeof(data) tmp = data; \
120 _hashtable_set(hashTable, key, &tmp); \
124 * @brief Remove the given key (+ associated value).
126 void hashtable_delete(
127 HashTable
* hashTable
, ///< "this" pointer.
128 char* key
///< Key of the element to delete.
132 * @brief Clear the entire dictionary.
134 void hashtable_clear(
135 HashTable
* hashTable
///< "this" pointer.
139 * @brief Destroy the dictionary: clear it, and free hashes array.
141 void hashtable_destroy(
142 HashTable
* hashTable
///< "this" pointer.