1 /*****************************************************************************/
5 /* Generic hash table */
9 /* (C) 2003-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
43 /*****************************************************************************/
44 /* struct HashTable */
45 /*****************************************************************************/
49 HashTable* InitHashTable (HashTable* T, unsigned Slots, const HashFunctions* Func)
50 /* Initialize a hash table and return it */
52 /* Initialize the fields */
58 /* Return the initialized table */
64 void DoneHashTable (HashTable* T)
65 /* Destroy the contents of a hash table. Note: This will not free the entries
69 /* Just free the array with the table pointers */
75 void FreeHashTable (HashTable* T)
76 /* Free a hash table. Note: This will not free the entries in the table! */
79 /* Free the contents */
81 /* Free the table structure itself */
88 static void HT_Alloc (HashTable* T)
89 /* Allocate table memory */
94 T->Table = xmalloc (T->Slots * sizeof (T->Table[0]));
96 /* Initialize the table */
97 for (I = 0; I < T->Slots; ++I) {
104 HashNode* HT_Find (const HashTable* T, const void* Key)
105 /* Find the node with the given index */
107 /* If we don't have a table, there's nothing to find */
112 /* Search for the entry */
113 return HT_FindHash (T, Key, T->Func->GenHash (Key));
118 HashNode* HT_FindHash (const HashTable* T, const void* Key, unsigned Hash)
119 /* Find the node with the given key. Differs from HT_Find in that the hash
120 * for the key is precalculated and passed to the function.
125 /* If we don't have a table, there's nothing to find */
130 /* Search for the entry in the given chain */
131 N = T->Table[Hash % T->Slots];
134 /* First compare the full hash, to avoid calling the compare function
135 * if it is not really necessary.
137 if (N->Hash == Hash &&
138 T->Func->Compare (Key, T->Func->GetKey (N)) == 0) {
143 /* Not found, next entry */
147 /* Return what we found */
153 void* HT_FindEntry (const HashTable* T, const void* Key)
154 /* Find the node with the given index and return the corresponding entry */
156 /* Since the HashEntry must be first member, we can use HT_Find here */
157 return HT_Find (T, Key);
162 void HT_Insert (HashTable* T, HashNode* N)
163 /* Insert a node into the given hash table */
167 /* If we don't have a table, we need to allocate it now */
172 /* Generate the hash over the node key. */
173 N->Hash = T->Func->GenHash (T->Func->GetKey (N));
175 /* Calculate the reduced hash */
176 RHash = N->Hash % T->Slots;
178 /* Insert the entry into the correct chain */
179 N->Next = T->Table[RHash];
188 void HT_Remove (HashTable* T, HashNode* N)
189 /* Remove a node from a hash table. */
191 /* Calculate the reduced hash, which is also the slot number */
192 unsigned Slot = N->Hash % T->Slots;
194 /* Remove the entry from the single linked list */
195 HashNode** Q = &T->Table[Slot];
197 /* If the pointer is NULL, the node is not in the table which we will
198 * consider a serious error.
202 /* Found - remove it */
213 void HT_InsertEntry (HashTable* T, void* Entry)
214 /* Insert an entry into the given hash table */
216 /* Since the hash node must be first member, Entry is also the pointer to
219 HT_Insert (T, Entry);
224 void HT_RemoveEntry (HashTable* T, void* Entry)
225 /* Remove an entry from the given hash table */
227 /* The entry is the first member, so we can just convert the pointer */
228 HT_Remove (T, Entry);
233 void HT_Walk (HashTable* T, void (*F) (void* Entry, void* Data), void* Data)
234 /* Walk over all nodes of a hash table. For each node, the user supplied
235 * function F is called, passing a pointer to the entry, and the data pointer
236 * passed to HT_Walk by the caller.
241 /* If we don't have a table there are no entries to walk over */
246 /* Walk over all chains */
247 for (I = 0; I < T->Slots; ++I) {
249 /* Get the pointer to the first entry of the hash chain */
250 HashNode* N = T->Table[I];
252 /* Walk over all entries in this chain */
254 /* Call the user function. N is also the pointer to the entry */
256 /* Next node in chain */