StringHash.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef COMMON_STRINGHASH_H
  24. #define COMMON_STRINGHASH_H
  25. #include <string>
  26. //! 32-bit hash value for a string
  27. class StringHash
  28. {
  29. public:
  30. //! Construct with zero value
  31. StringHash() :
  32. mData(0)
  33. {
  34. }
  35. //! Copy-construct from another hash
  36. StringHash(const StringHash& rhs) :
  37. mData(rhs.mData)
  38. {
  39. }
  40. //! Construct from a C string case-insensitively
  41. explicit StringHash(const char* str);
  42. //! Construct from a string case-insensitively
  43. explicit StringHash(const std::string& str);
  44. //! Assign from another hash
  45. StringHash& operator = (const StringHash& rhs)
  46. {
  47. mData = rhs.mData;
  48. return *this;
  49. }
  50. //! Add a hash
  51. StringHash operator + (const StringHash& rhs) const
  52. {
  53. StringHash ret;
  54. ret.mData = mData + rhs.mData;
  55. return ret;
  56. }
  57. //! Add-assign a hash
  58. StringHash& operator += (const StringHash& rhs)
  59. {
  60. mData += rhs.mData;
  61. return *this;
  62. }
  63. // Test for equality with another hash
  64. bool operator == (const StringHash& rhs) const
  65. {
  66. return mData == rhs.mData;
  67. }
  68. //! Test for inequality with another hash
  69. bool operator != (const StringHash& rhs) const
  70. {
  71. return mData != rhs.mData;
  72. }
  73. //! Test if less than another hash
  74. bool operator < (const StringHash& rhs) const
  75. {
  76. return mData < rhs.mData;
  77. }
  78. //! Test if greater than another hash
  79. bool operator > (const StringHash& rhs) const
  80. {
  81. return mData > rhs.mData;
  82. }
  83. //! Return true if nonzero hash value
  84. operator bool () const
  85. {
  86. return mData != 0;
  87. }
  88. //! Return unsigned data
  89. const unsigned* getData() const { return &mData; }
  90. //! Calculate hash value case-insensitively from a C string
  91. static unsigned calculate(const char* str);
  92. //! Hash value
  93. unsigned mData;
  94. };
  95. //! 16-bit hash value for a string
  96. class ShortStringHash
  97. {
  98. public:
  99. //! Construct with zero hash value
  100. ShortStringHash() :
  101. mData(0)
  102. {
  103. }
  104. //! Copy-construct from another hash value
  105. ShortStringHash(const ShortStringHash& rhs) :
  106. mData(rhs.mData)
  107. {
  108. }
  109. //! Copy-construct from another 32-bit hash value (ignore the high bits)
  110. explicit ShortStringHash(const StringHash& rhs) :
  111. mData(rhs.mData)
  112. {
  113. }
  114. //! Construct from a C string case-insensitively
  115. explicit ShortStringHash(const char* str);
  116. //! Construct from a string case-insensitively
  117. explicit ShortStringHash(const std::string& str);
  118. //! Assign from another hash
  119. ShortStringHash& operator = (const ShortStringHash& rhs)
  120. {
  121. mData = rhs.mData;
  122. return *this;
  123. }
  124. //! Add a hash
  125. ShortStringHash operator + (const ShortStringHash& rhs) const
  126. {
  127. ShortStringHash ret;
  128. ret.mData = mData + rhs.mData;
  129. return ret;
  130. }
  131. // Add-assign a hash
  132. ShortStringHash& operator += (const ShortStringHash& rhs)
  133. {
  134. mData += rhs.mData;
  135. return *this;
  136. }
  137. //! Test for equality with another hash
  138. bool operator == (const ShortStringHash& rhs) const
  139. {
  140. return mData == rhs.mData;
  141. }
  142. //! Test for inequality with another hash
  143. bool operator != (const ShortStringHash& rhs) const
  144. {
  145. return mData != rhs.mData;
  146. }
  147. //! Test if less than another hash
  148. bool operator < (const ShortStringHash& rhs) const
  149. {
  150. return mData < rhs.mData;
  151. }
  152. //! Test if greater than another hash
  153. bool operator > (const ShortStringHash& rhs) const
  154. {
  155. return mData > rhs.mData;
  156. }
  157. //! Return true if nonzero hash value
  158. operator bool () const
  159. {
  160. return mData != 0;
  161. }
  162. //! Return unsigned short data
  163. const unsigned short* getData() const { return &mData; }
  164. //! Calculate hash value case-insensitively from a C string
  165. static unsigned short calculate(const char* str);
  166. //! Hash value
  167. unsigned short mData;
  168. };
  169. #define HASH(str) (StringHash(#str))
  170. #define SHORT_HASH(str) (ShortStringHash(#str))
  171. #define DEFINE_HASH(id, str) static const StringHash id(#str)
  172. #define DEFINE_SHORT_HASH(id, str) static const ShortStringHash id(#str)
  173. //! Register reverse mapping for a hash. Optionally throw if a hash collision occurs
  174. void registerHash(const std::string& str, bool throwIfCollision = true);
  175. //! Return string for a hash if reverse mapping exists
  176. const std::string& hashToString(StringHash hash);
  177. //! Register reverse mapping for a short hash. Optionally throw if a hash collision occurs
  178. void registerShortHash(const std::string& str, bool throwIfCollision = true);
  179. //! Return string for a short hash if reverse mapping exists
  180. const std::string& shortHashToString(ShortStringHash hash);
  181. #endif // COMMON_STRINGHASH_H