hashtemplate.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/hashtemplate.h $*
  25. * *
  26. * Author:: Greg_h *
  27. * *
  28. * $Modtime:: 7/11/01 9:35p $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef HASH_TEMPLATE_H
  39. #define HASH_TEMPLATE_H
  40. #include "always.h"
  41. #include "wwstring.h"
  42. // Class for providing hash values
  43. template <class Key> class HashTemplateKeyClass
  44. {
  45. public:
  46. static inline unsigned int Get_Hash_Value (const Key& k);
  47. };
  48. // Default hash function for data types that can be cast into an unsigned int
  49. template <class Key> inline unsigned int HashTemplateKeyClass<Key>::Get_Hash_Value (const Key& k)
  50. {
  51. unsigned int hval = *((const unsigned int*)(&k));
  52. hval = hval + (hval>>5) + (hval>>10) + (hval >> 20);
  53. return hval;
  54. }
  55. // Specialization for floating point hash values (as the default dword-
  56. // casting yields a very bad hash function)
  57. template <> inline unsigned int HashTemplateKeyClass<float>::Get_Hash_Value (const float& s)
  58. {
  59. unsigned int z = *((const unsigned int*)(&s));
  60. return ((z>>22)+(z>>12)+(z));
  61. }
  62. // Hash class
  63. template <class KeyType, class ValueType>
  64. class HashTemplateClass
  65. {
  66. struct Entry;
  67. public:
  68. enum
  69. {
  70. NIL = -1 // internal enumeration for representing a NULL link
  71. };
  72. HashTemplateClass(void);
  73. ~HashTemplateClass(void);
  74. void Insert(const KeyType& s, const ValueType& d);
  75. void Remove(const KeyType& s);
  76. void Remove(const KeyType& s, const ValueType& d);
  77. ValueType Get(const KeyType& s) const;
  78. bool Get(const KeyType& s, ValueType& d) const;
  79. bool Exists(const KeyType& s) const;
  80. bool Exists(const KeyType& s, const ValueType& d) const;
  81. void Remove_All(void);
  82. unsigned int Get_Size(void) const;
  83. int* Get_Hash() { return Hash; }
  84. Entry* Get_Table() { return Table; }
  85. private:
  86. HashTemplateClass (const HashTemplateClass&); // not allowed
  87. HashTemplateClass& operator= (const HashTemplateClass&); // not allowed
  88. static unsigned int Get_Hash_Val(const KeyType& s, const unsigned int hash_array_size);
  89. void Re_Hash(void);
  90. int Alloc_Entry(void);
  91. struct Entry
  92. {
  93. int Next; // next pointer in linked list
  94. KeyType Key; // key
  95. ValueType Value; // value
  96. };
  97. int* Hash; // hash pointers
  98. Entry* Table; // allocation table
  99. int First; // handle of first free entry
  100. unsigned int Size; // size of hash table
  101. };
  102. template <class KeyType, class ValueType>
  103. class HashTemplateIterator
  104. {
  105. int HashIndex; // index to hash pointer table
  106. int Handle;
  107. HashTemplateClass<KeyType,ValueType>& HashTable;
  108. public:
  109. HashTemplateIterator(HashTemplateClass<KeyType,ValueType>& hash_table)
  110. :
  111. HashTable(hash_table)
  112. {
  113. First();
  114. }
  115. void First()
  116. {
  117. Handle=HashTemplateClass<KeyType,ValueType>::NIL;
  118. int size=HashTable.Get_Size();
  119. for (HashIndex=0;HashIndex<size;++HashIndex) {
  120. Handle = HashTable.Get_Hash()[HashIndex];
  121. if (Handle!=HashTemplateClass<KeyType,ValueType>::NIL) break;
  122. }
  123. }
  124. void Next()
  125. {
  126. Handle=HashTable.Get_Table()[Handle].Next;
  127. if (Handle==HashTemplateClass<KeyType,ValueType>::NIL) {
  128. int size=HashTable.Get_Size();
  129. for (++HashIndex;HashIndex<size;++HashIndex) {
  130. Handle = HashTable.Get_Hash()[HashIndex];
  131. if (Handle!=HashTemplateClass<KeyType,ValueType>::NIL) break;
  132. }
  133. }
  134. }
  135. bool Is_Done() const
  136. {
  137. return HashIndex==int(HashTable.Get_Size());
  138. }
  139. ValueType& Peek_Value()
  140. {
  141. return HashTable.Get_Table()[Handle].Value;
  142. }
  143. };
  144. //------------------------------------------------------------------------
  145. // Implementation
  146. //------------------------------------------------------------------------
  147. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Insert(const KeyType& s, const ValueType& d)
  148. {
  149. int h;
  150. h = Alloc_Entry();
  151. unsigned int hval = Get_Hash_Val(s,Size);
  152. Table[h].Key = s;
  153. Table[h].Value = d;
  154. Table[h].Next = Hash[hval];
  155. Hash[hval] = h;
  156. }
  157. template <class KeyType, class ValueType> inline unsigned int HashTemplateClass<KeyType,ValueType>::Get_Size (void) const
  158. {
  159. return Size;
  160. }
  161. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Remove_All (void)
  162. {
  163. for (unsigned int i = 0; i < Size; i++)
  164. {
  165. int f = Hash[i];
  166. if (f!=NIL)
  167. {
  168. int h = f;
  169. while (Table[h].Next != NIL)
  170. h = Table[h].Next;
  171. Table[h].Next = First; // link to first free
  172. First = f; // link to beginning
  173. Hash[i] = NIL; // kill entry in hash
  174. }
  175. }
  176. }
  177. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Remove (const KeyType& s)
  178. {
  179. unsigned int hval = Get_Hash_Val(s,Size);
  180. int prev = NIL;
  181. int h = Hash[hval];
  182. while (h != NIL)
  183. {
  184. if (Table[h].Key == s)
  185. {
  186. if (prev!=NIL)
  187. Table[prev].Next = Table[h].Next;
  188. else
  189. Hash[hval] = Table[h].Next;
  190. Table[h].Next = First;
  191. First = h;
  192. return;
  193. }
  194. prev = h;
  195. h = Table[h].Next;
  196. }
  197. }
  198. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Remove (const KeyType& s, const ValueType& d)
  199. {
  200. unsigned int hval = Get_Hash_Val(s,Size);
  201. int prev = NIL;
  202. int h = Hash[hval];
  203. while (h != NIL)
  204. {
  205. if (Table[h].Key == s && Table[h].Value == d)
  206. {
  207. if (prev!=NIL)
  208. Table[prev].Next = Table[h].Next;
  209. else
  210. Hash[hval] = Table[h].Next;
  211. Table[h].Next = First;
  212. First = h;
  213. return;
  214. }
  215. prev = h;
  216. h = Table[h].Next;
  217. }
  218. }
  219. template <class KeyType, class ValueType> inline ValueType HashTemplateClass<KeyType,ValueType>::Get (const KeyType& s) const
  220. {
  221. int h = Hash[Get_Hash_Val(s,Size)];
  222. while (h!=NIL)
  223. {
  224. if (Table[h].Key == s)
  225. return Table[h].Value;
  226. h = Table[h].Next;
  227. }
  228. return ValueType(0);
  229. }
  230. template <class KeyType, class ValueType> inline bool HashTemplateClass<KeyType,ValueType>::Get(const KeyType& s, ValueType& d) const
  231. {
  232. int h = Hash[Het_Hash_Val(s,Size)];
  233. while (h!=NIL)
  234. {
  235. if (Table[h].Key == s)
  236. {
  237. d = Table[h].Value;
  238. return true;
  239. }
  240. h = Table[h].Next;
  241. }
  242. return false;
  243. }
  244. template <class KeyType, class ValueType> inline bool HashTemplateClass<KeyType,ValueType>::Exists(const KeyType& s) const
  245. {
  246. int h = Hash[Get_Hash_Val(s,Size)];
  247. while (h!=NIL)
  248. {
  249. if (Table[h].Key == s)
  250. return true;
  251. h = Table[h].Next;
  252. }
  253. return false;
  254. }
  255. template <class KeyType, class ValueType> inline bool HashTemplateClass<KeyType,ValueType>::Exists(const KeyType& s, const ValueType& d) const
  256. {
  257. int h = Hash[Get_Hash_Val(s,Size)];
  258. while (h!=NIL)
  259. {
  260. if (Table[h].Key == s && Table[h].Value == d)
  261. return true;
  262. h = Table[h].Next;
  263. }
  264. return false;
  265. }
  266. template <class KeyType, class ValueType> inline unsigned int HashTemplateClass<KeyType,ValueType>::Get_Hash_Val(const KeyType& s, const unsigned int hash_array_size)
  267. {
  268. return HashTemplateKeyClass<KeyType>::Get_Hash_Value (s) & (hash_array_size-1);
  269. }
  270. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Re_Hash()
  271. {
  272. unsigned int new_size = Size*2;
  273. if (new_size < 4)
  274. new_size = 4;
  275. Entry *new_table = W3DNEWARRAY Entry[new_size];
  276. int *new_hash = W3DNEWARRAY int[new_size];
  277. int cnt = 0;
  278. int i;
  279. for (i = 0; i < (int)new_size; i++)
  280. {
  281. new_table[i].Next = NIL;
  282. new_hash[i] = NIL;
  283. }
  284. if (Size) // if we have existing data, it needs to be rehashed
  285. {
  286. for (i = 0; i < (int)Size; i++) // step through each old hash set
  287. {
  288. int h = Hash[i];
  289. while (h != NIL)
  290. {
  291. unsigned int hVal = Get_Hash_Val(Table[h].Key, new_size);
  292. new_table[cnt].Key = Table[h].Key;
  293. new_table[cnt].Value = Table[h].Value;
  294. new_table[cnt].Next = new_hash[hVal];
  295. new_hash[hVal] = cnt;
  296. cnt++;
  297. h = Table[h].Next;
  298. }
  299. }
  300. delete[] Hash;
  301. delete[] Table;
  302. }
  303. for (i = cnt; i < (int)new_size; i++)
  304. new_table[i].Next = i+1;
  305. new_table[new_size-1].Next = NIL;
  306. First = cnt;
  307. Hash = new_hash;
  308. Table = new_table;
  309. Size = new_size;
  310. }
  311. template <class KeyType, class ValueType> inline int HashTemplateClass<KeyType,ValueType>::Alloc_Entry()
  312. {
  313. if (First == NIL) // need to re-alloc and re-hash tables
  314. Re_Hash();
  315. int h = First;
  316. First = Table[First].Next;
  317. return h;
  318. }
  319. template <class KeyType, class ValueType> inline HashTemplateClass<KeyType,ValueType>::HashTemplateClass() : Hash(0),Table(0),First(NIL),Size(0)
  320. {
  321. Re_Hash();
  322. }
  323. template <class KeyType, class ValueType> inline HashTemplateClass<KeyType,ValueType>::~HashTemplateClass()
  324. {
  325. if (Hash)
  326. delete[] Hash;
  327. if (Table)
  328. delete[] Table;
  329. }
  330. // Get_Hash_Value specialization for StringClass. This is intended to be used
  331. // for filenames, so it takes into account the four characters that come before
  332. // the file extension (.xxx - extension expected to be 4 characters).
  333. template <> inline unsigned int HashTemplateKeyClass<StringClass>::Get_Hash_Value(const StringClass& s)
  334. {
  335. unsigned int len=s.Get_Length();
  336. unsigned char* buffer=(unsigned char*)s.Peek_Buffer();
  337. if (len<8) {
  338. unsigned int hval=0;
  339. for (unsigned int a=0;a<len;++a) {
  340. hval+=37*hval+buffer[a];
  341. }
  342. return hval;
  343. }
  344. unsigned int hval = *((const unsigned int*)(buffer+len-8));
  345. hval = hval + (hval>>5) + (hval>>10) + (hval >> 20);
  346. return hval;
  347. }
  348. #endif