HashCode.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef CORE_HPPASH_CODE_HPP
  2. #define CORE_HPPASH_CODE_HPP
  3. #include <limits.h>
  4. #include "utils/Types.hpp"
  5. namespace Core {
  6. template<typename H>
  7. inline u32 hashCode(const H& key) {
  8. return key.hashCode();
  9. }
  10. template<typename T>
  11. inline u32 hashNumber(T t) {
  12. static_assert(sizeof(t) <= 8);
  13. if constexpr(sizeof(t) <= 4) {
  14. return static_cast<u32>(t);
  15. }
  16. return static_cast<u32>(static_cast<u64>(t) ^
  17. (static_cast<u64>(t) >> 32));
  18. }
  19. inline u32 hashCode(char key) {
  20. return hashNumber(key);
  21. }
  22. inline u32 hashCode(signed char key) {
  23. return hashNumber(key);
  24. }
  25. inline u32 hashCode(signed short key) {
  26. return hashNumber(key);
  27. }
  28. inline u32 hashCode(signed int key) {
  29. return hashNumber(key);
  30. }
  31. inline u32 hashCode(signed long key) {
  32. return hashNumber(key);
  33. }
  34. inline u32 hashCode(signed long long key) {
  35. return hashNumber(key);
  36. }
  37. inline u32 hashCode(unsigned char key) {
  38. return hashNumber(key);
  39. }
  40. inline u32 hashCode(unsigned short key) {
  41. return hashNumber(key);
  42. }
  43. inline u32 hashCode(unsigned int key) {
  44. return hashNumber(key);
  45. }
  46. inline u32 hashCode(unsigned long key) {
  47. return hashNumber(key);
  48. }
  49. inline u32 hashCode(unsigned long long key) {
  50. return hashNumber(key);
  51. }
  52. template<typename T>
  53. inline consteval T emptyValue() {
  54. if constexpr(Core::IsSame<char, T>) {
  55. return CHAR_MAX;
  56. } else if constexpr(Core::IsSame<signed char, T>) {
  57. return SCHAR_MAX;
  58. } else if constexpr(Core::IsSame<signed short, T>) {
  59. return SHRT_MAX;
  60. } else if constexpr(Core::IsSame<signed int, T>) {
  61. return INT_MAX;
  62. } else if constexpr(Core::IsSame<signed long, T>) {
  63. return LONG_MAX;
  64. } else if constexpr(Core::IsSame<signed long long, T>) {
  65. return LLONG_MAX;
  66. } else if constexpr(Core::IsSame<unsigned char, T>) {
  67. return UCHAR_MAX;
  68. } else if constexpr(Core::IsSame<unsigned short, T>) {
  69. return USHRT_MAX;
  70. } else if constexpr(Core::IsSame<unsigned int, T>) {
  71. return UINT_MAX;
  72. } else if constexpr(Core::IsSame<unsigned long, T>) {
  73. return ULONG_MAX;
  74. } else if constexpr(Core::IsSame<unsigned long long, T>) {
  75. return ULLONG_MAX;
  76. } else {
  77. return T::emptyValue();
  78. }
  79. }
  80. }
  81. #endif