https://www.sjbaker.org/wiki/index.php?title=GameTools_gameHashTable&feed=atom&action=historyGameTools gameHashTable - Revision history2021-05-05T21:51:56ZRevision history for this page on the wikiMediaWiki 1.24.4https://www.sjbaker.org/wiki/index.php?title=GameTools_gameHashTable&diff=83&oldid=prevSteveBaker: New page: 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....2007-10-24T04:39:31Z<p>New page: 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....</p>
<p><b>New page</b></p><div>Hash tables and Checksums are convenient ways to store bulk data in random order and yet retrieve it quickly.<br />
<br />
== gameHashTable ==<br />
<br />
'''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|gameChecksum]]'' method is an excellent way to do that. The remaining hash table functions are pretty obvious:<br />
<br />
class gameHashTable<br />
{<br />
gameHashTable ( unsigned int size = DEFAULT_HASHTABLE_SIZE ) ;<br />
~gameHashTable () ;<br />
int getInt ( unsigned int key ) ;<br />
void add ( unsigned int key, int val ) ;<br />
void *get ( unsigned int key ) ;<br />
void add ( unsigned int key, void *data ) ;<br />
void remove ( unsigned int key ) ;<br />
unsigned int getFreeKeyBlock ( unsigned int numKeys ) ;<br />
unsigned int getFreeKey () ;<br />
} ;<br />
<br />
== gameChecksum ==<br />
<br />
To calculate a virtually unique checksum for a text string, the most convenient method is to call:<br />
<br />
unsigned int getChecksum ( const char *s ) ;<br />
<br />
...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'':<br />
<br />
class gameChecksum<br />
{<br />
gameChecksum() ;<br />
void clear() ;<br />
void add ( const char *s ) ;<br />
void add ( const unsigned char *b, int length ) ;<br />
void add ( unsigned char b ) ;<br />
void add ( int x ) ;<br />
void add ( bool x ) ;<br />
void add ( short x ) ;<br />
void add ( unsigned short x ) ;<br />
void add ( unsigned int x ) ;<br />
void add ( signed char x ) ;<br />
void add ( double x ) ;<br />
void add ( float x ) ;<br />
unsigned int get () ;<br />
} ;<br />
<br />
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.<br />
<br />
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.<br />
<br />
{{gameTools}}<br />
{{LoL}}</div>SteveBaker