pair_hash.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: keir@google.com (Keir Mierle)
  30. //
  31. // A hasher for std::pair<T, T>.
  32. #ifndef CERES_INTERNAL_PAIR_HASH_H_
  33. #define CERES_INTERNAL_PAIR_HASH_H_
  34. #include <cstddef>
  35. #include <cstdint>
  36. #include <functional>
  37. #include <utility>
  38. #include "ceres/internal/export.h"
  39. namespace ceres::internal {
  40. #if defined(_WIN32) && !defined(__MINGW64__) && !defined(__MINGW32__)
  41. #define GG_LONGLONG(x) x##I64
  42. #define GG_ULONGLONG(x) x##UI64
  43. #else
  44. #define GG_LONGLONG(x) x##LL
  45. #define GG_ULONGLONG(x) x##ULL
  46. #endif
  47. // The hash function is due to Bob Jenkins (see
  48. // http://burtleburtle.net/bob/hash/index.html). Each mix takes 36 instructions,
  49. // in 18 cycles if you're lucky. On x86 architectures, this requires 45
  50. // instructions in 27 cycles, if you're lucky.
  51. //
  52. // clang-format off
  53. //
  54. // 32bit version
  55. inline void hash_mix(uint32_t& a, uint32_t& b, uint32_t& c) {
  56. a -= b; a -= c; a ^= (c>>13);
  57. b -= c; b -= a; b ^= (a<<8);
  58. c -= a; c -= b; c ^= (b>>13);
  59. a -= b; a -= c; a ^= (c>>12);
  60. b -= c; b -= a; b ^= (a<<16);
  61. c -= a; c -= b; c ^= (b>>5);
  62. a -= b; a -= c; a ^= (c>>3);
  63. b -= c; b -= a; b ^= (a<<10);
  64. c -= a; c -= b; c ^= (b>>15);
  65. }
  66. // 64bit version
  67. inline void hash_mix(uint64_t& a, uint64_t& b, uint64_t& c) {
  68. a -= b; a -= c; a ^= (c>>43);
  69. b -= c; b -= a; b ^= (a<<9);
  70. c -= a; c -= b; c ^= (b>>8);
  71. a -= b; a -= c; a ^= (c>>38);
  72. b -= c; b -= a; b ^= (a<<23);
  73. c -= a; c -= b; c ^= (b>>5);
  74. a -= b; a -= c; a ^= (c>>35);
  75. b -= c; b -= a; b ^= (a<<49);
  76. c -= a; c -= b; c ^= (b>>11);
  77. }
  78. // clang-format on
  79. inline uint32_t Hash32NumWithSeed(uint32_t num, uint32_t c) {
  80. // The golden ratio; an arbitrary value.
  81. uint32_t b = 0x9e3779b9UL;
  82. hash_mix(num, b, c);
  83. return c;
  84. }
  85. inline uint64_t Hash64NumWithSeed(uint64_t num, uint64_t c) {
  86. // More of the golden ratio.
  87. uint64_t b = GG_ULONGLONG(0xe08c1d668b756f82);
  88. hash_mix(num, b, c);
  89. return c;
  90. }
  91. // Hasher for STL pairs. Requires hashers for both members to be defined.
  92. struct pair_hash {
  93. public:
  94. template <typename T>
  95. std::size_t operator()(const std::pair<T, T>& p) const {
  96. const std::size_t h1 = std::hash<T>()(p.first);
  97. const std::size_t h2 = std::hash<T>()(p.second);
  98. // The decision below is at compile time
  99. return (sizeof(h1) <= sizeof(uint32_t)) ? Hash32NumWithSeed(h1, h2)
  100. : Hash64NumWithSeed(h1, h2);
  101. }
  102. };
  103. } // namespace ceres::internal
  104. #endif // CERES_INTERNAL_PAIR_HASH_H_