HashTable.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __HASHTABLE_H__
  21. #define __HASHTABLE_H__
  22. /*
  23. ===============================================================================
  24. General hash table. Slower than idHashIndex but it can also be used for
  25. linked lists and other data structures than just indexes or arrays.
  26. ===============================================================================
  27. */
  28. template< class Type >
  29. class idHashTable {
  30. public:
  31. idHashTable( int newtablesize = 256 );
  32. idHashTable( const idHashTable<Type> &map );
  33. ~idHashTable( void );
  34. // returns total size of allocated memory
  35. size_t Allocated( void ) const;
  36. // returns total size of allocated memory including size of hash table type
  37. size_t Size( void ) const;
  38. void Set( const char *key, Type &value );
  39. bool Get( const char *key, Type **value = NULL ) const;
  40. bool Remove( const char *key );
  41. void Clear( void );
  42. void DeleteContents( void );
  43. // the entire contents can be itterated over, but note that the
  44. // exact index for a given element may change when new elements are added
  45. int Num( void ) const;
  46. Type * GetIndex( int index ) const;
  47. int GetSpread( void ) const;
  48. private:
  49. struct hashnode_s {
  50. idStr key;
  51. Type value;
  52. hashnode_s *next;
  53. hashnode_s( const idStr &k, Type v, hashnode_s *n ) : key( k ), value( v ), next( n ) {};
  54. hashnode_s( const char *k, Type v, hashnode_s *n ) : key( k ), value( v ), next( n ) {};
  55. };
  56. hashnode_s ** heads;
  57. int tablesize;
  58. int numentries;
  59. int tablesizemask;
  60. int GetHash( const char *key ) const;
  61. };
  62. /*
  63. ================
  64. idHashTable<Type>::idHashTable
  65. ================
  66. */
  67. template< class Type >
  68. ID_INLINE idHashTable<Type>::idHashTable( int newtablesize ) {
  69. assert( idMath::IsPowerOfTwo( newtablesize ) );
  70. tablesize = newtablesize;
  71. assert( tablesize > 0 );
  72. heads = new hashnode_s *[ tablesize ];
  73. memset( heads, 0, sizeof( *heads ) * tablesize );
  74. numentries = 0;
  75. tablesizemask = tablesize - 1;
  76. }
  77. /*
  78. ================
  79. idHashTable<Type>::idHashTable
  80. ================
  81. */
  82. template< class Type >
  83. ID_INLINE idHashTable<Type>::idHashTable( const idHashTable<Type> &map ) {
  84. int i;
  85. hashnode_s *node;
  86. hashnode_s **prev;
  87. assert( map.tablesize > 0 );
  88. tablesize = map.tablesize;
  89. heads = new hashnode_s *[ tablesize ];
  90. numentries = map.numentries;
  91. tablesizemask = map.tablesizemask;
  92. for( i = 0; i < tablesize; i++ ) {
  93. if ( !map.heads[ i ] ) {
  94. heads[ i ] = NULL;
  95. continue;
  96. }
  97. prev = &heads[ i ];
  98. for( node = map.heads[ i ]; node != NULL; node = node->next ) {
  99. *prev = new hashnode_s( node->key, node->value, NULL );
  100. prev = &( *prev )->next;
  101. }
  102. }
  103. }
  104. /*
  105. ================
  106. idHashTable<Type>::~idHashTable<Type>
  107. ================
  108. */
  109. template< class Type >
  110. ID_INLINE idHashTable<Type>::~idHashTable( void ) {
  111. Clear();
  112. delete[] heads;
  113. }
  114. /*
  115. ================
  116. idHashTable<Type>::Allocated
  117. ================
  118. */
  119. template< class Type >
  120. ID_INLINE size_t idHashTable<Type>::Allocated( void ) const {
  121. return sizeof( heads ) * tablesize + sizeof( *heads ) * numentries;
  122. }
  123. /*
  124. ================
  125. idHashTable<Type>::Size
  126. ================
  127. */
  128. template< class Type >
  129. ID_INLINE size_t idHashTable<Type>::Size( void ) const {
  130. return sizeof( idHashTable<Type> ) + sizeof( heads ) * tablesize + sizeof( *heads ) * numentries;
  131. }
  132. /*
  133. ================
  134. idHashTable<Type>::GetHash
  135. ================
  136. */
  137. template< class Type >
  138. ID_INLINE int idHashTable<Type>::GetHash( const char *key ) const {
  139. return ( idStr::Hash( key ) & tablesizemask );
  140. }
  141. /*
  142. ================
  143. idHashTable<Type>::Set
  144. ================
  145. */
  146. template< class Type >
  147. ID_INLINE void idHashTable<Type>::Set( const char *key, Type &value ) {
  148. hashnode_s *node, **nextPtr;
  149. int hash, s;
  150. hash = GetHash( key );
  151. for( nextPtr = &(heads[hash]), node = *nextPtr; node != NULL; nextPtr = &(node->next), node = *nextPtr ) {
  152. s = node->key.Cmp( key );
  153. if ( s == 0 ) {
  154. node->value = value;
  155. return;
  156. }
  157. if ( s > 0 ) {
  158. break;
  159. }
  160. }
  161. numentries++;
  162. *nextPtr = new hashnode_s( key, value, heads[ hash ] );
  163. (*nextPtr)->next = node;
  164. }
  165. /*
  166. ================
  167. idHashTable<Type>::Get
  168. ================
  169. */
  170. template< class Type >
  171. ID_INLINE bool idHashTable<Type>::Get( const char *key, Type **value ) const {
  172. hashnode_s *node;
  173. int hash, s;
  174. hash = GetHash( key );
  175. for( node = heads[ hash ]; node != NULL; node = node->next ) {
  176. s = node->key.Cmp( key );
  177. if ( s == 0 ) {
  178. if ( value ) {
  179. *value = &node->value;
  180. }
  181. return true;
  182. }
  183. if ( s > 0 ) {
  184. break;
  185. }
  186. }
  187. if ( value ) {
  188. *value = NULL;
  189. }
  190. return false;
  191. }
  192. /*
  193. ================
  194. idHashTable<Type>::GetIndex
  195. the entire contents can be itterated over, but note that the
  196. exact index for a given element may change when new elements are added
  197. ================
  198. */
  199. template< class Type >
  200. ID_INLINE Type *idHashTable<Type>::GetIndex( int index ) const {
  201. hashnode_s *node;
  202. int count;
  203. int i;
  204. if ( ( index < 0 ) || ( index > numentries ) ) {
  205. assert( 0 );
  206. return NULL;
  207. }
  208. count = 0;
  209. for( i = 0; i < tablesize; i++ ) {
  210. for( node = heads[ i ]; node != NULL; node = node->next ) {
  211. if ( count == index ) {
  212. return &node->value;
  213. }
  214. count++;
  215. }
  216. }
  217. return NULL;
  218. }
  219. /*
  220. ================
  221. idHashTable<Type>::Remove
  222. ================
  223. */
  224. template< class Type >
  225. ID_INLINE bool idHashTable<Type>::Remove( const char *key ) {
  226. hashnode_s **head;
  227. hashnode_s *node;
  228. hashnode_s *prev;
  229. int hash;
  230. hash = GetHash( key );
  231. head = &heads[ hash ];
  232. if ( *head ) {
  233. for( prev = NULL, node = *head; node != NULL; prev = node, node = node->next ) {
  234. if ( node->key == key ) {
  235. if ( prev ) {
  236. prev->next = node->next;
  237. } else {
  238. *head = node->next;
  239. }
  240. delete node;
  241. numentries--;
  242. return true;
  243. }
  244. }
  245. }
  246. return false;
  247. }
  248. /*
  249. ================
  250. idHashTable<Type>::Clear
  251. ================
  252. */
  253. template< class Type >
  254. ID_INLINE void idHashTable<Type>::Clear( void ) {
  255. int i;
  256. hashnode_s *node;
  257. hashnode_s *next;
  258. for( i = 0; i < tablesize; i++ ) {
  259. next = heads[ i ];
  260. while( next != NULL ) {
  261. node = next;
  262. next = next->next;
  263. delete node;
  264. }
  265. heads[ i ] = NULL;
  266. }
  267. numentries = 0;
  268. }
  269. /*
  270. ================
  271. idHashTable<Type>::DeleteContents
  272. ================
  273. */
  274. template< class Type >
  275. ID_INLINE void idHashTable<Type>::DeleteContents( void ) {
  276. int i;
  277. hashnode_s *node;
  278. hashnode_s *next;
  279. for( i = 0; i < tablesize; i++ ) {
  280. next = heads[ i ];
  281. while( next != NULL ) {
  282. node = next;
  283. next = next->next;
  284. delete node->value;
  285. delete node;
  286. }
  287. heads[ i ] = NULL;
  288. }
  289. numentries = 0;
  290. }
  291. /*
  292. ================
  293. idHashTable<Type>::Num
  294. ================
  295. */
  296. template< class Type >
  297. ID_INLINE int idHashTable<Type>::Num( void ) const {
  298. return numentries;
  299. }
  300. #if defined(ID_TYPEINFO)
  301. #define __GNUC__ 99
  302. #endif
  303. #if !defined(__GNUC__) || __GNUC__ < 4
  304. /*
  305. ================
  306. idHashTable<Type>::GetSpread
  307. ================
  308. */
  309. template< class Type >
  310. int idHashTable<Type>::GetSpread( void ) const {
  311. int i, average, error, e;
  312. hashnode_s *node;
  313. // if no items in hash
  314. if ( !numentries ) {
  315. return 100;
  316. }
  317. average = numentries / tablesize;
  318. error = 0;
  319. for ( i = 0; i < tablesize; i++ ) {
  320. numItems = 0;
  321. for( node = heads[ i ]; node != NULL; node = node->next ) {
  322. numItems++;
  323. }
  324. e = abs( numItems - average );
  325. if ( e > 1 ) {
  326. error += e - 1;
  327. }
  328. }
  329. return 100 - (error * 100 / numentries);
  330. }
  331. #endif
  332. #if defined(ID_TYPEINFO)
  333. #undef __GNUC__
  334. #endif
  335. #endif /* !__HASHTABLE_H__ */