StringHash.h 5.7 KB

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