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

Load file history
gravatar
deba@inf.elte.hu
Bug fixes and improvements in LGF IO
   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
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
/* -*- 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 reader.
#ifndef LEMON_LGF_READER_H
#define LEMON_LGF_READER_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <lemon/assert.h>
#include <lemon/graph_utils.h>
#include <lemon/lgf_writer.h>
#include <lemon/concept_check.h>
#include <lemon/concepts/maps.h>
namespace lemon {
namespace _reader_bits {
template <typename Value>
struct DefaultConverter {
Value operator()(const std::string& str) {
std::istringstream is(str);
Value value;
is >> value;
char c;
if (is >> std::ws >> c) {
throw DataFormatError("Remaining characters in token");
}
return value;
}
};
template <>
struct DefaultConverter<std::string> {
std::string operator()(const std::string& str) {
return str;
}
};
template <typename _Item>
class MapStorageBase {
public:
typedef _Item Item;
public:
MapStorageBase() {}
virtual ~MapStorageBase() {}
virtual void set(const Item& item, const std::string& value) = 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:
Map& _map;
Converter _converter;
public:
MapStorage(Map& map, const Converter& converter = Converter())
: _map(map), _converter(converter) {}
virtual ~MapStorage() {}
virtual void set(const Item& item ,const std::string& value) {
_map.set(item, _converter(value));
}
};
class ValueStorageBase {
public:
ValueStorageBase() {}
virtual ~ValueStorageBase() {}
virtual void set(const std::string&) = 0;
};
template <typename _Value, typename _Converter = DefaultConverter<_Value> >
class ValueStorage : public ValueStorageBase {
public:
typedef _Value Value;
typedef _Converter Converter;
private:
Value& _value;
Converter _converter;
public:
ValueStorage(Value& value, const Converter& converter = Converter())
: _value(value), _converter(converter) {}
virtual void set(const std::string& value) {
_value = _converter(value);
}
};
template <typename Value>
struct MapLookUpConverter {
const std::map<std::string, Value>& _map;
MapLookUpConverter(const std::map<std::string, Value>& map)
: _map(map) {}
Value operator()(const std::string& str) {
typename std::map<std::string, Value>::const_iterator it =
_map.find(str);
if (it == _map.end()) {
std::ostringstream msg;
msg << "Item not found: " << str;
throw DataFormatError(msg.str().c_str());
}
return it->second;
}
};
bool isWhiteSpace(char c) {
return c == ' ' || c == '\t' || c == '\v' ||
c == '\n' || c == '\r' || c == '\f';
}
bool isOct(char c) {
return '0' <= c && c <='7';
}
int valueOct(char c) {
LEMON_ASSERT(isOct(c), "The character is not octal.");
return c - '0';
}
bool isHex(char c) {
return ('0' <= c && c <= '9') ||
('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z');
}
int valueHex(char c) {
LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
if ('0' <= c && c <= '9') return c - '0';
if ('a' <= c && c <= 'z') return c - 'a' + 10;
return c - 'A' + 10;
}
bool isIdentifierFirstChar(char c) {
return ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') || c == '_';
}
bool isIdentifierChar(char c) {
return isIdentifierFirstChar(c) ||
('0' <= c && c <= '9');
}
char readEscape(std::istream& is) {
char c;
if (!is.get(c))
throw DataFormatError("Escape format error");
switch (c) {
case '\\':
return '\\';
case '\"':
return '\"';
case '\'':
return '\'';
case '\?':
return '\?';
case 'a':
return '\a';
case 'b':
return '\b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case 'v':
return '\v';
case 'x':
{
int code;
if (!is.get(c) || !isHex(c))
throw DataFormatError("Escape format error");
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
else code = code * 16 + valueHex(c);
return code;
}
default:
{
int code;
if (!isOct(c))
throw DataFormatError("Escape format error");
else if (code = valueOct(c), !is.get(c) || !isOct(c))
is.putback(c);
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
is.putback(c);
else code = code * 8 + valueOct(c);
return code;
}
}
}
std::istream& readToken(std::istream& is, std::string& str) {
std::ostringstream os;
char c;
is >> std::ws;
if (!is.get(c))
return is;
if (c == '\"') {
while (is.get(c) && c != '\"') {
if (c == '\\')
c = readEscape(is);
os << c;
}
if (!is)
throw DataFormatError("Quoted format error");
} else {
is.putback(c);
while (is.get(c) && !isWhiteSpace(c)) {
if (c == '\\')
c = readEscape(is);
os << c;
}
if (!is) {
is.clear();
} else {
is.putback(c);
}
}
str = os.str();
return is;
}
class Section {
public:
virtual ~Section() {}
virtual void process(std::istream& is, int& line_num) = 0;
};
template <typename Functor>
class LineSection : public Section {
private:
Functor _functor;
public:
LineSection(const Functor& functor) : _functor(functor) {}
virtual ~LineSection() {}
virtual void process(std::istream& is, int& line_num) {
char c;
std::string line;
while (is.get(c) && c != '@') {
if (c == '\n') {
++line_num;
} else if (c == '#') {
getline(is, line);
++line_num;
} else if (!isWhiteSpace(c)) {
is.putback(c);
getline(is, line);
_functor(line);
++line_num;
}
}
if (is) is.putback(c);
else if (is.eof()) is.clear();
}
};
template <typename Functor>
class StreamSection : public Section {
private:
Functor _functor;
public:
StreamSection(const Functor& functor) : _functor(functor) {}
virtual ~StreamSection() {}
virtual void process(std::istream& is, int& line_num) {
_functor(is, line_num);
char c;
std::string line;
while (is.get(c) && c != '@') {
if (c == '\n') {
++line_num;
} else if (!isWhiteSpace(c)) {
getline(is, line);
++line_num;
}
}
if (is) is.putback(c);
else if (is.eof()) is.clear();
}
};
}
/// \ingroup lemon_io
///
/// \brief LGF reader for directed graphs
///
/// This utility reads an \ref lgf-format "LGF" file.
///
/// The reading method does a batch processing. The user creates a
/// reader object, then various reading rules can be added to the
/// reader, and eventually the reading is executed with the \c run()
/// member function. A map reading rule can be added to the reader
/// with the \c nodeMap() or \c arcMap() members. An optional
/// converter parameter can also be added as a standard functor
/// converting from std::string to the value type of the map. If it
/// is set, it will determine how the tokens in the file should be
/// is converted to the map's value type. If the functor is not set,
/// then a default conversion will be used. One map can be read into
/// multiple map objects at the same time. The \c attribute(), \c
/// node() and \c arc() functions are used to add attribute reading
/// rules.
///
///\code
/// DigraphReader<Digraph>(std::cin, digraph).
/// nodeMap("coordinates", coord_map).
/// arcMap("capacity", cap_map).
/// node("source", src).
/// node("target", trg).
/// attribute("caption", caption).
/// run();
///\endcode
///
/// By default the reader uses the first section in the file of the
/// proper type. If a section has an optional name, then it can be
/// selected for reading by giving an optional name parameter to the
/// \c nodes(), \c arcs() or \c attributes() functions. The readers
/// also can load extra sections with the \c sectionLines() and
/// sectionStream() functions.
///
/// The \c useNodes() and \c useArcs() functions are used to tell the reader
/// that the nodes or arcs should not be constructed (added to the
/// graph) during the reading, but instead the label map of the items
/// are given as a parameter of these functions. An
/// application of these function is multipass reading, which is
/// important if two \e \@arcs sections must be read from the
/// file. In this example the first phase would read the node set and one
/// of the arc sets, while the second phase would read the second arc
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
/// The previously read label node map should be passed to the \c
/// useNodes() functions. Another application of multipass reading when
/// paths are given as a node map or an arc map. It is impossible read this in
/// a single pass, because the arcs are not constructed when the node
/// maps are read.
template <typename _Digraph>
class DigraphReader {
public:
typedef _Digraph Digraph;
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
private:
std::istream* _is;
bool local_is;
Digraph& _digraph;
std::string _nodes_caption;
std::string _arcs_caption;
std::string _attributes_caption;
typedef std::map<std::string, Node> NodeIndex;
NodeIndex _node_index;
typedef std::map<std::string, Arc> ArcIndex;
ArcIndex _arc_index;
typedef std::vector<std::pair<std::string,
_reader_bits::MapStorageBase<Node>*> > NodeMaps;
NodeMaps _node_maps;
typedef std::vector<std::pair<std::string,
_reader_bits::MapStorageBase<Arc>*> >ArcMaps;
ArcMaps _arc_maps;
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
Attributes;
Attributes _attributes;
typedef std::map<std::string, _reader_bits::Section*> Sections;
Sections _sections;
bool _use_nodes;
bool _use_arcs;
int line_num;
std::istringstream line;
public:
/// \brief Constructor
///
/// Construct a directed graph reader, which reads from the given
/// input stream.
DigraphReader(std::istream& is, Digraph& digraph)
: _is(&is), local_is(false), _digraph(digraph),
_use_nodes(false), _use_arcs(false) {}
/// \brief Constructor
///
/// Construct a directed graph reader, which reads from the given
/// file.
DigraphReader(const std::string& fn, Digraph& digraph)
: _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
_use_nodes(false), _use_arcs(false) {}
/// \brief Constructor
///
/// Construct a directed graph reader, which reads from the given
/// file.
DigraphReader(const char* fn, Digraph& digraph)
: _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
_use_nodes(false), _use_arcs(false) {}
/// \brief Copy constructor
///
/// The copy constructor transfers all data from the other reader,
/// therefore the copied reader will not be usable more.
DigraphReader(DigraphReader& other)
: _is(other._is), local_is(other.local_is), _digraph(other._digraph),
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs) {
other._is = 0;
other.local_is = 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;
_sections.swap(other._sections);
}
/// \brief Destructor
~DigraphReader() {
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;
}
for (typename Sections::iterator it = _sections.begin();
it != _sections.end(); ++it) {
delete it->second;
}
if (local_is) {
delete _is;
}
}
private:
DigraphReader& operator=(const DigraphReader&);
public:
/// \name Reading rules
/// @{
/// \brief Node map reading rule
///
/// Add a node map reading rule to the reader.
template <typename Map>
DigraphReader& nodeMap(const std::string& caption, Map& map) {
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Node>* storage =
new _reader_bits::MapStorage<Node, Map>(map);
_node_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Node map reading rule
///
/// Add a node map reading rule with specialized converter to the
/// reader.
template <typename Map, typename Converter>
DigraphReader& nodeMap(const std::string& caption, Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Node>* storage =
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
_node_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Arc map reading rule
///
/// Add an arc map reading rule to the reader.
template <typename Map>
DigraphReader& arcMap(const std::string& caption, Map& map) {
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Arc>* storage =
new _reader_bits::MapStorage<Arc, Map>(map);
_arc_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Arc map reading rule
///
/// Add an arc map reading rule with specialized converter to the
/// reader.
template <typename Map, typename Converter>
DigraphReader& arcMap(const std::string& caption, Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Arc>* storage =
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
_arc_maps.push_back(std::make_pair(caption, storage));
return *this;
}
/// \brief Attribute reading rule
///
/// Add an attribute reading rule to the reader.
template <typename Value>
DigraphReader& attribute(const std::string& caption, Value& value) {
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Value>(value);
_attributes.insert(std::make_pair(caption, storage));
return *this;
}
/// \brief Attribute reading rule
///
/// Add an attribute reading rule with specialized converter to the
/// reader.
template <typename Value, typename Converter>
DigraphReader& attribute(const std::string& caption, Value& value,
const Converter& converter = Converter()) {
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Value, Converter>(value, converter);
_attributes.insert(std::make_pair(caption, storage));
return *this;
}
/// \brief Node reading rule
///
/// Add a node reading rule to reader.
DigraphReader& node(const std::string& caption, Node& node) {
typedef _reader_bits::MapLookUpConverter<Node> Converter;
Converter converter(_node_index);
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Node, Converter>(node, converter);
_attributes.insert(std::make_pair(caption, storage));
return *this;
}
/// \brief Arc reading rule
///
/// Add an arc reading rule to reader.
DigraphReader& arc(const std::string& caption, Arc& arc) {
typedef _reader_bits::MapLookUpConverter<Arc> Converter;
Converter converter(_arc_index);
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
_attributes.insert(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
DigraphReader& 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
DigraphReader& 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
DigraphReader& attributes(const std::string& caption) {
_attributes_caption = caption;
return *this;
}
/// @}
/// \name Section readers
/// @{
/// \brief Add a section processor with line oriented reading
///
/// In the \e LGF file extra sections can be placed, which contain
/// any data in arbitrary format. These sections can be read with
/// this function line by line. The first parameter is the type
/// descriptor of the section, the second is a functor, which
/// takes just one \c std::string parameter. At the reading
/// process, each line of the section will be given to the functor
/// object. However, the empty lines and the comment lines are
/// filtered out, and the leading whitespaces are stipped from
/// each processed string.
///
/// For example let's see a section, which contain several
/// integers, which should be inserted into a vector.
///\code
/// @numbers
/// 12 45 23
/// 4
/// 23 6
///\endcode
///
/// The functor is implemented as an struct:
///\code
/// struct NumberSection {
/// std::vector<int>& _data;
/// NumberSection(std::vector<int>& data) : _data(data) {}
/// void operator()(const std::string& line) {
/// std::istringstream ls(line);
/// int value;
/// while (ls >> value) _data.push_back(value);
/// }
/// };
///
/// // ...
///
/// reader.sectionLines("numbers", NumberSection(vec));
///\endcode
template <typename Functor>
DigraphReader& sectionLines(const std::string& type, Functor functor) {
LEMON_ASSERT(!type.empty(), "Type is not empty.");
LEMON_ASSERT(_sections.find(type) == _sections.end(),
"Multiple reading of section.");
LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
type != "attributes", "Multiple reading of section.");
_sections.insert(std::make_pair(type,
new _reader_bits::LineSection<Functor>(functor)));
return *this;
}
/// \brief Add a section processor with stream oriented reading
///
/// In the \e LGF file extra sections can be placed, which contain
/// any data in arbitrary format. These sections can be read
/// directly with this function. The first parameter is the type
/// of the section, the second is a functor, which takes an \c
/// std::istream& and an int& parameter, the latter regard to the
/// line number of stream. The functor can read the input while
/// the section go on, and the line number should be modified
/// accordingly.
template <typename Functor>
DigraphReader& sectionStream(const std::string& type, Functor functor) {
LEMON_ASSERT(!type.empty(), "Type is not empty.");
LEMON_ASSERT(_sections.find(type) == _sections.end(),
"Multiple reading of section.");
LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
type != "attributes", "Multiple reading of section.");
_sections.insert(std::make_pair(type,
new _reader_bits::StreamSection<Functor>(functor)));
return *this;
}
/// @}
/// \name Using previously constructed node or arc set
/// @{
/// \brief Use previously constructed node set
///
/// Use previously constructed node set, and specify the node
/// label map.
template <typename Map>
DigraphReader& useNodes(const Map& map) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
_use_nodes = true;
_writer_bits::DefaultConverter<typename Map::Value> converter;
for (NodeIt n(_digraph); n != INVALID; ++n) {
_node_index.insert(std::make_pair(converter(map[n]), n));
}
return *this;
}
/// \brief Use previously constructed node set
///
/// Use previously constructed node set, and specify the node
/// label map and a functor which converts the label map values to
/// std::string.
template <typename Map, typename Converter>
DigraphReader& useNodes(const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
_use_nodes = true;
for (NodeIt n(_digraph); n != INVALID; ++n) {
_node_index.insert(std::make_pair(converter(map[n]), n));
}
return *this;
}
/// \brief Use previously constructed arc set
///
/// Use previously constructed arc set, and specify the arc
/// label map.
template <typename Map>
DigraphReader& useArcs(const Map& map) {
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
_use_arcs = true;
_writer_bits::DefaultConverter<typename Map::Value> converter;
for (ArcIt a(_digraph); a != INVALID; ++a) {
_arc_index.insert(std::make_pair(converter(map[a]), a));
}
return *this;
}
/// \brief Use previously constructed arc set
///
/// Use previously constructed arc set, and specify the arc
/// label map and a functor which converts the label map values to
/// std::string.
template <typename Map, typename Converter>
DigraphReader& useArcs(const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
_use_arcs = true;
for (ArcIt a(_digraph); a != INVALID; ++a) {
_arc_index.insert(std::make_pair(converter(map[a]), a));
}
return *this;
}
/// @}
private:
bool readLine() {
std::string str;
while(++line_num, std::getline(*_is, str)) {
line.clear(); line.str(str);
char c;
if (line >> std::ws >> c && c != '#') {
line.putback(c);
return true;
}
}
return false;
}
bool readSuccess() {
return static_cast<bool>(*_is);
}
void skipSection() {
char c;
while (readSuccess() && line >> c && c != '@') {
readLine();
}
line.putback(c);
}
void readNodes() {
std::vector<int> map_index(_node_maps.size());
int map_num, label_index;
if (!readLine())
throw DataFormatError("Cannot find map captions");
{
std::map<std::string, int> maps;
std::string map;
int index = 0;
while (_reader_bits::readToken(line, map)) {
if (maps.find(map) != maps.end()) {
std::ostringstream msg;
msg << "Multiple occurence of node map: " << map;
throw DataFormatError(msg.str().c_str());
}
maps.insert(std::make_pair(map, index));
++index;
}
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
std::map<std::string, int>::iterator jt =
maps.find(_node_maps[i].first);
if (jt == maps.end()) {
std::ostringstream msg;
msg << "Map not found in file: " << _node_maps[i].first;
throw DataFormatError(msg.str().c_str());
}
map_index[i] = jt->second;
}
{
std::map<std::string, int>::iterator jt = maps.find("label");
if (jt == maps.end())
throw DataFormatError("Label map not found in file");
label_index = jt->second;
}
map_num = maps.size();
}
char c;
while (readLine() && line >> c && c != '@') {
line.putback(c);
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
if (!_reader_bits::readToken(line, tokens[i])) {
std::ostringstream msg;
msg << "Column not found (" << i + 1 << ")";
throw DataFormatError(msg.str().c_str());
}
}
if (line >> std::ws >> c)
throw DataFormatError("Extra character on the end of line");
Node n;
if (!_use_nodes) {
n = _digraph.addNode();
_node_index.insert(std::make_pair(tokens[label_index], n));
} else {
typename std::map<std::string, Node>::iterator it =
_node_index.find(tokens[label_index]);
if (it == _node_index.end()) {
std::ostringstream msg;
msg << "Node with label not found: " << tokens[label_index];
throw DataFormatError(msg.str().c_str());
}
n = it->second;
}
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
_node_maps[i].second->set(n, tokens[map_index[i]]);
}
}
if (readSuccess()) {
line.putback(c);
}
}
void readArcs() {
std::vector<int> map_index(_arc_maps.size());
int map_num, label_index;
if (!readLine())
throw DataFormatError("Cannot find map captions");
{
std::map<std::string, int> maps;
std::string map;
int index = 0;
while (_reader_bits::readToken(line, map)) {
if (maps.find(map) != maps.end()) {
std::ostringstream msg;
msg << "Multiple occurence of arc map: " << map;
throw DataFormatError(msg.str().c_str());
}
maps.insert(std::make_pair(map, index));
++index;
}
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
std::map<std::string, int>::iterator jt =
maps.find(_arc_maps[i].first);
if (jt == maps.end()) {
std::ostringstream msg;
msg << "Map not found in file: " << _arc_maps[i].first;
throw DataFormatError(msg.str().c_str());
}
map_index[i] = jt->second;
}
{
std::map<std::string, int>::iterator jt = maps.find("label");
if (jt == maps.end())
throw DataFormatError("Label map not found in file");
label_index = jt->second;
}
map_num = maps.size();
}
char c;
while (readLine() && line >> c && c != '@') {
line.putback(c);
std::string source_token;
std::string target_token;
if (!_reader_bits::readToken(line, source_token))
throw DataFormatError("Source not found");
if (!_reader_bits::readToken(line, target_token))
throw DataFormatError("Source not found");
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
if (!_reader_bits::readToken(line, tokens[i])) {
std::ostringstream msg;
msg << "Column not found (" << i + 1 << ")";
throw DataFormatError(msg.str().c_str());
}
}
if (line >> std::ws >> c)
throw DataFormatError("Extra character on the end of line");
Arc a;
if (!_use_arcs) {
typename NodeIndex::iterator it;
it = _node_index.find(source_token);
if (it == _node_index.end()) {
std::ostringstream msg;
msg << "Item not found: " << source_token;
throw DataFormatError(msg.str().c_str());
}
Node source = it->second;
it = _node_index.find(target_token);
if (it == _node_index.end()) {
std::ostringstream msg;
msg << "Item not found: " << target_token;
throw DataFormatError(msg.str().c_str());
}
Node target = it->second;
a = _digraph.addArc(source, target);
_arc_index.insert(std::make_pair(tokens[label_index], a));
} else {
typename std::map<std::string, Arc>::iterator it =
_arc_index.find(tokens[label_index]);
if (it == _arc_index.end()) {
std::ostringstream msg;
msg << "Arc with label not found: " << tokens[label_index];
throw DataFormatError(msg.str().c_str());
}
a = it->second;
}
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
_arc_maps[i].second->set(a, tokens[map_index[i]]);
}
}
if (readSuccess()) {
line.putback(c);
}
}
void readAttributes() {
std::set<std::string> read_attr;
char c;
while (readLine() && line >> c && c != '@') {
line.putback(c);
std::string attr, token;
if (!_reader_bits::readToken(line, attr))
throw DataFormatError("Attribute name not found");
if (!_reader_bits::readToken(line, token))
throw DataFormatError("Attribute value not found");
if (line >> c)
throw DataFormatError("Extra character on the end of line");
{
std::set<std::string>::iterator it = read_attr.find(attr);
if (it != read_attr.end()) {
std::ostringstream msg;
msg << "Multiple occurence of attribute " << attr;
throw DataFormatError(msg.str().c_str());
}
read_attr.insert(attr);
}
{
typename Attributes::iterator it = _attributes.lower_bound(attr);
while (it != _attributes.end() && it->first == attr) {
it->second->set(token);
++it;
}
}
}
if (readSuccess()) {
line.putback(c);
}
for (typename Attributes::iterator it = _attributes.begin();
it != _attributes.end(); ++it) {
if (read_attr.find(it->first) == read_attr.end()) {
std::ostringstream msg;
msg << "Attribute not found in file: " << it->first;
throw DataFormatError(msg.str().c_str());
}
}
}
public:
/// \name Execution of the reader
/// @{
/// \brief Start the batch processing
///
/// This function starts the batch processing
void run() {
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
if (!*_is) {
throw DataFormatError("Cannot find file");
}
bool nodes_done = false;
bool arcs_done = false;
bool attributes_done = false;
std::set<std::string> extra_sections;
line_num = 0;
readLine();
while (readSuccess()) {
skipSection();
try {
char c;
std::string section, caption;
line >> c;
_reader_bits::readToken(line, section);
_reader_bits::readToken(line, caption);
if (line >> c)
throw DataFormatError("Extra character on the end of line");
if (section == "nodes" && !nodes_done) {
if (_nodes_caption.empty() || _nodes_caption == caption) {
readNodes();
nodes_done = true;
}
} else if ((section == "arcs" || section == "edges") &&
!arcs_done) {
if (_arcs_caption.empty() || _arcs_caption == caption) {
readArcs();
arcs_done = true;
}
} else if (section == "attributes" && !attributes_done) {
if (_attributes_caption.empty() || _attributes_caption == caption) {
readAttributes();
attributes_done = true;
}
} else {
if (extra_sections.find(section) != extra_sections.end()) {
std::ostringstream msg;
msg << "Multiple occurence of section " << section;
throw DataFormatError(msg.str().c_str());
}
Sections::iterator it = _sections.find(section);
if (it != _sections.end()) {
extra_sections.insert(section);
it->second->process(*_is, line_num);
readLine();
} else {
readLine();
skipSection();
}
}
} catch (DataFormatError& error) {
error.line(line_num);
throw;
}
}
if (!nodes_done) {
throw DataFormatError("Section @nodes not found");
}
if (!arcs_done) {
throw DataFormatError("Section @arcs not found");
}
if (!attributes_done && !_attributes.empty()) {
throw DataFormatError("Section @attributes not found");
}
}
/// @}
};
/// \relates DigraphReader
template <typename Digraph>
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
DigraphReader<Digraph> tmp(is, digraph);
return tmp;
}
/// \relates DigraphReader
template <typename Digraph>
DigraphReader<Digraph> digraphReader(const std::string& fn,
Digraph& digraph) {
DigraphReader<Digraph> tmp(fn, digraph);
return tmp;
}
/// \relates DigraphReader
template <typename Digraph>
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
DigraphReader<Digraph> tmp(fn, digraph);
return tmp;
}
}
#endif