5 #ifndef CGDS_HASH_TABLE_H
6 #define CGDS_HASH_TABLE_H
10 #include "cgds/safe_alloc.h"
11 #include "cgds/types.h"
12 #include "cgds/Vector.h"
15 * @brief Cell of a dictionary.
17 typedef struct HashCell
{
18 char* key
; ///< Key (as a string).
19 void* data
; ///< Generic data contained in this cell.
20 struct HashCell
* next
; ///< Pointer to next cell in the list.
24 * @brief Generic dictionary string --> any data.
26 typedef struct HashTable
{
27 UInt size
; ///< Count elements in the dictionary.
28 size_t dataSize
; ///< Size of a dict cell element in bytes.
29 size_t hashSize
; ///< (Maximum) Number of stored hash keys.
30 HashCell
** head
; ///< Pointers to the first cell in a list.
34 * @brief Initialize an empty dictionary.
37 HashTable
* hashTable
, ///< "this" pointer.
38 size_t dataSize
, ///< Size in bytes of a dictionary element.
39 size_t hashSize
///< (Maximum) Number of stored hash keys.
43 * @brief Return an allocated and initialized dictionary.
45 HashTable
* _hashtable_new(
46 size_t dataSize
, ///< Size in bytes of a dictionary element.
47 size_t hashSize
///< (Maximum) Number of stored hash keys.
51 * @brief Return an allocated and initialized vector.
52 * @param type Type of a vector element (int, char*, ...).
54 * Usage: HashTable* hashtable_new(<Type> type, UInt hash_size)
56 #define hashtable_new(type, hsize) \
57 _hashtable_new(sizeof(type), hsize)
60 * @brief Copy constructor (shallow copy, ok for basic types).
62 HashTable
* hashtable_copy(
63 HashTable
* hashTable
///< "this" pointer.
67 * @brief Check if the dictionary is empty.
70 HashTable
* hastTable
///< "this" pointer.
74 * @brief Return current size.
77 HashTable
* hastTable
///< "this" pointer.
81 * @brief Lookup element of given key.
84 HashTable
* hashTable
, ///< "this" pointer.
85 char* key
///< Key of the element to retrieve.
89 * @brief Lookup element of given key.
90 * @param hashTable "this" pointer.
91 * @param key Key of the element to retrieve..
92 * @param data 'out' variable to contain the result.
94 * Usage: void hashtable_get(HashTable* hashTable, char* key, void data)
96 #define hashtable_get(hashTable, key, data) \
98 void* pData = _hashtable_get(hashTable, key); \
99 data = *((typeof(&data))pData); \
103 * @brief Add the entry (key, value) to dictionary.
106 HashTable
* hashTable
, ///< "this" pointer.
107 char* key
, ///< Key of the element to add or modify.
108 void* data
///< Pointer to new data at given key.
112 * @brief Add the entry (key, value) to dictionary.
113 * @param hashTable "this" pointer.
114 * @param key Key of the element to add or modify.
115 * @param data New data at given key.
117 * Usage: void hashtable_set(HashTable* hashTable, char* key, void data)
119 #define hashtable_set(hashTable, key, data) \
121 typeof((data)) tmp = data; \
122 _hashtable_set(hashTable, key, &tmp); \
126 * @brief Remove the given key (+ associated value).
128 void hashtable_delete(
129 HashTable
* hashTable
, ///< "this" pointer.
130 char* key
///< Key of the element to delete.
134 * @brief Clear the entire dictionary.
136 void hashtable_clear(
137 HashTable
* hashTable
///< "this" pointer.
141 * @brief Destroy the dictionary: clear it, and free hashes array.
143 void hashtable_destroy(
144 HashTable
* hashTable
///< "this" pointer.