str_stl.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #pragma once
  2. #include <util/memory/alloc.h>
  3. #include <util/digest/numeric.h>
  4. #include <util/generic/string.h>
  5. #include <util/generic/string_hash.h>
  6. #include <util/generic/strbuf.h>
  7. #include <util/generic/typetraits.h>
  8. #include <functional>
  9. #include <typeindex>
  10. #include <utility>
  11. namespace std {
  12. template <>
  13. struct less<const char*> {
  14. bool operator()(const char* x, const char* y) const {
  15. return strcmp(x, y) < 0;
  16. }
  17. };
  18. template <>
  19. struct equal_to<const char*> {
  20. bool operator()(const char* x, const char* y) const {
  21. return strcmp(x, y) == 0;
  22. }
  23. bool operator()(const char* x, const TStringBuf y) const {
  24. return strlen(x) == y.size() && memcmp(x, y.data(), y.size()) == 0;
  25. }
  26. using is_transparent = void;
  27. };
  28. }
  29. namespace NHashPrivate {
  30. template <class T, bool needNumericHashing>
  31. struct THashHelper {
  32. inline size_t operator()(const T& t) const noexcept {
  33. return (size_t)t; // If you have a compilation error here, look at explanation below:
  34. // Probably error is caused by undefined template specialization of THash<T>
  35. // You can find examples of specialization in this file
  36. }
  37. };
  38. template <class T>
  39. struct THashHelper<T, true> {
  40. inline size_t operator()(const T& t) const noexcept {
  41. return NumericHash(t);
  42. }
  43. };
  44. template <typename C>
  45. struct TStringHash {
  46. using is_transparent = void;
  47. inline size_t operator()(const TBasicStringBuf<C> s) const noexcept {
  48. return NHashPrivate::ComputeStringHash(s.data(), s.size());
  49. }
  50. };
  51. }
  52. template <class T>
  53. struct hash: public NHashPrivate::THashHelper<T, std::is_scalar<T>::value && !std::is_integral<T>::value> {
  54. };
  55. template <typename T>
  56. struct hash<const T*> {
  57. inline size_t operator()(const T* t) const noexcept {
  58. return NumericHash(t);
  59. }
  60. };
  61. template <class T>
  62. struct hash<T*>: public ::hash<const T*> {
  63. };
  64. template <>
  65. struct hash<const char*>: ::NHashPrivate::TStringHash<char> {
  66. };
  67. template <>
  68. struct THash<TStringBuf>: ::NHashPrivate::TStringHash<char> {
  69. };
  70. template <>
  71. struct hash<TString>: ::NHashPrivate::TStringHash<char> {
  72. };
  73. template <>
  74. struct hash<TUtf16String>: ::NHashPrivate::TStringHash<wchar16> {
  75. };
  76. template <>
  77. struct THash<TWtringBuf>: ::NHashPrivate::TStringHash<wchar16> {
  78. };
  79. template <>
  80. struct hash<TUtf32String>: ::NHashPrivate::TStringHash<wchar32> {
  81. };
  82. template <>
  83. struct THash<TUtf32StringBuf>: ::NHashPrivate::TStringHash<wchar32> {
  84. };
  85. template <class C, class T, class A>
  86. struct hash<std::basic_string<C, T, A>>: ::NHashPrivate::TStringHash<C> {
  87. };
  88. template <>
  89. struct THash<std::type_index> {
  90. inline size_t operator()(const std::type_index& index) const {
  91. return index.hash_code();
  92. }
  93. };
  94. namespace NHashPrivate {
  95. template <typename T>
  96. Y_FORCE_INLINE static size_t HashObject(const T& val) {
  97. return THash<T>()(val);
  98. }
  99. template <size_t I, bool IsLastElement, typename... TArgs>
  100. struct TupleHashHelper {
  101. Y_FORCE_INLINE static size_t Hash(const std::tuple<TArgs...>& tuple) {
  102. return CombineHashes(HashObject(std::get<I>(tuple)),
  103. TupleHashHelper<I + 1, I + 2 >= sizeof...(TArgs), TArgs...>::Hash(tuple));
  104. }
  105. };
  106. template <size_t I, typename... TArgs>
  107. struct TupleHashHelper<I, true, TArgs...> {
  108. Y_FORCE_INLINE static size_t Hash(const std::tuple<TArgs...>& tuple) {
  109. return HashObject(std::get<I>(tuple));
  110. }
  111. };
  112. }
  113. template <typename... TArgs>
  114. struct THash<std::tuple<TArgs...>> {
  115. size_t operator()(const std::tuple<TArgs...>& tuple) const {
  116. return NHashPrivate::TupleHashHelper<0, 1 >= sizeof...(TArgs), TArgs...>::Hash(tuple);
  117. }
  118. };
  119. template <class T>
  120. struct THash: public ::hash<T> {
  121. };
  122. namespace NHashPrivate {
  123. template <class TFirst, class TSecond, bool IsEmpty = std::is_empty<THash<TFirst>>::value&& std::is_empty<THash<TSecond>>::value>
  124. struct TPairHash {
  125. private:
  126. THash<TFirst> FirstHash;
  127. THash<TSecond> SecondHash;
  128. public:
  129. template <class T>
  130. inline size_t operator()(const T& pair) const {
  131. return CombineHashes(FirstHash(pair.first), SecondHash(pair.second));
  132. }
  133. };
  134. /**
  135. * Specialization for the case where both hash functors are empty. Basically the
  136. * only one we care about. We don't introduce additional specializations for
  137. * cases where only one of the functors is empty as the code bloat is just not worth it.
  138. */
  139. template <class TFirst, class TSecond>
  140. struct TPairHash<TFirst, TSecond, true> {
  141. template <class T>
  142. inline size_t operator()(const T& pair) const {
  143. // maps have TFirst = const TFoo, which would make for an undefined specialization
  144. using TFirstClean = std::remove_cv_t<TFirst>;
  145. using TSecondClean = std::remove_cv_t<TSecond>;
  146. return CombineHashes(THash<TFirstClean>()(pair.first), THash<TSecondClean>()(pair.second));
  147. }
  148. };
  149. }
  150. template <class TFirst, class TSecond>
  151. struct hash<std::pair<TFirst, TSecond>>: public NHashPrivate::TPairHash<TFirst, TSecond> {
  152. };
  153. template <class T>
  154. struct TEqualTo: public std::equal_to<T> {
  155. };
  156. template <>
  157. struct TEqualTo<TString>: public TEqualTo<TStringBuf> {
  158. using is_transparent = void;
  159. };
  160. template <>
  161. struct TEqualTo<TUtf16String>: public TEqualTo<TWtringBuf> {
  162. using is_transparent = void;
  163. };
  164. template <>
  165. struct TEqualTo<TUtf32String>: public TEqualTo<TUtf32StringBuf> {
  166. using is_transparent = void;
  167. };
  168. template <class TFirst, class TSecond>
  169. struct TEqualTo<std::pair<TFirst, TSecond>> {
  170. template <class TOther>
  171. inline bool operator()(const std::pair<TFirst, TSecond>& a, const TOther& b) const {
  172. return TEqualTo<TFirst>()(a.first, b.first) && TEqualTo<TSecond>()(a.second, b.second);
  173. }
  174. using is_transparent = void;
  175. };
  176. template <class T>
  177. struct TCIEqualTo {
  178. };
  179. template <>
  180. struct TCIEqualTo<const char*> {
  181. inline bool operator()(const char* a, const char* b) const {
  182. return stricmp(a, b) == 0;
  183. }
  184. };
  185. template <>
  186. struct TCIEqualTo<TStringBuf> {
  187. inline bool operator()(const TStringBuf a, const TStringBuf b) const {
  188. return a.size() == b.size() && strnicmp(a.data(), b.data(), a.size()) == 0;
  189. }
  190. };
  191. template <>
  192. struct TCIEqualTo<TString> {
  193. inline bool operator()(const TString& a, const TString& b) const {
  194. return a.size() == b.size() && strnicmp(a.data(), b.data(), a.size()) == 0;
  195. }
  196. };
  197. template <class T>
  198. struct TLess: public std::less<T> {
  199. };
  200. template <>
  201. struct TLess<TString>: public TLess<TStringBuf> {
  202. using is_transparent = void;
  203. };
  204. template <>
  205. struct TLess<TUtf16String>: public TLess<TWtringBuf> {
  206. using is_transparent = void;
  207. };
  208. template <>
  209. struct TLess<TUtf32String>: public TLess<TUtf32StringBuf> {
  210. using is_transparent = void;
  211. };
  212. template <class T>
  213. struct TGreater: public std::greater<T> {
  214. };
  215. template <>
  216. struct TGreater<TString>: public TGreater<TStringBuf> {
  217. using is_transparent = void;
  218. };
  219. template <>
  220. struct TGreater<TUtf16String>: public TGreater<TWtringBuf> {
  221. using is_transparent = void;
  222. };
  223. template <>
  224. struct TGreater<TUtf32String>: public TGreater<TUtf32StringBuf> {
  225. using is_transparent = void;
  226. };