StringHash.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Str.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 as string.
  80. String ToString() const;
  81. /// Return hash value for HashSet & HashMap.
  82. unsigned ToHash() const { return value_; }
  83. /// Calculate hash value case-insensitively from a C string.
  84. static unsigned Calculate(const char* str);
  85. /// Zero hash.
  86. static const StringHash ZERO;
  87. private:
  88. /// Hash value.
  89. unsigned value_;
  90. };
  91. /// 16-bit hash value for a string.
  92. class ShortStringHash
  93. {
  94. public:
  95. /// Construct with zero hash value.
  96. ShortStringHash() :
  97. value_(0)
  98. {
  99. }
  100. /// Copy-construct from another hash value.
  101. ShortStringHash(const ShortStringHash& rhs) :
  102. value_(rhs.value_)
  103. {
  104. }
  105. /// Copy-construct from another 32-bit hash value (ignore the high bits.)
  106. explicit ShortStringHash(const StringHash& rhs) :
  107. value_(rhs.GetValue())
  108. {
  109. }
  110. /// Construct with an initial value.
  111. explicit ShortStringHash(unsigned short value) :
  112. value_(value)
  113. {
  114. }
  115. /// Construct from a C string case-insensitively.
  116. explicit ShortStringHash(const char* str);
  117. /// Construct from a string case-insensitively.
  118. explicit ShortStringHash(const String& str);
  119. /// Assign from another hash.
  120. ShortStringHash& operator = (const ShortStringHash& rhs)
  121. {
  122. value_ = rhs.value_;
  123. return *this;
  124. }
  125. /// Add a hash.
  126. ShortStringHash operator + (const ShortStringHash& rhs) const
  127. {
  128. ShortStringHash ret;
  129. ret.value_ = value_ + rhs.value_;
  130. return ret;
  131. }
  132. // Add-assign a hash.
  133. ShortStringHash& operator += (const ShortStringHash& rhs)
  134. {
  135. value_ += rhs.value_;
  136. return *this;
  137. }
  138. /// Test for equality with another hash.
  139. bool operator == (const ShortStringHash& rhs) const { return value_ == rhs.value_; }
  140. /// Test for inequality with another hash.
  141. bool operator != (const ShortStringHash& rhs) const { return value_ != rhs.value_; }
  142. /// Test if less than another hash.
  143. bool operator < (const ShortStringHash& rhs) const { return value_ < rhs.value_; }
  144. /// Test if greater than another hash.
  145. bool operator > (const ShortStringHash& rhs) const { return value_ > rhs.value_; }
  146. /// Return true if nonzero hash value.
  147. operator bool () const { return value_ != 0; }
  148. /// Return hash value.
  149. unsigned short GetValue() const { return value_; }
  150. /// Return as string.
  151. String ToString() const;
  152. /// Return hash value for HashSet & HashMap.
  153. unsigned ToHash() const { return value_; }
  154. /// Calculate hash value case-insensitively from a C string.
  155. static unsigned short Calculate(const char* str);
  156. /// Zero hash.
  157. static const ShortStringHash ZERO;
  158. private:
  159. /// Hash value.
  160. unsigned short value_;
  161. };
  162. #define HASH(str) (StringHash(#str))
  163. #define SHORTHASH(str) (ShortStringHash(#str))