graph  5.0.5
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
53 #include <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 
71 class GRAPH_API Graph : public GraphObject
72 {
73 
74 public:
75 
76  typedef std::set<Vertex*> VSet;
77  typedef std::set<Edge*> ESet;
78 
80  typedef VSet::value_type value_type;
81 #ifndef _WIN32
82  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 
173  Vertex* randomVertex() const;
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 
315  reverse_iterator rbegin();
316 
322  reverse_iterator rend();
323 
329  const_reverse_iterator rbegin() const;
330 
336  const_reverse_iterator rend() const;
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 
387 private:
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 
444 protected:
447  FactoryPtr _factory;
448 
449 };
450 
451 
452 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
453 
454 namespace 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 
479 inline
480 const std::set<Vertex*>&
482 {
483  return _vertices;
484 }
485 
486 
487 template <class T>
488 inline
489 std::set<Vertex*>
490 Graph::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 
510 inline
511 Edge*
512 Graph::addEdge(Vertex* vertex1, Vertex* vertex2, std::string s)
513 {
514  return addUndirectedEdge(vertex1, vertex2, s);
515 }
516 
517 
518 inline
519 const std::set<Edge*>&
521 {
522  return _edges;
523 }
524 
525 
526 template <class T>
527 inline
528 std::set<Edge*>
529 Graph::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 
549 template <class InputIterator>
550 inline
551 void
552 Graph::extract(Graph& graph, InputIterator iv1, InputIterator iv2)
553 {
554  internalExtract(graph, std::set<Vertex*>(iv1, iv2));
555 }
556 
557 
558 inline
561 {
562  return _vertices.begin();
563 }
564 
565 
566 inline
569 {
570  return _vertices.end();
571 }
572 
573 
574 inline
577 {
578  return _vertices.begin();
579 }
580 
581 
582 inline
584 Graph::end() const
585 {
586  return _vertices.end();
587 }
588 
589 
590 inline
593 {
594  return _vertices.rbegin();
595 }
596 
597 
598 inline
601 {
602  return _vertices.rend();
603 }
604 
605 inline
608 {
609  return _vertices.rbegin();
610 }
611 
612 
613 inline
615 Graph::rend() const
616 {
617  return _vertices.rend();
618 }
619 
620 
621 namespace carto
622 {
624 }
625 
626 #endif
#define DECLARE_GENERIC_OBJECT_TYPE(T)
VSet::pointer pointer
pointer is absent from MS Visual C++ / Intel Win32
Definition: graph.h:83
#define __deprecated__(msg)
VSet::const_reference const_reference
Definition: graph.h:86
const std::set< Edge * > & edges() const
Return the edges of the graph.
Definition: graph.h:520
const std::set< Vertex * > & vertices() const
Return the vertices of the graph.
Definition: graph.h:481
static std::string dataType()
Definition: graph.h:462
carto::rc_ptr< GraphFactory > FactoryPtr
Definition: graph.h:79
VSet::const_reverse_iterator const_reverse_iterator
Definition: graph.h:90
The base class for graphs.
Definition: graph.h:71
VSet::reference reference
Definition: graph.h:85
STL namespace.
static std::string objectType()
Definition: graph.h:460
static std::string name()
Definition: graph.h:464
reverse_iterator rbegin()
Get the beginning of the reversed vertex collection of the graph.
Definition: graph.h:592
The abstract base class for all types of edges; edges are created and managed by Graphs.
Definition: edge.h:68
VSet::reverse_iterator reverse_iterator
Definition: graph.h:89
VSet::value_type value_type
Definition: graph.h:80
#define GRAPH_API
Definition: graph_config.h:46
std::set< Vertex * > getVerticesWith(const std::string &s) const
Find the vertices which contain a given semantic attribute.
std::map< std::string, Syntax > SyntaxSet
reverse_iterator rend()
Get the end of the reversed vertex collection of the graph.
Definition: graph.h:600
Edge * addEdge(Vertex *vertex1, Vertex *vertex2, std::string s="")
A synonym of addUnDirectedEdge - see above.
Definition: graph.h:512
VSet::const_iterator const_iterator
Definition: graph.h:88
VSet::iterator iterator
Definition: graph.h:87
iterator begin()
Get the beginning of the vertex collection of the graph.
Definition: graph.h:560
std::set< Vertex * > VSet
Definition: graph.h:76
The abstract base class for graphs, vertices and edges.
Definition: graphobject.h:52
void extract(Graph &graph, InputIterator iv1, InputIterator iv2)
Extract a subgraph.
Definition: graph.h:552
FactoryPtr _factory
Abstract factory used to create vertices and edges.
Definition: graph.h:447
std::set< Edge * > ESet
Definition: graph.h:77
#define __attribute__(a)
std::set< Edge * > getEdgesWith(const std::string &s) const
Find the edges which contain a given semantic attribute.
Vertices are created and managed by Graphs.
Definition: vertex.h:63
iterator end()
Get the end of the vertex collection of the graph.
Definition: graph.h:568