GameTools gameHashTable
Hash tables and Checksums are convenient ways to store bulk data in random order and yet retrieve it quickly.
gameHashTable
gameHashTable supports a table of key/value pairs. The 'value' can either be a 'void *' pointer or an integer. Use gameHashTable::getFreeKey() to get a guaranteed-unique key - or use gameHashTable::getFreeKeyBlock() to get a number of consecutive, unusued keys - it returns the first of that set. Alternatively, you can just make up your own keys - so long as they are unique. The gameChecksum method is an excellent way to do that. The remaining hash table functions are pretty obvious:
class gameHashTable { gameHashTable ( unsigned int size = DEFAULT_HASHTABLE_SIZE ) ; ~gameHashTable () ; int getInt ( unsigned int key ) ; void add ( unsigned int key, int val ) ; void *get ( unsigned int key ) ; void add ( unsigned int key, void *data ) ; void remove ( unsigned int key ) ; unsigned int getFreeKeyBlock ( unsigned int numKeys ) ; unsigned int getFreeKey () ; } ;
gameChecksum
To calculate a virtually unique checksum for a text string, the most convenient method is to call:
unsigned int getChecksum ( const char *s ) ;
...which is a reasonably fast 32 bit checksum generator for null-terminated strings. If you need to calculate the checksum of some other kind of data, you need to use the underlying class gameChecksum:
class gameChecksum { gameChecksum() ; void clear() ; void add ( const char *s ) ; void add ( const unsigned char *b, int length ) ; void add ( unsigned char b ) ; void add ( int x ) ; void add ( bool x ) ; void add ( short x ) ; void add ( unsigned short x ) ; void add ( unsigned int x ) ; void add ( signed char x ) ; void add ( double x ) ; void add ( float x ) ; unsigned int get () ; } ;
The idea is to call gameChecksum::clear() (which is automatically done in the constructor function) to 'empty' the checksum generator, then call any of the gameChecksum::add(...) functions in any order and in any combination to add data that you'd like to be included into the check sum. When you are done, call gameChecksum::get() to get the resulting checksum code.
The checksum is guaranteed never to be zero - so you can safely use a zero checksum to flag error conditions in code that uses it. Whilst it is astronomically unlikely that two data structures (or strings or whatever) would generate the same result, it is possible that this could happen - so mission-critical code needs to check for this situation. A probability of one in four billion of a collision is good enough for many applications - but only a perfect checksum algorithm generates all codes with equal probability - so your odds are probably not that good.
Wikiid Pages relating to gameTools (edit) |
gameTools - Main page |
gameTools - Support Tools : |
gameTools - File Formats : |
gameTools - Source Code :
|
Wikiid Pages relating to Lemur of Lima (edit) |
Lemur of Lima - Main page |
Lemur of Lima - Controls |
Lemur of Lima - Levels : |
Lemur of Lima - Java Plugins : |
Lemur of Lima - Source Code Documentation : |