Exemplo n.º 1
0
std::pair<typename Graph::vertex_descriptor,int>
generateLevelsFromGraphProper( const Graph& g , LevelMap& levels ,
			       ParentMap& parents , 
			       typename Graph::vertex_descriptor * rootPtr )
{
  // Now to determine the root node
  typename Graph::vertex_iterator v , vend;
  tie(v,vend) = vertices( g.boostGraph() );
  typename Graph::vertex_descriptor root;
  if ( !rootPtr ) {
    // Init mins and find root if none was given
    int min = sumDOS( g , *v );
    root = *v;
    int ctr = 1;
    std::cerr << std::setw(8) << ctr << std::flush;
    for (; v!=vend; ++v ) {
      int sum = sumDOS( g , *v );
      if ( sum < min ) { min = sum; root=*v; }
      std::cerr << "\b\b\b\b\b\b\b\b" << std::setw(8) << ++ctr << std::flush;
    }
  } else {
    root = *rootPtr;
  }
  // The node with the most edges is at root
  //tie( root , count ) = getVertexWMostEdges( mst );  
  // Generate a breadth first level map
  setLevelMapFromMST( g , levels , parents , root );
  int maxL = 0;
  for ( typename LevelMap::const_iterator i=levels.begin(); i!=levels.end(); ++i ) {
    maxL = (int) GraphDetail::mmax<long>( *i ,maxL );
  }
  // Return the descriptor to the one that started it all
  // with the total level count
  return std::make_pair(root,maxL);
}
Exemplo n.º 2
0
    unsigned long
    compute_levels (TypeInfo const& ti, unsigned long cur, LevelMap& map)
    {
      unsigned long ret = cur;

      if (map.find (ti) == map.end () || map[ti] < cur) map[ti] = cur;

      for (TypeInfo::BaseIterator i = ti.begin_base ();
           i != ti.end_base ();
           i++)
      {
        unsigned long t = compute_levels (i->type_info (), cur + 1, map);
        if (t > ret) ret = t;
      }

      return ret;
    }
Exemplo n.º 3
0
    void Dispatcher::
    dispatch (SyntaxTree::NodePtr const& n)
    {
      LevelMap levels;

      unsigned long max = compute_levels (n->type_info (), 0, levels);

      //cerr << "starting dispatch process for "
      //     << n->type_info ().type_id () << " with "
      //     << max << " levels" << endl;

      for (unsigned long l = 0; l < max + 1; l++)
      {
        TypeInfoSet dispatched;

        for (LevelMap::const_iterator i = levels.begin ();
             i != levels.end ();
             i++)
        {
          if (i->second == l)
          {
            TraversalMap::const_iterator v =
              traversal_map_.find (i->first.type_id ());

            if (v != traversal_map_.end () && !(v->second.suppressed))
            {
              //cerr << "dispatching traverser for "
              //     << n->type_info ().type_id () << " as "
              //     << i->first.type_id () << endl;

              v->second.traverser->traverse (n);
              flatten_tree (i->first, dispatched);
            }
          }
        }

        // Remove traversed types from level map.
        for (TypeInfoSet::const_iterator i = dispatched.begin ();
             i != dispatched.end ();
             i++)
        {
          levels.erase (*i);
        }
      }
    }
Exemplo n.º 4
0
  void Dispatcher::
  dispatch (SyntaxTree::Node* n)
  {
    LevelMap levels;

    unsigned long max = compute_levels (n->type_info (), 0, levels);

    for (unsigned long l = 0; l < max + 1; l++)
    {
      TypeInfoSet dispatched;

      for (LevelMap::const_iterator i = levels.begin ();
           i != levels.end ();
           i++)
      {
        if (i->second == l)
        {
          TraversalMap::const_iterator v =
            traversal_map_.find (i->first.type_id ());

          if (v != traversal_map_.end ())
          {
            v->second->traverse (n);
            flatten_tree (i->first, dispatched);
          }
        }
      }

      // Remove traversed types from level map.
      for (TypeInfoSet::const_iterator i = dispatched.begin ();
           i != dispatched.end ();
           i++)
      {
        levels.erase (*i);
      }
    }
  }
Exemplo n.º 5
0
std::pair<typename Graph::vertex_descriptor,int>
generateLevelsFromGraph( const Graph& g , LevelMap& levels , ParentMap& parents , 
			 typename Graph::vertex_descriptor * rootPtr , Graph& mst ,
			 bool useOriginalWeights )
{
  using namespace boost;
  Graph newGraph( g );
  // First issue is to weight the nodes if desired
  if ( ! useOriginalWeights )
    generateWeightMapFromNegativeAdjacentVertexCount( newGraph );
  // Now to make a tree based on these weights
  setMSTFromGraph( newGraph , mst );
  // Now to determine the root node
  typename Graph::vertex_iterator v , vend;
  tie(v,vend) = vertices( mst.boostGraph() );
  typename Graph::vertex_descriptor root, v1, v2;


  if ( !rootPtr ) {

    // linear time algorithm to find the root by Ying Wang ([email protected])

    int i, k, n = mst.vertexCount();
    vector <typename Graph::vertex_descriptor> queue(n+1);
    int *ccount, *d;
    long long tot = 0, min, *a;
    typename Graph::out_edge_iterator ee1,ee2;

    int p, q;
    queue[p = q = 0] = *v;
    a = new long long[n+1];
    ccount = new int[n+1];
    d = new int[n+1];
    for(i = 0; i <= n; i++) ccount[i] = 1, d[i] = -1, a[i] = 0;
    d[ *v ] = 0;
    const typename Graph::boost_graph& bg = mst.boostGraph();

    for(; p<=q; p++) {
      v1 = queue[p];
      tie(ee1, ee2) = out_edges(v1, bg);
      for(; ee1 != ee2; ee1++) {
	v2 = target(*ee1, bg);
	if (d[v2] == -1) queue[++q] = v2, tot += (d[v2] = d[v1] + 1);
      }
    }

    a[ *v ] = min = tot;
    root = *v;

    for(k = q; k >= 0; k--) {
      v1 = queue[k];
      tie(ee1, ee2) = out_edges(v1, bg);
      for(; ee1 != ee2; ee1++) {
	v2 = target(*ee1, bg);
	if (d[v2] > d[v1]) {
	  ccount[v1] += ccount[v2];
	}
      }
    }

    for(k = 0; k <= q; k++) {
      v1 = queue[k];
      tie(ee1, ee2) = out_edges(v1, bg);
      for(; ee1 != ee2; ee1++) {
	v2 = target(*ee1, bg);
	if (d[v2] > d[v1]) {
	  a[v2] = a[v1] - ccount[v2] + (n - ccount[v2]);
	  if (a[v2] < min) root = v2, min = a[v2];
	}
      }
    }

    delete []a;
    delete []d;
    delete []ccount;
    cerr << "root finding done!" << endl;
  } else {
    root = *rootPtr;
  }
  // The node with the most edges is at root
  //tie( root , count ) = getVertexWMostEdges( mst );  
  // Generate a breadth first level map
  setLevelMapFromMST( mst , levels , parents , root );
  int maxL = 0;
  for ( typename LevelMap::const_iterator i=levels.begin(); i!=levels.end(); ++i ) {
    maxL = (int) GraphDetail::mmax<long>( *i ,maxL );
  }
  // Return the descriptor to the one that started it all
  // with the total level count
  return std::make_pair(root,maxL);
}