# HG changeset patch # User Alpar Juttner # Date 2009-02-23 12:48:47 # Node ID 635a8375227dff65047d7cdff432287b302f09a5 # Parent 06e0fb20a97c9f9acbbd88cba806271b7c97ef08 dimacs.h reads MAT files to both dir and undir graphs (#231) diff --git a/lemon/dimacs.h b/lemon/dimacs.h --- a/lemon/dimacs.h +++ b/lemon/dimacs.h @@ -295,9 +295,24 @@ _readDimacs(is, g, capacity, u, v, desc); } - /// DIMACS plain digraph reader function. + template + typename enable_if,void>::type + _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, + dummy<0> = 0) + { + g.addEdge(s,t); + } + template + typename disable_if,void>::type + _addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, + dummy<1> = 1) + { + g.addArc(s,t); + } + + /// DIMACS plain (di)graph reader function. /// - /// This function reads a digraph without any designated nodes and + /// This function reads a (di)graph without any designated nodes and /// maps from DIMACS format, i.e. from DIMACS files having a line /// starting with /// \code @@ -307,15 +322,38 @@ /// /// If the file type was previously evaluated by dimacsType(), then /// the descriptor struct should be given by the \c dest parameter. - template - void readDimacsMat(std::istream& is, Digraph &g, - DimacsDescriptor desc=DimacsDescriptor()) { - typename Digraph::Node u,v; - NullMap n; + template + void readDimacsMat(std::istream& is, Graph &g, + DimacsDescriptor desc=DimacsDescriptor()) + { if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); if(desc.type!=DimacsDescriptor::MAT) throw FormatError("Problem type mismatch"); - _readDimacs(is, g, n, u, v, desc); + + g.clear(); + std::vector nodes; + char c; + int i, j; + std::string str; + nodes.resize(desc.nodeNum + 1); + for (int k = 1; k <= desc.nodeNum; ++k) { + nodes[k] = g.addNode(); + } + + while (is >> c) { + switch (c) { + case 'c': // comment line + getline(is, str); + break; + case 'n': // node definition line + break; + case 'a': // arc (arc) definition line + is >> i >> j; + getline(is, str); + _addArcEdge(g,nodes[i], nodes[j]); + break; + } + } } /// DIMACS plain digraph writer function.