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

« back to all changes in this revision

Viewing changes to internal/ceres/map_util.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
// Originally by Anton Carver
 
32
 
 
33
#ifndef CERES_INTERNAL_MAP_UTIL_H_
 
34
#define CERES_INTERNAL_MAP_UTIL_H_
 
35
 
 
36
#include <utility>
 
37
#include "ceres/internal/port.h"
 
38
 
 
39
namespace ceres {
 
40
 
 
41
// Perform a lookup in a map or hash_map, assuming that the key exists.
 
42
// Crash if it does not.
 
43
//
 
44
// This is intended as a replacement for operator[] as an rvalue (for reading)
 
45
// when the key is guaranteed to exist.
 
46
//
 
47
// operator[] is discouraged for several reasons:
 
48
//  * It has a side-effect of inserting missing keys
 
49
//  * It is not thread-safe (even when it is not inserting, it can still
 
50
//      choose to resize the underlying storage)
 
51
//  * It invalidates iterators (when it chooses to resize)
 
52
//  * It default constructs a value object even if it doesn't need to
 
53
//
 
54
// This version assumes the key is printable, and includes it in the fatal log
 
55
// message.
 
56
template <class Collection>
 
57
const typename Collection::value_type::second_type&
 
58
FindOrDie(const Collection& collection,
 
59
          const typename Collection::value_type::first_type& key) {
 
60
  typename Collection::const_iterator it = collection.find(key);
 
61
  CHECK(it != collection.end()) << "Map key not found: " << key;
 
62
  return it->second;
 
63
}
 
64
 
 
65
// Perform a lookup in a map or hash_map.
 
66
// If the key is present in the map then the value associated with that
 
67
// key is returned, otherwise the value passed as a default is returned.
 
68
template <class Collection>
 
69
const typename Collection::value_type::second_type&
 
70
FindWithDefault(const Collection& collection,
 
71
                const typename Collection::value_type::first_type& key,
 
72
                const typename Collection::value_type::second_type& value) {
 
73
  typename Collection::const_iterator it = collection.find(key);
 
74
  if (it == collection.end()) {
 
75
    return value;
 
76
  }
 
77
  return it->second;
 
78
}
 
79
 
 
80
// Insert a new key and value into a map or hash_map.
 
81
// If the key is not present in the map the key and value are
 
82
// inserted, otherwise nothing happens. True indicates that an insert
 
83
// took place, false indicates the key was already present.
 
84
template <class Collection>
 
85
bool InsertIfNotPresent(
 
86
    Collection * const collection,
 
87
    const typename Collection::value_type::first_type& key,
 
88
    const typename Collection::value_type::second_type& value) {
 
89
  pair<typename Collection::iterator, bool> ret =
 
90
    collection->insert(typename Collection::value_type(key, value));
 
91
  return ret.second;
 
92
}
 
93
 
 
94
// Perform a lookup in a map or hash_map.
 
95
// Same as above but the returned pointer is not const and can be used to change
 
96
// the stored value.
 
97
template <class Collection>
 
98
typename Collection::value_type::second_type*
 
99
FindOrNull(Collection& collection,  // NOLINT
 
100
           const typename Collection::value_type::first_type& key) {
 
101
  typename Collection::iterator it = collection.find(key);
 
102
  if (it == collection.end()) {
 
103
    return 0;
 
104
  }
 
105
  return &it->second;
 
106
}
 
107
 
 
108
// Test to see if a set, map, hash_set or hash_map contains a particular key.
 
109
// Returns true if the key is in the collection.
 
110
template <class Collection, class Key>
 
111
bool ContainsKey(const Collection& collection, const Key& key) {
 
112
  typename Collection::const_iterator it = collection.find(key);
 
113
  return it != collection.end();
 
114
}
 
115
 
 
116
// Inserts a new key/value into a map or hash_map.
 
117
// Dies if the key is already present.
 
118
template<class Collection>
 
119
void InsertOrDie(Collection* const collection,
 
120
                 const typename Collection::value_type::first_type& key,
 
121
                 const typename Collection::value_type::second_type& data) {
 
122
  typedef typename Collection::value_type value_type;
 
123
  CHECK(collection->insert(value_type(key, data)).second)
 
124
    << "duplicate key: " << key;
 
125
}
 
126
 
 
127
}  // namespace ceres
 
128
 
 
129
#endif  // CERES_INTERNAL_MAP_UTIL_H_