PageRenderTime 59ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/AI_Patch/src/public/tier1/utlcommon.h

#
C++ Header | 345 lines | 205 code | 62 blank | 78 comment | 11 complexity | 06904101b8f6077f8014367531d31b53 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: common helpers for reuse among various Utl containers
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef UTLCOMMON_H
  9. #define UTLCOMMON_H
  10. #pragma once
  11. //-----------------------------------------------------------------------------
  12. // Henry Goffin (henryg) was here. Questions? Bugs? Go slap him around a bit.
  13. //-----------------------------------------------------------------------------
  14. // empty_t is the canonical "no-value" type which is fully defined but empty.
  15. struct empty_t {};
  16. // undefined_t is the canonical "undefined" type, used mostly for typedefs;
  17. // parameters of type undefined_t will not compile, which is actually useful
  18. // behavior when it comes to template programming. Google "SFINAE" for info.
  19. struct undefined_t;
  20. // CTypeSelect<sel,A,B>::type is a typedef of A if sel is nonzero, else B
  21. template <int sel, typename A, typename B>
  22. struct CTypeSelect { typedef A type; };
  23. template <typename A, typename B>
  24. struct CTypeSelect<0, A, B> { typedef B type; };
  25. // CTypeEquals<A, B>::value is nonzero if A and B are the same type
  26. template <typename A, typename B, bool bIgnoreConstVolatile = false, bool bIgnoreReference = false>
  27. struct CTypeEquals { enum { value = 0 }; };
  28. template <typename Same>
  29. struct CTypeEquals<Same, Same, false, false> { enum { value = 1 }; };
  30. template <typename A, typename B>
  31. struct CTypeEquals<A, B, true, true> : CTypeEquals< const volatile A&, const volatile B& > {};
  32. template <typename A, typename B>
  33. struct CTypeEquals<A, B, true, false> : CTypeEquals< const volatile A, const volatile B > {};
  34. template <typename A, typename B>
  35. struct CTypeEquals<A, B, false, true> : CTypeEquals< A&, B& > {};
  36. // CUtlKeyValuePair is intended for use with key-lookup containers.
  37. // Because it is specialized for "empty_t" values, one container can
  38. // function as either a set of keys OR a key-value dictionary while
  39. // avoiding storage waste or padding for the empty_t value objects.
  40. template <typename K, typename V>
  41. class CUtlKeyValuePair
  42. {
  43. public:
  44. typedef V ValueReturn_t;
  45. K m_key;
  46. V m_value;
  47. CUtlKeyValuePair() {}
  48. template < typename KInit >
  49. explicit CUtlKeyValuePair( const KInit &k ) : m_key( k ) {}
  50. template < typename KInit, typename VInit >
  51. CUtlKeyValuePair( const KInit &k, const VInit &v ) : m_key( k ), m_value( v ) {}
  52. V &GetValue() { return m_value; }
  53. const V &GetValue() const { return m_value; }
  54. };
  55. template <typename K>
  56. class CUtlKeyValuePair<K, empty_t>
  57. {
  58. public:
  59. typedef const K ValueReturn_t;
  60. K m_key;
  61. CUtlKeyValuePair() {}
  62. template < typename KInit >
  63. explicit CUtlKeyValuePair( const KInit &k ) : m_key( k ) {}
  64. template < typename KInit >
  65. CUtlKeyValuePair( const KInit &k, empty_t ) : m_key( k ) {}
  66. CUtlKeyValuePair( const K &k, const empty_t& ) : m_key( k ) {}
  67. const K &GetValue() const { return m_key; }
  68. };
  69. // Default functors. You can specialize these if your type does
  70. // not implement operator== or operator< in an efficient way for
  71. // some odd reason.
  72. template <typename T> struct DefaultLessFunctor;
  73. template <typename T> struct DefaultEqualFunctor;
  74. // Hashing functor used by hash tables. You can either specialize
  75. // for types which are widely used, or plug a custom functor directly
  76. // into the hash table. If you do roll your own, please read up on
  77. // bit-mixing and the avalanche property; be sure that your values
  78. // are reasonably well-distributed across the entire 32-bit range.
  79. // http://en.wikipedia.org/wiki/Avalanche_effect
  80. // http://home.comcast.net/~bretm/hash/5.html
  81. //
  82. template <typename T> struct DefaultHashFunctor;
  83. // Argument type information. Struct currently contains one or two typedefs:
  84. // typename Arg_t = primary argument type. Usually const T&, sometimes T.
  85. // typename Alt_t = optional alternate type. Usually *undefined*.
  86. //
  87. // Any specializations should be implemented via simple inheritance
  88. // from ArgumentTypeInfoImpl< BestArgType, [optional] AlternateArgType >
  89. //
  90. template <typename T> struct ArgumentTypeInfo;
  91. // Some fundamental building-block functors...
  92. struct StringLessFunctor { bool operator()( const char *a, const char *b ) const { return Q_strcmp( a, b ) < 0; } };
  93. struct StringEqualFunctor { bool operator()( const char *a, const char *b ) const { return Q_strcmp( a, b ) == 0; } };
  94. struct CaselessStringLessFunctor { bool operator()( const char *a, const char *b ) const { return Q_strcasecmp( a, b ) < 0; } };
  95. struct CaselessStringEqualFunctor { bool operator()( const char *a, const char *b ) const { return Q_strcasecmp( a, b ) == 0; } };
  96. struct Mix32HashFunctor { unsigned int operator()( uint32 s ) const; };
  97. struct StringHashFunctor { unsigned int operator()( const char* s ) const; };
  98. struct CaselessStringHashFunctor { unsigned int operator()( const char* s ) const; };
  99. struct PointerLessFunctor { bool operator()( const void *a, const void *b ) const { return a < b; } };
  100. struct PointerEqualFunctor { bool operator()( const void *a, const void *b ) const { return a == b; } };
  101. struct PointerHashFunctor { unsigned int operator()( const void* s ) const { return Mix32HashFunctor()((uint32)POINTER_TO_INT(s)); } };
  102. // Generic implementation of Less and Equal functors
  103. template < typename T >
  104. struct DefaultLessFunctor
  105. {
  106. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a < b; }
  107. bool operator()( typename ArgumentTypeInfo< T >::Alt_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a < b; }
  108. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Alt_t b ) const { return a < b; }
  109. };
  110. template < typename T >
  111. struct DefaultEqualFunctor
  112. {
  113. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a == b; }
  114. bool operator()( typename ArgumentTypeInfo< T >::Alt_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a == b; }
  115. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Alt_t b ) const { return a == b; }
  116. };
  117. // Hashes for basic types
  118. template <> struct DefaultHashFunctor<char> : Mix32HashFunctor { };
  119. template <> struct DefaultHashFunctor<signed char> : Mix32HashFunctor { };
  120. template <> struct DefaultHashFunctor<unsigned char> : Mix32HashFunctor { };
  121. template <> struct DefaultHashFunctor<signed short> : Mix32HashFunctor { };
  122. template <> struct DefaultHashFunctor<unsigned short> : Mix32HashFunctor { };
  123. template <> struct DefaultHashFunctor<signed int> : Mix32HashFunctor { };
  124. template <> struct DefaultHashFunctor<unsigned int> : Mix32HashFunctor { };
  125. template <> struct DefaultHashFunctor<signed long> : Mix32HashFunctor { };
  126. template <> struct DefaultHashFunctor<unsigned long> : Mix32HashFunctor { };
  127. template <> struct DefaultHashFunctor<void*> : PointerHashFunctor { };
  128. template <> struct DefaultHashFunctor<const void*> : PointerHashFunctor { };
  129. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
  130. template <> struct DefaultHashFunctor<wchar_t> : Mix32HashFunctor { };
  131. #endif
  132. // String specializations. If you want to operate on raw values, use
  133. // PointerLessFunctor and friends from the "building-block" section above
  134. template <> struct DefaultLessFunctor<char*> : StringLessFunctor { };
  135. template <> struct DefaultLessFunctor<const char*> : StringLessFunctor { };
  136. template <> struct DefaultEqualFunctor<char*> : StringEqualFunctor { };
  137. template <> struct DefaultEqualFunctor<const char*> : StringEqualFunctor { };
  138. template <> struct DefaultHashFunctor<char*> : StringHashFunctor { };
  139. template <> struct DefaultHashFunctor<const char*> : StringHashFunctor { };
  140. // CUtlString/CUtlConstString are specialized here and not in utlstring.h
  141. // because I consider string datatypes to be fundamental, and don't feel
  142. // comfortable making that header file dependent on this one. (henryg)
  143. class CUtlString;
  144. template < typename T > class CUtlConstStringBase;
  145. template <> struct DefaultLessFunctor<CUtlString> : StringLessFunctor { };
  146. template <> struct DefaultHashFunctor<CUtlString> : StringHashFunctor { };
  147. template < typename T > struct DefaultLessFunctor< CUtlConstStringBase<T> > : StringLessFunctor { };
  148. template < typename T > struct DefaultHashFunctor< CUtlConstStringBase<T> > : StringHashFunctor { };
  149. // Helpers to deduce if a type defines a public AltArgumentType_t typedef:
  150. template < typename T >
  151. struct HasClassAltArgumentType
  152. {
  153. template < typename X > static long Test( typename X::AltArgumentType_t* );
  154. template < typename X > static char Test( ... );
  155. enum { value = ( sizeof( Test< T >( NULL ) ) != sizeof( char ) ) };
  156. };
  157. template < typename T, bool = HasClassAltArgumentType< T >::value >
  158. struct GetClassAltArgumentType { typedef typename T::AltArgumentType_t Result_t; };
  159. template < typename T >
  160. struct GetClassAltArgumentType< T, false > { typedef undefined_t Result_t; };
  161. // Unwrap references; reference types don't have member typedefs.
  162. template < typename T >
  163. struct GetClassAltArgumentType< T&, false > : GetClassAltArgumentType< T > { };
  164. // ArgumentTypeInfoImpl is the base for all ArgumentTypeInfo specializations.
  165. template < typename ArgT, typename AltT = typename GetClassAltArgumentType<ArgT>::Result_t >
  166. struct ArgumentTypeInfoImpl
  167. {
  168. enum { has_alt = 1 };
  169. typedef ArgT Arg_t;
  170. typedef AltT Alt_t;
  171. };
  172. // Handle cases where AltArgumentType_t is typedef'd to undefined_t
  173. template < typename ArgT >
  174. struct ArgumentTypeInfoImpl< ArgT, undefined_t >
  175. {
  176. enum { has_alt = 0 };
  177. typedef ArgT Arg_t;
  178. typedef undefined_t Alt_t;
  179. };
  180. // Handle cases where AltArgumentType_t is typedef'd to the primary type
  181. template < typename ArgT >
  182. struct ArgumentTypeInfoImpl< ArgT, ArgT >
  183. {
  184. enum { has_alt = 0 };
  185. typedef ArgT Arg_t;
  186. typedef undefined_t Alt_t;
  187. };
  188. // By default, everything is passed via const ref and doesn't define an alternate type.
  189. template <typename T> struct ArgumentTypeInfo : ArgumentTypeInfoImpl< const T& > { };
  190. // Small native types are most efficiently passed by value.
  191. template <> struct ArgumentTypeInfo< bool > : ArgumentTypeInfoImpl< bool > { };
  192. template <> struct ArgumentTypeInfo< char > : ArgumentTypeInfoImpl< char > { };
  193. template <> struct ArgumentTypeInfo< signed char > : ArgumentTypeInfoImpl< signed char > { };
  194. template <> struct ArgumentTypeInfo< unsigned char > : ArgumentTypeInfoImpl< unsigned char > { };
  195. template <> struct ArgumentTypeInfo< signed short > : ArgumentTypeInfoImpl< signed short > { };
  196. template <> struct ArgumentTypeInfo< unsigned short > : ArgumentTypeInfoImpl< unsigned short > { };
  197. template <> struct ArgumentTypeInfo< signed int > : ArgumentTypeInfoImpl< signed int > { };
  198. template <> struct ArgumentTypeInfo< unsigned int > : ArgumentTypeInfoImpl< unsigned int > { };
  199. template <> struct ArgumentTypeInfo< signed long > : ArgumentTypeInfoImpl< signed long > { };
  200. template <> struct ArgumentTypeInfo< unsigned long > : ArgumentTypeInfoImpl< unsigned long > { };
  201. template <> struct ArgumentTypeInfo< signed long long > : ArgumentTypeInfoImpl< signed long long > { };
  202. template <> struct ArgumentTypeInfo< unsigned long long > : ArgumentTypeInfoImpl< unsigned long long > { };
  203. template <> struct ArgumentTypeInfo< float > : ArgumentTypeInfoImpl< float > { };
  204. template <> struct ArgumentTypeInfo< double > : ArgumentTypeInfoImpl< double > { };
  205. template <> struct ArgumentTypeInfo< long double > : ArgumentTypeInfoImpl< long double > { };
  206. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
  207. template <> struct ArgumentTypeInfo< wchar_t > : ArgumentTypeInfoImpl< wchar_t > { };
  208. #endif
  209. // Pointers are also most efficiently passed by value.
  210. template < typename T > struct ArgumentTypeInfo< T* > : ArgumentTypeInfoImpl< T* > { };
  211. // Specializations to unwrap const-decorated types and references
  212. template <typename T> struct ArgumentTypeInfo<const T> : ArgumentTypeInfo<T> { };
  213. template <typename T> struct ArgumentTypeInfo<volatile T> : ArgumentTypeInfo<T> { };
  214. template <typename T> struct ArgumentTypeInfo<const volatile T> : ArgumentTypeInfo<T> { };
  215. template <typename T> struct ArgumentTypeInfo<T&> : ArgumentTypeInfo<T> { };
  216. template <typename T> struct DefaultLessFunctor<const T> : DefaultLessFunctor<T> { };
  217. template <typename T> struct DefaultLessFunctor<volatile T> : DefaultLessFunctor<T> { };
  218. template <typename T> struct DefaultLessFunctor<const volatile T> : DefaultLessFunctor<T> { };
  219. template <typename T> struct DefaultLessFunctor<T&> : DefaultLessFunctor<T> { };
  220. template <typename T> struct DefaultEqualFunctor<const T> : DefaultEqualFunctor<T> { };
  221. template <typename T> struct DefaultEqualFunctor<volatile T> : DefaultEqualFunctor<T> { };
  222. template <typename T> struct DefaultEqualFunctor<const volatile T> : DefaultEqualFunctor<T> { };
  223. template <typename T> struct DefaultEqualFunctor<T&> : DefaultEqualFunctor<T> { };
  224. template <typename T> struct DefaultHashFunctor<const T> : DefaultHashFunctor<T> { };
  225. template <typename T> struct DefaultHashFunctor<volatile T> : DefaultHashFunctor<T> { };
  226. template <typename T> struct DefaultHashFunctor<const volatile T> : DefaultHashFunctor<T> { };
  227. template <typename T> struct DefaultHashFunctor<T&> : DefaultHashFunctor<T> { };
  228. // Hash all pointer types as raw pointers by default
  229. template <typename T> struct DefaultHashFunctor< T * > : PointerHashFunctor { };
  230. // Here follow the useful implementations.
  231. // Bob Jenkins's 32-bit mix function.
  232. inline unsigned int Mix32HashFunctor::operator()( uint32 n ) const
  233. {
  234. // Perform a mixture of the bits in n, where each bit
  235. // of the input value has an equal chance to affect each
  236. // bit of the output. This turns tightly clustered input
  237. // values into a smooth distribution.
  238. //
  239. // This takes 16-20 cycles on modern x86 architectures;
  240. // that's roughly the same cost as a mispredicted branch.
  241. // It's also reasonably efficient on PPC-based consoles.
  242. //
  243. // If you're still thinking, "too many instructions!",
  244. // do keep in mind that reading one byte of uncached RAM
  245. // is about 30x slower than executing this code. It pays
  246. // to have a good hash function which minimizes collisions
  247. // (and therefore long lookup chains).
  248. n = ( n + 0x7ed55d16 ) + ( n << 12 );
  249. n = ( n ^ 0xc761c23c ) ^ ( n >> 19 );
  250. n = ( n + 0x165667b1 ) + ( n << 5 );
  251. n = ( n + 0xd3a2646c ) ^ ( n << 9 );
  252. n = ( n + 0xfd7046c5 ) + ( n << 3 );
  253. n = ( n ^ 0xb55a4f09 ) ^ ( n >> 16 );
  254. return n;
  255. }
  256. // Based on the widely-used FNV-1A string hash with a final
  257. // mixing step to improve dispersion for very small and very
  258. // large hash table sizes.
  259. inline unsigned int StringHashFunctor::operator()( const char* s ) const
  260. {
  261. uint32 h = 2166136261u;
  262. for ( ; *s; ++s )
  263. {
  264. uint32 c = (unsigned char) *s;
  265. h = (h ^ c) * 16777619;
  266. }
  267. return (h ^ (h << 17)) + (h >> 21);
  268. }
  269. // Equivalent to StringHashFunctor on lower-case strings.
  270. inline unsigned int CaselessStringHashFunctor::operator()( const char* s ) const
  271. {
  272. uint32 h = 2166136261u;
  273. for ( ; *s; ++s )
  274. {
  275. uint32 c = (unsigned char) *s;
  276. // Brutally fast branchless ASCII tolower():
  277. // if ((c >= 'A') && (c <= 'Z')) c += ('a' - 'A');
  278. c += (((('A'-1) - c) & (c - ('Z'+1))) >> 26) & 32;
  279. h = (h ^ c) * 16777619;
  280. }
  281. return (h ^ (h << 17)) + (h >> 21);
  282. }
  283. #endif // UTLCOMMON_H