MorphoGraphX
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 namespace util {
28 
58 template <class vvgraph, bool do_checks = true>
59 class Insert
60 #ifdef JUST_FOR_DOXYGEN
61 {
62 };
63 #else
64 ;
65 #endif
66 
67 template <class vvgraph> class Insert<vvgraph, true> {
68 public:
69  typedef typename vvgraph::vertex_t vertex;
70 
71  vertex operator()(const vertex& a, const vertex& b, vvgraph& S) const
72  {
73  if(a.isNull() || b.isNull()) {
74  return vertex(0);
75  }
76 
77  unsigned int check = 0;
78  if(S.edge(a, b))
79  check++;
80  if(S.edge(b, a))
81  check++;
82 
83  switch(check) {
84  case 0:
85  Information::err << "Warning: Attempt to insert a vertex between vertices that have no relation." << endl;
86  return vertex(0);
87  break;
88  case 1:
89  Information::err << "Warning: Attempt to insert a vertex between vertices that have an assymetric relation."
90  << endl;
91  return vertex(0);
92  break;
93  default:
94  break;
95  }
96 
97  vertex x;
98  S.insert(x);
99 
100  S.replace(a, b, x);
101  S.replace(b, a, x);
102 
103  S.insertEdge(x, a);
104  S.insertEdge(x, b);
106  return x;
107  }
108 };
109 
110 template <class vvgraph> class Insert<vvgraph, false> {
111 public:
112  typedef typename vvgraph::vertex_t vertex;
113 
114  const vertex& operator()(const vertex& a, const vertex& b, vvgraph& S) const
115  {
116  vertex x;
117  const vertex& r = *S.insert(x);
118 
119  S.replace(a, b, x);
120  S.replace(b, a, x);
121 
122  S.insertEdge(x, a);
123  S.insertEdge(x, b);
124 
125  return r;
126  }
127 };
128 
129 template <typename VertexContent, typename EdgeContent, typename Alloc>
134 {
136  static const Insert fct = Insert();
137  return fct(a, b, S);
138 }
139 
144 template <class Graph>
145 typename Graph::edge_t insertAfter(const typename Graph::vertex_t& v, const typename Graph::vertex_t& ref,
146  const typename Graph::vertex_t& nv, Graph& S)
147 {
148  if(ref)
149  return S.spliceAfter(v, ref, nv);
150  return S.insertEdge(v, nv);
151 }
152 
157 template <class Graph>
158 typename Graph::edge_t insertBefore(const typename Graph::vertex_t& v, const typename Graph::vertex_t& ref,
159  const typename Graph::vertex_t& nv, Graph& S)
160 {
161  if(ref)
162  return S.spliceBefore(v, ref, nv);
163  return S.insertEdge(v, nv);
164 }
165 } // namespace util
166 } // namespace mgx
167 #endif // INSERT_HPP
edge_t insertEdge(const vertex_t &src, const vertex_t &tgt)
Insert a new edge in the graph, without ordering.
Definition: VVGraph.hpp:2045
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:1904
vvgraph::vertex_t vertex
Type of a vertex.
Definition: Mesh.hpp:49
Insert a new vertex on an edge.
Definition: Insert.hpp:59
This namespace contains all the API of MorphoGraphX.
Definition: Assert.hpp:26
Vertex of a vv graph.
Definition: Vertex.hpp:67
Define the STATIC_ASSERT macro.
Contain the definition of the VVGraph template class.
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:140
bool isNull() const
Test if a vertex is a null vertex.
Definition: Vertex.hpp:284
iterator insert(const vertex_t &v)
Insert a new vertex in the graph.
Definition: VVGraph.hpp:1595
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:153
edge_t edge(const vertex_t &src, const vertex_t &tgt)
Returns the edge from src to tgt.
Definition: VVGraph.hpp:1964