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 (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 /* Since the HashEntry must be first member, we can use HT_Find here */
131 return HT_Find (T, Key);
136 void HT_Insert (HashTable* T, HashNode* N)
137 /* Insert a node into the given hash table */
141 /* If we don't have a table, we need to allocate it now */
146 /* Generate the hash over the node key. */
147 N->Hash = T->Func->GenHash (T->Func->GetKey (N));
149 /* Calculate the reduced hash */
150 RHash = N->Hash % T->Slots;
152 /* Insert the entry into the correct chain */
153 N->Next = T->Table[RHash];
165 void HT_Remove (HashNode* N)
166 /* Remove a node from a hash table. */
168 /* Get the table from the node */
169 HashTable* T = N->Owner;
171 /* Calculate the reduced hash, which is also the slot number */
172 unsigned Slot = N->Hash % T->Slots;
174 /* Remove the entry from the single linked list */
175 HashNode** Q = &T->Table[Slot];
177 /* If the pointer is NULL, the node is not in the table which we will
178 * consider a serious error.
182 /* Found - remove it */
193 void HT_InsertEntry (HashTable* T, void* Entry)
194 /* Insert an entry into the given hash table */
196 /* Since the hash node must be first member, Entry is also the pointer to
199 HT_Insert (T, Entry);
204 void HT_RemoveEntry (HashTable* T, void* Entry)
205 /* Remove an entry from the given hash table */
207 /* The entry is the first member, so we can just convert the pointer */
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. N is also the pointer to the entry */
242 /* Next node in chain */