graph_analysis
ArcIterator.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_BOOST_GRAPH_ARC_ITERATOR_HPP
2 #define GRAPH_ANALYSIS_BOOST_GRAPH_ARC_ITERATOR_HPP
3 
4 #include "../EdgeIterator.hpp"
5 #include "../Vertex.hpp"
6 #include <boost/graph/adjacency_list.hpp>
7 
8 namespace graph_analysis {
9 namespace boost_graph {
10 
11 template<typename T>
12 class ArcIterator : public EdgeIterator
13 {
14  typedef typename boost::graph_traits< typename T::graph_t >::edge_iterator EdgeIteratorImpl;
15  typedef typename boost::graph_traits< typename T::graph_t >::edge_descriptor EdgeDescriptor;
16 
17 public:
18  ArcIterator(const T& graph)
19  : mGraph(graph)
20  {
21  boost::tie(mStart, mEnd) = boost::edges(mGraph.raw());
22  mCurrent = mStart;
23  }
24 
25  bool next()
26  {
27  while( mCurrent != mEnd)
28  {
29  EdgeDescriptor edgeDescriptor = *mCurrent;
30  Edge::Ptr edge = mGraph.raw()[edgeDescriptor];
31  ++mCurrent;
32  if( skip(edge))
33  {
34  continue;
35  }
36  setNext(edge);
37  return true;
38  }
39  return false;
40  }
41 
42 private:
43  EdgeIteratorImpl mCurrent;
44  EdgeIteratorImpl mStart;
45  EdgeIteratorImpl mEnd;
46 
47  const T& mGraph;
48 };
49 
50 template<typename T>
52 {
53  typedef typename boost::graph_traits< typename T::graph_t >::out_edge_iterator EdgeIteratorImpl;
54  typedef typename boost::graph_traits< typename T::graph_t >::edge_descriptor EdgeDescriptor;
55  typedef typename boost::graph_traits< typename T::graph_t >::vertex_descriptor VertexDescriptor;
56 
57 public:
58  OutArcIterator(const T& graph, Vertex::Ptr vertex)
59  : mGraph(graph)
60  {
61  GraphElementId id = mGraph.getVertexId(vertex);
62  typename T::VertexMap::const_iterator cit = mGraph.mVertexMap.find(id);
63  if(cit == mGraph.mVertexMap.end())
64  {
65  throw std::invalid_argument("graph_analysis::boost_graph::OutArcIterator: '" + vertex->toString() + "' is not part of this graph");
66  }
67  VertexDescriptor vertexDescriptor = cit->second;
68  boost::tie(mStart, mEnd) = boost::out_edges(vertexDescriptor, mGraph.raw());
69  mCurrent = mStart;
70  }
71 
72  bool next()
73  {
74  while( mCurrent != mEnd)
75  {
76  EdgeDescriptor edgeDescriptor = *mCurrent;
77  Edge::Ptr edge = mGraph.raw()[edgeDescriptor];
78  ++mCurrent;
79  if( skip(edge))
80  {
81  continue;
82  }
83  setNext(edge);
84  return true;
85  }
86  return false;
87  }
88 
89 private:
90  EdgeIteratorImpl mCurrent;
91  EdgeIteratorImpl mStart;
92  EdgeIteratorImpl mEnd;
93 
94  const T& mGraph;
95 };
96 
97 template<typename T>
99 {
100  typedef typename boost::graph_traits< typename T::graph_t >::in_edge_iterator EdgeIteratorImpl;
101  typedef typename boost::graph_traits< typename T::graph_t >::edge_descriptor EdgeDescriptor;
102  typedef typename boost::graph_traits< typename T::graph_t >::vertex_descriptor VertexDescriptor;
103 
104 public:
105  InArcIterator(const T& graph, Vertex::Ptr vertex)
106  : mGraph(graph)
107  {
108  GraphElementId id = mGraph.getVertexId(vertex);
109  typename T::VertexMap::const_iterator cit = mGraph.mVertexMap.find(id);
110  if(cit == mGraph.mVertexMap.end())
111  {
112  throw std::invalid_argument("graph_analysis::boost_graph::InArcIterator: '" + vertex->toString() + "' is not part of this graph");
113  }
114  VertexDescriptor vertexDescriptor = cit->second;
115  boost::tie(mStart, mEnd) = boost::in_edges(vertexDescriptor, mGraph.raw());
116  mCurrent = mStart;
117  }
118 
119  bool next()
120  {
121  while( mCurrent != mEnd)
122  {
123  EdgeDescriptor edgeDescriptor = *mCurrent;
124  Edge::Ptr edge = mGraph.raw()[edgeDescriptor];
125  ++mCurrent;
126  if( skip(edge))
127  {
128  continue;
129  }
130  setNext(edge);
131  return true;
132  }
133  return false;
134  }
135 
136 private:
137  EdgeIteratorImpl mCurrent;
138  EdgeIteratorImpl mStart;
139  EdgeIteratorImpl mEnd;
140 
141  const T& mGraph;
142 };
143 
144 template<typename T>
146 {
147 public:
148  InOutArcIterator(const T& graph, Vertex::Ptr vertex)
149  : mGraph(graph)
150  , mInArcIterator(graph, vertex)
151  , mOutArcIterator(graph, vertex)
152  {}
153 
154  bool next()
155  {
156  if(mInArcIterator.next())
157  {
158  setNext( mInArcIterator.current() );
159  return true;
160  }
161 
162  if(mOutArcIterator.next())
163  {
164  setNext( mOutArcIterator.current() );
165  return true;
166  }
167 
168  return false;
169  }
170 
171 
172 protected:
173  const T& mGraph;
176 };
177 
178 } // end namespace boost_graph
179 } // end namespace graph_analysis
180 #endif // GRAPH_ANALYSIS_BOOST_GRAPH_ARC_ITERATOR_HPP
boost::graph_traits< BidirectionalGraph >::vertex_descriptor VertexDescriptor
Definition: DirectedGraph.hpp:31
bool next()
Test if there is a next item and if it is then updates the current to next.
Definition: ArcIterator.hpp:25
Definition: ArcIterator.hpp:98
uint32_t GraphElementId
Definition: GraphElement.hpp:21
OutArcIterator(const T &graph, Vertex::Ptr vertex)
Definition: ArcIterator.hpp:58
bool next()
Test if there is a next item and if it is then updates the current to next.
Definition: ArcIterator.hpp:154
Definition: ArcIterator.hpp:51
bool next()
Test if there is a next item and if it is then updates the current to next.
Definition: ArcIterator.hpp:72
Definition: ArcIterator.hpp:145
InOutArcIterator(const T &graph, Vertex::Ptr vertex)
Definition: ArcIterator.hpp:148
const T & mGraph
Definition: ArcIterator.hpp:173
void setNext(const Edge::Ptr &edge)
Set next edge Should be applied by class implementing next()
Definition: EdgeIterator.hpp:48
shared_ptr< Edge > Ptr
Definition: Edge.hpp:21
ArcIterator(const T &graph)
Definition: ArcIterator.hpp:18
OutArcIterator< T > mOutArcIterator
Definition: ArcIterator.hpp:175
Definition: ArcIterator.hpp:12
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
shared_ptr< Vertex > Ptr
Definition: Vertex.hpp:22
InArcIterator< T > mInArcIterator
Definition: ArcIterator.hpp:174
InArcIterator(const T &graph, Vertex::Ptr vertex)
Definition: ArcIterator.hpp:105
bool skip(const Edge::Ptr &edge) const
Check if edge should be skipped.
Definition: EdgeIterator.cpp:5
bool next()
Test if there is a next item and if it is then updates the current to next.
Definition: ArcIterator.hpp:119
EdgeIterator interface Allows to access all edges of a graph. Requires implementation to wrap the und...
Definition: EdgeIterator.hpp:14