Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
3.7 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef UTLSTRINGMAP_H
  7. #define UTLSTRINGMAP_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "utlsymbol.h"
  12. template <class T>
  13. class CUtlStringMap
  14. {
  15. public:
  16. typedef UtlSymId_t IndexType_t;
  17. CUtlStringMap( bool caseInsensitive = true, int initsize = 32 ) :
  18. m_SymbolTable( 0, 32, caseInsensitive ),
  19. m_Vector( initsize )
  20. {
  21. }
  22. // Get data by the string itself:
  23. T& operator[]( const char *pString )
  24. {
  25. CUtlSymbol symbol = m_SymbolTable.AddString( pString );
  26. int index = ( int )( UtlSymId_t )symbol;
  27. if( m_Vector.Count() <= index )
  28. {
  29. m_Vector.EnsureCount( index + 1 );
  30. }
  31. return m_Vector[index];
  32. }
  33. // Get data by the string's symbol table ID - only used to retrieve a pre-existing symbol, not create a new one!
  34. T& operator[]( UtlSymId_t n )
  35. {
  36. Assert( n <= m_Vector.Count() );
  37. return m_Vector[n];
  38. }
  39. const T& operator[]( UtlSymId_t n ) const
  40. {
  41. Assert( n <= m_Vector.Count() );
  42. return m_Vector[n];
  43. }
  44. unsigned int Count() const
  45. {
  46. Assert( m_Vector.Count() == m_SymbolTable.GetNumStrings() );
  47. return m_Vector.Count();
  48. }
  49. bool Defined( const char *pString ) const
  50. {
  51. return m_SymbolTable.Find( pString ) != UTL_INVAL_SYMBOL;
  52. }
  53. UtlSymId_t Find( const char *pString ) const
  54. {
  55. return m_SymbolTable.Find( pString );
  56. }
  57. UtlSymId_t AddString( const char *pString )
  58. {
  59. CUtlSymbol symbol = m_SymbolTable.AddString( pString );
  60. int index = ( int )( UtlSymId_t )symbol;
  61. if( m_Vector.Count() <= index )
  62. {
  63. m_Vector.EnsureCount( index + 1 );
  64. }
  65. return symbol;
  66. }
  67. /// Add a string to the map and also insert an item at
  68. /// its location in the same operation. Returns the
  69. /// newly created index (or the one that was just
  70. /// overwritten, if pString already existed.)
  71. UtlSymId_t Insert( const char *pString, const T &item )
  72. {
  73. CUtlSymbol symbol = m_SymbolTable.AddString( pString );
  74. UtlSymId_t index = symbol; // implicit coercion
  75. if ( m_Vector.Count() > index )
  76. {
  77. // this string is already in the dictionary.
  78. }
  79. else if ( m_Vector.Count() == index )
  80. {
  81. // this is the expected case when we've added one more to the tail.
  82. m_Vector.AddToTail( item );
  83. }
  84. else // ( m_Vector.Count() < index )
  85. {
  86. // this is a strange shouldn't-happen case.
  87. AssertMsg( false, "CUtlStringMap insert unexpected entries." );
  88. m_Vector.EnsureCount( index + 1 );
  89. m_Vector[index] = item;
  90. }
  91. return index;
  92. }
  93. /// iterate, not in any particular order.
  94. IndexType_t First() const
  95. {
  96. if ( Count() > 0 )
  97. {
  98. return 0;
  99. }
  100. else
  101. {
  102. return InvalidIndex();
  103. }
  104. }
  105. static UtlSymId_t InvalidIndex()
  106. {
  107. return UTL_INVAL_SYMBOL;
  108. }
  109. // iterators (for uniformity with other map types)
  110. inline UtlSymId_t Head() const
  111. {
  112. return m_SymbolTable.GetNumStrings() > 0 ? 0 : InvalidIndex();
  113. }
  114. inline UtlSymId_t Next( const UtlSymId_t &i ) const
  115. {
  116. UtlSymId_t n = i+1;
  117. return n < m_SymbolTable.GetNumStrings() ? n : InvalidIndex();
  118. }
  119. int GetNumStrings( void ) const
  120. {
  121. return m_SymbolTable.GetNumStrings();
  122. }
  123. const char *String( int n ) const
  124. {
  125. return m_SymbolTable.String( n );
  126. }
  127. // Clear all of the data from the map
  128. void Clear()
  129. {
  130. m_Vector.RemoveAll();
  131. m_SymbolTable.RemoveAll();
  132. }
  133. void Purge()
  134. {
  135. m_Vector.Purge();
  136. m_SymbolTable.RemoveAll();
  137. }
  138. void PurgeAndDeleteElements()
  139. {
  140. m_Vector.PurgeAndDeleteElements();
  141. m_SymbolTable.RemoveAll();
  142. }
  143. private:
  144. CUtlVector<T> m_Vector;
  145. CUtlSymbolTable m_SymbolTable;
  146. };
  147. template< class T >
  148. class CUtlStringMapAutoPurge : public CUtlStringMap < T >
  149. {
  150. public:
  151. ~CUtlStringMapAutoPurge( void )
  152. {
  153. this->PurgeAndDeleteElements();
  154. }
  155. };
  156. #endif // UTLSTRINGMAP_H