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