From c203321596a38502cb3008a3883805cb91d3535a Mon Sep 17 00:00:00 2001 From: Rainer Kuemmerle Date: Sat, 25 Jan 2025 18:04:24 +0100 Subject: [PATCH] Apply minor syntax sugar --- g2o/core/abstract_graph.cpp | 2 ++ g2o/core/hyper_graph.h | 9 +++++---- g2o/core/optimizable_graph.cpp | 20 ++++++++------------ unit_test/general/graph_operations.cpp | 11 ++++++++++- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/g2o/core/abstract_graph.cpp b/g2o/core/abstract_graph.cpp index fbf177405..9a4b1e47b 100644 --- a/g2o/core/abstract_graph.cpp +++ b/g2o/core/abstract_graph.cpp @@ -90,6 +90,8 @@ void AbstractGraph::clear() { void AbstractGraph::renameTags( const std::unordered_map& tag_mapping) { + if (tag_mapping.empty()) return; + auto map_tag = [&tag_mapping](const std::string& tag) { auto it = tag_mapping.find(tag); if (it == tag_mapping.end()) return tag; diff --git a/g2o/core/hyper_graph.h b/g2o/core/hyper_graph.h index b6e4b269f..db254d761 100644 --- a/g2o/core/hyper_graph.h +++ b/g2o/core/hyper_graph.h @@ -34,6 +34,7 @@ #include #include #include +#include #include #include "g2o_core_api.h" @@ -153,7 +154,7 @@ class G2O_CORE_API HyperGraph { //! returns the set of hyper-edges that are leaving/entering in this vertex EdgeSetWeak& edges() { return edges_; } [[nodiscard]] HyperGraphElementType elementType() const final { - return kHgetVertex; + return HyperGraphElementType::kHgetVertex; } protected: @@ -203,15 +204,15 @@ class G2O_CORE_API HyperGraph { /** set the ith vertex on the hyper-edge to the pointer supplied */ - void setVertex(size_t i, const std::shared_ptr& v) { + void setVertex(size_t i, std::shared_ptr v) { assert(i < vertices_.size() && "index out of bounds"); - vertices_[i] = v; + vertices_[i] = std::move(v); } [[nodiscard]] int id() const { return id_; } void setId(int id); [[nodiscard]] HyperGraphElementType elementType() const final { - return kHgetEdge; + return HyperGraphElementType::kHgetEdge; } [[nodiscard]] int numUndefinedVertices() const; diff --git a/g2o/core/optimizable_graph.cpp b/g2o/core/optimizable_graph.cpp index 491165fe7..f7efebc60 100644 --- a/g2o/core/optimizable_graph.cpp +++ b/g2o/core/optimizable_graph.cpp @@ -89,7 +89,7 @@ bool saveParameter(AbstractGraph& abstract_graph, Parameter* p) { if (tag.empty()) return false; std::vector data; p->getParameterData(data); - abstract_graph.parameters().emplace_back(tag, p->id(), data); + abstract_graph.parameters().emplace_back(tag, p->id(), std::move(data)); return true; } @@ -108,8 +108,7 @@ void addDataToGraphElement( const std::vector& data_vector) { Factory* factory = Factory::instance(); - HyperGraph::GraphElemBitset elemDataBitset; - elemDataBitset[HyperGraph::kHgetData] = true; + const HyperGraph::GraphElemBitset elemDataBitset(1 << HyperGraph::kHgetData); for (const auto& abstract_data : data_vector) { const std::shared_ptr element = factory->construct(abstract_data.tag, elemDataBitset); @@ -355,13 +354,11 @@ bool OptimizableGraph::load(std::istream& is, io::Format format) { return false; } - if (!renamedTypesLookup_.empty()) { - abstract_graph.renameTags(renamedTypesLookup_); - } + abstract_graph.renameTags(renamedTypesLookup_); // Create the parameters of the graph - HyperGraph::GraphElemBitset elemParamBitset; - elemParamBitset[HyperGraph::kHgetParameter] = true; + const HyperGraph::GraphElemBitset elemParamBitset( + 1 << HyperGraph::kHgetParameter); for (const auto& abstract_param : abstract_graph.parameters()) { const std::shared_ptr pelement = factory->construct(abstract_param.tag, elemParamBitset); @@ -384,8 +381,8 @@ bool OptimizableGraph::load(std::istream& is, io::Format format) { } // Create the vertices of the graph - HyperGraph::GraphElemBitset elemVertexBitset; - elemVertexBitset[HyperGraph::kHgetVertex] = true; + const HyperGraph::GraphElemBitset elemVertexBitset( + 1 << HyperGraph::kHgetVertex); for (const auto& abstract_vertex : abstract_graph.vertices()) { const std::shared_ptr graph_element = factory->construct(abstract_vertex.tag, elemVertexBitset); @@ -411,8 +408,7 @@ bool OptimizableGraph::load(std::istream& is, io::Format format) { } // Create the edges of the graph - HyperGraph::GraphElemBitset elemEdgeBitset; - elemEdgeBitset[HyperGraph::kHgetEdge] = true; + const HyperGraph::GraphElemBitset elemEdgeBitset(1 << HyperGraph::kHgetEdge); for (const auto& abstract_edge : abstract_graph.edges()) { const std::shared_ptr graph_element = factory->construct(abstract_edge.tag, elemEdgeBitset); diff --git a/unit_test/general/graph_operations.cpp b/unit_test/general/graph_operations.cpp index c7f417cce..1526be98f 100644 --- a/unit_test/general/graph_operations.cpp +++ b/unit_test/general/graph_operations.cpp @@ -51,7 +51,6 @@ #include "g2o/types/slam3d/edge_se3_pointxyz.h" #include "g2o/types/slam3d/vertex_pointxyz.h" #include "g2o/types/slam3d/vertex_se3.h" -#include "gmock/gmock.h" #include "unit_test/test_helper/allocate_optimizer.h" #include "unit_test/test_helper/eigen_matcher.h" @@ -82,6 +81,16 @@ TEST(General, BinaryEdgeConstructor) { ASSERT_EQ(nullptr, e2.vertices()[1]); } +TEST(General, GraphElemBitset) { + g2o::HyperGraph::GraphElemBitset elemParamBitset; + elemParamBitset[g2o::HyperGraph::kHgetParameter] = true; + + const g2o::HyperGraph::GraphElemBitset elemParamFromInt( + 1 << g2o::HyperGraph::kHgetParameter); + + EXPECT_EQ(elemParamBitset, elemParamFromInt); +} + TEST(General, GraphAddVertex) { auto optimizer = g2o::internal::createOptimizerForTests();