DGtal  0.9.2
GraphVisitorRange.h
1 
17 #pragma once
18 
31 #if defined(GraphVisitorRange_RECURSES)
32 #error Recursive header files inclusion detected in GraphVisitorRange.h
33 #else // defined(GraphVisitorRange_RECURSES)
34 
35 #define GraphVisitorRange_RECURSES
36 
37 #if !defined GraphVisitorRange_h
38 
39 #define GraphVisitorRange_h
40 
42 // Inclusions
43 #include <iostream>
44 #include "DGtal/base/Common.h"
45 #include "DGtal/base/CountedPtr.h"
47 
48 namespace DGtal
49 {
50 
52  // template class GraphVisitorRange
70  template < typename TGraphVisitor >
72  {
73  public:
74  typedef TGraphVisitor GraphVisitor;
75  typedef typename GraphVisitor::Graph Graph;
76  typedef typename GraphVisitor::Vertex Vertex;
77  typedef typename GraphVisitor::Node Node;
78  typedef typename GraphVisitor::Size Size;
79  typedef typename GraphVisitor::Data Data;
80 
85  struct NodeAccessor {
86  typedef const Node value;
87  typedef const Node value_type;
88  typedef const Node* pointer;
89  typedef const Node& reference;
90  inline
91  static reference get( const Node & node )
92  { return node; }
93  };
94 
99  struct VertexAccessor {
100  typedef const Vertex value;
101  typedef const Vertex value_type;
102  typedef const Vertex* pointer;
103  typedef const Vertex& reference;
104  inline
105  static reference get( const Node & node )
106  { return node.first; }
107  };
108 
116  template <typename TAccessor>
118  {
120  typedef TAccessor Accessor;
121 
122  // stl iterator types.
123  typedef std::input_iterator_tag iterator_category;
124  typedef typename Accessor::value value_type;
125  typedef std::ptrdiff_t difference_type;
126  typedef typename Accessor::pointer pointer;
127  typedef typename Accessor::reference reference;
128 
131 
132  inline
134  : myVisitor( 0 ) {}
135  inline
137  : myVisitor( ptrV ) {}
138  inline
139  GenericConstIterator( const Self & other )
140  : myVisitor( other.myVisitor ) {}
141 
142  inline
143  Self & operator=( const Self & other )
144  {
145  if ( this != &other )
146  myVisitor = other.myVisitor;
147  return *this;
148  }
149 
150  inline
151  reference
152  operator*() const
153  {
154  ASSERT( ( myVisitor.get() != 0 )
155  && "DGtal::GraphVisitorRange<>::GenericConstIterator<>::operator*(): you cannot dereferenced a null visitor (i.e. end()).");
156  return Accessor::get( myVisitor->current() );
157  }
158 
159  inline
160  pointer
161  operator->() const
162  {
163  ASSERT( ( myVisitor.get() != 0 )
164  && "DGtal::GraphVisitorRange<>::GenericConstIterator<>::operator->(): you cannot dereferenced a null visitor (i.e. end()).");
165  return & Accessor::get( operator*() );
166  }
167 
168  inline
169  Self&
171  {
172  myVisitor->expand();
173  return *this;
174  }
175 
176  inline
177  Self
179  {
180  Self __tmp = *this;
181  myVisitor->expand();
182  return __tmp;
183  }
184 
185  inline
186  bool operator==( const Self & other ) const
187  {
188  if ( ( myVisitor.get() == 0 ) || myVisitor->finished() )
189  return ( other.myVisitor.get() == 0 ) || other.myVisitor->finished();
190  else if ( other.myVisitor.get() == 0 )
191  return false;
192  else
193  return &(myVisitor->current()) == &(other.myVisitor->current());
194  }
195 
196  inline
197  bool operator!=( const Self & other ) const
198  {
199  return ! ( this->operator==( other ) );
200  }
201  };
202 
209 
210  typedef VertexConstIterator ConstIterator;
211  typedef ConstIterator const_iterator;
212 
213  // ----------------------- Standard services ------------------------------
214  public:
215 
220 
229  GraphVisitorRange( GraphVisitor* visitorPtr );
230 
237  ConstIterator begin() const;
238 
243  ConstIterator end() const;
244 
251  NodeConstIterator beginNode() const;
252 
257  NodeConstIterator endNode() const;
258 
259  // ----------------------- Interface --------------------------------------
260  public:
261 
266  void selfDisplay ( std::ostream & out ) const;
267 
272  bool isValid() const;
273 
274  // ------------------------- Protected Datas ------------------------------
275  private:
276  // ------------------------- Private Datas --------------------------------
277  private:
278 
281 
282  // ------------------------- Hidden services ------------------------------
283  protected:
284 
290 
291  private:
292 
298  GraphVisitorRange ( const GraphVisitorRange & other );
299 
307 
308  // ------------------------- Internals ------------------------------------
309  private:
310 
311  }; // end of class GraphVisitorRange
312 
313 
320  template <typename TGraphVisitor>
321  std::ostream&
322  operator<< ( std::ostream & out, const GraphVisitorRange<TGraphVisitor> & object );
323 
324 } // namespace DGtal
325 
326 
328 // Includes inline functions.
329 #include "DGtal/graph/GraphVisitorRange.ih"
330 
331 // //
333 
334 #endif // !defined GraphVisitorRange_h
335 
336 #undef GraphVisitorRange_RECURSES
337 #endif // else defined(GraphVisitorRange_RECURSES)
GenericConstIterator< VertexAccessor > VertexConstIterator
GenericConstIterator< TAccessor > Self
GraphVisitor::Data Data
GenericConstIterator(CountedPtr< GraphVisitor > ptrV)
NodeConstIterator beginNode() const
T * get() const
Definition: CountedPtr.h:195
void selfDisplay(std::ostream &out) const
GraphVisitor::Graph Graph
ConstIterator begin() const
GraphVisitor::Size Size
CountedPtr< GraphVisitor > myVisitor
A smart pointer on a graph visitor.
GraphVisitor::Vertex Vertex
DGtal is the top-level namespace which contains all DGtal functions and types.
VertexConstIterator ConstIterator
Aim: Transforms a graph visitor into a single pass input range.
GraphVisitorRange & operator=(const GraphVisitorRange &other)
GenericConstIterator< NodeAccessor > NodeConstIterator
GraphVisitor::Node Node
ConstIterator end() const
NodeConstIterator endNode() const
CountedPtr< GraphVisitor > myVisitor
Smart pointer to a Visitor.