Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
graph_base.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_UTIL_INTERNAL_GRAPH_BASE_HH
28 # define MLN_UTIL_INTERNAL_GRAPH_BASE_HH
29 
33 
34 # include <cstddef>
35 # include <algorithm>
36 # include <vector>
37 # include <set>
38 # include <iostream>
39 
40 # include <mln/core/concept/object.hh>
41 # include <mln/core/concept/graph.hh>
42 # include <mln/core/concept/proxy.hh>
43 # include <mln/core/internal/data.hh>
44 
45 # include <mln/util/edge.hh>
46 # include <mln/util/vertex.hh>
47 # include <mln/util/ord_pair.hh>
48 # include <mln/util/tracked_ptr.hh>
49 
50 
51 namespace mln
52 {
53 
54  namespace util
55  {
56 
57  /*-------------.
58  | Graph base. |
59  `-------------*/
60 
61  namespace internal
62  {
64  template<typename E>
65  class graph_base : public Graph<E>
66  {
67 
68  public:
70  typedef util::vertex<E> vertex_t;
72  typedef util::edge<E> edge_t;
73 
75  typedef std::vector<edge_id_t> vertex_data_t;
76 
78  typedef ord_pair<vertex_id_t> edge_data_t;
79 
80  public:
84  const void* id() const;
86 
90  bool has(const util::vertex<E>& v) const;
91 
93 
97  bool has(const util::edge<E>& e) const;
99 
100 
104  vertex_id_t v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const;
105 
107  bool is_valid() const;
109  void invalidate();
110 
112 
113  // FIXME: We might want to externalize this in routine of
114  // namespace mln::debug.
118  void print_debug(std::ostream& ostr) const;
119 
121  const util::tracked_ptr< mln::internal::data<E> >& data_hook_() const;
122 
123  protected:
124 
126  util::tracked_ptr< mln::internal::data<E> > data_;
127 
129  graph_base<E>();
130 
131  };
132 
133  } // end of namespace mln::util::internal
134 
135  } // End of namespace mln::util
136 
137  template <typename E>
138  bool
139  operator==(const util::internal::graph_base<E>& lhs,
140  const util::internal::graph_base<E>& rhs);
141 
142 # ifndef MLN_INCLUDE_ONLY
143 
144  namespace util
145  {
146 
147  namespace internal
148  {
149 
150  /*--------------------------------------------.
151  | Construction, assignments and destruction. |
152  `--------------------------------------------*/
153 
154  template<typename E>
155  inline
156  graph_base<E>::graph_base()
157  {
158  }
159 
160  /*-------------.
161  | Misc methods |
162  `-------------*/
163 
164  template<typename E>
165  inline
166  const void*
167  graph_base<E>::id() const
168  {
169  return static_cast<const void*>(data_.ptr_);
170  }
171 
172  /*-------------------------.
173  | Vertex and edges related |
174  `-------------------------*/
175 
176  template<typename E>
177  inline
179  graph_base<E>::v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const
180  {
181  const E *g = exact(this);
182  mln_precondition(g->has_e(id_e));
183  mln_precondition(g->has_v(id_v));
184  mln_precondition(g->v1(id_e) == id_v
185  || g->v2(id_e) == id_v);
186 
187  if (g->v1(id_e) == id_v)
188  return g->v2(id_e);
189  return g->v1(id_e);
190  }
191 
192  /*---------------.
193  | Vertex related |
194  `---------------*/
195 
196  template<typename E>
197  inline
198  bool
199  graph_base<E>::has(const util::vertex<E>& v) const
200  {
201  return exact(this)->has_v(v.id());
202  }
203 
204  /*--------------.
205  | Edges related |
206  `---------------*/
207 
208  template<typename E>
209  inline
210  bool
211  graph_base<E>::has(const util::edge<E>& e) const
212  {
213  return exact(this)->has_e(e.id());
214  }
215 
216 
217  template <typename E>
218  inline
219  bool
220  graph_base<E>::is_valid() const
221  {
222  return data_ != 0;
223  }
224 
225  template <typename E>
226  inline
227  void
228  graph_base<E>::invalidate()
229  {
230  data_.clean_();
231  }
232 
233 
234  /*--------.
235  | Debug. |
236  `--------*/
237 
238  template<typename E>
239  inline
240  void
241  graph_base<E>::print_debug (std::ostream& ostr) const
242  {
243  const E *g = exact(this);
244 
245  ostr << "graph: " << std::endl;
246  for (unsigned v = 0; v < g->v_nmax(); ++v)
247  {
248  ostr << "vertex: " << v << std::endl << " -- adjacent vertices: ";
249  for (unsigned n = 0; n < g->v_nmax_nbh_vertices(v); ++n)
250  ostr << g->v_ith_nbh_vertex(v, n) << " ";
251  ostr << std::endl;
252  }
253  ostr << std::endl;
254 
255  ostr << "edges:" << std::endl;
256  for (unsigned i = 0; i < g->e_nmax(); ++i)
257  ostr << "edge " << i << ": ("
258  << g->v1(i) << ", "
259  << g->v2(i) << " )"
260  << std::endl;
261  }
262 
263  template<typename E>
264  inline
265  const util::tracked_ptr< mln::internal::data<E> >&
266  graph_base<E>::data_hook_() const
267  {
268  return data_;
269  }
270 
271  } // end of namespace mln::util::internal
272 
273  } // end of namespace mln::util
274 
275 # endif // ! MLN_INCLUDE_ONLY
276 
277  template <typename E>
278  inline
279  bool
280  operator==(const util::internal::graph_base<E>& lhs,
281  const util::internal::graph_base<E>& rhs)
282  {
283  return lhs.id() == rhs.id();
284  }
285 
286 } // end of namespace mln
287 
288 #endif // ! MLN_UTIL_INTERNAL_GRAPH_BASE_HH