bioprocessing 6.0.4
basegraph.h
Go to the documentation of this file.
1/* Copyright (C) 2000-2013 CEA
2 *
3 * This software and supporting documentation were developed by
4 * bioPICSEL
5 * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6 * 18, route du Panorama
7 * 92265 Fontenay-aux-Roses
8 * France
9 */
10
11#ifndef BIOPROCESSING_GRAPH_BASEGRAPH
12#define BIOPROCESSING_GRAPH_BASEGRAPH
13
14//--- cartobase --------------------------------------------------------------
15#include <cartobase/smart/rcptr.h> // carto::rc_ptr
16//--- std --------------------------------------------------------------------
17#include <set> // std::set
18//----------------------------------------------------------------------------
19
20namespace bio {
21
22 template <typename E> class BaseGraph;
23 template <typename E> class BaseGraphRef;
24
25//============================================================================
26// BASE GRAPH: DECLARATION
27//============================================================================
38 template <typename E>
39 class BaseGraph: public carto::RCObject
40 {
41 //--- typedef ------------------------------------------------------------
42 protected:
44 public:
45 typedef E Edge;
46 typedef typename Edge::Vertex Vertex;
49
50 //--- constructor --------------------------------------------------------
51 public:
52 virtual ~BaseGraph() {}
53
54 //--- virtual methods ----------------------------------------------------
55 // These methods depend on the chosen implementation and must be defined
56 // by the inheriting class.
57 public:
58 virtual void insert( Vertex v ) { throw; } //< not defined
59 virtual void insert( Edge e ) { throw; } //< not defined
60 virtual bool contains( const Vertex & v ) const { throw; } //< not defined
61 virtual bool contains( const Edge & e ) const { throw; } //< not defined
62 virtual bool empty() const { throw; } //< not defined
63 virtual void clear() { throw; } //< not defined
64
65 //--- virtual operators --------------------------------------------------
66 // These comparison operators must be defined in the implementation class
67 public:
68 virtual bool operator== ( const This & other ) const { throw; } //< not defined
69 virtual bool operator!= ( const This & other ) const { throw; } //< not defined
70 virtual bool operator> ( const This & other ) const { throw; } //< not defined
71 virtual bool operator< ( const This & other ) const { throw; } //< not defined
72 virtual bool operator>= ( const This & other ) const { throw; } //< not defined
73 virtual bool operator<= ( const This & other ) const { throw; } //< not defined
74 virtual bool operator= ( const This & other ) { throw; } //< not defined
75
76 //--- virtual properties -------------------------------------------------
77 // Classic graph properties.
78 // A number of static utility methods templated on the implemented graph
79 // class are defined below. They can be used to easily defined these
80 // methods in the implementation class
81 public:
82 virtual bool isAdjacent( const Vertex & x, const Vertex & y ) const { throw; } //< not defined
83 virtual bool isLinked( const Vertex & x, const Vertex & y ) const { throw; } //< not defined
84 virtual bool isSubGraph( const Graph & g ) const { throw; } //< not defined
85 virtual bool isConnectedComponent( const Graph & g ) const { throw; } //< not defined
86 virtual bool isAdjacentTo( const Vertex & v, const Graph & g ) const { throw; } //< not defined
87 virtual bool isAdjacentFrom( const Vertex & v, const Graph & g ) const { throw; } //< not defined
88 virtual bool isAdjacent( const Edge & e ) const { throw; } //< not defined
89 virtual bool isLinked( const Vertex & v ) const { throw; } //< not defined
90 virtual bool isExtension( const Graph & h, const Graph & g ) const { throw; } //< not defined
91 virtual bool isForestRelativeTo( const Graph & h, const Graph & g ) const { throw; } //< not defined
92 virtual bool isSpanningForestRelativeTo( const Graph & h, const Graph & g ) const { throw; } //< not defined
93 virtual bool isTree( const Graph & g ) const { throw; } //< not defined
94 virtual bool isSpanningTree( const Graph & g ) const { throw; } //< not defined
95 virtual bool isForest( const Graph & g ) const { throw; } //< not defined
96 virtual bool isSpanningForest( const Graph & g ) const { throw; } //< not defined
97 template <typename EdgeIterator>
98 bool isGraphCut( EdgeIterator begin, EdgeIterator end, const Graph & g ) const { throw; } //< not defined
99 template <typename EdgeSet>
100 bool isGraphCut( const EdgeSet & es, const Graph & g ) const { throw; } //< not defined
101
102 //--- defined static methods ---------------------------------------------
103 public:
104 template <typename Set>
105 static Set getComplement( const Set & s, const Set & e );
106
107 //--- utility templated methods ------------------------------------------
112 protected:
113 template <typename G>
114 static bool isAdjacent( const G & thisg, const Vertex & x, const Vertex & y );
115 template <typename G>
116 static bool isLinked( const G & thisg, const Vertex & x, const Vertex & y );
117 template <typename VertexSet, typename G>
118 static bool isLinked( const G & thisg, const Vertex & x, const Vertex & y, VertexSet & s );
119 template <typename G>
120 static bool isSubGraph( const G & thisg, const typename G::Ref & g );
121 template <typename G>
122 static bool isConnectedComponent( const G & thisg, const typename G::Ref & g );
123 template <typename G>
124 static bool isAdjacent( const G & thisg, const Edge & e );
125 template <typename G>
126 static bool isAdjacentTo( const G & thisg, const Vertex & v, const typename G::Ref & g );
127 template <typename G>
128 static bool isAdjacentFrom( const G & thisg, const Vertex & v, const typename G::Ref & g );
129 template <typename G>
130 static typename G::Ref getConnectedComponent( G & thisg, Vertex & v );
131 template <typename G>
132 static void getConnectedComponent( G & thisg, Vertex & v, typename G::Ref & g );
133
134
135 //--- friends ------------------------------------------------------------
136 friend class BaseGraphRef<E>;
137 };
138
139
140//============================================================================
141// BASE GRAPH: DEFINITION
142//============================================================================
143
144 //--- utility templated methods --------------------------------------------
145
146 template <typename E>
147 template <typename G>
148 bool BaseGraph<E>::isAdjacent( const G & thisg,
149 const Vertex & x,
150 const Vertex & y )
151 {
152 typename G::edge_const_iterator e;
153 for( e = thisg.beginEdge(x); e != thisg.endEdge(x); ++e )
154 if( e->other(x) == y )
155 return true;
156 return false;
157 }
158
159 template <typename E>
160 template <typename G>
161 bool BaseGraph<E>::isLinked( const G & thisg,
162 const Vertex & x,
163 const Vertex & y )
164 {
165 if( x == y )
166 return false;
167 std::set<Vertex> viewed;
168 viewed.insert( x );
169 typename G::edge_const_iterator e;
170 for( e = thisg.beginEdge(x); e != thisg.endEdge(x); ++e )
171 if( isLinked( thisg, e->other(x), y, viewed ) )
172 return true;
173 return false;
174 }
175
176 template <typename E>
177 template <typename VertexSet, typename G>
178 bool BaseGraph<E>::isLinked( const G & thisg,
179 const Vertex & x,
180 const Vertex & y,
181 VertexSet & s )
182 {
183 if( x == y )
184 return true;
185 if( s.insert( x ).second )
186 {
187 typename G::edge_const_iterator e;
188 for( e = thisg.beginEdge(x); e != thisg.endEdge(x); ++e )
189 if( isLinked( thisg, e->other(x), y, s ) )
190 return true;
191 }
192 return false;
193 }
194
195 template <typename E>
196 template <typename G>
197 bool BaseGraph<E>::isSubGraph( const G & thisg,
198 const typename G::Ref & g )
199 {
200 if( g.empty() )
201 return false;
202
203 typename G::vertex_const_iterator v;
204 for( v = g.beginVertex(); v != g.endVertex(); ++v )
205 if( !thisg.contains(*v) )
206 return false;
207
208 typename G::edge_const_iterator e;
209 for( e = g.beginEdge(); e != g.endEdge(); ++e )
210 if( !thisg.contains(*e) )
211 return false;
212
213 return true;
214 }
215
216 template <typename E>
217 template <typename G>
219 const typename G::Ref & g )
220 {
221 if( g.empty() )
222 return false;
223
224 Vertex one = *( g.beginVertex() );
225 return g == thisg.getConnectedComponent( one );
226 }
227
228 template <typename E>
229 template <typename G>
230 bool BaseGraph<E>::isAdjacent( const G & thisg,
231 const Edge & e )
232 {
233 return ( thisg.contains( e.x() ) || thisg.contains( e.y() ) ) && !thisg.contains( e );
234 }
235
236 template <typename E>
237 template <typename G>
238 bool BaseGraph<E>::isAdjacentTo( const G & thisg,
239 const Vertex & v,
240 const typename G::Ref & g )
241 {
242 typename G::edge_const_iterator e;
243 for( e = thisg.beginEdge(v); e != thisg.endEdge(v); ++e )
244 if( g.isAdjacent( *e ) )
245 return true;
246 return false;
247 }
248
249 template <typename E>
250 template <typename G>
251 bool BaseGraph<E>::isAdjacentFrom( const G & thisg,
252 const Vertex & v,
253 const typename G::Ref & g )
254 {
255 typename G::edge_const_iterator e;
256 for( e = g.beginEdge(v); e != g.endEdge(v); ++e )
257 if( thisg.isAdjacent( *e ) )
258 return true;
259 return false;
260 }
261
262 template <typename E>
263 template <typename G>
264 typename G::Ref BaseGraph<E>::getConnectedComponent( G & thisg,
265 Vertex & v )
266 {
267 typename G::Ref out;
268 thisg.getConnectedComponent( v, out );
269 return out;
270 }
271
272 template <typename E>
273 template <typename G>
275 Vertex & v,
276 typename G::Ref & g )
277 {
278 g.insert( v );
279 typename G::edge_iterator e;
280 for( e = thisg.beginEdge(v); e != thisg.endEdge(v); ++e ) {
281 g.insert( *e );
282 if( !g.contains( e->other(v) ) )
283 thisg.getConnectedComponent( e->other(v), g );
284 }
285 }
286
287 //--- defined methods ------------------------------------------------------
288
289 template <typename E>
290 template <typename Set>
291 Set BaseGraph<E>::getComplement( const Set & s, const Set & e )
292 {
293 Set out;
294 typename Set::const_iterator i;
295 for( i = e.begin(); i != e.end(); ++i ) {
296 if( !s.count(*i) )
297 out.insert(*i);
298 }
299 return out;
300 }
301
302//============================================================================
303// BASE GRAPH REF
304//============================================================================
310 template <typename E>
311 class BaseGraphRef: public carto::rc_ptr<BaseGraph<E> >
312 {
313 //--- typedef ------------------------------------------------------------
314 protected:
315 typedef typename carto::rc_ptr<BaseGraph<E> > Base;
318 public:
319 typedef typename Pointed::Vertex Vertex;
320 typedef typename Pointed::Edge Edge;
321 typedef typename Pointed::Graph Graph;
322
323 //--- constructors -------------------------------------------------------
324 public:
325 BaseGraphRef(): Base( new Pointed() ) {}
326 BaseGraphRef( Pointed * g ): Base( g ) {}
327 BaseGraphRef( const Base & other ): Base(other) {}
328 virtual ~BaseGraphRef() {}
329
330 //--- none factory -------------------------------------------------------
331 public:
332 static Graph none() { return This( (Pointed*)0 ); }
333
334 //--- virtual methods ----------------------------------------------------
335 public:
336 virtual void insert( Vertex v ) { return (*this)->insert(v); }
337 virtual void insert( Edge e ) { return (*this)->insert(e); }
338 virtual bool contains( const Vertex & v ) const { return (*this)->contains(v); }
339 virtual bool contains( const Edge & e ) const { return (*this)->contains(e); }
340 virtual bool empty() const { return (*this)->empty(); }
341 virtual void clear() { return (*this)->clear(); }
342
343 //--- virtual operators ----------------------------------------------------
344 public:
345 virtual bool operator== ( const This & other ) const
346 {
347 if( !(*this) && !other )
348 return true;
349 else if( !(*this) || !other )
350 return false;
351 else
352 return *(this->get()) == *(other.get());
353 }
354 virtual bool operator!= ( const This & other ) const
355 {
356 return !( *this == other );
357 }
358 virtual bool operator> ( const This & other ) const
359 {
360 if( *this == other )
361 return false;
362 else
363 return this > &other;
364 }
365 virtual bool operator< ( const This & other ) const
366 {
367 if( *this == other )
368 return false;
369 else
370 return this < &other;
371 }
372 virtual bool operator>= ( const This & other ) const
373 {
374 return ( *this == other ) || ( *this > other );
375 }
376 virtual bool operator<= ( const This & other ) const
377 {
378 return ( *this == other ) || ( *this < other );
379 }
380
381 //--- virtual properties -------------------------------------------------
382 public:
383 virtual bool isAdjacent( const Vertex & x, const Vertex & y ) const { return (*this)->isAdjacent(x,y); }
384 virtual bool isLinked( const Vertex & x, const Vertex & y ) const { return (*this)->isLinked(x,y); }
385 virtual bool isSubGraph( const Graph & g ) const { return (*this)->isSubGraph(g); }
386 virtual bool isConnectedComponent( const Graph & g ) const { return (*this)->isConnectedComponent(g); }
387 virtual bool isAdjacentTo( const Vertex & v, const Graph & g ) const { return (*this)->isAdjacentTo(v,g); }
388 virtual bool isAdjacentFrom( const Vertex & v, const Graph & g ) const { return (*this)->isAdjacentFrom(v,g); }
389 virtual bool isAdjacent( const Edge & e ) const { return (*this)->isAdjacent(e); }
390 virtual bool isLinked( const Vertex & v ) const { return (*this)->isLinked(v); }
391 virtual bool isExtension( const Graph & h, const Graph & g ) const { return (*this)->isExtension(h,g); }
392 virtual bool isForestRelativeTo( const Graph & h, const Graph & g ) const { return (*this)->isForestRelativeTo(h,g); }
393 virtual bool isSpanningForestRelativeTo( const Graph & h, const Graph & g ) const { return (*this)->isSpanningForestRelativeTo(h,g); }
394 virtual bool isTree( const Graph & g ) const { return (*this)->isTree(g); }
395 virtual bool isSpanningTree( const Graph & g ) const { return (*this)->isSpanningTree(g); }
396 virtual bool isForest( const Graph & g ) const { return (*this)->isForest(g); }
397 virtual bool isSpanningForest( const Graph & g ) const { return (*this)->isSpanningForest(g); }
398 template <typename EdgeIterator>
399 bool isGraphCut( EdgeIterator begin, EdgeIterator end, const Graph & g ) const { return (*this)->isGraphCut(begin,end,g); }
400 template <typename EdgeSet>
401 bool isGraphCut( const EdgeSet & es, const Graph & g ) const { return (*this)->isGraphCut(es,g); }
402
403 //--- defined static methods ---------------------------------------------
404 public:
405 template <typename Set>
406 static Set getComplement( const Set & s, const Set & e ) { return Pointed::getComplement(s,e); }
407
408 //--- friends ------------------------------------------------------------
409 friend class BaseGraph<E>;
410 };
411
412} // namespace bio
413
414#endif // BIOPROCESSING_GRAPH_BASEGRAPH
Reference to a BaseGraph.
Definition basegraph.h:312
virtual bool operator==(const This &other) const
Definition basegraph.h:345
Pointed::Vertex Vertex
Definition basegraph.h:319
carto::rc_ptr< BaseGraph< E > > Base
Definition basegraph.h:315
virtual bool contains(const Vertex &v) const
Definition basegraph.h:338
virtual void insert(Edge e)
Definition basegraph.h:337
static Set getComplement(const Set &s, const Set &e)
Definition basegraph.h:406
virtual bool operator>=(const This &other) const
Definition basegraph.h:372
static Graph none()
Definition basegraph.h:332
virtual bool isLinked(const Vertex &v) const
Definition basegraph.h:390
BaseGraphRef< E > This
Definition basegraph.h:317
virtual void clear()
Definition basegraph.h:341
virtual bool isAdjacent(const Vertex &x, const Vertex &y) const
Definition basegraph.h:383
virtual bool contains(const Edge &e) const
Definition basegraph.h:339
virtual bool isForest(const Graph &g) const
Definition basegraph.h:396
virtual bool isTree(const Graph &g) const
Definition basegraph.h:394
virtual void insert(Vertex v)
Definition basegraph.h:336
Pointed::Graph Graph
Definition basegraph.h:321
bool isGraphCut(EdgeIterator begin, EdgeIterator end, const Graph &g) const
Definition basegraph.h:399
Pointed::Edge Edge
Definition basegraph.h:320
BaseGraphRef(Pointed *g)
Definition basegraph.h:326
virtual bool operator!=(const This &other) const
Definition basegraph.h:354
virtual bool isForestRelativeTo(const Graph &h, const Graph &g) const
Definition basegraph.h:392
virtual bool isAdjacent(const Edge &e) const
Definition basegraph.h:389
virtual bool isConnectedComponent(const Graph &g) const
Definition basegraph.h:386
BaseGraph< E > Pointed
Definition basegraph.h:316
virtual bool isLinked(const Vertex &x, const Vertex &y) const
Definition basegraph.h:384
virtual bool isExtension(const Graph &h, const Graph &g) const
Definition basegraph.h:391
virtual bool isSpanningTree(const Graph &g) const
Definition basegraph.h:395
virtual bool isSpanningForest(const Graph &g) const
Definition basegraph.h:397
virtual bool empty() const
Definition basegraph.h:340
virtual bool operator>(const This &other) const
Definition basegraph.h:358
virtual bool isAdjacentTo(const Vertex &v, const Graph &g) const
Definition basegraph.h:387
virtual bool isSpanningForestRelativeTo(const Graph &h, const Graph &g) const
Definition basegraph.h:393
virtual bool isSubGraph(const Graph &g) const
Definition basegraph.h:385
virtual bool isAdjacentFrom(const Vertex &v, const Graph &g) const
Definition basegraph.h:388
virtual bool operator<=(const This &other) const
Definition basegraph.h:376
bool isGraphCut(const EdgeSet &es, const Graph &g) const
Definition basegraph.h:401
BaseGraphRef(const Base &other)
Definition basegraph.h:327
virtual ~BaseGraphRef()
Definition basegraph.h:328
virtual bool operator<(const This &other) const
Definition basegraph.h:365
Base class for graphs.
Definition basegraph.h:40
static bool isAdjacent(const G &thisg, const Edge &e)
Definition basegraph.h:230
virtual void insert(Vertex v)
Definition basegraph.h:58
bool isGraphCut(EdgeIterator begin, EdgeIterator end, const Graph &g) const
Definition basegraph.h:98
static bool isLinked(const G &thisg, const Vertex &x, const Vertex &y, VertexSet &s)
Definition basegraph.h:178
static bool isLinked(const G &thisg, const Vertex &x, const Vertex &y)
Definition basegraph.h:161
static bool isSubGraph(const G &thisg, const typename G::Ref &g)
Definition basegraph.h:197
BaseGraphRef< E > Ref
Reference type.
Definition basegraph.h:48
virtual bool operator<(const This &other) const
Definition basegraph.h:71
E Edge
Usable edge type.
Definition basegraph.h:45
BaseGraphRef< E > Graph
Usable graph type.
Definition basegraph.h:47
virtual bool isAdjacent(const Vertex &x, const Vertex &y) const
Definition basegraph.h:82
virtual bool operator!=(const This &other) const
Definition basegraph.h:69
static bool isAdjacentFrom(const G &thisg, const Vertex &v, const typename G::Ref &g)
Definition basegraph.h:251
virtual bool isForestRelativeTo(const Graph &h, const Graph &g) const
Definition basegraph.h:91
static bool isAdjacent(const G &thisg, const Vertex &x, const Vertex &y)
These methods are templated on the iterator type (which is implementation dependant and thus not know...
Definition basegraph.h:148
virtual bool isAdjacentFrom(const Vertex &v, const Graph &g) const
Definition basegraph.h:87
virtual bool contains(const Edge &e) const
Definition basegraph.h:61
virtual bool isSubGraph(const Graph &g) const
Definition basegraph.h:84
virtual bool isSpanningForestRelativeTo(const Graph &h, const Graph &g) const
Definition basegraph.h:92
virtual bool isSpanningTree(const Graph &g) const
Definition basegraph.h:94
virtual bool contains(const Vertex &v) const
Definition basegraph.h:60
static Set getComplement(const Set &s, const Set &e)
Definition basegraph.h:291
virtual ~BaseGraph()
Definition basegraph.h:52
virtual bool operator>=(const This &other) const
Definition basegraph.h:72
static bool isAdjacentTo(const G &thisg, const Vertex &v, const typename G::Ref &g)
Definition basegraph.h:238
virtual bool operator>(const This &other) const
Definition basegraph.h:70
virtual bool isForest(const Graph &g) const
Definition basegraph.h:95
virtual bool isLinked(const Vertex &x, const Vertex &y) const
Definition basegraph.h:83
virtual bool isLinked(const Vertex &v) const
Definition basegraph.h:89
bool isGraphCut(const EdgeSet &es, const Graph &g) const
Definition basegraph.h:100
virtual bool isConnectedComponent(const Graph &g) const
Definition basegraph.h:85
virtual bool isSpanningForest(const Graph &g) const
Definition basegraph.h:96
virtual bool empty() const
Definition basegraph.h:62
virtual bool operator<=(const This &other) const
Definition basegraph.h:73
static G::Ref getConnectedComponent(G &thisg, Vertex &v)
Definition basegraph.h:264
BaseGraph< E > This
Type of *this.
Definition basegraph.h:43
virtual bool isExtension(const Graph &h, const Graph &g) const
Definition basegraph.h:90
virtual bool operator=(const This &other)
Definition basegraph.h:74
virtual bool isAdjacent(const Edge &e) const
Definition basegraph.h:88
Edge::Vertex Vertex
Usable vertex type.
Definition basegraph.h:46
virtual void clear()
Definition basegraph.h:63
virtual bool isTree(const Graph &g) const
Definition basegraph.h:93
virtual void insert(Edge e)
Definition basegraph.h:59
virtual bool isAdjacentTo(const Vertex &v, const Graph &g) const
Definition basegraph.h:86
static bool isConnectedComponent(const G &thisg, const typename G::Ref &g)
Definition basegraph.h:218
virtual bool operator==(const This &other) const
Definition basegraph.h:68