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 void FreeHashTable (HashTable* T)
50 /* Free a hash table. Note: This will not free the entries in the table! */
53 /* Free the contents */
55 /* Free the table structure itself */
62 static void HT_Alloc (HashTable* T)
63 /* Allocate table memory */
68 T->Table = xmalloc (T->Slots * sizeof (T->Table[0]));
70 /* Initialize the table */
71 for (I = 0; I < T->Slots; ++I) {
78 HashNode* HT_Find (const HashTable* T, const void* Key)
79 /* Find the node with the given index */
81 /* If we don't have a table, there's nothing to find */
86 /* Search for the entry */
87 return HT_FindHash (T, Key, T->Func->GenHash (Key));
92 HashNode* HT_FindHash (const HashTable* T, const void* Key, unsigned Hash)
93 /* Find the node with the given key. Differs from HT_Find in that the hash
94 * for the key is precalculated and passed to the function.
99 /* If we don't have a table, there's nothing to find */
104 /* Search for the entry in the given chain */
105 N = T->Table[Hash % T->Slots];
108 /* First compare the full hash, to avoid calling the compare function
109 * if it is not really necessary.
111 if (N->Hash == Hash &&
112 T->Func->Compare (Key, T->Func->GetKey (HN_GetEntry (N))) == 0) {
117 /* Not found, next entry */
121 /* Return what we found */
127 void* HT_FindEntry (const HashTable* T, const void* Key)
128 /* Find the node with the given index and return the corresponding entry */
130 /* First, search for the hash node */
131 HashNode* N = HT_Find (T, Key);
133 /* Convert the node into an entry if necessary */
134 return N? HN_GetEntry (N) : 0;
139 void HT_Insert (HashTable* T, HashNode* N)
140 /* Insert a node into the given hash table */
144 /* If we don't have a table, we need to allocate it now */
149 /* Generate the hash over the node key. */
150 N->Hash = T->Func->GenHash (T->Func->GetKey (HN_GetEntry (N)));
152 /* Calculate the reduced hash */
153 RHash = N->Hash % T->Slots;
155 /* Insert the entry into the correct chain */
156 N->Next = T->Table[RHash];
168 void HT_Remove (HashNode* N)
169 /* Remove a node from a hash table. */
171 /* Get the table from the node */
172 HashTable* T = N->Owner;
174 /* Calculate the reduced hash, which is also the slot number */
175 unsigned Slot = N->Hash % T->Slots;
177 /* Remove the entry from the single linked list */
178 HashNode** Q = &T->Table[Slot];
180 /* If the pointer is NULL, the node is not in the table which we will
181 * consider a serious error.
185 /* Found - remove it */
196 void HT_InsertEntry (HashTable* T, void* Entry)
197 /* Insert an entry into the given hash table */
199 HT_Insert (T, T->Func->GetHashNode (Entry));
204 void HT_RemoveEntry (HashTable* T, void* Entry)
205 /* Remove an entry from the given hash table */
207 /* Get the node from the entry */
208 HashNode* N = T->Func->GetHashNode (Entry);
210 /* Make sure the entry is actually in the given table */
211 CHECK (N->Owner == T);
213 /* Remove the node */
219 void HT_Walk (HashTable* T, void (*F) (void* Entry, void* Data), void* Data)
220 /* Walk over all nodes of a hash table. For each node, the user supplied
221 * function F is called, passing a pointer to the entry, and the data pointer
222 * passed to HT_Walk by the caller.
227 /* If we don't have a table there are no entries to walk over */
232 /* Walk over all chains */
233 for (I = 0; I < T->Slots; ++I) {
235 /* Get the pointer to the first entry of the hash chain */
236 HashNode* N = T->Table[I];
238 /* Walk over all entries in this chain */
240 /* Call the user function */
241 F (HN_GetEntry (N), Data);
242 /* Next node in chain */