home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / containers / StrPool.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  5KB  |  224 lines

  1.  
  2. #ifndef __STRPOOL_H__
  3. #define __STRPOOL_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     idStrPool
  9.  
  10. ===============================================================================
  11. */
  12.  
  13. class idPoolStr : public idStr {
  14.     friend class idStrPool;
  15.  
  16. public:
  17.                         idPoolStr() { numUsers = 0; }
  18.                         ~idPoolStr() { assert( numUsers == 0 ); }
  19.  
  20.                         // returns total size of allocated memory
  21.     size_t                Allocated( void ) const { return idStr::Allocated(); }
  22.                         // returns total size of allocated memory including size of string pool type
  23.     size_t                Size( void ) const { return sizeof( *this ) + Allocated(); }
  24.                         // returns a pointer to the pool this string was allocated from
  25.     const idStrPool *    GetPool( void ) const { return pool; }
  26.  
  27. private:
  28.     idStrPool *            pool;
  29.     mutable int            numUsers;
  30. };
  31.  
  32. class idStrPool {
  33. public:
  34.                         idStrPool() { caseSensitive = true; }
  35.  
  36.     void                SetCaseSensitive( bool caseSensitive );
  37.  
  38.     int                    Num( void ) const { return pool.Num(); }
  39.     size_t                Allocated( void ) const;
  40.     size_t                Size( void ) const;
  41.  
  42.     const idPoolStr *    operator[]( int index ) const { return pool[index]; }
  43.  
  44.     const idPoolStr *    AllocString( const char *string );
  45.     void                FreeString( const idPoolStr *poolStr );
  46.     const idPoolStr *    CopyString( const idPoolStr *poolStr );
  47.     void                Clear( void );
  48.  
  49. // RAVEN BEGIN
  50. // mwhitlock: Dynamic memory consolidation
  51. #if defined(_RV_MEM_SYS_SUPPORT)
  52.     void                SetAllocatorHeap ( rvHeap* heap )
  53.     {
  54.         assert(heap);
  55.         pool.SetAllocatorHeap(heap);
  56.         poolHash.SetAllocatorHeap(heap);
  57.     }
  58. #endif
  59. // RAVEN END
  60.  
  61. private:
  62.     bool                caseSensitive;
  63.     idList<idPoolStr *>    pool;
  64.     idHashIndex            poolHash;
  65. };
  66.  
  67. /*
  68. ================
  69. idStrPool::SetCaseSensitive
  70. ================
  71. */
  72. ID_INLINE void idStrPool::SetCaseSensitive( bool caseSensitive ) {
  73.     this->caseSensitive = caseSensitive;
  74. }
  75.  
  76. /*
  77. ================
  78. idStrPool::AllocString
  79. ================
  80. */
  81. ID_INLINE const idPoolStr *idStrPool::AllocString( const char *string ) {
  82.     int i, hash;
  83.     idPoolStr *poolStr;
  84.  
  85.     hash = poolHash.GenerateKey( string, caseSensitive );
  86.     if ( caseSensitive ) {
  87.         for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  88.             if ( pool[i]->Cmp( string ) == 0 ) {
  89.                 pool[i]->numUsers++;
  90.                 return pool[i];
  91.             }
  92.         }
  93.     } else {
  94.         for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  95.             if ( pool[i]->Icmp( string ) == 0 ) {
  96.                 pool[i]->numUsers++;
  97.                 return pool[i];
  98.             }
  99.         }
  100.     }
  101.  
  102. // RAVEN BEGIN
  103. // mwhitlock: Dynamic memory consolidation
  104. #if defined(_RV_MEM_SYS_SUPPORT)
  105.     RV_PUSH_SYS_HEAP_ID(RV_HEAP_ID_PERMANENT);
  106. #endif
  107. // RAVEN END
  108.     poolStr = new idPoolStr;
  109. // RAVEN BEGIN
  110. // mwhitlock: Dynamic memory consolidation
  111. #if defined(_RV_MEM_SYS_SUPPORT)
  112.     RV_POP_HEAP();
  113. #endif
  114. // RAVEN END
  115.     *static_cast<idStr *>(poolStr) = string;
  116.     poolStr->pool = this;
  117.     poolStr->numUsers = 1;
  118.     poolHash.Add( hash, pool.Append( poolStr ) );
  119.     return poolStr;
  120. }
  121.  
  122. /*
  123. ================
  124. idStrPool::FreeString
  125. ================
  126. */
  127. ID_INLINE void idStrPool::FreeString( const idPoolStr *poolStr ) {
  128.     int i, hash;
  129.  
  130.     assert( poolStr->numUsers >= 1 );
  131.     assert( poolStr->pool == this );
  132.  
  133.     poolStr->numUsers--;
  134.     if ( poolStr->numUsers <= 0 ) {
  135.         hash = poolHash.GenerateKey( poolStr->c_str(), caseSensitive );
  136.         if ( caseSensitive ) { 
  137.             for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  138.                 if ( pool[i]->Cmp( poolStr->c_str() ) == 0 ) {
  139.                     break;
  140.                 }
  141.             }
  142.         } else {
  143.             for ( i = poolHash.First( hash ); i != -1; i = poolHash.Next( i ) ) {
  144.                 if ( pool[i]->Icmp( poolStr->c_str() ) == 0 ) {
  145.                     break;
  146.                 }
  147.             }
  148.         }
  149.         assert( i != -1 );
  150.         assert( pool[i] == poolStr );
  151.         delete pool[i];
  152.         pool.RemoveIndex( i );
  153.         poolHash.RemoveIndex( hash, i );
  154.     }
  155. }
  156.  
  157. /*
  158. ================
  159. idStrPool::CopyString
  160. ================
  161. */
  162. ID_INLINE const idPoolStr *idStrPool::CopyString( const idPoolStr *poolStr ) {
  163.  
  164.     assert( poolStr->numUsers >= 1 );
  165.  
  166.     if ( poolStr->pool == this ) {
  167.         // the string is from this pool so just increase the user count
  168.         poolStr->numUsers++;
  169.         return poolStr;
  170.     } else {
  171.         // the string is from another pool so it needs to be re-allocated from this pool.
  172.         return AllocString( poolStr->c_str() );
  173.     }
  174. }
  175.  
  176. /*
  177. ================
  178. idStrPool::Clear
  179. ================
  180. */
  181. ID_INLINE void idStrPool::Clear( void ) {
  182.     int i;
  183.  
  184.     for ( i = 0; i < pool.Num(); i++ ) {
  185.         pool[i]->numUsers = 0;
  186.     }
  187.     pool.DeleteContents( true );
  188.     poolHash.Free();
  189. }
  190.  
  191. /*
  192. ================
  193. idStrPool::Allocated
  194. ================
  195. */
  196. ID_INLINE size_t idStrPool::Allocated( void ) const {
  197.     int i;
  198.     size_t size;
  199.  
  200.     size = pool.Allocated() + poolHash.Allocated();
  201.     for ( i = 0; i < pool.Num(); i++ ) {
  202.         size += pool[i]->Allocated();
  203.     }
  204.     return size;
  205. }
  206.  
  207. /*
  208. ================
  209. idStrPool::Size
  210. ================
  211. */
  212. ID_INLINE size_t idStrPool::Size( void ) const {
  213.     int i;
  214.     size_t size;
  215.  
  216.     size = pool.Size() + poolHash.Size();
  217.     for ( i = 0; i < pool.Num(); i++ ) {
  218.         size += pool[i]->Size();
  219.     }
  220.     return size;
  221. }
  222.  
  223. #endif /* !__STRPOOL_H__ */
  224.