Leaked source code of windows server 2003
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.

218 lines
4.3 KiB

  1. //---------------------------------------------------------------------------
  2. // StrMap.h
  3. //
  4. // Template class for a map of strings to something
  5. //---------------------------------------------------------------------------
  6. #pragma once
  7. #ifndef _STRMAP_H_
  8. #define _STRMAP_H_
  9. #include "MyString.h"
  10. #include <utility>
  11. #include <algorithm>
  12. #include "myvector.h"
  13. template< class T >
  14. struct StringPairEqual
  15. {
  16. StringPairEqual( const String& str)
  17. : m_rstr( str ) {}
  18. bool operator()(
  19. const pair<String, T>& rhs )
  20. {
  21. return ( m_rstr == rhs.first );
  22. }
  23. private:
  24. const String& m_rstr;
  25. };
  26. template< class T >
  27. struct StringPairCompare
  28. {
  29. int operator()(
  30. const pair<String, T>& lhs,
  31. const pair<String, T>& rhs )
  32. {
  33. return lhs.first.compare( rhs.first );
  34. }
  35. };
  36. // template function--finds the first element n for which cmp(val,vec[n]) is true and
  37. // cmp(val,vec[n-1]) is false
  38. template< class T, class Compare >
  39. __TYPENAME TVector<T>::iterator
  40. binary_find(
  41. TVector<T>& vec,
  42. const T& val,
  43. Compare& cmp )
  44. {
  45. TVector<T>::iterator iter = vec.begin();
  46. if ( vec.size() > 0 )
  47. {
  48. size_t top = vec.size() - 1;
  49. size_t bot = 0;
  50. size_t mid = top >> 1;
  51. while ( top != bot )
  52. {
  53. int c = cmp( val, vec[mid] );
  54. if ( c > 0 )
  55. {
  56. // val > vec[mid]
  57. bot = mid + 1;
  58. mid = ( top + bot ) >> 1;
  59. }
  60. else if ( c < 0 )
  61. {
  62. // val < vec[mid]
  63. top = mid;
  64. mid = ( top + bot ) >> 1;
  65. }
  66. else
  67. {
  68. // val == vec[mid]
  69. top = bot = mid;
  70. }
  71. }
  72. iter = vec.begin() + mid;
  73. if ( cmp( val, vec[mid] ) > 0 )
  74. {
  75. iter++;
  76. }
  77. }
  78. return iter;
  79. }
  80. template< class T >
  81. class TStringMap
  82. {
  83. public:
  84. typedef String key_type;
  85. typedef T referent_type;
  86. typedef pair<key_type,referent_type> value_type;
  87. typedef TVector<value_type> vector_type;
  88. typedef typename vector_type::reference reference;
  89. typedef typename vector_type::iterator iterator;
  90. typedef typename vector_type::const_iterator const_iterator;
  91. TStringMap(){};
  92. ~TStringMap(){};
  93. iterator begin() { return m_vec.begin(); }
  94. const_iterator begin() const { return m_vec.begin(); }
  95. iterator end() {return m_vec.end();}
  96. const_iterator end() const {return m_vec.end();}
  97. iterator find( const String& );
  98. pair<iterator, bool> insert(const value_type& x);
  99. referent_type& operator[]( const String& );
  100. referent_type& operator[]( size_t n ) { return m_vec[n].second; }
  101. const referent_type& operator[]( const String& ) const ;
  102. const referent_type& operator[]( size_t n ) const { return m_vec[n].second; }
  103. void clear(){ m_vec.clear(); }
  104. size_t size() const { return m_vec.size(); }
  105. private:
  106. vector_type m_vec;
  107. };
  108. #if 0
  109. template<class T>
  110. inline
  111. TStringMap<T>::iterator
  112. TStringMap<T>::begin()
  113. {
  114. return m_vec.begin();
  115. }
  116. template<class T>
  117. inline
  118. TStringMap<T>::const_iterator
  119. TStringMap<T>::begin()
  120. const
  121. {
  122. return m_vec.begin();
  123. }
  124. template<class T>
  125. inline
  126. TStringMap<T>::iterator
  127. TStringMap<T>::end()
  128. {
  129. return m_vec.end();
  130. }
  131. template<class T>
  132. inline
  133. TStringMap<T>::const_iterator
  134. TStringMap<T>::end()
  135. const
  136. {
  137. return m_vec.end();
  138. }
  139. #endif
  140. template<class T>
  141. inline
  142. __TYPENAME TStringMap<T>::iterator
  143. TStringMap<T>::find(
  144. const String& str )
  145. {
  146. value_type vt(str,T());
  147. iterator iter = binary_find( m_vec, vt, StringPairCompare<T>() );
  148. StringPairEqual<T> spe(str);
  149. if ( ( iter != m_vec.end() ) && spe(*iter) )
  150. {
  151. }
  152. else
  153. {
  154. iter = m_vec.end();
  155. }
  156. return iter;
  157. }
  158. template<class T>
  159. inline
  160. pair< __TYPENAME TStringMap<T>::iterator, bool >
  161. TStringMap<T>::insert(
  162. const __TYPENAME TStringMap<T>::value_type& val )
  163. {
  164. bool inserted = false;
  165. iterator iter = binary_find( m_vec, val, StringPairCompare<T>() );
  166. StringPairEqual<T> spe(val.first);
  167. if ( ( iter != m_vec.end() ) && spe(*iter) )
  168. {
  169. }
  170. else
  171. {
  172. inserted = true;
  173. iter = m_vec.insert( iter, val );
  174. }
  175. return pair<TStringMap<T>::iterator, bool>( iter, inserted );
  176. }
  177. template<class T>
  178. inline
  179. T&
  180. TStringMap<T>::operator[](
  181. const String& s )
  182. {
  183. value_type vt(s,T());
  184. iterator iter = binary_find( m_vec, vt, StringPairCompare<T>() );
  185. StringPairEqual<T> spe(s);
  186. if ( ( iter != m_vec.end() ) && spe(*iter) )
  187. {
  188. }
  189. else
  190. {
  191. iter = m_vec.insert( iter, vt );
  192. }
  193. return (*iter).second;
  194. }
  195. #endif // !_STRMAP_H_