lemon/bits/item_writer.h
author deba
Fri, 03 Nov 2006 16:29:32 +0000
changeset 2293 1ee6e8788cc7
parent 2254 50cb2b90daa9
child 2386 81b47fc5c444
permissions -rw-r--r--
First implementation of the static graph class
It could be improved to get better running times on benchmarks
deba@1409
     1
/* -*- C++ -*-
deba@1409
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1409
     8
 *
deba@1409
     9
 * Permission to use, modify and distribute this software is granted
deba@1409
    10
 * provided that this copyright notice appears in all copies. For
deba@1409
    11
 * precise terms see the accompanying LICENSE file.
deba@1409
    12
 *
deba@1409
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1409
    14
 * express or implied, and with no claim as to its suitability for any
deba@1409
    15
 * purpose.
deba@1409
    16
 *
deba@1409
    17
 */
deba@1409
    18
alpar@1946
    19
/// \ingroup item_io
deba@1409
    20
/// \file
deba@1409
    21
/// \brief Item writer bits for lemon output.
deba@1409
    22
deba@1409
    23
#ifndef LEMON_BITS_ITEM_WRITER_H
deba@1409
    24
#define LEMON_BITS_ITEM_WRITER_H
deba@1409
    25
deba@1409
    26
#include <iostream>
deba@2254
    27
#include <sstream>
deba@1409
    28
#include <string>
deba@1409
    29
deba@1409
    30
#include <vector>
deba@1409
    31
#include <deque>
deba@1409
    32
#include <list>
deba@1409
    33
#include <set>
deba@1409
    34
deba@1409
    35
namespace lemon {
deba@1409
    36
  
deba@1409
    37
  template <typename Value>
deba@1409
    38
  class DefaultWriter;
deba@1409
    39
alpar@1946
    40
  /// \ingroup item_io
deba@1409
    41
  /// \brief Writer class for quoted strings.
deba@1409
    42
  ///
deba@2254
    43
  /// Writer class for unformatted strings.
deba@2254
    44
  /// \author Balazs Dezso
deba@2254
    45
  class UnformattedWriter {
deba@2254
    46
  public:
deba@2254
    47
    typedef std::string Value;
deba@2254
    48
deba@2254
    49
    /// \brief Constructor for the writer.
deba@2254
    50
    ///
deba@2254
    51
    /// Constructor for the writer.
deba@2254
    52
    UnformattedWriter() {}
deba@2254
    53
deba@2254
    54
    /// \brief Writes an unformatted string to the given stream.
deba@2254
    55
    ///
deba@2254
    56
    /// Writes an unformatted string to the given stream.
deba@2254
    57
    void write(std::ostream& os, const std::string& value) const {
deba@2254
    58
      os << value;
deba@2254
    59
    }
deba@2254
    60
deba@2254
    61
    bool readable(const std::string& value) const {
deba@2254
    62
      std::istringstream is(value);
deba@2254
    63
      char c;
deba@2254
    64
      while (is.get(c) && !whiteSpace(c)) {
deba@2254
    65
        if (!processChar(c, is)) return false;
deba@2254
    66
      }
deba@2254
    67
      if (is) return false;
deba@2254
    68
      return true;
deba@2254
    69
    }
deba@2254
    70
deba@2254
    71
  private:
deba@2254
    72
deba@2254
    73
    bool processChar(char c, std::istream& is) const {
deba@2254
    74
      switch (c) {
deba@2254
    75
      case '(':
deba@2254
    76
        is.putback(c);
deba@2254
    77
        if (!readableParsed('(', ')', is)) return false;
deba@2254
    78
        break;
deba@2254
    79
      case '[':
deba@2254
    80
        is.putback(c);
deba@2254
    81
        if (!readableParsed('[', ']', is)) return false;
deba@2254
    82
        break;
deba@2254
    83
      case '{':
deba@2254
    84
        is.putback(c);
deba@2254
    85
        if (!readableParsed('{', '}', is)) return false;
deba@2254
    86
        break;
deba@2255
    87
      case '/':
deba@2255
    88
        is.putback(c);
deba@2255
    89
        if (!readableParsed('/', '/', is)) return false;
deba@2255
    90
        break;
deba@2254
    91
      case '\"':
deba@2254
    92
        is.putback(c);
deba@2254
    93
        if (!readableQuoted('\"', is)) return false;
deba@2254
    94
        break;
deba@2254
    95
      case '\'':
deba@2254
    96
        is.putback(c);
deba@2254
    97
        if (!readableQuoted('\'', is)) return false;
deba@2254
    98
        break;
deba@2254
    99
      default:
deba@2254
   100
        break;
deba@2254
   101
      }
deba@2254
   102
      return true;
deba@2254
   103
    }
deba@2254
   104
deba@2254
   105
    bool readableParsed(char open, char close, std::istream& is) const {
deba@2254
   106
      char c;
deba@2254
   107
      if (!is.get(c) || c != open) return false;
deba@2254
   108
      while (is.get(c) && c != close) {
deba@2254
   109
        if (!processChar(c, is)) return false;
deba@2254
   110
      }
deba@2254
   111
      if (!is) return false;
deba@2254
   112
      return true;
deba@2254
   113
    }
deba@2254
   114
deba@2254
   115
    bool readableQuoted(char quote, std::istream& is) const {
deba@2254
   116
      char c;
deba@2254
   117
      bool esc = false;
deba@2254
   118
      if (!is.get(c) || c != quote) return false;
deba@2255
   119
      while (is.get(c) && (c != quote || esc)) {
deba@2254
   120
        if (c == '\\') esc = !esc;
deba@2254
   121
        else esc = false;
deba@2254
   122
      }
deba@2254
   123
      if (!is) return false;
deba@2254
   124
      return true;
deba@2254
   125
    }
deba@2254
   126
deba@2254
   127
    static bool whiteSpace(char c) {
deba@2254
   128
      return c == ' ' || c == '\t' || c == '\v' || 
deba@2254
   129
        c == '\n' || c == '\r' || c == '\f'; 
deba@2254
   130
    }
deba@2254
   131
deba@2254
   132
  };
deba@2254
   133
deba@2254
   134
  /// \ingroup item_io
deba@2254
   135
  /// \brief Writer class for quoted strings.
deba@2254
   136
  ///
deba@1409
   137
  /// Writer class for quoted strings. It can process the escape
deba@1409
   138
  /// sequences in the string.
deba@1409
   139
  /// \author Balazs Dezso
deba@1409
   140
  class QuotedStringWriter {
deba@2254
   141
    friend class QuotedCharWriter;
deba@1409
   142
  public:
deba@1409
   143
    typedef std::string Value;
deba@1409
   144
deba@1409
   145
    /// \brief Constructor for the writer.
deba@1409
   146
    ///
deba@1409
   147
    /// Constructor for the writer. If the given parameter is true
deba@1409
   148
    /// the writer creates escape sequences from special characters.
deba@1409
   149
    QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
deba@1409
   150
deba@1409
   151
    /// \brief Writes a quoted string to the given stream.
deba@1409
   152
    ///
deba@1409
   153
    /// Writes a quoted string to the given stream.
deba@1852
   154
    void write(std::ostream& os, const std::string& value) const {
deba@1409
   155
      os << "\"";
deba@1409
   156
      if (escaped) {
klao@1535
   157
	std::ostringstream ls;
deba@1409
   158
	for (int i = 0; i < (int)value.size(); ++i) {
deba@1409
   159
	  writeEscape(ls, value[i]);
deba@1409
   160
	}
deba@1409
   161
	os << ls.str();
deba@1409
   162
      } else {
deba@1409
   163
	os << value;
deba@1409
   164
      }
deba@1409
   165
      os << "\"";
deba@1409
   166
    }
deba@1409
   167
deba@1409
   168
  private:
deba@1409
   169
    
deba@1409
   170
    static void writeEscape(std::ostream& os, char c) {
deba@1409
   171
      switch (c) {
deba@1409
   172
      case '\\':
deba@1409
   173
	os << "\\\\";
deba@1409
   174
	return;
deba@1409
   175
      case '\"':
deba@1409
   176
	os << "\\\"";
deba@1409
   177
	return;
deba@1409
   178
      case '\'':
deba@1409
   179
	os << "\\\'";
deba@1409
   180
	return;
deba@1409
   181
      case '\?':
deba@1409
   182
	os << "\\\?";
deba@1409
   183
	return;
deba@1409
   184
      case '\a':
deba@1409
   185
	os << "\\a";
deba@1409
   186
	return;
deba@1409
   187
      case '\b':
deba@1409
   188
	os << "\\b";
deba@1409
   189
	return;
deba@1409
   190
      case '\f':
deba@1409
   191
	os << "\\f";
deba@1409
   192
	return;
deba@1409
   193
      case '\r':
deba@1409
   194
	os << "\\r";
deba@1409
   195
	return;
deba@1409
   196
      case '\n':
deba@1409
   197
	os << "\\n";
deba@1409
   198
	return;
deba@1409
   199
      case '\t':
deba@1409
   200
	os << "\\t";
deba@1409
   201
	return;
deba@1409
   202
      case '\v':
deba@1409
   203
	os << "\\v";
deba@1409
   204
	return;
deba@1409
   205
      default:
deba@1409
   206
	if (c < 0x20) {
deba@1409
   207
	  os << '\\' << std::oct << (int)c;
deba@1409
   208
	} else {
deba@1409
   209
	  os << c;
deba@1409
   210
	}
deba@1409
   211
	return;
deba@1409
   212
      }     
deba@1409
   213
    }
deba@1409
   214
  private:
deba@1409
   215
    bool escaped;
deba@1409
   216
  };
deba@1409
   217
alpar@1946
   218
  /// \ingroup item_io
deba@2254
   219
  /// \brief Writer class for quoted chars.
deba@2254
   220
  ///
deba@2254
   221
  /// Writer class for quoted char. It can process the escape
deba@2254
   222
  /// sequences in the string.
deba@2254
   223
  /// \author Balazs Dezso
deba@2254
   224
  class QuotedCharWriter {
deba@2254
   225
  public:
deba@2254
   226
    typedef char Value;
deba@2254
   227
deba@2254
   228
    /// \brief Constructor for the writer.
deba@2254
   229
    ///
deba@2254
   230
    /// Constructor for the writer. If the given parameter is true
deba@2254
   231
    /// the writer creates escape sequences from special characters.
deba@2254
   232
    QuotedCharWriter(bool _escaped = true) : escaped(_escaped) {}
deba@2254
   233
deba@2254
   234
    /// \brief Writes a quoted char to the given stream.
deba@2254
   235
    ///
deba@2254
   236
    /// Writes a quoted char to the given stream.
deba@2254
   237
    void write(std::ostream& os, const char& value) const {
deba@2254
   238
      os << "\'";
deba@2254
   239
      if (escaped) {
deba@2254
   240
	std::ostringstream ls;
deba@2254
   241
        QuotedStringWriter::writeEscape(ls, value);
deba@2254
   242
	os << ls.str();
deba@2254
   243
      } else {
deba@2254
   244
	os << value;
deba@2254
   245
      }
deba@2254
   246
      os << "\'";
deba@2254
   247
    }
deba@2254
   248
deba@2254
   249
  private:
deba@2254
   250
    bool escaped;
deba@2254
   251
  };
deba@2254
   252
deba@2254
   253
  /// \ingroup item_io
deba@1533
   254
  /// \brief Writer class for quoted char array.
deba@1533
   255
  ///
deba@1533
   256
  /// Writer class for quoted char array. It can process the escape
deba@1533
   257
  /// sequences in the char array.
deba@1533
   258
  /// \author Balazs Dezso
deba@1533
   259
  class QuotedCharArrayWriter {
deba@1533
   260
  public:
deba@1533
   261
    typedef const char* Value;
deba@1533
   262
deba@1533
   263
    /// \brief Constructor for the writer.
deba@1533
   264
    ///
deba@1533
   265
    /// Constructor for the writer. If the given parameter is true
deba@1533
   266
    /// the writer creates escape sequences from special characters.
deba@1533
   267
    QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
deba@1533
   268
deba@1533
   269
    /// \brief Writes a quoted char array to the given stream.
deba@1533
   270
    ///
deba@1533
   271
    /// Writes a quoted char array to the given stream.
deba@1852
   272
    void write(std::ostream& os, const char* value) const {
deba@1533
   273
      QuotedStringWriter(escaped).write(os, std::string(value));
deba@1533
   274
    }
deba@1533
   275
deba@1533
   276
  private:    
deba@1533
   277
    bool escaped;
deba@1533
   278
  };
deba@1533
   279
deba@1533
   280
alpar@1946
   281
  /// \ingroup item_io
deba@1409
   282
  ///
deba@1409
   283
  /// \brief Writer for standard containers.
deba@1409
   284
  ///
deba@1409
   285
  /// Writer for each iterable standard containers. The representation
deba@1409
   286
  /// of the container is the values enumerated between an open and a
deba@1409
   287
  /// close parse. 
deba@1409
   288
  ///
deba@1409
   289
  /// \author Balazs Dezso
deba@1409
   290
  template <
deba@1409
   291
    typename _Container, 
deba@1409
   292
    typename _ItemWriter = DefaultWriter<typename _Container::value_type> 
deba@1409
   293
  >
deba@1409
   294
  class IterableWriter {
deba@1409
   295
  public:
deba@1409
   296
    typedef _Container Value;
deba@1409
   297
    typedef _ItemWriter ItemWriter;
deba@1409
   298
deba@1409
   299
  private:
deba@1409
   300
deba@1409
   301
    ItemWriter item_writer;
deba@1409
   302
deba@1409
   303
  public:
deba@1409
   304
deba@1852
   305
    IterableWriter(const ItemWriter& _item_writer = ItemWriter())
deba@1852
   306
      : item_writer(_item_writer) {}
deba@1852
   307
deba@1409
   308
    /// \brief Writes the values of the container to the given stream.
deba@1409
   309
    ///
deba@1409
   310
    /// Writes the values of the container to the given stream.
deba@1409
   311
    void write(std::ostream& os, const Value& value) const {
deba@1409
   312
      typename Value::const_iterator it;
deba@1409
   313
      os << '(';
deba@1409
   314
      for (it = value.begin(); it != value.end(); ++it) {
deba@1409
   315
	item_writer.write(os, *it);
deba@1409
   316
	os << ' ';
deba@1409
   317
      }
deba@1409
   318
      os << ')';
deba@1409
   319
    }
deba@1409
   320
deba@1409
   321
  };
deba@1409
   322
alpar@1946
   323
  /// \ingroup item_io
deba@1852
   324
  ///
deba@1852
   325
  /// \brief Writer for standard pairs.
deba@1852
   326
  ///
deba@1852
   327
  /// Writer for standard pairs. The representation of a pair is
alpar@1946
   328
  ///\code ( first_value => second_value ) \endcode.
deba@1852
   329
  /// \author Balazs Dezso
deba@1852
   330
  template <typename _Pair, 
deba@1852
   331
	    typename _FirstWriter = 
deba@1852
   332
	    DefaultWriter<typename _Pair::first_type>,
deba@1852
   333
	    typename _SecondWriter = 
deba@1852
   334
	    DefaultWriter<typename _Pair::second_type> >
deba@1852
   335
  class PairWriter {
deba@1852
   336
  public:
deba@1852
   337
deba@1852
   338
    typedef _Pair Value;
deba@1852
   339
deba@1852
   340
    typedef _FirstWriter FirstWriter;
deba@1852
   341
    typedef _SecondWriter SecondWriter;
deba@1852
   342
deba@1852
   343
  private:
deba@1852
   344
deba@1852
   345
    FirstWriter first_writer;
deba@1852
   346
    SecondWriter second_writer;
deba@1852
   347
deba@1852
   348
  public:
deba@1852
   349
    
deba@1852
   350
    /// \brief Constructor.
deba@1852
   351
    ///
deba@1852
   352
    /// Constructor for the PairWriter.
deba@1852
   353
    PairWriter(const FirstWriter& _first_writer = FirstWriter(), 
deba@1852
   354
	       const SecondWriter& _second_writer = SecondWriter()) 
deba@1852
   355
      : first_writer(_first_writer), second_writer(_second_writer) {}
deba@1852
   356
    
deba@1852
   357
    /// \brief Writes the pair from the given stream.
deba@1852
   358
    ///
deba@1852
   359
    /// Writes the pair from the given stream.
deba@1852
   360
    void write(std::ostream& os, const Value& value) const {
deba@1852
   361
      os << "( ";
deba@1852
   362
      first_writer.write(os, value.first);
deba@1852
   363
      os << " => ";
deba@1852
   364
      second_writer.write(os, value.second);
deba@1852
   365
      os << " )";
deba@1852
   366
    }
deba@1852
   367
deba@1852
   368
  };
deba@1852
   369
alpar@1946
   370
  /// \ingroup item_io
deba@1409
   371
  /// 
deba@1409
   372
  /// \brief The default item writer template class.
deba@1409
   373
  ///
deba@1409
   374
  /// The default item writer template class. If some section writer
deba@1409
   375
  /// needs to write a value to the stream it will give the default way for it.
deba@1409
   376
  ///
deba@1409
   377
  /// \author Balazs Dezso
deba@1409
   378
  template <typename _Value>
deba@1409
   379
  class DefaultWriter {
deba@1409
   380
  public:
deba@1409
   381
    /// The value type.
deba@1409
   382
    typedef _Value Value;
deba@1409
   383
    /// \brief Writes the value to the given stream.
deba@1409
   384
    ///
deba@1409
   385
    /// Writes the value to the given stream.
deba@1409
   386
    void write(std::ostream& os, const Value& value) const {
deba@1409
   387
      os << value;
deba@1409
   388
    }
deba@1409
   389
  };
deba@1409
   390
deba@1429
   391
  template <>
deba@2254
   392
  class DefaultWriter<std::string> {
deba@2254
   393
  public:
deba@2254
   394
    typedef std::string Value;
deba@2254
   395
    
deba@2254
   396
    void write(std::ostream& os, const Value& value) const {
deba@2254
   397
      if (UnformattedWriter().readable(value)) {
deba@2254
   398
        UnformattedWriter().write(os, value);
deba@2254
   399
      } else {
deba@2254
   400
        QuotedStringWriter().write(os, value);
deba@2254
   401
      }
deba@2254
   402
    }
deba@2254
   403
      
deba@2254
   404
  };
deba@2254
   405
deba@2254
   406
  template <>
deba@2254
   407
  class DefaultWriter<char> 
deba@2254
   408
    : public QuotedCharWriter {};
deba@2254
   409
deba@2254
   410
  template <>
deba@2254
   411
  class DefaultWriter<bool> {
deba@2254
   412
  public:
deba@2254
   413
    typedef bool Value;
deba@2254
   414
    
deba@2254
   415
    void write(std::ostream& os, const Value& value) const {
deba@2254
   416
      os << (value ? "1" : "0");
deba@2254
   417
    }
deba@2254
   418
      
deba@2254
   419
  };
deba@1429
   420
deba@1533
   421
  template <int length>
deba@1533
   422
  class DefaultWriter<char[length]> 
deba@1533
   423
    : public QuotedCharArrayWriter {};
deba@1533
   424
deba@1533
   425
  template <int length>
deba@1533
   426
  class DefaultWriter<const char[length]> 
deba@1533
   427
    : public QuotedCharArrayWriter {};
deba@1533
   428
deba@1852
   429
  template <>
deba@1852
   430
  class DefaultWriter<char*> 
deba@1852
   431
    : public QuotedCharArrayWriter {};
deba@1852
   432
deba@1852
   433
  template <>
deba@1852
   434
  class DefaultWriter<const char*> 
deba@1852
   435
    : public QuotedCharArrayWriter {};
deba@1852
   436
deba@1409
   437
  template <typename Item>
deba@1409
   438
  class DefaultWriter<std::vector<Item> > 
deba@1409
   439
    : public IterableWriter<std::vector<Item> > {};
deba@1409
   440
deba@1409
   441
  template <typename Item>
deba@1409
   442
  class DefaultWriter<std::deque<Item> > 
deba@1409
   443
    : public IterableWriter<std::deque<Item> > {};
deba@1409
   444
deba@1409
   445
  template <typename Item>
deba@1409
   446
  class DefaultWriter<std::list<Item> > 
deba@1409
   447
    : public IterableWriter<std::list<Item> > {};
deba@1409
   448
  
deba@1409
   449
  template <typename Item>
deba@1409
   450
  class DefaultWriter<std::set<Item> > 
deba@1409
   451
    : public IterableWriter<std::set<Item> > {};
deba@1409
   452
deba@1852
   453
  template <typename Key, typename Value>
deba@1852
   454
  class DefaultWriter<std::map<Key, Value> > 
deba@1852
   455
    : public IterableWriter<std::map<Key, Value> > {};
deba@1852
   456
deba@1409
   457
  template <typename Item>
deba@1409
   458
  class DefaultWriter<std::multiset<Item> > 
deba@1409
   459
    : public IterableWriter<std::multiset<Item> > {};
deba@1409
   460
deba@1852
   461
  template <typename Key, typename Value>
deba@1852
   462
  class DefaultWriter<std::multimap<Key, Value> > 
deba@1852
   463
    : public IterableWriter<std::multimap<Key, Value> > {};
deba@1852
   464
deba@1852
   465
  template <typename First, typename Second>
deba@1852
   466
  class DefaultWriter<std::pair<First, Second> > 
deba@1852
   467
    : public PairWriter<std::pair<First, Second> > {};
deba@1852
   468
alpar@1946
   469
  /// \ingroup item_io
deba@1409
   470
  /// \brief Standard WriterTraits for the section writers.
deba@1409
   471
  ///
deba@1409
   472
  /// Standard WriterTraits for the section writers.
deba@1409
   473
  /// It defines standard writing method for all type of value. 
deba@1409
   474
  /// \author Balazs Dezso
deba@1409
   475
  struct DefaultWriterTraits {
deba@1409
   476
deba@1409
   477
    template <typename _Value>
deba@1533
   478
    struct Writer : DefaultWriter<_Value> {
deba@1533
   479
      typedef DefaultWriter<_Value> Parent;
deba@1533
   480
    };
deba@1409
   481
deba@1409
   482
  };
deba@1409
   483
deba@1409
   484
}
deba@1409
   485
deba@1409
   486
#endif