Hash.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Hash.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Hash.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Hashing
  8. // Module: Hash
  9. //
  10. // Definition of the Hash class.
  11. //
  12. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Hash_INCLUDED
  18. #define Foundation_Hash_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include <cstddef>
  21. namespace Poco {
  22. std::size_t Foundation_API hash(Int8 n);
  23. std::size_t Foundation_API hash(UInt8 n);
  24. std::size_t Foundation_API hash(Int16 n);
  25. std::size_t Foundation_API hash(UInt16 n);
  26. std::size_t Foundation_API hash(Int32 n);
  27. std::size_t Foundation_API hash(UInt32 n);
  28. std::size_t Foundation_API hash(Int64 n);
  29. std::size_t Foundation_API hash(UInt64 n);
  30. std::size_t Foundation_API hash(const std::string& str);
  31. template <class T>
  32. struct Hash
  33. /// A generic hash function.
  34. {
  35. std::size_t operator () (T value) const
  36. /// Returns the hash for the given value.
  37. {
  38. return Poco::hash(value);
  39. }
  40. };
  41. //
  42. // inlines
  43. //
  44. inline std::size_t hash(Int8 n)
  45. {
  46. return static_cast<std::size_t>(n)*2654435761U;
  47. }
  48. inline std::size_t hash(UInt8 n)
  49. {
  50. return static_cast<std::size_t>(n)*2654435761U;
  51. }
  52. inline std::size_t hash(Int16 n)
  53. {
  54. return static_cast<std::size_t>(n)*2654435761U;
  55. }
  56. inline std::size_t hash(UInt16 n)
  57. {
  58. return static_cast<std::size_t>(n)*2654435761U;
  59. }
  60. inline std::size_t hash(Int32 n)
  61. {
  62. return static_cast<std::size_t>(n)*2654435761U;
  63. }
  64. inline std::size_t hash(UInt32 n)
  65. {
  66. return static_cast<std::size_t>(n)*2654435761U;
  67. }
  68. inline std::size_t hash(Int64 n)
  69. {
  70. return static_cast<std::size_t>(n)*2654435761U;
  71. }
  72. inline std::size_t hash(UInt64 n)
  73. {
  74. return static_cast<std::size_t>(n)*2654435761U;
  75. }
  76. } // namespace Poco
  77. #endif // Foundation_Hash_INCLUDED