Hash.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /// Pointer hash function.
  25. template <class T> unsigned MakeHash(T* value)
  26. {
  27. return ((unsigned)value) / sizeof(T);
  28. }
  29. /// Const pointer hash function.
  30. template <class T> unsigned MakeHash(const T* value)
  31. {
  32. return ((unsigned)value) / sizeof(T);
  33. }
  34. /// Generic hash function.
  35. template <class T> unsigned MakeHash(const T& value)
  36. {
  37. return value.ToHash();
  38. }
  39. /// Void pointer hash function.
  40. template<> inline unsigned MakeHash(void* value)
  41. {
  42. return (unsigned)value;
  43. }
  44. /// Const void pointer hash function.
  45. template<> inline unsigned MakeHash(const void* value)
  46. {
  47. return (unsigned)value;
  48. }
  49. /// Long long hash function.
  50. template<> inline unsigned MakeHash(const long long& value)
  51. {
  52. return (value >> 32) | (value & 0xffffffff);
  53. }
  54. /// Unsigned long long hash function.
  55. template<> inline unsigned MakeHash(const unsigned long long& value)
  56. {
  57. return (value >> 32) | (value & 0xffffffff);
  58. }
  59. /// Int hash function.
  60. template<> inline unsigned MakeHash(const int& value)
  61. {
  62. return value;
  63. }
  64. /// Unsigned hash function.
  65. template<> inline unsigned MakeHash(const unsigned& value)
  66. {
  67. return value;
  68. }
  69. /// Short hash function.
  70. template<> inline unsigned MakeHash(const short& value)
  71. {
  72. return value;
  73. }
  74. /// Unsigned short hash function.
  75. template<> inline unsigned MakeHash(const unsigned short& value)
  76. {
  77. return value;
  78. }
  79. /// Char hash function.
  80. template<> inline unsigned MakeHash(const char& value)
  81. {
  82. return value;
  83. }
  84. /// Unsigned char hash function.
  85. template<> inline unsigned MakeHash(const unsigned char& value)
  86. {
  87. return value;
  88. }