uarray.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. ** Command & Conquer Generals(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. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Library *
  23. * *
  24. * $Archive:: /G/wwlib/uarray.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 9/24/99 1:56p $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * UniqueArrayClass<T>::UniqueArrayClass -- constructor *
  35. * UniqueArrayClass<T>::~UniqueArrayClass -- destructor *
  36. * UniqueArrayClass<T>::Add -- Add an item to the array *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #if _MSC_VER >= 1000
  39. #pragma once
  40. #endif // _MSC_VER >= 1000
  41. #ifndef UARRAY_H
  42. #define UARRAY_H
  43. #ifndef HASHCALC_H
  44. #include "hashcalc.h"
  45. #endif
  46. #ifndef VECTOR_H
  47. #include "vector.h"
  48. #endif
  49. /*
  50. ** UniqueArrayClass
  51. ** This template class can be used to generate an array of unique objects
  52. ** amongst a huge list of objects which may or may not be unique. However,
  53. ** in order to use the UniqueArrayClass, you will need to implement a
  54. ** HashCalculatorClass for the type you are using.
  55. **
  56. ** Note that the UniqueArrayClass does *copies* of the objects you are
  57. ** giving it. It is meant to be used with relatively lightweight objects.
  58. */
  59. template <class T> class UniqueArrayClass
  60. {
  61. public:
  62. UniqueArrayClass(int initialsize,int growthrate,HashCalculatorClass<T> * hasher);
  63. ~UniqueArrayClass(void);
  64. int Add(const T & new_item);
  65. int Count(void) const { return Get_Unique_Count(); }
  66. int Get_Unique_Count(void) const { return UniqueItems.Count(); }
  67. const T & Get(int index) const { return UniqueItems[index].Item; }
  68. const T & operator [] (int index) const { return Get(index); }
  69. private:
  70. enum { NO_ITEM = 0xFFFFFFFF };
  71. class HashItem
  72. {
  73. public:
  74. T Item;
  75. int NextHashIndex;
  76. bool operator == (const HashItem & that) { return ((Item == that.Item) && (NextHashIndex == that.NextHashIndex)); }
  77. bool operator != (const HashItem & that) { return !(*this == that); }
  78. };
  79. // Dynamic Vector of the unique items:
  80. DynamicVectorClass<HashItem> UniqueItems;
  81. // Hash table:
  82. int HashTableSize;
  83. int * HashTable;
  84. // object which does the hashing for the type
  85. HashCalculatorClass<T> * HashCalculator;
  86. friend class VectorClass<T>;
  87. friend class DynamicVectorClass<T>;
  88. };
  89. /***********************************************************************************************
  90. * UniqueArrayClass<T>::UniqueArrayClass -- constructor *
  91. * *
  92. * INPUT: *
  93. * *
  94. * OUTPUT: *
  95. * *
  96. * WARNINGS: *
  97. * *
  98. * HISTORY: *
  99. * 5/29/98 GTH : Created. *
  100. *=============================================================================================*/
  101. template <class T>
  102. UniqueArrayClass<T>::UniqueArrayClass(int initial_size,int growth_rate,HashCalculatorClass<T> * hasher) :
  103. UniqueItems(initial_size),
  104. HashCalculator(hasher)
  105. {
  106. // set the growth rate.
  107. UniqueItems.Set_Growth_Step(growth_rate);
  108. // sizing and allocating the actual hash table
  109. int bits = HashCalculator->Num_Hash_Bits();
  110. assert(bits > 0);
  111. assert(bits < 24);
  112. HashTableSize = 1<<bits;
  113. HashTable = new int[HashTableSize];
  114. for (int hidx=0; hidx < HashTableSize; hidx++) {
  115. HashTable[hidx] = NO_ITEM;
  116. }
  117. }
  118. /***********************************************************************************************
  119. * UniqueArrayClass<T>::~UniqueArrayClass -- destructor *
  120. * *
  121. * INPUT: *
  122. * *
  123. * OUTPUT: *
  124. * *
  125. * WARNINGS: *
  126. * *
  127. * HISTORY: *
  128. * 5/29/98 GTH : Created. *
  129. *=============================================================================================*/
  130. template <class T>
  131. UniqueArrayClass<T>::~UniqueArrayClass(void)
  132. {
  133. if (HashTable != NULL) {
  134. delete[] HashTable;
  135. HashTable = NULL;
  136. }
  137. }
  138. /***********************************************************************************************
  139. * UniqueArrayClass<T>::Add -- Add an item to the array *
  140. * *
  141. * Only adds the item to the end of the array if another duplicate item is not found. Returns *
  142. * the array index of where the item is stored. *
  143. * *
  144. * INPUT: *
  145. * *
  146. * OUTPUT: *
  147. * *
  148. * WARNINGS: *
  149. * *
  150. * HISTORY: *
  151. * 5/29/98 GTH : Created. *
  152. *=============================================================================================*/
  153. template <class T>
  154. inline int UniqueArrayClass<T>::Add(const T & new_item)
  155. {
  156. /*
  157. ** Use the hash table to quickly (hopefully :-) detect
  158. ** whether this item is already in the array
  159. */
  160. int num_hash_vals;
  161. HashCalculator->Compute_Hash(new_item);
  162. num_hash_vals = HashCalculator->Num_Hash_Values();
  163. unsigned int lasthash = 0xFFFFFFFF;
  164. unsigned int hash;
  165. for (int hidx = 0; hidx < num_hash_vals; hidx++) {
  166. hash = HashCalculator->Get_Hash_Value(hidx);
  167. if (hash != lasthash) {
  168. int test_item_index = HashTable[hash];
  169. while (test_item_index != 0xFFFFFFFF) {
  170. if (HashCalculator->Items_Match(UniqueItems[test_item_index].Item,new_item)) {
  171. return test_item_index;
  172. }
  173. test_item_index = UniqueItems[test_item_index].NextHashIndex;
  174. }
  175. }
  176. lasthash = hash;
  177. }
  178. /*
  179. ** Ok, this is a new item so add it (copy it!) into the array
  180. */
  181. int index = UniqueItems.Count();
  182. int hash_index = HashCalculator->Get_Hash_Value(0);
  183. HashItem entry;
  184. entry.Item = new_item;
  185. entry.NextHashIndex = HashTable[hash_index];
  186. HashTable[hash_index] = index;
  187. UniqueItems.Add(entry);
  188. return index;
  189. }
  190. #endif // UARRAY_H