123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #ifndef CERES_INTERNAL_MAP_UTIL_H_
- #define CERES_INTERNAL_MAP_UTIL_H_
- #include <utility>
- #include "ceres/internal/export.h"
- #include "glog/logging.h"
- namespace ceres {
- template <class Collection>
- const typename Collection::value_type::second_type& FindOrDie(
- const Collection& collection,
- const typename Collection::value_type::first_type& key) {
- typename Collection::const_iterator it = collection.find(key);
- CHECK(it != collection.end()) << "Map key not found: " << key;
- return it->second;
- }
- template <class Collection>
- const typename Collection::value_type::second_type FindWithDefault(
- const Collection& collection,
- const typename Collection::value_type::first_type& key,
- const typename Collection::value_type::second_type& value) {
- typename Collection::const_iterator it = collection.find(key);
- if (it == collection.end()) {
- return value;
- }
- return it->second;
- }
- template <class Collection>
- bool InsertIfNotPresent(
- Collection* const collection,
- const typename Collection::value_type::first_type& key,
- const typename Collection::value_type::second_type& value) {
- std::pair<typename Collection::iterator, bool> ret =
- collection->insert(typename Collection::value_type(key, value));
- return ret.second;
- }
- template <class Collection>
- typename Collection::value_type::second_type* FindOrNull(
- Collection& collection, // NOLINT
- const typename Collection::value_type::first_type& key) {
- typename Collection::iterator it = collection.find(key);
- if (it == collection.end()) {
- return 0;
- }
- return &it->second;
- }
- template <class Collection, class Key>
- bool ContainsKey(const Collection& collection, const Key& key) {
- typename Collection::const_iterator it = collection.find(key);
- return it != collection.end();
- }
- template <class Collection>
- void InsertOrDie(Collection* const collection,
- const typename Collection::value_type::first_type& key,
- const typename Collection::value_type::second_type& data) {
- using value_type = typename Collection::value_type;
- CHECK(collection->insert(value_type(key, data)).second)
- << "duplicate key: " << key;
- }
- }
- #endif
|