src/lemon/graph_writer.h
author alpar
Mon, 21 Mar 2005 16:16:52 +0000
changeset 1237 2414b5ab7684
parent 1208 f486d30e4e7b
child 1287 984723507b86
permissions -rw-r--r--
Constructor of IdMap<G,I>::InverseMap made public
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
deba@1137
     3
 *
alpar@1164
     4
 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@1137
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
deba@1137
     6
 *
deba@1137
     7
 * Permission to use, modify and distribute this software is granted
deba@1137
     8
 * provided that this copyright notice appears in all copies. For
deba@1137
     9
 * precise terms see the accompanying LICENSE file.
deba@1137
    10
 *
deba@1137
    11
 * This software is provided "AS IS" with no warranty of any kind,
deba@1137
    12
 * express or implied, and with no claim as to its suitability for any
deba@1137
    13
 * purpose.
deba@1137
    14
 *
deba@1137
    15
 */
deba@1137
    16
deba@1137
    17
///\ingroup gio
deba@1137
    18
///\file
deba@1137
    19
///\brief Graph writer.
deba@1137
    20
deba@1214
    21
#ifndef LEMON_GRAPH_WRITER_H
deba@1214
    22
#define LEMON_GRAPH_WRITER_H
deba@1137
    23
deba@1137
    24
#include <iostream>
deba@1137
    25
#include <sstream>
deba@1137
    26
deba@1137
    27
#include <map>
deba@1137
    28
#include <vector>
deba@1137
    29
deba@1137
    30
#include <memory>
deba@1137
    31
deba@1208
    32
#include <lemon/map_utils.h>
deba@1214
    33
deba@1137
    34
#include <lemon/invalid.h>
deba@1137
    35
#include <lemon/error.h>
deba@1137
    36
deba@1137
    37
deba@1137
    38
namespace lemon {
deba@1137
    39
deba@1137
    40
  /// \brief Standard WriterTraits for the GraphWriter class.
deba@1137
    41
  ///
deba@1137
    42
  /// Standard WriterTraits for the GraphWriter class.
deba@1137
    43
  /// It defines standard writing method for all type of value. 
deba@1137
    44
  struct DefaultWriterTraits {
deba@1137
    45
deba@1137
    46
    /// \brief Template class for writing an value.
deba@1137
    47
    ///
deba@1137
    48
    /// Template class for writing an value.
deba@1137
    49
    template <typename _Value>
deba@1137
    50
    struct Writer {
deba@1137
    51
      /// The value type.
deba@1137
    52
      typedef _Value Value;
deba@1137
    53
deba@1188
    54
      /// \brief Writes a value to the given stream.
deba@1137
    55
      ///
deba@1188
    56
      /// Writes a value to the given stream.
deba@1137
    57
      void write(std::ostream& os, const Value& value) {
deba@1137
    58
	os << value << '\t';
deba@1137
    59
      }
deba@1137
    60
    };
deba@1137
    61
deba@1137
    62
  };
deba@1137
    63
deba@1137
    64
deba@1137
    65
  /// \brief Writer class for quoted strings.
deba@1137
    66
  ///
deba@1137
    67
  /// Writer class for quoted strings. It can process the escape
deba@1137
    68
  /// sequences in the string.
deba@1137
    69
  class QuotedStringWriter {
deba@1137
    70
  public:
deba@1137
    71
    typedef std::string Value;
deba@1137
    72
deba@1137
    73
    /// \brief Constructor for the writer.
deba@1137
    74
    ///
deba@1137
    75
    /// Constructor for the writer. If the given parameter is true
deba@1137
    76
    /// the writer creates escape sequences from special characters.
deba@1137
    77
    QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
deba@1137
    78
deba@1188
    79
    /// \brief Writes a quoted string to the given stream.
deba@1137
    80
    ///
deba@1188
    81
    /// Writes a quoted string to the given stream.
deba@1137
    82
    void write(std::ostream& os, const std::string& value) {
deba@1137
    83
      os << "\"";
deba@1137
    84
      if (escaped) {
deba@1137
    85
	ostringstream ls;
deba@1137
    86
	for (int i = 0; i < (int)value.size(); ++i) {
deba@1137
    87
	  writeEscape(ls, value[i]);
deba@1137
    88
	}
deba@1137
    89
	os << ls.str();
deba@1137
    90
      } else {
deba@1137
    91
	os << value;
deba@1137
    92
      }
deba@1137
    93
      os << "\"";
deba@1137
    94
    }
deba@1137
    95
deba@1137
    96
  private:
deba@1137
    97
    
deba@1137
    98
    static void writeEscape(std::ostream& os, char c) {
deba@1137
    99
      switch (c) {
deba@1137
   100
      case '\\':
deba@1137
   101
	os << "\\\\";
deba@1137
   102
	return;
deba@1137
   103
      case '\"':
deba@1137
   104
	os << "\\\"";
deba@1137
   105
	return;
deba@1137
   106
      case '\'':
deba@1137
   107
	os << "\\\'";
deba@1137
   108
	return;
deba@1137
   109
      case '\?':
deba@1137
   110
	os << "\\\?";
deba@1137
   111
	return;
deba@1137
   112
      case '\a':
deba@1137
   113
	os << "\\a";
deba@1137
   114
	return;
deba@1137
   115
      case '\b':
deba@1137
   116
	os << "\\b";
deba@1137
   117
	return;
deba@1137
   118
      case '\f':
deba@1137
   119
	os << "\\f";
deba@1137
   120
	return;
deba@1137
   121
      case '\r':
deba@1137
   122
	os << "\\r";
deba@1137
   123
	return;
deba@1137
   124
      case '\n':
deba@1137
   125
	os << "\\n";
deba@1137
   126
	return;
deba@1137
   127
      case '\t':
deba@1137
   128
	os << "\\t";
deba@1137
   129
	return;
deba@1137
   130
      case '\v':
deba@1137
   131
	os << "\\v";
deba@1137
   132
	return;
deba@1137
   133
      default:
deba@1137
   134
	if (c < 0x20) {
deba@1137
   135
	  os << '\\' << oct << (int)c;
deba@1137
   136
	} else {
deba@1137
   137
	  os << c;
deba@1137
   138
	}
deba@1137
   139
	return;
deba@1137
   140
      }     
deba@1137
   141
    }
deba@1137
   142
  private:
deba@1137
   143
    bool escaped;
deba@1137
   144
  };
deba@1137
   145
deba@1137
   146
  
deba@1137
   147
  /// \brief The graph writer class.
deba@1137
   148
  ///
deba@1137
   149
  /// The writer class for the graph output.
alpar@1138
   150
  /// \see \ref GraphReader
alpar@1138
   151
  /// \see \ref graph-io-page
deba@1137
   152
  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
deba@1137
   153
  class GraphWriter {
deba@1137
   154
  public:
deba@1137
   155
    
deba@1137
   156
    typedef _Graph Graph;
deba@1137
   157
    typedef typename Graph::Node Node;
deba@1137
   158
    typedef typename Graph::NodeIt NodeIt;
deba@1137
   159
    typedef typename Graph::Edge Edge;
deba@1137
   160
    typedef typename Graph::EdgeIt EdgeIt;
deba@1137
   161
deba@1137
   162
    typedef _WriterTraits WriterTraits;
deba@1137
   163
 
deba@1137
   164
    /// \brief Construct a new GraphWriter.
deba@1137
   165
    ///
deba@1137
   166
    /// Construct a new GraphWriter. It writes from the given map,
deba@1137
   167
    /// it constructs the given map and it use the given writer as the
deba@1137
   168
    /// default skipper.
deba@1208
   169
    GraphWriter(std::ostream& _os, const Graph& _graph) 
deba@1208
   170
      : os(_os), graph(_graph) {}
deba@1137
   171
deba@1137
   172
deba@1137
   173
    /// \brief Destruct the graph writer.
deba@1137
   174
    ///
deba@1137
   175
    /// Destruct the graph writer.
deba@1137
   176
    ~GraphWriter() {
deba@1137
   177
      for (typename NodeMapWriters::iterator it = node_map_writers.begin(); 
deba@1137
   178
	   it != node_map_writers.end(); ++it) {
deba@1137
   179
	delete it->second;
deba@1137
   180
      }
deba@1137
   181
deba@1137
   182
      for (typename EdgeMapWriters::iterator it = edge_map_writers.begin();
deba@1137
   183
	   it != edge_map_writers.end(); ++it) {
deba@1137
   184
	delete it->second;
deba@1137
   185
      }
deba@1137
   186
deba@1137
   187
    }
deba@1137
   188
deba@1137
   189
    // Node map rules
deba@1137
   190
deba@1137
   191
    /// \brief Add a new node map writer command for the writer.
deba@1137
   192
    ///
deba@1137
   193
    /// Add a new node map writer command for the writer.
deba@1137
   194
    template <typename Map>
deba@1137
   195
    GraphWriter& addNodeMap(std::string name, const Map& map) {
deba@1137
   196
      return addNodeMap<typename WriterTraits::template Writer<
deba@1137
   197
	typename Map::Value>, Map>(name, map);
deba@1137
   198
    }
deba@1137
   199
deba@1137
   200
    /// \brief Add a new node map writer command for the writer.
deba@1137
   201
    ///
deba@1137
   202
    /// Add a new node map writer command for the writer.
deba@1137
   203
    template <typename Writer, typename Map>
deba@1137
   204
    GraphWriter& addNodeMap(std::string name, const Map& map, 
deba@1137
   205
			      const Writer& writer = Writer()) {
deba@1137
   206
      node_map_writers.push_back(
deba@1137
   207
        make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
deba@1137
   208
      return *this;
deba@1137
   209
    }
deba@1137
   210
deba@1137
   211
    // Edge map rules
deba@1137
   212
deba@1137
   213
    /// \brief Add a new edge map writer command for the writer.
deba@1137
   214
    ///
deba@1137
   215
    /// Add a new edge map writer command for the writer.
deba@1137
   216
    template <typename Map>
deba@1137
   217
    GraphWriter& addEdgeMap(std::string name, const Map& map) { 
deba@1137
   218
      return addEdgeMap<typename WriterTraits::template Writer<
deba@1137
   219
        typename Map::Value>, Map>(name, map);
deba@1137
   220
    }
deba@1137
   221
deba@1137
   222
deba@1137
   223
    /// \brief Add a new edge map writer command for the writer.
deba@1137
   224
    ///
deba@1137
   225
    /// Add a new edge map writer command for the writer.
deba@1137
   226
    template <typename Writer, typename Map>
deba@1137
   227
    GraphWriter& addEdgeMap(std::string name, 
deba@1137
   228
			    const Map& map, const Writer& writer = Writer()) {
deba@1137
   229
      edge_map_writers.push_back(make_pair(name, 
deba@1137
   230
	new MapWriter<Edge, Map, Writer>(map, writer)));
deba@1137
   231
      return *this;
deba@1137
   232
    }
deba@1137
   233
deba@1137
   234
    /// \brief Add a new labeled node writer for the writer.
deba@1137
   235
    ///
deba@1137
   236
    /// Add a new labeled node writer for the writer.
deba@1137
   237
    GraphWriter& addNode(std::string name, const Node& node) {
deba@1137
   238
      node_writers.push_back(make_pair(name, node));
deba@1137
   239
      return *this;
deba@1137
   240
    }
deba@1137
   241
deba@1137
   242
    /// \brief Add a new labeled edge writer for the writer.
deba@1137
   243
    ///
deba@1137
   244
    /// Add a new labeled edge writer for the writer.
deba@1137
   245
    GraphWriter& addEdge(std::string name, const Edge& edge) {
deba@1137
   246
      edge_writers.push_back(make_pair(name, edge));
deba@1137
   247
      return *this;
deba@1137
   248
    }
deba@1137
   249
deba@1137
   250
    /// \brief Executes the writer commands.
deba@1137
   251
    ///
deba@1137
   252
    /// Executes the writer commands.
deba@1137
   253
    void run() {   
deba@1137
   254
      writeNodeSet();
deba@1137
   255
      writeEdgeSet();
deba@1137
   256
      writeNodes();
deba@1137
   257
      writeEdges();
deba@1137
   258
      os << "@end" << std::endl;
deba@1137
   259
    }
deba@1137
   260
deba@1137
   261
  private:
deba@1137
   262
deba@1137
   263
    void writeNodeSet() {
deba@1137
   264
      if (node_map_writers.size() == 0) return;
deba@1137
   265
      os << "@nodeset" << std::endl;
deba@1137
   266
      for (int i = 0; i < (int)node_map_writers.size(); ++i) {
deba@1137
   267
	os << node_map_writers[i].first << '\t';
deba@1137
   268
      } 
deba@1137
   269
      os << std::endl;
deba@1137
   270
      for (NodeIt it(graph); it != INVALID; ++it) {
deba@1137
   271
	for (int i = 0; i < (int)node_map_writers.size(); ++i) {
deba@1137
   272
	  node_map_writers[i].second->write(os, it);
deba@1137
   273
	}
deba@1137
   274
	os << std::endl;
deba@1137
   275
      }
deba@1137
   276
deba@1137
   277
    }
deba@1137
   278
deba@1137
   279
    void writeEdgeSet() {
deba@1137
   280
      if (edge_map_writers.size() == 0) return;
deba@1137
   281
      if (node_map_writers.size() == 0) {
deba@1188
   282
	//	ErrorMessage message;
deba@1188
   283
	//	message << "Missing node id map";
deba@1188
   284
	//	throw IOLogicError(message);
deba@1137
   285
      }
deba@1137
   286
      os << "@edgeset" << std::endl;
deba@1137
   287
      os << "\t\t";
deba@1137
   288
      for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
deba@1137
   289
	os << edge_map_writers[i].first << '\t';
deba@1137
   290
      } 
deba@1137
   291
      os << std::endl;
deba@1137
   292
      for (EdgeIt it(graph); it != INVALID; ++it) {
deba@1137
   293
	node_map_writers[0].second->write(os, graph.source(it));
deba@1137
   294
	node_map_writers[0].second->write(os, graph.target(it));
deba@1137
   295
	for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
deba@1137
   296
	  edge_map_writers[i].second->write(os, it);
deba@1137
   297
	}
deba@1137
   298
	os << std::endl;
deba@1137
   299
      }
deba@1137
   300
    }
deba@1137
   301
deba@1137
   302
    void writeNodes() {
deba@1137
   303
      if (node_writers.size() == 0) return;
deba@1137
   304
      if (node_map_writers.size() == 0) {
deba@1188
   305
	//	throw Exception() << "Missing node id map";
deba@1137
   306
      }
deba@1137
   307
      os << "@nodes" << std::endl;
deba@1137
   308
      for (int i = 0; i < (int)node_writers.size(); ++i) {
deba@1137
   309
	os << node_writers[i].first << '\t';
deba@1137
   310
	node_map_writers[0].second->write(os, node_writers[i].second);
deba@1137
   311
	os << std::endl;
deba@1137
   312
      } 
deba@1137
   313
    }
deba@1137
   314
deba@1137
   315
    void writeEdges() {
deba@1137
   316
      if (edge_writers.size() == 0) return;
deba@1137
   317
      if (edge_map_writers.size() == 0) {
deba@1188
   318
	//	throw Exception() << "Missing edge id map";
deba@1137
   319
      }
deba@1137
   320
      os << "@edges" << std::endl;
deba@1137
   321
      for (int i = 0; i < (int)edge_writers.size(); ++i) {
deba@1137
   322
	os << edge_writers[i].first << '\t';
deba@1137
   323
	edge_map_writers[0].second->write(os, edge_writers[i].second);
deba@1137
   324
	os << std::endl;
deba@1137
   325
      } 
deba@1137
   326
    }
deba@1137
   327
    
deba@1137
   328
    // Writers
deba@1137
   329
deba@1137
   330
    template <class _Item>
deba@1137
   331
    class WriterBase {
deba@1137
   332
    public:
deba@1137
   333
      typedef _Item Item;
deba@1137
   334
      virtual void write(std::ostream&, const Item&) = 0;
deba@1137
   335
    };
deba@1137
   336
deba@1137
   337
    template <class _Item, typename _Map, typename _Writer>
deba@1137
   338
    class MapWriter : public WriterBase<_Item> {
deba@1137
   339
    public:
deba@1137
   340
      typedef _Map Map;
deba@1137
   341
      typedef _Writer Writer;
deba@1137
   342
      typedef typename Writer::Value Value;
deba@1137
   343
      typedef _Item Item;
deba@1137
   344
      
deba@1137
   345
      const Map& map;
deba@1137
   346
      Writer writer;
deba@1137
   347
deba@1137
   348
      MapWriter(const Map& _map, const Writer& _writer) 
deba@1137
   349
	: map(_map), writer(_writer) {}
deba@1137
   350
deba@1137
   351
deba@1137
   352
      virtual void write(std::ostream& os, const Item& item) {
deba@1137
   353
	writer.write(os, map[item]);
deba@1137
   354
      }
deba@1137
   355
deba@1137
   356
    };
deba@1137
   357
deba@1137
   358
deba@1137
   359
deba@1137
   360
    typedef std::vector< std::pair<std::string, WriterBase<Node>*> > 
deba@1137
   361
      NodeMapWriters;
deba@1137
   362
    NodeMapWriters node_map_writers;
deba@1137
   363
deba@1137
   364
    typedef std::vector< std::pair<std::string, WriterBase<Edge>*> > 
deba@1137
   365
      EdgeMapWriters;
deba@1137
   366
    EdgeMapWriters edge_map_writers;
deba@1137
   367
deba@1137
   368
    typedef std::vector<std::pair<std::string, Node> > NodeWriters;
deba@1137
   369
    NodeWriters node_writers;
deba@1137
   370
deba@1137
   371
    typedef std::vector<std::pair<std::string, Edge> > EdgeWriters;
deba@1137
   372
    EdgeWriters edge_writers;
deba@1137
   373
deba@1137
   374
    std::ostream& os;
deba@1208
   375
    const Graph& graph;
deba@1137
   376
deba@1137
   377
  };
deba@1137
   378
deba@1208
   379
  /// Ready to use writer function.  
deba@1208
   380
  template<typename Graph, typename CapacityMap, typename CostMap>
deba@1208
   381
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   382
		  const CapacityMap& capacity, const typename Graph::Node &s,
deba@1208
   383
		  const typename Graph::Node &t, const CostMap& cost) {
deba@1208
   384
    GraphWriter<Graph> reader(os, g);
deba@1208
   385
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1208
   386
    reader.addNodeMap("id", nodeIdMap);
deba@1208
   387
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1208
   388
    reader.addEdgeMap("id", edgeIdMap);
deba@1208
   389
    reader.addEdgeMap("capacity", capacity);
deba@1208
   390
    reader.addEdgeMap("cost", cost);
deba@1208
   391
    reader.addNode("source", s);
deba@1208
   392
    reader.addNode("target", t);
deba@1208
   393
    reader.run();
deba@1208
   394
  }
deba@1208
   395
deba@1208
   396
  /// Ready to use writer function.  
deba@1208
   397
  template<typename Graph, typename CapacityMap, typename CostMap>
deba@1208
   398
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   399
		  const CapacityMap& capacity, const typename Graph::Node &s,
deba@1208
   400
		  const typename Graph::Node &t) {
deba@1208
   401
    GraphWriter<Graph> reader(os, g);
deba@1208
   402
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1208
   403
    reader.addNodeMap("id", nodeIdMap);
deba@1208
   404
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1208
   405
    reader.addEdgeMap("id", edgeIdMap);
deba@1208
   406
    reader.addEdgeMap("capacity", capacity);
deba@1208
   407
    reader.addNode("source", s);
deba@1208
   408
    reader.addNode("target", t);
deba@1208
   409
    reader.run();
deba@1208
   410
  }
deba@1208
   411
deba@1208
   412
  /// Ready to use writer function.  
deba@1208
   413
  template<typename Graph, typename CapacityMap>
deba@1208
   414
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   415
		  const CapacityMap& capacity, const typename Graph::Node &s) {
deba@1208
   416
    GraphWriter<Graph> reader(os, g);
deba@1208
   417
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1208
   418
    reader.addNodeMap("id", nodeIdMap);
deba@1208
   419
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1208
   420
    reader.addEdgeMap("id", edgeIdMap);
deba@1208
   421
    reader.addEdgeMap("capacity", capacity);
deba@1208
   422
    reader.addNode("source", s);
deba@1208
   423
    reader.run();
deba@1208
   424
  }
deba@1208
   425
  /// Ready to use writer function.  
deba@1208
   426
  template<typename Graph, typename CapacityMap>
deba@1208
   427
  void writeGraph(std::ostream& os, const Graph &g, 
deba@1208
   428
		  const CapacityMap& capacity) {
deba@1208
   429
    GraphWriter<Graph> reader(os, g);
deba@1208
   430
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1208
   431
    reader.addNodeMap("id", nodeIdMap);
deba@1208
   432
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1208
   433
    reader.addEdgeMap("id", edgeIdMap);
deba@1208
   434
    reader.addEdgeMap("capacity", capacity);
deba@1208
   435
    reader.run();
deba@1208
   436
  }
deba@1208
   437
  /// Ready to use writer function.  
deba@1208
   438
  template<typename Graph>
deba@1208
   439
  void writeGraph(std::ostream& os, const Graph &g) {
deba@1208
   440
    GraphWriter<Graph> reader(os, g);
deba@1208
   441
    IdMap<Graph, typename Graph::Node> nodeIdMap(g);
deba@1208
   442
    reader.addNodeMap("id", nodeIdMap);
deba@1208
   443
    IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
deba@1208
   444
    reader.addEdgeMap("id", edgeIdMap);
deba@1208
   445
    reader.run();
deba@1208
   446
  }
deba@1208
   447
deba@1137
   448
deba@1137
   449
}
deba@1214
   450
deba@1214
   451
#endif