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:
@@ -94,7 +94,7 @@ copyFile (uio_DirHandle *srcDir, const char *srcName,
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ static inline uio_bool CharHashTable_equal(CharHashTable_HashTable *hashTable,
|
||||
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 @@ CharHashTable_copy(CharHashTable_HashTable *hashTable,
|
||||
}
|
||||
|
||||
static inline void
|
||||
CharHashTable_free(CharHashTable_HashTable *hashTable,
|
||||
CharHashTable_freeKey(CharHashTable_HashTable *hashTable,
|
||||
char *key) {
|
||||
(void) *hashTable;
|
||||
uio_free(key);
|
||||
|
||||
@@ -28,7 +28,9 @@ typedef void HASHTABLE_(Value);
|
||||
#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"
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ typedef struct CharHashTable_HashTable uio_GPDirEntries;
|
||||
|
||||
#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)
|
||||
|
||||
@@ -46,12 +46,14 @@ static inline HASHTABLE_(HashEntry) *HASHTABLE_(allocHashEntry)(void);
|
||||
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_(newHashTable)(
|
||||
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 @@ HASHTABLE_(newHashTable)(
|
||||
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 @@ HASHTABLE_(add)(HASHTABLE_(HashTable) *hashTable,
|
||||
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 @@ HASHTABLE_(remove)(HASHTABLE_(HashTable) *hashTable,
|
||||
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 @@ HASHTABLE_(remove)(HASHTABLE_(HashTable) *hashTable,
|
||||
return true;
|
||||
}
|
||||
|
||||
// Find the Value stored for some Key.
|
||||
HASHTABLE_(Value) *
|
||||
HASHTABLE_(find)(HASHTABLE_(HashTable) *hashTable,
|
||||
const HASHTABLE_(Key) *key) {
|
||||
@@ -151,11 +158,13 @@ HASHTABLE_(find)(HASHTABLE_(HashTable) *hashTable,
|
||||
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 @@ HASHTABLE_(setup)(HASHTABLE_(HashTable) *hashTable, uio_uint32 initialSize) {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Resize the buckets in the HashTable.
|
||||
static void
|
||||
HASHTABLE_(resize)(HASHTABLE_(HashTable) *hashTable) {
|
||||
HASHTABLE_(HashEntry) **oldEntries;
|
||||
@@ -224,6 +234,7 @@ nextPower2(uio_uint32 x) {
|
||||
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 @@ HASHTABLE_(getIterator)(const HASHTABLE_(HashTable) *hashTable) {
|
||||
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 @@ HASHTABLE_(iteratorNext)(HASHTABLE_(Iterator) *iterator) {
|
||||
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 @@ HASHTABLE_(newHashEntry)(uio_uint32 hash, HASHTABLE_(Key) *key,
|
||||
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 @@ HASHTABLE_(deleteHashTable)(HASHTABLE_(HashTable) *hashTable) {
|
||||
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 @@ HASHTABLE_(deleteHashTable)(HASHTABLE_(HashTable) *hashTable) {
|
||||
uio_free(hashTable);
|
||||
}
|
||||
|
||||
// Auxiliary function to deallocate a HashEntry.
|
||||
static inline void
|
||||
HASHTABLE_(freeHashEntry)(HASHTABLE_(HashEntry) *entry) {
|
||||
uio_free(entry);
|
||||
|
||||
@@ -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_uint32 (*HASHTABLE_(HashFunction))(const HASHTABLE_(Key) *);
|
||||
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 @@ typedef struct HASHTABLE_(Iterator) HASHTABLE_(Iterator);
|
||||
|
||||
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_(HashTable) *HASHTABLE_(newHashTable)(
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user