williamr@2: //======================================================================= williamr@2: // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. williamr@2: // Copyright 2004 The Trustees of Indiana University. williamr@2: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Douglas Gregor williamr@2: // williamr@2: // Distributed under the Boost Software License, Version 1.0. (See williamr@2: // accompanying file LICENSE_1_0.txt or copy at williamr@2: // http://www.boost.org/LICENSE_1_0.txt) williamr@2: //======================================================================= williamr@2: #ifndef BOOST_GRAPH_LEDA_HPP williamr@2: #define BOOST_GRAPH_LEDA_HPP williamr@2: williamr@2: #include <boost/config.hpp> williamr@2: #include <boost/iterator/iterator_facade.hpp> williamr@2: #include <boost/graph/graph_traits.hpp> williamr@2: #include <boost/graph/properties.hpp> williamr@2: williamr@2: #include <LEDA/graph.h> williamr@2: #include <LEDA/node_array.h> williamr@2: #include <LEDA/node_map.h> williamr@2: williamr@2: // The functions and classes in this file allows the user to williamr@2: // treat a LEDA GRAPH object as a boost graph "as is". No williamr@2: // wrapper is needed for the GRAPH object. williamr@2: williamr@2: // Remember to define LEDA_PREFIX so that LEDA types such as williamr@2: // leda_edge show up as "leda_edge" and not just "edge". williamr@2: williamr@2: // Warning: this implementation relies on partial specialization williamr@2: // for the graph_traits class (so it won't compile with Visual C++) williamr@2: williamr@2: // Warning: this implementation is in alpha and has not been tested williamr@2: williamr@2: #if !defined BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION williamr@2: namespace boost { williamr@2: williamr@2: struct leda_graph_traversal_category : williamr@2: public virtual bidirectional_graph_tag, williamr@2: public virtual adjacency_graph_tag, williamr@2: public virtual vertex_list_graph_tag { }; williamr@2: williamr@2: template <class vtype, class etype> williamr@2: struct graph_traits< leda::GRAPH<vtype,etype> > { williamr@2: typedef leda_node vertex_descriptor; williamr@2: typedef leda_edge edge_descriptor; williamr@2: williamr@2: class adjacency_iterator williamr@2: : public iterator_facade<adjacency_iterator, williamr@2: leda_node, williamr@2: bidirectional_traversal_tag, williamr@2: leda_node, williamr@2: const leda_node*> williamr@2: { williamr@2: public: williamr@2: explicit adjacency_iterator(leda_edge edge = 0) : base(edge) {} williamr@2: williamr@2: private: williamr@2: leda_node dereference() const { return leda::target(base); } williamr@2: williamr@2: bool equal(const adjacency_iterator& other) const williamr@2: { return base == other.base; } williamr@2: williamr@2: void increment() { base = Succ_Adj_Edge(base, 0); } williamr@2: void decrement() { base = Pred_Adj_Edge(base, 0); } williamr@2: williamr@2: leda_edge base; williamr@2: williamr@2: friend class iterator_core_access; williamr@2: }; williamr@2: williamr@2: class out_edge_iterator williamr@2: : public iterator_facade<out_edge_iterator, williamr@2: leda_edge, williamr@2: bidirectional_traversal_tag, williamr@2: const leda_edge&, williamr@2: const leda_edge*> williamr@2: { williamr@2: public: williamr@2: explicit out_edge_iterator(leda_edge edge = 0) : base(edge) {} williamr@2: williamr@2: private: williamr@2: const leda_edge& dereference() const { return base; } williamr@2: williamr@2: bool equal(const out_edge_iterator& other) const williamr@2: { return base == other.base; } williamr@2: williamr@2: void increment() { base = Succ_Adj_Edge(base, 0); } williamr@2: void decrement() { base = Pred_Adj_Edge(base, 0); } williamr@2: williamr@2: leda_edge base; williamr@2: williamr@2: friend class iterator_core_access; williamr@2: }; williamr@2: williamr@2: class in_edge_iterator williamr@2: : public iterator_facade<in_edge_iterator, williamr@2: leda_edge, williamr@2: bidirectional_traversal_tag, williamr@2: const leda_edge&, williamr@2: const leda_edge*> williamr@2: { williamr@2: public: williamr@2: explicit in_edge_iterator(leda_edge edge = 0) : base(edge) {} williamr@2: williamr@2: private: williamr@2: const leda_edge& dereference() const { return base; } williamr@2: williamr@2: bool equal(const in_edge_iterator& other) const williamr@2: { return base == other.base; } williamr@2: williamr@2: void increment() { base = Succ_Adj_Edge(base, 1); } williamr@2: void decrement() { base = Pred_Adj_Edge(base, 1); } williamr@2: williamr@2: leda_edge base; williamr@2: williamr@2: friend class iterator_core_access; williamr@2: }; williamr@2: williamr@2: class vertex_iterator williamr@2: : public iterator_facade<vertex_iterator, williamr@2: leda_node, williamr@2: bidirectional_traversal_tag, williamr@2: const leda_node&, williamr@2: const leda_node*> williamr@2: { williamr@2: public: williamr@2: vertex_iterator(leda_node node = 0, williamr@2: const leda::GRAPH<vtype, etype>* g = 0) williamr@2: : base(node), g(g) {} williamr@2: williamr@2: private: williamr@2: const leda_node& dereference() const { return base; } williamr@2: williamr@2: bool equal(const vertex_iterator& other) const williamr@2: { return base == other.base; } williamr@2: williamr@2: void increment() { base = g->succ_node(base); } williamr@2: void decrement() { base = g->pred_node(base); } williamr@2: williamr@2: leda_node base; williamr@2: const leda::GRAPH<vtype, etype>* g; williamr@2: williamr@2: friend class iterator_core_access; williamr@2: }; williamr@2: williamr@2: typedef directed_tag directed_category; williamr@2: typedef allow_parallel_edge_tag edge_parallel_category; // not sure here williamr@2: typedef leda_graph_traversal_category traversal_category; williamr@2: typedef int vertices_size_type; williamr@2: typedef int edges_size_type; williamr@2: typedef int degree_size_type; williamr@2: }; williamr@2: williamr@2: template <class vtype, class etype> williamr@2: struct vertex_property< leda::GRAPH<vtype,etype> > { williamr@2: typedef vtype type; williamr@2: }; williamr@2: williamr@2: template <class vtype, class etype> williamr@2: struct edge_property< leda::GRAPH<vtype,etype> > { williamr@2: typedef etype type; williamr@2: }; williamr@2: williamr@2: } // namespace boost williamr@2: #endif williamr@2: williamr@2: namespace boost { williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor williamr@2: source(typename graph_traits< leda::GRAPH<vtype,etype> >::edge_descriptor e, williamr@2: const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return source(e); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor williamr@2: target(typename graph_traits< leda::GRAPH<vtype,etype> >::edge_descriptor e, williamr@2: const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return target(e); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: inline std::pair< williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_iterator, williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_iterator > williamr@2: vertices(const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: typedef typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_iterator williamr@2: Iter; williamr@2: return std::make_pair( Iter(g.first_node(),&g), Iter(0,&g) ); williamr@2: } williamr@2: williamr@2: // no edges(g) function williamr@2: williamr@2: template <class vtype, class etype> williamr@2: inline std::pair< williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::out_edge_iterator, williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::out_edge_iterator > williamr@2: out_edges( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: typedef typename graph_traits< leda::GRAPH<vtype,etype> > williamr@2: ::out_edge_iterator Iter; williamr@2: return std::make_pair( Iter(First_Adj_Edge(u,0)), Iter(0) ); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: inline std::pair< williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::in_edge_iterator, williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::in_edge_iterator > williamr@2: in_edges( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: typedef typename graph_traits< leda::GRAPH<vtype,etype> > williamr@2: ::in_edge_iterator Iter; williamr@2: return std::make_pair( Iter(First_Adj_Edge(u,1)), Iter(0) ); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: inline std::pair< williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::adjacency_iterator, williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::adjacency_iterator > williamr@2: adjacent_vertices( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: typedef typename graph_traits< leda::GRAPH<vtype,etype> > williamr@2: ::adjacency_iterator Iter; williamr@2: return std::make_pair( Iter(First_Adj_Edge(u,0)), Iter(0) ); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertices_size_type williamr@2: num_vertices(const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return g.number_of_nodes(); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::edges_size_type williamr@2: num_edges(const leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return g.number_of_edges(); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::degree_size_type williamr@2: out_degree( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: const leda::GRAPH<vtype,etype>&) williamr@2: { williamr@2: return outdeg(u); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::degree_size_type williamr@2: in_degree( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: const leda::GRAPH<vtype,etype>&) williamr@2: { williamr@2: return indeg(u); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::degree_size_type williamr@2: degree( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: const leda::GRAPH<vtype,etype>&) williamr@2: { williamr@2: return outdeg(u) + indeg(u); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor williamr@2: add_vertex(leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return g.new_node(); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor williamr@2: add_vertex(const vtype& vp, leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return g.new_node(vp); williamr@2: } williamr@2: williamr@2: // Hmm, LEDA doesn't have the equivalent of clear_vertex() -JGS williamr@2: // need to write an implementation williamr@2: template <class vtype, class etype> williamr@2: void clear_vertex( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: g.del_node(u); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: void remove_vertex( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: g.del_node(u); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: std::pair< williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::edge_descriptor, williamr@2: bool> williamr@2: add_edge( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor v, williamr@2: leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return std::make_pair(g.new_edge(u, v), true); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: std::pair< williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::edge_descriptor, williamr@2: bool> williamr@2: add_edge( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor v, williamr@2: const etype& et, williamr@2: leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: return std::make_pair(g.new_edge(u, v, et), true); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: void williamr@2: remove_edge( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor u, williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::vertex_descriptor v, williamr@2: leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::out_edge_iterator williamr@2: i,iend; williamr@2: for (boost::tie(i,iend) = out_edges(u,g); i != iend; ++i) williamr@2: if (target(*i,g) == v) williamr@2: g.del_edge(*i); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype> williamr@2: void williamr@2: remove_edge( williamr@2: typename graph_traits< leda::GRAPH<vtype,etype> >::edge_descriptor e, williamr@2: leda::GRAPH<vtype,etype>& g) williamr@2: { williamr@2: g.del_edge(e); williamr@2: } williamr@2: williamr@2: //=========================================================================== williamr@2: // property maps williamr@2: williamr@2: class leda_graph_id_map williamr@2: : public put_get_helper<int, leda_graph_id_map> williamr@2: { williamr@2: public: williamr@2: typedef readable_property_map_tag category; williamr@2: typedef int value_type; williamr@2: typedef int reference; williamr@2: typedef leda_node key_type; williamr@2: leda_graph_id_map() { } williamr@2: template <class T> williamr@2: long operator[](T x) const { return x->id(); } williamr@2: }; williamr@2: template <class vtype, class etype> williamr@2: inline leda_graph_id_map williamr@2: get(vertex_index_t, const leda::GRAPH<vtype, etype>& g) { williamr@2: return leda_graph_id_map(); williamr@2: } williamr@2: template <class vtype, class etype> williamr@2: inline leda_graph_id_map williamr@2: get(edge_index_t, const leda::GRAPH<vtype, etype>& g) { williamr@2: return leda_graph_id_map(); williamr@2: } williamr@2: williamr@2: template <class Tag> williamr@2: struct leda_property_map { }; williamr@2: williamr@2: template <> williamr@2: struct leda_property_map<vertex_index_t> { williamr@2: template <class vtype, class etype> williamr@2: struct bind_ { williamr@2: typedef leda_graph_id_map type; williamr@2: typedef leda_graph_id_map const_type; williamr@2: }; williamr@2: }; williamr@2: template <> williamr@2: struct leda_property_map<edge_index_t> { williamr@2: template <class vtype, class etype> williamr@2: struct bind_ { williamr@2: typedef leda_graph_id_map type; williamr@2: typedef leda_graph_id_map const_type; williamr@2: }; williamr@2: }; williamr@2: williamr@2: williamr@2: template <class Data, class DataRef, class GraphPtr> williamr@2: class leda_graph_data_map williamr@2: : public put_get_helper<DataRef, williamr@2: leda_graph_data_map<Data,DataRef,GraphPtr> > williamr@2: { williamr@2: public: williamr@2: typedef Data value_type; williamr@2: typedef DataRef reference; williamr@2: typedef void key_type; williamr@2: typedef lvalue_property_map_tag category; williamr@2: leda_graph_data_map(GraphPtr g) : m_g(g) { } williamr@2: template <class NodeOrEdge> williamr@2: DataRef operator[](NodeOrEdge x) const { return (*m_g)[x]; } williamr@2: protected: williamr@2: GraphPtr m_g; williamr@2: }; williamr@2: williamr@2: template <> williamr@2: struct leda_property_map<vertex_all_t> { williamr@2: template <class vtype, class etype> williamr@2: struct bind_ { williamr@2: typedef leda_graph_data_map<vtype, vtype&, leda::GRAPH<vtype, etype>*> type; williamr@2: typedef leda_graph_data_map<vtype, const vtype&, williamr@2: const leda::GRAPH<vtype, etype>*> const_type; williamr@2: }; williamr@2: }; williamr@2: template <class vtype, class etype > williamr@2: inline typename property_map< leda::GRAPH<vtype, etype>, vertex_all_t>::type williamr@2: get(vertex_all_t, leda::GRAPH<vtype, etype>& g) { williamr@2: typedef typename property_map< leda::GRAPH<vtype, etype>, vertex_all_t>::type williamr@2: pmap_type; williamr@2: return pmap_type(&g); williamr@2: } williamr@2: template <class vtype, class etype > williamr@2: inline typename property_map< leda::GRAPH<vtype, etype>, vertex_all_t>::const_type williamr@2: get(vertex_all_t, const leda::GRAPH<vtype, etype>& g) { williamr@2: typedef typename property_map< leda::GRAPH<vtype, etype>, williamr@2: vertex_all_t>::const_type pmap_type; williamr@2: return pmap_type(&g); williamr@2: } williamr@2: williamr@2: template <> williamr@2: struct leda_property_map<edge_all_t> { williamr@2: template <class vtype, class etype> williamr@2: struct bind_ { williamr@2: typedef leda_graph_data_map<etype, etype&, leda::GRAPH<vtype, etype>*> type; williamr@2: typedef leda_graph_data_map<etype, const etype&, williamr@2: const leda::GRAPH<vtype, etype>*> const_type; williamr@2: }; williamr@2: }; williamr@2: template <class vtype, class etype > williamr@2: inline typename property_map< leda::GRAPH<vtype, etype>, edge_all_t>::type williamr@2: get(edge_all_t, leda::GRAPH<vtype, etype>& g) { williamr@2: typedef typename property_map< leda::GRAPH<vtype, etype>, edge_all_t>::type williamr@2: pmap_type; williamr@2: return pmap_type(&g); williamr@2: } williamr@2: template <class vtype, class etype > williamr@2: inline typename property_map< leda::GRAPH<vtype, etype>, edge_all_t>::const_type williamr@2: get(edge_all_t, const leda::GRAPH<vtype, etype>& g) { williamr@2: typedef typename property_map< leda::GRAPH<vtype, etype>, williamr@2: edge_all_t>::const_type pmap_type; williamr@2: return pmap_type(&g); williamr@2: } williamr@2: williamr@2: // property map interface to the LEDA node_array class williamr@2: williamr@2: template <class E, class ERef, class NodeMapPtr> williamr@2: class leda_node_property_map williamr@2: : public put_get_helper<ERef, leda_node_property_map<E, ERef, NodeMapPtr> > williamr@2: { williamr@2: public: williamr@2: typedef E value_type; williamr@2: typedef ERef reference; williamr@2: typedef leda_node key_type; williamr@2: typedef lvalue_property_map_tag category; williamr@2: leda_node_property_map(NodeMapPtr a) : m_array(a) { } williamr@2: ERef operator[](leda_node n) const { return (*m_array)[n]; } williamr@2: protected: williamr@2: NodeMapPtr m_array; williamr@2: }; williamr@2: template <class E> williamr@2: leda_node_property_map<E, const E&, const leda_node_array<E>*> williamr@2: make_leda_node_property_map(const leda_node_array<E>& a) williamr@2: { williamr@2: typedef leda_node_property_map<E, const E&, const leda_node_array<E>*> williamr@2: pmap_type; williamr@2: return pmap_type(&a); williamr@2: } williamr@2: template <class E> williamr@2: leda_node_property_map<E, E&, leda_node_array<E>*> williamr@2: make_leda_node_property_map(leda_node_array<E>& a) williamr@2: { williamr@2: typedef leda_node_property_map<E, E&, leda_node_array<E>*> pmap_type; williamr@2: return pmap_type(&a); williamr@2: } williamr@2: williamr@2: template <class E> williamr@2: leda_node_property_map<E, const E&, const leda_node_map<E>*> williamr@2: make_leda_node_property_map(const leda_node_map<E>& a) williamr@2: { williamr@2: typedef leda_node_property_map<E,const E&,const leda_node_map<E>*> williamr@2: pmap_type; williamr@2: return pmap_type(&a); williamr@2: } williamr@2: template <class E> williamr@2: leda_node_property_map<E, E&, leda_node_map<E>*> williamr@2: make_leda_node_property_map(leda_node_map<E>& a) williamr@2: { williamr@2: typedef leda_node_property_map<E, E&, leda_node_map<E>*> pmap_type; williamr@2: return pmap_type(&a); williamr@2: } williamr@2: williamr@2: // g++ 'enumeral_type' in template unification not implemented workaround williamr@2: template <class vtype, class etype, class Tag> williamr@2: struct property_map<leda::GRAPH<vtype, etype>, Tag> { williamr@2: typedef typename williamr@2: leda_property_map<Tag>::template bind_<vtype, etype> map_gen; williamr@2: typedef typename map_gen::type type; williamr@2: typedef typename map_gen::const_type const_type; williamr@2: }; williamr@2: williamr@2: template <class vtype, class etype, class PropertyTag, class Key> williamr@2: inline williamr@2: typename boost::property_traits< williamr@2: typename boost::property_map<leda::GRAPH<vtype, etype>,PropertyTag>::const_type williamr@2: >::value_type williamr@2: get(PropertyTag p, const leda::GRAPH<vtype, etype>& g, const Key& key) { williamr@2: return get(get(p, g), key); williamr@2: } williamr@2: williamr@2: template <class vtype, class etype, class PropertyTag, class Key,class Value> williamr@2: inline void williamr@2: put(PropertyTag p, leda::GRAPH<vtype, etype>& g, williamr@2: const Key& key, const Value& value) williamr@2: { williamr@2: typedef typename property_map<leda::GRAPH<vtype, etype>, PropertyTag>::type Map; williamr@2: Map pmap = get(p, g); williamr@2: put(pmap, key, value); williamr@2: } williamr@2: williamr@2: } // namespace boost williamr@2: williamr@2: williamr@2: #endif // BOOST_GRAPH_LEDA_HPP