Team Fortress 2 Source Code as on 22/4/2020
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.

371 lines
16 KiB

  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 Mix64HashFunctor { unsigned int operator()( uint64 s ) const; };
  98. struct StringHashFunctor { unsigned int operator()( const char* s ) const; };
  99. struct CaselessStringHashFunctor { unsigned int operator()( const char* s ) const; };
  100. struct PointerLessFunctor { bool operator()( const void *a, const void *b ) const { return a < b; } };
  101. struct PointerEqualFunctor { bool operator()( const void *a, const void *b ) const { return a == b; } };
  102. #if defined( PLATFORM_64BITS )
  103. struct PointerHashFunctor { unsigned int operator()( const void* s ) const { return Mix64HashFunctor()( ( uintp ) s ); } };
  104. #else
  105. struct PointerHashFunctor { unsigned int operator()( const void* s ) const { return Mix32HashFunctor()( ( uintp ) s ); } };
  106. #endif
  107. // Generic implementation of Less and Equal functors
  108. template < typename T >
  109. struct DefaultLessFunctor
  110. {
  111. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a < b; }
  112. bool operator()( typename ArgumentTypeInfo< T >::Alt_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a < b; }
  113. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Alt_t b ) const { return a < b; }
  114. };
  115. template < typename T >
  116. struct DefaultEqualFunctor
  117. {
  118. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a == b; }
  119. bool operator()( typename ArgumentTypeInfo< T >::Alt_t a, typename ArgumentTypeInfo< T >::Arg_t b ) const { return a == b; }
  120. bool operator()( typename ArgumentTypeInfo< T >::Arg_t a, typename ArgumentTypeInfo< T >::Alt_t b ) const { return a == b; }
  121. };
  122. // Hashes for basic types
  123. template <> struct DefaultHashFunctor<char> : Mix32HashFunctor { };
  124. template <> struct DefaultHashFunctor<signed char> : Mix32HashFunctor { };
  125. template <> struct DefaultHashFunctor<unsigned char> : Mix32HashFunctor { };
  126. template <> struct DefaultHashFunctor<signed short> : Mix32HashFunctor { };
  127. template <> struct DefaultHashFunctor<unsigned short> : Mix32HashFunctor { };
  128. template <> struct DefaultHashFunctor<signed int> : Mix32HashFunctor { };
  129. template <> struct DefaultHashFunctor<unsigned int> : Mix32HashFunctor { };
  130. #if !defined(PLATFORM_64BITS) || defined(_WIN32)
  131. template <> struct DefaultHashFunctor<signed long> : Mix32HashFunctor { };
  132. template <> struct DefaultHashFunctor<unsigned long> : Mix32HashFunctor { };
  133. #elif defined(POSIX)
  134. template <> struct DefaultHashFunctor<signed long> : Mix64HashFunctor { };
  135. template <> struct DefaultHashFunctor<unsigned long> : Mix64HashFunctor { };
  136. #endif
  137. template <> struct DefaultHashFunctor<signed long long> : Mix64HashFunctor { };
  138. template <> struct DefaultHashFunctor<unsigned long long> : Mix64HashFunctor { };
  139. template <> struct DefaultHashFunctor<void*> : PointerHashFunctor { };
  140. template <> struct DefaultHashFunctor<const void*> : PointerHashFunctor { };
  141. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
  142. template <> struct DefaultHashFunctor<wchar_t> : Mix32HashFunctor { };
  143. #endif
  144. // String specializations. If you want to operate on raw values, use
  145. // PointerLessFunctor and friends from the "building-block" section above
  146. template <> struct DefaultLessFunctor<char*> : StringLessFunctor { };
  147. template <> struct DefaultLessFunctor<const char*> : StringLessFunctor { };
  148. template <> struct DefaultEqualFunctor<char*> : StringEqualFunctor { };
  149. template <> struct DefaultEqualFunctor<const char*> : StringEqualFunctor { };
  150. template <> struct DefaultHashFunctor<char*> : StringHashFunctor { };
  151. template <> struct DefaultHashFunctor<const char*> : StringHashFunctor { };
  152. // CUtlString/CUtlConstString are specialized here and not in utlstring.h
  153. // because I consider string datatypes to be fundamental, and don't feel
  154. // comfortable making that header file dependent on this one. (henryg)
  155. class CUtlString;
  156. template < typename T > class CUtlConstStringBase;
  157. template <> struct DefaultLessFunctor<CUtlString> : StringLessFunctor { };
  158. template <> struct DefaultHashFunctor<CUtlString> : StringHashFunctor { };
  159. template < typename T > struct DefaultLessFunctor< CUtlConstStringBase<T> > : StringLessFunctor { };
  160. template < typename T > struct DefaultHashFunctor< CUtlConstStringBase<T> > : StringHashFunctor { };
  161. // Helpers to deduce if a type defines a public AltArgumentType_t typedef:
  162. template < typename T >
  163. struct HasClassAltArgumentType
  164. {
  165. template < typename X > static long Test( typename X::AltArgumentType_t* );
  166. template < typename X > static char Test( ... );
  167. enum { value = ( sizeof( Test< T >( NULL ) ) != sizeof( char ) ) };
  168. };
  169. template < typename T, bool = HasClassAltArgumentType< T >::value >
  170. struct GetClassAltArgumentType { typedef typename T::AltArgumentType_t Result_t; };
  171. template < typename T >
  172. struct GetClassAltArgumentType< T, false > { typedef undefined_t Result_t; };
  173. // Unwrap references; reference types don't have member typedefs.
  174. template < typename T >
  175. struct GetClassAltArgumentType< T&, false > : GetClassAltArgumentType< T > { };
  176. // ArgumentTypeInfoImpl is the base for all ArgumentTypeInfo specializations.
  177. template < typename ArgT, typename AltT = typename GetClassAltArgumentType<ArgT>::Result_t >
  178. struct ArgumentTypeInfoImpl
  179. {
  180. enum { has_alt = 1 };
  181. typedef ArgT Arg_t;
  182. typedef AltT Alt_t;
  183. };
  184. // Handle cases where AltArgumentType_t is typedef'd to undefined_t
  185. template < typename ArgT >
  186. struct ArgumentTypeInfoImpl< ArgT, undefined_t >
  187. {
  188. enum { has_alt = 0 };
  189. typedef ArgT Arg_t;
  190. typedef undefined_t Alt_t;
  191. };
  192. // Handle cases where AltArgumentType_t is typedef'd to the primary type
  193. template < typename ArgT >
  194. struct ArgumentTypeInfoImpl< ArgT, ArgT >
  195. {
  196. enum { has_alt = 0 };
  197. typedef ArgT Arg_t;
  198. typedef undefined_t Alt_t;
  199. };
  200. // By default, everything is passed via const ref and doesn't define an alternate type.
  201. template <typename T> struct ArgumentTypeInfo : ArgumentTypeInfoImpl< const T& > { };
  202. // Small native types are most efficiently passed by value.
  203. template <> struct ArgumentTypeInfo< bool > : ArgumentTypeInfoImpl< bool > { };
  204. template <> struct ArgumentTypeInfo< char > : ArgumentTypeInfoImpl< char > { };
  205. template <> struct ArgumentTypeInfo< signed char > : ArgumentTypeInfoImpl< signed char > { };
  206. template <> struct ArgumentTypeInfo< unsigned char > : ArgumentTypeInfoImpl< unsigned char > { };
  207. template <> struct ArgumentTypeInfo< signed short > : ArgumentTypeInfoImpl< signed short > { };
  208. template <> struct ArgumentTypeInfo< unsigned short > : ArgumentTypeInfoImpl< unsigned short > { };
  209. template <> struct ArgumentTypeInfo< signed int > : ArgumentTypeInfoImpl< signed int > { };
  210. template <> struct ArgumentTypeInfo< unsigned int > : ArgumentTypeInfoImpl< unsigned int > { };
  211. template <> struct ArgumentTypeInfo< signed long > : ArgumentTypeInfoImpl< signed long > { };
  212. template <> struct ArgumentTypeInfo< unsigned long > : ArgumentTypeInfoImpl< unsigned long > { };
  213. template <> struct ArgumentTypeInfo< signed long long > : ArgumentTypeInfoImpl< signed long long > { };
  214. template <> struct ArgumentTypeInfo< unsigned long long > : ArgumentTypeInfoImpl< unsigned long long > { };
  215. template <> struct ArgumentTypeInfo< float > : ArgumentTypeInfoImpl< float > { };
  216. template <> struct ArgumentTypeInfo< double > : ArgumentTypeInfoImpl< double > { };
  217. template <> struct ArgumentTypeInfo< long double > : ArgumentTypeInfoImpl< long double > { };
  218. #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
  219. template <> struct ArgumentTypeInfo< wchar_t > : ArgumentTypeInfoImpl< wchar_t > { };
  220. #endif
  221. // Pointers are also most efficiently passed by value.
  222. template < typename T > struct ArgumentTypeInfo< T* > : ArgumentTypeInfoImpl< T* > { };
  223. // Specializations to unwrap const-decorated types and references
  224. template <typename T> struct ArgumentTypeInfo<const T> : ArgumentTypeInfo<T> { };
  225. template <typename T> struct ArgumentTypeInfo<volatile T> : ArgumentTypeInfo<T> { };
  226. template <typename T> struct ArgumentTypeInfo<const volatile T> : ArgumentTypeInfo<T> { };
  227. template <typename T> struct ArgumentTypeInfo<T&> : ArgumentTypeInfo<T> { };
  228. template <typename T> struct DefaultLessFunctor<const T> : DefaultLessFunctor<T> { };
  229. template <typename T> struct DefaultLessFunctor<volatile T> : DefaultLessFunctor<T> { };
  230. template <typename T> struct DefaultLessFunctor<const volatile T> : DefaultLessFunctor<T> { };
  231. template <typename T> struct DefaultLessFunctor<T&> : DefaultLessFunctor<T> { };
  232. template <typename T> struct DefaultEqualFunctor<const T> : DefaultEqualFunctor<T> { };
  233. template <typename T> struct DefaultEqualFunctor<volatile T> : DefaultEqualFunctor<T> { };
  234. template <typename T> struct DefaultEqualFunctor<const volatile T> : DefaultEqualFunctor<T> { };
  235. template <typename T> struct DefaultEqualFunctor<T&> : DefaultEqualFunctor<T> { };
  236. template <typename T> struct DefaultHashFunctor<const T> : DefaultHashFunctor<T> { };
  237. template <typename T> struct DefaultHashFunctor<volatile T> : DefaultHashFunctor<T> { };
  238. template <typename T> struct DefaultHashFunctor<const volatile T> : DefaultHashFunctor<T> { };
  239. template <typename T> struct DefaultHashFunctor<T&> : DefaultHashFunctor<T> { };
  240. // Hash all pointer types as raw pointers by default
  241. template <typename T> struct DefaultHashFunctor< T * > : PointerHashFunctor { };
  242. // Here follow the useful implementations.
  243. // Bob Jenkins's 32-bit mix function.
  244. inline unsigned int Mix32HashFunctor::operator()( uint32 n ) const
  245. {
  246. // Perform a mixture of the bits in n, where each bit
  247. // of the input value has an equal chance to affect each
  248. // bit of the output. This turns tightly clustered input
  249. // values into a smooth distribution.
  250. //
  251. // This takes 16-20 cycles on modern x86 architectures;
  252. // that's roughly the same cost as a mispredicted branch.
  253. // It's also reasonably efficient on PPC-based consoles.
  254. //
  255. // If you're still thinking, "too many instructions!",
  256. // do keep in mind that reading one byte of uncached RAM
  257. // is about 30x slower than executing this code. It pays
  258. // to have a good hash function which minimizes collisions
  259. // (and therefore long lookup chains).
  260. n = ( n + 0x7ed55d16 ) + ( n << 12 );
  261. n = ( n ^ 0xc761c23c ) ^ ( n >> 19 );
  262. n = ( n + 0x165667b1 ) + ( n << 5 );
  263. n = ( n + 0xd3a2646c ) ^ ( n << 9 );
  264. n = ( n + 0xfd7046c5 ) + ( n << 3 );
  265. n = ( n ^ 0xb55a4f09 ) ^ ( n >> 16 );
  266. return n;
  267. }
  268. inline unsigned int Mix64HashFunctor::operator()( uint64 s ) const
  269. {
  270. // Thomas Wang hash, http://www.concentric.net/~ttwang/tech/inthash.htm
  271. s = ( ~s ) + ( s << 21 ); // s = (s << 21) - s - 1;
  272. s = s ^ ( s >> 24 );
  273. s = (s + ( s << 3 ) ) + ( s << 8 ); // s * 265
  274. s = s ^ ( s >> 14 );
  275. s = ( s + ( s << 2 ) ) + ( s << 4 ); // s * 21
  276. s = s ^ ( s >> 28 );
  277. s = s + ( s << 31 );
  278. return (unsigned int)s;
  279. }
  280. // Based on the widely-used FNV-1A string hash with a final
  281. // mixing step to improve dispersion for very small and very
  282. // large hash table sizes.
  283. inline unsigned int StringHashFunctor::operator()( const char* s ) const
  284. {
  285. uint32 h = 2166136261u;
  286. for ( ; *s; ++s )
  287. {
  288. uint32 c = (unsigned char) *s;
  289. h = (h ^ c) * 16777619;
  290. }
  291. return (h ^ (h << 17)) + (h >> 21);
  292. }
  293. // Equivalent to StringHashFunctor on lower-case strings.
  294. inline unsigned int CaselessStringHashFunctor::operator()( const char* s ) const
  295. {
  296. uint32 h = 2166136261u;
  297. for ( ; *s; ++s )
  298. {
  299. uint32 c = (unsigned char) *s;
  300. // Brutally fast branchless ASCII tolower():
  301. // if ((c >= 'A') && (c <= 'Z')) c += ('a' - 'A');
  302. c += (((('A'-1) - c) & (c - ('Z'+1))) >> 26) & 32;
  303. h = (h ^ c) * 16777619;
  304. }
  305. return (h ^ (h << 17)) + (h >> 21);
  306. }
  307. #endif // UTLCOMMON_H