1 /*****************************************************************************/
5 /* Generic hash table */
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
42 /*****************************************************************************/
43 /* struct HashTable */
44 /*****************************************************************************/
48 void FreeHashTable (HashTable* T)
49 /* Free a hash table. Note: This will not free the entries in the table! */
52 /* Free the contents */
54 /* Free the table structure itself */
61 static void HT_Alloc (HashTable* T)
62 /* Allocate table memory */
67 T->Table = xmalloc (T->Slots * sizeof (T->Table[0]));
69 /* Initialize the table */
70 for (I = 0; I < T->Slots; ++I) {
77 HashNode* HT_Find (const HashTable* T, const void* Key)
78 /* Find the node with the given index */
80 /* If we don't have a table, there's nothing to find */
85 /* Search for the entry */
86 return HT_FindHash (T, Key, T->Func->GenHash (Key));
91 HashNode* HT_FindHash (const HashTable* T, const void* Key, unsigned Hash)
92 /* Find the node with the given key. Differs from HT_Find in that the hash
93 * for the key is precalculated and passed to the function.
98 /* If we don't have a table, there's nothing to find */
103 /* Search for the entry in the given chain */
104 N = T->Table[Hash % T->Slots];
107 /* First compare the full hash, to avoid calling the compare function
108 * if it is not really necessary.
110 if (N->Hash == Hash &&
111 T->Func->Compare (Key, T->Func->GetKey (HN_GetEntry (N))) == 0) {
116 /* Not found, next entry */
120 /* Return what we found */
126 void* HT_FindEntry (const HashTable* T, const void* Key)
127 /* Find the node with the given index and return the corresponding entry */
129 /* First, search for the hash node */
130 HashNode* N = HT_Find (T, Key);
132 /* Convert the node into an entry if necessary */
133 return N? HN_GetEntry (N) : 0;
138 void HT_Insert (HashTable* T, HashNode* N)
139 /* Insert a node into the given hash table */
143 /* If we don't have a table, we need to allocate it now */
148 /* Generate the hash over the node key. */
149 N->Hash = T->Func->GenHash (T->Func->GetKey (HN_GetEntry (N)));
151 /* Calculate the reduced hash */
152 RHash = N->Hash % T->Slots;
154 /* Insert the entry into the correct chain */
155 N->Next = T->Table[RHash];
167 void HT_InsertEntry (HashTable* T, void* Entry)
168 /* Insert an entry into the given hash table */
170 HT_Insert (T, T->Func->GetHashNode (Entry));
175 void HT_Walk (HashTable* T, void (*F) (void* Entry, void* Data), void* Data)
176 /* Walk over all nodes of a hash table. For each node, the user supplied
177 * function F is called, passing a pointer to the entry, and the data pointer
178 * passed to HT_Walk by the caller.
183 /* If we don't have a table there are no entries to walk over */
188 /* Walk over all chains */
189 for (I = 0; I < T->Slots; ++I) {
191 /* Get the pointer to the first entry of the hash chain */
192 HashNode* N = T->Table[I];
194 /* Walk over all entries in this chain */
196 /* Call the user function */
197 F (HN_GetEntry (N), Data);
198 /* Next node in chain */