+HashTable* InitHashTable (HashTable* T, unsigned Slots, const HashFunctions* Func)
+/* Initialize a hash table and return it */
+{
+ /* Initialize the fields */
+ T->Slots = Slots;
+ T->Count = 0;
+ T->Table = 0;
+ T->Func = Func;
+
+ /* Return the initialized table */
+ return T;
+}
+
+
+
+void DoneHashTable (HashTable* T)
+/* Destroy the contents of a hash table. Note: This will not free the entries
+ * in the table!
+ */
+{
+ /* Just free the array with the table pointers */
+ xfree (T->Table);
+}
+
+
+
void FreeHashTable (HashTable* T)
/* Free a hash table. Note: This will not free the entries in the table! */
{
/* Generate the hash over the node key. */
N->Hash = T->Func->GenHash (T->Func->GetKey (N));
-
+
/* Calculate the reduced hash */
RHash = N->Hash % T->Slots;
N->Next = T->Table[RHash];
T->Table[RHash] = N;
- /* Set the owner */
- N->Owner = T;
-
/* One more entry */
++T->Count;
}
-void HT_Remove (HashNode* N)
+void HT_Remove (HashTable* T, HashNode* N)
/* Remove a node from a hash table. */
{
- /* Get the table from the node */
- HashTable* T = N->Owner;
-
/* Calculate the reduced hash, which is also the slot number */
unsigned Slot = N->Hash % T->Slots;
/* Remove an entry from the given hash table */
{
/* The entry is the first member, so we can just convert the pointer */
- HashNode* N = Entry;
-
- /* Make sure the entry is actually in the given table */
- CHECK (N->Owner == T);
-
- /* Remove the node */
- HT_Remove (N);
+ HT_Remove (T, Entry);
}
-/* Hash table node. NOTE: This structure must be the first member of a struct
- * that is hashed by the module. Having it first allows to omit a pointer to
- * the entry itself, because the C standard guarantees that a pointer to a
+/* Hash table node. NOTE: This structure must be the first member of a struct
+ * that is hashed by the module. Having it first allows to omit a pointer to
+ * the entry itself, because the C standard guarantees that a pointer to a
* struct can be converted to its first member.
*/
typedef struct HashNode HashNode;
struct HashNode {
HashNode* Next; /* Next entry in hash list */
- struct HashTable* Owner; /* Owner table */
unsigned Hash; /* The full hash value */
};
/* Initialize a hash node. */
{
N->Next = 0;
- N->Owner = 0;
}
#else
-#define InitHashNode(N) \
- (N)->Next = 0, \
- (N)->Owner = 0
+#define InitHashNode(N) do { (N)->Next = 0; } while (0)
#endif
-#if defined(HAVE_INLINE)
-INLINE HashTable* InitHashTable (HashTable* T, unsigned Slots, const HashFunctions* Func)
+HashTable* InitHashTable (HashTable* T, unsigned Slots, const HashFunctions* Func);
/* Initialize a hash table and return it */
-{
- /* Initialize the fields */
- T->Slots = Slots;
- T->Count = 0;
- T->Table = 0;
- T->Func = Func;
-
- /* Return the initialized table */
- return T;
-}
-#else
-#define InitHashTable(T, Slots, Func) \
- (T)->Slots = (Slots), \
- (T)->Count = 0, \
- (T)->Table = 0, \
- (T)->Func = (Func), \
- (T)
-#endif
-#if defined(HAVE_INLINE)
-INLINE void DoneHashTable (HashTable* T)
+void DoneHashTable (HashTable* T);
/* Destroy the contents of a hash table. Note: This will not free the entries
* in the table!
*/
-{
- /* Just free the array with the table pointers */
- xfree (T->Table);
-}
-#else
-#define DoneHashTable(T) xfree ((T)->Table)
-#endif
#if defined(HAVE_INLINE)
INLINE HashTable* NewHashTable (unsigned Slots, const HashFunctions* Func)
void HT_Insert (HashTable* T, HashNode* N);
/* Insert a node into the given hash table */
-void HT_Remove (HashNode* N);
+void HT_Remove (HashTable* T, HashNode* N);
/* Remove a node from its hash table */
void HT_InsertEntry (HashTable* T, void* Entry);