dimacs.h reads MAT files to both dir and undir graphs (#231)
authorAlpar Juttner <alpar@cs.elte.hu>
Mon, 23 Feb 2009 11:48:47 +0000
changeset 572635a8375227d
parent 571 06e0fb20a97c
child 573 28b154307c0d
dimacs.h reads MAT files to both dir and undir graphs (#231)
lemon/dimacs.h
     1.1 --- a/lemon/dimacs.h	Mon Feb 23 11:31:22 2009 +0000
     1.2 +++ b/lemon/dimacs.h	Mon Feb 23 11:48:47 2009 +0000
     1.3 @@ -295,9 +295,24 @@
     1.4      _readDimacs(is, g, capacity, u, v, desc);
     1.5    }
     1.6  
     1.7 -  /// DIMACS plain digraph reader function.
     1.8 +  template<typename Graph>
     1.9 +  typename enable_if<lemon::UndirectedTagIndicator<Graph>,void>::type
    1.10 +  _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t,
    1.11 +              dummy<0> = 0)
    1.12 +  {
    1.13 +    g.addEdge(s,t);
    1.14 +  }
    1.15 +  template<typename Graph>
    1.16 +  typename disable_if<lemon::UndirectedTagIndicator<Graph>,void>::type
    1.17 +  _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t,
    1.18 +              dummy<1> = 1)
    1.19 +  {
    1.20 +    g.addArc(s,t);
    1.21 +  }
    1.22 +  
    1.23 +  /// DIMACS plain (di)graph reader function.
    1.24    ///
    1.25 -  /// This function reads a digraph without any designated nodes and
    1.26 +  /// This function reads a (di)graph without any designated nodes and
    1.27    /// maps from DIMACS format, i.e. from DIMACS files having a line
    1.28    /// starting with
    1.29    /// \code
    1.30 @@ -307,15 +322,38 @@
    1.31    ///
    1.32    /// If the file type was previously evaluated by dimacsType(), then
    1.33    /// the descriptor struct should be given by the \c dest parameter.
    1.34 -  template<typename Digraph>
    1.35 -  void readDimacsMat(std::istream& is, Digraph &g,
    1.36 -                     DimacsDescriptor desc=DimacsDescriptor()) {
    1.37 -    typename Digraph::Node u,v;
    1.38 -    NullMap<typename Digraph::Arc, int> n;
    1.39 +  template<typename Graph>
    1.40 +  void readDimacsMat(std::istream& is, Graph &g,
    1.41 +                     DimacsDescriptor desc=DimacsDescriptor())
    1.42 +  {
    1.43      if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
    1.44      if(desc.type!=DimacsDescriptor::MAT)
    1.45        throw FormatError("Problem type mismatch");
    1.46 -    _readDimacs(is, g, n, u, v, desc);
    1.47 +
    1.48 +    g.clear();
    1.49 +    std::vector<typename Graph::Node> nodes;
    1.50 +    char c;
    1.51 +    int i, j;
    1.52 +    std::string str;
    1.53 +    nodes.resize(desc.nodeNum + 1);
    1.54 +    for (int k = 1; k <= desc.nodeNum; ++k) {
    1.55 +      nodes[k] = g.addNode();
    1.56 +    }
    1.57 +    
    1.58 +    while (is >> c) {
    1.59 +      switch (c) {
    1.60 +      case 'c': // comment line
    1.61 +        getline(is, str);
    1.62 +        break;
    1.63 +      case 'n': // node definition line
    1.64 +        break;
    1.65 +      case 'a': // arc (arc) definition line
    1.66 +        is >> i >> j;
    1.67 +        getline(is, str);
    1.68 +        _addArcEdge(g,nodes[i], nodes[j]);
    1.69 +        break;
    1.70 +      }
    1.71 +    }
    1.72    }
    1.73  
    1.74    /// DIMACS plain digraph writer function.