00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
00114
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 }
00134
00135 }
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
00152
00153
00154 template<typename E>
00155 inline
00156 graph_base<E>::graph_base()
00157 {
00158 }
00159
00160
00161
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
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
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
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
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 }
00272
00273 }
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 }
00287
00288 #endif // ! MLN_UTIL_INTERNAL_GRAPH_BASE_HH