Commit | Line | Data |
---|---|---|
1ff641f9 BA |
1 | /** |
2 | * @file HashTable.h | |
3 | */ | |
4 | ||
5 | #ifndef CGDS_HASH_TABLE_H | |
6 | #define CGDS_HASH_TABLE_H | |
7 | ||
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
10 | #include "cgds/safe_alloc.h" | |
11 | #include "cgds/types.h" | |
1ff641f9 BA |
12 | |
13 | /** | |
14 | * @brief Cell of a dictionary. | |
15 | */ | |
16 | typedef struct HashCell { | |
e45132ac | 17 | char* key; ///< Key (as a string). |
1ff641f9 | 18 | void* data; ///< Generic data contained in this cell. |
e45132ac | 19 | struct HashCell* next; ///< Pointer to next cell in the list. |
1ff641f9 BA |
20 | } HashCell; |
21 | ||
22 | /** | |
23 | * @brief Generic dictionary string --> any data. | |
24 | */ | |
25 | typedef struct HashTable { | |
26 | UInt size; ///< Count elements in the dictionary. | |
e45132ac | 27 | size_t dataSize; ///< Size of a dict cell element in bytes. |
1ff641f9 | 28 | size_t hashSize; ///< (Maximum) Number of stored hash keys. |
e45132ac | 29 | HashCell** head; ///< Pointers to the first cell in a list. |
1ff641f9 BA |
30 | } HashTable; |
31 | ||
32 | /** | |
33 | * @brief Initialize an empty dictionary. | |
34 | */ | |
35 | void _hashtable_init( | |
e45132ac BA |
36 | HashTable* hashTable, ///< "this" pointer. |
37 | size_t dataSize, ///< Size in bytes of a dictionary element. | |
1ff641f9 BA |
38 | size_t hashSize ///< (Maximum) Number of stored hash keys. |
39 | ); | |
40 | ||
41 | /** | |
42 | * @brief Return an allocated and initialized dictionary. | |
43 | */ | |
44 | HashTable* _hashtable_new( | |
e45132ac | 45 | size_t dataSize, ///< Size in bytes of a dictionary element. |
1ff641f9 BA |
46 | size_t hashSize ///< (Maximum) Number of stored hash keys. |
47 | ); | |
48 | ||
49 | /** | |
50 | * @brief Return an allocated and initialized vector. | |
51 | * @param type Type of a vector element (int, char*, ...). | |
52 | * | |
53 | * Usage: HashTable* hashtable_new(<Type> type, UInt hash_size) | |
54 | */ | |
55 | #define hashtable_new(type, hsize) \ | |
eed1b5d2 | 56 | _hashtable_new(sizeof(type), hsize) |
1ff641f9 BA |
57 | |
58 | /** | |
59 | * @brief Copy constructor (shallow copy, ok for basic types). | |
60 | */ | |
61 | HashTable* hashtable_copy( | |
e45132ac | 62 | HashTable* hashTable ///< "this" pointer. |
1ff641f9 BA |
63 | ); |
64 | ||
65 | /** | |
66 | * @brief Check if the dictionary is empty. | |
67 | */ | |
68 | bool hashtable_empty( | |
e45132ac | 69 | HashTable* hastTable ///< "this" pointer. |
1ff641f9 BA |
70 | ); |
71 | ||
72 | /** | |
73 | * @brief Return current size. | |
74 | */ | |
75 | UInt hashtable_size( | |
e45132ac | 76 | HashTable* hastTable ///< "this" pointer. |
1ff641f9 BA |
77 | ); |
78 | ||
79 | /** | |
80 | * @brief Lookup element of given key. | |
81 | */ | |
82 | void* _hashtable_get( | |
e45132ac BA |
83 | HashTable* hashTable, ///< "this" pointer. |
84 | char* key ///< Key of the element to retrieve. | |
1ff641f9 BA |
85 | ); |
86 | ||
87 | /** | |
88 | * @brief Lookup element of given key. | |
89 | * @param hashTable "this" pointer. | |
90 | * @param key Key of the element to retrieve.. | |
eed1b5d2 | 91 | * @param data 'out' variable (ptr) to contain the result. |
1ff641f9 | 92 | * |
588a2232 | 93 | * Usage: void hashtable_get(HashTable* hashTable, char* key, void* data) |
1ff641f9 BA |
94 | */ |
95 | #define hashtable_get(hashTable, key, data) \ | |
96 | { \ | |
eed1b5d2 | 97 | data = (typeof(data))_hashtable_get(hashTable, key); \ |
1ff641f9 BA |
98 | } |
99 | ||
100 | /** | |
101 | * @brief Add the entry (key, value) to dictionary. | |
102 | */ | |
103 | void _hashtable_set( | |
e45132ac BA |
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. | |
1ff641f9 BA |
107 | ); |
108 | ||
109 | /** | |
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. | |
114 | * | |
115 | * Usage: void hashtable_set(HashTable* hashTable, char* key, void data) | |
116 | */ | |
117 | #define hashtable_set(hashTable, key, data) \ | |
118 | { \ | |
eed1b5d2 | 119 | typeof(data) tmp = data; \ |
e45132ac | 120 | _hashtable_set(hashTable, key, &tmp); \ |
1ff641f9 BA |
121 | } |
122 | ||
123 | /** | |
124 | * @brief Remove the given key (+ associated value). | |
588a2232 BA |
125 | * |
126 | * Usage: void hashtable_delete(HashTable* hashTable, char* key) | |
1ff641f9 BA |
127 | */ |
128 | void hashtable_delete( | |
e45132ac | 129 | HashTable* hashTable, ///< "this" pointer. |
1ff641f9 BA |
130 | char* key ///< Key of the element to delete. |
131 | ); | |
132 | ||
133 | /** | |
134 | * @brief Clear the entire dictionary. | |
135 | */ | |
136 | void hashtable_clear( | |
e45132ac | 137 | HashTable* hashTable ///< "this" pointer. |
1ff641f9 BA |
138 | ); |
139 | ||
140 | /** | |
141 | * @brief Destroy the dictionary: clear it, and free hashes array. | |
142 | */ | |
143 | void hashtable_destroy( | |
e45132ac | 144 | HashTable* hashTable ///< "this" pointer. |
1ff641f9 BA |
145 | ); |
146 | ||
147 | #endif |