graph 6.0.0
Graph: generic attributed relational graphs
graph.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34#ifndef GRAPH_GRAPH_GRAPH_H
35#define GRAPH_GRAPH_GRAPH_H
36
37
38//=============================================================================
39// H E A D E R F I L E S
40//=============================================================================
41
43#ifndef GRAPH_GRAPH_GRAPHOBJECT_H
45#endif
46#ifndef GRAPH_GRAPH_VERTEX_H
47#include <graph/graph/vertex.h>
48#endif
49#ifndef GRAPH_GRAPH_EDGE_H
50#include <graph/graph/edge.h>
51#endif
52#ifndef GRAPH_GRAPH_GFACTORY_H
54#endif
55#include <cartobase/smart/rcptr.h>
56#include <cartobase/type/types.h>
57
58#include <algorithm>
59#include <functional>
60#include <set>
61#include <stdexcept>
62
63
64//=============================================================================
65// C L A S S D E C L A R A T I O N
66//=============================================================================
67
72{
73
74public:
75
76 typedef std::set<Vertex*> VSet;
77 typedef std::set<Edge*> ESet;
78
80 typedef VSet::value_type value_type;
81#ifndef _WIN32
83 typedef VSet::pointer pointer;
84#endif
85 typedef VSet::reference reference;
86 typedef VSet::const_reference const_reference;
87 typedef VSet::iterator iterator;
88 typedef VSet::const_iterator const_iterator;
89 typedef VSet::reverse_iterator reverse_iterator;
90 typedef VSet::const_reverse_iterator const_reverse_iterator;
91
94 Graph(const std::string& s = "");
95
100 Graph(const FactoryPtr factory, const std::string& s = "");
101
107 virtual ~Graph();
108
115 template <class InputIterator>
116 void extract(Graph& graph, InputIterator iv1, InputIterator iv2);
117
118 //---------------------------------------------------------------------
120 //---------------------------------------------------------------------
122
125 void clear();
126
128
129 //---------------------------------------------------------------------
131 //---------------------------------------------------------------------
133
142 Vertex* addVertex(const std::string& s = "");
143
154 Vertex* cloneVertex(const Vertex* vertex);
155
160 bool hasVertex(const Vertex* vertex) const;
161
168 void removeVertex(Vertex* vertex);
169
174
178 const std::set<Vertex*>& vertices() const;
179
184 std::set<Vertex*> getVerticesWith(const std::string& s) const;
185
192 template <class T>
193 std::set<Vertex*> getVerticesWith(const std::string& s,
194 const T& t) const;
195
197
198 //---------------------------------------------------------------------
200 //---------------------------------------------------------------------
202
215 Edge* addUndirectedEdge(Vertex* vertex1, Vertex* vertex2, std::
216 string s);
217
220 Edge* addEdge(Vertex* vertex1, Vertex* vertex2, std::string s = "");
221
234 Edge* addDirectedEdge(Vertex* vertex1, Vertex* vertex2, std::string s);
235
240 bool hasEdge(const Edge* edge) const;
241
248 void removeEdge(Edge* edge);
249
253 const std::set<Edge*>& edges() const;
254
260 std::set<Edge*> edges(const Vertex* vertex1,
261 const Vertex* vertex2) const;
262
267 std::set<Edge*> getEdgesWith(const std::string& s) const;
268
276 template <class T>
277 std::set<Edge*> getEdgesWith(const std::string& s, const T& t) const;
278
280
281 //---------------------------------------------------------------------
283 //---------------------------------------------------------------------
285
290 iterator begin();
291
296 iterator end();
297
302 const_iterator begin() const;
303
308 const_iterator end() const;
309
316
323
330
337
339
340 //---------------------------------------------------------------------
342 //---------------------------------------------------------------------
344
348 size_t order() const;
349
353 size_t size() const
354#ifndef AIMS_GRAPH_SIZE_NO_DEPREC_WARNING
355 __attribute__((__deprecated__("use edgeSize() for "
356 "the number of edges. In a future release, size() will return the "
357 "number properties as it does in GenericObject")))
358#endif
359 ;
363 size_t edgesSize() const;
364
369 virtual bool isUndirected() const;
370
375 virtual bool isDirected() const;
376
382 virtual bool check(const carto::SyntaxSet& syntax,
383 std::set<std::string>& missing) const;
384
386
387private:
388
389 //---------------------------------------------------------------------
391 //---------------------------------------------------------------------
393
399 void internalExtract(Graph& graph, const std::set<Vertex*>& vertices);
400
402
403 //---------------------------------------------------------------------
405 //---------------------------------------------------------------------
407
410 Graph(const Graph&);
411
414 Graph& operator=(const Graph&);
415
417
418 //---------------------------------------------------------------------
420 //---------------------------------------------------------------------
422
425 std::set<Vertex*> _vertices;
426
440 std::set<Edge*> _edges;
441
443
444protected:
448
449};
450
451
452#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
453
454namespace carto
455{
456
457 template <> class DataTypeCode< Graph >
458 {
459 public:
460 static std::string objectType()
461 { return "Graph"; }
462 static std::string dataType()
463 { return "VOID"; }
464 static std::string name()
465 {
466 return "Graph";
467 }
468 };
469
470}
471
472#endif // DOXYGEN_HIDE_INTERNAL_CLASSES
473
474
475//=============================================================================
476// I N L I N E M E T H O D S
477//=============================================================================
478
479inline
480const std::set<Vertex*>&
482{
483 return _vertices;
484}
485
486
487template <class T>
488inline
489std::set<Vertex*>
490Graph::getVerticesWith(const std::string& s, const T& t) const
491{
492 std::set<Vertex*> vertices;
493
494 for (VSet::const_iterator v = begin(); v != end(); ++v)
495 {
496 if ((*v)->hasProperty(s))
497 {
498 T tmp;
499 if ((*v)->getProperty(s, tmp) && tmp == t)
500 {
501 vertices.insert(*v);
502 }
503 }
504 }
505
506 return vertices;
507}
508
509
510inline
511Edge*
512Graph::addEdge(Vertex* vertex1, Vertex* vertex2, std::string s)
513{
514 return addUndirectedEdge(vertex1, vertex2, s);
515}
516
517
518inline
519const std::set<Edge*>&
521{
522 return _edges;
523}
524
525
526template <class T>
527inline
528std::set<Edge*>
529Graph::getEdgesWith(const std::string& s, const T& t) const
530{
531 std::set<Edge*> edges;
532
533 for (ESet::iterator e = _edges.begin(); e != _edges.end(); ++e)
534 {
535 if ((*e)->hasProperty(s))
536 {
537 T tmp;
538 if ((*e)->getProperty(s, tmp) && tmp == t)
539 {
540 edges.insert(*e);
541 }
542 }
543 }
544
545 return edges;
546}
547
548
549template <class InputIterator>
550inline
551void
552Graph::extract(Graph& graph, InputIterator iv1, InputIterator iv2)
553{
554 internalExtract(graph, std::set<Vertex*>(iv1, iv2));
555}
556
557
558inline
561{
562 return _vertices.begin();
563}
564
565
566inline
569{
570 return _vertices.end();
571}
572
573
574inline
577{
578 return _vertices.begin();
579}
580
581
582inline
585{
586 return _vertices.end();
587}
588
589
590inline
593{
594 return _vertices.rbegin();
595}
596
597
598inline
601{
602 return _vertices.rend();
603}
604
605inline
608{
609 return _vertices.rbegin();
610}
611
612
613inline
616{
617 return _vertices.rend();
618}
619
620
621namespace carto
622{
624}
625
626#endif
#define __deprecated__(msg)
#define __attribute__(a)
The abstract base class for all types of edges; edges are created and managed by Graphs.
Definition edge.h:69
GraphObject(const std::string &s)
The programmer should not call the constructor of an abstract base class.
The base class for graphs.
Definition graph.h:72
bool hasVertex(const Vertex *vertex) const
Does the graph contain a given vertex?
reverse_iterator rbegin()
Get the beginning of the reversed vertex collection of the graph.
Definition graph.h:592
void removeEdge(Edge *edge)
Delete and remove an edge from the graph.
virtual bool check(const carto::SyntaxSet &syntax, std::set< std::string > &missing) const
Reimplemented to check children recursively.
virtual ~Graph()
The destructor is responsible for releasing the memory allocated by the addVertex and addEdge methods...
size_t order() const
The order of a graph is the number of its vertices.
size_t size() const __attribute__((__deprecated__("use edgeSize() for " "the number of edges. In a future release
iterator end()
Get the end of the vertex collection of the graph.
Definition graph.h:568
VSet::reference reference
Definition graph.h:85
virtual bool isDirected() const
Does the graph contain only directed edges?
Edge * addUndirectedEdge(Vertex *vertex1, Vertex *vertex2, std::string s)
Allocate and add to the graph an undirected edge containing the given vertices.
VSet::value_type value_type
Definition graph.h:80
VSet::reverse_iterator reverse_iterator
Definition graph.h:89
virtual bool isUndirected() const
Does the graph contain only undirected edges?
iterator begin()
Get the beginning of the vertex collection of the graph.
Definition graph.h:560
std::set< Vertex * > VSet
Definition graph.h:76
bool hasEdge(const Edge *edge) const
Does the graph contain a particular edge?
std::set< Edge * > edges(const Vertex *vertex1, const Vertex *vertex2) const
Return the set of edges linking two given vertices.
const std::set< Vertex * > & vertices() const
Return the vertices of the graph.
Definition graph.h:481
const std::set< Edge * > & edges() const
Return the edges of the graph.
Definition graph.h:520
Graph(const std::string &s="")
Vertex * addVertex(const std::string &s="")
Allocate and add a vertex to the graph.
VSet::const_reverse_iterator const_reverse_iterator
Definition graph.h:90
VSet::const_reference const_reference
Definition graph.h:86
VSet::pointer pointer
pointer is absent from MS Visual C++ / Intel Win32
Definition graph.h:83
void removeVertex(Vertex *vertex)
Delete and remove a vertex from the graph.
Graph(const FactoryPtr factory, const std::string &s="")
Vertex * randomVertex() const
Return a random vertex (CAUTION: not perfectly random!)
VSet::iterator iterator
Definition graph.h:87
std::set< Vertex * > getVerticesWith(const std::string &s) const
Find the vertices which contain a given semantic attribute.
std::set< Edge * > getEdgesWith(const std::string &s) const
Find the edges which contain a given semantic attribute.
FactoryPtr _factory
Abstract factory used to create vertices and edges.
Definition graph.h:447
void extract(Graph &graph, InputIterator iv1, InputIterator iv2)
Extract a subgraph.
Definition graph.h:552
void clear()
Delete vertices and edges.
size_t edgesSize() const
The edgesSize of a graph is the number of its edges.
VSet::const_iterator const_iterator
Definition graph.h:88
Edge * addDirectedEdge(Vertex *vertex1, Vertex *vertex2, std::string s)
Allocate and add to the graph a directed edge containing the given vertices.
std::set< Edge * > ESet
Definition graph.h:77
Vertex * cloneVertex(const Vertex *vertex)
Clone a vertex of the graph, without attaching it to any edge.
Edge * addEdge(Vertex *vertex1, Vertex *vertex2, std::string s="")
A synonym of addUnDirectedEdge - see above.
Definition graph.h:512
carto::rc_ptr< GraphFactory > FactoryPtr
Definition graph.h:79
reverse_iterator rend()
Get the end of the reversed vertex collection of the graph.
Definition graph.h:600
Vertices are created and managed by Graphs.
Definition vertex.h:64
static std::string name()
Definition graph.h:464
static std::string dataType()
Definition graph.h:462
static std::string objectType()
Definition graph.h:460
#define GRAPH_API
std::map< std::string, Syntax > SyntaxSet
STL namespace.
#define DECLARE_GENERIC_OBJECT_TYPE(T)