X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcommon%2Fhashtab.h;h=f110fa1a0c0d68b8da7be334dc25bee44bce25fc;hb=8590de5cf3515d7df156a5d470ad06bd68f97555;hp=fc56983918585f589282e411d8dbc062017d32e8;hpb=f571ec44aefc80288973f6c9abb5ef02f3274c36;p=cc65 diff --git a/src/common/hashtab.h b/src/common/hashtab.h index fc5698391..f110fa1a0 100644 --- a/src/common/hashtab.h +++ b/src/common/hashtab.h @@ -45,20 +45,19 @@ /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ -/* 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. - */ +/* 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 */ }; @@ -71,14 +70,14 @@ struct HashFunctions { unsigned (*GenHash) (const void* Key); /* Generate the hash over a key. */ - const void* (*GetKey) (void* Entry); + const void* (*GetKey) (const void* Entry); /* Given a pointer to the user entry data, return a pointer to the key */ int (*Compare) (const void* Key1, const void* Key2); /* Compare two keys. The function must return a value less than zero if - * Key1 is smaller than Key2, zero if both are equal, and a value greater - * than zero if Key1 is greater then Key2. - */ + ** Key1 is smaller than Key2, zero if both are equal, and a value greater + ** than zero if Key1 is greater then Key2. + */ }; /* Hash table */ @@ -105,12 +104,9 @@ INLINE void InitHashNode (HashNode* N) /* 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 @@ -121,40 +117,13 @@ INLINE void InitHashNode (HashNode* N) -#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 +** in the table! +*/ #if defined(HAVE_INLINE) INLINE HashTable* NewHashTable (unsigned Slots, const HashFunctions* Func) @@ -170,40 +139,41 @@ INLINE HashTable* NewHashTable (unsigned Slots, const HashFunctions* Func) void FreeHashTable (HashTable* T); /* Free a hash table. Note: This will not free the entries in the table! */ -HashNode* HT_Find (const HashTable* T, const void* Key); -/* Find the node with the given key*/ +#if defined(HAVE_INLINE) +INLINE unsigned HT_GetCount (const HashTable* T) +/* Return the number of items in the table. */ +{ + return T->Count; +} +#else +#define HT_GetCount(T) ((T)->Count) +#endif HashNode* HT_FindHash (const HashTable* T, const void* Key, unsigned Hash); /* Find the node with the given key. Differs from HT_Find in that the hash - * for the key is precalculated and passed to the function. - */ +** for the key is precalculated and passed to the function. +*/ -void* HT_FindEntry (const HashTable* T, const void* Key); -/* Find the node with the given key and return the corresponding entry */ +void* HT_Find (const HashTable* T, const void* Key); +/* Find the entry with the given key and return it */ -void HT_Insert (HashTable* T, HashNode* N); -/* Insert a node into the given hash table */ - -void HT_Remove (HashNode* N); -/* Remove a node from its hash table */ - -void HT_InsertEntry (HashTable* T, void* Entry); +void HT_Insert (HashTable* T, void* Entry); /* Insert an entry into the given hash table */ -void HT_RemoveEntry (HashTable* T, void* Entry); +void HT_Remove (HashTable* T, void* Entry); /* Remove an entry from the given hash table */ -void HT_Walk (HashTable* T, void (*F) (void* Entry, void* Data), void* Data); -/* Walk over all nodes of a hash table. For each node, the user supplied - * function F is called, passing a pointer to the entry, and the data pointer - * passed to HT_Walk by the caller. - */ +void HT_Walk (HashTable* T, int (*F) (void* Entry, void* Data), void* Data); +/* Walk over all nodes of a hash table, optionally deleting entries from the +** table. For each node, the user supplied function F is called, passing a +** pointer to the entry, and the data pointer passed to HT_Walk by the caller. +** If F returns true, the node is deleted from the hash table otherwise it's +** left in place. While deleting the node, the node is not accessed, so it is +** safe for F to free the memory associcated with the entry. +*/ /* End of hashtab.h */ #endif - - -