~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to internal/ceres/collections_port.h

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Ceres Solver - A fast non-linear least squares minimizer
 
2
// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
 
3
// http://code.google.com/p/ceres-solver/
 
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
// Portable HashMap and HashSet, and a specialized overload for hashing pairs.
 
32
 
 
33
#ifndef CERES_INTERNAL_COLLECTIONS_PORT_H_
 
34
#define CERES_INTERNAL_COLLECTIONS_PORT_H_
 
35
 
 
36
#include <tr1/unordered_map>
 
37
#include <tr1/unordered_set>
 
38
#include <utility>
 
39
#include "ceres/integral_types.h"
 
40
#include "ceres/internal/port.h"
 
41
 
 
42
namespace ceres {
 
43
namespace internal {
 
44
 
 
45
template<typename K, typename V>
 
46
struct HashMap : tr1::unordered_map<K, V> {};
 
47
 
 
48
template<typename K>
 
49
struct HashSet : tr1::unordered_set<K> {};
 
50
 
 
51
#ifdef _WIN32
 
52
#define GG_LONGLONG(x) x##I64
 
53
#define GG_ULONGLONG(x) x##UI64
 
54
#else
 
55
#define GG_LONGLONG(x) x##LL
 
56
#define GG_ULONGLONG(x) x##ULL
 
57
#endif
 
58
 
 
59
// The hash function is due to Bob Jenkins (see
 
60
// http://burtleburtle.net/bob/hash/index.html). Each mix takes 36 instructions,
 
61
// in 18 cycles if you're lucky. On x86 architectures, this requires 45
 
62
// instructions in 27 cycles, if you're lucky.
 
63
//
 
64
// 32bit version
 
65
inline void hash_mix(uint32& a, uint32& b, uint32& c) {
 
66
  a -= b; a -= c; a ^= (c>>13);
 
67
  b -= c; b -= a; b ^= (a<<8);
 
68
  c -= a; c -= b; c ^= (b>>13);
 
69
  a -= b; a -= c; a ^= (c>>12);
 
70
  b -= c; b -= a; b ^= (a<<16);
 
71
  c -= a; c -= b; c ^= (b>>5);
 
72
  a -= b; a -= c; a ^= (c>>3);
 
73
  b -= c; b -= a; b ^= (a<<10);
 
74
  c -= a; c -= b; c ^= (b>>15);
 
75
}
 
76
 
 
77
// 64bit version
 
78
inline void hash_mix(uint64& a, uint64& b, uint64& c) {
 
79
  a -= b; a -= c; a ^= (c>>43);
 
80
  b -= c; b -= a; b ^= (a<<9);
 
81
  c -= a; c -= b; c ^= (b>>8);
 
82
  a -= b; a -= c; a ^= (c>>38);
 
83
  b -= c; b -= a; b ^= (a<<23);
 
84
  c -= a; c -= b; c ^= (b>>5);
 
85
  a -= b; a -= c; a ^= (c>>35);
 
86
  b -= c; b -= a; b ^= (a<<49);
 
87
  c -= a; c -= b; c ^= (b>>11);
 
88
}
 
89
 
 
90
inline uint32 Hash32NumWithSeed(uint32 num, uint32 c) {
 
91
  // The golden ratio; an arbitrary value.
 
92
  uint32 b = 0x9e3779b9UL;
 
93
  hash_mix(num, b, c);
 
94
  return c;
 
95
}
 
96
 
 
97
inline uint64 Hash64NumWithSeed(uint64 num, uint64 c) {
 
98
  // More of the golden ratio.
 
99
  uint64 b = GG_ULONGLONG(0xe08c1d668b756f82);
 
100
  hash_mix(num, b, c);
 
101
  return c;
 
102
}
 
103
 
 
104
}  // namespace internal
 
105
}  // namespace ceres
 
106
 
 
107
// Since on some platforms this is a doubly-nested namespace (std::tr1) and
 
108
// others it is not, the entire namespace line must be in a macro.
 
109
CERES_HASH_NAMESPACE_START
 
110
 
 
111
// The outrageously annoying specializations below are for portability reasons.
 
112
// In short, it's not possible to have two overloads of hash<pair<T1, T2>
 
113
 
 
114
// Hasher for STL pairs. Requires hashers for both members to be defined.
 
115
template<typename T>
 
116
struct hash<pair<T, T> > {
 
117
  size_t operator()(const pair<T, T>& p) const {
 
118
    size_t h1 = hash<T>()(p.first);
 
119
    size_t h2 = hash<T>()(p.second);
 
120
    // The decision below is at compile time
 
121
    return (sizeof(h1) <= sizeof(ceres::internal::uint32)) ?
 
122
            ceres::internal::Hash32NumWithSeed(h1, h2) :
 
123
            ceres::internal::Hash64NumWithSeed(h1, h2);
 
124
  }
 
125
  // Less than operator for MSVC.
 
126
  bool operator()(const pair<T, T>& a,
 
127
                  const pair<T, T>& b) const {
 
128
    return a < b;
 
129
  }
 
130
  static const size_t bucket_size = 4;  // These are required by MSVC
 
131
  static const size_t min_buckets = 8;  // 4 and 8 are defaults.
 
132
};
 
133
 
 
134
CERES_HASH_NAMESPACE_END
 
135
 
 
136
#endif  // CERES_INTERNAL_COLLECTIONS_PORT_H_