[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[127] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[127] | 4 | * |
---|
| 5 | * Copyright (C) 2003-2008 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | ///\ingroup lemon_io |
---|
| 20 | ///\file |
---|
[236] | 21 | ///\brief \ref lgf-format "LEMON Graph Format" reader. |
---|
[127] | 22 | |
---|
| 23 | |
---|
| 24 | #ifndef LEMON_LGF_READER_H |
---|
| 25 | #define LEMON_LGF_READER_H |
---|
| 26 | |
---|
| 27 | #include <iostream> |
---|
| 28 | #include <fstream> |
---|
| 29 | #include <sstream> |
---|
| 30 | |
---|
| 31 | #include <set> |
---|
| 32 | #include <map> |
---|
| 33 | |
---|
| 34 | #include <lemon/assert.h> |
---|
[220] | 35 | #include <lemon/core.h> |
---|
[127] | 36 | |
---|
| 37 | #include <lemon/lgf_writer.h> |
---|
| 38 | |
---|
| 39 | #include <lemon/concept_check.h> |
---|
| 40 | #include <lemon/concepts/maps.h> |
---|
| 41 | |
---|
| 42 | namespace lemon { |
---|
| 43 | |
---|
| 44 | namespace _reader_bits { |
---|
| 45 | |
---|
| 46 | template <typename Value> |
---|
| 47 | struct DefaultConverter { |
---|
| 48 | Value operator()(const std::string& str) { |
---|
[209] | 49 | std::istringstream is(str); |
---|
| 50 | Value value; |
---|
[290] | 51 | if (!(is >> value)) { |
---|
| 52 | throw FormatError("Cannot read token"); |
---|
| 53 | } |
---|
[209] | 54 | |
---|
| 55 | char c; |
---|
| 56 | if (is >> std::ws >> c) { |
---|
[290] | 57 | throw FormatError("Remaining characters in token"); |
---|
[209] | 58 | } |
---|
| 59 | return value; |
---|
[127] | 60 | } |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | template <> |
---|
| 64 | struct DefaultConverter<std::string> { |
---|
| 65 | std::string operator()(const std::string& str) { |
---|
[209] | 66 | return str; |
---|
[127] | 67 | } |
---|
| 68 | }; |
---|
| 69 | |
---|
[209] | 70 | template <typename _Item> |
---|
[127] | 71 | class MapStorageBase { |
---|
| 72 | public: |
---|
| 73 | typedef _Item Item; |
---|
| 74 | |
---|
| 75 | public: |
---|
| 76 | MapStorageBase() {} |
---|
| 77 | virtual ~MapStorageBase() {} |
---|
| 78 | |
---|
| 79 | virtual void set(const Item& item, const std::string& value) = 0; |
---|
| 80 | |
---|
| 81 | }; |
---|
| 82 | |
---|
[209] | 83 | template <typename _Item, typename _Map, |
---|
| 84 | typename _Converter = DefaultConverter<typename _Map::Value> > |
---|
[127] | 85 | class MapStorage : public MapStorageBase<_Item> { |
---|
| 86 | public: |
---|
| 87 | typedef _Map Map; |
---|
| 88 | typedef _Converter Converter; |
---|
| 89 | typedef _Item Item; |
---|
[209] | 90 | |
---|
[127] | 91 | private: |
---|
| 92 | Map& _map; |
---|
| 93 | Converter _converter; |
---|
| 94 | |
---|
| 95 | public: |
---|
[209] | 96 | MapStorage(Map& map, const Converter& converter = Converter()) |
---|
| 97 | : _map(map), _converter(converter) {} |
---|
[127] | 98 | virtual ~MapStorage() {} |
---|
| 99 | |
---|
| 100 | virtual void set(const Item& item ,const std::string& value) { |
---|
[209] | 101 | _map.set(item, _converter(value)); |
---|
[127] | 102 | } |
---|
| 103 | }; |
---|
| 104 | |
---|
[209] | 105 | template <typename _Graph, bool _dir, typename _Map, |
---|
| 106 | typename _Converter = DefaultConverter<typename _Map::Value> > |
---|
[165] | 107 | class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> { |
---|
| 108 | public: |
---|
| 109 | typedef _Map Map; |
---|
| 110 | typedef _Converter Converter; |
---|
| 111 | typedef _Graph Graph; |
---|
| 112 | typedef typename Graph::Edge Item; |
---|
| 113 | static const bool dir = _dir; |
---|
[209] | 114 | |
---|
[165] | 115 | private: |
---|
| 116 | const Graph& _graph; |
---|
| 117 | Map& _map; |
---|
| 118 | Converter _converter; |
---|
| 119 | |
---|
| 120 | public: |
---|
[209] | 121 | GraphArcMapStorage(const Graph& graph, Map& map, |
---|
| 122 | const Converter& converter = Converter()) |
---|
| 123 | : _graph(graph), _map(map), _converter(converter) {} |
---|
[165] | 124 | virtual ~GraphArcMapStorage() {} |
---|
| 125 | |
---|
| 126 | virtual void set(const Item& item ,const std::string& value) { |
---|
[209] | 127 | _map.set(_graph.direct(item, dir), _converter(value)); |
---|
[165] | 128 | } |
---|
| 129 | }; |
---|
| 130 | |
---|
[127] | 131 | class ValueStorageBase { |
---|
| 132 | public: |
---|
| 133 | ValueStorageBase() {} |
---|
| 134 | virtual ~ValueStorageBase() {} |
---|
| 135 | |
---|
| 136 | virtual void set(const std::string&) = 0; |
---|
| 137 | }; |
---|
| 138 | |
---|
| 139 | template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
---|
| 140 | class ValueStorage : public ValueStorageBase { |
---|
| 141 | public: |
---|
| 142 | typedef _Value Value; |
---|
| 143 | typedef _Converter Converter; |
---|
| 144 | |
---|
| 145 | private: |
---|
| 146 | Value& _value; |
---|
| 147 | Converter _converter; |
---|
| 148 | |
---|
| 149 | public: |
---|
| 150 | ValueStorage(Value& value, const Converter& converter = Converter()) |
---|
[212] | 151 | : _value(value), _converter(converter) {} |
---|
[127] | 152 | |
---|
| 153 | virtual void set(const std::string& value) { |
---|
[209] | 154 | _value = _converter(value); |
---|
[127] | 155 | } |
---|
| 156 | }; |
---|
| 157 | |
---|
| 158 | template <typename Value> |
---|
| 159 | struct MapLookUpConverter { |
---|
| 160 | const std::map<std::string, Value>& _map; |
---|
| 161 | |
---|
| 162 | MapLookUpConverter(const std::map<std::string, Value>& map) |
---|
| 163 | : _map(map) {} |
---|
| 164 | |
---|
| 165 | Value operator()(const std::string& str) { |
---|
| 166 | typename std::map<std::string, Value>::const_iterator it = |
---|
| 167 | _map.find(str); |
---|
| 168 | if (it == _map.end()) { |
---|
| 169 | std::ostringstream msg; |
---|
| 170 | msg << "Item not found: " << str; |
---|
[290] | 171 | throw FormatError(msg.str()); |
---|
[127] | 172 | } |
---|
| 173 | return it->second; |
---|
| 174 | } |
---|
| 175 | }; |
---|
| 176 | |
---|
[165] | 177 | template <typename Graph> |
---|
| 178 | struct GraphArcLookUpConverter { |
---|
| 179 | const Graph& _graph; |
---|
| 180 | const std::map<std::string, typename Graph::Edge>& _map; |
---|
[209] | 181 | |
---|
| 182 | GraphArcLookUpConverter(const Graph& graph, |
---|
| 183 | const std::map<std::string, |
---|
| 184 | typename Graph::Edge>& map) |
---|
| 185 | : _graph(graph), _map(map) {} |
---|
| 186 | |
---|
[165] | 187 | typename Graph::Arc operator()(const std::string& str) { |
---|
[209] | 188 | if (str.empty() || (str[0] != '+' && str[0] != '-')) { |
---|
[290] | 189 | throw FormatError("Item must start with '+' or '-'"); |
---|
[209] | 190 | } |
---|
| 191 | typename std::map<std::string, typename Graph::Edge> |
---|
| 192 | ::const_iterator it = _map.find(str.substr(1)); |
---|
| 193 | if (it == _map.end()) { |
---|
[290] | 194 | throw FormatError("Item not found"); |
---|
[209] | 195 | } |
---|
| 196 | return _graph.direct(it->second, str[0] == '+'); |
---|
[165] | 197 | } |
---|
| 198 | }; |
---|
| 199 | |
---|
[197] | 200 | inline bool isWhiteSpace(char c) { |
---|
[209] | 201 | return c == ' ' || c == '\t' || c == '\v' || |
---|
| 202 | c == '\n' || c == '\r' || c == '\f'; |
---|
[127] | 203 | } |
---|
[209] | 204 | |
---|
[197] | 205 | inline bool isOct(char c) { |
---|
[209] | 206 | return '0' <= c && c <='7'; |
---|
[127] | 207 | } |
---|
[209] | 208 | |
---|
[197] | 209 | inline int valueOct(char c) { |
---|
[127] | 210 | LEMON_ASSERT(isOct(c), "The character is not octal."); |
---|
| 211 | return c - '0'; |
---|
| 212 | } |
---|
| 213 | |
---|
[197] | 214 | inline bool isHex(char c) { |
---|
[209] | 215 | return ('0' <= c && c <= '9') || |
---|
| 216 | ('a' <= c && c <= 'z') || |
---|
| 217 | ('A' <= c && c <= 'Z'); |
---|
[127] | 218 | } |
---|
[209] | 219 | |
---|
[197] | 220 | inline int valueHex(char c) { |
---|
[127] | 221 | LEMON_ASSERT(isHex(c), "The character is not hexadecimal."); |
---|
| 222 | if ('0' <= c && c <= '9') return c - '0'; |
---|
| 223 | if ('a' <= c && c <= 'z') return c - 'a' + 10; |
---|
| 224 | return c - 'A' + 10; |
---|
| 225 | } |
---|
| 226 | |
---|
[197] | 227 | inline bool isIdentifierFirstChar(char c) { |
---|
[127] | 228 | return ('a' <= c && c <= 'z') || |
---|
[209] | 229 | ('A' <= c && c <= 'Z') || c == '_'; |
---|
[127] | 230 | } |
---|
| 231 | |
---|
[197] | 232 | inline bool isIdentifierChar(char c) { |
---|
[127] | 233 | return isIdentifierFirstChar(c) || |
---|
[209] | 234 | ('0' <= c && c <= '9'); |
---|
[127] | 235 | } |
---|
| 236 | |
---|
[197] | 237 | inline char readEscape(std::istream& is) { |
---|
[127] | 238 | char c; |
---|
| 239 | if (!is.get(c)) |
---|
[290] | 240 | throw FormatError("Escape format error"); |
---|
[127] | 241 | |
---|
| 242 | switch (c) { |
---|
| 243 | case '\\': |
---|
[209] | 244 | return '\\'; |
---|
[127] | 245 | case '\"': |
---|
[209] | 246 | return '\"'; |
---|
[127] | 247 | case '\'': |
---|
[209] | 248 | return '\''; |
---|
[127] | 249 | case '\?': |
---|
[209] | 250 | return '\?'; |
---|
[127] | 251 | case 'a': |
---|
[209] | 252 | return '\a'; |
---|
[127] | 253 | case 'b': |
---|
[209] | 254 | return '\b'; |
---|
[127] | 255 | case 'f': |
---|
[209] | 256 | return '\f'; |
---|
[127] | 257 | case 'n': |
---|
[209] | 258 | return '\n'; |
---|
[127] | 259 | case 'r': |
---|
[209] | 260 | return '\r'; |
---|
[127] | 261 | case 't': |
---|
[209] | 262 | return '\t'; |
---|
[127] | 263 | case 'v': |
---|
[209] | 264 | return '\v'; |
---|
[127] | 265 | case 'x': |
---|
[209] | 266 | { |
---|
| 267 | int code; |
---|
| 268 | if (!is.get(c) || !isHex(c)) |
---|
[290] | 269 | throw FormatError("Escape format error"); |
---|
[209] | 270 | else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); |
---|
| 271 | else code = code * 16 + valueHex(c); |
---|
| 272 | return code; |
---|
| 273 | } |
---|
[127] | 274 | default: |
---|
[209] | 275 | { |
---|
| 276 | int code; |
---|
| 277 | if (!isOct(c)) |
---|
[290] | 278 | throw FormatError("Escape format error"); |
---|
[209] | 279 | else if (code = valueOct(c), !is.get(c) || !isOct(c)) |
---|
| 280 | is.putback(c); |
---|
| 281 | else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) |
---|
| 282 | is.putback(c); |
---|
| 283 | else code = code * 8 + valueOct(c); |
---|
| 284 | return code; |
---|
| 285 | } |
---|
| 286 | } |
---|
[127] | 287 | } |
---|
[209] | 288 | |
---|
[197] | 289 | inline std::istream& readToken(std::istream& is, std::string& str) { |
---|
[127] | 290 | std::ostringstream os; |
---|
| 291 | |
---|
| 292 | char c; |
---|
| 293 | is >> std::ws; |
---|
[209] | 294 | |
---|
| 295 | if (!is.get(c)) |
---|
| 296 | return is; |
---|
[127] | 297 | |
---|
| 298 | if (c == '\"') { |
---|
[209] | 299 | while (is.get(c) && c != '\"') { |
---|
| 300 | if (c == '\\') |
---|
| 301 | c = readEscape(is); |
---|
| 302 | os << c; |
---|
| 303 | } |
---|
| 304 | if (!is) |
---|
[290] | 305 | throw FormatError("Quoted format error"); |
---|
[127] | 306 | } else { |
---|
[209] | 307 | is.putback(c); |
---|
| 308 | while (is.get(c) && !isWhiteSpace(c)) { |
---|
| 309 | if (c == '\\') |
---|
| 310 | c = readEscape(is); |
---|
| 311 | os << c; |
---|
| 312 | } |
---|
| 313 | if (!is) { |
---|
| 314 | is.clear(); |
---|
| 315 | } else { |
---|
| 316 | is.putback(c); |
---|
| 317 | } |
---|
[127] | 318 | } |
---|
| 319 | str = os.str(); |
---|
| 320 | return is; |
---|
| 321 | } |
---|
[162] | 322 | |
---|
| 323 | class Section { |
---|
| 324 | public: |
---|
| 325 | virtual ~Section() {} |
---|
| 326 | virtual void process(std::istream& is, int& line_num) = 0; |
---|
| 327 | }; |
---|
| 328 | |
---|
| 329 | template <typename Functor> |
---|
| 330 | class LineSection : public Section { |
---|
| 331 | private: |
---|
| 332 | |
---|
| 333 | Functor _functor; |
---|
| 334 | |
---|
| 335 | public: |
---|
[209] | 336 | |
---|
[162] | 337 | LineSection(const Functor& functor) : _functor(functor) {} |
---|
| 338 | virtual ~LineSection() {} |
---|
| 339 | |
---|
| 340 | virtual void process(std::istream& is, int& line_num) { |
---|
[209] | 341 | char c; |
---|
| 342 | std::string line; |
---|
| 343 | while (is.get(c) && c != '@') { |
---|
| 344 | if (c == '\n') { |
---|
| 345 | ++line_num; |
---|
| 346 | } else if (c == '#') { |
---|
| 347 | getline(is, line); |
---|
| 348 | ++line_num; |
---|
| 349 | } else if (!isWhiteSpace(c)) { |
---|
| 350 | is.putback(c); |
---|
| 351 | getline(is, line); |
---|
| 352 | _functor(line); |
---|
| 353 | ++line_num; |
---|
| 354 | } |
---|
| 355 | } |
---|
| 356 | if (is) is.putback(c); |
---|
| 357 | else if (is.eof()) is.clear(); |
---|
[162] | 358 | } |
---|
| 359 | }; |
---|
| 360 | |
---|
| 361 | template <typename Functor> |
---|
| 362 | class StreamSection : public Section { |
---|
| 363 | private: |
---|
| 364 | |
---|
| 365 | Functor _functor; |
---|
| 366 | |
---|
| 367 | public: |
---|
[209] | 368 | |
---|
[162] | 369 | StreamSection(const Functor& functor) : _functor(functor) {} |
---|
[209] | 370 | virtual ~StreamSection() {} |
---|
[162] | 371 | |
---|
| 372 | virtual void process(std::istream& is, int& line_num) { |
---|
[209] | 373 | _functor(is, line_num); |
---|
| 374 | char c; |
---|
| 375 | std::string line; |
---|
| 376 | while (is.get(c) && c != '@') { |
---|
| 377 | if (c == '\n') { |
---|
| 378 | ++line_num; |
---|
| 379 | } else if (!isWhiteSpace(c)) { |
---|
| 380 | getline(is, line); |
---|
| 381 | ++line_num; |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | if (is) is.putback(c); |
---|
| 385 | else if (is.eof()) is.clear(); |
---|
[162] | 386 | } |
---|
| 387 | }; |
---|
[209] | 388 | |
---|
[127] | 389 | } |
---|
[156] | 390 | |
---|
[190] | 391 | template <typename Digraph> |
---|
| 392 | class DigraphReader; |
---|
| 393 | |
---|
| 394 | template <typename Digraph> |
---|
| 395 | DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph); |
---|
| 396 | |
---|
| 397 | template <typename Digraph> |
---|
| 398 | DigraphReader<Digraph> digraphReader(const std::string& fn, Digraph& digraph); |
---|
| 399 | |
---|
| 400 | template <typename Digraph> |
---|
| 401 | DigraphReader<Digraph> digraphReader(const char *fn, Digraph& digraph); |
---|
| 402 | |
---|
[156] | 403 | /// \ingroup lemon_io |
---|
[209] | 404 | /// |
---|
[192] | 405 | /// \brief \ref lgf-format "LGF" reader for directed graphs |
---|
[156] | 406 | /// |
---|
| 407 | /// This utility reads an \ref lgf-format "LGF" file. |
---|
| 408 | /// |
---|
| 409 | /// The reading method does a batch processing. The user creates a |
---|
| 410 | /// reader object, then various reading rules can be added to the |
---|
| 411 | /// reader, and eventually the reading is executed with the \c run() |
---|
| 412 | /// member function. A map reading rule can be added to the reader |
---|
| 413 | /// with the \c nodeMap() or \c arcMap() members. An optional |
---|
[162] | 414 | /// converter parameter can also be added as a standard functor |
---|
[192] | 415 | /// converting from \c std::string to the value type of the map. If it |
---|
[162] | 416 | /// is set, it will determine how the tokens in the file should be |
---|
[192] | 417 | /// converted to the value type of the map. If the functor is not set, |
---|
[162] | 418 | /// then a default conversion will be used. One map can be read into |
---|
| 419 | /// multiple map objects at the same time. The \c attribute(), \c |
---|
| 420 | /// node() and \c arc() functions are used to add attribute reading |
---|
| 421 | /// rules. |
---|
[156] | 422 | /// |
---|
| 423 | ///\code |
---|
[192] | 424 | /// DigraphReader<Digraph>(std::cin, digraph). |
---|
| 425 | /// nodeMap("coordinates", coord_map). |
---|
| 426 | /// arcMap("capacity", cap_map). |
---|
| 427 | /// node("source", src). |
---|
| 428 | /// node("target", trg). |
---|
| 429 | /// attribute("caption", caption). |
---|
| 430 | /// run(); |
---|
[156] | 431 | ///\endcode |
---|
| 432 | /// |
---|
| 433 | /// By default the reader uses the first section in the file of the |
---|
| 434 | /// proper type. If a section has an optional name, then it can be |
---|
[162] | 435 | /// selected for reading by giving an optional name parameter to the |
---|
[189] | 436 | /// \c nodes(), \c arcs() or \c attributes() functions. |
---|
[156] | 437 | /// |
---|
| 438 | /// The \c useNodes() and \c useArcs() functions are used to tell the reader |
---|
| 439 | /// that the nodes or arcs should not be constructed (added to the |
---|
| 440 | /// graph) during the reading, but instead the label map of the items |
---|
| 441 | /// are given as a parameter of these functions. An |
---|
[192] | 442 | /// application of these functions is multipass reading, which is |
---|
| 443 | /// important if two \c \@arcs sections must be read from the |
---|
| 444 | /// file. In this case the first phase would read the node set and one |
---|
[156] | 445 | /// of the arc sets, while the second phase would read the second arc |
---|
| 446 | /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
---|
| 447 | /// The previously read label node map should be passed to the \c |
---|
| 448 | /// useNodes() functions. Another application of multipass reading when |
---|
[210] | 449 | /// paths are given as a node map or an arc map. |
---|
| 450 | /// It is impossible to read this in |
---|
[156] | 451 | /// a single pass, because the arcs are not constructed when the node |
---|
| 452 | /// maps are read. |
---|
[127] | 453 | template <typename _Digraph> |
---|
| 454 | class DigraphReader { |
---|
| 455 | public: |
---|
| 456 | |
---|
| 457 | typedef _Digraph Digraph; |
---|
[148] | 458 | TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
---|
[209] | 459 | |
---|
[127] | 460 | private: |
---|
| 461 | |
---|
| 462 | |
---|
| 463 | std::istream* _is; |
---|
| 464 | bool local_is; |
---|
[290] | 465 | std::string _filename; |
---|
[127] | 466 | |
---|
| 467 | Digraph& _digraph; |
---|
| 468 | |
---|
| 469 | std::string _nodes_caption; |
---|
| 470 | std::string _arcs_caption; |
---|
| 471 | std::string _attributes_caption; |
---|
| 472 | |
---|
| 473 | typedef std::map<std::string, Node> NodeIndex; |
---|
| 474 | NodeIndex _node_index; |
---|
| 475 | typedef std::map<std::string, Arc> ArcIndex; |
---|
| 476 | ArcIndex _arc_index; |
---|
[209] | 477 | |
---|
| 478 | typedef std::vector<std::pair<std::string, |
---|
| 479 | _reader_bits::MapStorageBase<Node>*> > NodeMaps; |
---|
| 480 | NodeMaps _node_maps; |
---|
[127] | 481 | |
---|
| 482 | typedef std::vector<std::pair<std::string, |
---|
| 483 | _reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
---|
| 484 | ArcMaps _arc_maps; |
---|
| 485 | |
---|
[209] | 486 | typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
---|
[127] | 487 | Attributes; |
---|
| 488 | Attributes _attributes; |
---|
| 489 | |
---|
| 490 | bool _use_nodes; |
---|
| 491 | bool _use_arcs; |
---|
| 492 | |
---|
[188] | 493 | bool _skip_nodes; |
---|
| 494 | bool _skip_arcs; |
---|
| 495 | |
---|
[127] | 496 | int line_num; |
---|
| 497 | std::istringstream line; |
---|
| 498 | |
---|
| 499 | public: |
---|
| 500 | |
---|
[156] | 501 | /// \brief Constructor |
---|
| 502 | /// |
---|
| 503 | /// Construct a directed graph reader, which reads from the given |
---|
| 504 | /// input stream. |
---|
[209] | 505 | DigraphReader(std::istream& is, Digraph& digraph) |
---|
[127] | 506 | : _is(&is), local_is(false), _digraph(digraph), |
---|
[209] | 507 | _use_nodes(false), _use_arcs(false), |
---|
| 508 | _skip_nodes(false), _skip_arcs(false) {} |
---|
[127] | 509 | |
---|
[156] | 510 | /// \brief Constructor |
---|
| 511 | /// |
---|
| 512 | /// Construct a directed graph reader, which reads from the given |
---|
| 513 | /// file. |
---|
[209] | 514 | DigraphReader(const std::string& fn, Digraph& digraph) |
---|
[290] | 515 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
---|
| 516 | _filename(fn), _digraph(digraph), |
---|
[212] | 517 | _use_nodes(false), _use_arcs(false), |
---|
[290] | 518 | _skip_nodes(false), _skip_arcs(false) { |
---|
| 519 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 520 | } |
---|
[209] | 521 | |
---|
[156] | 522 | /// \brief Constructor |
---|
| 523 | /// |
---|
| 524 | /// Construct a directed graph reader, which reads from the given |
---|
| 525 | /// file. |
---|
[209] | 526 | DigraphReader(const char* fn, Digraph& digraph) |
---|
[290] | 527 | : _is(new std::ifstream(fn)), local_is(true), |
---|
| 528 | _filename(fn), _digraph(digraph), |
---|
[212] | 529 | _use_nodes(false), _use_arcs(false), |
---|
[290] | 530 | _skip_nodes(false), _skip_arcs(false) { |
---|
| 531 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 532 | } |
---|
[127] | 533 | |
---|
[156] | 534 | /// \brief Destructor |
---|
[127] | 535 | ~DigraphReader() { |
---|
[209] | 536 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 537 | it != _node_maps.end(); ++it) { |
---|
| 538 | delete it->second; |
---|
[127] | 539 | } |
---|
| 540 | |
---|
[209] | 541 | for (typename ArcMaps::iterator it = _arc_maps.begin(); |
---|
| 542 | it != _arc_maps.end(); ++it) { |
---|
| 543 | delete it->second; |
---|
[127] | 544 | } |
---|
| 545 | |
---|
[209] | 546 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 547 | it != _attributes.end(); ++it) { |
---|
| 548 | delete it->second; |
---|
[127] | 549 | } |
---|
| 550 | |
---|
| 551 | if (local_is) { |
---|
[209] | 552 | delete _is; |
---|
[127] | 553 | } |
---|
| 554 | |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | private: |
---|
[190] | 558 | |
---|
[209] | 559 | friend DigraphReader<Digraph> digraphReader<>(std::istream& is, |
---|
| 560 | Digraph& digraph); |
---|
| 561 | friend DigraphReader<Digraph> digraphReader<>(const std::string& fn, |
---|
| 562 | Digraph& digraph); |
---|
| 563 | friend DigraphReader<Digraph> digraphReader<>(const char *fn, |
---|
| 564 | Digraph& digraph); |
---|
| 565 | |
---|
| 566 | DigraphReader(DigraphReader& other) |
---|
[190] | 567 | : _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
---|
[209] | 568 | _use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
---|
| 569 | _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { |
---|
[190] | 570 | |
---|
| 571 | other._is = 0; |
---|
| 572 | other.local_is = false; |
---|
[209] | 573 | |
---|
[190] | 574 | _node_index.swap(other._node_index); |
---|
| 575 | _arc_index.swap(other._arc_index); |
---|
| 576 | |
---|
| 577 | _node_maps.swap(other._node_maps); |
---|
| 578 | _arc_maps.swap(other._arc_maps); |
---|
| 579 | _attributes.swap(other._attributes); |
---|
| 580 | |
---|
| 581 | _nodes_caption = other._nodes_caption; |
---|
| 582 | _arcs_caption = other._arcs_caption; |
---|
| 583 | _attributes_caption = other._attributes_caption; |
---|
| 584 | |
---|
| 585 | } |
---|
| 586 | |
---|
[127] | 587 | DigraphReader& operator=(const DigraphReader&); |
---|
| 588 | |
---|
| 589 | public: |
---|
| 590 | |
---|
[156] | 591 | /// \name Reading rules |
---|
| 592 | /// @{ |
---|
[209] | 593 | |
---|
[156] | 594 | /// \brief Node map reading rule |
---|
| 595 | /// |
---|
| 596 | /// Add a node map reading rule to the reader. |
---|
[127] | 597 | template <typename Map> |
---|
| 598 | DigraphReader& nodeMap(const std::string& caption, Map& map) { |
---|
| 599 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 600 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 601 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
[127] | 602 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 603 | return *this; |
---|
| 604 | } |
---|
| 605 | |
---|
[156] | 606 | /// \brief Node map reading rule |
---|
| 607 | /// |
---|
| 608 | /// Add a node map reading rule with specialized converter to the |
---|
| 609 | /// reader. |
---|
[127] | 610 | template <typename Map, typename Converter> |
---|
[209] | 611 | DigraphReader& nodeMap(const std::string& caption, Map& map, |
---|
| 612 | const Converter& converter = Converter()) { |
---|
[127] | 613 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 614 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 615 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
[127] | 616 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 617 | return *this; |
---|
| 618 | } |
---|
| 619 | |
---|
[156] | 620 | /// \brief Arc map reading rule |
---|
| 621 | /// |
---|
| 622 | /// Add an arc map reading rule to the reader. |
---|
[127] | 623 | template <typename Map> |
---|
| 624 | DigraphReader& arcMap(const std::string& caption, Map& map) { |
---|
| 625 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 626 | _reader_bits::MapStorageBase<Arc>* storage = |
---|
| 627 | new _reader_bits::MapStorage<Arc, Map>(map); |
---|
[127] | 628 | _arc_maps.push_back(std::make_pair(caption, storage)); |
---|
| 629 | return *this; |
---|
| 630 | } |
---|
| 631 | |
---|
[156] | 632 | /// \brief Arc map reading rule |
---|
| 633 | /// |
---|
| 634 | /// Add an arc map reading rule with specialized converter to the |
---|
| 635 | /// reader. |
---|
[127] | 636 | template <typename Map, typename Converter> |
---|
[209] | 637 | DigraphReader& arcMap(const std::string& caption, Map& map, |
---|
| 638 | const Converter& converter = Converter()) { |
---|
[127] | 639 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 640 | _reader_bits::MapStorageBase<Arc>* storage = |
---|
| 641 | new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter); |
---|
[127] | 642 | _arc_maps.push_back(std::make_pair(caption, storage)); |
---|
| 643 | return *this; |
---|
| 644 | } |
---|
| 645 | |
---|
[156] | 646 | /// \brief Attribute reading rule |
---|
| 647 | /// |
---|
| 648 | /// Add an attribute reading rule to the reader. |
---|
[127] | 649 | template <typename Value> |
---|
| 650 | DigraphReader& attribute(const std::string& caption, Value& value) { |
---|
[209] | 651 | _reader_bits::ValueStorageBase* storage = |
---|
| 652 | new _reader_bits::ValueStorage<Value>(value); |
---|
[127] | 653 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 654 | return *this; |
---|
| 655 | } |
---|
| 656 | |
---|
[156] | 657 | /// \brief Attribute reading rule |
---|
| 658 | /// |
---|
| 659 | /// Add an attribute reading rule with specialized converter to the |
---|
| 660 | /// reader. |
---|
[127] | 661 | template <typename Value, typename Converter> |
---|
[209] | 662 | DigraphReader& attribute(const std::string& caption, Value& value, |
---|
| 663 | const Converter& converter = Converter()) { |
---|
| 664 | _reader_bits::ValueStorageBase* storage = |
---|
| 665 | new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
---|
[127] | 666 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 667 | return *this; |
---|
| 668 | } |
---|
| 669 | |
---|
[156] | 670 | /// \brief Node reading rule |
---|
| 671 | /// |
---|
| 672 | /// Add a node reading rule to reader. |
---|
[127] | 673 | DigraphReader& node(const std::string& caption, Node& node) { |
---|
| 674 | typedef _reader_bits::MapLookUpConverter<Node> Converter; |
---|
| 675 | Converter converter(_node_index); |
---|
[209] | 676 | _reader_bits::ValueStorageBase* storage = |
---|
| 677 | new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
---|
[127] | 678 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 679 | return *this; |
---|
| 680 | } |
---|
| 681 | |
---|
[156] | 682 | /// \brief Arc reading rule |
---|
| 683 | /// |
---|
| 684 | /// Add an arc reading rule to reader. |
---|
[127] | 685 | DigraphReader& arc(const std::string& caption, Arc& arc) { |
---|
| 686 | typedef _reader_bits::MapLookUpConverter<Arc> Converter; |
---|
| 687 | Converter converter(_arc_index); |
---|
[209] | 688 | _reader_bits::ValueStorageBase* storage = |
---|
| 689 | new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
---|
[127] | 690 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 691 | return *this; |
---|
| 692 | } |
---|
| 693 | |
---|
[156] | 694 | /// @} |
---|
| 695 | |
---|
| 696 | /// \name Select section by name |
---|
| 697 | /// @{ |
---|
| 698 | |
---|
| 699 | /// \brief Set \c \@nodes section to be read |
---|
| 700 | /// |
---|
| 701 | /// Set \c \@nodes section to be read |
---|
[127] | 702 | DigraphReader& nodes(const std::string& caption) { |
---|
| 703 | _nodes_caption = caption; |
---|
| 704 | return *this; |
---|
| 705 | } |
---|
| 706 | |
---|
[156] | 707 | /// \brief Set \c \@arcs section to be read |
---|
| 708 | /// |
---|
| 709 | /// Set \c \@arcs section to be read |
---|
[127] | 710 | DigraphReader& arcs(const std::string& caption) { |
---|
| 711 | _arcs_caption = caption; |
---|
| 712 | return *this; |
---|
| 713 | } |
---|
| 714 | |
---|
[156] | 715 | /// \brief Set \c \@attributes section to be read |
---|
| 716 | /// |
---|
| 717 | /// Set \c \@attributes section to be read |
---|
[127] | 718 | DigraphReader& attributes(const std::string& caption) { |
---|
| 719 | _attributes_caption = caption; |
---|
| 720 | return *this; |
---|
| 721 | } |
---|
| 722 | |
---|
[156] | 723 | /// @} |
---|
| 724 | |
---|
| 725 | /// \name Using previously constructed node or arc set |
---|
| 726 | /// @{ |
---|
| 727 | |
---|
| 728 | /// \brief Use previously constructed node set |
---|
| 729 | /// |
---|
| 730 | /// Use previously constructed node set, and specify the node |
---|
| 731 | /// label map. |
---|
[127] | 732 | template <typename Map> |
---|
| 733 | DigraphReader& useNodes(const Map& map) { |
---|
| 734 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 735 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[127] | 736 | _use_nodes = true; |
---|
| 737 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 738 | for (NodeIt n(_digraph); n != INVALID; ++n) { |
---|
[209] | 739 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[127] | 740 | } |
---|
| 741 | return *this; |
---|
| 742 | } |
---|
| 743 | |
---|
[156] | 744 | /// \brief Use previously constructed node set |
---|
| 745 | /// |
---|
| 746 | /// Use previously constructed node set, and specify the node |
---|
| 747 | /// label map and a functor which converts the label map values to |
---|
[192] | 748 | /// \c std::string. |
---|
[127] | 749 | template <typename Map, typename Converter> |
---|
[209] | 750 | DigraphReader& useNodes(const Map& map, |
---|
| 751 | const Converter& converter = Converter()) { |
---|
[127] | 752 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 753 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[127] | 754 | _use_nodes = true; |
---|
| 755 | for (NodeIt n(_digraph); n != INVALID; ++n) { |
---|
[209] | 756 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[127] | 757 | } |
---|
| 758 | return *this; |
---|
| 759 | } |
---|
| 760 | |
---|
[156] | 761 | /// \brief Use previously constructed arc set |
---|
| 762 | /// |
---|
| 763 | /// Use previously constructed arc set, and specify the arc |
---|
| 764 | /// label map. |
---|
[127] | 765 | template <typename Map> |
---|
| 766 | DigraphReader& useArcs(const Map& map) { |
---|
| 767 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
| 768 | LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
---|
| 769 | _use_arcs = true; |
---|
| 770 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 771 | for (ArcIt a(_digraph); a != INVALID; ++a) { |
---|
[209] | 772 | _arc_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[127] | 773 | } |
---|
| 774 | return *this; |
---|
| 775 | } |
---|
| 776 | |
---|
[156] | 777 | /// \brief Use previously constructed arc set |
---|
| 778 | /// |
---|
| 779 | /// Use previously constructed arc set, and specify the arc |
---|
| 780 | /// label map and a functor which converts the label map values to |
---|
[192] | 781 | /// \c std::string. |
---|
[127] | 782 | template <typename Map, typename Converter> |
---|
[209] | 783 | DigraphReader& useArcs(const Map& map, |
---|
| 784 | const Converter& converter = Converter()) { |
---|
[127] | 785 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 786 | LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
---|
[127] | 787 | _use_arcs = true; |
---|
| 788 | for (ArcIt a(_digraph); a != INVALID; ++a) { |
---|
[209] | 789 | _arc_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[127] | 790 | } |
---|
| 791 | return *this; |
---|
| 792 | } |
---|
| 793 | |
---|
[188] | 794 | /// \brief Skips the reading of node section |
---|
| 795 | /// |
---|
| 796 | /// Omit the reading of the node section. This implies that each node |
---|
[192] | 797 | /// map reading rule will be abandoned, and the nodes of the graph |
---|
[188] | 798 | /// will not be constructed, which usually cause that the arc set |
---|
[192] | 799 | /// could not be read due to lack of node name resolving. |
---|
| 800 | /// Therefore \c skipArcs() function should also be used, or |
---|
| 801 | /// \c useNodes() should be used to specify the label of the nodes. |
---|
[188] | 802 | DigraphReader& skipNodes() { |
---|
[209] | 803 | LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
---|
[188] | 804 | _skip_nodes = true; |
---|
| 805 | return *this; |
---|
| 806 | } |
---|
| 807 | |
---|
| 808 | /// \brief Skips the reading of arc section |
---|
| 809 | /// |
---|
| 810 | /// Omit the reading of the arc section. This implies that each arc |
---|
[192] | 811 | /// map reading rule will be abandoned, and the arcs of the graph |
---|
[188] | 812 | /// will not be constructed. |
---|
| 813 | DigraphReader& skipArcs() { |
---|
[209] | 814 | LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); |
---|
[188] | 815 | _skip_arcs = true; |
---|
| 816 | return *this; |
---|
| 817 | } |
---|
| 818 | |
---|
[156] | 819 | /// @} |
---|
| 820 | |
---|
[127] | 821 | private: |
---|
| 822 | |
---|
| 823 | bool readLine() { |
---|
| 824 | std::string str; |
---|
| 825 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 826 | line.clear(); line.str(str); |
---|
| 827 | char c; |
---|
| 828 | if (line >> std::ws >> c && c != '#') { |
---|
| 829 | line.putback(c); |
---|
| 830 | return true; |
---|
| 831 | } |
---|
[127] | 832 | } |
---|
| 833 | return false; |
---|
| 834 | } |
---|
| 835 | |
---|
| 836 | bool readSuccess() { |
---|
| 837 | return static_cast<bool>(*_is); |
---|
| 838 | } |
---|
[209] | 839 | |
---|
[127] | 840 | void skipSection() { |
---|
| 841 | char c; |
---|
| 842 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 843 | readLine(); |
---|
[127] | 844 | } |
---|
| 845 | line.putback(c); |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | void readNodes() { |
---|
| 849 | |
---|
| 850 | std::vector<int> map_index(_node_maps.size()); |
---|
| 851 | int map_num, label_index; |
---|
| 852 | |
---|
[186] | 853 | char c; |
---|
| 854 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 855 | if (readSuccess() && line) line.putback(c); |
---|
| 856 | if (!_node_maps.empty()) |
---|
[290] | 857 | throw FormatError("Cannot find map names"); |
---|
[209] | 858 | return; |
---|
[186] | 859 | } |
---|
| 860 | line.putback(c); |
---|
| 861 | |
---|
[127] | 862 | { |
---|
[209] | 863 | std::map<std::string, int> maps; |
---|
| 864 | |
---|
| 865 | std::string map; |
---|
| 866 | int index = 0; |
---|
| 867 | while (_reader_bits::readToken(line, map)) { |
---|
| 868 | if (maps.find(map) != maps.end()) { |
---|
| 869 | std::ostringstream msg; |
---|
| 870 | msg << "Multiple occurence of node map: " << map; |
---|
[290] | 871 | throw FormatError(msg.str()); |
---|
[209] | 872 | } |
---|
| 873 | maps.insert(std::make_pair(map, index)); |
---|
| 874 | ++index; |
---|
| 875 | } |
---|
| 876 | |
---|
| 877 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 878 | std::map<std::string, int>::iterator jt = |
---|
| 879 | maps.find(_node_maps[i].first); |
---|
| 880 | if (jt == maps.end()) { |
---|
| 881 | std::ostringstream msg; |
---|
| 882 | msg << "Map not found in file: " << _node_maps[i].first; |
---|
[290] | 883 | throw FormatError(msg.str()); |
---|
[209] | 884 | } |
---|
| 885 | map_index[i] = jt->second; |
---|
| 886 | } |
---|
| 887 | |
---|
| 888 | { |
---|
| 889 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 890 | if (jt != maps.end()) { |
---|
| 891 | label_index = jt->second; |
---|
| 892 | } else { |
---|
| 893 | label_index = -1; |
---|
| 894 | } |
---|
| 895 | } |
---|
| 896 | map_num = maps.size(); |
---|
[127] | 897 | } |
---|
| 898 | |
---|
| 899 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 900 | line.putback(c); |
---|
| 901 | |
---|
| 902 | std::vector<std::string> tokens(map_num); |
---|
| 903 | for (int i = 0; i < map_num; ++i) { |
---|
| 904 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 905 | std::ostringstream msg; |
---|
| 906 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 907 | throw FormatError(msg.str()); |
---|
[209] | 908 | } |
---|
| 909 | } |
---|
| 910 | if (line >> std::ws >> c) |
---|
[290] | 911 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 912 | |
---|
| 913 | Node n; |
---|
| 914 | if (!_use_nodes) { |
---|
| 915 | n = _digraph.addNode(); |
---|
| 916 | if (label_index != -1) |
---|
| 917 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
---|
| 918 | } else { |
---|
| 919 | if (label_index == -1) |
---|
[290] | 920 | throw FormatError("Label map not found in file"); |
---|
[209] | 921 | typename std::map<std::string, Node>::iterator it = |
---|
| 922 | _node_index.find(tokens[label_index]); |
---|
| 923 | if (it == _node_index.end()) { |
---|
| 924 | std::ostringstream msg; |
---|
| 925 | msg << "Node with label not found: " << tokens[label_index]; |
---|
[290] | 926 | throw FormatError(msg.str()); |
---|
[209] | 927 | } |
---|
| 928 | n = it->second; |
---|
| 929 | } |
---|
| 930 | |
---|
| 931 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 932 | _node_maps[i].second->set(n, tokens[map_index[i]]); |
---|
| 933 | } |
---|
[127] | 934 | |
---|
| 935 | } |
---|
| 936 | if (readSuccess()) { |
---|
[209] | 937 | line.putback(c); |
---|
[127] | 938 | } |
---|
| 939 | } |
---|
| 940 | |
---|
| 941 | void readArcs() { |
---|
| 942 | |
---|
| 943 | std::vector<int> map_index(_arc_maps.size()); |
---|
| 944 | int map_num, label_index; |
---|
| 945 | |
---|
[186] | 946 | char c; |
---|
| 947 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 948 | if (readSuccess() && line) line.putback(c); |
---|
| 949 | if (!_arc_maps.empty()) |
---|
[290] | 950 | throw FormatError("Cannot find map names"); |
---|
[209] | 951 | return; |
---|
[186] | 952 | } |
---|
| 953 | line.putback(c); |
---|
[209] | 954 | |
---|
[127] | 955 | { |
---|
[209] | 956 | std::map<std::string, int> maps; |
---|
| 957 | |
---|
| 958 | std::string map; |
---|
| 959 | int index = 0; |
---|
| 960 | while (_reader_bits::readToken(line, map)) { |
---|
| 961 | if (maps.find(map) != maps.end()) { |
---|
| 962 | std::ostringstream msg; |
---|
| 963 | msg << "Multiple occurence of arc map: " << map; |
---|
[290] | 964 | throw FormatError(msg.str()); |
---|
[209] | 965 | } |
---|
| 966 | maps.insert(std::make_pair(map, index)); |
---|
| 967 | ++index; |
---|
| 968 | } |
---|
| 969 | |
---|
| 970 | for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
---|
| 971 | std::map<std::string, int>::iterator jt = |
---|
| 972 | maps.find(_arc_maps[i].first); |
---|
| 973 | if (jt == maps.end()) { |
---|
| 974 | std::ostringstream msg; |
---|
| 975 | msg << "Map not found in file: " << _arc_maps[i].first; |
---|
[290] | 976 | throw FormatError(msg.str()); |
---|
[209] | 977 | } |
---|
| 978 | map_index[i] = jt->second; |
---|
| 979 | } |
---|
| 980 | |
---|
| 981 | { |
---|
| 982 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 983 | if (jt != maps.end()) { |
---|
| 984 | label_index = jt->second; |
---|
| 985 | } else { |
---|
| 986 | label_index = -1; |
---|
| 987 | } |
---|
| 988 | } |
---|
| 989 | map_num = maps.size(); |
---|
[127] | 990 | } |
---|
| 991 | |
---|
| 992 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 993 | line.putback(c); |
---|
| 994 | |
---|
| 995 | std::string source_token; |
---|
| 996 | std::string target_token; |
---|
| 997 | |
---|
| 998 | if (!_reader_bits::readToken(line, source_token)) |
---|
[290] | 999 | throw FormatError("Source not found"); |
---|
[209] | 1000 | |
---|
| 1001 | if (!_reader_bits::readToken(line, target_token)) |
---|
[290] | 1002 | throw FormatError("Target not found"); |
---|
[209] | 1003 | |
---|
| 1004 | std::vector<std::string> tokens(map_num); |
---|
| 1005 | for (int i = 0; i < map_num; ++i) { |
---|
| 1006 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 1007 | std::ostringstream msg; |
---|
| 1008 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 1009 | throw FormatError(msg.str()); |
---|
[209] | 1010 | } |
---|
| 1011 | } |
---|
| 1012 | if (line >> std::ws >> c) |
---|
[290] | 1013 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 1014 | |
---|
| 1015 | Arc a; |
---|
| 1016 | if (!_use_arcs) { |
---|
[127] | 1017 | |
---|
| 1018 | typename NodeIndex::iterator it; |
---|
[209] | 1019 | |
---|
[127] | 1020 | it = _node_index.find(source_token); |
---|
| 1021 | if (it == _node_index.end()) { |
---|
| 1022 | std::ostringstream msg; |
---|
| 1023 | msg << "Item not found: " << source_token; |
---|
[290] | 1024 | throw FormatError(msg.str()); |
---|
[127] | 1025 | } |
---|
| 1026 | Node source = it->second; |
---|
| 1027 | |
---|
| 1028 | it = _node_index.find(target_token); |
---|
[209] | 1029 | if (it == _node_index.end()) { |
---|
| 1030 | std::ostringstream msg; |
---|
[127] | 1031 | msg << "Item not found: " << target_token; |
---|
[290] | 1032 | throw FormatError(msg.str()); |
---|
[209] | 1033 | } |
---|
| 1034 | Node target = it->second; |
---|
| 1035 | |
---|
| 1036 | a = _digraph.addArc(source, target); |
---|
| 1037 | if (label_index != -1) |
---|
| 1038 | _arc_index.insert(std::make_pair(tokens[label_index], a)); |
---|
| 1039 | } else { |
---|
| 1040 | if (label_index == -1) |
---|
[290] | 1041 | throw FormatError("Label map not found in file"); |
---|
[209] | 1042 | typename std::map<std::string, Arc>::iterator it = |
---|
| 1043 | _arc_index.find(tokens[label_index]); |
---|
| 1044 | if (it == _arc_index.end()) { |
---|
| 1045 | std::ostringstream msg; |
---|
| 1046 | msg << "Arc with label not found: " << tokens[label_index]; |
---|
[290] | 1047 | throw FormatError(msg.str()); |
---|
[209] | 1048 | } |
---|
| 1049 | a = it->second; |
---|
| 1050 | } |
---|
| 1051 | |
---|
| 1052 | for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
---|
| 1053 | _arc_maps[i].second->set(a, tokens[map_index[i]]); |
---|
| 1054 | } |
---|
[127] | 1055 | |
---|
| 1056 | } |
---|
| 1057 | if (readSuccess()) { |
---|
[209] | 1058 | line.putback(c); |
---|
[127] | 1059 | } |
---|
| 1060 | } |
---|
| 1061 | |
---|
| 1062 | void readAttributes() { |
---|
| 1063 | |
---|
| 1064 | std::set<std::string> read_attr; |
---|
| 1065 | |
---|
| 1066 | char c; |
---|
| 1067 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1068 | line.putback(c); |
---|
| 1069 | |
---|
| 1070 | std::string attr, token; |
---|
| 1071 | if (!_reader_bits::readToken(line, attr)) |
---|
[290] | 1072 | throw FormatError("Attribute name not found"); |
---|
[209] | 1073 | if (!_reader_bits::readToken(line, token)) |
---|
[290] | 1074 | throw FormatError("Attribute value not found"); |
---|
[209] | 1075 | if (line >> c) |
---|
[290] | 1076 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 1077 | |
---|
| 1078 | { |
---|
| 1079 | std::set<std::string>::iterator it = read_attr.find(attr); |
---|
| 1080 | if (it != read_attr.end()) { |
---|
| 1081 | std::ostringstream msg; |
---|
| 1082 | msg << "Multiple occurence of attribute " << attr; |
---|
[290] | 1083 | throw FormatError(msg.str()); |
---|
[209] | 1084 | } |
---|
| 1085 | read_attr.insert(attr); |
---|
| 1086 | } |
---|
| 1087 | |
---|
| 1088 | { |
---|
| 1089 | typename Attributes::iterator it = _attributes.lower_bound(attr); |
---|
| 1090 | while (it != _attributes.end() && it->first == attr) { |
---|
| 1091 | it->second->set(token); |
---|
| 1092 | ++it; |
---|
| 1093 | } |
---|
| 1094 | } |
---|
[127] | 1095 | |
---|
| 1096 | } |
---|
| 1097 | if (readSuccess()) { |
---|
[209] | 1098 | line.putback(c); |
---|
[127] | 1099 | } |
---|
| 1100 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
[209] | 1101 | it != _attributes.end(); ++it) { |
---|
| 1102 | if (read_attr.find(it->first) == read_attr.end()) { |
---|
| 1103 | std::ostringstream msg; |
---|
| 1104 | msg << "Attribute not found in file: " << it->first; |
---|
[290] | 1105 | throw FormatError(msg.str()); |
---|
[209] | 1106 | } |
---|
[127] | 1107 | } |
---|
| 1108 | } |
---|
| 1109 | |
---|
| 1110 | public: |
---|
[156] | 1111 | |
---|
[209] | 1112 | /// \name Execution of the reader |
---|
[156] | 1113 | /// @{ |
---|
| 1114 | |
---|
| 1115 | /// \brief Start the batch processing |
---|
| 1116 | /// |
---|
| 1117 | /// This function starts the batch processing |
---|
[127] | 1118 | void run() { |
---|
| 1119 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
---|
[163] | 1120 | if (!*_is) { |
---|
[290] | 1121 | throw FormatError("Cannot find file"); |
---|
[163] | 1122 | } |
---|
[209] | 1123 | |
---|
[188] | 1124 | bool nodes_done = _skip_nodes; |
---|
| 1125 | bool arcs_done = _skip_arcs; |
---|
[127] | 1126 | bool attributes_done = false; |
---|
| 1127 | |
---|
[209] | 1128 | line_num = 0; |
---|
[127] | 1129 | readLine(); |
---|
[172] | 1130 | skipSection(); |
---|
[127] | 1131 | |
---|
| 1132 | while (readSuccess()) { |
---|
[209] | 1133 | try { |
---|
| 1134 | char c; |
---|
| 1135 | std::string section, caption; |
---|
| 1136 | line >> c; |
---|
| 1137 | _reader_bits::readToken(line, section); |
---|
| 1138 | _reader_bits::readToken(line, caption); |
---|
| 1139 | |
---|
| 1140 | if (line >> c) |
---|
[290] | 1141 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 1142 | |
---|
| 1143 | if (section == "nodes" && !nodes_done) { |
---|
| 1144 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
---|
| 1145 | readNodes(); |
---|
| 1146 | nodes_done = true; |
---|
| 1147 | } |
---|
| 1148 | } else if ((section == "arcs" || section == "edges") && |
---|
| 1149 | !arcs_done) { |
---|
| 1150 | if (_arcs_caption.empty() || _arcs_caption == caption) { |
---|
| 1151 | readArcs(); |
---|
| 1152 | arcs_done = true; |
---|
| 1153 | } |
---|
| 1154 | } else if (section == "attributes" && !attributes_done) { |
---|
| 1155 | if (_attributes_caption.empty() || _attributes_caption == caption) { |
---|
| 1156 | readAttributes(); |
---|
| 1157 | attributes_done = true; |
---|
| 1158 | } |
---|
| 1159 | } else { |
---|
| 1160 | readLine(); |
---|
| 1161 | skipSection(); |
---|
| 1162 | } |
---|
[290] | 1163 | } catch (FormatError& error) { |
---|
[209] | 1164 | error.line(line_num); |
---|
[290] | 1165 | error.file(_filename); |
---|
[209] | 1166 | throw; |
---|
| 1167 | } |
---|
[127] | 1168 | } |
---|
| 1169 | |
---|
| 1170 | if (!nodes_done) { |
---|
[290] | 1171 | throw FormatError("Section @nodes not found"); |
---|
[127] | 1172 | } |
---|
| 1173 | |
---|
| 1174 | if (!arcs_done) { |
---|
[290] | 1175 | throw FormatError("Section @arcs not found"); |
---|
[127] | 1176 | } |
---|
| 1177 | |
---|
| 1178 | if (!attributes_done && !_attributes.empty()) { |
---|
[290] | 1179 | throw FormatError("Section @attributes not found"); |
---|
[127] | 1180 | } |
---|
| 1181 | |
---|
| 1182 | } |
---|
[156] | 1183 | |
---|
| 1184 | /// @} |
---|
[209] | 1185 | |
---|
[127] | 1186 | }; |
---|
| 1187 | |
---|
[192] | 1188 | /// \brief Return a \ref DigraphReader class |
---|
[209] | 1189 | /// |
---|
[192] | 1190 | /// This function just returns a \ref DigraphReader class. |
---|
[156] | 1191 | /// \relates DigraphReader |
---|
[127] | 1192 | template <typename Digraph> |
---|
| 1193 | DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) { |
---|
[163] | 1194 | DigraphReader<Digraph> tmp(is, digraph); |
---|
| 1195 | return tmp; |
---|
[127] | 1196 | } |
---|
| 1197 | |
---|
[192] | 1198 | /// \brief Return a \ref DigraphReader class |
---|
[209] | 1199 | /// |
---|
[192] | 1200 | /// This function just returns a \ref DigraphReader class. |
---|
[156] | 1201 | /// \relates DigraphReader |
---|
[127] | 1202 | template <typename Digraph> |
---|
[209] | 1203 | DigraphReader<Digraph> digraphReader(const std::string& fn, |
---|
| 1204 | Digraph& digraph) { |
---|
[163] | 1205 | DigraphReader<Digraph> tmp(fn, digraph); |
---|
| 1206 | return tmp; |
---|
[127] | 1207 | } |
---|
| 1208 | |
---|
[192] | 1209 | /// \brief Return a \ref DigraphReader class |
---|
[209] | 1210 | /// |
---|
[192] | 1211 | /// This function just returns a \ref DigraphReader class. |
---|
[156] | 1212 | /// \relates DigraphReader |
---|
[127] | 1213 | template <typename Digraph> |
---|
| 1214 | DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) { |
---|
[163] | 1215 | DigraphReader<Digraph> tmp(fn, digraph); |
---|
| 1216 | return tmp; |
---|
[127] | 1217 | } |
---|
[165] | 1218 | |
---|
[190] | 1219 | template <typename Graph> |
---|
| 1220 | class GraphReader; |
---|
| 1221 | |
---|
| 1222 | template <typename Graph> |
---|
[209] | 1223 | GraphReader<Graph> graphReader(std::istream& is, Graph& graph); |
---|
[190] | 1224 | |
---|
| 1225 | template <typename Graph> |
---|
[209] | 1226 | GraphReader<Graph> graphReader(const std::string& fn, Graph& graph); |
---|
[190] | 1227 | |
---|
| 1228 | template <typename Graph> |
---|
[209] | 1229 | GraphReader<Graph> graphReader(const char *fn, Graph& graph); |
---|
[190] | 1230 | |
---|
[165] | 1231 | /// \ingroup lemon_io |
---|
[209] | 1232 | /// |
---|
[192] | 1233 | /// \brief \ref lgf-format "LGF" reader for undirected graphs |
---|
[165] | 1234 | /// |
---|
| 1235 | /// This utility reads an \ref lgf-format "LGF" file. |
---|
[192] | 1236 | /// |
---|
| 1237 | /// It can be used almost the same way as \c DigraphReader. |
---|
| 1238 | /// The only difference is that this class can handle edges and |
---|
| 1239 | /// edge maps as well as arcs and arc maps. |
---|
[201] | 1240 | /// |
---|
| 1241 | /// The columns in the \c \@edges (or \c \@arcs) section are the |
---|
| 1242 | /// edge maps. However, if there are two maps with the same name |
---|
| 1243 | /// prefixed with \c '+' and \c '-', then these can be read into an |
---|
| 1244 | /// arc map. Similarly, an attribute can be read into an arc, if |
---|
| 1245 | /// it's value is an edge label prefixed with \c '+' or \c '-'. |
---|
[165] | 1246 | template <typename _Graph> |
---|
| 1247 | class GraphReader { |
---|
| 1248 | public: |
---|
| 1249 | |
---|
| 1250 | typedef _Graph Graph; |
---|
| 1251 | TEMPLATE_GRAPH_TYPEDEFS(Graph); |
---|
[209] | 1252 | |
---|
[165] | 1253 | private: |
---|
| 1254 | |
---|
| 1255 | std::istream* _is; |
---|
| 1256 | bool local_is; |
---|
[290] | 1257 | std::string _filename; |
---|
[165] | 1258 | |
---|
| 1259 | Graph& _graph; |
---|
| 1260 | |
---|
| 1261 | std::string _nodes_caption; |
---|
| 1262 | std::string _edges_caption; |
---|
| 1263 | std::string _attributes_caption; |
---|
| 1264 | |
---|
| 1265 | typedef std::map<std::string, Node> NodeIndex; |
---|
| 1266 | NodeIndex _node_index; |
---|
| 1267 | typedef std::map<std::string, Edge> EdgeIndex; |
---|
| 1268 | EdgeIndex _edge_index; |
---|
[209] | 1269 | |
---|
| 1270 | typedef std::vector<std::pair<std::string, |
---|
| 1271 | _reader_bits::MapStorageBase<Node>*> > NodeMaps; |
---|
| 1272 | NodeMaps _node_maps; |
---|
[165] | 1273 | |
---|
| 1274 | typedef std::vector<std::pair<std::string, |
---|
| 1275 | _reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
---|
| 1276 | EdgeMaps _edge_maps; |
---|
| 1277 | |
---|
[209] | 1278 | typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
---|
[165] | 1279 | Attributes; |
---|
| 1280 | Attributes _attributes; |
---|
| 1281 | |
---|
| 1282 | bool _use_nodes; |
---|
| 1283 | bool _use_edges; |
---|
| 1284 | |
---|
[188] | 1285 | bool _skip_nodes; |
---|
| 1286 | bool _skip_edges; |
---|
| 1287 | |
---|
[165] | 1288 | int line_num; |
---|
| 1289 | std::istringstream line; |
---|
| 1290 | |
---|
| 1291 | public: |
---|
| 1292 | |
---|
| 1293 | /// \brief Constructor |
---|
| 1294 | /// |
---|
[192] | 1295 | /// Construct an undirected graph reader, which reads from the given |
---|
[165] | 1296 | /// input stream. |
---|
[209] | 1297 | GraphReader(std::istream& is, Graph& graph) |
---|
[165] | 1298 | : _is(&is), local_is(false), _graph(graph), |
---|
[209] | 1299 | _use_nodes(false), _use_edges(false), |
---|
| 1300 | _skip_nodes(false), _skip_edges(false) {} |
---|
[165] | 1301 | |
---|
| 1302 | /// \brief Constructor |
---|
| 1303 | /// |
---|
[192] | 1304 | /// Construct an undirected graph reader, which reads from the given |
---|
[165] | 1305 | /// file. |
---|
[209] | 1306 | GraphReader(const std::string& fn, Graph& graph) |
---|
[290] | 1307 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
---|
| 1308 | _filename(fn), _graph(graph), |
---|
[212] | 1309 | _use_nodes(false), _use_edges(false), |
---|
[290] | 1310 | _skip_nodes(false), _skip_edges(false) { |
---|
| 1311 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 1312 | } |
---|
[209] | 1313 | |
---|
[165] | 1314 | /// \brief Constructor |
---|
| 1315 | /// |
---|
[192] | 1316 | /// Construct an undirected graph reader, which reads from the given |
---|
[165] | 1317 | /// file. |
---|
[209] | 1318 | GraphReader(const char* fn, Graph& graph) |
---|
[290] | 1319 | : _is(new std::ifstream(fn)), local_is(true), |
---|
| 1320 | _filename(fn), _graph(graph), |
---|
[212] | 1321 | _use_nodes(false), _use_edges(false), |
---|
[290] | 1322 | _skip_nodes(false), _skip_edges(false) { |
---|
| 1323 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 1324 | } |
---|
[165] | 1325 | |
---|
| 1326 | /// \brief Destructor |
---|
| 1327 | ~GraphReader() { |
---|
[209] | 1328 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 1329 | it != _node_maps.end(); ++it) { |
---|
| 1330 | delete it->second; |
---|
[165] | 1331 | } |
---|
| 1332 | |
---|
[209] | 1333 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
| 1334 | it != _edge_maps.end(); ++it) { |
---|
| 1335 | delete it->second; |
---|
[165] | 1336 | } |
---|
| 1337 | |
---|
[209] | 1338 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 1339 | it != _attributes.end(); ++it) { |
---|
| 1340 | delete it->second; |
---|
[165] | 1341 | } |
---|
| 1342 | |
---|
| 1343 | if (local_is) { |
---|
[209] | 1344 | delete _is; |
---|
[165] | 1345 | } |
---|
| 1346 | |
---|
| 1347 | } |
---|
| 1348 | |
---|
| 1349 | private: |
---|
[209] | 1350 | friend GraphReader<Graph> graphReader<>(std::istream& is, Graph& graph); |
---|
| 1351 | friend GraphReader<Graph> graphReader<>(const std::string& fn, |
---|
| 1352 | Graph& graph); |
---|
| 1353 | friend GraphReader<Graph> graphReader<>(const char *fn, Graph& graph); |
---|
| 1354 | |
---|
| 1355 | GraphReader(GraphReader& other) |
---|
[190] | 1356 | : _is(other._is), local_is(other.local_is), _graph(other._graph), |
---|
[209] | 1357 | _use_nodes(other._use_nodes), _use_edges(other._use_edges), |
---|
| 1358 | _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
---|
[190] | 1359 | |
---|
| 1360 | other._is = 0; |
---|
| 1361 | other.local_is = false; |
---|
[209] | 1362 | |
---|
[190] | 1363 | _node_index.swap(other._node_index); |
---|
| 1364 | _edge_index.swap(other._edge_index); |
---|
| 1365 | |
---|
| 1366 | _node_maps.swap(other._node_maps); |
---|
| 1367 | _edge_maps.swap(other._edge_maps); |
---|
| 1368 | _attributes.swap(other._attributes); |
---|
| 1369 | |
---|
| 1370 | _nodes_caption = other._nodes_caption; |
---|
| 1371 | _edges_caption = other._edges_caption; |
---|
| 1372 | _attributes_caption = other._attributes_caption; |
---|
| 1373 | |
---|
| 1374 | } |
---|
| 1375 | |
---|
[165] | 1376 | GraphReader& operator=(const GraphReader&); |
---|
| 1377 | |
---|
| 1378 | public: |
---|
| 1379 | |
---|
| 1380 | /// \name Reading rules |
---|
| 1381 | /// @{ |
---|
[209] | 1382 | |
---|
[165] | 1383 | /// \brief Node map reading rule |
---|
| 1384 | /// |
---|
| 1385 | /// Add a node map reading rule to the reader. |
---|
| 1386 | template <typename Map> |
---|
| 1387 | GraphReader& nodeMap(const std::string& caption, Map& map) { |
---|
| 1388 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1389 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 1390 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
[165] | 1391 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1392 | return *this; |
---|
| 1393 | } |
---|
| 1394 | |
---|
| 1395 | /// \brief Node map reading rule |
---|
| 1396 | /// |
---|
| 1397 | /// Add a node map reading rule with specialized converter to the |
---|
| 1398 | /// reader. |
---|
| 1399 | template <typename Map, typename Converter> |
---|
[209] | 1400 | GraphReader& nodeMap(const std::string& caption, Map& map, |
---|
| 1401 | const Converter& converter = Converter()) { |
---|
[165] | 1402 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1403 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 1404 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
[165] | 1405 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1406 | return *this; |
---|
| 1407 | } |
---|
| 1408 | |
---|
| 1409 | /// \brief Edge map reading rule |
---|
| 1410 | /// |
---|
| 1411 | /// Add an edge map reading rule to the reader. |
---|
| 1412 | template <typename Map> |
---|
| 1413 | GraphReader& edgeMap(const std::string& caption, Map& map) { |
---|
| 1414 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1415 | _reader_bits::MapStorageBase<Edge>* storage = |
---|
| 1416 | new _reader_bits::MapStorage<Edge, Map>(map); |
---|
[165] | 1417 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1418 | return *this; |
---|
| 1419 | } |
---|
| 1420 | |
---|
| 1421 | /// \brief Edge map reading rule |
---|
| 1422 | /// |
---|
| 1423 | /// Add an edge map reading rule with specialized converter to the |
---|
| 1424 | /// reader. |
---|
| 1425 | template <typename Map, typename Converter> |
---|
[209] | 1426 | GraphReader& edgeMap(const std::string& caption, Map& map, |
---|
| 1427 | const Converter& converter = Converter()) { |
---|
[165] | 1428 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1429 | _reader_bits::MapStorageBase<Edge>* storage = |
---|
| 1430 | new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
---|
[165] | 1431 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1432 | return *this; |
---|
| 1433 | } |
---|
| 1434 | |
---|
| 1435 | /// \brief Arc map reading rule |
---|
| 1436 | /// |
---|
| 1437 | /// Add an arc map reading rule to the reader. |
---|
| 1438 | template <typename Map> |
---|
| 1439 | GraphReader& arcMap(const std::string& caption, Map& map) { |
---|
| 1440 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 1441 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
---|
| 1442 | new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
---|
[165] | 1443 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
[209] | 1444 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
---|
| 1445 | new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map); |
---|
[165] | 1446 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 1447 | return *this; |
---|
| 1448 | } |
---|
| 1449 | |
---|
| 1450 | /// \brief Arc map reading rule |
---|
| 1451 | /// |
---|
| 1452 | /// Add an arc map reading rule with specialized converter to the |
---|
| 1453 | /// reader. |
---|
| 1454 | template <typename Map, typename Converter> |
---|
[209] | 1455 | GraphReader& arcMap(const std::string& caption, Map& map, |
---|
| 1456 | const Converter& converter = Converter()) { |
---|
[165] | 1457 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 1458 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
---|
| 1459 | new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter> |
---|
| 1460 | (_graph, map, converter); |
---|
[165] | 1461 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
[209] | 1462 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
---|
| 1463 | new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter> |
---|
| 1464 | (_graph, map, converter); |
---|
[165] | 1465 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 1466 | return *this; |
---|
| 1467 | } |
---|
| 1468 | |
---|
| 1469 | /// \brief Attribute reading rule |
---|
| 1470 | /// |
---|
| 1471 | /// Add an attribute reading rule to the reader. |
---|
| 1472 | template <typename Value> |
---|
| 1473 | GraphReader& attribute(const std::string& caption, Value& value) { |
---|
[209] | 1474 | _reader_bits::ValueStorageBase* storage = |
---|
| 1475 | new _reader_bits::ValueStorage<Value>(value); |
---|
[165] | 1476 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1477 | return *this; |
---|
| 1478 | } |
---|
| 1479 | |
---|
| 1480 | /// \brief Attribute reading rule |
---|
| 1481 | /// |
---|
| 1482 | /// Add an attribute reading rule with specialized converter to the |
---|
| 1483 | /// reader. |
---|
| 1484 | template <typename Value, typename Converter> |
---|
[209] | 1485 | GraphReader& attribute(const std::string& caption, Value& value, |
---|
| 1486 | const Converter& converter = Converter()) { |
---|
| 1487 | _reader_bits::ValueStorageBase* storage = |
---|
| 1488 | new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
---|
[165] | 1489 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1490 | return *this; |
---|
| 1491 | } |
---|
| 1492 | |
---|
| 1493 | /// \brief Node reading rule |
---|
| 1494 | /// |
---|
| 1495 | /// Add a node reading rule to reader. |
---|
| 1496 | GraphReader& node(const std::string& caption, Node& node) { |
---|
| 1497 | typedef _reader_bits::MapLookUpConverter<Node> Converter; |
---|
| 1498 | Converter converter(_node_index); |
---|
[209] | 1499 | _reader_bits::ValueStorageBase* storage = |
---|
| 1500 | new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
---|
[165] | 1501 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1502 | return *this; |
---|
| 1503 | } |
---|
| 1504 | |
---|
| 1505 | /// \brief Edge reading rule |
---|
| 1506 | /// |
---|
| 1507 | /// Add an edge reading rule to reader. |
---|
| 1508 | GraphReader& edge(const std::string& caption, Edge& edge) { |
---|
| 1509 | typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
---|
| 1510 | Converter converter(_edge_index); |
---|
[209] | 1511 | _reader_bits::ValueStorageBase* storage = |
---|
| 1512 | new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
---|
[165] | 1513 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1514 | return *this; |
---|
| 1515 | } |
---|
| 1516 | |
---|
| 1517 | /// \brief Arc reading rule |
---|
| 1518 | /// |
---|
| 1519 | /// Add an arc reading rule to reader. |
---|
| 1520 | GraphReader& arc(const std::string& caption, Arc& arc) { |
---|
| 1521 | typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter; |
---|
| 1522 | Converter converter(_graph, _edge_index); |
---|
[209] | 1523 | _reader_bits::ValueStorageBase* storage = |
---|
| 1524 | new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
---|
[165] | 1525 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1526 | return *this; |
---|
| 1527 | } |
---|
| 1528 | |
---|
| 1529 | /// @} |
---|
| 1530 | |
---|
| 1531 | /// \name Select section by name |
---|
| 1532 | /// @{ |
---|
| 1533 | |
---|
| 1534 | /// \brief Set \c \@nodes section to be read |
---|
| 1535 | /// |
---|
[192] | 1536 | /// Set \c \@nodes section to be read. |
---|
[165] | 1537 | GraphReader& nodes(const std::string& caption) { |
---|
| 1538 | _nodes_caption = caption; |
---|
| 1539 | return *this; |
---|
| 1540 | } |
---|
| 1541 | |
---|
| 1542 | /// \brief Set \c \@edges section to be read |
---|
| 1543 | /// |
---|
[192] | 1544 | /// Set \c \@edges section to be read. |
---|
[165] | 1545 | GraphReader& edges(const std::string& caption) { |
---|
| 1546 | _edges_caption = caption; |
---|
| 1547 | return *this; |
---|
| 1548 | } |
---|
| 1549 | |
---|
| 1550 | /// \brief Set \c \@attributes section to be read |
---|
| 1551 | /// |
---|
[192] | 1552 | /// Set \c \@attributes section to be read. |
---|
[165] | 1553 | GraphReader& attributes(const std::string& caption) { |
---|
| 1554 | _attributes_caption = caption; |
---|
| 1555 | return *this; |
---|
| 1556 | } |
---|
| 1557 | |
---|
| 1558 | /// @} |
---|
| 1559 | |
---|
| 1560 | /// \name Using previously constructed node or edge set |
---|
| 1561 | /// @{ |
---|
| 1562 | |
---|
| 1563 | /// \brief Use previously constructed node set |
---|
| 1564 | /// |
---|
| 1565 | /// Use previously constructed node set, and specify the node |
---|
| 1566 | /// label map. |
---|
| 1567 | template <typename Map> |
---|
| 1568 | GraphReader& useNodes(const Map& map) { |
---|
| 1569 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1570 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[165] | 1571 | _use_nodes = true; |
---|
| 1572 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 1573 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
[209] | 1574 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[165] | 1575 | } |
---|
| 1576 | return *this; |
---|
| 1577 | } |
---|
| 1578 | |
---|
| 1579 | /// \brief Use previously constructed node set |
---|
| 1580 | /// |
---|
| 1581 | /// Use previously constructed node set, and specify the node |
---|
| 1582 | /// label map and a functor which converts the label map values to |
---|
[192] | 1583 | /// \c std::string. |
---|
[165] | 1584 | template <typename Map, typename Converter> |
---|
[209] | 1585 | GraphReader& useNodes(const Map& map, |
---|
| 1586 | const Converter& converter = Converter()) { |
---|
[165] | 1587 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1588 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[165] | 1589 | _use_nodes = true; |
---|
| 1590 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
[209] | 1591 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[165] | 1592 | } |
---|
| 1593 | return *this; |
---|
| 1594 | } |
---|
| 1595 | |
---|
| 1596 | /// \brief Use previously constructed edge set |
---|
| 1597 | /// |
---|
| 1598 | /// Use previously constructed edge set, and specify the edge |
---|
| 1599 | /// label map. |
---|
| 1600 | template <typename Map> |
---|
| 1601 | GraphReader& useEdges(const Map& map) { |
---|
| 1602 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
| 1603 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
---|
| 1604 | _use_edges = true; |
---|
| 1605 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 1606 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
---|
[209] | 1607 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[165] | 1608 | } |
---|
| 1609 | return *this; |
---|
| 1610 | } |
---|
| 1611 | |
---|
| 1612 | /// \brief Use previously constructed edge set |
---|
| 1613 | /// |
---|
| 1614 | /// Use previously constructed edge set, and specify the edge |
---|
| 1615 | /// label map and a functor which converts the label map values to |
---|
[192] | 1616 | /// \c std::string. |
---|
[165] | 1617 | template <typename Map, typename Converter> |
---|
[209] | 1618 | GraphReader& useEdges(const Map& map, |
---|
| 1619 | const Converter& converter = Converter()) { |
---|
[165] | 1620 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1621 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
---|
[165] | 1622 | _use_edges = true; |
---|
| 1623 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
---|
[209] | 1624 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[165] | 1625 | } |
---|
| 1626 | return *this; |
---|
| 1627 | } |
---|
| 1628 | |
---|
[192] | 1629 | /// \brief Skip the reading of node section |
---|
[188] | 1630 | /// |
---|
| 1631 | /// Omit the reading of the node section. This implies that each node |
---|
[192] | 1632 | /// map reading rule will be abandoned, and the nodes of the graph |
---|
[188] | 1633 | /// will not be constructed, which usually cause that the edge set |
---|
| 1634 | /// could not be read due to lack of node name |
---|
[192] | 1635 | /// could not be read due to lack of node name resolving. |
---|
| 1636 | /// Therefore \c skipEdges() function should also be used, or |
---|
| 1637 | /// \c useNodes() should be used to specify the label of the nodes. |
---|
[188] | 1638 | GraphReader& skipNodes() { |
---|
[209] | 1639 | LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
---|
[188] | 1640 | _skip_nodes = true; |
---|
| 1641 | return *this; |
---|
| 1642 | } |
---|
| 1643 | |
---|
[192] | 1644 | /// \brief Skip the reading of edge section |
---|
[188] | 1645 | /// |
---|
| 1646 | /// Omit the reading of the edge section. This implies that each edge |
---|
[192] | 1647 | /// map reading rule will be abandoned, and the edges of the graph |
---|
[188] | 1648 | /// will not be constructed. |
---|
| 1649 | GraphReader& skipEdges() { |
---|
[209] | 1650 | LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
---|
[188] | 1651 | _skip_edges = true; |
---|
| 1652 | return *this; |
---|
| 1653 | } |
---|
| 1654 | |
---|
[165] | 1655 | /// @} |
---|
| 1656 | |
---|
| 1657 | private: |
---|
| 1658 | |
---|
| 1659 | bool readLine() { |
---|
| 1660 | std::string str; |
---|
| 1661 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 1662 | line.clear(); line.str(str); |
---|
| 1663 | char c; |
---|
| 1664 | if (line >> std::ws >> c && c != '#') { |
---|
| 1665 | line.putback(c); |
---|
| 1666 | return true; |
---|
| 1667 | } |
---|
[165] | 1668 | } |
---|
| 1669 | return false; |
---|
| 1670 | } |
---|
| 1671 | |
---|
| 1672 | bool readSuccess() { |
---|
| 1673 | return static_cast<bool>(*_is); |
---|
| 1674 | } |
---|
[209] | 1675 | |
---|
[165] | 1676 | void skipSection() { |
---|
| 1677 | char c; |
---|
| 1678 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 1679 | readLine(); |
---|
[165] | 1680 | } |
---|
| 1681 | line.putback(c); |
---|
| 1682 | } |
---|
| 1683 | |
---|
| 1684 | void readNodes() { |
---|
| 1685 | |
---|
| 1686 | std::vector<int> map_index(_node_maps.size()); |
---|
| 1687 | int map_num, label_index; |
---|
| 1688 | |
---|
[186] | 1689 | char c; |
---|
| 1690 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 1691 | if (readSuccess() && line) line.putback(c); |
---|
| 1692 | if (!_node_maps.empty()) |
---|
[290] | 1693 | throw FormatError("Cannot find map names"); |
---|
[209] | 1694 | return; |
---|
[186] | 1695 | } |
---|
| 1696 | line.putback(c); |
---|
[209] | 1697 | |
---|
[165] | 1698 | { |
---|
[209] | 1699 | std::map<std::string, int> maps; |
---|
| 1700 | |
---|
| 1701 | std::string map; |
---|
| 1702 | int index = 0; |
---|
| 1703 | while (_reader_bits::readToken(line, map)) { |
---|
| 1704 | if (maps.find(map) != maps.end()) { |
---|
| 1705 | std::ostringstream msg; |
---|
| 1706 | msg << "Multiple occurence of node map: " << map; |
---|
[290] | 1707 | throw FormatError(msg.str()); |
---|
[209] | 1708 | } |
---|
| 1709 | maps.insert(std::make_pair(map, index)); |
---|
| 1710 | ++index; |
---|
| 1711 | } |
---|
| 1712 | |
---|
| 1713 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 1714 | std::map<std::string, int>::iterator jt = |
---|
| 1715 | maps.find(_node_maps[i].first); |
---|
| 1716 | if (jt == maps.end()) { |
---|
| 1717 | std::ostringstream msg; |
---|
| 1718 | msg << "Map not found in file: " << _node_maps[i].first; |
---|
[290] | 1719 | throw FormatError(msg.str()); |
---|
[209] | 1720 | } |
---|
| 1721 | map_index[i] = jt->second; |
---|
| 1722 | } |
---|
| 1723 | |
---|
| 1724 | { |
---|
| 1725 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 1726 | if (jt != maps.end()) { |
---|
| 1727 | label_index = jt->second; |
---|
| 1728 | } else { |
---|
| 1729 | label_index = -1; |
---|
| 1730 | } |
---|
| 1731 | } |
---|
| 1732 | map_num = maps.size(); |
---|
[165] | 1733 | } |
---|
| 1734 | |
---|
| 1735 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1736 | line.putback(c); |
---|
| 1737 | |
---|
| 1738 | std::vector<std::string> tokens(map_num); |
---|
| 1739 | for (int i = 0; i < map_num; ++i) { |
---|
| 1740 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 1741 | std::ostringstream msg; |
---|
| 1742 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 1743 | throw FormatError(msg.str()); |
---|
[209] | 1744 | } |
---|
| 1745 | } |
---|
| 1746 | if (line >> std::ws >> c) |
---|
[290] | 1747 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 1748 | |
---|
| 1749 | Node n; |
---|
| 1750 | if (!_use_nodes) { |
---|
| 1751 | n = _graph.addNode(); |
---|
| 1752 | if (label_index != -1) |
---|
| 1753 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
---|
| 1754 | } else { |
---|
| 1755 | if (label_index == -1) |
---|
[290] | 1756 | throw FormatError("Label map not found in file"); |
---|
[209] | 1757 | typename std::map<std::string, Node>::iterator it = |
---|
| 1758 | _node_index.find(tokens[label_index]); |
---|
| 1759 | if (it == _node_index.end()) { |
---|
| 1760 | std::ostringstream msg; |
---|
| 1761 | msg << "Node with label not found: " << tokens[label_index]; |
---|
[290] | 1762 | throw FormatError(msg.str()); |
---|
[209] | 1763 | } |
---|
| 1764 | n = it->second; |
---|
| 1765 | } |
---|
| 1766 | |
---|
| 1767 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 1768 | _node_maps[i].second->set(n, tokens[map_index[i]]); |
---|
| 1769 | } |
---|
[165] | 1770 | |
---|
| 1771 | } |
---|
| 1772 | if (readSuccess()) { |
---|
[209] | 1773 | line.putback(c); |
---|
[165] | 1774 | } |
---|
| 1775 | } |
---|
| 1776 | |
---|
| 1777 | void readEdges() { |
---|
| 1778 | |
---|
| 1779 | std::vector<int> map_index(_edge_maps.size()); |
---|
| 1780 | int map_num, label_index; |
---|
| 1781 | |
---|
[186] | 1782 | char c; |
---|
| 1783 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 1784 | if (readSuccess() && line) line.putback(c); |
---|
| 1785 | if (!_edge_maps.empty()) |
---|
[290] | 1786 | throw FormatError("Cannot find map names"); |
---|
[209] | 1787 | return; |
---|
[186] | 1788 | } |
---|
| 1789 | line.putback(c); |
---|
[209] | 1790 | |
---|
[165] | 1791 | { |
---|
[209] | 1792 | std::map<std::string, int> maps; |
---|
| 1793 | |
---|
| 1794 | std::string map; |
---|
| 1795 | int index = 0; |
---|
| 1796 | while (_reader_bits::readToken(line, map)) { |
---|
| 1797 | if (maps.find(map) != maps.end()) { |
---|
| 1798 | std::ostringstream msg; |
---|
| 1799 | msg << "Multiple occurence of edge map: " << map; |
---|
[290] | 1800 | throw FormatError(msg.str()); |
---|
[209] | 1801 | } |
---|
| 1802 | maps.insert(std::make_pair(map, index)); |
---|
| 1803 | ++index; |
---|
| 1804 | } |
---|
| 1805 | |
---|
| 1806 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
---|
| 1807 | std::map<std::string, int>::iterator jt = |
---|
| 1808 | maps.find(_edge_maps[i].first); |
---|
| 1809 | if (jt == maps.end()) { |
---|
| 1810 | std::ostringstream msg; |
---|
| 1811 | msg << "Map not found in file: " << _edge_maps[i].first; |
---|
[290] | 1812 | throw FormatError(msg.str()); |
---|
[209] | 1813 | } |
---|
| 1814 | map_index[i] = jt->second; |
---|
| 1815 | } |
---|
| 1816 | |
---|
| 1817 | { |
---|
| 1818 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 1819 | if (jt != maps.end()) { |
---|
| 1820 | label_index = jt->second; |
---|
| 1821 | } else { |
---|
| 1822 | label_index = -1; |
---|
| 1823 | } |
---|
| 1824 | } |
---|
| 1825 | map_num = maps.size(); |
---|
[165] | 1826 | } |
---|
| 1827 | |
---|
| 1828 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1829 | line.putback(c); |
---|
| 1830 | |
---|
| 1831 | std::string source_token; |
---|
| 1832 | std::string target_token; |
---|
| 1833 | |
---|
| 1834 | if (!_reader_bits::readToken(line, source_token)) |
---|
[290] | 1835 | throw FormatError("Node u not found"); |
---|
[209] | 1836 | |
---|
| 1837 | if (!_reader_bits::readToken(line, target_token)) |
---|
[290] | 1838 | throw FormatError("Node v not found"); |
---|
[209] | 1839 | |
---|
| 1840 | std::vector<std::string> tokens(map_num); |
---|
| 1841 | for (int i = 0; i < map_num; ++i) { |
---|
| 1842 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 1843 | std::ostringstream msg; |
---|
| 1844 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 1845 | throw FormatError(msg.str()); |
---|
[209] | 1846 | } |
---|
| 1847 | } |
---|
| 1848 | if (line >> std::ws >> c) |
---|
[290] | 1849 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 1850 | |
---|
| 1851 | Edge e; |
---|
| 1852 | if (!_use_edges) { |
---|
[165] | 1853 | |
---|
| 1854 | typename NodeIndex::iterator it; |
---|
[209] | 1855 | |
---|
[165] | 1856 | it = _node_index.find(source_token); |
---|
| 1857 | if (it == _node_index.end()) { |
---|
| 1858 | std::ostringstream msg; |
---|
| 1859 | msg << "Item not found: " << source_token; |
---|
[290] | 1860 | throw FormatError(msg.str()); |
---|
[165] | 1861 | } |
---|
| 1862 | Node source = it->second; |
---|
| 1863 | |
---|
| 1864 | it = _node_index.find(target_token); |
---|
[209] | 1865 | if (it == _node_index.end()) { |
---|
| 1866 | std::ostringstream msg; |
---|
[165] | 1867 | msg << "Item not found: " << target_token; |
---|
[290] | 1868 | throw FormatError(msg.str()); |
---|
[209] | 1869 | } |
---|
| 1870 | Node target = it->second; |
---|
| 1871 | |
---|
| 1872 | e = _graph.addEdge(source, target); |
---|
| 1873 | if (label_index != -1) |
---|
| 1874 | _edge_index.insert(std::make_pair(tokens[label_index], e)); |
---|
| 1875 | } else { |
---|
| 1876 | if (label_index == -1) |
---|
[290] | 1877 | throw FormatError("Label map not found in file"); |
---|
[209] | 1878 | typename std::map<std::string, Edge>::iterator it = |
---|
| 1879 | _edge_index.find(tokens[label_index]); |
---|
| 1880 | if (it == _edge_index.end()) { |
---|
| 1881 | std::ostringstream msg; |
---|
| 1882 | msg << "Edge with label not found: " << tokens[label_index]; |
---|
[290] | 1883 | throw FormatError(msg.str()); |
---|
[209] | 1884 | } |
---|
| 1885 | e = it->second; |
---|
| 1886 | } |
---|
| 1887 | |
---|
| 1888 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
---|
| 1889 | _edge_maps[i].second->set(e, tokens[map_index[i]]); |
---|
| 1890 | } |
---|
[165] | 1891 | |
---|
| 1892 | } |
---|
| 1893 | if (readSuccess()) { |
---|
[209] | 1894 | line.putback(c); |
---|
[165] | 1895 | } |
---|
| 1896 | } |
---|
| 1897 | |
---|
| 1898 | void readAttributes() { |
---|
| 1899 | |
---|
| 1900 | std::set<std::string> read_attr; |
---|
| 1901 | |
---|
| 1902 | char c; |
---|
| 1903 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1904 | line.putback(c); |
---|
| 1905 | |
---|
| 1906 | std::string attr, token; |
---|
| 1907 | if (!_reader_bits::readToken(line, attr)) |
---|
[290] | 1908 | throw FormatError("Attribute name not found"); |
---|
[209] | 1909 | if (!_reader_bits::readToken(line, token)) |
---|
[290] | 1910 | throw FormatError("Attribute value not found"); |
---|
[209] | 1911 | if (line >> c) |
---|
[290] | 1912 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 1913 | |
---|
| 1914 | { |
---|
| 1915 | std::set<std::string>::iterator it = read_attr.find(attr); |
---|
| 1916 | if (it != read_attr.end()) { |
---|
| 1917 | std::ostringstream msg; |
---|
| 1918 | msg << "Multiple occurence of attribute " << attr; |
---|
[290] | 1919 | throw FormatError(msg.str()); |
---|
[209] | 1920 | } |
---|
| 1921 | read_attr.insert(attr); |
---|
| 1922 | } |
---|
| 1923 | |
---|
| 1924 | { |
---|
| 1925 | typename Attributes::iterator it = _attributes.lower_bound(attr); |
---|
| 1926 | while (it != _attributes.end() && it->first == attr) { |
---|
| 1927 | it->second->set(token); |
---|
| 1928 | ++it; |
---|
| 1929 | } |
---|
| 1930 | } |
---|
[165] | 1931 | |
---|
| 1932 | } |
---|
| 1933 | if (readSuccess()) { |
---|
[209] | 1934 | line.putback(c); |
---|
[165] | 1935 | } |
---|
| 1936 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
[209] | 1937 | it != _attributes.end(); ++it) { |
---|
| 1938 | if (read_attr.find(it->first) == read_attr.end()) { |
---|
| 1939 | std::ostringstream msg; |
---|
| 1940 | msg << "Attribute not found in file: " << it->first; |
---|
[290] | 1941 | throw FormatError(msg.str()); |
---|
[209] | 1942 | } |
---|
[165] | 1943 | } |
---|
| 1944 | } |
---|
| 1945 | |
---|
| 1946 | public: |
---|
| 1947 | |
---|
[209] | 1948 | /// \name Execution of the reader |
---|
[165] | 1949 | /// @{ |
---|
| 1950 | |
---|
| 1951 | /// \brief Start the batch processing |
---|
| 1952 | /// |
---|
| 1953 | /// This function starts the batch processing |
---|
| 1954 | void run() { |
---|
[209] | 1955 | |
---|
[165] | 1956 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
---|
[209] | 1957 | |
---|
[188] | 1958 | bool nodes_done = _skip_nodes; |
---|
| 1959 | bool edges_done = _skip_edges; |
---|
[165] | 1960 | bool attributes_done = false; |
---|
| 1961 | |
---|
[209] | 1962 | line_num = 0; |
---|
[165] | 1963 | readLine(); |
---|
[172] | 1964 | skipSection(); |
---|
[165] | 1965 | |
---|
| 1966 | while (readSuccess()) { |
---|
[209] | 1967 | try { |
---|
| 1968 | char c; |
---|
| 1969 | std::string section, caption; |
---|
| 1970 | line >> c; |
---|
| 1971 | _reader_bits::readToken(line, section); |
---|
| 1972 | _reader_bits::readToken(line, caption); |
---|
| 1973 | |
---|
| 1974 | if (line >> c) |
---|
[290] | 1975 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 1976 | |
---|
| 1977 | if (section == "nodes" && !nodes_done) { |
---|
| 1978 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
---|
| 1979 | readNodes(); |
---|
| 1980 | nodes_done = true; |
---|
| 1981 | } |
---|
| 1982 | } else if ((section == "edges" || section == "arcs") && |
---|
| 1983 | !edges_done) { |
---|
| 1984 | if (_edges_caption.empty() || _edges_caption == caption) { |
---|
| 1985 | readEdges(); |
---|
| 1986 | edges_done = true; |
---|
| 1987 | } |
---|
| 1988 | } else if (section == "attributes" && !attributes_done) { |
---|
| 1989 | if (_attributes_caption.empty() || _attributes_caption == caption) { |
---|
| 1990 | readAttributes(); |
---|
| 1991 | attributes_done = true; |
---|
| 1992 | } |
---|
| 1993 | } else { |
---|
| 1994 | readLine(); |
---|
| 1995 | skipSection(); |
---|
| 1996 | } |
---|
[290] | 1997 | } catch (FormatError& error) { |
---|
[209] | 1998 | error.line(line_num); |
---|
[290] | 1999 | error.file(_filename); |
---|
[209] | 2000 | throw; |
---|
| 2001 | } |
---|
[165] | 2002 | } |
---|
| 2003 | |
---|
| 2004 | if (!nodes_done) { |
---|
[290] | 2005 | throw FormatError("Section @nodes not found"); |
---|
[165] | 2006 | } |
---|
| 2007 | |
---|
| 2008 | if (!edges_done) { |
---|
[290] | 2009 | throw FormatError("Section @edges not found"); |
---|
[165] | 2010 | } |
---|
| 2011 | |
---|
| 2012 | if (!attributes_done && !_attributes.empty()) { |
---|
[290] | 2013 | throw FormatError("Section @attributes not found"); |
---|
[165] | 2014 | } |
---|
| 2015 | |
---|
| 2016 | } |
---|
| 2017 | |
---|
| 2018 | /// @} |
---|
[209] | 2019 | |
---|
[165] | 2020 | }; |
---|
| 2021 | |
---|
[192] | 2022 | /// \brief Return a \ref GraphReader class |
---|
[209] | 2023 | /// |
---|
[192] | 2024 | /// This function just returns a \ref GraphReader class. |
---|
[165] | 2025 | /// \relates GraphReader |
---|
| 2026 | template <typename Graph> |
---|
| 2027 | GraphReader<Graph> graphReader(std::istream& is, Graph& graph) { |
---|
| 2028 | GraphReader<Graph> tmp(is, graph); |
---|
| 2029 | return tmp; |
---|
| 2030 | } |
---|
| 2031 | |
---|
[192] | 2032 | /// \brief Return a \ref GraphReader class |
---|
[209] | 2033 | /// |
---|
[192] | 2034 | /// This function just returns a \ref GraphReader class. |
---|
[165] | 2035 | /// \relates GraphReader |
---|
| 2036 | template <typename Graph> |
---|
[209] | 2037 | GraphReader<Graph> graphReader(const std::string& fn, |
---|
| 2038 | Graph& graph) { |
---|
[165] | 2039 | GraphReader<Graph> tmp(fn, graph); |
---|
| 2040 | return tmp; |
---|
| 2041 | } |
---|
| 2042 | |
---|
[192] | 2043 | /// \brief Return a \ref GraphReader class |
---|
[209] | 2044 | /// |
---|
[192] | 2045 | /// This function just returns a \ref GraphReader class. |
---|
[165] | 2046 | /// \relates GraphReader |
---|
| 2047 | template <typename Graph> |
---|
| 2048 | GraphReader<Graph> graphReader(const char* fn, Graph& graph) { |
---|
| 2049 | GraphReader<Graph> tmp(fn, graph); |
---|
| 2050 | return tmp; |
---|
| 2051 | } |
---|
[173] | 2052 | |
---|
[190] | 2053 | class SectionReader; |
---|
| 2054 | |
---|
| 2055 | SectionReader sectionReader(std::istream& is); |
---|
| 2056 | SectionReader sectionReader(const std::string& fn); |
---|
| 2057 | SectionReader sectionReader(const char* fn); |
---|
[209] | 2058 | |
---|
[192] | 2059 | /// \ingroup lemon_io |
---|
| 2060 | /// |
---|
[189] | 2061 | /// \brief Section reader class |
---|
| 2062 | /// |
---|
[209] | 2063 | /// In the \ref lgf-format "LGF" file extra sections can be placed, |
---|
[192] | 2064 | /// which contain any data in arbitrary format. Such sections can be |
---|
[209] | 2065 | /// read with this class. A reading rule can be added to the class |
---|
[192] | 2066 | /// with two different functions. With the \c sectionLines() function a |
---|
| 2067 | /// functor can process the section line-by-line, while with the \c |
---|
[189] | 2068 | /// sectionStream() member the section can be read from an input |
---|
| 2069 | /// stream. |
---|
| 2070 | class SectionReader { |
---|
| 2071 | private: |
---|
[209] | 2072 | |
---|
[189] | 2073 | std::istream* _is; |
---|
| 2074 | bool local_is; |
---|
[290] | 2075 | std::string _filename; |
---|
[189] | 2076 | |
---|
| 2077 | typedef std::map<std::string, _reader_bits::Section*> Sections; |
---|
| 2078 | Sections _sections; |
---|
| 2079 | |
---|
| 2080 | int line_num; |
---|
| 2081 | std::istringstream line; |
---|
| 2082 | |
---|
| 2083 | public: |
---|
| 2084 | |
---|
| 2085 | /// \brief Constructor |
---|
| 2086 | /// |
---|
| 2087 | /// Construct a section reader, which reads from the given input |
---|
| 2088 | /// stream. |
---|
[209] | 2089 | SectionReader(std::istream& is) |
---|
[189] | 2090 | : _is(&is), local_is(false) {} |
---|
| 2091 | |
---|
| 2092 | /// \brief Constructor |
---|
| 2093 | /// |
---|
| 2094 | /// Construct a section reader, which reads from the given file. |
---|
[209] | 2095 | SectionReader(const std::string& fn) |
---|
[290] | 2096 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
---|
| 2097 | _filename(fn) { |
---|
| 2098 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 2099 | } |
---|
[209] | 2100 | |
---|
[189] | 2101 | /// \brief Constructor |
---|
| 2102 | /// |
---|
| 2103 | /// Construct a section reader, which reads from the given file. |
---|
[209] | 2104 | SectionReader(const char* fn) |
---|
[290] | 2105 | : _is(new std::ifstream(fn)), local_is(true), |
---|
| 2106 | _filename(fn) { |
---|
| 2107 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 2108 | } |
---|
[189] | 2109 | |
---|
| 2110 | /// \brief Destructor |
---|
| 2111 | ~SectionReader() { |
---|
[209] | 2112 | for (Sections::iterator it = _sections.begin(); |
---|
| 2113 | it != _sections.end(); ++it) { |
---|
| 2114 | delete it->second; |
---|
[189] | 2115 | } |
---|
| 2116 | |
---|
| 2117 | if (local_is) { |
---|
[209] | 2118 | delete _is; |
---|
[189] | 2119 | } |
---|
| 2120 | |
---|
| 2121 | } |
---|
| 2122 | |
---|
| 2123 | private: |
---|
[190] | 2124 | |
---|
| 2125 | friend SectionReader sectionReader(std::istream& is); |
---|
| 2126 | friend SectionReader sectionReader(const std::string& fn); |
---|
| 2127 | friend SectionReader sectionReader(const char* fn); |
---|
| 2128 | |
---|
[209] | 2129 | SectionReader(SectionReader& other) |
---|
[190] | 2130 | : _is(other._is), local_is(other.local_is) { |
---|
| 2131 | |
---|
| 2132 | other._is = 0; |
---|
| 2133 | other.local_is = false; |
---|
[209] | 2134 | |
---|
[190] | 2135 | _sections.swap(other._sections); |
---|
| 2136 | } |
---|
[209] | 2137 | |
---|
[189] | 2138 | SectionReader& operator=(const SectionReader&); |
---|
| 2139 | |
---|
| 2140 | public: |
---|
| 2141 | |
---|
| 2142 | /// \name Section readers |
---|
| 2143 | /// @{ |
---|
| 2144 | |
---|
| 2145 | /// \brief Add a section processor with line oriented reading |
---|
| 2146 | /// |
---|
| 2147 | /// The first parameter is the type descriptor of the section, the |
---|
| 2148 | /// second is a functor, which takes just one \c std::string |
---|
| 2149 | /// parameter. At the reading process, each line of the section |
---|
| 2150 | /// will be given to the functor object. However, the empty lines |
---|
| 2151 | /// and the comment lines are filtered out, and the leading |
---|
| 2152 | /// whitespaces are trimmed from each processed string. |
---|
| 2153 | /// |
---|
| 2154 | /// For example let's see a section, which contain several |
---|
| 2155 | /// integers, which should be inserted into a vector. |
---|
| 2156 | ///\code |
---|
| 2157 | /// @numbers |
---|
| 2158 | /// 12 45 23 |
---|
| 2159 | /// 4 |
---|
| 2160 | /// 23 6 |
---|
| 2161 | ///\endcode |
---|
| 2162 | /// |
---|
[192] | 2163 | /// The functor is implemented as a struct: |
---|
[189] | 2164 | ///\code |
---|
| 2165 | /// struct NumberSection { |
---|
| 2166 | /// std::vector<int>& _data; |
---|
| 2167 | /// NumberSection(std::vector<int>& data) : _data(data) {} |
---|
| 2168 | /// void operator()(const std::string& line) { |
---|
| 2169 | /// std::istringstream ls(line); |
---|
| 2170 | /// int value; |
---|
| 2171 | /// while (ls >> value) _data.push_back(value); |
---|
| 2172 | /// } |
---|
| 2173 | /// }; |
---|
| 2174 | /// |
---|
| 2175 | /// // ... |
---|
| 2176 | /// |
---|
[209] | 2177 | /// reader.sectionLines("numbers", NumberSection(vec)); |
---|
[189] | 2178 | ///\endcode |
---|
| 2179 | template <typename Functor> |
---|
| 2180 | SectionReader& sectionLines(const std::string& type, Functor functor) { |
---|
[192] | 2181 | LEMON_ASSERT(!type.empty(), "Type is empty."); |
---|
[209] | 2182 | LEMON_ASSERT(_sections.find(type) == _sections.end(), |
---|
| 2183 | "Multiple reading of section."); |
---|
| 2184 | _sections.insert(std::make_pair(type, |
---|
[189] | 2185 | new _reader_bits::LineSection<Functor>(functor))); |
---|
| 2186 | return *this; |
---|
| 2187 | } |
---|
| 2188 | |
---|
| 2189 | |
---|
| 2190 | /// \brief Add a section processor with stream oriented reading |
---|
| 2191 | /// |
---|
| 2192 | /// The first parameter is the type of the section, the second is |
---|
[192] | 2193 | /// a functor, which takes an \c std::istream& and an \c int& |
---|
[189] | 2194 | /// parameter, the latter regard to the line number of stream. The |
---|
| 2195 | /// functor can read the input while the section go on, and the |
---|
| 2196 | /// line number should be modified accordingly. |
---|
| 2197 | template <typename Functor> |
---|
| 2198 | SectionReader& sectionStream(const std::string& type, Functor functor) { |
---|
[192] | 2199 | LEMON_ASSERT(!type.empty(), "Type is empty."); |
---|
[209] | 2200 | LEMON_ASSERT(_sections.find(type) == _sections.end(), |
---|
| 2201 | "Multiple reading of section."); |
---|
| 2202 | _sections.insert(std::make_pair(type, |
---|
| 2203 | new _reader_bits::StreamSection<Functor>(functor))); |
---|
[189] | 2204 | return *this; |
---|
[209] | 2205 | } |
---|
| 2206 | |
---|
[189] | 2207 | /// @} |
---|
| 2208 | |
---|
| 2209 | private: |
---|
| 2210 | |
---|
| 2211 | bool readLine() { |
---|
| 2212 | std::string str; |
---|
| 2213 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 2214 | line.clear(); line.str(str); |
---|
| 2215 | char c; |
---|
| 2216 | if (line >> std::ws >> c && c != '#') { |
---|
| 2217 | line.putback(c); |
---|
| 2218 | return true; |
---|
| 2219 | } |
---|
[189] | 2220 | } |
---|
| 2221 | return false; |
---|
| 2222 | } |
---|
| 2223 | |
---|
| 2224 | bool readSuccess() { |
---|
| 2225 | return static_cast<bool>(*_is); |
---|
| 2226 | } |
---|
[209] | 2227 | |
---|
[189] | 2228 | void skipSection() { |
---|
| 2229 | char c; |
---|
| 2230 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 2231 | readLine(); |
---|
[189] | 2232 | } |
---|
| 2233 | line.putback(c); |
---|
| 2234 | } |
---|
| 2235 | |
---|
| 2236 | public: |
---|
| 2237 | |
---|
| 2238 | |
---|
[209] | 2239 | /// \name Execution of the reader |
---|
[189] | 2240 | /// @{ |
---|
| 2241 | |
---|
| 2242 | /// \brief Start the batch processing |
---|
| 2243 | /// |
---|
[192] | 2244 | /// This function starts the batch processing. |
---|
[189] | 2245 | void run() { |
---|
[209] | 2246 | |
---|
[189] | 2247 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
---|
[209] | 2248 | |
---|
[189] | 2249 | std::set<std::string> extra_sections; |
---|
| 2250 | |
---|
[209] | 2251 | line_num = 0; |
---|
[189] | 2252 | readLine(); |
---|
| 2253 | skipSection(); |
---|
| 2254 | |
---|
| 2255 | while (readSuccess()) { |
---|
[209] | 2256 | try { |
---|
| 2257 | char c; |
---|
| 2258 | std::string section, caption; |
---|
| 2259 | line >> c; |
---|
| 2260 | _reader_bits::readToken(line, section); |
---|
| 2261 | _reader_bits::readToken(line, caption); |
---|
| 2262 | |
---|
| 2263 | if (line >> c) |
---|
[290] | 2264 | throw FormatError("Extra character on the end of line"); |
---|
[209] | 2265 | |
---|
| 2266 | if (extra_sections.find(section) != extra_sections.end()) { |
---|
| 2267 | std::ostringstream msg; |
---|
| 2268 | msg << "Multiple occurence of section " << section; |
---|
[290] | 2269 | throw FormatError(msg.str()); |
---|
[209] | 2270 | } |
---|
| 2271 | Sections::iterator it = _sections.find(section); |
---|
| 2272 | if (it != _sections.end()) { |
---|
| 2273 | extra_sections.insert(section); |
---|
| 2274 | it->second->process(*_is, line_num); |
---|
| 2275 | } |
---|
| 2276 | readLine(); |
---|
| 2277 | skipSection(); |
---|
[290] | 2278 | } catch (FormatError& error) { |
---|
[209] | 2279 | error.line(line_num); |
---|
[290] | 2280 | error.file(_filename); |
---|
[209] | 2281 | throw; |
---|
| 2282 | } |
---|
[189] | 2283 | } |
---|
| 2284 | for (Sections::iterator it = _sections.begin(); |
---|
[209] | 2285 | it != _sections.end(); ++it) { |
---|
| 2286 | if (extra_sections.find(it->first) == extra_sections.end()) { |
---|
| 2287 | std::ostringstream os; |
---|
| 2288 | os << "Cannot find section: " << it->first; |
---|
[290] | 2289 | throw FormatError(os.str()); |
---|
[209] | 2290 | } |
---|
[189] | 2291 | } |
---|
| 2292 | } |
---|
| 2293 | |
---|
| 2294 | /// @} |
---|
[209] | 2295 | |
---|
[189] | 2296 | }; |
---|
| 2297 | |
---|
[192] | 2298 | /// \brief Return a \ref SectionReader class |
---|
[209] | 2299 | /// |
---|
[192] | 2300 | /// This function just returns a \ref SectionReader class. |
---|
[189] | 2301 | /// \relates SectionReader |
---|
| 2302 | inline SectionReader sectionReader(std::istream& is) { |
---|
| 2303 | SectionReader tmp(is); |
---|
| 2304 | return tmp; |
---|
| 2305 | } |
---|
| 2306 | |
---|
[192] | 2307 | /// \brief Return a \ref SectionReader class |
---|
[209] | 2308 | /// |
---|
[192] | 2309 | /// This function just returns a \ref SectionReader class. |
---|
[189] | 2310 | /// \relates SectionReader |
---|
| 2311 | inline SectionReader sectionReader(const std::string& fn) { |
---|
| 2312 | SectionReader tmp(fn); |
---|
| 2313 | return tmp; |
---|
| 2314 | } |
---|
| 2315 | |
---|
[192] | 2316 | /// \brief Return a \ref SectionReader class |
---|
[209] | 2317 | /// |
---|
[192] | 2318 | /// This function just returns a \ref SectionReader class. |
---|
[189] | 2319 | /// \relates SectionReader |
---|
| 2320 | inline SectionReader sectionReader(const char* fn) { |
---|
| 2321 | SectionReader tmp(fn); |
---|
| 2322 | return tmp; |
---|
| 2323 | } |
---|
| 2324 | |
---|
[173] | 2325 | /// \ingroup lemon_io |
---|
| 2326 | /// |
---|
[209] | 2327 | /// \brief Reader for the contents of the \ref lgf-format "LGF" file |
---|
[173] | 2328 | /// |
---|
| 2329 | /// This class can be used to read the sections, the map names and |
---|
[236] | 2330 | /// the attributes from a file. Usually, the LEMON programs know |
---|
[173] | 2331 | /// that, which type of graph, which maps and which attributes |
---|
| 2332 | /// should be read from a file, but in general tools (like glemon) |
---|
[179] | 2333 | /// the contents of an LGF file should be guessed somehow. This class |
---|
[173] | 2334 | /// reads the graph and stores the appropriate information for |
---|
| 2335 | /// reading the graph. |
---|
| 2336 | /// |
---|
[209] | 2337 | ///\code |
---|
| 2338 | /// LgfContents contents("graph.lgf"); |
---|
[179] | 2339 | /// contents.run(); |
---|
[173] | 2340 | /// |
---|
[192] | 2341 | /// // Does it contain any node section and arc section? |
---|
[179] | 2342 | /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) { |
---|
[192] | 2343 | /// std::cerr << "Failure, cannot find graph." << std::endl; |
---|
[173] | 2344 | /// return -1; |
---|
| 2345 | /// } |
---|
[209] | 2346 | /// std::cout << "The name of the default node section: " |
---|
[179] | 2347 | /// << contents.nodeSection(0) << std::endl; |
---|
[209] | 2348 | /// std::cout << "The number of the arc maps: " |
---|
[179] | 2349 | /// << contents.arcMaps(0).size() << std::endl; |
---|
[209] | 2350 | /// std::cout << "The name of second arc map: " |
---|
[179] | 2351 | /// << contents.arcMaps(0)[1] << std::endl; |
---|
[173] | 2352 | ///\endcode |
---|
[209] | 2353 | class LgfContents { |
---|
[173] | 2354 | private: |
---|
| 2355 | |
---|
| 2356 | std::istream* _is; |
---|
| 2357 | bool local_is; |
---|
| 2358 | |
---|
| 2359 | std::vector<std::string> _node_sections; |
---|
| 2360 | std::vector<std::string> _edge_sections; |
---|
| 2361 | std::vector<std::string> _attribute_sections; |
---|
| 2362 | std::vector<std::string> _extra_sections; |
---|
| 2363 | |
---|
| 2364 | std::vector<bool> _arc_sections; |
---|
| 2365 | |
---|
| 2366 | std::vector<std::vector<std::string> > _node_maps; |
---|
| 2367 | std::vector<std::vector<std::string> > _edge_maps; |
---|
| 2368 | |
---|
| 2369 | std::vector<std::vector<std::string> > _attributes; |
---|
| 2370 | |
---|
| 2371 | |
---|
| 2372 | int line_num; |
---|
| 2373 | std::istringstream line; |
---|
[209] | 2374 | |
---|
[173] | 2375 | public: |
---|
| 2376 | |
---|
| 2377 | /// \brief Constructor |
---|
| 2378 | /// |
---|
[179] | 2379 | /// Construct an \e LGF contents reader, which reads from the given |
---|
[173] | 2380 | /// input stream. |
---|
[209] | 2381 | LgfContents(std::istream& is) |
---|
[173] | 2382 | : _is(&is), local_is(false) {} |
---|
| 2383 | |
---|
| 2384 | /// \brief Constructor |
---|
| 2385 | /// |
---|
[179] | 2386 | /// Construct an \e LGF contents reader, which reads from the given |
---|
[173] | 2387 | /// file. |
---|
[209] | 2388 | LgfContents(const std::string& fn) |
---|
[290] | 2389 | : _is(new std::ifstream(fn.c_str())), local_is(true) { |
---|
| 2390 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 2391 | } |
---|
[173] | 2392 | |
---|
| 2393 | /// \brief Constructor |
---|
| 2394 | /// |
---|
[179] | 2395 | /// Construct an \e LGF contents reader, which reads from the given |
---|
[173] | 2396 | /// file. |
---|
[179] | 2397 | LgfContents(const char* fn) |
---|
[290] | 2398 | : _is(new std::ifstream(fn)), local_is(true) { |
---|
| 2399 | if (!(*_is)) throw IoError(fn, "Cannot open file"); |
---|
| 2400 | } |
---|
[209] | 2401 | |
---|
[173] | 2402 | /// \brief Destructor |
---|
[179] | 2403 | ~LgfContents() { |
---|
[173] | 2404 | if (local_is) delete _is; |
---|
| 2405 | } |
---|
| 2406 | |
---|
[190] | 2407 | private: |
---|
[209] | 2408 | |
---|
[190] | 2409 | LgfContents(const LgfContents&); |
---|
| 2410 | LgfContents& operator=(const LgfContents&); |
---|
| 2411 | |
---|
| 2412 | public: |
---|
| 2413 | |
---|
[173] | 2414 | |
---|
| 2415 | /// \name Node sections |
---|
| 2416 | /// @{ |
---|
| 2417 | |
---|
| 2418 | /// \brief Gives back the number of node sections in the file. |
---|
| 2419 | /// |
---|
| 2420 | /// Gives back the number of node sections in the file. |
---|
| 2421 | int nodeSectionNum() const { |
---|
| 2422 | return _node_sections.size(); |
---|
| 2423 | } |
---|
| 2424 | |
---|
[209] | 2425 | /// \brief Returns the node section name at the given position. |
---|
[173] | 2426 | /// |
---|
[209] | 2427 | /// Returns the node section name at the given position. |
---|
[173] | 2428 | const std::string& nodeSection(int i) const { |
---|
| 2429 | return _node_sections[i]; |
---|
| 2430 | } |
---|
| 2431 | |
---|
| 2432 | /// \brief Gives back the node maps for the given section. |
---|
| 2433 | /// |
---|
| 2434 | /// Gives back the node maps for the given section. |
---|
[182] | 2435 | const std::vector<std::string>& nodeMapNames(int i) const { |
---|
[173] | 2436 | return _node_maps[i]; |
---|
| 2437 | } |
---|
| 2438 | |
---|
| 2439 | /// @} |
---|
| 2440 | |
---|
[209] | 2441 | /// \name Arc/Edge sections |
---|
[173] | 2442 | /// @{ |
---|
| 2443 | |
---|
[181] | 2444 | /// \brief Gives back the number of arc/edge sections in the file. |
---|
[173] | 2445 | /// |
---|
[181] | 2446 | /// Gives back the number of arc/edge sections in the file. |
---|
| 2447 | /// \note It is synonym of \c edgeSectionNum(). |
---|
[173] | 2448 | int arcSectionNum() const { |
---|
| 2449 | return _edge_sections.size(); |
---|
| 2450 | } |
---|
| 2451 | |
---|
[209] | 2452 | /// \brief Returns the arc/edge section name at the given position. |
---|
[173] | 2453 | /// |
---|
[209] | 2454 | /// Returns the arc/edge section name at the given position. |
---|
[181] | 2455 | /// \note It is synonym of \c edgeSection(). |
---|
[173] | 2456 | const std::string& arcSection(int i) const { |
---|
| 2457 | return _edge_sections[i]; |
---|
| 2458 | } |
---|
| 2459 | |
---|
[181] | 2460 | /// \brief Gives back the arc/edge maps for the given section. |
---|
[173] | 2461 | /// |
---|
[181] | 2462 | /// Gives back the arc/edge maps for the given section. |
---|
[182] | 2463 | /// \note It is synonym of \c edgeMapNames(). |
---|
| 2464 | const std::vector<std::string>& arcMapNames(int i) const { |
---|
[173] | 2465 | return _edge_maps[i]; |
---|
| 2466 | } |
---|
| 2467 | |
---|
| 2468 | /// @} |
---|
| 2469 | |
---|
[181] | 2470 | /// \name Synonyms |
---|
[173] | 2471 | /// @{ |
---|
| 2472 | |
---|
[181] | 2473 | /// \brief Gives back the number of arc/edge sections in the file. |
---|
[173] | 2474 | /// |
---|
[181] | 2475 | /// Gives back the number of arc/edge sections in the file. |
---|
| 2476 | /// \note It is synonym of \c arcSectionNum(). |
---|
[173] | 2477 | int edgeSectionNum() const { |
---|
| 2478 | return _edge_sections.size(); |
---|
| 2479 | } |
---|
| 2480 | |
---|
[209] | 2481 | /// \brief Returns the section name at the given position. |
---|
[173] | 2482 | /// |
---|
[209] | 2483 | /// Returns the section name at the given position. |
---|
[181] | 2484 | /// \note It is synonym of \c arcSection(). |
---|
[173] | 2485 | const std::string& edgeSection(int i) const { |
---|
| 2486 | return _edge_sections[i]; |
---|
| 2487 | } |
---|
| 2488 | |
---|
| 2489 | /// \brief Gives back the edge maps for the given section. |
---|
| 2490 | /// |
---|
| 2491 | /// Gives back the edge maps for the given section. |
---|
[182] | 2492 | /// \note It is synonym of \c arcMapNames(). |
---|
| 2493 | const std::vector<std::string>& edgeMapNames(int i) const { |
---|
[173] | 2494 | return _edge_maps[i]; |
---|
| 2495 | } |
---|
| 2496 | |
---|
| 2497 | /// @} |
---|
| 2498 | |
---|
[209] | 2499 | /// \name Attribute sections |
---|
[173] | 2500 | /// @{ |
---|
| 2501 | |
---|
| 2502 | /// \brief Gives back the number of attribute sections in the file. |
---|
| 2503 | /// |
---|
| 2504 | /// Gives back the number of attribute sections in the file. |
---|
| 2505 | int attributeSectionNum() const { |
---|
| 2506 | return _attribute_sections.size(); |
---|
| 2507 | } |
---|
| 2508 | |
---|
[209] | 2509 | /// \brief Returns the attribute section name at the given position. |
---|
[173] | 2510 | /// |
---|
[209] | 2511 | /// Returns the attribute section name at the given position. |
---|
[182] | 2512 | const std::string& attributeSectionNames(int i) const { |
---|
[173] | 2513 | return _attribute_sections[i]; |
---|
| 2514 | } |
---|
| 2515 | |
---|
| 2516 | /// \brief Gives back the attributes for the given section. |
---|
| 2517 | /// |
---|
| 2518 | /// Gives back the attributes for the given section. |
---|
| 2519 | const std::vector<std::string>& attributes(int i) const { |
---|
| 2520 | return _attributes[i]; |
---|
| 2521 | } |
---|
| 2522 | |
---|
| 2523 | /// @} |
---|
| 2524 | |
---|
[209] | 2525 | /// \name Extra sections |
---|
[173] | 2526 | /// @{ |
---|
| 2527 | |
---|
| 2528 | /// \brief Gives back the number of extra sections in the file. |
---|
| 2529 | /// |
---|
| 2530 | /// Gives back the number of extra sections in the file. |
---|
| 2531 | int extraSectionNum() const { |
---|
| 2532 | return _extra_sections.size(); |
---|
| 2533 | } |
---|
| 2534 | |
---|
[209] | 2535 | /// \brief Returns the extra section type at the given position. |
---|
[173] | 2536 | /// |
---|
[209] | 2537 | /// Returns the section type at the given position. |
---|
[173] | 2538 | const std::string& extraSection(int i) const { |
---|
| 2539 | return _extra_sections[i]; |
---|
| 2540 | } |
---|
| 2541 | |
---|
| 2542 | /// @} |
---|
| 2543 | |
---|
| 2544 | private: |
---|
| 2545 | |
---|
| 2546 | bool readLine() { |
---|
| 2547 | std::string str; |
---|
| 2548 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 2549 | line.clear(); line.str(str); |
---|
| 2550 | char c; |
---|
| 2551 | if (line >> std::ws >> c && c != '#') { |
---|
| 2552 | line.putback(c); |
---|
| 2553 | return true; |
---|
| 2554 | } |
---|
[173] | 2555 | } |
---|
| 2556 | return false; |
---|
| 2557 | } |
---|
| 2558 | |
---|
| 2559 | bool readSuccess() { |
---|
| 2560 | return static_cast<bool>(*_is); |
---|
| 2561 | } |
---|
| 2562 | |
---|
| 2563 | void skipSection() { |
---|
| 2564 | char c; |
---|
| 2565 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 2566 | readLine(); |
---|
[173] | 2567 | } |
---|
| 2568 | line.putback(c); |
---|
| 2569 | } |
---|
| 2570 | |
---|
| 2571 | void readMaps(std::vector<std::string>& maps) { |
---|
[186] | 2572 | char c; |
---|
| 2573 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 2574 | if (readSuccess() && line) line.putback(c); |
---|
| 2575 | return; |
---|
[186] | 2576 | } |
---|
| 2577 | line.putback(c); |
---|
[173] | 2578 | std::string map; |
---|
| 2579 | while (_reader_bits::readToken(line, map)) { |
---|
[209] | 2580 | maps.push_back(map); |
---|
[173] | 2581 | } |
---|
| 2582 | } |
---|
| 2583 | |
---|
| 2584 | void readAttributes(std::vector<std::string>& attrs) { |
---|
| 2585 | readLine(); |
---|
| 2586 | char c; |
---|
| 2587 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 2588 | line.putback(c); |
---|
| 2589 | std::string attr; |
---|
| 2590 | _reader_bits::readToken(line, attr); |
---|
| 2591 | attrs.push_back(attr); |
---|
| 2592 | readLine(); |
---|
[173] | 2593 | } |
---|
| 2594 | line.putback(c); |
---|
| 2595 | } |
---|
| 2596 | |
---|
| 2597 | public: |
---|
| 2598 | |
---|
[209] | 2599 | /// \name Execution of the contents reader |
---|
[173] | 2600 | /// @{ |
---|
| 2601 | |
---|
[192] | 2602 | /// \brief Starts the reading |
---|
[173] | 2603 | /// |
---|
[192] | 2604 | /// This function starts the reading. |
---|
[173] | 2605 | void run() { |
---|
| 2606 | |
---|
| 2607 | readLine(); |
---|
| 2608 | skipSection(); |
---|
| 2609 | |
---|
| 2610 | while (readSuccess()) { |
---|
| 2611 | |
---|
[209] | 2612 | char c; |
---|
| 2613 | line >> c; |
---|
| 2614 | |
---|
| 2615 | std::string section, caption; |
---|
| 2616 | _reader_bits::readToken(line, section); |
---|
| 2617 | _reader_bits::readToken(line, caption); |
---|
| 2618 | |
---|
| 2619 | if (section == "nodes") { |
---|
| 2620 | _node_sections.push_back(caption); |
---|
| 2621 | _node_maps.push_back(std::vector<std::string>()); |
---|
| 2622 | readMaps(_node_maps.back()); |
---|
| 2623 | readLine(); skipSection(); |
---|
| 2624 | } else if (section == "arcs" || section == "edges") { |
---|
| 2625 | _edge_sections.push_back(caption); |
---|
| 2626 | _arc_sections.push_back(section == "arcs"); |
---|
| 2627 | _edge_maps.push_back(std::vector<std::string>()); |
---|
| 2628 | readMaps(_edge_maps.back()); |
---|
| 2629 | readLine(); skipSection(); |
---|
| 2630 | } else if (section == "attributes") { |
---|
| 2631 | _attribute_sections.push_back(caption); |
---|
| 2632 | _attributes.push_back(std::vector<std::string>()); |
---|
| 2633 | readAttributes(_attributes.back()); |
---|
| 2634 | } else { |
---|
| 2635 | _extra_sections.push_back(section); |
---|
| 2636 | readLine(); skipSection(); |
---|
| 2637 | } |
---|
[173] | 2638 | } |
---|
| 2639 | } |
---|
| 2640 | |
---|
| 2641 | /// @} |
---|
[209] | 2642 | |
---|
[173] | 2643 | }; |
---|
[127] | 2644 | } |
---|
| 2645 | |
---|
| 2646 | #endif |
---|