Blame internal/ceres/collections_port.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: keir@google.com (Keir Mierle)
Packit ea1746
//
Packit ea1746
// Portable HashMap and HashSet, and a specialized overload for hashing pairs.
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_COLLECTIONS_PORT_H_
Packit ea1746
#define CERES_INTERNAL_COLLECTIONS_PORT_H_
Packit ea1746
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
Packit ea1746
#if defined(CERES_NO_UNORDERED_MAP)
Packit ea1746
#  include <map>
Packit ea1746
#  include <set>
Packit ea1746
#endif
Packit ea1746
Packit ea1746
#if defined(CERES_TR1_UNORDERED_MAP)
Packit ea1746
#  include <tr1/unordered_map>
Packit ea1746
#  include <tr1/unordered_set>
Packit ea1746
#  define CERES_HASH_NAMESPACE_START namespace std { namespace tr1 {
Packit ea1746
#  define CERES_HASH_NAMESPACE_END } }
Packit ea1746
#endif
Packit ea1746
Packit ea1746
#if defined(CERES_STD_UNORDERED_MAP)
Packit ea1746
#  include <unordered_map>
Packit ea1746
#  include <unordered_set>
Packit ea1746
#  define CERES_HASH_NAMESPACE_START namespace std {
Packit ea1746
#  define CERES_HASH_NAMESPACE_END }
Packit ea1746
#endif
Packit ea1746
Packit ea1746
#if defined(CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
Packit ea1746
#  include <unordered_map>
Packit ea1746
#  include <unordered_set>
Packit ea1746
#  define CERES_HASH_NAMESPACE_START namespace std { namespace tr1 {
Packit ea1746
#  define CERES_HASH_NAMESPACE_END } }
Packit ea1746
#endif
Packit ea1746
Packit ea1746
#if !defined(CERES_NO_UNORDERED_MAP) && !defined(CERES_TR1_UNORDERED_MAP) && \
Packit ea1746
    !defined(CERES_STD_UNORDERED_MAP) && !defined(CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)  // NOLINT
Packit ea1746
#  error One of: CERES_NO_UNORDERED_MAP, CERES_TR1_UNORDERED_MAP,\
Packit ea1746
 CERES_STD_UNORDERED_MAP, CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE must be defined!  // NOLINT
Packit ea1746
#endif
Packit ea1746
Packit ea1746
#include <utility>
Packit ea1746
#include "ceres/integral_types.h"
Packit ea1746
Packit ea1746
// Some systems don't have access to unordered_map/unordered_set. In
Packit ea1746
// that case, substitute the hash map/set with normal map/set. The
Packit ea1746
// price to pay is slower speed for some operations.
Packit ea1746
#if defined(CERES_NO_UNORDERED_MAP)
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
template<typename K, typename V>
Packit ea1746
struct HashMap : map<K, V> {};
Packit ea1746
Packit ea1746
template<typename K>
Packit ea1746
struct HashSet : set<K> {};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#else
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
#if defined(CERES_TR1_UNORDERED_MAP) || \
Packit ea1746
    defined(CERES_STD_UNORDERED_MAP_IN_TR1_NAMESPACE)
Packit ea1746
template<typename K, typename V>
Packit ea1746
struct HashMap : std::tr1::unordered_map<K, V> {};
Packit ea1746
template<typename K>
Packit ea1746
struct HashSet : std::tr1::unordered_set<K> {};
Packit ea1746
#endif
Packit ea1746
Packit ea1746
#if defined(CERES_STD_UNORDERED_MAP)
Packit ea1746
template<typename K, typename V>
Packit ea1746
struct HashMap : std::unordered_map<K, V> {};
Packit ea1746
template<typename K>
Packit ea1746
struct HashSet : std::unordered_set<K> {};
Packit ea1746
#endif
Packit ea1746
Packit ea1746
#if defined(_WIN32) && !defined(__MINGW64__) && !defined(__MINGW32__)
Packit ea1746
#define GG_LONGLONG(x) x##I64
Packit ea1746
#define GG_ULONGLONG(x) x##UI64
Packit ea1746
#else
Packit ea1746
#define GG_LONGLONG(x) x##LL
Packit ea1746
#define GG_ULONGLONG(x) x##ULL
Packit ea1746
#endif
Packit ea1746
Packit ea1746
// The hash function is due to Bob Jenkins (see
Packit ea1746
// http://burtleburtle.net/bob/hash/index.html). Each mix takes 36 instructions,
Packit ea1746
// in 18 cycles if you're lucky. On x86 architectures, this requires 45
Packit ea1746
// instructions in 27 cycles, if you're lucky.
Packit ea1746
//
Packit ea1746
// 32bit version
Packit ea1746
inline void hash_mix(uint32& a, uint32& b, uint32& c) {
Packit ea1746
  a -= b; a -= c; a ^= (c>>13);
Packit ea1746
  b -= c; b -= a; b ^= (a<<8);
Packit ea1746
  c -= a; c -= b; c ^= (b>>13);
Packit ea1746
  a -= b; a -= c; a ^= (c>>12);
Packit ea1746
  b -= c; b -= a; b ^= (a<<16);
Packit ea1746
  c -= a; c -= b; c ^= (b>>5);
Packit ea1746
  a -= b; a -= c; a ^= (c>>3);
Packit ea1746
  b -= c; b -= a; b ^= (a<<10);
Packit ea1746
  c -= a; c -= b; c ^= (b>>15);
Packit ea1746
}
Packit ea1746
Packit ea1746
// 64bit version
Packit ea1746
inline void hash_mix(uint64& a, uint64& b, uint64& c) {
Packit ea1746
  a -= b; a -= c; a ^= (c>>43);
Packit ea1746
  b -= c; b -= a; b ^= (a<<9);
Packit ea1746
  c -= a; c -= b; c ^= (b>>8);
Packit ea1746
  a -= b; a -= c; a ^= (c>>38);
Packit ea1746
  b -= c; b -= a; b ^= (a<<23);
Packit ea1746
  c -= a; c -= b; c ^= (b>>5);
Packit ea1746
  a -= b; a -= c; a ^= (c>>35);
Packit ea1746
  b -= c; b -= a; b ^= (a<<49);
Packit ea1746
  c -= a; c -= b; c ^= (b>>11);
Packit ea1746
}
Packit ea1746
Packit ea1746
inline uint32 Hash32NumWithSeed(uint32 num, uint32 c) {
Packit ea1746
  // The golden ratio; an arbitrary value.
Packit ea1746
  uint32 b = 0x9e3779b9UL;
Packit ea1746
  hash_mix(num, b, c);
Packit ea1746
  return c;
Packit ea1746
}
Packit ea1746
Packit ea1746
inline uint64 Hash64NumWithSeed(uint64 num, uint64 c) {
Packit ea1746
  // More of the golden ratio.
Packit ea1746
  uint64 b = GG_ULONGLONG(0xe08c1d668b756f82);
Packit ea1746
  hash_mix(num, b, c);
Packit ea1746
  return c;
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
// Since on some platforms this is a doubly-nested namespace (std::tr1) and
Packit ea1746
// others it is not, the entire namespace line must be in a macro.
Packit ea1746
CERES_HASH_NAMESPACE_START
Packit ea1746
Packit ea1746
// The outrageously annoying specializations below are for portability reasons.
Packit ea1746
// In short, it's not possible to have two overloads of hash<pair<T1, T2>
Packit ea1746
Packit ea1746
// Hasher for STL pairs. Requires hashers for both members to be defined.
Packit ea1746
template<typename T>
Packit ea1746
struct hash<pair<T, T> > {
Packit ea1746
  size_t operator()(const pair<T, T>& p) const {
Packit ea1746
    size_t h1 = hash<T>()(p.first);
Packit ea1746
    size_t h2 = hash<T>()(p.second);
Packit ea1746
    // The decision below is at compile time
Packit ea1746
    return (sizeof(h1) <= sizeof(ceres::internal::uint32)) ?
Packit ea1746
            ceres::internal::Hash32NumWithSeed(h1, h2) :
Packit ea1746
            ceres::internal::Hash64NumWithSeed(h1, h2);
Packit ea1746
  }
Packit ea1746
  // Less than operator for MSVC.
Packit ea1746
  bool operator()(const pair<T, T>& a,
Packit ea1746
                  const pair<T, T>& b) const {
Packit ea1746
    return a < b;
Packit ea1746
  }
Packit ea1746
  static const size_t bucket_size = 4;  // These are required by MSVC
Packit ea1746
  static const size_t min_buckets = 8;  // 4 and 8 are defaults.
Packit ea1746
};
Packit ea1746
Packit ea1746
CERES_HASH_NAMESPACE_END
Packit ea1746
Packit ea1746
#endif  // CERES_NO_UNORDERED_MAP
Packit ea1746
#endif  // CERES_INTERNAL_COLLECTIONS_PORT_H_