StrPool.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 __STRPOOL_H__
  21. #define __STRPOOL_H__
  22. /*
  23. ===============================================================================
  24. idStrPool
  25. ===============================================================================
  26. */
  27. class idStrPool;
  28. class idPoolStr : public idStr {
  29. friend class idStrPool;
  30. public:
  31. idPoolStr() { numUsers = 0; }
  32. ~idPoolStr() { assert( numUsers == 0 ); }
  33. // returns total size of allocated memory
  34. size_t Allocated( void ) const { return idStr::Allocated(); }
  35. // returns total size of allocated memory including size of string pool type
  36. size_t Size( void ) const { return sizeof( *this ) + Allocated(); }
  37. // returns a pointer to the pool this string was allocated from
  38. const idStrPool * GetPool( void ) const { return pool; }
  39. private:
  40. idStrPool * pool;
  41. mutable int numUsers;
  42. };
  43. class idStrPool {
  44. public:
  45. idStrPool() { caseSensitive = true; }
  46. void SetCaseSensitive( bool caseSensitive );
  47. int Num( void ) const { return pool.Num(); }
  48. size_t Allocated( void ) const;
  49. size_t Size( void ) const;
  50. const idPoolStr * operator[]( int index ) const { return pool[index]; }
  51. const idPoolStr * AllocString( const char *string );
  52. void FreeString( const idPoolStr *poolStr );
  53. const idPoolStr * CopyString( const idPoolStr *poolStr );
  54. void Clear( void );
  55. private:
  56. bool caseSensitive;
  57. idList<idPoolStr *> pool;
  58. idHashIndex poolHash;
  59. };
  60. /*
  61. ================
  62. idStrPool::SetCaseSensitive
  63. ================
  64. */
  65. ID_INLINE void idStrPool::SetCaseSensitive( bool caseSensitive ) {
  66. this->caseSensitive = caseSensitive;
  67. }
  68. /*
  69. ================
  70. idStrPool::AllocString
  71. ================
  72. */
  73. ID_INLINE const idPoolStr *idStrPool::AllocString( const char *string ) {
  74. int i, hash;
  75. idPoolStr *poolStr;
  76. hash = poolHash.GenerateKey( string, caseSensitive );
  77. if ( caseSensitive ) {
  78. for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  79. if ( pool[i]->Cmp( string ) == 0 ) {
  80. pool[i]->numUsers++;
  81. return pool[i];
  82. }
  83. }
  84. } else {
  85. for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  86. if ( pool[i]->Icmp( string ) == 0 ) {
  87. pool[i]->numUsers++;
  88. return pool[i];
  89. }
  90. }
  91. }
  92. poolStr = new idPoolStr;
  93. *static_cast<idStr *>(poolStr) = string;
  94. poolStr->pool = this;
  95. poolStr->numUsers = 1;
  96. poolHash.Add( hash, pool.Append( poolStr ) );
  97. return poolStr;
  98. }
  99. /*
  100. ================
  101. idStrPool::FreeString
  102. ================
  103. */
  104. ID_INLINE void idStrPool::FreeString( const idPoolStr *poolStr ) {
  105. int i, hash;
  106. assert( poolStr->numUsers >= 1 );
  107. assert( poolStr->pool == this );
  108. poolStr->numUsers--;
  109. if ( poolStr->numUsers <= 0 && pool.Num() > 0 ) {
  110. hash = poolHash.GenerateKey( poolStr->c_str(), caseSensitive );
  111. if ( caseSensitive ) {
  112. for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  113. if ( pool[i]->Cmp( poolStr->c_str() ) == 0 ) {
  114. break;
  115. }
  116. }
  117. } else {
  118. for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  119. if ( pool[i]->Icmp( poolStr->c_str() ) == 0 ) {
  120. break;
  121. }
  122. }
  123. }
  124. assert( i != -1 );
  125. assert( pool[i] == poolStr );
  126. delete pool[i];
  127. pool.RemoveIndex( i );
  128. poolHash.RemoveIndex( hash, i );
  129. }
  130. }
  131. /*
  132. ================
  133. idStrPool::CopyString
  134. ================
  135. */
  136. ID_INLINE const idPoolStr *idStrPool::CopyString( const idPoolStr *poolStr ) {
  137. assert( poolStr->numUsers >= 1 );
  138. if ( poolStr->pool == this ) {
  139. // the string is from this pool so just increase the user count
  140. poolStr->numUsers++;
  141. return poolStr;
  142. } else {
  143. // the string is from another pool so it needs to be re-allocated from this pool.
  144. return AllocString( poolStr->c_str() );
  145. }
  146. }
  147. /*
  148. ================
  149. idStrPool::Clear
  150. ================
  151. */
  152. ID_INLINE void idStrPool::Clear( void ) {
  153. int i;
  154. for ( i = 0; i < pool.Num(); i++ ) {
  155. pool[i]->numUsers = 0;
  156. }
  157. pool.DeleteContents( true );
  158. poolHash.Free();
  159. }
  160. /*
  161. ================
  162. idStrPool::Allocated
  163. ================
  164. */
  165. ID_INLINE size_t idStrPool::Allocated( void ) const {
  166. int i;
  167. size_t size;
  168. size = pool.Allocated() + poolHash.Allocated();
  169. for ( i = 0; i < pool.Num(); i++ ) {
  170. size += pool[i]->Allocated();
  171. }
  172. return size;
  173. }
  174. /*
  175. ================
  176. idStrPool::Size
  177. ================
  178. */
  179. ID_INLINE size_t idStrPool::Size( void ) const {
  180. int i;
  181. size_t size;
  182. size = pool.Size() + poolHash.Size();
  183. for ( i = 0; i < pool.Num(); i++ ) {
  184. size += pool[i]->Size();
  185. }
  186. return size;
  187. }
  188. #endif /* !__STRPOOL_H__ */