• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

graph_base.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_UTIL_INTERNAL_GRAPH_BASE_HH
00027 # define MLN_UTIL_INTERNAL_GRAPH_BASE_HH
00028 
00032 
00033 # include <cstddef>
00034 # include <algorithm>
00035 # include <vector>
00036 # include <set>
00037 # include <iostream>
00038 
00039 # include <mln/core/concept/object.hh>
00040 # include <mln/core/concept/graph.hh>
00041 # include <mln/core/concept/proxy.hh>
00042 # include <mln/core/internal/data.hh>
00043 
00044 # include <mln/util/edge.hh>
00045 # include <mln/util/vertex.hh>
00046 # include <mln/util/ord_pair.hh>
00047 # include <mln/util/tracked_ptr.hh>
00048 
00049 
00050 namespace mln
00051 {
00052 
00053   namespace util
00054   {
00055 
00056     /*-------------.
00057     | Graph base.  |
00058     `-------------*/
00059 
00060     namespace internal
00061     {
00063       template<typename E>
00064       class graph_base : public Graph<E>
00065       {
00066 
00067       protected:
00069         typedef util::vertex<E> vertex_t;
00071         typedef util::edge<E> edge_t;
00072 
00074         typedef std::vector<edge_id_t> vertex_data_t;
00075 
00077         typedef ord_pair<vertex_id_t> edge_data_t;
00078 
00079       public:
00083         const void* id() const;
00085 
00089         bool has(const util::vertex<E>& v) const;
00090 
00092 
00096         bool has(const util::edge<E>& e) const;
00098 
00099 
00103         vertex_id_t v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const;
00105 
00106         // FIXME: We might want to externalize this in routine of
00107         // namespace mln::debug.
00111         void print_debug(std::ostream& ostr) const;
00112 
00113 
00114 
00115       protected:
00116 
00118         util::tracked_ptr< mln::internal::data<E> > data_;
00119 
00121         graph_base<E>();
00122 
00123       };
00124 
00125     } // end of namespace mln::util::internal
00126 
00127   } // End of namespace mln::util
00128 
00129   template <typename E>
00130   bool
00131   operator==(const util::internal::graph_base<E>& lhs,
00132              const util::internal::graph_base<E>& rhs);
00133 
00134 # ifndef MLN_INCLUDE_ONLY
00135 
00136   namespace util
00137   {
00138 
00139     namespace internal
00140     {
00141 
00142       /*--------------------------------------------.
00143       | Construction, assignments and destruction.  |
00144       `--------------------------------------------*/
00145 
00146       template<typename E>
00147       inline
00148       graph_base<E>::graph_base()
00149       {
00150       }
00151 
00152       /*-------------.
00153       | Misc methods |
00154       `-------------*/
00155 
00156       template<typename E>
00157       inline
00158       const void*
00159       graph_base<E>::id() const
00160       {
00161         return static_cast<const void*>(data_.ptr_);
00162       }
00163 
00164       /*-------------------------.
00165       | Vertex and edges related |
00166       `-------------------------*/
00167 
00168       template<typename E>
00169       inline
00170       vertex_id_t
00171       graph_base<E>::v_other(const edge_id_t& id_e, const vertex_id_t& id_v) const
00172       {
00173         const E *g = exact(this);
00174         mln_precondition(g->has_e(id_e));
00175         mln_precondition(g->has_v(id_v));
00176         mln_precondition(g->v1(id_e) == id_v
00177             || g->v2(id_e) == id_v);
00178 
00179         if (g->v1(id_e) == id_v)
00180           return g->v2(id_e);
00181         return g->v1(id_e);
00182       }
00183 
00184       /*---------------.
00185       | Vertex related |
00186       `---------------*/
00187 
00188       template<typename E>
00189       inline
00190       bool
00191       graph_base<E>::has(const util::vertex<E>& v) const
00192       {
00193         return exact(this)->has_v(v.id());
00194       }
00195 
00196       /*--------------.
00197       | Edges related |
00198       `---------------*/
00199 
00200       template<typename E>
00201       inline
00202       bool
00203       graph_base<E>::has(const util::edge<E>& e) const
00204       {
00205         return exact(this)->has_e(e.id());
00206       }
00207 
00208       /*--------.
00209       | Debug.  |
00210       `--------*/
00211 
00212       template<typename E>
00213       inline
00214       void
00215       graph_base<E>::print_debug (std::ostream& ostr) const
00216       {
00217         const E *g = exact(this);
00218 
00219         ostr << "graph: "       << std::endl;
00220         for (unsigned v = 0; v < g->v_nmax(); ++v)
00221           {
00222             ostr << "vertex: " << v << std::endl << " -- adjacent vertices: ";
00223             for (unsigned n = 0; n < g->v_nmax_nbh_vertices(v); ++n)
00224               ostr << g->v_ith_nbh_vertex(v, n) << " ";
00225             ostr << std::endl;
00226           }
00227         ostr << std::endl;
00228 
00229         ostr << "edges:" << std::endl;
00230         for (unsigned i = 0; i < g->e_nmax(); ++i)
00231           ostr << "edge " << i << ": ("
00232                << g->v1(i) << ", "
00233                << g->v2(i) << " )"
00234                << std::endl;
00235       }
00236 
00237     } // end of namespace mln::util::internal
00238 
00239   } // end of namespace mln::util
00240 
00241 # endif // ! MLN_INCLUDE_ONLY
00242 
00243   template <typename E>
00244   inline
00245   bool
00246   operator==(const util::internal::graph_base<E>& lhs,
00247              const util::internal::graph_base<E>& rhs)
00248   {
00249     return lhs.id() == rhs.id();
00250   }
00251 
00252 } // end of namespace mln
00253 
00254 #endif // ! MLN_UTIL_INTERNAL_GRAPH_BASE_HH

Generated on Thu Sep 8 2011 18:31:52 for Milena (Olena) by  doxygen 1.7.1