27 #ifndef MLN_UTIL_GRAPH_HH
28 # define MLN_UTIL_GRAPH_HH
34 # include <mln/util/internal/graph_base.hh>
35 # include <mln/util/internal/graph_iter.hh>
36 # include <mln/util/internal/graph_nbh_iter.hh>
37 # include <mln/util/vertex.hh>
38 # include <mln/util/edge.hh>
39 # include <mln/util/ord_pair.hh>
45 namespace util {
class graph; }
53 struct data<util::graph>
55 typedef util::graph G;
56 typedef std::vector<std::vector<util::edge_id_t> > vertices_t;
57 typedef std::vector<util::ord_pair<util::vertex_id_t> > edges_t;
58 typedef std::set<util::ord_pair<util::vertex_id_t> > edges_set_t;
63 data(
unsigned nvertices);
73 edges_set_t edges_set_;
87 class graph :
public internal::graph_base<graph>
90 typedef internal::graph_base<graph> super;
92 typedef super::vertex_data_t vertex_data_t;
93 typedef super::edge_data_t edge_data_t;
108 typedef mln::internal::vertex_fwd_iterator<graph> vertex_fwd_iter;
109 typedef mln::internal::vertex_bkd_iterator<graph> vertex_bkd_iter;
110 typedef vertex_fwd_iter vertex_iter;
116 typedef mln::internal::vertex_nbh_edge_bkd_iterator<graph> vertex_nbh_edge_bkd_iter;
123 typedef mln::internal::vertex_nbh_vertex_bkd_iterator<graph> vertex_nbh_vertex_bkd_iter;
130 typedef mln::internal::edge_bkd_iterator<graph> edge_bkd_iter;
137 typedef mln::internal::edge_nbh_edge_bkd_iterator<graph> edge_nbh_edge_bkd_iter;
145 graph(
unsigned nvertices);
161 std::pair<vertex_id_t, vertex_id_t>
add_vertices(
unsigned n);
178 size_t v_nmax_nbh_edges(
const vertex_id_t& id_v)
const;
186 size_t v_nmax_nbh_vertices(
const vertex_id_t& id_v)
const;
208 const std::vector<util::ord_pair<vertex_id_t> >&
edges()
const;
220 edge_t
edge(
const vertex_t&
v1,
const vertex_t&
v2)
const;
229 size_t e_nmax_nbh_edges(
const edge_id_t& id_e)
const;
236 template <
typename G2>
243 operator<<(std::ostream& ostr,
const graph& g);
252 # ifndef MLN_INCLUDE_ONLY
268 vertices_.resize(nvertices);
284 this->data_ =
new mln::internal::data<util::graph>();
290 this->data_ =
new mln::internal::data<util::graph>(nvertices);
303 data_->vertices_.resize(data_->vertices_.size() + 1);
309 std::pair<vertex_id_t, vertex_id_t>
314 data_->vertices_.resize(data_->vertices_.size() + n);
316 return std::make_pair(
v_nmax() - n,
324 mln_assertion(
has_v(id_v));
325 return vertex_t(*
this, id_v);
333 return data_->vertices_.size();
340 return id_v < data_->vertices_.size();
345 graph::v_nmax_nbh_edges(
const vertex_id_t& id_v)
const
347 mln_precondition(
has_v(id_v));
348 return data_->vertices_[id_v].size();
355 mln_precondition(
has_v(id_v));
356 if (i >= v_nmax_nbh_edges(id_v))
358 return data_->vertices_[id_v][i];
363 graph::v_nmax_nbh_vertices(
const vertex_id_t& id_v)
const
365 mln_precondition(
has_v(id_v));
366 return v_nmax_nbh_edges(id_v);
373 mln_precondition(
has_v(id_v));
376 return v_other(id_e, id_v);
388 mln_precondition(id_v1 != id_v2);
389 mln_precondition(
has_v(id_v1));
390 mln_precondition(
has_v(id_v2));
393 edge_data_t
edge(id_v1, id_v2);
397 if (data_->edges_set_.find(edge) != data_->edges_set_.end ())
408 data_->edges_.push_back(edge);
413 data_->edges_set_.insert(edge);
415 data_->vertices_[edge.first()].push_back(
id);
416 data_->vertices_[edge.second()].push_back(
id);
427 const std::vector<util::ord_pair<vertex_id_t> >&
430 return this->data_->edges_;
437 mln_assertion(e <
e_nmax());
438 return edge_t(*
this, e);
445 return data_->edges_.size();
452 return id_e < data_->edges_.size();
459 mln_precondition(
has_v(v1));
460 mln_precondition(
has_v(v2));
467 std::swap(id_v1, id_v2);
469 for (
unsigned i = 0; i < data_->vertices_[id_v1].size(); ++i)
470 if (data_->edges_[data_->vertices_[id_v1][i]].second() == id_v2)
471 return edge_t(*
this, data_->vertices_[id_v1][i]);
481 mln_precondition(
has_e(id_e));
482 return data_->edges_[id_e].first();
489 mln_precondition(
has_e(id_e));
490 return data_->edges_[id_e].second();
495 graph::e_nmax_nbh_edges(
const edge_id_t& id_e)
const
497 mln_precondition(
has_e(id_e));
498 return v_nmax_nbh_edges(
v1(id_e)) + v_nmax_nbh_edges(
v2(id_e));
505 mln_precondition(
has_e(id_e));
506 if (i >= e_nmax_nbh_edges(id_e))
509 unsigned v1_nmax = v_nmax_nbh_edges(
v1(id_e));
516 template <
typename G2>
521 return g.id() == this->id();
527 operator<<(std::ostream& ostr,
const graph& g)
529 mln_vertex_iter_(
graph) v(g);
530 mln_vertex_nbh_edge_iter_(
graph) e(v);
533 ostr <<
"v(" << v <<
") : ";
546 # endif // ! MLN_INCLUDE_ONLY
549 #endif // ! MLN_UTIL_GRAPH_HH