StringHash.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Str.h"
  24. namespace Urho3D
  25. {
  26. /// 32-bit hash value for a string.
  27. class URHO3D_API StringHash
  28. {
  29. public:
  30. /// Construct with zero value.
  31. StringHash() :
  32. value_(0)
  33. {
  34. }
  35. /// Copy-construct from another hash.
  36. StringHash(const StringHash& rhs) :
  37. value_(rhs.value_)
  38. {
  39. }
  40. /// Construct with an initial value.
  41. explicit StringHash(unsigned value) :
  42. value_(value)
  43. {
  44. }
  45. /// Construct from a C string case-insensitively.
  46. StringHash(const char* str);
  47. /// Construct from a string case-insensitively.
  48. StringHash(const String& str);
  49. /// Assign from another hash.
  50. StringHash& operator = (const StringHash& rhs)
  51. {
  52. value_ = rhs.value_;
  53. return *this;
  54. }
  55. /// Add a hash.
  56. StringHash operator + (const StringHash& rhs) const
  57. {
  58. StringHash ret;
  59. ret.value_ = value_ + rhs.value_;
  60. return ret;
  61. }
  62. /// Add-assign a hash.
  63. StringHash& operator += (const StringHash& rhs)
  64. {
  65. value_ += rhs.value_;
  66. return *this;
  67. }
  68. // Test for equality with another hash.
  69. bool operator == (const StringHash& rhs) const { return value_ == rhs.value_; }
  70. /// Test for inequality with another hash.
  71. bool operator != (const StringHash& rhs) const { return value_ != rhs.value_; }
  72. /// Test if less than another hash.
  73. bool operator < (const StringHash& rhs) const { return value_ < rhs.value_; }
  74. /// Test if greater than another hash.
  75. bool operator > (const StringHash& rhs) const { return value_ > rhs.value_; }
  76. /// Return true if nonzero hash value.
  77. operator bool () const { return value_ != 0; }
  78. /// Return hash value.
  79. unsigned Value() const { return value_; }
  80. /// Return as string.
  81. String ToString() const;
  82. /// Return hash value for HashSet & HashMap.
  83. unsigned ToHash() const { return value_; }
  84. /// Calculate hash value case-insensitively from a C string.
  85. static unsigned Calculate(const char* str);
  86. /// Zero hash.
  87. static const StringHash ZERO;
  88. private:
  89. /// Hash value.
  90. unsigned value_;
  91. };
  92. }