hash.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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: /Commando/Code/wwlib/hash.h 2 6/15/00 10:53a Byon_g $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando / G 3D Library *
  24. * *
  25. * $Archive:: /Commando/Code/wwlib/hash.h $*
  26. * *
  27. * Author:: Greg_h *
  28. * *
  29. * $Modtime:: 6/15/00 9:43a $*
  30. * *
  31. * $Revision:: 2 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if defined(_MSC_VER)
  37. #pragma once
  38. #endif
  39. #ifndef HASH_H
  40. #define HASH_H
  41. #include "always.h"
  42. class HashTableIteratorClass;
  43. /*
  44. ** HashableClass
  45. */
  46. class HashableClass {
  47. public:
  48. HashableClass( void ) : NextHash( NULL ) {}
  49. virtual ~HashableClass( void ) {}
  50. virtual const char * Get_Key( void ) = 0;
  51. private:
  52. HashableClass * NextHash;
  53. friend class HashTableClass;
  54. friend class HashTableIteratorClass;
  55. };
  56. /*
  57. ** HashTableClass
  58. */
  59. class HashTableClass {
  60. public:
  61. HashTableClass( int size );
  62. ~HashTableClass( void );
  63. void Reset( void );
  64. void Add( HashableClass * entry );
  65. bool Remove( HashableClass * entry );
  66. HashableClass * Find( const char * key );
  67. private:
  68. // HashTableSize MUST be a power of two
  69. int HashTableSize;
  70. HashableClass * * HashTable;
  71. // Convert key to a table index
  72. int Hash( const char * key );
  73. friend class HashTableIteratorClass;
  74. };
  75. /*
  76. **
  77. */
  78. class HashTableIteratorClass
  79. {
  80. public:
  81. HashTableIteratorClass( HashTableClass & table ) : Table( table ) {}
  82. virtual ~HashTableIteratorClass( void ) {}
  83. void First( void );
  84. void Next( void );
  85. bool Is_Done( void ) { return CurrentEntry == NULL; }
  86. HashableClass * Get_Current( void ) { return CurrentEntry; }
  87. private:
  88. const HashTableClass & Table;
  89. int Index;
  90. HashableClass * CurrentEntry;
  91. HashableClass * NextEntry;
  92. void Advance_Next( void );
  93. };
  94. #endif // HASH_H