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