HashCode.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "utils/HashCode.h"
  2. #include "utils/Utility.h"
  3. template<typename T>
  4. static u32 hashNumber(T t) {
  5. constexpr u32 L = sizeof(T) / 4 + (sizeof(T) % 4 != 0);
  6. u32 parts[L];
  7. Core::memorySet(parts, 0, sizeof(parts));
  8. Core::memoryCopy(parts, &t, sizeof(T));
  9. u32 hash = 0;
  10. for(u32 i = 0; i < L; i++) {
  11. hash ^= parts[i];
  12. }
  13. return hash;
  14. }
  15. u32 Core::hashCode(char key) {
  16. return hashNumber(key);
  17. }
  18. u32 Core::hashCode(signed char key) {
  19. return hashNumber(key);
  20. }
  21. u32 Core::hashCode(signed short key) {
  22. return hashNumber(key);
  23. }
  24. u32 Core::hashCode(signed int key) {
  25. return hashNumber(key);
  26. }
  27. u32 Core::hashCode(signed long key) {
  28. return hashNumber(key);
  29. }
  30. u32 Core::hashCode(signed long long key) {
  31. return hashNumber(key);
  32. }
  33. u32 Core::hashCode(unsigned char key) {
  34. return hashNumber(key);
  35. }
  36. u32 Core::hashCode(unsigned short key) {
  37. return hashNumber(key);
  38. }
  39. u32 Core::hashCode(unsigned int key) {
  40. return hashNumber(key);
  41. }
  42. u32 Core::hashCode(unsigned long key) {
  43. return hashNumber(key);
  44. }
  45. u32 Core::hashCode(unsigned long long key) {
  46. return hashNumber(key);
  47. }