Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef MRPT_DIRECTEDGRAPH_H
00029 #define MRPT_DIRECTEDGRAPH_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032
00033
00034
00035
00036
00037 namespace mrpt
00038 {
00039 namespace graphs
00040 {
00041 using mrpt::utils::TNodeID;
00042 using mrpt::utils::TPairNodeIDs;
00043
00044
00045
00046
00047 namespace detail
00048 {
00049
00050 struct edge_annotations_empty { };
00051 }
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 template<class TYPE_EDGES, class EDGE_ANNOTATIONS = detail::edge_annotations_empty>
00062 class CDirectedGraph
00063 {
00064 public:
00065
00066 struct edge_t : public TYPE_EDGES, public EDGE_ANNOTATIONS
00067 {
00068
00069 inline edge_t () : TYPE_EDGES() { }
00070 template <typename ARG1> inline edge_t (const ARG1 &a1) : TYPE_EDGES(a1) { }
00071 template <typename ARG1,typename ARG2> inline edge_t (const ARG1 &a1,const ARG2 &a2) : TYPE_EDGES(a1,a2) { }
00072 };
00073
00074
00075 typedef typename mrpt::aligned_containers<TPairNodeIDs,edge_t>::multimap_t edges_map_t;
00076 typedef typename edges_map_t::iterator iterator;
00077 typedef typename edges_map_t::const_iterator const_iterator;
00078
00079
00080 edges_map_t edges;
00081
00082
00083 inline CDirectedGraph(const edges_map_t &obj) : edges(obj) { }
00084 inline CDirectedGraph() : edges() {}
00085
00086 inline iterator begin() { return edges.begin(); }
00087 inline iterator end() { return edges.end(); }
00088 inline const_iterator begin() const { return edges.begin(); }
00089 inline const_iterator end() const { return edges.end(); }
00090
00091
00092
00093
00094
00095 inline size_t edgeCount() const { return edges.size(); }
00096 inline void clearEdges() { edges.clear(); }
00097
00098
00099
00100 inline void insertEdge(TNodeID from_nodeID, TNodeID to_nodeID,const edge_t &edge_value )
00101 {
00102 EIGEN_ALIGN16 typename edges_map_t::value_type entry(
00103 std::make_pair(from_nodeID,to_nodeID),
00104 edge_value);
00105 edges.insert(entry);
00106 }
00107
00108
00109 inline void insertEdgeAtEnd(TNodeID from_nodeID, TNodeID to_nodeID,const edge_t &edge_value )
00110 {
00111 EIGEN_ALIGN16 typename edges_map_t::value_type entry(
00112 std::make_pair(from_nodeID,to_nodeID),
00113 edge_value);
00114 edges.insert(edges.end(), entry);
00115 }
00116
00117
00118 inline bool edgeExists(TNodeID from_nodeID, TNodeID to_nodeID) const
00119 { return edges.find(std::make_pair(from_nodeID,to_nodeID))!=edges.end(); }
00120
00121
00122
00123
00124
00125
00126 edge_t & getEdge(TNodeID from_nodeID, TNodeID to_nodeID)
00127 {
00128 iterator it = edges.find(std::make_pair(from_nodeID,to_nodeID));
00129 if (it==edges.end())
00130 THROW_EXCEPTION( format("Edge %u->%u does not exist",(unsigned)from_nodeID,(unsigned)to_nodeID) )
00131 else return it->second;
00132 }
00133
00134
00135
00136
00137
00138
00139 const edge_t & getEdge(TNodeID from_nodeID, TNodeID to_nodeID) const
00140 {
00141 const_iterator it = edges.find(std::make_pair(from_nodeID,to_nodeID));
00142 if (it==edges.end())
00143 THROW_EXCEPTION( format("Edge %u->%u does not exist",(unsigned)from_nodeID,(unsigned)to_nodeID) )
00144 else return it->second;
00145 }
00146
00147
00148 std::pair<iterator,iterator> getEdges(TNodeID from_nodeID, TNodeID to_nodeID) {
00149 return edges.equal_range( std::make_pair(from_nodeID,to_nodeID) );
00150 }
00151
00152 std::pair<const_iterator,const_iterator> getEdges(TNodeID from_nodeID, TNodeID to_nodeID) const {
00153 return edges.equal_range( std::make_pair(from_nodeID,to_nodeID) );
00154 }
00155
00156
00157
00158 inline void eraseEdge(TNodeID from_nodeID, TNodeID to_nodeID) {
00159 edges.erase(std::make_pair(from_nodeID,to_nodeID));
00160 }
00161
00162
00163
00164 void getAllNodes( std::set<TNodeID> &lstNode_IDs) const
00165 {
00166 lstNode_IDs.clear();
00167 for (typename edges_map_t::const_iterator it=edges.begin();it!=edges.end();++it)
00168 {
00169 lstNode_IDs.insert(it->first.first);
00170 lstNode_IDs.insert(it->first.second);
00171 }
00172 }
00173
00174
00175 inline std::set<TNodeID> getAllNodes() const { std::set<TNodeID> lst; getAllNodes(lst); return lst; }
00176
00177
00178
00179 size_t countDifferentNodesInEdges() const
00180 {
00181 std::set<TNodeID> aux;
00182 for (typename edges_map_t::const_iterator it=edges.begin();it!=edges.end();++it)
00183 {
00184 aux.insert(it->first.first);
00185 aux.insert(it->first.second);
00186 }
00187 return aux.size();
00188 }
00189
00190
00191 void getNeighborsOf(const TNodeID nodeID, std::set<TNodeID> &neighborIDs) const
00192 {
00193 neighborIDs.clear();
00194 for (typename edges_map_t::const_iterator it=edges.begin();it!=edges.end();++it)
00195 {
00196 if (it->first.first==nodeID)
00197 neighborIDs.insert(it->first.second);
00198 else if (it->first.second==nodeID)
00199 neighborIDs.insert(it->first.first);
00200 }
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210 template <class MAP_NODEID_SET_NODEIDS>
00211 void getAdjacencyMatrix( MAP_NODEID_SET_NODEIDS &outAdjacency ) const
00212 {
00213 outAdjacency.clear();
00214 for (typename edges_map_t::const_iterator it=edges.begin();it!=edges.end();++it)
00215 {
00216 outAdjacency[it->first.first].insert(it->first.second);
00217 outAdjacency[it->first.second].insert(it->first.first);
00218 }
00219 }
00220
00221
00222
00223 template <class MAP_NODEID_SET_NODEIDS,class SET_NODEIDS>
00224 void getAdjacencyMatrix( MAP_NODEID_SET_NODEIDS &outAdjacency, const SET_NODEIDS &onlyForTheseNodes ) const
00225 {
00226 outAdjacency.clear();
00227 const typename SET_NODEIDS::const_iterator setEnd = onlyForTheseNodes.end();
00228 for (typename edges_map_t::const_iterator it=edges.begin();it!=edges.end();++it)
00229 {
00230 if (onlyForTheseNodes.find(it->first.first)==setEnd || onlyForTheseNodes.find(it->first.second)==setEnd)
00231 continue;
00232 outAdjacency[it->first.first].insert(it->first.second);
00233 outAdjacency[it->first.second].insert(it->first.first);
00234 }
00235 }
00236
00237
00238
00239 };
00240
00241
00242 }
00243 }
00244 #endif