MorphoGraphX  2.0-1-227
Insert.hpp
Go to the documentation of this file.
1 //
2 // This file is part of MorphoGraphX - http://www.MorphoGraphX.org
3 // Copyright (C) 2012-2015 Richard S. Smith and collaborators.
4 //
5 // If you use MorphoGraphX in your work, please cite:
6 // http://dx.doi.org/10.7554/eLife.05864
7 //
8 // MorphoGraphX is free software, and is licensed under under the terms of the
9 // GNU General (GPL) Public License version 2.0, http://www.gnu.org/licenses.
10 //
11 #ifndef INSERT_HPP
12 #define INSERT_HPP
13 
18 #include <Config.hpp>
19 
20 #include <Information.hpp>
21 #include <StaticAssert.hpp>
22 #include <VVGraph.hpp>
23 
24 #include <utility>
25 
26 namespace mgx
27 {
57  template <class vvGraph> class Insert {
58  public:
59  typedef typename vvGraph::vertex_t vertex;
60 
61  vertex operator()(const vertex& a, const vertex& b, vvGraph& S) const
62  {
63  if(a.isNull() || b.isNull()) {
64  return vertex(0);
65  }
66 
67  unsigned int check = 0;
68  if(S.edge(a, b))
69  check++;
70  if(S.edge(b, a))
71  check++;
72 
73  switch(check) {
74  case 0:
75  Information::err << "Warning: Attempt to insert a vertex between vertices that have no relation." << endl;
76  return vertex(0);
77  break;
78  case 1:
79  Information::err << "Warning: Attempt to insert a vertex between vertices that have an assymetric relation."
80  << endl;
81  return vertex(0);
82  break;
83  default:
84  break;
85  }
86 
87  vertex x;
88  S.insert(x);
89 
90  S.replace(a, b, x);
91  S.replace(b, a, x);
92 
93  S.insertEdge(x, a);
94  S.insertEdge(x, b);
95 
96  return x;
97  }
98  };
99 
100  template <typename VertexContent, typename EdgeContent>
105  {
107  static const Insert fct = Insert();
108  return fct(a, b, S);
109  }
110 
115  template <class Graph>
116  typename Graph::edge_t insertAfter(const typename Graph::vertex_t& v, const typename Graph::vertex_t& ref,
117  const typename Graph::vertex_t& nv, Graph& S)
118  {
119  if(ref)
120  return S.spliceAfter(v, ref, nv);
121  return S.insertEdge(v, nv);
122  }
123 
128  template <class Graph>
129  typename Graph::edge_t insertBefore(const typename Graph::vertex_t& v, const typename Graph::vertex_t& ref,
130  const typename Graph::vertex_t& nv, Graph& S)
131  {
132  if(ref)
133  return S.spliceBefore(v, ref, nv);
134  return S.insertEdge(v, nv);
135  }
136 }
137 #endif
mgx::insertAfter
Graph::edge_t insertAfter(const typename Graph::vertex_t &v, const typename Graph::vertex_t &ref, const typename Graph::vertex_t &nv, Graph &S)
Splice nv after ref in v if ref is not null.
Definition: Insert.hpp:116
Information.hpp
mgx::VVGraph::insert
bool insert(const vertex_t &v, bool checkUnique=true)
Insert a new vertex in the graph.
Definition: VVGraph.hpp:1470
StaticAssert.hpp
mgx::Information::err
mgx_EXPORT QTextStream err
VVGraph.hpp
mgx::insert
const VVGraph< VertexContent, EdgeContent >::vertex_t & insert(const typename VVGraph< VertexContent, EdgeContent >::vertex_t &a, const typename VVGraph< VertexContent, EdgeContent >::vertex_t &b, VVGraph< VertexContent, EdgeContent > &S)
Definition: Insert.hpp:102
mgx::Vertex::isNull
bool isNull() const
Test if a vertex is a null vertex.
Definition: Vertex.hpp:235
mgx
Distributed matrix library.
Definition: Assert.hpp:26
mgx::VVGraph::replace
edge_t replace(const vertex_t &v, const vertex_t &neighbor, const vertex_t &new_neighbor)
Replace a vertex by another in a neighborhood.
Definition: VVGraph.hpp:1711
mgx::VVGraph::insertEdge
edge_t insertEdge(const vertex_t &src, const vertex_t &tgt)
Insert a new edge in the graph, without ordering.
Definition: VVGraph.hpp:1823
mgx::Insert::operator()
vertex operator()(const vertex &a, const vertex &b, vvGraph &S) const
Definition: Insert.hpp:61
mgx::VVGraph::edge
edge_t edge(const vertex_t &src, const vertex_t &tgt)
Returns the edge from src to tgt.
Definition: VVGraph.hpp:1766
mgx::Insert
Insert a new vertex on an edge.
Definition: Insert.hpp:57
mgx::insertBefore
Graph::edge_t insertBefore(const typename Graph::vertex_t &v, const typename Graph::vertex_t &ref, const typename Graph::vertex_t &nv, Graph &S)
Splice nv before ref in v if ref is not null.
Definition: Insert.hpp:129
mgx::VVGraph< VertexData, EdgeData >
mgx::VVGraph::vertex_t
Vertex< VertexContent > vertex_t
Smart pointer on a vertex.
Definition: VVGraph.hpp:170
mgx::Insert::vertex
vvGraph::vertex_t vertex
Definition: Insert.hpp:59
mgx::Vertex
Definition: Vertex.hpp:58