COIN-OR::LEMON - Graph Library

Changeset 1208:f486d30e4e7b in lemon-0.x for src/lemon


Ignore:
Timestamp:
03/09/05 15:15:22 (19 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1627
Message:

Easy input-output function for common graphs.
Modified Exception handling in graph_reader.

Location:
src/lemon
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/lemon/graph_reader.h

    r1164 r1208  
    3636  // Exceptions
    3737
    38   class IOException {
    39   public:
    40     virtual ~IOException() {}
    41     virtual string what() const = 0;
    42   };
    43 
    44   class DataFormatException : public IOException {
    45     std::string message;
    46   public:
    47     DataFormatException(const std::string& _message)
    48       : message(_message) {}
    49     std::string what() const {
    50       return "DataFormatException: " + message;
    51     }
    52   };
    53 
    54   template <typename _Exception>
    55   class StreamException : public _Exception {
    56   public:
    57     typedef _Exception Exception;
    58     StreamException(int _line, Exception _exception)
    59       : Exception(_exception), line_num(_line) {}
    60     virtual int line() const {
    61       return line_num;
    62     }
    63 
    64     virtual ~StreamException() {}
    65 
    66     virtual std::string what() const {
    67       ostringstream os;
    68       os << Exception::what() << " in line " << line();
    69       return os.str();
    70     }
    71   private:
    72     int line_num;
    73   }; 
    74 
    7538
    7639  /// \brief Standard ReaderTraits for the GraphReader class.
     
    9255      void read(std::istream& is, Value& value) {
    9356        if (!(is >> value))
    94           throw DataFormatException("Default Reader format exception");
     57          throw DataFormatError("Default reader format exception");
    9558      }
    9659    };
     
    12386      is >> ws;
    12487      if (!is.get(c) || c != '\"')
    125         throw DataFormatException("Quoted string format");
     88        throw DataFormatError("Quoted string format error");
    12689      while (is.get(c) && c != '\"') {
    12790        if (escaped && c == '\\') {
     
    13194        }
    13295      }
    133       if (!is) throw DataFormatException("Quoted string format");
     96      if (!is) throw DataFormatError("Quoted string format error");
    13497    }
    13598
     
    165128          int code;
    166129          if (!is.get(c) || !isHex(c))
    167             throw DataFormatException("Escape format exception");
     130            throw DataFormatError("Escape format error");
    168131          else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
    169132          else code = code * 16 + valueHex(c);
     
    174137          int code;
    175138          if (!isOct(c))
    176             throw DataFormatException("Escape format exception");
     139            throw DataFormatError("Escape format error");
    177140          else if (code = valueOct(c), !is.get(c) || !isOct(c))
    178141            is.putback(c);
     
    226189    /// \brief Construct a new GraphReader.
    227190    ///
    228     /// Construct a new GraphReader. It reads from the given map,
    229     /// it constructs the given map and it use the given reader as the
    230     /// default skipper.
     191    /// Construct a new GraphReader. It reads into the given graph
     192    /// and it use the given reader as the default skipper.
    231193    GraphReader(std::istream& _is, Graph& _graph,
    232194                const DefaultReader& _reader = DefaultReader())
     
    266228                             const Reader& reader = Reader()) {
    267229      if (node_map_readers.find(name) != node_map_readers.end()) {
    268         throw Exception() << "Multiple read rule for node map: " << name;
     230        ErrorMessage msg;
     231        msg << "Multiple read rule for node map: " << name;
     232        throw IOLogicError(msg.message());
    269233      }
    270234      node_map_readers.insert(
     
    280244                             const Reader& reader = Reader()) {
    281245      if (node_map_readers.find(name) != node_map_readers.end()) {
    282         throw Exception() << "Multiple read rule for node map: " << name;
     246        ErrorMessage msg;
     247        msg << "Multiple read rule for node map: " << name;
     248        throw IOLogicError(msg.message());
    283249      }
    284250      node_map_readers.insert(
     
    304270                             const Reader& reader = Reader()) {
    305271      if (edge_map_readers.find(name) != edge_map_readers.end()) {
    306         throw Exception() << "Multiple read rule for edge map: " << name;
     272        ErrorMessage msg;
     273        msg << "Multiple read rule for edge map: " << name;
     274        throw IOLogicError(msg.message());
    307275      }
    308276      edge_map_readers.insert(
     
    318286                             const Reader& reader = Reader()) {
    319287      if (edge_map_readers.find(name) != edge_map_readers.end()) {
    320         throw Exception() << "Multiple read rule for edge map: " << name;
     288        ErrorMessage msg;
     289        msg << "Multiple read rule for edge map: " << name;
     290        throw IOLogicError(msg.message());
    321291      }
    322292      edge_map_readers.insert(
     
    330300    GraphReader& addNode(std::string name, Node& node) {
    331301      if (node_readers.find(name) != node_readers.end()) {
    332         throw Exception() << "Multiple read rule for node";
     302        ErrorMessage msg;
     303        msg << "Multiple read rule for node: " << name;
     304        throw IOLogicError(msg.message());
    333305      }
    334306      node_readers.insert(make_pair(name, &node));
     
    341313    GraphReader& addEdge(std::string name, Edge& edge) {
    342314      if (edge_readers.find(name) != edge_readers.end()) {
    343         throw Exception() << "Multiple read rule for edge";
     315        ErrorMessage msg;
     316        msg << "Multiple read rule for edge: " << name;
     317        throw IOLogicError(msg.message());
    344318      }
    345319      edge_readers.insert(make_pair(name, &edge));
     
    369343        }
    370344        if (line.find("@end") != 0) {
    371           throw DataFormatException("Invalid control sequence: " + line);
    372         }
    373       } catch (DataFormatException e) {
    374         throw StreamException<DataFormatException>(line_num, e);
     345          throw DataFormatError("Invalid control sequence error");
     346        }
     347      } catch (DataFormatError e) {
     348        e.line(line_num);
     349        throw e;
    375350      }
    376351    }
     
    400375
    401376      if (index.size() == 0) {
    402         throw DataFormatException("No node map found");
     377        throw DataFormatError("Cannot find node id map");
    403378      }
    404379
     
    437412
    438413      if (index.size() == 0) {
    439         throw DataFormatException("No edge map found");
     414        throw DataFormatError("Cannot find edge id map");
    440415      }
    441416
     
    493468        }
    494469      }
    495       throw DataFormatException("End of stream");
     470      throw DataFormatError("End of stream error");
    496471    }
    497472   
     
    533508          inverse.insert(make_pair(value, item));
    534509        } else {
    535           throw DataFormatException("Multiple ID occurence");
     510          throw DataFormatError("Multiple ID occurence");
    536511        }
    537512      }
     
    544519          return it->second;
    545520        } else {
    546           throw DataFormatException("Invalid ID");
     521          throw DataFormatError("Invalid ID error");
    547522        }
    548523      }     
     
    571546          inverse.insert(make_pair(value, item));
    572547        } else {
    573           throw DataFormatException("Multiple ID occurence");
     548          throw DataFormatError("Multiple ID occurence error");
    574549        }
    575550      }
     
    582557          return it->second;
    583558        } else {
    584           throw DataFormatException("Invalid ID");
     559          throw DataFormatError("Invalid ID error");
    585560        }
    586561      }     
     
    673648  };
    674649
     650  /// Ready to use reader function. 
     651  template<typename Graph, typename CapacityMap, typename CostMap>
     652  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
     653                  typename Graph::Node &s, typename Graph::Node &t,
     654                  CostMap& cost) {
     655    GraphReader<Graph> reader(is, g);
     656    reader.addEdgeMap("capacity", capacity);
     657    reader.addEdgeMap("cost", cost);
     658    reader.addNode("source", s);
     659    reader.addNode("target", t);
     660    reader.run();
     661  }
     662
     663  template<typename Graph, typename CapacityMap>
     664  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
     665                  typename Graph::Node &s, typename Graph::Node &t) {
     666    GraphReader<Graph> reader(is, g);
     667    reader.addEdgeMap("capacity", capacity);
     668    reader.addNode("source", s);
     669    reader.addNode("target", t);
     670    reader.run();
     671  }
     672
     673  template<typename Graph, typename CapacityMap>
     674  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
     675                  typename Graph::Node &s) {
     676    GraphReader<Graph> reader(is, g);
     677    reader.addEdgeMap("capacity", capacity);
     678    reader.addNode("source", s);
     679    reader.run();
     680  }
     681
     682  template<typename Graph, typename CapacityMap>
     683  void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
     684    GraphReader<Graph> reader(is, g);
     685    reader.addEdgeMap("capacity", capacity);
     686    reader.run();
     687  }
     688
     689  template<typename Graph>
     690  void readGraph(std::istream& is, Graph &g) {
     691    GraphReader<Graph> reader(is, g);
     692    reader.run();
     693  }
     694
    675695}
  • src/lemon/graph_writer.h

    r1188 r1208  
    2828#include <memory>
    2929
     30#include <lemon/map_utils.h>
    3031#include <lemon/invalid.h>
    3132#include <lemon/error.h>
     
    163164    /// it constructs the given map and it use the given writer as the
    164165    /// default skipper.
    165     GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
     166    GraphWriter(std::ostream& _os, const Graph& _graph)
     167      : os(_os), graph(_graph) {}
    166168
    167169
     
    368370
    369371    std::ostream& os;
    370     Graph& graph;
     372    const Graph& graph;
    371373
    372374  };
    373375
     376  /// Ready to use writer function. 
     377  template<typename Graph, typename CapacityMap, typename CostMap>
     378  void writeGraph(std::ostream& os, const Graph &g,
     379                  const CapacityMap& capacity, const typename Graph::Node &s,
     380                  const typename Graph::Node &t, const CostMap& cost) {
     381    GraphWriter<Graph> reader(os, g);
     382    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
     383    reader.addNodeMap("id", nodeIdMap);
     384    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
     385    reader.addEdgeMap("id", edgeIdMap);
     386    reader.addEdgeMap("capacity", capacity);
     387    reader.addEdgeMap("cost", cost);
     388    reader.addNode("source", s);
     389    reader.addNode("target", t);
     390    reader.run();
     391  }
     392
     393  /// Ready to use writer function. 
     394  template<typename Graph, typename CapacityMap, typename CostMap>
     395  void writeGraph(std::ostream& os, const Graph &g,
     396                  const CapacityMap& capacity, const typename Graph::Node &s,
     397                  const typename Graph::Node &t) {
     398    GraphWriter<Graph> reader(os, g);
     399    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
     400    reader.addNodeMap("id", nodeIdMap);
     401    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
     402    reader.addEdgeMap("id", edgeIdMap);
     403    reader.addEdgeMap("capacity", capacity);
     404    reader.addNode("source", s);
     405    reader.addNode("target", t);
     406    reader.run();
     407  }
     408
     409  /// Ready to use writer function. 
     410  template<typename Graph, typename CapacityMap>
     411  void writeGraph(std::ostream& os, const Graph &g,
     412                  const CapacityMap& capacity, const typename Graph::Node &s) {
     413    GraphWriter<Graph> reader(os, g);
     414    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
     415    reader.addNodeMap("id", nodeIdMap);
     416    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
     417    reader.addEdgeMap("id", edgeIdMap);
     418    reader.addEdgeMap("capacity", capacity);
     419    reader.addNode("source", s);
     420    reader.run();
     421  }
     422  /// Ready to use writer function. 
     423  template<typename Graph, typename CapacityMap>
     424  void writeGraph(std::ostream& os, const Graph &g,
     425                  const CapacityMap& capacity) {
     426    GraphWriter<Graph> reader(os, g);
     427    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
     428    reader.addNodeMap("id", nodeIdMap);
     429    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
     430    reader.addEdgeMap("id", edgeIdMap);
     431    reader.addEdgeMap("capacity", capacity);
     432    reader.run();
     433  }
     434  /// Ready to use writer function. 
     435  template<typename Graph>
     436  void writeGraph(std::ostream& os, const Graph &g) {
     437    GraphWriter<Graph> reader(os, g);
     438    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
     439    reader.addNodeMap("id", nodeIdMap);
     440    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
     441    reader.addEdgeMap("id", edgeIdMap);
     442    reader.run();
     443  }
     444
    374445
    375446}
Note: See TracChangeset for help on using the changeset viewer.