COIN-OR::LEMON - Graph Library

Ticket #231: dimacs-undir-4ccb5d61e2af.patch

File dimacs-undir-4ccb5d61e2af.patch, 2.5 KB (added by alpar, 9 years ago)
  • lemon/dimacs.h

    # HG changeset patch
    # User Alpar Juttner <alpar@cs.elte.hu>
    # Date 1235202124 0
    # Node ID 4ccb5d61e2af222f6e54a39c03b80089ceb749e5
    # Parent  2b6d5d22bb23c5f0a014e064a9a236bfc452c645
    dimacs.h reads MAT files to both dir and undir graphs
    
    diff --git a/lemon/dimacs.h b/lemon/dimacs.h
    a b  
    295295    _readDimacs(is, g, capacity, u, v, desc); 
    296296  } 
    297297 
    298   /// DIMACS plain digraph reader function. 
     298  template<typename Graph> 
     299  typename enable_if<lemon::UndirectedTagIndicator<Graph>,void>::type 
     300  _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, 
     301              dummy<0> = 0) 
     302  { 
     303    g.addEdge(s,t); 
     304  } 
     305  template<typename Graph> 
     306  typename disable_if<lemon::UndirectedTagIndicator<Graph>,void>::type 
     307  _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, 
     308              dummy<1> = 1) 
     309  { 
     310    g.addArc(s,t); 
     311  } 
     312   
     313  /// DIMACS plain (di)graph reader function. 
    299314  /// 
    300   /// This function reads a digraph without any designated nodes and 
     315  /// This function reads a (di)graph without any designated nodes and 
    301316  /// maps from DIMACS format, i.e. from DIMACS files having a line 
    302317  /// starting with 
    303318  /// \code 
     
    307322  /// 
    308323  /// If the file type was previously evaluated by dimacsType(), then 
    309324  /// the descriptor struct should be given by the \c dest parameter. 
    310   template<typename Digraph> 
    311   void readDimacsMat(std::istream& is, Digraph &g, 
    312                      DimacsDescriptor desc=DimacsDescriptor()) { 
    313     typename Digraph::Node u,v; 
    314     NullMap<typename Digraph::Arc, int> n; 
     325  template<typename Graph> 
     326  void readDimacsMat(std::istream& is, Graph &g, 
     327                     DimacsDescriptor desc=DimacsDescriptor()) 
     328  { 
    315329    if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); 
    316330    if(desc.type!=DimacsDescriptor::MAT) 
    317331      throw FormatError("Problem type mismatch"); 
    318     _readDimacs(is, g, n, u, v, desc); 
     332 
     333    g.clear(); 
     334    std::vector<typename Graph::Node> nodes; 
     335    char c; 
     336    int i, j; 
     337    std::string str; 
     338    nodes.resize(desc.nodeNum + 1); 
     339    for (int k = 1; k <= desc.nodeNum; ++k) { 
     340      nodes[k] = g.addNode(); 
     341    } 
     342     
     343    while (is >> c) { 
     344      switch (c) { 
     345      case 'c': // comment line 
     346        getline(is, str); 
     347        break; 
     348      case 'n': // node definition line 
     349        break; 
     350      case 'a': // arc (arc) definition line 
     351        is >> i >> j; 
     352        getline(is, str); 
     353        _addArcEdge(g,nodes[i], nodes[j]); 
     354        break; 
     355      } 
     356    } 
    319357  } 
    320358 
    321359  /// DIMACS plain digraph writer function.