src/lemon/graph_writer.h
author alpar
Tue, 15 Feb 2005 15:00:31 +0000
changeset 1150 c20bcf71efe3
parent 1137 83a48cfd8553
child 1164 80bb73097736
permissions -rw-r--r--
Minor changes.
deba@1137
     1
/* -*- C++ -*-
deba@1137
     2
 * src/lemon/graph_writer.h - Part of LEMON, a generic C++ optimization library
deba@1137
     3
 *
deba@1137
     4
 * Copyright (C) 2004 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@1137
    21
deba@1137
    22
#include <iostream>
deba@1137
    23
#include <sstream>
deba@1137
    24
deba@1137
    25
#include <map>
deba@1137
    26
#include <vector>
deba@1137
    27
deba@1137
    28
#include <memory>
deba@1137
    29
deba@1137
    30
#include <lemon/invalid.h>
deba@1137
    31
#include <lemon/error.h>
deba@1137
    32
deba@1137
    33
deba@1137
    34
namespace lemon {
deba@1137
    35
deba@1137
    36
  /// \brief Standard WriterTraits for the GraphWriter class.
deba@1137
    37
  ///
deba@1137
    38
  /// Standard WriterTraits for the GraphWriter class.
deba@1137
    39
  /// It defines standard writing method for all type of value. 
deba@1137
    40
  struct DefaultWriterTraits {
deba@1137
    41
deba@1137
    42
    /// \brief Template class for writing an value.
deba@1137
    43
    ///
deba@1137
    44
    /// Template class for writing an value.
deba@1137
    45
    template <typename _Value>
deba@1137
    46
    struct Writer {
deba@1137
    47
      /// The value type.
deba@1137
    48
      typedef _Value Value;
deba@1137
    49
deba@1137
    50
      /// \brief Writes a value from the given stream.
deba@1137
    51
      ///
deba@1137
    52
      /// Writes a value from the given stream.
deba@1137
    53
      void write(std::ostream& os, const Value& value) {
deba@1137
    54
	os << value << '\t';
deba@1137
    55
      }
deba@1137
    56
    };
deba@1137
    57
deba@1137
    58
  };
deba@1137
    59
deba@1137
    60
deba@1137
    61
  /// \brief Writer class for quoted strings.
deba@1137
    62
  ///
deba@1137
    63
  /// Writer class for quoted strings. It can process the escape
deba@1137
    64
  /// sequences in the string.
deba@1137
    65
  class QuotedStringWriter {
deba@1137
    66
  public:
deba@1137
    67
    typedef std::string Value;
deba@1137
    68
deba@1137
    69
    /// \brief Constructor for the writer.
deba@1137
    70
    ///
deba@1137
    71
    /// Constructor for the writer. If the given parameter is true
deba@1137
    72
    /// the writer creates escape sequences from special characters.
deba@1137
    73
    QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
deba@1137
    74
deba@1137
    75
    /// \brief Writes a quoted string from the given stream.
deba@1137
    76
    ///
deba@1137
    77
    /// Writes a quoted string from the given stream.
deba@1137
    78
    void write(std::ostream& os, const std::string& value) {
deba@1137
    79
      os << "\"";
deba@1137
    80
      if (escaped) {
deba@1137
    81
	ostringstream ls;
deba@1137
    82
	for (int i = 0; i < (int)value.size(); ++i) {
deba@1137
    83
	  writeEscape(ls, value[i]);
deba@1137
    84
	}
deba@1137
    85
	os << ls.str();
deba@1137
    86
      } else {
deba@1137
    87
	os << value;
deba@1137
    88
      }
deba@1137
    89
      os << "\"";
deba@1137
    90
    }
deba@1137
    91
deba@1137
    92
  private:
deba@1137
    93
    
deba@1137
    94
    static void writeEscape(std::ostream& os, char c) {
deba@1137
    95
      switch (c) {
deba@1137
    96
      case '\\':
deba@1137
    97
	os << "\\\\";
deba@1137
    98
	return;
deba@1137
    99
      case '\"':
deba@1137
   100
	os << "\\\"";
deba@1137
   101
	return;
deba@1137
   102
      case '\'':
deba@1137
   103
	os << "\\\'";
deba@1137
   104
	return;
deba@1137
   105
      case '\?':
deba@1137
   106
	os << "\\\?";
deba@1137
   107
	return;
deba@1137
   108
      case '\a':
deba@1137
   109
	os << "\\a";
deba@1137
   110
	return;
deba@1137
   111
      case '\b':
deba@1137
   112
	os << "\\b";
deba@1137
   113
	return;
deba@1137
   114
      case '\f':
deba@1137
   115
	os << "\\f";
deba@1137
   116
	return;
deba@1137
   117
      case '\r':
deba@1137
   118
	os << "\\r";
deba@1137
   119
	return;
deba@1137
   120
      case '\n':
deba@1137
   121
	os << "\\n";
deba@1137
   122
	return;
deba@1137
   123
      case '\t':
deba@1137
   124
	os << "\\t";
deba@1137
   125
	return;
deba@1137
   126
      case '\v':
deba@1137
   127
	os << "\\v";
deba@1137
   128
	return;
deba@1137
   129
      default:
deba@1137
   130
	if (c < 0x20) {
deba@1137
   131
	  os << '\\' << oct << (int)c;
deba@1137
   132
	} else {
deba@1137
   133
	  os << c;
deba@1137
   134
	}
deba@1137
   135
	return;
deba@1137
   136
      }     
deba@1137
   137
    }
deba@1137
   138
  private:
deba@1137
   139
    bool escaped;
deba@1137
   140
  };
deba@1137
   141
deba@1137
   142
  
deba@1137
   143
  /// \brief The graph writer class.
deba@1137
   144
  ///
deba@1137
   145
  /// The writer class for the graph output.
alpar@1138
   146
  /// \see \ref GraphReader
alpar@1138
   147
  /// \see \ref graph-io-page
deba@1137
   148
  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
deba@1137
   149
  class GraphWriter {
deba@1137
   150
  public:
deba@1137
   151
    
deba@1137
   152
    typedef _Graph Graph;
deba@1137
   153
    typedef typename Graph::Node Node;
deba@1137
   154
    typedef typename Graph::NodeIt NodeIt;
deba@1137
   155
    typedef typename Graph::Edge Edge;
deba@1137
   156
    typedef typename Graph::EdgeIt EdgeIt;
deba@1137
   157
deba@1137
   158
    typedef _WriterTraits WriterTraits;
deba@1137
   159
 
deba@1137
   160
    /// \brief Construct a new GraphWriter.
deba@1137
   161
    ///
deba@1137
   162
    /// Construct a new GraphWriter. It writes from the given map,
deba@1137
   163
    /// it constructs the given map and it use the given writer as the
deba@1137
   164
    /// default skipper.
deba@1137
   165
    GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
deba@1137
   166
deba@1137
   167
deba@1137
   168
    /// \brief Destruct the graph writer.
deba@1137
   169
    ///
deba@1137
   170
    /// Destruct the graph writer.
deba@1137
   171
    ~GraphWriter() {
deba@1137
   172
      for (typename NodeMapWriters::iterator it = node_map_writers.begin(); 
deba@1137
   173
	   it != node_map_writers.end(); ++it) {
deba@1137
   174
	delete it->second;
deba@1137
   175
      }
deba@1137
   176
deba@1137
   177
      for (typename EdgeMapWriters::iterator it = edge_map_writers.begin();
deba@1137
   178
	   it != edge_map_writers.end(); ++it) {
deba@1137
   179
	delete it->second;
deba@1137
   180
      }
deba@1137
   181
deba@1137
   182
    }
deba@1137
   183
deba@1137
   184
    // Node map rules
deba@1137
   185
deba@1137
   186
    /// \brief Add a new node map writer command for the writer.
deba@1137
   187
    ///
deba@1137
   188
    /// Add a new node map writer command for the writer.
deba@1137
   189
    template <typename Map>
deba@1137
   190
    GraphWriter& addNodeMap(std::string name, const Map& map) {
deba@1137
   191
      return addNodeMap<typename WriterTraits::template Writer<
deba@1137
   192
	typename Map::Value>, Map>(name, map);
deba@1137
   193
    }
deba@1137
   194
deba@1137
   195
    /// \brief Add a new node map writer command for the writer.
deba@1137
   196
    ///
deba@1137
   197
    /// Add a new node map writer command for the writer.
deba@1137
   198
    template <typename Writer, typename Map>
deba@1137
   199
    GraphWriter& addNodeMap(std::string name, const Map& map, 
deba@1137
   200
			      const Writer& writer = Writer()) {
deba@1137
   201
      node_map_writers.push_back(
deba@1137
   202
        make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
deba@1137
   203
      return *this;
deba@1137
   204
    }
deba@1137
   205
deba@1137
   206
    // Edge map rules
deba@1137
   207
deba@1137
   208
    /// \brief Add a new edge map writer command for the writer.
deba@1137
   209
    ///
deba@1137
   210
    /// Add a new edge map writer command for the writer.
deba@1137
   211
    template <typename Map>
deba@1137
   212
    GraphWriter& addEdgeMap(std::string name, const Map& map) { 
deba@1137
   213
      return addEdgeMap<typename WriterTraits::template Writer<
deba@1137
   214
        typename Map::Value>, Map>(name, map);
deba@1137
   215
    }
deba@1137
   216
deba@1137
   217
deba@1137
   218
    /// \brief Add a new edge map writer command for the writer.
deba@1137
   219
    ///
deba@1137
   220
    /// Add a new edge map writer command for the writer.
deba@1137
   221
    template <typename Writer, typename Map>
deba@1137
   222
    GraphWriter& addEdgeMap(std::string name, 
deba@1137
   223
			    const Map& map, const Writer& writer = Writer()) {
deba@1137
   224
      edge_map_writers.push_back(make_pair(name, 
deba@1137
   225
	new MapWriter<Edge, Map, Writer>(map, writer)));
deba@1137
   226
      return *this;
deba@1137
   227
    }
deba@1137
   228
deba@1137
   229
    /// \brief Add a new labeled node writer for the writer.
deba@1137
   230
    ///
deba@1137
   231
    /// Add a new labeled node writer for the writer.
deba@1137
   232
    GraphWriter& addNode(std::string name, const Node& node) {
deba@1137
   233
      node_writers.push_back(make_pair(name, node));
deba@1137
   234
      return *this;
deba@1137
   235
    }
deba@1137
   236
deba@1137
   237
    /// \brief Add a new labeled edge writer for the writer.
deba@1137
   238
    ///
deba@1137
   239
    /// Add a new labeled edge writer for the writer.
deba@1137
   240
    GraphWriter& addEdge(std::string name, const Edge& edge) {
deba@1137
   241
      edge_writers.push_back(make_pair(name, edge));
deba@1137
   242
      return *this;
deba@1137
   243
    }
deba@1137
   244
deba@1137
   245
    /// \brief Executes the writer commands.
deba@1137
   246
    ///
deba@1137
   247
    /// Executes the writer commands.
deba@1137
   248
    void run() {   
deba@1137
   249
      writeNodeSet();
deba@1137
   250
      writeEdgeSet();
deba@1137
   251
      writeNodes();
deba@1137
   252
      writeEdges();
deba@1137
   253
      os << "@end" << std::endl;
deba@1137
   254
    }
deba@1137
   255
deba@1137
   256
  private:
deba@1137
   257
deba@1137
   258
    void writeNodeSet() {
deba@1137
   259
      if (node_map_writers.size() == 0) return;
deba@1137
   260
      os << "@nodeset" << std::endl;
deba@1137
   261
      for (int i = 0; i < (int)node_map_writers.size(); ++i) {
deba@1137
   262
	os << node_map_writers[i].first << '\t';
deba@1137
   263
      } 
deba@1137
   264
      os << std::endl;
deba@1137
   265
      for (NodeIt it(graph); it != INVALID; ++it) {
deba@1137
   266
	for (int i = 0; i < (int)node_map_writers.size(); ++i) {
deba@1137
   267
	  node_map_writers[i].second->write(os, it);
deba@1137
   268
	}
deba@1137
   269
	os << std::endl;
deba@1137
   270
      }
deba@1137
   271
deba@1137
   272
    }
deba@1137
   273
deba@1137
   274
    void writeEdgeSet() {
deba@1137
   275
      if (edge_map_writers.size() == 0) return;
deba@1137
   276
      if (node_map_writers.size() == 0) {
deba@1137
   277
	throw Exception() << "Missing node id map";
deba@1137
   278
      }
deba@1137
   279
      os << "@edgeset" << std::endl;
deba@1137
   280
      os << "\t\t";
deba@1137
   281
      for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
deba@1137
   282
	os << edge_map_writers[i].first << '\t';
deba@1137
   283
      } 
deba@1137
   284
      os << std::endl;
deba@1137
   285
      for (EdgeIt it(graph); it != INVALID; ++it) {
deba@1137
   286
	node_map_writers[0].second->write(os, graph.source(it));
deba@1137
   287
	node_map_writers[0].second->write(os, graph.target(it));
deba@1137
   288
	for (int i = 0; i < (int)edge_map_writers.size(); ++i) {
deba@1137
   289
	  edge_map_writers[i].second->write(os, it);
deba@1137
   290
	}
deba@1137
   291
	os << std::endl;
deba@1137
   292
      }
deba@1137
   293
    }
deba@1137
   294
deba@1137
   295
    void writeNodes() {
deba@1137
   296
      if (node_writers.size() == 0) return;
deba@1137
   297
      if (node_map_writers.size() == 0) {
deba@1137
   298
	throw Exception() << "Missing node id map";
deba@1137
   299
      }
deba@1137
   300
      os << "@nodes" << std::endl;
deba@1137
   301
      for (int i = 0; i < (int)node_writers.size(); ++i) {
deba@1137
   302
	os << node_writers[i].first << '\t';
deba@1137
   303
	node_map_writers[0].second->write(os, node_writers[i].second);
deba@1137
   304
	os << std::endl;
deba@1137
   305
      } 
deba@1137
   306
    }
deba@1137
   307
deba@1137
   308
    void writeEdges() {
deba@1137
   309
      if (edge_writers.size() == 0) return;
deba@1137
   310
      if (edge_map_writers.size() == 0) {
deba@1137
   311
	throw Exception() << "Missing edge id map";
deba@1137
   312
      }
deba@1137
   313
      os << "@edges" << std::endl;
deba@1137
   314
      for (int i = 0; i < (int)edge_writers.size(); ++i) {
deba@1137
   315
	os << edge_writers[i].first << '\t';
deba@1137
   316
	edge_map_writers[0].second->write(os, edge_writers[i].second);
deba@1137
   317
	os << std::endl;
deba@1137
   318
      } 
deba@1137
   319
    }
deba@1137
   320
    
deba@1137
   321
    // Writers
deba@1137
   322
deba@1137
   323
    template <class _Item>
deba@1137
   324
    class WriterBase {
deba@1137
   325
    public:
deba@1137
   326
      typedef _Item Item;
deba@1137
   327
      virtual void write(std::ostream&, const Item&) = 0;
deba@1137
   328
    };
deba@1137
   329
deba@1137
   330
    template <class _Item, typename _Map, typename _Writer>
deba@1137
   331
    class MapWriter : public WriterBase<_Item> {
deba@1137
   332
    public:
deba@1137
   333
      typedef _Map Map;
deba@1137
   334
      typedef _Writer Writer;
deba@1137
   335
      typedef typename Writer::Value Value;
deba@1137
   336
      typedef _Item Item;
deba@1137
   337
      
deba@1137
   338
      const Map& map;
deba@1137
   339
      Writer writer;
deba@1137
   340
deba@1137
   341
      MapWriter(const Map& _map, const Writer& _writer) 
deba@1137
   342
	: map(_map), writer(_writer) {}
deba@1137
   343
deba@1137
   344
deba@1137
   345
      virtual void write(std::ostream& os, const Item& item) {
deba@1137
   346
	writer.write(os, map[item]);
deba@1137
   347
      }
deba@1137
   348
deba@1137
   349
    };
deba@1137
   350
deba@1137
   351
deba@1137
   352
deba@1137
   353
    typedef std::vector< std::pair<std::string, WriterBase<Node>*> > 
deba@1137
   354
      NodeMapWriters;
deba@1137
   355
    NodeMapWriters node_map_writers;
deba@1137
   356
deba@1137
   357
    typedef std::vector< std::pair<std::string, WriterBase<Edge>*> > 
deba@1137
   358
      EdgeMapWriters;
deba@1137
   359
    EdgeMapWriters edge_map_writers;
deba@1137
   360
deba@1137
   361
    typedef std::vector<std::pair<std::string, Node> > NodeWriters;
deba@1137
   362
    NodeWriters node_writers;
deba@1137
   363
deba@1137
   364
    typedef std::vector<std::pair<std::string, Edge> > EdgeWriters;
deba@1137
   365
    EdgeWriters edge_writers;
deba@1137
   366
deba@1137
   367
    std::ostream& os;
deba@1137
   368
    Graph& graph;
deba@1137
   369
deba@1137
   370
  };
deba@1137
   371
deba@1137
   372
deba@1137
   373
}