Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2008, 2009, 2010 EPITA Research and Development 00002 // Laboratory (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 00026 00027 #ifndef MLN_UTIL_VERTEX_HH 00028 # define MLN_UTIL_VERTEX_HH 00029 00030 # include <iostream> 00031 # include <mln/util/graph_ids.hh> 00032 # include <mln/util/internal/vertex_impl.hh> 00033 # include <mln/core/concept/proxy.hh> 00034 # include <mln/core/concept/site.hh> 00035 # include <mln/util/graph_ids.hh> 00036 # include <mln/util/edge.hh> 00037 00041 00042 00043 00044 namespace mln 00045 { 00046 00047 // Forward declaration. 00048 namespace util { template<typename G> class vertex; } 00049 namespace util { template<typename G> class edge; } 00050 00052 template <typename E> 00053 struct Vertex 00054 { 00055 }; 00056 00057 template <> 00058 struct Vertex<void> 00059 { 00060 typedef Site<void> super; 00061 }; 00062 00063 00064 00065 namespace util 00066 { 00067 00069 00070 template<typename G> 00071 class vertex 00072 : public Site< vertex<G> >, 00073 public internal::vertex_impl_<G> 00074 { 00075 public: 00077 typedef Vertex<void> Category; 00078 00080 typedef typename vertex_id_t::value_t id_value_t; 00081 00083 typedef vertex_id_t id_t; 00084 00086 typedef G graph_t; 00087 00090 vertex(); 00091 explicit vertex(const G& g); 00092 vertex(const G& g, const id_value_t& id); 00093 vertex(const G& g, const vertex_id_t& id); 00095 00097 bool is_valid() const; 00099 void invalidate(); 00100 00102 vertex_id_t other(const edge_id_t& id_e) const; 00103 00105 edge_id_t ith_nbh_edge(unsigned i) const; 00106 00110 unsigned nmax_nbh_edges() const; 00111 00113 vertex_id_t ith_nbh_vertex(unsigned i) const; 00114 00116 unsigned nmax_nbh_vertices() const; 00117 00119 edge<G> edge_with(const vertex<G>& v_id) const; 00120 00122 void change_graph(const G& g); 00123 00125 void update_id(const vertex_id_t& id); 00126 00128 const G& graph() const; 00129 00131 const vertex_id_t& id() const; 00132 00135 operator vertex_id_t() const; 00136 00137 protected: 00138 G g_; 00139 vertex_id_t id_; 00140 }; 00141 00142 00144 template <typename G> 00145 std::ostream& 00146 operator<<(std::ostream& ostr, const vertex<G>& v); 00147 00150 template<typename G> 00151 bool 00152 operator==(const vertex<G>& v1, const vertex<G>& v2); 00153 00154 00156 template<typename G> 00157 bool 00158 operator<(const vertex<G>& lhs, const vertex<G>& rhs); 00159 00160 00161 } // end of namespace mln::util 00162 00163 00164 00165 namespace internal 00166 { 00167 00170 00171 template <typename G, typename E> 00172 struct subject_impl< const util::vertex<G>, E > 00173 { 00174 // Can't be provided since there is an ambiguity with the iterator's 00175 // member. 00176 // 00177 // FIXME: Check the above statement again, as a naive test does 00178 // not exhibit its conclusion. 00179 // bool is_valid() const; 00180 00181 const G& graph() const; 00182 const util::vertex_id_t& id() const; 00183 00184 util::vertex_id_t other(const util::edge_id_t& id_e) const; 00185 util::edge_id_t ith_nbh_edge(unsigned i) const; 00186 unsigned nmax_nbh_edges() const; 00187 util::vertex_id_t ith_nbh_vertex(unsigned i) const; 00188 unsigned nmax_nbh_vertices() const; 00189 util::edge<G> edge_with(const util::vertex<G>& v) const; 00190 00191 private: 00192 const E& exact_() const; 00193 }; 00194 00195 template <typename G, typename E> 00196 struct subject_impl< util::vertex<G>, E > : 00197 subject_impl< const util::vertex<G>, E > 00198 { 00199 void invalidate(); 00200 void change_graph(const G& g); 00201 void update_id(const util::vertex_id_t& id); 00202 00203 private: 00204 E& exact_(); 00205 }; 00206 00208 00209 } // end of namespace mln::internal 00210 00211 } // end of namespace mln 00212 00213 00214 00215 00216 # ifndef MLN_INCLUDE_ONLY 00217 00218 namespace mln 00219 { 00220 00221 namespace util 00222 { 00223 00224 template <typename G> 00225 inline 00226 vertex<G>::vertex() 00227 { 00228 invalidate(); 00229 } 00230 00231 template <typename G> 00232 inline 00233 vertex<G>::vertex(const G& g) 00234 : g_(g) 00235 { 00236 invalidate(); 00237 } 00238 00239 template<typename G> 00240 inline 00241 vertex<G>::vertex(const G& g, const id_value_t& id) 00242 : g_(g), id_(id) 00243 { 00244 mln_assertion(is_valid()); 00245 } 00246 00247 template<typename G> 00248 inline 00249 vertex<G>::vertex(const G& g, const vertex_id_t& id) 00250 : g_(g), id_(id) 00251 { 00252 mln_assertion(is_valid()); 00253 } 00254 00255 template<typename G> 00256 inline 00257 bool 00258 vertex<G>::is_valid() const 00259 { 00260 return id_ != mln_max(unsigned) && g_.is_valid() && g_.has_v(id_); 00261 } 00262 00263 template<typename G> 00264 inline 00265 void 00266 vertex<G>::invalidate() 00267 { 00268 id_ = mln_max(unsigned); 00269 } 00270 00271 template<typename G> 00272 inline 00273 vertex_id_t 00274 vertex<G>::other(const edge_id_t& id_e) const 00275 { 00276 mln_precondition(g_.has_v(id_)); 00277 mln_precondition(g_.has_e(id_e)); 00278 mln_precondition(g_.v1(id_e) == id_ || g_.v2(id_e) == id_); 00279 return g_.v_other(id_e, id_); 00280 } 00281 00282 template<typename G> 00283 inline 00284 edge_id_t 00285 vertex<G>::ith_nbh_edge(unsigned i) const 00286 { 00287 mln_precondition(g_.has_v(id_)); 00288 return g_.v_ith_nbh_edge(id_, i); 00289 } 00290 00291 template<typename G> 00292 inline 00293 unsigned 00294 vertex<G>::nmax_nbh_edges() const 00295 { 00296 mln_precondition(g_.has_v(id_)); 00297 return g_.v_nmax_nbh_edges(id_); 00298 } 00299 00300 template<typename G> 00301 inline 00302 vertex_id_t 00303 vertex<G>::ith_nbh_vertex(unsigned i) const 00304 { 00305 mln_precondition(g_.has_v(id_)); 00306 return g_.v_ith_nbh_vertex(id_, i); 00307 } 00308 00309 template<typename G> 00310 inline 00311 unsigned 00312 vertex<G>::nmax_nbh_vertices() const 00313 { 00314 mln_precondition(g_.has_v(id_)); 00315 return g_.v_nmax_nbh_vertices(id_); 00316 } 00317 00318 template<typename G> 00319 inline 00320 edge<G> 00321 vertex<G>::edge_with(const vertex<G>& v) const 00322 { 00323 mln_precondition(g_.has_v(id_)); 00324 mln_precondition(g_.has_v(v)); 00325 return g_.edge(*this, v); 00326 } 00327 00328 template<typename G> 00329 inline 00330 void 00331 vertex<G>::change_graph(const G& g) 00332 { 00333 mln_precondition(g.is_valid()); 00334 g_ = g; 00335 } 00336 00337 template<typename G> 00338 inline 00339 void 00340 vertex<G>::update_id(const vertex_id_t& id) 00341 { 00342 id_ = id; 00343 } 00344 00345 template<typename G> 00346 inline 00347 const G& 00348 vertex<G>::graph() const 00349 { 00350 return g_; 00351 } 00352 00353 template<typename G> 00354 inline 00355 const vertex_id_t& 00356 vertex<G>::id() const 00357 { 00358 return id_; 00359 } 00360 00361 template<typename G> 00362 inline 00363 vertex<G>::operator vertex_id_t() const 00364 { 00365 return id_; 00366 } 00367 00368 00369 template <typename G> 00370 inline 00371 std::ostream& 00372 operator<<(std::ostream& ostr, const vertex<G>& v) 00373 { 00374 return ostr << v.id(); 00375 } 00376 00377 template<typename G> 00378 inline 00379 bool 00380 operator==(const vertex<G>& v1, const vertex<G>& v2) 00381 { 00382 return v1.id() == v2.id() 00383 && (v1.graph().is_subgraph_of(v2.graph()) 00384 || v2.graph().is_subgraph_of(v1.graph())); 00385 } 00386 00387 template<typename G> 00388 inline 00389 bool 00390 operator<(const vertex<G>& lhs, const vertex<G>& rhs) 00391 { 00392 return lhs.id() < rhs.id(); 00393 } 00394 00395 } // end of namespace mln::util 00396 00397 00398 namespace internal 00399 { 00400 00401 /*-------------------------------------------. 00402 | subject_impl< const util::vertex<G>, E >. | 00403 `-------------------------------------------*/ 00404 00405 template <typename G, typename E> 00406 inline 00407 const E& 00408 subject_impl< const util::vertex<G>, E >::exact_() const 00409 { 00410 return internal::force_exact<const E>(*this); 00411 } 00412 00413 template <typename G, typename E> 00414 inline 00415 const G& 00416 subject_impl< const util::vertex<G>, E >::graph() const 00417 { 00418 return exact_().get_subject().graph(); 00419 } 00420 00421 template <typename G, typename E> 00422 inline 00423 const util::vertex_id_t& 00424 subject_impl< const util::vertex<G>, E >::id() const 00425 { 00426 return exact_().get_subject().id(); 00427 }; 00428 00429 00430 00431 template <typename G, typename E> 00432 inline 00433 util::vertex_id_t 00434 subject_impl< const util::vertex<G>, E >::other(const util::edge_id_t& id_e) const 00435 { 00436 return exact_().get_subject().other(id_e); 00437 } 00438 00439 template <typename G, typename E> 00440 inline 00441 util::edge_id_t 00442 subject_impl< const util::vertex<G>, E >::ith_nbh_edge(unsigned i) const 00443 { 00444 return exact_().get_subject().ith_nbh_edge(i); 00445 } 00446 00447 template <typename G, typename E> 00448 inline 00449 unsigned 00450 subject_impl< const util::vertex<G>, E >::nmax_nbh_edges() const 00451 { 00452 return exact_().get_subject().nmax_nbh_edges(); 00453 } 00454 00455 template <typename G, typename E> 00456 inline 00457 util::vertex_id_t 00458 subject_impl< const util::vertex<G>, E >::ith_nbh_vertex(unsigned i) const 00459 { 00460 return exact_().get_subject().ith_nbh_vertex(i); 00461 } 00462 00463 template <typename G, typename E> 00464 inline 00465 unsigned 00466 subject_impl< const util::vertex<G>, E >::nmax_nbh_vertices() const 00467 { 00468 return exact_().get_subject().nmax_nbh_vertices(); 00469 } 00470 00471 template <typename G, typename E> 00472 inline 00473 util::edge<G> 00474 subject_impl< const util::vertex<G>, E >::edge_with(const util::vertex<G>& v) const 00475 { 00476 return exact_().get_subject().edge_with(v); 00477 } 00478 00479 00480 /*-------------------------------------. 00481 | subject_impl< util::vertex<G>, E >. | 00482 `-------------------------------------*/ 00483 00484 template <typename G, typename E> 00485 inline 00486 E& 00487 subject_impl< util::vertex<G>, E >::exact_() 00488 { 00489 return internal::force_exact<E>(*this); 00490 } 00491 00492 template <typename G, typename E> 00493 inline 00494 void 00495 subject_impl< util::vertex<G>, E >::invalidate() 00496 { 00497 exact_().get_subject().invalidate(); 00498 } 00499 00500 template <typename G, typename E> 00501 inline 00502 void 00503 subject_impl< util::vertex<G>, E >::change_graph(const G& g) 00504 { 00505 exact_().get_subject().change_graph(g); 00506 } 00507 00508 template <typename G, typename E> 00509 inline 00510 void 00511 subject_impl< util::vertex<G>, E >::update_id(const util::vertex_id_t& id) 00512 { 00513 exact_().get_subject().update_id(id); 00514 }; 00515 00516 } // end of namespace mln::internal 00517 00518 } // end of namespace mln 00519 00520 # endif // ! MLN_INCLUDE_ONLY 00521 00522 00523 #endif // ! MLN_UTIL_VERTEX_HH