StringHash.pkg 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. $#include "StringHash.h"
  2. /// 32-bit hash value for a string.
  3. class StringHash
  4. {
  5. public:
  6. /// Construct with zero value.
  7. StringHash() :
  8. value_(0)
  9. {
  10. }
  11. /// Copy-construct from another hash.
  12. StringHash(const StringHash& rhs) :
  13. value_(rhs.value_)
  14. {
  15. }
  16. /// Construct with an initial value.
  17. explicit StringHash(unsigned value) :
  18. value_(value)
  19. {
  20. }
  21. /// Construct from a C string case-insensitively.
  22. StringHash(const char* str);
  23. /// Construct from a string case-insensitively.
  24. StringHash(const String& str);
  25. /// Add a hash.
  26. StringHash operator + (const StringHash& rhs) const
  27. {
  28. StringHash ret;
  29. ret.value_ = value_ + rhs.value_;
  30. return ret;
  31. }
  32. // Test for equality with another hash.
  33. bool operator == (const StringHash& rhs) const { return value_ == rhs.value_; }
  34. /// Test if less than another hash.
  35. bool operator < (const StringHash& rhs) const { return value_ < rhs.value_; }
  36. /// Return true if nonzero hash value.
  37. operator bool () const { return value_ != 0; }
  38. /// Return hash value.
  39. unsigned Value() const { return value_; }
  40. /// Return as string.
  41. String ToString() const;
  42. /// Return hash value for HashSet & HashMap.
  43. unsigned ToHash() const { return value_; }
  44. /// Calculate hash value case-insensitively from a C string.
  45. static unsigned Calculate(const char* str);
  46. /// Zero hash.
  47. static const StringHash ZERO;
  48. };
  49. /// 16-bit hash value for a string.
  50. class ShortStringHash
  51. {
  52. public:
  53. /// Construct with zero hash value.
  54. ShortStringHash();
  55. /// Copy-construct from another hash value.
  56. ShortStringHash(const ShortStringHash& rhs);
  57. /// Copy-construct from another 32-bit hash value (ignore the high bits.)
  58. explicit ShortStringHash(const StringHash& rhs);
  59. /// Construct with an initial value.
  60. explicit ShortStringHash(unsigned short value);
  61. /// Construct from a C string case-insensitively.
  62. ShortStringHash(const char* str);
  63. /// Construct from a string case-insensitively.
  64. ShortStringHash(const String& str);
  65. /// Add a hash.
  66. ShortStringHash operator + (const ShortStringHash& rhs) const;
  67. /// Test for equality with another hash.
  68. bool operator == (const ShortStringHash& rhs) const;
  69. /// Test if less than another hash.
  70. bool operator < (const ShortStringHash& rhs) const;
  71. /// Return hash value.
  72. unsigned short Value() const { return value_; }
  73. /// Calculate hash value case-insensitively from a C string.
  74. static unsigned short Calculate(const char* str);
  75. /// Zero hash.
  76. static const ShortStringHash ZERO;
  77. };