Added a callback function for keys.

Also some more comments.



git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3703 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
Meep-Eep
2011-12-20 20:16:17 +00:00
parent 2fdbcc6f78
commit 3d91a7cf07
8 changed files with 59 additions and 16 deletions
+2
View File
@@ -1,4 +1,6 @@
Changes towards version 0.8: 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 - Annigilate ActivateStarShip() - SvdB
- Removed obsolete RESPONSE_TO_REF() - SvdB - Removed obsolete RESPONSE_TO_REF() - SvdB
- Don't require the 'shadow' dir in addon packs, from Alex - Don't require the 'shadow' dir in addon packs, from Alex
+1 -1
View File
@@ -94,7 +94,7 @@ copyFile (uio_DirHandle *srcDir, const char *srcName,
buf = HMalloc(BUFSIZE); buf = HMalloc(BUFSIZE);
// This was originally a statically allocated buffer, // This was originally a statically allocated buffer,
// but as this function might be run from a thread with // 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) while (1)
{ {
numInBuf = uio_read (src, buf, BUFSIZE); numInBuf = uio_read (src, buf, BUFSIZE);
+2 -1
View File
@@ -34,7 +34,8 @@
static RESOURCE_INDEX static RESOURCE_INDEX
allocResourceIndex (void) { allocResourceIndex (void) {
RESOURCE_INDEX ndx = HMalloc (sizeof (RESOURCE_INDEX_DESC)); 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; return ndx;
} }
+2 -2
View File
@@ -30,7 +30,7 @@ static inline uio_bool CharHashTable_equal(CharHashTable_HashTable *hashTable,
const char *key1, const char *key2); const char *key1, const char *key2);
static inline char *CharHashTable_copy(CharHashTable_HashTable *hashTable, static inline char *CharHashTable_copy(CharHashTable_HashTable *hashTable,
const char *key); const char *key);
static inline void CharHashTable_free(CharHashTable_HashTable *hashTable, static inline void CharHashTable_freeKey(CharHashTable_HashTable *hashTable,
char *key); char *key);
#include "hashtable.c" #include "hashtable.c"
@@ -68,7 +68,7 @@ CharHashTable_copy(CharHashTable_HashTable *hashTable,
} }
static inline void static inline void
CharHashTable_free(CharHashTable_HashTable *hashTable, CharHashTable_freeKey(CharHashTable_HashTable *hashTable,
char *key) { char *key) {
(void) *hashTable; (void) *hashTable;
uio_free(key); uio_free(key);
+3 -1
View File
@@ -28,7 +28,9 @@ typedef void HASHTABLE_(Value);
#define CharHashTable_HASH CharHashTable_hash #define CharHashTable_HASH CharHashTable_hash
#define CharHashTable_EQUAL CharHashTable_equal #define CharHashTable_EQUAL CharHashTable_equal
#define CharHashTable_COPY CharHashTable_copy #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" #include "hashtable.h"
+1 -1
View File
@@ -33,7 +33,7 @@ typedef struct CharHashTable_HashTable uio_GPDirEntries;
#define uio_GPDirEntries_new() \ #define uio_GPDirEntries_new() \
((uio_GPDirEntries *) CharHashTable_newHashTable(NULL, NULL, NULL, \ ((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) \ #define uio_GPDirEntries_add(hashTable, name, item) \
CharHashTable_add((CharHashTable_HashTable *) hashTable, name, \ CharHashTable_add((CharHashTable_HashTable *) hashTable, name, \
(void *) item) (void *) item)
+27 -4
View File
@@ -46,12 +46,14 @@ static inline HASHTABLE_(HashEntry) *HASHTABLE_(allocHashEntry)(void);
static inline void HASHTABLE_(freeHashEntry)( static inline void HASHTABLE_(freeHashEntry)(
HASHTABLE_(HashEntry) *entry); HASHTABLE_(HashEntry) *entry);
// Create a new HashTable.
HASHTABLE_(HashTable) * HASHTABLE_(HashTable) *
HASHTABLE_(newHashTable)( HASHTABLE_(newHashTable)(
HASHTABLE_(HashFunction) hashFunction, HASHTABLE_(HashFunction) hashFunction,
HASHTABLE_(EqualFunction) equalFunction, HASHTABLE_(EqualFunction) equalFunction,
HASHTABLE_(CopyFunction) copyFunction, HASHTABLE_(CopyFunction) copyFunction,
HASHTABLE_(FreeFunction) freeFunction, HASHTABLE_(FreeKeyFunction) freeKeyFunction,
HASHTABLE_(FreeValueFunction) freeValueFunction,
uio_uint32 initialSize, uio_uint32 initialSize,
double minFillQuotient, double minFillQuotient,
double maxFillQuotient) { double maxFillQuotient) {
@@ -63,7 +65,8 @@ HASHTABLE_(newHashTable)(
hashTable->hashFunction = hashFunction; hashTable->hashFunction = hashFunction;
hashTable->equalFunction = equalFunction; hashTable->equalFunction = equalFunction;
hashTable->copyFunction = copyFunction; hashTable->copyFunction = copyFunction;
hashTable->freeFunction = freeFunction; hashTable->freeKeyFunction = freeKeyFunction;
hashTable->freeValueFunction = freeValueFunction;
hashTable->minFillQuotient = minFillQuotient; hashTable->minFillQuotient = minFillQuotient;
hashTable->maxFillQuotient = maxFillQuotient; hashTable->maxFillQuotient = maxFillQuotient;
@@ -72,6 +75,7 @@ HASHTABLE_(newHashTable)(
return hashTable; return hashTable;
} }
// Add an entry to the HashTable.
uio_bool uio_bool
HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable, HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable,
const HASHTABLE_(Key) *key, HASHTABLE_(Value) *value) { const HASHTABLE_(Key) *key, HASHTABLE_(Value) *value) {
@@ -104,6 +108,7 @@ HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable,
return true; return true;
} }
// Remove an entry with a specified Key from the HashTable.
uio_bool uio_bool
HASHTABLE_(remove)(HASHTABLE_(HashTable) *hashTable, HASHTABLE_(remove)(HASHTABLE_(HashTable) *hashTable,
const HASHTABLE_(Key) *key) { const HASHTABLE_(Key) *key) {
@@ -122,7 +127,8 @@ HASHTABLE_(remove)(HASHTABLE_(HashTable) *hashTable,
entry = &(*entry)->next; entry = &(*entry)->next;
} }
next = (*entry)->next; next = (*entry)->next;
HASHTABLE_(FREE)(hashTable, (*entry)->key); HASHTABLE_(FREEKEY)(hashTable, (*entry)->key);
HASHTABLE_(FREEVALUE)(hashTable, (*entry)->value);
HASHTABLE_(freeHashEntry)(*entry); HASHTABLE_(freeHashEntry)(*entry);
*entry = next; *entry = next;
@@ -133,6 +139,7 @@ HASHTABLE_(remove)(HASHTABLE_(HashTable) *hashTable,
return true; return true;
} }
// Find the Value stored for some Key.
HASHTABLE_(Value) * HASHTABLE_(Value) *
HASHTABLE_(find)(HASHTABLE_(HashTable) *hashTable, HASHTABLE_(find)(HASHTABLE_(HashTable) *hashTable,
const HASHTABLE_(Key) *key) { const HASHTABLE_(Key) *key) {
@@ -151,11 +158,13 @@ HASHTABLE_(find)(HASHTABLE_(HashTable) *hashTable,
return NULL; return NULL;
} }
// Returns the number of entries in the HashTable.
uio_uint32 uio_uint32
HASHTABLE_(count)(const HASHTABLE_(HashTable) *hashTable) { HASHTABLE_(count)(const HASHTABLE_(HashTable) *hashTable) {
return hashTable->numEntries; return hashTable->numEntries;
} }
// Auxiliary function to (re)initialise the buckets in the HashTable.
static void static void
HASHTABLE_(setup)(HASHTABLE_(HashTable) *hashTable, uio_uint32 initialSize) { HASHTABLE_(setup)(HASHTABLE_(HashTable) *hashTable, uio_uint32 initialSize) {
if (initialSize < 4) if (initialSize < 4)
@@ -175,6 +184,7 @@ HASHTABLE_(setup)(HASHTABLE_(HashTable) *hashTable, uio_uint32 initialSize) {
#endif #endif
} }
// Resize the buckets in the HashTable.
static void static void
HASHTABLE_(resize)(HASHTABLE_(HashTable) *hashTable) { HASHTABLE_(resize)(HASHTABLE_(HashTable) *hashTable) {
HASHTABLE_(HashEntry) **oldEntries; HASHTABLE_(HashEntry) **oldEntries;
@@ -224,6 +234,7 @@ nextPower2(uio_uint32 x) {
return x + 1; 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. // NB: Iterator should be considered invalid if the HashTable is changed.
// TODO: change this (make it thread-safe) // TODO: change this (make it thread-safe)
// this can be done by only marking items as deleted when // this can be done by only marking items as deleted when
@@ -252,21 +263,26 @@ HASHTABLE_(getIterator)(const HASHTABLE_(HashTable) *hashTable) {
return iterator; return iterator;
} }
// Returns true if and only if there are no more entries in the hash table
// for the Iterator to find.
int int
HASHTABLE_(iteratorDone)(const HASHTABLE_(Iterator) *iterator) { HASHTABLE_(iteratorDone)(const HASHTABLE_(Iterator) *iterator) {
return iterator->bucketNr >= iterator->hashTable->size; return iterator->bucketNr >= iterator->hashTable->size;
} }
// Get the Key of the entry pointed to by an Iterator.
HASHTABLE_(Key) * HASHTABLE_(Key) *
HASHTABLE_(iteratorKey)(HASHTABLE_(Iterator) *iterator) { HASHTABLE_(iteratorKey)(HASHTABLE_(Iterator) *iterator) {
return iterator->entry->key; return iterator->entry->key;
} }
// Get the Value of the entry pointed to by an Iterator.
HASHTABLE_(Value) * HASHTABLE_(Value) *
HASHTABLE_(iteratorValue)(HASHTABLE_(Iterator) *iterator) { HASHTABLE_(iteratorValue)(HASHTABLE_(Iterator) *iterator) {
return iterator->entry->value; 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. // Should not be called if the iterator is already past the last entry.
HASHTABLE_(Iterator) * HASHTABLE_(Iterator) *
HASHTABLE_(iteratorNext)(HASHTABLE_(Iterator) *iterator) { HASHTABLE_(iteratorNext)(HASHTABLE_(Iterator) *iterator) {
@@ -293,16 +309,19 @@ HASHTABLE_(iteratorNext)(HASHTABLE_(Iterator) *iterator) {
return iterator; return iterator;
} }
// Free the Iterator.
void void
HASHTABLE_(freeIterator)(HASHTABLE_(Iterator) *iterator) { HASHTABLE_(freeIterator)(HASHTABLE_(Iterator) *iterator) {
uio_free(iterator); uio_free(iterator);
} }
// Auxiliary function to allocate a HashTable.
static inline HASHTABLE_(HashTable) * static inline HASHTABLE_(HashTable) *
HASHTABLE_(allocHashTable)(void) { HASHTABLE_(allocHashTable)(void) {
return uio_malloc(sizeof (HASHTABLE_(HashTable))); return uio_malloc(sizeof (HASHTABLE_(HashTable)));
} }
// Auxiliary function to create a HashEntry.
static inline HASHTABLE_(HashEntry) * static inline HASHTABLE_(HashEntry) *
HASHTABLE_(newHashEntry)(uio_uint32 hash, HASHTABLE_(Key) *key, HASHTABLE_(newHashEntry)(uio_uint32 hash, HASHTABLE_(Key) *key,
HASHTABLE_(Value) *value, HASHTABLE_(HashEntry) *next) { HASHTABLE_(Value) *value, HASHTABLE_(HashEntry) *next) {
@@ -316,11 +335,13 @@ HASHTABLE_(newHashEntry)(uio_uint32 hash, HASHTABLE_(Key) *key,
return result; return result;
} }
// Allocate a new HashEntry.
static inline HASHTABLE_(HashEntry) * static inline HASHTABLE_(HashEntry) *
HASHTABLE_(allocHashEntry)(void) { HASHTABLE_(allocHashEntry)(void) {
return uio_malloc(sizeof (HASHTABLE_(HashEntry))); return uio_malloc(sizeof (HASHTABLE_(HashEntry)));
} }
// Delete the HashTable.
void void
HASHTABLE_(deleteHashTable)(HASHTABLE_(HashTable) *hashTable) { HASHTABLE_(deleteHashTable)(HASHTABLE_(HashTable) *hashTable) {
uio_uint32 i; uio_uint32 i;
@@ -333,7 +354,8 @@ HASHTABLE_(deleteHashTable)(HASHTABLE_(HashTable) *hashTable) {
entry = *bucketPtr; entry = *bucketPtr;
while (entry != NULL) { while (entry != NULL) {
next = entry->next; next = entry->next;
HASHTABLE_(FREE)(hashTable, entry->key); HASHTABLE_(FREEKEY)(hashTable, entry->key);
HASHTABLE_(FREEVALUE)(hashTable, entry->value);
HASHTABLE_(freeHashEntry)(entry); HASHTABLE_(freeHashEntry)(entry);
entry = next; entry = next;
i--; i--;
@@ -344,6 +366,7 @@ HASHTABLE_(deleteHashTable)(HASHTABLE_(HashTable) *hashTable) {
uio_free(hashTable); uio_free(hashTable);
} }
// Auxiliary function to deallocate a HashEntry.
static inline void static inline void
HASHTABLE_(freeHashEntry)(HASHTABLE_(HashEntry) *entry) { HASHTABLE_(freeHashEntry)(HASHTABLE_(HashEntry) *entry) {
uio_free(entry); uio_free(entry);
+21 -6
View File
@@ -39,6 +39,8 @@
// (and typedefs) from the HASHTABLE_ block below. // (and typedefs) from the HASHTABLE_ block below.
// In the .c file, #define HASHTABLE_INTERNAL, #include the .h file // In the .c file, #define HASHTABLE_INTERNAL, #include the .h file
// and hashtable.c (in this order), and add the necessary functions. // 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_ #ifndef HASHTABLE_
# define HASHTABLE_(identifier) HashTable ## _ ## identifier # define HASHTABLE_(identifier) HashTable ## _ ## identifier
typedef void HashTable_Key; typedef void HashTable_Key;
@@ -49,8 +51,10 @@
(hashTable)->equalFunction(hashKey1, hashKey2) (hashTable)->equalFunction(hashKey1, hashKey2)
# define HashTable_COPY(hashTable, hashKey) \ # define HashTable_COPY(hashTable, hashKey) \
(hashTable)->copyFunction(hashKey) (hashTable)->copyFunction(hashKey)
# define HashTable_FREE(hashTable, hashKey) \ # define HashTable_FREEKEY(hashTable, hashKey) \
(hashTable)->freeFunction(hashKey) (hashTable)->freeKeyFunction(hashKey)
# define HashTable_FREEVALUE(hashTable, hashValue) \
(hashTable)->freeValueFunction(hashKey)
#endif #endif
@@ -59,8 +63,9 @@ typedef uio_uint32 (*HASHTABLE_(HashFunction))(const HASHTABLE_(Key) *);
typedef uio_bool (*HASHTABLE_(EqualFunction))(const HASHTABLE_(Key) *, typedef uio_bool (*HASHTABLE_(EqualFunction))(const HASHTABLE_(Key) *,
const HASHTABLE_(Key) *); const HASHTABLE_(Key) *);
typedef HASHTABLE_(Value) *(*HASHTABLE_(CopyFunction))( typedef HASHTABLE_(Value) *(*HASHTABLE_(CopyFunction))(
const HASHTABLE_(Value) *); const HASHTABLE_(Key) *);
typedef void (*HASHTABLE_(FreeFunction))(HASHTABLE_(Value) *); typedef void (*HASHTABLE_(FreeKeyFunction))(HASHTABLE_(Key) *);
typedef void (*HASHTABLE_(FreeValueFunction))(HASHTABLE_(Value) *);
typedef struct HASHTABLE_(HashTable) HASHTABLE_(HashTable); typedef struct HASHTABLE_(HashTable) HASHTABLE_(HashTable);
typedef struct HASHTABLE_(HashEntry) HASHTABLE_(HashEntry); typedef struct HASHTABLE_(HashEntry) HASHTABLE_(HashEntry);
@@ -68,9 +73,17 @@ typedef struct HASHTABLE_(Iterator) HASHTABLE_(Iterator);
struct HASHTABLE_(HashTable) { struct HASHTABLE_(HashTable) {
HASHTABLE_(HashFunction) hashFunction; HASHTABLE_(HashFunction) hashFunction;
// Function creating a uio_uint32 hash of a key.
HASHTABLE_(EqualFunction) equalFunction; HASHTABLE_(EqualFunction) equalFunction;
// Function used to compare two keys.
HASHTABLE_(CopyFunction) copyFunction; 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; double minFillQuotient;
// How much of half of the hashtable needs to be filled // How much of half of the hashtable needs to be filled
@@ -114,7 +127,9 @@ HASHTABLE_(HashTable) *HASHTABLE_(newHashTable)(
HASHTABLE_(HashFunction) hashFunction, HASHTABLE_(HashFunction) hashFunction,
HASHTABLE_(EqualFunction) equalFunction, HASHTABLE_(EqualFunction) equalFunction,
HASHTABLE_(CopyFunction) copyFunction, HASHTABLE_(CopyFunction) copyFunction,
HASHTABLE_(FreeFunction) freeFunction, uio_uint32 initialSize, HASHTABLE_(FreeKeyFunction) freeKeyFunction,
HASHTABLE_(FreeValueFunction) freeValueFunction,
uio_uint32 initialSize,
double minFillQuotient, double maxFillQuotient); double minFillQuotient, double maxFillQuotient);
uio_bool HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable, uio_bool HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable,
const HASHTABLE_(Key) *key, HASHTABLE_(Value) *value); const HASHTABLE_(Key) *key, HASHTABLE_(Value) *value);