You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

318 lines
10 KiB

  1. // ----------------------------------------------------------------------
  2. // Copyright (c) 2016, Steven Gregory Popovitch - greg7mdp@gmail.com
  3. // All rights reserved.
  4. //
  5. // Code derived derived from Boost libraries.
  6. // Boost software licence reproduced below.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * The name of Steven Gregory Popovitch may not be used to
  19. // endorse or promote products derived from this software without
  20. // specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. // ----------------------------------------------------------------------
  34. // ---------------------------------------------------------------------------
  35. // Boost Software License - Version 1.0 - August 17th, 2003
  36. //
  37. // Permission is hereby granted, free of charge, to any person or organization
  38. // obtaining a copy of the software and accompanying documentation covered by
  39. // this license (the "Software") to use, reproduce, display, distribute,
  40. // execute, and transmit the Software, and to prepare derivative works of the
  41. // Software, and to permit third-parties to whom the Software is furnished to
  42. // do so, all subject to the following:
  43. //
  44. // The copyright notices in the Software and this entire statement, including
  45. // the above license grant, this restriction and the following disclaimer,
  46. // must be included in all copies of the Software, in whole or in part, and
  47. // all derivative works of the Software, unless such copies or derivative
  48. // works are solely in the form of machine-executable object code generated by
  49. // a source language processor.
  50. //
  51. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  52. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  53. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  54. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  55. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  56. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  57. // DEALINGS IN THE SOFTWARE.
  58. // ---------------------------------------------------------------------------
  59. // ----------------------------------------------------------------------
  60. // H A S H F U N C T I O N S
  61. // ----------------------------
  62. //
  63. // Implements spp::spp_hash() and spp::hash_combine()
  64. //
  65. // The exact same code is duplicated in sparsepp.h.
  66. //
  67. // WARNING: Any change here has to be duplicated in sparsepp.h.
  68. // ----------------------------------------------------------------------
  69. #if !defined(spp_utils_h_guard_)
  70. #define spp_utils_h_guard_
  71. #if defined(_MSC_VER)
  72. #if (_MSC_VER >= 1600 ) // vs2010 (1900 is vs2015)
  73. #include <functional>
  74. #define SPP_HASH_CLASS std::hash
  75. #else
  76. #include <hash_map>
  77. #define SPP_HASH_CLASS stdext::hash_compare
  78. #endif
  79. #if (_MSC_FULL_VER < 190021730)
  80. #define SPP_NO_CXX11_NOEXCEPT
  81. #endif
  82. #elif defined(__GNUC__)
  83. #if defined(__GXX_EXPERIMENTAL_CXX0X__) || (__cplusplus >= 201103L)
  84. #include <functional>
  85. #define SPP_HASH_CLASS std::hash
  86. #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) < 40600
  87. #define SPP_NO_CXX11_NOEXCEPT
  88. #endif
  89. #else
  90. #include <tr1/unordered_map>
  91. #define SPP_HASH_CLASS std::tr1::hash
  92. #define SPP_NO_CXX11_NOEXCEPT
  93. #endif
  94. #elif defined __clang__
  95. #include <functional>
  96. #define SPP_HASH_CLASS std::hash
  97. #if !__has_feature(cxx_noexcept)
  98. #define SPP_NO_CXX11_NOEXCEPT
  99. #endif
  100. #else
  101. #include <functional>
  102. #define SPP_HASH_CLASS std::hash
  103. #endif
  104. #ifdef SPP_NO_CXX11_NOEXCEPT
  105. #define SPP_NOEXCEPT
  106. #else
  107. #define SPP_NOEXCEPT noexcept
  108. #endif
  109. #ifdef SPP_NO_CXX11_CONSTEXPR
  110. #define SPP_CONSTEXPR
  111. #else
  112. #define SPP_CONSTEXPR constexpr
  113. #endif
  114. #define SPP_INLINE
  115. #ifndef SPP_NAMESPACE
  116. #define SPP_NAMESPACE spp
  117. #endif
  118. namespace SPP_NAMESPACE
  119. {
  120. template <class T>
  121. struct spp_hash
  122. {
  123. SPP_INLINE size_t operator()(const T &__v) const SPP_NOEXCEPT
  124. {
  125. SPP_HASH_CLASS<T> hasher;
  126. return hasher(__v);
  127. }
  128. };
  129. template <class T>
  130. struct spp_hash<T *>
  131. {
  132. static size_t spp_log2 (size_t val) SPP_NOEXCEPT
  133. {
  134. size_t res = 0;
  135. while (val > 1)
  136. {
  137. val >>= 1;
  138. res++;
  139. }
  140. return res;
  141. }
  142. SPP_INLINE size_t operator()(const T *__v) const SPP_NOEXCEPT
  143. {
  144. static const size_t shift = 3; // spp_log2(1 + sizeof(T)); // T might be incomplete!
  145. return static_cast<size_t>((*(reinterpret_cast<const uintptr_t *>(&__v))) >> shift);
  146. }
  147. };
  148. // from http://burtleburtle.net/bob/hash/integer.html
  149. // fast and efficient for power of two table sizes where we always
  150. // consider the last bits.
  151. // ---------------------------------------------------------------
  152. inline size_t spp_mix_32(uint32_t a)
  153. {
  154. a = a ^ (a >> 4);
  155. a = (a ^ 0xdeadbeef) + (a << 5);
  156. a = a ^ (a >> 11);
  157. return static_cast<size_t>(a);
  158. }
  159. // Maybe we should do a more thorough scrambling as described in
  160. // https://gist.github.com/badboy/6267743
  161. // -------------------------------------------------------------
  162. inline size_t spp_mix_64(uint64_t a)
  163. {
  164. a = a ^ (a >> 4);
  165. a = (a ^ 0xdeadbeef) + (a << 5);
  166. a = a ^ (a >> 11);
  167. return a;
  168. }
  169. template <>
  170. struct spp_hash<bool> : public std::unary_function<bool, size_t>
  171. {
  172. SPP_INLINE size_t operator()(bool __v) const SPP_NOEXCEPT
  173. { return static_cast<size_t>(__v); }
  174. };
  175. template <>
  176. struct spp_hash<char> : public std::unary_function<char, size_t>
  177. {
  178. SPP_INLINE size_t operator()(char __v) const SPP_NOEXCEPT
  179. { return static_cast<size_t>(__v); }
  180. };
  181. template <>
  182. struct spp_hash<signed char> : public std::unary_function<signed char, size_t>
  183. {
  184. SPP_INLINE size_t operator()(signed char __v) const SPP_NOEXCEPT
  185. { return static_cast<size_t>(__v); }
  186. };
  187. template <>
  188. struct spp_hash<unsigned char> : public std::unary_function<unsigned char, size_t>
  189. {
  190. SPP_INLINE size_t operator()(unsigned char __v) const SPP_NOEXCEPT
  191. { return static_cast<size_t>(__v); }
  192. };
  193. template <>
  194. struct spp_hash<wchar_t> : public std::unary_function<wchar_t, size_t>
  195. {
  196. SPP_INLINE size_t operator()(wchar_t __v) const SPP_NOEXCEPT
  197. { return static_cast<size_t>(__v); }
  198. };
  199. template <>
  200. struct spp_hash<int16_t> : public std::unary_function<int16_t, size_t>
  201. {
  202. SPP_INLINE size_t operator()(int16_t __v) const SPP_NOEXCEPT
  203. { return spp_mix_32(static_cast<uint32_t>(__v)); }
  204. };
  205. template <>
  206. struct spp_hash<uint16_t> : public std::unary_function<uint16_t, size_t>
  207. {
  208. SPP_INLINE size_t operator()(uint16_t __v) const SPP_NOEXCEPT
  209. { return spp_mix_32(static_cast<uint32_t>(__v)); }
  210. };
  211. template <>
  212. struct spp_hash<int32_t> : public std::unary_function<int32_t, size_t>
  213. {
  214. SPP_INLINE size_t operator()(int32_t __v) const SPP_NOEXCEPT
  215. { return spp_mix_32(static_cast<uint32_t>(__v)); }
  216. };
  217. template <>
  218. struct spp_hash<uint32_t> : public std::unary_function<uint32_t, size_t>
  219. {
  220. SPP_INLINE size_t operator()(uint32_t __v) const SPP_NOEXCEPT
  221. { return spp_mix_32(static_cast<uint32_t>(__v)); }
  222. };
  223. template <>
  224. struct spp_hash<int64_t> : public std::unary_function<int64_t, size_t>
  225. {
  226. SPP_INLINE size_t operator()(int64_t __v) const SPP_NOEXCEPT
  227. { return spp_mix_64(static_cast<uint64_t>(__v)); }
  228. };
  229. template <>
  230. struct spp_hash<uint64_t> : public std::unary_function<uint64_t, size_t>
  231. {
  232. SPP_INLINE size_t operator()(uint64_t __v) const SPP_NOEXCEPT
  233. { return spp_mix_64(static_cast<uint64_t>(__v)); }
  234. };
  235. template <>
  236. struct spp_hash<float> : public std::unary_function<float, size_t>
  237. {
  238. SPP_INLINE size_t operator()(float __v) const SPP_NOEXCEPT
  239. {
  240. // -0.0 and 0.0 should return same hash
  241. uint32_t *as_int = reinterpret_cast<uint32_t *>(&__v);
  242. return (__v == 0) ? static_cast<size_t>(0) : spp_mix_32(*as_int);
  243. }
  244. };
  245. template <>
  246. struct spp_hash<double> : public std::unary_function<double, size_t>
  247. {
  248. SPP_INLINE size_t operator()(double __v) const SPP_NOEXCEPT
  249. {
  250. // -0.0 and 0.0 should return same hash
  251. uint64_t *as_int = reinterpret_cast<uint64_t *>(&__v);
  252. return (__v == 0) ? static_cast<size_t>(0) : spp_mix_64(*as_int);
  253. }
  254. };
  255. template <class T, int sz> struct Combiner
  256. {
  257. inline void operator()(T& seed, T value);
  258. };
  259. template <class T> struct Combiner<T, 4>
  260. {
  261. inline void operator()(T& seed, T value)
  262. {
  263. seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  264. }
  265. };
  266. template <class T> struct Combiner<T, 8>
  267. {
  268. inline void operator()(T& seed, T value)
  269. {
  270. seed ^= value + T(0xc6a4a7935bd1e995) + (seed << 6) + (seed >> 2);
  271. }
  272. };
  273. template <class T>
  274. inline void hash_combine(std::size_t& seed, T const& v)
  275. {
  276. spp::spp_hash<T> hasher;
  277. Combiner<std::size_t, sizeof(std::size_t)> combiner;
  278. combiner(seed, hasher(v));
  279. }
  280. }
  281. #endif // spp_utils_h_guard_