HashUtility.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. namespace UnityEngine.Timeline
  2. {
  3. static class HashUtility
  4. {
  5. // Note. We could have used "params int[] hashes" but we want to avoid allocating.
  6. public static int CombineHash(this int h1, int h2)
  7. {
  8. return h1 ^ (int)(h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2)); // Similar to c++ boost::hash_combine
  9. }
  10. public static int CombineHash(int h1, int h2, int h3)
  11. {
  12. return CombineHash(h1, h2).CombineHash(h3);
  13. }
  14. public static int CombineHash(int h1, int h2, int h3, int h4)
  15. {
  16. return CombineHash(h1, h2, h3).CombineHash(h4);
  17. }
  18. public static int CombineHash(int h1, int h2, int h3, int h4, int h5)
  19. {
  20. return CombineHash(h1, h2, h3, h4).CombineHash(h5);
  21. }
  22. public static int CombineHash(int h1, int h2, int h3, int h4, int h5, int h6)
  23. {
  24. return CombineHash(h1, h2, h3, h4, h5).CombineHash(h6);
  25. }
  26. public static int CombineHash(int h1, int h2, int h3, int h4, int h5, int h6, int h7)
  27. {
  28. return CombineHash(h1, h2, h3, h4, h5, h6).CombineHash(h7);
  29. }
  30. public static int CombineHash(int[] hashes)
  31. {
  32. if (hashes == null || hashes.Length == 0)
  33. return 0;
  34. var h = hashes[0];
  35. for (int i = 1; i < hashes.Length; ++i)
  36. {
  37. h = CombineHash(h, hashes[i]);
  38. }
  39. return h;
  40. }
  41. }
  42. }