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