From: <Mee...@us...> - 2011-12-20 20:16:23
|
Revision: 3703 http://sc2.svn.sourceforge.net/sc2/?rev=3703&view=rev Author: Meep-Eep Date: 2011-12-20 20:16:17 +0000 (Tue, 20 Dec 2011) Log Message: ----------- Added a callback function for keys. Also some more comments. Modified Paths: -------------- trunk/sc2/ChangeLog trunk/sc2/src/libs/file/files.c trunk/sc2/src/libs/resource/resinit.c trunk/sc2/src/libs/uio/charhashtable.c trunk/sc2/src/libs/uio/charhashtable.h trunk/sc2/src/libs/uio/gphys.h trunk/sc2/src/libs/uio/hashtable.c trunk/sc2/src/libs/uio/hashtable.h Modified: trunk/sc2/ChangeLog =================================================================== --- trunk/sc2/ChangeLog 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/ChangeLog 2011-12-20 20:16:17 UTC (rev 3703) @@ -1,4 +1,6 @@ Changes towards version 0.8: +- Added a free callback function for the values of the key-value pairs + in hash tables - SvdB - Annigilate ActivateStarShip() - SvdB - Removed obsolete RESPONSE_TO_REF() - SvdB - Don't require the 'shadow' dir in addon packs, from Alex Modified: trunk/sc2/src/libs/file/files.c =================================================================== --- trunk/sc2/src/libs/file/files.c 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/src/libs/file/files.c 2011-12-20 20:16:17 UTC (rev 3703) @@ -94,7 +94,7 @@ buf = HMalloc(BUFSIZE); // This was originally a statically allocated buffer, // but as this function might be run from a thread with - // a small Stack, this is better. + // a small stack, this is better. while (1) { numInBuf = uio_read (src, buf, BUFSIZE); Modified: trunk/sc2/src/libs/resource/resinit.c =================================================================== --- trunk/sc2/src/libs/resource/resinit.c 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/src/libs/resource/resinit.c 2011-12-20 20:16:17 UTC (rev 3703) @@ -34,7 +34,8 @@ static RESOURCE_INDEX allocResourceIndex (void) { RESOURCE_INDEX ndx = HMalloc (sizeof (RESOURCE_INDEX_DESC)); - ndx->map = CharHashTable_newHashTable (NULL, NULL, NULL, NULL, 0, 0.85, 0.9); + ndx->map = CharHashTable_newHashTable (NULL, NULL, NULL, NULL, NULL, + 0, 0.85, 0.9); return ndx; } Modified: trunk/sc2/src/libs/uio/charhashtable.c =================================================================== --- trunk/sc2/src/libs/uio/charhashtable.c 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/src/libs/uio/charhashtable.c 2011-12-20 20:16:17 UTC (rev 3703) @@ -30,7 +30,7 @@ const char *key1, const char *key2); static inline char *CharHashTable_copy(CharHashTable_HashTable *hashTable, const char *key); -static inline void CharHashTable_free(CharHashTable_HashTable *hashTable, +static inline void CharHashTable_freeKey(CharHashTable_HashTable *hashTable, char *key); #include "hashtable.c" @@ -68,7 +68,7 @@ } static inline void -CharHashTable_free(CharHashTable_HashTable *hashTable, +CharHashTable_freeKey(CharHashTable_HashTable *hashTable, char *key) { (void) *hashTable; uio_free(key); Modified: trunk/sc2/src/libs/uio/charhashtable.h =================================================================== --- trunk/sc2/src/libs/uio/charhashtable.h 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/src/libs/uio/charhashtable.h 2011-12-20 20:16:17 UTC (rev 3703) @@ -28,7 +28,9 @@ #define CharHashTable_HASH CharHashTable_hash #define CharHashTable_EQUAL CharHashTable_equal #define CharHashTable_COPY CharHashTable_copy -#define CharHashTable_FREE CharHashTable_free +#define CharHashTable_FREEKEY CharHashTable_freeKey +#define CharHashTable_FREEVALUE(hashTable, key) \ + ((void) (hashTable), (void) (key)) #include "hashtable.h" Modified: trunk/sc2/src/libs/uio/gphys.h =================================================================== --- trunk/sc2/src/libs/uio/gphys.h 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/src/libs/uio/gphys.h 2011-12-20 20:16:17 UTC (rev 3703) @@ -33,7 +33,7 @@ #define uio_GPDirEntries_new() \ ((uio_GPDirEntries *) CharHashTable_newHashTable(NULL, NULL, NULL, \ - NULL, 0, 0.85, 0.9)) + NULL, NULL, 0, 0.85, 0.9)) #define uio_GPDirEntries_add(hashTable, name, item) \ CharHashTable_add((CharHashTable_HashTable *) hashTable, name, \ (void *) item) Modified: trunk/sc2/src/libs/uio/hashtable.c =================================================================== --- trunk/sc2/src/libs/uio/hashtable.c 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/src/libs/uio/hashtable.c 2011-12-20 20:16:17 UTC (rev 3703) @@ -46,12 +46,14 @@ static inline void HASHTABLE_(freeHashEntry)( HASHTABLE_(HashEntry) *entry); +// Create a new HashTable. HASHTABLE_(HashTable) * HASHTABLE_(newHashTable)( HASHTABLE_(HashFunction) hashFunction, HASHTABLE_(EqualFunction) equalFunction, HASHTABLE_(CopyFunction) copyFunction, - HASHTABLE_(FreeFunction) freeFunction, + HASHTABLE_(FreeKeyFunction) freeKeyFunction, + HASHTABLE_(FreeValueFunction) freeValueFunction, uio_uint32 initialSize, double minFillQuotient, double maxFillQuotient) { @@ -63,7 +65,8 @@ hashTable->hashFunction = hashFunction; hashTable->equalFunction = equalFunction; hashTable->copyFunction = copyFunction; - hashTable->freeFunction = freeFunction; + hashTable->freeKeyFunction = freeKeyFunction; + hashTable->freeValueFunction = freeValueFunction; hashTable->minFillQuotient = minFillQuotient; hashTable->maxFillQuotient = maxFillQuotient; @@ -72,6 +75,7 @@ return hashTable; } +// Add an entry to the HashTable. uio_bool HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable, const HASHTABLE_(Key) *key, HASHTABLE_(Value) *value) { @@ -104,6 +108,7 @@ return true; } +// Remove an entry with a specified Key from the HashTable. uio_bool HASHTABLE_(remove)(HASHTABLE_(HashTable) *hashTable, const HASHTABLE_(Key) *key) { @@ -122,7 +127,8 @@ entry = &(*entry)->next; } next = (*entry)->next; - HASHTABLE_(FREE)(hashTable, (*entry)->key); + HASHTABLE_(FREEKEY)(hashTable, (*entry)->key); + HASHTABLE_(FREEVALUE)(hashTable, (*entry)->value); HASHTABLE_(freeHashEntry)(*entry); *entry = next; @@ -133,6 +139,7 @@ return true; } +// Find the Value stored for some Key. HASHTABLE_(Value) * HASHTABLE_(find)(HASHTABLE_(HashTable) *hashTable, const HASHTABLE_(Key) *key) { @@ -151,11 +158,13 @@ return NULL; } +// Returns the number of entries in the HashTable. uio_uint32 HASHTABLE_(count)(const HASHTABLE_(HashTable) *hashTable) { return hashTable->numEntries; } +// Auxiliary function to (re)initialise the buckets in the HashTable. static void HASHTABLE_(setup)(HASHTABLE_(HashTable) *hashTable, uio_uint32 initialSize) { if (initialSize < 4) @@ -175,6 +184,7 @@ #endif } +// Resize the buckets in the HashTable. static void HASHTABLE_(resize)(HASHTABLE_(HashTable) *hashTable) { HASHTABLE_(HashEntry) **oldEntries; @@ -224,6 +234,7 @@ return x + 1; } +// Get an iterator to iterate through all the entries in the HashTable. // NB: Iterator should be considered invalid if the HashTable is changed. // TODO: change this (make it thread-safe) // this can be done by only marking items as deleted when @@ -252,21 +263,26 @@ return iterator; } +// Returns true if and only if there are no more entries in the hash table +// for the Iterator to find. int HASHTABLE_(iteratorDone)(const HASHTABLE_(Iterator) *iterator) { return iterator->bucketNr >= iterator->hashTable->size; } +// Get the Key of the entry pointed to by an Iterator. HASHTABLE_(Key) * HASHTABLE_(iteratorKey)(HASHTABLE_(Iterator) *iterator) { return iterator->entry->key; } +// Get the Value of the entry pointed to by an Iterator. HASHTABLE_(Value) * HASHTABLE_(iteratorValue)(HASHTABLE_(Iterator) *iterator) { return iterator->entry->value; } +// Move the Iterator to the next entry in the HashTable. // Should not be called if the iterator is already past the last entry. HASHTABLE_(Iterator) * HASHTABLE_(iteratorNext)(HASHTABLE_(Iterator) *iterator) { @@ -293,16 +309,19 @@ return iterator; } +// Free the Iterator. void HASHTABLE_(freeIterator)(HASHTABLE_(Iterator) *iterator) { uio_free(iterator); } +// Auxiliary function to allocate a HashTable. static inline HASHTABLE_(HashTable) * HASHTABLE_(allocHashTable)(void) { return uio_malloc(sizeof (HASHTABLE_(HashTable))); } +// Auxiliary function to create a HashEntry. static inline HASHTABLE_(HashEntry) * HASHTABLE_(newHashEntry)(uio_uint32 hash, HASHTABLE_(Key) *key, HASHTABLE_(Value) *value, HASHTABLE_(HashEntry) *next) { @@ -316,11 +335,13 @@ return result; } +// Allocate a new HashEntry. static inline HASHTABLE_(HashEntry) * HASHTABLE_(allocHashEntry)(void) { return uio_malloc(sizeof (HASHTABLE_(HashEntry))); } +// Delete the HashTable. void HASHTABLE_(deleteHashTable)(HASHTABLE_(HashTable) *hashTable) { uio_uint32 i; @@ -333,7 +354,8 @@ entry = *bucketPtr; while (entry != NULL) { next = entry->next; - HASHTABLE_(FREE)(hashTable, entry->key); + HASHTABLE_(FREEKEY)(hashTable, entry->key); + HASHTABLE_(FREEVALUE)(hashTable, entry->value); HASHTABLE_(freeHashEntry)(entry); entry = next; i--; @@ -344,6 +366,7 @@ uio_free(hashTable); } +// Auxiliary function to deallocate a HashEntry. static inline void HASHTABLE_(freeHashEntry)(HASHTABLE_(HashEntry) *entry) { uio_free(entry); Modified: trunk/sc2/src/libs/uio/hashtable.h =================================================================== --- trunk/sc2/src/libs/uio/hashtable.h 2011-12-16 21:01:55 UTC (rev 3702) +++ trunk/sc2/src/libs/uio/hashtable.h 2011-12-20 20:16:17 UTC (rev 3703) @@ -39,6 +39,8 @@ // (and typedefs) from the HASHTABLE_ block below. // In the .c file, #define HASHTABLE_INTERNAL, #include the .h file // and hashtable.c (in this order), and add the necessary functions. +// If you do not need to free the Value, you can define HashTable_FREEVALUE +// as a no-op. #ifndef HASHTABLE_ # define HASHTABLE_(identifier) HashTable ## _ ## identifier typedef void HashTable_Key; @@ -49,8 +51,10 @@ (hashTable)->equalFunction(hashKey1, hashKey2) # define HashTable_COPY(hashTable, hashKey) \ (hashTable)->copyFunction(hashKey) -# define HashTable_FREE(hashTable, hashKey) \ - (hashTable)->freeFunction(hashKey) +# define HashTable_FREEKEY(hashTable, hashKey) \ + (hashTable)->freeKeyFunction(hashKey) +# define HashTable_FREEVALUE(hashTable, hashValue) \ + (hashTable)->freeValueFunction(hashKey) #endif @@ -59,8 +63,9 @@ typedef uio_bool (*HASHTABLE_(EqualFunction))(const HASHTABLE_(Key) *, const HASHTABLE_(Key) *); typedef HASHTABLE_(Value) *(*HASHTABLE_(CopyFunction))( - const HASHTABLE_(Value) *); -typedef void (*HASHTABLE_(FreeFunction))(HASHTABLE_(Value) *); + const HASHTABLE_(Key) *); +typedef void (*HASHTABLE_(FreeKeyFunction))(HASHTABLE_(Key) *); +typedef void (*HASHTABLE_(FreeValueFunction))(HASHTABLE_(Value) *); typedef struct HASHTABLE_(HashTable) HASHTABLE_(HashTable); typedef struct HASHTABLE_(HashEntry) HASHTABLE_(HashEntry); @@ -68,9 +73,17 @@ struct HASHTABLE_(HashTable) { HASHTABLE_(HashFunction) hashFunction; + // Function creating a uio_uint32 hash of a key. HASHTABLE_(EqualFunction) equalFunction; + // Function used to compare two keys. HASHTABLE_(CopyFunction) copyFunction; - HASHTABLE_(FreeFunction) freeFunction; + // Function used to copy a key. + HASHTABLE_(FreeKeyFunction) freeKeyFunction; + // Function used to free a key. + HASHTABLE_(FreeValueFunction) freeValueFunction; + // Function used to free a value. Called when an entry is + // removed using the remove function, or for entries + // still in the HashTable when the HashTable is deleted. double minFillQuotient; // How much of half of the hashtable needs to be filled @@ -114,7 +127,9 @@ HASHTABLE_(HashFunction) hashFunction, HASHTABLE_(EqualFunction) equalFunction, HASHTABLE_(CopyFunction) copyFunction, - HASHTABLE_(FreeFunction) freeFunction, uio_uint32 initialSize, + HASHTABLE_(FreeKeyFunction) freeKeyFunction, + HASHTABLE_(FreeValueFunction) freeValueFunction, + uio_uint32 initialSize, double minFillQuotient, double maxFillQuotient); uio_bool HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable, const HASHTABLE_(Key) *key, HASHTABLE_(Value) *value); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |