tc_hash_fun.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef _TC_HASH_FUN_H_
  17. #define _TC_HASH_FUN_H_
  18. #include <iostream>
  19. #include <string>
  20. using namespace std;
  21. namespace tars
  22. {
  23. /////////////////////////////////////////////////
  24. /**
  25. * @file tc_hash_fun.h
  26. * @brief hash算法.
  27. *可以对输入的字节流进行hash得到相当均匀的hash值
  28. *
  29. *
  30. */
  31. /////////////////////////////////////////////////
  32. template <class _Key> struct hash { };
  33. template <class _Key> struct hash_new { };
  34. inline size_t hash_string(const char* s)
  35. {
  36. unsigned long h = 0;
  37. for ( ; *s; ++s)
  38. h = 5*h + *s;
  39. return size_t(h);
  40. }
  41. //////////////////////////////////////////////////////////
  42. /**
  43. * @brief 尽量采用hash_new, 更均衡一些.
  44. *
  45. *可以对输入的字节流进行hash得到相当均匀的hash值
  46. */
  47. //////////////////////////////////////////////////////////
  48. template <>
  49. struct hash<string>
  50. {
  51. size_t operator()(const string &s) const
  52. {
  53. size_t h = 0, g;
  54. const char *arKey = s.c_str();
  55. size_t nKeyLength = s.length();
  56. const char *arEnd = arKey + nKeyLength;
  57. while (arKey < arEnd)
  58. {
  59. h = (h << 4) + *arKey++;
  60. if ((g = (h & 0xF0000000)))
  61. {
  62. h = h ^ (g >> 24);
  63. h = h ^ g;
  64. }
  65. }
  66. return h;
  67. }
  68. };
  69. template <>
  70. struct hash_new<string>
  71. {
  72. size_t operator()(const string &s) const
  73. {
  74. const char *ptr= s.c_str();
  75. size_t key_length = s.length();
  76. uint32_t value= 0;
  77. while (key_length--)
  78. {
  79. value += *ptr++;
  80. value += (value << 10);
  81. value ^= (value >> 6);
  82. }
  83. value += (value << 3);
  84. value ^= (value >> 11);
  85. value += (value << 15);
  86. return value == 0 ? 1 : value;
  87. }
  88. };
  89. template <>
  90. struct hash<char*>
  91. {
  92. size_t operator()(const char* s) const { return hash_string(s); }
  93. };
  94. template <>
  95. struct hash<const char*>
  96. {
  97. size_t operator()(const char* s) const { return hash_string(s); }
  98. };
  99. template <>
  100. struct hash<char>
  101. {
  102. size_t operator()(char x) const { return x; }
  103. };
  104. template <>
  105. struct hash<unsigned char>
  106. {
  107. size_t operator()(unsigned char x) const { return x; }
  108. };
  109. template <>
  110. struct hash<signed char>
  111. {
  112. size_t operator()(unsigned char x) const { return x; }
  113. };
  114. template <>
  115. struct hash<short>
  116. {
  117. size_t operator()(short x) const { return x; }
  118. };
  119. template <>
  120. struct hash<unsigned short>
  121. {
  122. size_t operator()(unsigned short x) const { return x; }
  123. };
  124. template <>
  125. struct hash<int>
  126. {
  127. size_t operator()(int x) const { return x; }
  128. };
  129. template <>
  130. struct hash<unsigned int>
  131. {
  132. size_t operator()(unsigned int x) const { return x; }
  133. };
  134. template <>
  135. struct hash<long>
  136. {
  137. size_t operator()(long x) const { return x; }
  138. };
  139. template <>
  140. struct hash<unsigned long>
  141. {
  142. size_t operator()(unsigned long x) const { return x; }
  143. };
  144. /**
  145. * @brief 一个奇妙的hash算法.
  146. *
  147. *可以对输入的字节流进行hash得到相当均匀的hash值
  148. */
  149. struct magic_string_hash
  150. {
  151. size_t operator()(const string &s) const
  152. {
  153. const char *ptr= s.c_str();
  154. size_t key_length = s.length();
  155. uint32_t value= 0;
  156. while (key_length--)
  157. {
  158. value += *ptr++;
  159. value += (value << 10);
  160. value ^= (value >> 6);
  161. }
  162. value += (value << 3);
  163. value ^= (value >> 11);
  164. value += (value << 15);
  165. return value == 0 ? 1 : value;
  166. }
  167. };
  168. ////////////////////////////////////////////////////////////////////
  169. }
  170. #endif