Move FindOrNull to common/utils.h. (#1305)

master
Alexander Belyaev 2018-07-20 10:07:45 +02:00 committed by Wally B. Feed
parent 42d7133a2b
commit c473a65a5e
2 changed files with 32 additions and 14 deletions

View File

@ -0,0 +1,29 @@
/*
* Copyright 2018 The Cartographer Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace cartographer {
namespace common {
template <typename MapType, typename KeyType = typename MapType::key_type,
typename ValueType = typename MapType::mapped_type>
ValueType* FindOrNull(MapType& map, const KeyType& key) {
auto it = map.find(key);
if (it == map.end()) return nullptr;
return &(it->second);
}
} // namespace common
} // namespace cartographer

View File

@ -17,21 +17,10 @@
#include "cartographer/pose_graph/constraint/relative_pose_constraint_2d.h" #include "cartographer/pose_graph/constraint/relative_pose_constraint_2d.h"
#include "cartographer/common/make_unique.h" #include "cartographer/common/make_unique.h"
#include "cartographer/common/utils.h"
namespace cartographer { namespace cartographer {
namespace pose_graph { namespace pose_graph {
namespace {
// TODO(pifon): Move to common/utils.h.
template <typename MapType, typename KeyType = typename MapType::key_type,
typename ValueType = typename MapType::mapped_type>
ValueType* FindOrNull(MapType& map, const KeyType& key) {
auto it = map.find(key);
if (it == map.end()) return nullptr;
return &(it->second);
}
} // namespace
RelativePoseConstraint2D::RelativePoseConstraint2D( RelativePoseConstraint2D::RelativePoseConstraint2D(
const ConstraintId& id, const proto::RelativePose2D& proto) const ConstraintId& id, const proto::RelativePose2D& proto)
@ -43,13 +32,13 @@ RelativePoseConstraint2D::RelativePoseConstraint2D(
void RelativePoseConstraint2D::AddToOptimizer(Nodes* nodes, void RelativePoseConstraint2D::AddToOptimizer(Nodes* nodes,
ceres::Problem* problem) const { ceres::Problem* problem) const {
auto first_node = FindOrNull(nodes->pose_2d_nodes, first_); auto first_node = common::FindOrNull(nodes->pose_2d_nodes, first_);
if (first_node == nullptr) { if (first_node == nullptr) {
LOG(INFO) << "First node was not found in pose_2d_nodes."; LOG(INFO) << "First node was not found in pose_2d_nodes.";
return; return;
} }
auto second_node = FindOrNull(nodes->pose_2d_nodes, second_); auto second_node = common::FindOrNull(nodes->pose_2d_nodes, second_);
if (second_node == nullptr) { if (second_node == nullptr) {
LOG(INFO) << "Second node was not found in pose_2d_nodes."; LOG(INFO) << "Second node was not found in pose_2d_nodes.";
return; return;