StringHash.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #pragma once
  24. #include "StringBase.h"
  25. /// 32-bit hash value for a string.
  26. class StringHash
  27. {
  28. public:
  29. /// Construct with zero value.
  30. StringHash() :
  31. value_(0)
  32. {
  33. }
  34. /// Copy-construct from another hash.
  35. StringHash(const StringHash& rhs) :
  36. value_(rhs.value_)
  37. {
  38. }
  39. /// Construct with an initial value.
  40. explicit StringHash(unsigned value) :
  41. value_(value)
  42. {
  43. }
  44. /// Construct from a C string case-insensitively.
  45. explicit StringHash(const char* str);
  46. /// Construct from a string case-insensitively.
  47. explicit StringHash(const String& str);
  48. /// Assign from another hash.
  49. StringHash& operator = (const StringHash& rhs)
  50. {
  51. value_ = rhs.value_;
  52. return *this;
  53. }
  54. /// Add a hash.
  55. StringHash operator + (const StringHash& rhs) const
  56. {
  57. StringHash ret;
  58. ret.value_ = value_ + rhs.value_;
  59. return ret;
  60. }
  61. /// Add-assign a hash.
  62. StringHash& operator += (const StringHash& rhs)
  63. {
  64. value_ += rhs.value_;
  65. return *this;
  66. }
  67. // Test for equality with another hash.
  68. bool operator == (const StringHash& rhs) const { return value_ == rhs.value_; }
  69. /// Test for inequality with another hash.
  70. bool operator != (const StringHash& rhs) const { return value_ != rhs.value_; }
  71. /// Test if less than another hash.
  72. bool operator < (const StringHash& rhs) const { return value_ < rhs.value_; }
  73. /// Test if greater than another hash.
  74. bool operator > (const StringHash& rhs) const { return value_ > rhs.value_; }
  75. /// Return true if nonzero hash value.
  76. operator bool () const { return value_ != 0; }
  77. /// Return hash value.
  78. unsigned GetValue() const { return value_; }
  79. /// Return unsigned data.
  80. const unsigned* GetData() const { return &value_; }
  81. /// Return as string.
  82. String ToString() const;
  83. /// Return hash value for HashSet & HashMap.
  84. unsigned ToHash() const { return value_; }
  85. /// Calculate hash value case-insensitively from a C string.
  86. static unsigned Calculate(const char* str);
  87. /// Zero hash.
  88. static const StringHash ZERO;
  89. private:
  90. /// Hash value.
  91. unsigned value_;
  92. };
  93. /// 16-bit hash value for a string.
  94. class ShortStringHash
  95. {
  96. public:
  97. /// Construct with zero hash value.
  98. ShortStringHash() :
  99. value_(0)
  100. {
  101. }
  102. /// Copy-construct from another hash value.
  103. ShortStringHash(const ShortStringHash& rhs) :
  104. value_(rhs.value_)
  105. {
  106. }
  107. /// Copy-construct from another 32-bit hash value (ignore the high bits.)
  108. explicit ShortStringHash(const StringHash& rhs) :
  109. value_(rhs.GetValue())
  110. {
  111. }
  112. /// Construct with an initial value.
  113. explicit ShortStringHash(unsigned short value) :
  114. value_(value)
  115. {
  116. }
  117. /// Construct from a C string case-insensitively.
  118. explicit ShortStringHash(const char* str);
  119. /// Construct from a string case-insensitively.
  120. explicit ShortStringHash(const String& str);
  121. /// Assign from another hash.
  122. ShortStringHash& operator = (const ShortStringHash& rhs)
  123. {
  124. value_ = rhs.value_;
  125. return *this;
  126. }
  127. /// Add a hash.
  128. ShortStringHash operator + (const ShortStringHash& rhs) const
  129. {
  130. ShortStringHash ret;
  131. ret.value_ = value_ + rhs.value_;
  132. return ret;
  133. }
  134. // Add-assign a hash.
  135. ShortStringHash& operator += (const ShortStringHash& rhs)
  136. {
  137. value_ += rhs.value_;
  138. return *this;
  139. }
  140. /// Test for equality with another hash.
  141. bool operator == (const ShortStringHash& rhs) const { return value_ == rhs.value_; }
  142. /// Test for inequality with another hash.
  143. bool operator != (const ShortStringHash& rhs) const { return value_ != rhs.value_; }
  144. /// Test if less than another hash.
  145. bool operator < (const ShortStringHash& rhs) const { return value_ < rhs.value_; }
  146. /// Test if greater than another hash.
  147. bool operator > (const ShortStringHash& rhs) const { return value_ > rhs.value_; }
  148. /// Return true if nonzero hash value.
  149. operator bool () const { return value_ != 0; }
  150. /// Return hash value.
  151. unsigned short GetValue() const { return value_; }
  152. /// Return unsigned short data.
  153. const unsigned short* GetData() const { return &value_; }
  154. /// Return as string.
  155. String ToString() const;
  156. /// Return hash value for HashSet & HashMap.
  157. unsigned ToHash() const { return value_; }
  158. /// Calculate hash value case-insensitively from a C string.
  159. static unsigned short Calculate(const char* str);
  160. /// Zero hash.
  161. static const ShortStringHash ZERO;
  162. private:
  163. /// Hash value.
  164. unsigned short value_;
  165. };
  166. #define HASH(str) (StringHash(#str))
  167. #define SHORTHASH(str) (ShortStringHash(#str))