00001 // g2o - General Graph Optimization 00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard 00003 // 00004 // g2o is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as published 00006 // by the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // g2o is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #include "hyper_graph.h" 00018 00019 #include <assert.h> 00020 #include <queue> 00021 00022 namespace g2o { 00023 00024 HyperGraph::Vertex::Vertex(int id) : _id(id) 00025 { 00026 } 00027 00028 HyperGraph::Vertex::~Vertex() 00029 { 00030 } 00031 00032 HyperGraph::Edge::Edge(int id) : _id(id) 00033 { 00034 } 00035 00036 HyperGraph::Edge::~Edge() 00037 { 00038 } 00039 00040 void HyperGraph::Edge::resize(size_t size) 00041 { 00042 _vertices.resize(size); 00043 } 00044 00045 void HyperGraph::Edge::setId(int id) 00046 { 00047 _id = id; 00048 } 00049 00050 HyperGraph::Vertex* HyperGraph::vertex(int id) 00051 { 00052 VertexIDMap::iterator it=_vertices.find(id); 00053 if (it==_vertices.end()) 00054 return 0; 00055 return it->second; 00056 } 00057 00058 const HyperGraph::Vertex* HyperGraph::vertex(int id) const 00059 { 00060 VertexIDMap::const_iterator it=_vertices.find(id); 00061 if (it==_vertices.end()) 00062 return 0; 00063 return it->second; 00064 } 00065 00066 HyperGraph::Vertex* HyperGraph::addVertex(Vertex* v) 00067 { 00068 Vertex* vn=vertex(v->id()); 00069 if (vn) 00070 return 0; 00071 _vertices.insert( std::make_pair(v->id(),v) ); 00072 return v; 00073 } 00074 00075 HyperGraph::Edge* HyperGraph::addEdge(Edge* e) 00076 { 00077 std::pair<EdgeSet::iterator, bool> result = _edges.insert(e); 00078 if (! result.second) 00079 return 0; 00080 for (std::vector<Vertex*>::iterator it = e->vertices().begin(); it != e->vertices().end(); ++it) { 00081 Vertex* v = *it; 00082 v->edges().insert(e); 00083 } 00084 return e; 00085 } 00086 00087 bool HyperGraph::removeVertex(Vertex* v) 00088 { 00089 VertexIDMap::iterator it=_vertices.find(v->id()); 00090 if (it==_vertices.end()) 00091 return false; 00092 assert(it->second==v); 00093 //remove all edges which are entering or leaving v; 00094 EdgeSet tmp(v->edges()); 00095 for (EdgeSet::iterator it=tmp.begin(); it!=tmp.end(); it++){ 00096 if (!removeEdge(*it)){ 00097 assert(0); 00098 } 00099 } 00100 _vertices.erase(it); 00101 delete v; 00102 return true; 00103 } 00104 00105 bool HyperGraph::removeEdge(Edge* e) 00106 { 00107 EdgeSet::iterator it = _edges.find(e); 00108 if (it == _edges.end()) 00109 return false; 00110 _edges.erase(it); 00111 00112 for (std::vector<Vertex*>::iterator vit = e->vertices().begin(); vit != e->vertices().end(); ++vit) { 00113 Vertex* v = *vit; 00114 it = v->edges().find(e); 00115 assert(it!=v->edges().end()); 00116 v->edges().erase(it); 00117 } 00118 00119 delete e; 00120 return true; 00121 } 00122 00123 HyperGraph::HyperGraph() 00124 { 00125 } 00126 00127 void HyperGraph::clear() 00128 { 00129 for (VertexIDMap::iterator it=_vertices.begin(); it!=_vertices.end(); it++) 00130 delete (it->second); 00131 for (EdgeSet::iterator it=_edges.begin(); it!=_edges.end(); it++) 00132 delete (*it); 00133 _vertices.clear(); 00134 _edges.clear(); 00135 } 00136 00137 HyperGraph::~HyperGraph() 00138 { 00139 clear(); 00140 } 00141 00142 } // end namespace