graph  4.7.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
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  __attribute__((__deprecated__("use edgeSize() for "
355  "the number of edges. In a future release, size() will return the "
356  "number properties as it does in GenericObject")));
360  size_t edgesSize() const;
361 
366  virtual bool isUndirected() const;
367 
372  virtual bool isDirected() const;
373 
379  virtual bool check(const carto::SyntaxSet& syntax,
380  std::set<std::string>& missing) const;
381 
383 
384 private:
385 
386  //---------------------------------------------------------------------
388  //---------------------------------------------------------------------
390 
396  void internalExtract(Graph& graph, const std::set<Vertex*>& vertices);
397 
399 
400  //---------------------------------------------------------------------
402  //---------------------------------------------------------------------
404 
407  Graph(const Graph&);
408 
411  Graph& operator=(const Graph&);
412 
414 
415  //---------------------------------------------------------------------
417  //---------------------------------------------------------------------
419 
422  std::set<Vertex*> _vertices;
423 
437  std::set<Edge*> _edges;
438 
440 
441 protected:
444  FactoryPtr _factory;
445 
446 };
447 
448 
449 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
450 
451 namespace carto
452 {
453 
454  template <> class DataTypeCode< Graph >
455  {
456  public:
457  static std::string objectType()
458  { return "Graph"; }
459  static std::string dataType()
460  { return "VOID"; }
461  static std::string name()
462  {
463  return "Graph";
464  }
465  };
466 
467 }
468 
469 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
470 
471 
472 //=============================================================================
473 // I N L I N E M E T H O D S
474 //=============================================================================
475 
476 inline
477 const std::set<Vertex*>&
479 {
480  return _vertices;
481 }
482 
483 
484 template <class T>
485 inline
486 std::set<Vertex*>
487 Graph::getVerticesWith(const std::string& s, const T& t) const
488 {
489  std::set<Vertex*> vertices;
490 
491  for (VSet::const_iterator v = begin(); v != end(); ++v)
492  {
493  if ((*v)->hasProperty(s))
494  {
495  T tmp;
496  if ((*v)->getProperty(s, tmp) && tmp == t)
497  {
498  vertices.insert(*v);
499  }
500  }
501  }
502 
503  return vertices;
504 }
505 
506 
507 inline
508 Edge*
509 Graph::addEdge(Vertex* vertex1, Vertex* vertex2, std::string s)
510 {
511  return addUndirectedEdge(vertex1, vertex2, s);
512 }
513 
514 
515 inline
516 const std::set<Edge*>&
518 {
519  return _edges;
520 }
521 
522 
523 template <class T>
524 inline
525 std::set<Edge*>
526 Graph::getEdgesWith(const std::string& s, const T& t) const
527 {
528  std::set<Edge*> edges;
529 
530  for (ESet::iterator e = _edges.begin(); e != _edges.end(); ++e)
531  {
532  if ((*e)->hasProperty(s))
533  {
534  T tmp;
535  if ((*e)->getProperty(s, tmp) && tmp == t)
536  {
537  edges.insert(*e);
538  }
539  }
540  }
541 
542  return edges;
543 }
544 
545 
546 template <class InputIterator>
547 inline
548 void
549 Graph::extract(Graph& graph, InputIterator iv1, InputIterator iv2)
550 {
551  internalExtract(graph, std::set<Vertex*>(iv1, iv2));
552 }
553 
554 
555 inline
558 {
559  return _vertices.begin();
560 }
561 
562 
563 inline
566 {
567  return _vertices.end();
568 }
569 
570 
571 inline
574 {
575  return _vertices.begin();
576 }
577 
578 
579 inline
581 Graph::end() const
582 {
583  return _vertices.end();
584 }
585 
586 
587 inline
590 {
591  return _vertices.rbegin();
592 }
593 
594 
595 inline
598 {
599  return _vertices.rend();
600 }
601 
602 inline
605 {
606  return _vertices.rbegin();
607 }
608 
609 
610 inline
612 Graph::rend() const
613 {
614  return _vertices.rend();
615 }
616 
617 
618 namespace carto
619 {
620  DECLARE_GENERIC_OBJECT_TYPE( Graph * )
621 }
622 
623 #endif
#define DECLARE_GENERIC_OBJECT_TYPE(T)
VSet::pointer pointer
pointer is absent from MS Visual C++ / Intel Win32
Definition: graph.h:83
VSet::const_reference const_reference
Definition: graph.h:86
const std::set< Edge * > & edges() const
Return the edges of the graph.
Definition: graph.h:517
const std::set< Vertex * > & vertices() const
Return the vertices of the graph.
Definition: graph.h:478
static std::string dataType()
Definition: graph.h:459
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:457
static std::string name()
Definition: graph.h:461
reverse_iterator rbegin()
Get the beginning of the reversed vertex collection of the graph.
Definition: graph.h:589
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:597
Edge * addEdge(Vertex *vertex1, Vertex *vertex2, std::string s="")
A synonym of addUnDirectedEdge - see above.
Definition: graph.h:509
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:557
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:549
std::set< Edge * > ESet
Definition: graph.h:77
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:565