hash.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /VSS_Sync/wwlib/hash.cpp 3 10/17/00 4:48p Vss_sync $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando / G 3D Library *
  24. * *
  25. * $Archive:: /VSS_Sync/wwlib/hash.cpp $*
  26. * *
  27. * Author:: Greg_h *
  28. * *
  29. * $Modtime:: 10/16/00 11:42a $*
  30. * *
  31. * $Revision:: 3 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include "hash.h"
  37. #include "wwdebug.h"
  38. #include "realcrc.h"
  39. #ifdef _UNIX
  40. #include "osdep.h"
  41. #endif
  42. #include <string.h>
  43. /*
  44. ** HashTableClass
  45. */
  46. HashTableClass::HashTableClass( int size ) :
  47. HashTableSize( size )
  48. {
  49. // Assert HashTableSize is a power of 2
  50. WWASSERT( (HashTableSize & (HashTableSize-1)) == 0 );
  51. // Allocate and clear the table
  52. HashTable = new HashableClass * [ HashTableSize ];
  53. Reset();
  54. }
  55. HashTableClass::~HashTableClass( void )
  56. {
  57. // If we need to, free the hash table
  58. if ( HashTable != NULL) {
  59. delete [] HashTable;
  60. HashTable = NULL;
  61. }
  62. }
  63. void HashTableClass::Reset( void )
  64. {
  65. for ( int i = 0; i < HashTableSize; i++ ) {
  66. HashTable[i] = NULL;
  67. }
  68. }
  69. void HashTableClass::Add( HashableClass * entry )
  70. {
  71. WWASSERT( entry != NULL);
  72. int index = Hash( entry->Get_Key() );
  73. WWASSERT( entry->NextHash == NULL );
  74. entry->NextHash = HashTable[ index ];
  75. HashTable[ index ] = entry;
  76. }
  77. bool HashTableClass::Remove( HashableClass * entry )
  78. {
  79. WWASSERT(entry != NULL);
  80. // Find in the hash table.
  81. const char *key = entry->Get_Key();
  82. int index = Hash( key );
  83. if ( HashTable[ index ] != NULL ) {
  84. // Special check for first entry
  85. if ( HashTable[ index ] == entry ) {
  86. HashTable[ index ] = entry->NextHash;
  87. return true;
  88. }
  89. // Search the list for the entry, and remove it
  90. HashableClass * node = HashTable[ index ];
  91. while ( node->NextHash != NULL ) {
  92. if ( node->NextHash == entry ) {
  93. node->NextHash = entry->NextHash;
  94. return true;
  95. }
  96. node = node->NextHash;
  97. }
  98. }
  99. return false;
  100. }
  101. HashableClass * HashTableClass::Find( const char * key )
  102. {
  103. // Find in the hash table.
  104. int index = Hash( key );
  105. for ( HashableClass * node = HashTable[ index ]; node != NULL; node = node->NextHash ) {
  106. if ( ::stricmp( node->Get_Key(), key ) == 0 ) {
  107. return node;
  108. }
  109. }
  110. return NULL;
  111. }
  112. int HashTableClass::Hash( const char * key )
  113. {
  114. return CRC_Stringi( key ) & (HashTableSize-1);
  115. }
  116. /*
  117. **
  118. */
  119. void HashTableIteratorClass::First(void)
  120. {
  121. Index = 0;
  122. NextEntry = Table.HashTable[ Index ];
  123. Advance_Next();
  124. Next(); // Accept the next we found, and go to the next next
  125. }
  126. void HashTableIteratorClass::Next(void)
  127. {
  128. CurrentEntry = NextEntry;
  129. if ( NextEntry != NULL ) {
  130. NextEntry = NextEntry->NextHash;
  131. Advance_Next();
  132. }
  133. }
  134. void HashTableIteratorClass::Advance_Next(void)
  135. {
  136. while ( NextEntry == NULL ) {
  137. Index++;
  138. if ( Index >= Table.HashTableSize ) {
  139. return; // Done!
  140. }
  141. NextEntry = Table.HashTable[ Index ];
  142. }
  143. }