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