StringHash.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/Str.h"
  5. #include "../Math/MathDefs.h"
  6. namespace Urho3D
  7. {
  8. class StringHashRegister;
  9. /// 32-bit hash value for a string.
  10. class URHO3D_API StringHash
  11. {
  12. public:
  13. /// Construct with zero value.
  14. StringHash() noexcept
  15. : value_(0)
  16. {
  17. }
  18. /// Copy-construct from another hash.
  19. StringHash(const StringHash& rhs) noexcept = default;
  20. /// Construct with an initial value.
  21. constexpr explicit StringHash(hash32 value) noexcept
  22. : value_(value)
  23. {
  24. }
  25. #ifdef URHO3D_HASH_DEBUG
  26. /// Construct from a C string.
  27. StringHash(const char* str) noexcept; // NOLINT(google-explicit-constructor)
  28. #else
  29. constexpr StringHash(const char* str) noexcept
  30. : value_(Calculate(str))
  31. {
  32. }
  33. #endif
  34. /// Construct from a string.
  35. StringHash(const String& str) noexcept; // NOLINT(google-explicit-constructor)
  36. /// Assign from another hash.
  37. StringHash& operator =(const StringHash& rhs) noexcept = default;
  38. /// Add a hash.
  39. StringHash operator +(const StringHash& rhs) const
  40. {
  41. StringHash ret;
  42. ret.value_ = value_ + rhs.value_;
  43. return ret;
  44. }
  45. /// Add-assign a hash.
  46. StringHash& operator +=(const StringHash& rhs)
  47. {
  48. value_ += rhs.value_;
  49. return *this;
  50. }
  51. /// Test for equality with another hash.
  52. bool operator ==(const StringHash& rhs) const { return value_ == rhs.value_; }
  53. /// Test for inequality with another hash.
  54. bool operator !=(const StringHash& rhs) const { return value_ != rhs.value_; }
  55. /// Test if less than another hash.
  56. bool operator <(const StringHash& rhs) const { return value_ < rhs.value_; }
  57. /// Test if greater than another hash.
  58. bool operator >(const StringHash& rhs) const { return value_ > rhs.value_; }
  59. /// Return true if nonzero hash value.
  60. explicit operator bool() const { return value_ != 0; }
  61. /// Return hash value.
  62. /// @property
  63. hash32 Value() const { return value_; }
  64. /// Return as string.
  65. String ToString() const;
  66. /// Return string which has specific hash value. Return first string if many (in order of calculation). Use for debug purposes only. Return empty string if URHO3D_HASH_DEBUG is off.
  67. String Reverse() const;
  68. /// Return hash value for HashSet & HashMap.
  69. hash32 ToHash() const { return value_; }
  70. /// Calculate hash value from a C string.
  71. static constexpr hash32 Calculate(const char* str, hash32 hash = 0)
  72. {
  73. if (!str)
  74. return hash;
  75. while (*str)
  76. hash = SDBMHash(hash, (u8)*str++);
  77. return hash;
  78. }
  79. /// Get global StringHashRegister. Use for debug purposes only. Return nullptr if URHO3D_HASH_DEBUG is off.
  80. static StringHashRegister* GetGlobalStringHashRegister();
  81. /// Zero hash.
  82. static const StringHash ZERO;
  83. private:
  84. /// Hash value.
  85. hash32 value_;
  86. };
  87. constexpr StringHash operator ""_hash(const char* str, size_t)
  88. {
  89. return StringHash(StringHash::Calculate(str));
  90. }
  91. }