hash.inl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /// @ref gtx_hash
  2. ///
  3. /// @see core (dependence)
  4. ///
  5. /// @defgroup gtx_hash GLM_GTX_hash
  6. /// @ingroup gtx
  7. ///
  8. /// @brief Add std::hash support for glm types
  9. ///
  10. /// <glm/gtx/hash.inl> need to be included to use the features of this extension.
  11. namespace glm {
  12. namespace detail
  13. {
  14. GLM_INLINE void hash_combine(size_t &seed, size_t hash)
  15. {
  16. hash += 0x9e3779b9 + (seed << 6) + (seed >> 2);
  17. seed ^= hash;
  18. }
  19. }}
  20. namespace std
  21. {
  22. template<typename T, glm::qualifier Q>
  23. GLM_FUNC_QUALIFIER size_t hash<glm::vec<1, T, Q>>::operator()(glm::vec<1, T, Q> const& v) const
  24. {
  25. hash<T> hasher;
  26. return hasher(v.x);
  27. }
  28. template<typename T, glm::qualifier Q>
  29. GLM_FUNC_QUALIFIER size_t hash<glm::vec<2, T, Q>>::operator()(glm::vec<2, T, Q> const& v) const
  30. {
  31. size_t seed = 0;
  32. hash<T> hasher;
  33. glm::detail::hash_combine(seed, hasher(v.x));
  34. glm::detail::hash_combine(seed, hasher(v.y));
  35. return seed;
  36. }
  37. template<typename T, glm::qualifier Q>
  38. GLM_FUNC_QUALIFIER size_t hash<glm::vec<3, T, Q>>::operator()(glm::vec<3, T, Q> const& v) const
  39. {
  40. size_t seed = 0;
  41. hash<T> hasher;
  42. glm::detail::hash_combine(seed, hasher(v.x));
  43. glm::detail::hash_combine(seed, hasher(v.y));
  44. glm::detail::hash_combine(seed, hasher(v.z));
  45. return seed;
  46. }
  47. template<typename T, glm::qualifier Q>
  48. GLM_FUNC_QUALIFIER size_t hash<glm::vec<4, T, Q>>::operator()(glm::vec<4, T, Q> const& v) const
  49. {
  50. size_t seed = 0;
  51. hash<T> hasher;
  52. glm::detail::hash_combine(seed, hasher(v.x));
  53. glm::detail::hash_combine(seed, hasher(v.y));
  54. glm::detail::hash_combine(seed, hasher(v.z));
  55. glm::detail::hash_combine(seed, hasher(v.w));
  56. return seed;
  57. }
  58. template<typename T, glm::qualifier Q>
  59. GLM_FUNC_QUALIFIER size_t hash<glm::tquat<T, Q>>::operator()(glm::tquat<T,Q> const& q) const
  60. {
  61. size_t seed = 0;
  62. hash<T> hasher;
  63. glm::detail::hash_combine(seed, hasher(q.x));
  64. glm::detail::hash_combine(seed, hasher(q.y));
  65. glm::detail::hash_combine(seed, hasher(q.z));
  66. glm::detail::hash_combine(seed, hasher(q.w));
  67. return seed;
  68. }
  69. template<typename T, glm::qualifier Q>
  70. GLM_FUNC_QUALIFIER size_t hash<glm::tdualquat<T, Q>>::operator()(glm::tdualquat<T, Q> const& q) const
  71. {
  72. size_t seed = 0;
  73. hash<glm::tquat<T, Q>> hasher;
  74. glm::detail::hash_combine(seed, hasher(q.real));
  75. glm::detail::hash_combine(seed, hasher(q.dual));
  76. return seed;
  77. }
  78. template<typename T, glm::qualifier Q>
  79. GLM_FUNC_QUALIFIER size_t hash<glm::mat<2, 2, T, Q>>::operator()(glm::mat<2, 2, T, Q> const& m) const
  80. {
  81. size_t seed = 0;
  82. hash<glm::vec<2, T, Q>> hasher;
  83. glm::detail::hash_combine(seed, hasher(m[0]));
  84. glm::detail::hash_combine(seed, hasher(m[1]));
  85. return seed;
  86. }
  87. template<typename T, glm::qualifier Q>
  88. GLM_FUNC_QUALIFIER size_t hash<glm::mat<2, 3, T, Q>>::operator()(glm::mat<2, 3, T, Q> const& m) const
  89. {
  90. size_t seed = 0;
  91. hash<glm::vec<3, T, Q>> hasher;
  92. glm::detail::hash_combine(seed, hasher(m[0]));
  93. glm::detail::hash_combine(seed, hasher(m[1]));
  94. return seed;
  95. }
  96. template<typename T, glm::qualifier Q>
  97. GLM_FUNC_QUALIFIER size_t hash<glm::mat<2, 4, T, Q>>::operator()(glm::mat<2, 4, T, Q> const& m) const
  98. {
  99. size_t seed = 0;
  100. hash<glm::vec<4, T, Q>> hasher;
  101. glm::detail::hash_combine(seed, hasher(m[0]));
  102. glm::detail::hash_combine(seed, hasher(m[1]));
  103. return seed;
  104. }
  105. template<typename T, glm::qualifier Q>
  106. GLM_FUNC_QUALIFIER size_t hash<glm::mat<3, 2, T, Q>>::operator()(glm::mat<3, 2, T, Q> const& m) const
  107. {
  108. size_t seed = 0;
  109. hash<glm::vec<2, T, Q>> hasher;
  110. glm::detail::hash_combine(seed, hasher(m[0]));
  111. glm::detail::hash_combine(seed, hasher(m[1]));
  112. glm::detail::hash_combine(seed, hasher(m[2]));
  113. return seed;
  114. }
  115. template<typename T, glm::qualifier Q>
  116. GLM_FUNC_QUALIFIER size_t hash<glm::mat<3, 3, T, Q>>::operator()(glm::mat<3, 3, T, Q> const& m) const
  117. {
  118. size_t seed = 0;
  119. hash<glm::vec<3, T, Q>> hasher;
  120. glm::detail::hash_combine(seed, hasher(m[0]));
  121. glm::detail::hash_combine(seed, hasher(m[1]));
  122. glm::detail::hash_combine(seed, hasher(m[2]));
  123. return seed;
  124. }
  125. template<typename T, glm::qualifier Q>
  126. GLM_FUNC_QUALIFIER size_t hash<glm::mat<3, 4, T, Q>>::operator()(glm::mat<3, 4, T, Q> const& m) const
  127. {
  128. size_t seed = 0;
  129. hash<glm::vec<4, T, Q>> hasher;
  130. glm::detail::hash_combine(seed, hasher(m[0]));
  131. glm::detail::hash_combine(seed, hasher(m[1]));
  132. glm::detail::hash_combine(seed, hasher(m[2]));
  133. return seed;
  134. }
  135. template<typename T, glm::qualifier Q>
  136. GLM_FUNC_QUALIFIER size_t hash<glm::mat<4, 2, T,Q>>::operator()(glm::mat<4, 2, T,Q> const& m) const
  137. {
  138. size_t seed = 0;
  139. hash<glm::vec<2, T, Q>> hasher;
  140. glm::detail::hash_combine(seed, hasher(m[0]));
  141. glm::detail::hash_combine(seed, hasher(m[1]));
  142. glm::detail::hash_combine(seed, hasher(m[2]));
  143. glm::detail::hash_combine(seed, hasher(m[3]));
  144. return seed;
  145. }
  146. template<typename T, glm::qualifier Q>
  147. GLM_FUNC_QUALIFIER size_t hash<glm::mat<4, 3, T,Q>>::operator()(glm::mat<4, 3, T,Q> const& m) const
  148. {
  149. size_t seed = 0;
  150. hash<glm::vec<3, T, Q>> hasher;
  151. glm::detail::hash_combine(seed, hasher(m[0]));
  152. glm::detail::hash_combine(seed, hasher(m[1]));
  153. glm::detail::hash_combine(seed, hasher(m[2]));
  154. glm::detail::hash_combine(seed, hasher(m[3]));
  155. return seed;
  156. }
  157. template<typename T, glm::qualifier Q>
  158. GLM_FUNC_QUALIFIER size_t hash<glm::mat<4, 4, T,Q>>::operator()(glm::mat<4, 4, T, Q> const& m) const
  159. {
  160. size_t seed = 0;
  161. hash<glm::vec<4, T, Q>> hasher;
  162. glm::detail::hash_combine(seed, hasher(m[0]));
  163. glm::detail::hash_combine(seed, hasher(m[1]));
  164. glm::detail::hash_combine(seed, hasher(m[2]));
  165. glm::detail::hash_combine(seed, hasher(m[3]));
  166. return seed;
  167. }
  168. }