Blame internal/ceres/map_util.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
// Originally by Anton Carver
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_MAP_UTIL_H_
Packit ea1746
#define CERES_INTERNAL_MAP_UTIL_H_
Packit ea1746
Packit ea1746
#include <utility>
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
#include "glog/logging.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
// Perform a lookup in a map or hash_map, assuming that the key exists.
Packit ea1746
// Crash if it does not.
Packit ea1746
//
Packit ea1746
// This is intended as a replacement for operator[] as an rvalue (for reading)
Packit ea1746
// when the key is guaranteed to exist.
Packit ea1746
//
Packit ea1746
// operator[] is discouraged for several reasons:
Packit ea1746
//  * It has a side-effect of inserting missing keys
Packit ea1746
//  * It is not thread-safe (even when it is not inserting, it can still
Packit ea1746
//      choose to resize the underlying storage)
Packit ea1746
//  * It invalidates iterators (when it chooses to resize)
Packit ea1746
//  * It default constructs a value object even if it doesn't need to
Packit ea1746
//
Packit ea1746
// This version assumes the key is printable, and includes it in the fatal log
Packit ea1746
// message.
Packit ea1746
template <class Collection>
Packit ea1746
const typename Collection::value_type::second_type&
Packit ea1746
FindOrDie(const Collection& collection,
Packit ea1746
          const typename Collection::value_type::first_type& key) {
Packit ea1746
  typename Collection::const_iterator it = collection.find(key);
Packit ea1746
  CHECK(it != collection.end()) << "Map key not found: " << key;
Packit ea1746
  return it->second;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Perform a lookup in a map or hash_map.
Packit ea1746
// If the key is present in the map then the value associated with that
Packit ea1746
// key is returned, otherwise the value passed as a default is returned.
Packit ea1746
template <class Collection>
Packit ea1746
const typename Collection::value_type::second_type
Packit ea1746
FindWithDefault(const Collection& collection,
Packit ea1746
                const typename Collection::value_type::first_type& key,
Packit ea1746
                const typename Collection::value_type::second_type& value) {
Packit ea1746
  typename Collection::const_iterator it = collection.find(key);
Packit ea1746
  if (it == collection.end()) {
Packit ea1746
    return value;
Packit ea1746
  }
Packit ea1746
  return it->second;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Insert a new key and value into a map or hash_map.
Packit ea1746
// If the key is not present in the map the key and value are
Packit ea1746
// inserted, otherwise nothing happens. True indicates that an insert
Packit ea1746
// took place, false indicates the key was already present.
Packit ea1746
template <class Collection>
Packit ea1746
bool InsertIfNotPresent(
Packit ea1746
    Collection * const collection,
Packit ea1746
    const typename Collection::value_type::first_type& key,
Packit ea1746
    const typename Collection::value_type::second_type& value) {
Packit ea1746
  std::pair<typename Collection::iterator, bool> ret =
Packit ea1746
      collection->insert(typename Collection::value_type(key, value));
Packit ea1746
  return ret.second;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Perform a lookup in a map or hash_map.
Packit ea1746
// Same as above but the returned pointer is not const and can be used to change
Packit ea1746
// the stored value.
Packit ea1746
template <class Collection>
Packit ea1746
typename Collection::value_type::second_type*
Packit ea1746
FindOrNull(Collection& collection,  // NOLINT
Packit ea1746
           const typename Collection::value_type::first_type& key) {
Packit ea1746
  typename Collection::iterator it = collection.find(key);
Packit ea1746
  if (it == collection.end()) {
Packit ea1746
    return 0;
Packit ea1746
  }
Packit ea1746
  return &it->second;
Packit ea1746
}
Packit ea1746
Packit ea1746
// Test to see if a set, map, hash_set or hash_map contains a particular key.
Packit ea1746
// Returns true if the key is in the collection.
Packit ea1746
template <class Collection, class Key>
Packit ea1746
bool ContainsKey(const Collection& collection, const Key& key) {
Packit ea1746
  typename Collection::const_iterator it = collection.find(key);
Packit ea1746
  return it != collection.end();
Packit ea1746
}
Packit ea1746
Packit ea1746
// Inserts a new key/value into a map or hash_map.
Packit ea1746
// Dies if the key is already present.
Packit ea1746
template<class Collection>
Packit ea1746
void InsertOrDie(Collection* const collection,
Packit ea1746
                 const typename Collection::value_type::first_type& key,
Packit ea1746
                 const typename Collection::value_type::second_type& data) {
Packit ea1746
  typedef typename Collection::value_type value_type;
Packit ea1746
  CHECK(collection->insert(value_type(key, data)).second)
Packit ea1746
    << "duplicate key: " << key;
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_MAP_UTIL_H_