hashtemplate.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. /***********************************************************************************************
  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:: 11/19/01 12:16p $*
  29. * *
  30. * $Revision:: 7 $*
  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 Set_Value(const KeyType& s, const ValueType& d);
  76. void Remove(const KeyType& s);
  77. void Remove(const KeyType& s, const ValueType& d);
  78. ValueType Get(const KeyType& s) const;
  79. bool Get(const KeyType& s, ValueType& d) const;
  80. bool Exists(const KeyType& s) const;
  81. bool Exists(const KeyType& s, const ValueType& d) const;
  82. void Remove_All(void);
  83. unsigned int Get_Size(void) const;
  84. int* Get_Hash() { return Hash; }
  85. Entry* Get_Table() { return Table; }
  86. private:
  87. HashTemplateClass (const HashTemplateClass&); // not allowed
  88. HashTemplateClass& operator= (const HashTemplateClass&); // not allowed
  89. static unsigned int Get_Hash_Val(const KeyType& s, const unsigned int hash_array_size);
  90. void Re_Hash(void);
  91. int Alloc_Entry(void);
  92. struct Entry
  93. {
  94. int Next; // next pointer in linked list
  95. KeyType Key; // key
  96. ValueType Value; // value
  97. };
  98. int* Hash; // hash pointers
  99. Entry* Table; // allocation table
  100. int First; // handle of first free entry
  101. unsigned int Size; // size of hash table
  102. };
  103. template <class KeyType, class ValueType>
  104. class HashTemplateIterator
  105. {
  106. int HashIndex; // index to hash pointer table
  107. int Handle;
  108. HashTemplateClass<KeyType,ValueType>& HashTable;
  109. public:
  110. HashTemplateIterator(HashTemplateClass<KeyType,ValueType>& hash_table)
  111. :
  112. HashTable(hash_table)
  113. {
  114. First();
  115. }
  116. void First()
  117. {
  118. Handle=HashTemplateClass<KeyType,ValueType>::NIL;
  119. int size=HashTable.Get_Size();
  120. for (HashIndex=0;HashIndex<size;++HashIndex) {
  121. Handle = HashTable.Get_Hash()[HashIndex];
  122. if (Handle!=HashTemplateClass<KeyType,ValueType>::NIL) break;
  123. }
  124. }
  125. void Next()
  126. {
  127. Handle=HashTable.Get_Table()[Handle].Next;
  128. if (Handle==HashTemplateClass<KeyType,ValueType>::NIL) {
  129. int size=HashTable.Get_Size();
  130. for (++HashIndex;HashIndex<size;++HashIndex) {
  131. Handle = HashTable.Get_Hash()[HashIndex];
  132. if (Handle!=HashTemplateClass<KeyType,ValueType>::NIL) break;
  133. }
  134. }
  135. }
  136. bool Is_Done() const
  137. {
  138. return HashIndex==int(HashTable.Get_Size());
  139. }
  140. ValueType& Peek_Value()
  141. {
  142. return HashTable.Get_Table()[Handle].Value;
  143. }
  144. const KeyType& Peek_Key()
  145. {
  146. return HashTable.Get_Table()[Handle].Key;
  147. }
  148. };
  149. //------------------------------------------------------------------------
  150. // Implementation
  151. //------------------------------------------------------------------------
  152. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Insert(const KeyType& s, const ValueType& d)
  153. {
  154. int h;
  155. h = Alloc_Entry();
  156. unsigned int hval = Get_Hash_Val(s,Size);
  157. Table[h].Key = s;
  158. Table[h].Value = d;
  159. Table[h].Next = Hash[hval];
  160. Hash[hval] = h;
  161. }
  162. template <class KeyType, class ValueType> inline unsigned int HashTemplateClass<KeyType,ValueType>::Get_Size (void) const
  163. {
  164. return Size;
  165. }
  166. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Remove_All (void)
  167. {
  168. for (unsigned int i = 0; i < Size; i++)
  169. {
  170. int f = Hash[i];
  171. if (f!=NIL)
  172. {
  173. int h = f;
  174. while (Table[h].Next != NIL)
  175. h = Table[h].Next;
  176. Table[h].Next = First; // link to first free
  177. First = f; // link to beginning
  178. Hash[i] = NIL; // kill entry in hash
  179. }
  180. }
  181. }
  182. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Remove (const KeyType& s)
  183. {
  184. if (!Hash) return;
  185. unsigned int hval = Get_Hash_Val(s,Size);
  186. int prev = NIL;
  187. int h = Hash[hval];
  188. while (h != NIL)
  189. {
  190. if (Table[h].Key == s)
  191. {
  192. if (prev!=NIL)
  193. Table[prev].Next = Table[h].Next;
  194. else
  195. Hash[hval] = Table[h].Next;
  196. Table[h].Next = First;
  197. First = h;
  198. return;
  199. }
  200. prev = h;
  201. h = Table[h].Next;
  202. }
  203. }
  204. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Remove (const KeyType& s, const ValueType& d)
  205. {
  206. if (!Hash) return;
  207. unsigned int hval = Get_Hash_Val(s,Size);
  208. int prev = NIL;
  209. int h = Hash[hval];
  210. while (h != NIL)
  211. {
  212. if (Table[h].Key == s && Table[h].Value == d)
  213. {
  214. if (prev!=NIL)
  215. Table[prev].Next = Table[h].Next;
  216. else
  217. Hash[hval] = Table[h].Next;
  218. Table[h].Next = First;
  219. First = h;
  220. return;
  221. }
  222. prev = h;
  223. h = Table[h].Next;
  224. }
  225. }
  226. // Set the value at existing key, or if not found insert a new value
  227. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Set_Value (const KeyType& s, const ValueType& v)
  228. {
  229. if (Hash) {
  230. int h = Hash[Get_Hash_Val(s,Size)];
  231. while (h!=NIL) {
  232. if (Table[h].Key == s) {
  233. Table[h].Value=v;
  234. return;
  235. }
  236. h = Table[h].Next;
  237. }
  238. }
  239. Insert(s,v);
  240. }
  241. template <class KeyType, class ValueType> inline ValueType HashTemplateClass<KeyType,ValueType>::Get (const KeyType& s) const
  242. {
  243. if (Hash) {
  244. int h = Hash[Get_Hash_Val(s,Size)];
  245. while (h!=NIL)
  246. {
  247. if (Table[h].Key == s)
  248. return Table[h].Value;
  249. h = Table[h].Next;
  250. }
  251. }
  252. return ValueType(0);
  253. }
  254. template <class KeyType, class ValueType> inline bool HashTemplateClass<KeyType,ValueType>::Get(const KeyType& s, ValueType& d) const
  255. {
  256. if (Hash) {
  257. int h = Hash[Get_Hash_Val(s,Size)];
  258. while (h!=NIL)
  259. {
  260. if (Table[h].Key == s)
  261. {
  262. d = Table[h].Value;
  263. return true;
  264. }
  265. h = Table[h].Next;
  266. }
  267. }
  268. return false;
  269. }
  270. template <class KeyType, class ValueType> inline bool HashTemplateClass<KeyType,ValueType>::Exists(const KeyType& s) const
  271. {
  272. if (Hash) {
  273. int h = Hash[Get_Hash_Val(s,Size)];
  274. while (h!=NIL)
  275. {
  276. if (Table[h].Key == s)
  277. return true;
  278. h = Table[h].Next;
  279. }
  280. }
  281. return false;
  282. }
  283. template <class KeyType, class ValueType> inline bool HashTemplateClass<KeyType,ValueType>::Exists(const KeyType& s, const ValueType& d) const
  284. {
  285. if (Hash) {
  286. int h = Hash[Get_Hash_Val(s,Size)];
  287. while (h!=NIL)
  288. {
  289. if (Table[h].Key == s && Table[h].Value == d)
  290. return true;
  291. h = Table[h].Next;
  292. }
  293. }
  294. return false;
  295. }
  296. template <class KeyType, class ValueType> inline unsigned int HashTemplateClass<KeyType,ValueType>::Get_Hash_Val(const KeyType& s, const unsigned int hash_array_size)
  297. {
  298. return HashTemplateKeyClass<KeyType>::Get_Hash_Value (s) & (hash_array_size-1);
  299. }
  300. template <class KeyType, class ValueType> inline void HashTemplateClass<KeyType,ValueType>::Re_Hash()
  301. {
  302. unsigned int new_size = Size*2;
  303. if (new_size < 4)
  304. new_size = 4;
  305. Entry *new_table = new Entry[new_size];
  306. int *new_hash = new int[new_size];
  307. int cnt = 0;
  308. int i;
  309. for (i = 0; i < (int)new_size; i++)
  310. {
  311. new_table[i].Next = NIL;
  312. new_hash[i] = NIL;
  313. }
  314. if (Size) // if we have existing data, it needs to be rehashed
  315. {
  316. for (i = 0; i < (int)Size; i++) // step through each old hash set
  317. {
  318. int h = Hash[i];
  319. while (h != NIL)
  320. {
  321. unsigned int hVal = Get_Hash_Val(Table[h].Key, new_size);
  322. new_table[cnt].Key = Table[h].Key;
  323. new_table[cnt].Value = Table[h].Value;
  324. new_table[cnt].Next = new_hash[hVal];
  325. new_hash[hVal] = cnt;
  326. cnt++;
  327. h = Table[h].Next;
  328. }
  329. }
  330. delete[] Hash;
  331. delete[] Table;
  332. }
  333. for (i = cnt; i < (int)new_size; i++)
  334. new_table[i].Next = i+1;
  335. new_table[new_size-1].Next = NIL;
  336. First = cnt;
  337. Hash = new_hash;
  338. Table = new_table;
  339. Size = new_size;
  340. }
  341. template <class KeyType, class ValueType> inline int HashTemplateClass<KeyType,ValueType>::Alloc_Entry()
  342. {
  343. if (First == NIL) // need to re-alloc and re-hash tables
  344. Re_Hash();
  345. int h = First;
  346. First = Table[First].Next;
  347. return h;
  348. }
  349. template <class KeyType, class ValueType> inline HashTemplateClass<KeyType,ValueType>::HashTemplateClass() : Hash(0),Table(0),First(NIL),Size(0)
  350. {
  351. // Re_Hash();
  352. }
  353. template <class KeyType, class ValueType> inline HashTemplateClass<KeyType,ValueType>::~HashTemplateClass()
  354. {
  355. if (Hash)
  356. delete[] Hash;
  357. if (Table)
  358. delete[] Table;
  359. }
  360. // Get_Hash_Value specialization for StringClass. This is intended to be used
  361. // for filenames, so it takes into account the four characters that come before
  362. // the file extension (.xxx - extension expected to be 4 characters).
  363. template <> inline unsigned int HashTemplateKeyClass<StringClass>::Get_Hash_Value(const StringClass& s)
  364. {
  365. unsigned int len=s.Get_Length();
  366. unsigned char* buffer=(unsigned char*)s.Peek_Buffer();
  367. if (len<8) {
  368. unsigned int hval=0;
  369. for (unsigned int a=0;a<len;++a) {
  370. hval+=37*hval+buffer[a];
  371. }
  372. return hval;
  373. }
  374. unsigned int hval = *((const unsigned int*)(buffer+len-8));
  375. hval = hval + (hval>>5) + (hval>>10) + (hval >> 20);
  376. return hval;
  377. }
  378. #endif