graph_analysis
BaseIterable.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 namespace graph_analysis
4 {
5 
6 /* This class wraps the VertexIterator to stl (specially foreach compatible)
7  * iterators. The Template arguments are:
8  * \tparam T Type of the graph_analysis Iterator like graph_analysis::VertexIterator::Ptr
9  * \tparam G The Parent class which provides the regular operator like graph_analysis::BaseGraph
10  * \tparam E The type the iterator normally returns like graph_analysis::Vertex::Ptr
11  */
12 template <class T, class G, class E>
14 {
15  protected:
16  typedef T (G::*FuncPtr)() const;
17 
18  public:
19  /*
20  * Entry Point which get's called by the Graph representations.
21  * the G *graph is normally a pointer to this from the parent class G
22  * the function pointer _func is a pointer to the function of G which
23  * construct the graph_library pointer of type T
24  */
26  : graph(graph)
27  , func(_func)
28  {
29  }
30 
31  /*
32  * Helper class which provides the actual stl-like pointer.
33  * It normally only get's constructed by the BaseIterable class
34  * and should not be used from somewhere else
35  */
36  class Iterator
37  {
38  public:
39  Iterator(BaseIterable *parent, T _obj)
40  : parent(parent)
41  , obj(_obj)
42  {
43  if (obj.get() && !obj->current()) {
44  obj.reset();
45  }
46  }
47 
49  {
50  return parent->begin();
51  }
53  {
54  return parent->end();
55  }
56 
57  bool operator==(const Iterator &other) const
58  {
59  return !operator!=(other);
60  }
61 
62  bool operator!=(const Iterator &other) const
63  {
64  return other.obj.get() != obj.get();
65  }
66 
68  {
69  if (!obj->next()) {
70  obj.reset();
71  }
72  return *this;
73  }
74 
76  {
77  Iterator &ret = *this;
78  this->operator++();
79  return ret;
80  }
81 
82  E operator*() const
83  {
84  return obj->current();
85  }
86 
87  private:
88  BaseIterable *parent;
89  T obj;
90  };
91 
92  public:
93  /* Created the Interator which points to the first element
94  * If no element can be generated (e.G. next() from the baseclass false
95  * it returns end() instead
96  */
97  Iterator begin()
98  {
99  T it = (graph->*func)();
100  if (!it->next()) {
101  return end();
102  }
103  return Iterator(this, it);
104  }
105  /*
106  * This functions returns a Iterator where the Pointer to the
107  * shared_pointer T is not-initialized (zero pointer)
108  */
109  Iterator end()
110  {
111  return Iterator(this, T());
112  }
113 
114  protected:
115  // Underlayin graph
116  G *graph;
117  // function pointer which can construct a graph_anaysis pointer of type T
119 };
120 
121 /* This class is child of BaseIterable, it is intended to use in case the construction
122  * of a iterator depends on a other object. This can be the case if a iterator for a child-graph or a directed
123  * graph needs to be constructed. The addition Type D points to the needed object type.
124  */
125 template <class T, class G, class E, class D>
126 class SpecializedIterable : public BaseIterable<T, SpecializedIterable<T, G, E, D>, E>
127 {
128  public:
129  typedef T (G::*FuncPtr)(const D&) const;
130 
131  /*
132  * In difference to the BaseIterable class this function needs a pointer to the parent class G, (same as previously) and
133  * a Object of type D, which is the needed object to construct a Iterator of this object.
134  * The FuncPtr _f2 is the needed function which accepts a D as parameter and constructs a Iterator of type T
135  * The parent of BaseIterable does not point to G, instead to this SpecializedIterable<T, G, E, D>
136  */
137  SpecializedIterable(const G * _parent2, const D& _d, FuncPtr _f2)
138  : BaseIterable<T, SpecializedIterable<T, G, E, D>, E>(this, &SpecializedIterable<T, G, E, D>::get)
139  , f2(_f2)
140  , parent2(_parent2)
141  , d(_d)
142  {
143  }
144 
145  /*
146  * Helper function which gets used by BaseIterable to construct a new Iterator object.
147  * to do so it uses the object D d object, to call the function f2 from G parent2
148  */
149  T get() const
150  {
151  return (parent2->*f2)(d);
152  };
153 
154  protected:
155  FuncPtr f2;
156  const G *parent2;
157  D d;
158 };
159 }
graph_analysis::SpecializedIterable::FuncPtr
T(G::* FuncPtr)(const D &) const
Definition: BaseIterable.hpp:129
graph_analysis::SpecializedIterable::d
D d
Definition: BaseIterable.hpp:157
graph_analysis::BaseIterable::Iterator::operator==
bool operator==(const Iterator &other) const
Definition: BaseIterable.hpp:57
graph_analysis::BaseIterable
Definition: BaseIterable.hpp:13
graph_analysis::SpecializedIterable::get
T get() const
Definition: BaseIterable.hpp:149
graph_analysis::BaseIterable::Iterator::Iterator
Iterator(BaseIterable *parent, T _obj)
Definition: BaseIterable.hpp:39
graph_analysis
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
graph_analysis::BaseIterable::Iterator::end
Iterator end()
Definition: BaseIterable.hpp:52
graph_analysis::SpecializedIterable::f2
FuncPtr f2
Definition: BaseIterable.hpp:152
graph_analysis::BaseIterable::end
Iterator end()
Definition: BaseIterable.hpp:109
graph_analysis::BaseIterable::BaseIterable
BaseIterable(G *graph, FuncPtr _func)
Definition: BaseIterable.hpp:25
graph_analysis::BaseIterable::Iterator
Definition: BaseIterable.hpp:36
graph_analysis::SpecializedIterable::parent2
const G * parent2
Definition: BaseIterable.hpp:156
graph_analysis::BaseIterable::graph
G * graph
Definition: BaseIterable.hpp:116
graph_analysis::BaseIterable::Iterator::begin
Iterator begin()
Definition: BaseIterable.hpp:48
graph_analysis::SpecializedIterable::SpecializedIterable
SpecializedIterable(const G *_parent2, const D &_d, FuncPtr _f2)
Definition: BaseIterable.hpp:137
graph_analysis::BaseIterable::Iterator::operator*
E operator*() const
Definition: BaseIterable.hpp:82
graph_analysis::BaseIterable::begin
Iterator begin()
Definition: BaseIterable.hpp:97
graph_analysis::BaseIterable::func
FuncPtr func
Definition: BaseIterable.hpp:118
graph_analysis::BaseIterable::Iterator::operator++
Iterator & operator++(int)
Definition: BaseIterable.hpp:75
graph_analysis::BaseIterable::Iterator::operator++
Iterator & operator++()
Definition: BaseIterable.hpp:67
graph_analysis::SpecializedIterable
Definition: BaseIterable.hpp:126
graph_analysis::BaseIterable::FuncPtr
T(G::* FuncPtr)() const
Definition: BaseIterable.hpp:16
graph_analysis::BaseIterable::Iterator::operator!=
bool operator!=(const Iterator &other) const
Definition: BaseIterable.hpp:62