TuttleOFX  1
InternalGraph.tcc
Go to the documentation of this file.
00001 #include "GraphExporter.hpp"
00002 
00003 #include <boost/graph/graphviz.hpp>
00004 
00005 namespace tuttle {
00006 namespace host {
00007 namespace graph {
00008 
00009 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00010 void InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::toDominatorTree()
00011 {
00012         typedef typename boost::property_map<GraphContainer, boost::vertex_index_t>::type IndexMap;
00013         typedef typename std::vector<vertex_descriptor >::iterator VectorDescIter;
00014         typedef typename boost::iterator_property_map<VectorDescIter, IndexMap > PredMap;
00015 
00016         std::vector<vertex_descriptor> domTreePredVector;
00017         IndexMap indexMap( get( boost::vertex_index, _graph ) );
00018         vertex_iterator uItr;
00019         vertex_iterator uEnd;
00020         int j = 0;
00021         for( boost::tie( uItr, uEnd ) = vertices( _graph ); uItr != uEnd; ++uItr, ++j )
00022         {
00023                 put( indexMap, *uItr, j );
00024         }
00025 
00026         // Lengauer-Tarjan dominator tree algorithm
00027         domTreePredVector = std::vector<vertex_descriptor>( num_vertices( _graph ), boost::graph_traits<GraphContainer>::null_vertex() );
00028         PredMap domTreePredMap =
00029             boost::make_iterator_property_map( domTreePredVector.begin(), indexMap );
00030 
00031         boost::lengauer_tarjan_dominator_tree( _graph, vertex( 0, _graph ), domTreePredMap );
00032 
00033         for( boost::tie( uItr, uEnd ) = vertices( _graph ); uItr != uEnd; ++uItr )
00034                 boost::clear_vertex( *uItr, _graph );
00035 
00036         for( boost::tie( uItr, uEnd ) = vertices( _graph ); uItr != uEnd; ++uItr )
00037         {
00038                 if( get( domTreePredMap, *uItr ) != boost::graph_traits<GraphContainer>::null_vertex() )
00039                 {
00040                         add_edge( get( domTreePredMap, *uItr ), *uItr, _graph );
00041                         //get(indexMap, *uItr) indice du vertex courant
00042                         //get(domTreePredMap, *uItr) descriptor du dominator
00043                         //get(indexMap, get(domTreePredMap, *uItr)) indice du dominator
00044                 }
00045         }
00046 }
00047 
00048 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00049 std::vector<typename InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::vertex_descriptor>
00050 InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::rootVertices()
00051 {
00052         std::vector<vertex_descriptor> vroots;
00053         vertex_range_t vrange = getVertices();
00054         for( vertex_iterator it = vrange.first; it != vrange.second; ++it )
00055                 if( in_degree( *it, _graph ) == 0 )
00056                         vroots.push_back( *it );
00057 
00058         return vroots;
00059 }
00060 
00061 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00062 std::vector<typename InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::vertex_descriptor>
00063 InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::leafVertices()
00064 {
00065         std::vector<vertex_descriptor> vleafs;
00066         vertex_range_t vrange = getVertices();
00067         for( vertex_iterator it = vrange.first; it != vrange.second; ++it )
00068                 if( out_degree( *it, _graph ) == 0 )
00069                         vleafs.push_back( *it );
00070 
00071         return vleafs;
00072 }
00073 
00074 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00075 void InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::rebuildVertexDescriptorMap()
00076 {
00077         _vertexDescriptorMap.clear();
00078         BOOST_FOREACH( vertex_descriptor vd, getVertices() )
00079         {
00080                 _vertexDescriptorMap[instance( vd ).getKey()] = vd;
00081         }
00082 }
00083 
00084 template< typename VERTEX, typename EDGE, typename OutEdgeList, typename VertexList, typename EdgeList >
00085 std::size_t InternalGraph<VERTEX, EDGE, OutEdgeList, VertexList, EdgeList>::removeUnconnectedVertices( const vertex_descriptor& vroot )
00086 {
00087         visitor::MarkUsed<This> vis( *this );
00088         this->depthFirstVisit( vis, vroot );
00089 
00090         std::list<std::string> toRemove;
00091         BOOST_FOREACH( const vertex_descriptor &vd, getVertices() )
00092         {
00093                 const Vertex& v = instance( vd );
00094 
00095                 if( !v.isUsed() )
00096                 {
00097                         toRemove.push_back( v.getName() );
00098                 }
00099         }
00100         BOOST_FOREACH( const std::string & vs, toRemove )
00101         {
00102                 //TUTTLE_TLOG( TUTTLE_TRACE, "removeVertex: " << vs );
00103                 this->removeVertex( getVertexDescriptor( vs ) );
00104         }
00105 
00106         return toRemove.size();
00107 }
00108 
00109 template< typename Vertex, typename Edge >
00110 std::ostream& operator<<( std::ostream& os, const InternalGraph<Vertex, Edge>& g )
00111 {
00112         os << "  vertex count: " << g.getVertexCount() << std::endl
00113            << "  edge count: " << g.getEdgeCount() << std::endl;
00114 
00115         exportSimple<Vertex, Edge>( os, g );
00116 
00117         return os;
00118 }
00119 
00120 }
00121 }
00122 }