Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2007, 2008, 2009 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_INTERNAL_GRAPH_BASE_HH 00028 # define MLN_UTIL_INTERNAL_GRAPH_BASE_HH 00029 00033 00034 # include <cstddef> 00035 # include <algorithm> 00036 # include <vector> 00037 # include <set> 00038 # include <iostream> 00039 00040 # include <mln/core/concept/object.hh> 00041 # include <mln/core/concept/graph.hh> 00042 # include <mln/core/concept/proxy.hh> 00043 # include <mln/core/internal/data.hh> 00044 00045 # include <mln/util/edge.hh> 00046 # include <mln/util/vertex.hh> 00047 # include <mln/util/ord_pair.hh> 00048 # include <mln/util/tracked_ptr.hh> 00049 00050 00051 namespace mln 00052 { 00053 00054 namespace util 00055 { 00056 00057 /*-------------. 00058 | Graph base. | 00059 `-------------*/ 00060 00061 namespace internal 00062 { 00064 template<typename E> 00065 class graph_base : public Graph<E> 00066 { 00067 00068 public: 00070 typedef util::vertex<E> vertex_t; 00072 typedef util::edge<E> edge_t; 00073 00075 typedef std::vector<edge_id_t> vertex_data_t; 00076 00078 typedef ord_pair<vertex_id_t> edge_data_t; 00079 00080 public: 00084 const void* id() const; 00086 00090 bool has(const util::vertex<E>& v) const; 00091 00093 00097 bool has(const util::edge<E>& e) const; 00099 00100 00104 vertex_id_t v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const; 00105 00107 bool is_valid() const; 00109 void invalidate(); 00110 00112 00113 // FIXME: We might want to externalize this in routine of 00114 // namespace mln::debug. 00118 void print_debug(std::ostream& ostr) const; 00119 00121 const util::tracked_ptr< mln::internal::data<E> >& data_hook_() const; 00122 00123 protected: 00124 00126 util::tracked_ptr< mln::internal::data<E> > data_; 00127 00129 graph_base<E>(); 00130 00131 }; 00132 00133 } // end of namespace mln::util::internal 00134 00135 } // End of namespace mln::util 00136 00137 template <typename E> 00138 bool 00139 operator==(const util::internal::graph_base<E>& lhs, 00140 const util::internal::graph_base<E>& rhs); 00141 00142 # ifndef MLN_INCLUDE_ONLY 00143 00144 namespace util 00145 { 00146 00147 namespace internal 00148 { 00149 00150 /*--------------------------------------------. 00151 | Construction, assignments and destruction. | 00152 `--------------------------------------------*/ 00153 00154 template<typename E> 00155 inline 00156 graph_base<E>::graph_base() 00157 { 00158 } 00159 00160 /*-------------. 00161 | Misc methods | 00162 `-------------*/ 00163 00164 template<typename E> 00165 inline 00166 const void* 00167 graph_base<E>::id() const 00168 { 00169 return static_cast<const void*>(data_.ptr_); 00170 } 00171 00172 /*-------------------------. 00173 | Vertex and edges related | 00174 `-------------------------*/ 00175 00176 template<typename E> 00177 inline 00178 vertex_id_t 00179 graph_base<E>::v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const 00180 { 00181 const E *g = exact(this); 00182 mln_precondition(g->has_e(id_e)); 00183 mln_precondition(g->has_v(id_v)); 00184 mln_precondition(g->v1(id_e) == id_v 00185 || g->v2(id_e) == id_v); 00186 00187 if (g->v1(id_e) == id_v) 00188 return g->v2(id_e); 00189 return g->v1(id_e); 00190 } 00191 00192 /*---------------. 00193 | Vertex related | 00194 `---------------*/ 00195 00196 template<typename E> 00197 inline 00198 bool 00199 graph_base<E>::has(const util::vertex<E>& v) const 00200 { 00201 return exact(this)->has_v(v.id()); 00202 } 00203 00204 /*--------------. 00205 | Edges related | 00206 `---------------*/ 00207 00208 template<typename E> 00209 inline 00210 bool 00211 graph_base<E>::has(const util::edge<E>& e) const 00212 { 00213 return exact(this)->has_e(e.id()); 00214 } 00215 00216 00217 template <typename E> 00218 inline 00219 bool 00220 graph_base<E>::is_valid() const 00221 { 00222 return data_ != 0; 00223 } 00224 00225 template <typename E> 00226 inline 00227 void 00228 graph_base<E>::invalidate() 00229 { 00230 data_.clean_(); 00231 } 00232 00233 00234 /*--------. 00235 | Debug. | 00236 `--------*/ 00237 00238 template<typename E> 00239 inline 00240 void 00241 graph_base<E>::print_debug (std::ostream& ostr) const 00242 { 00243 const E *g = exact(this); 00244 00245 ostr << "graph: " << std::endl; 00246 for (unsigned v = 0; v < g->v_nmax(); ++v) 00247 { 00248 ostr << "vertex: " << v << std::endl << " -- adjacent vertices: "; 00249 for (unsigned n = 0; n < g->v_nmax_nbh_vertices(v); ++n) 00250 ostr << g->v_ith_nbh_vertex(v, n) << " "; 00251 ostr << std::endl; 00252 } 00253 ostr << std::endl; 00254 00255 ostr << "edges:" << std::endl; 00256 for (unsigned i = 0; i < g->e_nmax(); ++i) 00257 ostr << "edge " << i << ": (" 00258 << g->v1(i) << ", " 00259 << g->v2(i) << " )" 00260 << std::endl; 00261 } 00262 00263 template<typename E> 00264 inline 00265 const util::tracked_ptr< mln::internal::data<E> >& 00266 graph_base<E>::data_hook_() const 00267 { 00268 return data_; 00269 } 00270 00271 } // end of namespace mln::util::internal 00272 00273 } // end of namespace mln::util 00274 00275 # endif // ! MLN_INCLUDE_ONLY 00276 00277 template <typename E> 00278 inline 00279 bool 00280 operator==(const util::internal::graph_base<E>& lhs, 00281 const util::internal::graph_base<E>& rhs) 00282 { 00283 return lhs.id() == rhs.id(); 00284 } 00285 00286 } // end of namespace mln 00287 00288 #endif // ! MLN_UTIL_INTERNAL_GRAPH_BASE_HH