Location: LEMON/LEMON-official/lemon/lgf_writer.h

Load file history
gravatar
alpar (Alpar Juttner)
More flexible header names in .lgf + largely improved doc
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/* -*- C++ -*-
*
* This file is a part of LEMON, a generic C++ optimization library
*
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
///\ingroup lemon_io
///\file
///\brief Lemon Graph Format writer.
#ifndef LEMON_LGF_WRITER_H
#define LEMON_LGF_WRITER_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <functional>
#include <lemon/assert.h>
#include <lemon/graph_utils.h>
namespace lemon {
namespace _writer_bits {
template <typename Value>
struct DefaultConverter {
std::string operator()(const Value& value) {
std::ostringstream os;
os << value;
return os.str();
}
};
template <typename T>
bool operator<(const T&, const T&) {
throw DataFormatError("Label map is not comparable");
}
template <typename _Map>
class MapLess {
public:
typedef _Map Map;
typedef typename Map::Key Item;
private:
const Map& _map;
public:
MapLess(const Map& map) : _map(map) {}
bool operator()(const Item& left, const Item& right) {
return _map[left] < _map[right];
}
};
template <typename _Item>
class MapStorageBase {
public:
typedef _Item Item;
public:
MapStorageBase() {}
virtual ~MapStorageBase() {}
virtual std::string get(const Item& item) = 0;
virtual void sort(std::vector<Item>&) = 0;
};
template <typename _Item, typename _Map,
typename _Converter = DefaultConverter<typename _Map::Value> >
class MapStorage : public MapStorageBase<_Item> {
public:
typedef _Map Map;
typedef _Converter Converter;
typedef _Item Item;
private:
const Map& _map;
Converter _converter;
public:
MapStorage(const Map& map, const Converter& converter = Converter())
: _map(map), _converter(converter) {}
virtual ~MapStorage() {}
virtual std::string get(const Item& item) {
return _converter(_map[item]);
}
virtual void sort(std::vector<Item>& items) {
MapLess<Map> less(_map);
std::sort(items.begin(), items.end(), less);
}
};
class ValueStorageBase {
public:
ValueStorageBase() {}
virtual ~ValueStorageBase() {}
virtual std::string get() = 0;
};
template <typename _Value, typename _Converter = DefaultConverter<_Value> >
class ValueStorage : public ValueStorageBase {
public:
typedef _Value Value;
typedef _Converter Converter;
private:
const Value& _value;
Converter _converter;
public:
ValueStorage(const Value& value, const Converter& converter = Converter())
: _value(value), _converter(converter) {}
virtual std::string get() {
return _converter(_value);
}
};
template <typename Value>
struct MapLookUpConverter {
const std::map<Value, std::string>& _map;
MapLookUpConverter(const std::map<Value, std::string>& map)
: _map(map) {}
std::string operator()(const Value& str) {
typename std::map<Value, std::string>::const_iterator it =
_map.find(str);
if (it == _map.end()) {
throw DataFormatError("Item not found");
}
return it->second;
}
};
bool isWhiteSpace(char c) {
return c == ' ' || c == '\t' || c == '\v' ||
c == '\n' || c == '\r' || c == '\f';
}
bool isEscaped(char c) {
return c == '\\' || c == '\"' || c == '\'' ||
c == '\a' || c == '\b';
}
static void writeEscape(std::ostream& os, char c) {
switch (c) {
case '\\':
os << "\\\\";
return;
case '\"':
os << "\\\"";
return;
case '\a':
os << "\\a";
return;
case '\b':
os << "\\b";
return;
case '\f':
os << "\\f";
return;
case '\r':
os << "\\r";
return;
case '\n':
os << "\\n";
return;
case '\t':
os << "\\t";
return;
case '\v':
os << "\\v";
return;
default:
if (c < 0x20) {
os << '\\' << std::oct << static_cast<int>(c);
} else {
os << c;
}
return;
}
}
bool requireEscape(const std::string& str) {
if (str.empty() || str[0] == '@') return true;
std::istringstream is(str);
char c;
while (is.get(c)) {
if (isWhiteSpace(c) || isEscaped(c)) {
return true;
}
}
return false;
}
std::ostream& writeToken(std::ostream& os, const std::string& str) {
if (requireEscape(str)) {
os << '\"';
for (std::string::const_iterator it = str.begin();
it != str.end(); ++it) {
writeEscape(os, *it);
}
os << '\"';
} else {
os << str;
}
return os;
}
}
/// \ingroup lemon_io
///
/// \brief LGF writer for directed graphs
///
/// This utility writes an \ref lgf-format "LGF" file.
///
/// The writing method does a batch processing. The user creates a
/// writer object, then various writing rules can be added to the
/// writer, and eventually the writing is executed with the \c run()
/// member function. A map writing rule can be added to the writer
/// with the \c nodeMap() or \c arcMap() members. An optional
/// converter parameter can also be added as a standard functor converting from
/// the value type of the map to std::string. If it is set, it will
/// determine how the map's value type is written to the output
/// stream. If the functor is not set, then a default conversion
/// will be used. The \c attribute(), \c node() and \c arc() functions
/// are used to add attribute writing rules.
///
///\code
/// DigraphWriter<Digraph>(std::cout, digraph).
/// nodeMap("coordinates", coord_map).
/// nodeMap("size", size).
/// nodeMap("title", title).
/// arcMap("capacity", cap_map).
/// node("source", src).
/// node("target", trg).
/// attribute("caption", caption).
/// run();
///\endcode
///
///
/// By default, the writer does not write additional captions to the
/// sections, but they can be give as an optional parameter of
/// the \c nodes(), \c arcs() or \c
/// attributes() functions.
///
/// The \c skipNodes() and \c skipArcs() functions forbid the
/// writing of the sections. If two arc sections should be written to the
/// output, it can be done in two passes, the first pass writes the
/// node section and the first arc section, then the second pass
/// skips the node section and writes just the arc section to the
/// stream. The output stream can be retrieved with the \c ostream()
/// function, hence the second pass can append its output to the output of the
/// first pass.
template <typename _Digraph>
class DigraphWriter {
public:
typedef _Digraph Digraph;
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
private:
std::ostream* _os;
bool local_os;
Digraph& _digraph;
std::string _nodes_caption;
std::string _arcs_caption;
std::string _attributes_caption;
typedef std::map<Node, std::string> NodeIndex;
NodeIndex _node_index;
typedef std::map<Arc, std::string> ArcIndex;
ArcIndex _arc_index;
typedef std::vector<std::pair<std::string,
_writer_bits::MapStorageBase<Node>* > > NodeMaps;
NodeMaps _node_maps;
typedef std::vector<std::pair<std::string,
_writer_bits::MapStorageBase<Arc>* > >ArcMaps;
ArcMaps _arc_maps;
typedef std::vector<std::pair<std::string,
_writer_bits::ValueStorageBase*> > Attributes;
Attributes _attributes;
bool _skip_nodes;
bool _skip_arcs;
public:
/// \brief Constructor
///
/// Construct a directed graph writer, which writes to the given
/// output stream.
DigraphWriter(std::ostream& is, Digraph& digraph)
: _os(&is), local_os(false), _digraph(digraph),
_skip_nodes(false), _skip_arcs(false) {}
/// \brief Constructor
///
/// Construct a directed graph writer, which writes to the given
/// output file.
DigraphWriter(const std::string& fn, Digraph& digraph)
: _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
_skip_nodes(false), _skip_arcs(false) {}
/// \brief Constructor
///
/// Construct a directed graph writer, which writes to the given
/// output file.
DigraphWriter(const char* fn, Digraph& digraph)
: _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
_skip_nodes(false), _skip_arcs(false) {}
/// \brief Copy constructor
///
/// The copy constructor transfers all data from the other writer,
/// therefore the copied writer will not be usable more.
DigraphWriter(DigraphWriter& other)
: _os(other._os), local_os(other.local_os), _digraph(other._digraph),
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
other.is = 0;
other.local_os = false;
_node_index.swap(other._node_index);
_arc_index.swap(other._arc_index);
_node_maps.swap(other._node_maps);
_arc_maps.swap(other._arc_maps);
_attributes.swap(other._attributes);
_nodes_caption = other._nodes_caption;
_arcs_caption = other._arcs_caption;
_attributes_caption = other._attributes_caption;
}
/// \brief Destructor
~DigraphWriter() {
for (typename NodeMaps::iterator it = _node_maps.begin();
it != _node_maps.end(); ++it) {
delete it->second;
}
for (typename ArcMaps::iterator it = _arc_maps.begin();
it != _arc_maps.end(); ++it) {
delete it->second;
}
for (typename Attributes::iterator it = _attributes.begin();
it != _attributes.end(); ++it) {
delete it->second;
}
if (local_os) {
delete _os;
}
}
private:
DigraphWriter& operator=(const DigraphWriter&);
public:
/// \name Writing rules
/// @{
/// \brief Node map reading rule
///
/// Add a node map reading rule to the writer.
template <typename Map>
DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
_writer_bits::MapStorageBase<Node>* storage =
new _writer_bits::MapStorage<Node, Map>(map);
_node_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Node map writing rule
///
/// Add a node map writing rule with specialized converter to the
/// writer.
template <typename Map, typename Converter>
DigraphWriter& nodeMap(const std::string& caption, const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
_writer_bits::MapStorageBase<Node>* storage =
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter);
_node_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Arc map writing rule
///
/// Add an arc map writing rule to the writer.
template <typename Map>
DigraphWriter& arcMap(const std::string& caption, const Map& map) {
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
_writer_bits::MapStorageBase<Arc>* storage =
new _writer_bits::MapStorage<Arc, Map>(map);
_arc_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Arc map writing rule
///
/// Add an arc map writing rule with specialized converter to the
/// writer.
template <typename Map, typename Converter>
DigraphWriter& arcMap(const std::string& caption, const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
_writer_bits::MapStorageBase<Arc>* storage =
new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter);
_arc_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Attribute writing rule
///
/// Add an attribute writing rule to the writer.
template <typename Value>
DigraphWriter& attribute(const std::string& caption, const Value& value) {
_writer_bits::ValueStorageBase* storage =
new _writer_bits::ValueStorage<Value>(value);
_attributes.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Attribute writing rule
///
/// Add an attribute writing rule with specialized converter to the
/// writer.
template <typename Value, typename Converter>
DigraphWriter& attribute(const std::string& caption, const Value& value,
const Converter& converter = Converter()) {
_writer_bits::ValueStorageBase* storage =
new _writer_bits::ValueStorage<Value, Converter>(value, converter);
_attributes.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Node writing rule
///
/// Add a node writing rule to the writer.
DigraphWriter& node(const std::string& caption, const Node& node) {
typedef _writer_bits::MapLookUpConverter<Node> Converter;
Converter converter(_node_index);
_writer_bits::ValueStorageBase* storage =
new _writer_bits::ValueStorage<Node, Converter>(node, converter);
_attributes.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Arc writing rule
///
/// Add an arc writing rule to writer.
DigraphWriter& arc(const std::string& caption, const Arc& arc) {
typedef _writer_bits::MapLookUpConverter<Arc> Converter;
Converter converter(_arc_index);
_writer_bits::ValueStorageBase* storage =
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter);
_attributes.push_back(std::make_pair(caption, storage));
return *this;
}
/// \name Select section by name
/// @{
/// \brief Set \c \@nodes section to be read
///
/// Set \c \@nodes section to be read
DigraphWriter& nodes(const std::string& caption) {
_nodes_caption = caption;
return *this;
}
/// \brief Set \c \@arcs section to be read
///
/// Set \c \@arcs section to be read
DigraphWriter& arcs(const std::string& caption) {
_arcs_caption = caption;
return *this;
}
/// \brief Set \c \@attributes section to be read
///
/// Set \c \@attributes section to be read
DigraphWriter& attributes(const std::string& caption) {
_attributes_caption = caption;
return *this;
}
/// \name Skipping section
/// @{
/// \brief Skip writing the node set
///
/// The \c \@nodes section will be not written to the stream.
DigraphWriter& skipNodes() {
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
return *this;
}
/// \brief Skip writing arc set
///
/// The \c \@arcs section will be not written to the stream.
DigraphWriter& skipArcs() {
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
return *this;
}
/// @}
private:
void writeNodes() {
_writer_bits::MapStorageBase<Node>* label = 0;
for (typename NodeMaps::iterator it = _node_maps.begin();
it != _node_maps.end(); ++it) {
if (it->first == "label") {
label = it->second;
break;
}
}
*_os << "@nodes";
if (!_nodes_caption.empty()) {
_writer_bits::writeToken(*_os << ' ', _nodes_caption);
}
*_os << std::endl;
if (label == 0) {
*_os << "label" << '\t';
}
for (typename NodeMaps::iterator it = _node_maps.begin();
it != _node_maps.end(); ++it) {
_writer_bits::writeToken(*_os, it->first) << '\t';
}
*_os << std::endl;
std::vector<Node> nodes;
for (NodeIt n(_digraph); n != INVALID; ++n) {
nodes.push_back(n);
}
if (label == 0) {
IdMap<Digraph, Node> id_map(_digraph);
_writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map);
std::sort(nodes.begin(), nodes.end(), id_less);
} else {
label->sort(nodes);
}
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
Node n = nodes[i];
if (label == 0) {
std::ostringstream os;
os << _digraph.id(n);
_writer_bits::writeToken(*_os, os.str());
*_os << '\t';
_node_index.insert(std::make_pair(n, os.str()));
}
for (typename NodeMaps::iterator it = _node_maps.begin();
it != _node_maps.end(); ++it) {
std::string value = it->second->get(n);
_writer_bits::writeToken(*_os, value);
if (it->first == "label") {
_node_index.insert(std::make_pair(n, value));
}
*_os << '\t';
}
*_os << std::endl;
}
}
void writeArcs() {
_writer_bits::MapStorageBase<Arc>* label = 0;
for (typename ArcMaps::iterator it = _arc_maps.begin();
it != _arc_maps.end(); ++it) {
if (it->first == "label") {
label = it->second;
break;
}
}
*_os << "@arcs";
if (!_arcs_caption.empty()) {
_writer_bits::writeToken(*_os << ' ', _arcs_caption);
}
*_os << std::endl;
*_os << '\t' << '\t';
if (label == 0) {
*_os << "label" << '\t';
}
for (typename ArcMaps::iterator it = _arc_maps.begin();
it != _arc_maps.end(); ++it) {
_writer_bits::writeToken(*_os, it->first) << '\t';
}
*_os << std::endl;
std::vector<Arc> arcs;
for (ArcIt n(_digraph); n != INVALID; ++n) {
arcs.push_back(n);
}
if (label == 0) {
IdMap<Digraph, Arc> id_map(_digraph);
_writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map);
std::sort(arcs.begin(), arcs.end(), id_less);
} else {
label->sort(arcs);
}
for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
Arc a = arcs[i];
_writer_bits::writeToken(*_os, _node_index.
find(_digraph.source(a))->second);
*_os << '\t';
_writer_bits::writeToken(*_os, _node_index.
find(_digraph.target(a))->second);
*_os << '\t';
if (label == 0) {
std::ostringstream os;
os << _digraph.id(a);
_writer_bits::writeToken(*_os, os.str());
*_os << '\t';
_arc_index.insert(std::make_pair(a, os.str()));
}
for (typename ArcMaps::iterator it = _arc_maps.begin();
it != _arc_maps.end(); ++it) {
std::string value = it->second->get(a);
_writer_bits::writeToken(*_os, value);
if (it->first == "label") {
_arc_index.insert(std::make_pair(a, value));
}
*_os << '\t';
}
*_os << std::endl;
}
}
void writeAttributes() {
if (_attributes.empty()) return;
*_os << "@attributes";
if (!_attributes_caption.empty()) {
_writer_bits::writeToken(*_os << ' ', _attributes_caption);
}
*_os << std::endl;
for (typename Attributes::iterator it = _attributes.begin();
it != _attributes.end(); ++it) {
_writer_bits::writeToken(*_os, it->first) << ' ';
_writer_bits::writeToken(*_os, it->second->get());
*_os << std::endl;
}
}
public:
/// \name Execution of the writer
/// @{
/// \brief Start the batch processing
///
/// This function starts the batch processing
void run() {
if (!_skip_nodes) {
writeNodes();
}
if (!_skip_arcs) {
writeArcs();
}
writeAttributes();
}
/// \brief Gives back the stream of the writer
///
/// Gives back the stream of the writer
std::ostream& ostream() {
return *_os;
}
/// @}
};
/// \relates DigraphWriter
template <typename Digraph>
DigraphWriter<Digraph> digraphWriter(std::istream& is, Digraph& digraph) {
return DigraphWriter<Digraph>(is, digraph);
}
/// \relates DigraphWriter
template <typename Digraph>
DigraphWriter<Digraph> digraphWriter(const std::string& fn,
Digraph& digraph) {
return DigraphWriter<Digraph>(fn, digraph);
}
/// \relates DigraphWriter
template <typename Digraph>
DigraphWriter<Digraph> digraphWriter(const char* fn, Digraph& digraph) {
return DigraphWriter<Digraph>(fn, digraph);
}
}
#endif