[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 | |
---|
[645] | 104 | template <typename _GR, bool _dir, typename _Map, |
---|
[209] | 105 | typename _Converter = DefaultConverter<typename _Map::Value> > |
---|
[645] | 106 | class GraphArcMapStorage : public MapStorageBase<typename _GR::Edge> { |
---|
[165] | 107 | public: |
---|
| 108 | typedef _Map Map; |
---|
| 109 | typedef _Converter Converter; |
---|
[645] | 110 | typedef _GR GR; |
---|
| 111 | typedef typename GR::Edge Item; |
---|
[165] | 112 | static const bool dir = _dir; |
---|
[209] | 113 | |
---|
[165] | 114 | private: |
---|
[645] | 115 | const GR& _graph; |
---|
[165] | 116 | Map& _map; |
---|
| 117 | Converter _converter; |
---|
| 118 | |
---|
| 119 | public: |
---|
[645] | 120 | GraphArcMapStorage(const GR& graph, Map& map, |
---|
[209] | 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 | |
---|
[645] | 176 | template <typename GR> |
---|
[165] | 177 | struct GraphArcLookUpConverter { |
---|
[645] | 178 | const GR& _graph; |
---|
| 179 | const std::map<std::string, typename GR::Edge>& _map; |
---|
| 180 | |
---|
| 181 | GraphArcLookUpConverter(const GR& graph, |
---|
[209] | 182 | const std::map<std::string, |
---|
[645] | 183 | typename GR::Edge>& map) |
---|
[209] | 184 | : _graph(graph), _map(map) {} |
---|
| 185 | |
---|
[645] | 186 | typename GR::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 | } |
---|
[645] | 190 | typename std::map<std::string, typename GR::Edge> |
---|
[209] | 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 | |
---|
[645] | 390 | template <typename DGR> |
---|
[190] | 391 | class DigraphReader; |
---|
| 392 | |
---|
[645] | 393 | template <typename TDGR> |
---|
| 394 | DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is = std::cin); |
---|
| 395 | template <typename TDGR> |
---|
| 396 | DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn); |
---|
| 397 | template <typename TDGR> |
---|
| 398 | DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn); |
---|
[190] | 399 | |
---|
[156] | 400 | /// \ingroup lemon_io |
---|
[209] | 401 | /// |
---|
[192] | 402 | /// \brief \ref lgf-format "LGF" reader for directed graphs |
---|
[156] | 403 | /// |
---|
| 404 | /// This utility reads an \ref lgf-format "LGF" file. |
---|
| 405 | /// |
---|
| 406 | /// The reading method does a batch processing. The user creates a |
---|
| 407 | /// reader object, then various reading rules can be added to the |
---|
| 408 | /// reader, and eventually the reading is executed with the \c run() |
---|
| 409 | /// member function. A map reading rule can be added to the reader |
---|
| 410 | /// with the \c nodeMap() or \c arcMap() members. An optional |
---|
[162] | 411 | /// converter parameter can also be added as a standard functor |
---|
[192] | 412 | /// converting from \c std::string to the value type of the map. If it |
---|
[162] | 413 | /// is set, it will determine how the tokens in the file should be |
---|
[192] | 414 | /// converted to the value type of the map. If the functor is not set, |
---|
[162] | 415 | /// then a default conversion will be used. One map can be read into |
---|
| 416 | /// multiple map objects at the same time. The \c attribute(), \c |
---|
| 417 | /// node() and \c arc() functions are used to add attribute reading |
---|
| 418 | /// rules. |
---|
[156] | 419 | /// |
---|
| 420 | ///\code |
---|
[645] | 421 | /// DigraphReader<DGR>(digraph, std::cin). |
---|
[192] | 422 | /// nodeMap("coordinates", coord_map). |
---|
| 423 | /// arcMap("capacity", cap_map). |
---|
| 424 | /// node("source", src). |
---|
| 425 | /// node("target", trg). |
---|
| 426 | /// attribute("caption", caption). |
---|
| 427 | /// run(); |
---|
[156] | 428 | ///\endcode |
---|
| 429 | /// |
---|
[833] | 430 | /// By default, the reader uses the first section in the file of the |
---|
[156] | 431 | /// proper type. If a section has an optional name, then it can be |
---|
[162] | 432 | /// selected for reading by giving an optional name parameter to the |
---|
[189] | 433 | /// \c nodes(), \c arcs() or \c attributes() functions. |
---|
[156] | 434 | /// |
---|
| 435 | /// The \c useNodes() and \c useArcs() functions are used to tell the reader |
---|
| 436 | /// that the nodes or arcs should not be constructed (added to the |
---|
| 437 | /// graph) during the reading, but instead the label map of the items |
---|
| 438 | /// are given as a parameter of these functions. An |
---|
[192] | 439 | /// application of these functions is multipass reading, which is |
---|
| 440 | /// important if two \c \@arcs sections must be read from the |
---|
| 441 | /// file. In this case the first phase would read the node set and one |
---|
[156] | 442 | /// of the arc sets, while the second phase would read the second arc |
---|
| 443 | /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
---|
| 444 | /// The previously read label node map should be passed to the \c |
---|
| 445 | /// useNodes() functions. Another application of multipass reading when |
---|
[210] | 446 | /// paths are given as a node map or an arc map. |
---|
| 447 | /// It is impossible to read this in |
---|
[156] | 448 | /// a single pass, because the arcs are not constructed when the node |
---|
| 449 | /// maps are read. |
---|
[645] | 450 | template <typename DGR> |
---|
[127] | 451 | class DigraphReader { |
---|
| 452 | public: |
---|
| 453 | |
---|
[645] | 454 | typedef DGR Digraph; |
---|
[606] | 455 | |
---|
| 456 | private: |
---|
| 457 | |
---|
[645] | 458 | TEMPLATE_DIGRAPH_TYPEDEFS(DGR); |
---|
[209] | 459 | |
---|
[127] | 460 | std::istream* _is; |
---|
| 461 | bool local_is; |
---|
[290] | 462 | std::string _filename; |
---|
[127] | 463 | |
---|
[645] | 464 | DGR& _digraph; |
---|
[127] | 465 | |
---|
| 466 | std::string _nodes_caption; |
---|
| 467 | std::string _arcs_caption; |
---|
| 468 | std::string _attributes_caption; |
---|
| 469 | |
---|
| 470 | typedef std::map<std::string, Node> NodeIndex; |
---|
| 471 | NodeIndex _node_index; |
---|
| 472 | typedef std::map<std::string, Arc> ArcIndex; |
---|
| 473 | ArcIndex _arc_index; |
---|
[209] | 474 | |
---|
| 475 | typedef std::vector<std::pair<std::string, |
---|
| 476 | _reader_bits::MapStorageBase<Node>*> > NodeMaps; |
---|
| 477 | NodeMaps _node_maps; |
---|
[127] | 478 | |
---|
| 479 | typedef std::vector<std::pair<std::string, |
---|
| 480 | _reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
---|
| 481 | ArcMaps _arc_maps; |
---|
| 482 | |
---|
[209] | 483 | typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
---|
[127] | 484 | Attributes; |
---|
| 485 | Attributes _attributes; |
---|
| 486 | |
---|
| 487 | bool _use_nodes; |
---|
| 488 | bool _use_arcs; |
---|
| 489 | |
---|
[188] | 490 | bool _skip_nodes; |
---|
| 491 | bool _skip_arcs; |
---|
| 492 | |
---|
[127] | 493 | int line_num; |
---|
| 494 | std::istringstream line; |
---|
| 495 | |
---|
| 496 | public: |
---|
| 497 | |
---|
[156] | 498 | /// \brief Constructor |
---|
| 499 | /// |
---|
| 500 | /// Construct a directed graph reader, which reads from the given |
---|
| 501 | /// input stream. |
---|
[645] | 502 | DigraphReader(DGR& digraph, std::istream& is = std::cin) |
---|
[127] | 503 | : _is(&is), local_is(false), _digraph(digraph), |
---|
[209] | 504 | _use_nodes(false), _use_arcs(false), |
---|
| 505 | _skip_nodes(false), _skip_arcs(false) {} |
---|
[127] | 506 | |
---|
[156] | 507 | /// \brief Constructor |
---|
| 508 | /// |
---|
| 509 | /// Construct a directed graph reader, which reads from the given |
---|
| 510 | /// file. |
---|
[645] | 511 | DigraphReader(DGR& digraph, const std::string& fn) |
---|
[290] | 512 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
---|
| 513 | _filename(fn), _digraph(digraph), |
---|
[212] | 514 | _use_nodes(false), _use_arcs(false), |
---|
[290] | 515 | _skip_nodes(false), _skip_arcs(false) { |
---|
[295] | 516 | if (!(*_is)) { |
---|
| 517 | delete _is; |
---|
| 518 | throw IoError("Cannot open file", fn); |
---|
| 519 | } |
---|
[290] | 520 | } |
---|
[209] | 521 | |
---|
[156] | 522 | /// \brief Constructor |
---|
| 523 | /// |
---|
| 524 | /// Construct a directed graph reader, which reads from the given |
---|
| 525 | /// file. |
---|
[645] | 526 | DigraphReader(DGR& digraph, const char* fn) |
---|
[290] | 527 | : _is(new std::ifstream(fn)), local_is(true), |
---|
| 528 | _filename(fn), _digraph(digraph), |
---|
[212] | 529 | _use_nodes(false), _use_arcs(false), |
---|
[290] | 530 | _skip_nodes(false), _skip_arcs(false) { |
---|
[295] | 531 | if (!(*_is)) { |
---|
| 532 | delete _is; |
---|
| 533 | throw IoError("Cannot open file", fn); |
---|
| 534 | } |
---|
[290] | 535 | } |
---|
[127] | 536 | |
---|
[156] | 537 | /// \brief Destructor |
---|
[127] | 538 | ~DigraphReader() { |
---|
[209] | 539 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 540 | it != _node_maps.end(); ++it) { |
---|
| 541 | delete it->second; |
---|
[127] | 542 | } |
---|
| 543 | |
---|
[209] | 544 | for (typename ArcMaps::iterator it = _arc_maps.begin(); |
---|
| 545 | it != _arc_maps.end(); ++it) { |
---|
| 546 | delete it->second; |
---|
[127] | 547 | } |
---|
| 548 | |
---|
[209] | 549 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 550 | it != _attributes.end(); ++it) { |
---|
| 551 | delete it->second; |
---|
[127] | 552 | } |
---|
| 553 | |
---|
| 554 | if (local_is) { |
---|
[209] | 555 | delete _is; |
---|
[127] | 556 | } |
---|
| 557 | |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | private: |
---|
[190] | 561 | |
---|
[645] | 562 | template <typename TDGR> |
---|
| 563 | friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is); |
---|
| 564 | template <typename TDGR> |
---|
[956] | 565 | friend DigraphReader<TDGR> digraphReader(TDGR& digraph, |
---|
[645] | 566 | const std::string& fn); |
---|
| 567 | template <typename TDGR> |
---|
| 568 | friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn); |
---|
[209] | 569 | |
---|
| 570 | DigraphReader(DigraphReader& other) |
---|
[190] | 571 | : _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
---|
[209] | 572 | _use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
---|
| 573 | _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { |
---|
[190] | 574 | |
---|
| 575 | other._is = 0; |
---|
| 576 | other.local_is = false; |
---|
[209] | 577 | |
---|
[190] | 578 | _node_index.swap(other._node_index); |
---|
| 579 | _arc_index.swap(other._arc_index); |
---|
| 580 | |
---|
| 581 | _node_maps.swap(other._node_maps); |
---|
| 582 | _arc_maps.swap(other._arc_maps); |
---|
| 583 | _attributes.swap(other._attributes); |
---|
| 584 | |
---|
| 585 | _nodes_caption = other._nodes_caption; |
---|
| 586 | _arcs_caption = other._arcs_caption; |
---|
| 587 | _attributes_caption = other._attributes_caption; |
---|
| 588 | |
---|
| 589 | } |
---|
| 590 | |
---|
[127] | 591 | DigraphReader& operator=(const DigraphReader&); |
---|
| 592 | |
---|
| 593 | public: |
---|
| 594 | |
---|
[631] | 595 | /// \name Reading Rules |
---|
[156] | 596 | /// @{ |
---|
[209] | 597 | |
---|
[156] | 598 | /// \brief Node map reading rule |
---|
| 599 | /// |
---|
| 600 | /// Add a node map reading rule to the reader. |
---|
[127] | 601 | template <typename Map> |
---|
| 602 | DigraphReader& nodeMap(const std::string& caption, Map& map) { |
---|
| 603 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 604 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 605 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
[127] | 606 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 607 | return *this; |
---|
| 608 | } |
---|
| 609 | |
---|
[156] | 610 | /// \brief Node map reading rule |
---|
| 611 | /// |
---|
| 612 | /// Add a node map reading rule with specialized converter to the |
---|
| 613 | /// reader. |
---|
[127] | 614 | template <typename Map, typename Converter> |
---|
[209] | 615 | DigraphReader& nodeMap(const std::string& caption, Map& map, |
---|
| 616 | const Converter& converter = Converter()) { |
---|
[127] | 617 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 618 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 619 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
[127] | 620 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 621 | return *this; |
---|
| 622 | } |
---|
| 623 | |
---|
[156] | 624 | /// \brief Arc map reading rule |
---|
| 625 | /// |
---|
| 626 | /// Add an arc map reading rule to the reader. |
---|
[127] | 627 | template <typename Map> |
---|
| 628 | DigraphReader& arcMap(const std::string& caption, Map& map) { |
---|
| 629 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 630 | _reader_bits::MapStorageBase<Arc>* storage = |
---|
| 631 | new _reader_bits::MapStorage<Arc, Map>(map); |
---|
[127] | 632 | _arc_maps.push_back(std::make_pair(caption, storage)); |
---|
| 633 | return *this; |
---|
| 634 | } |
---|
| 635 | |
---|
[156] | 636 | /// \brief Arc map reading rule |
---|
| 637 | /// |
---|
| 638 | /// Add an arc map reading rule with specialized converter to the |
---|
| 639 | /// reader. |
---|
[127] | 640 | template <typename Map, typename Converter> |
---|
[209] | 641 | DigraphReader& arcMap(const std::string& caption, Map& map, |
---|
| 642 | const Converter& converter = Converter()) { |
---|
[127] | 643 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 644 | _reader_bits::MapStorageBase<Arc>* storage = |
---|
| 645 | new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter); |
---|
[127] | 646 | _arc_maps.push_back(std::make_pair(caption, storage)); |
---|
| 647 | return *this; |
---|
| 648 | } |
---|
| 649 | |
---|
[156] | 650 | /// \brief Attribute reading rule |
---|
| 651 | /// |
---|
| 652 | /// Add an attribute reading rule to the reader. |
---|
[127] | 653 | template <typename Value> |
---|
| 654 | DigraphReader& attribute(const std::string& caption, Value& value) { |
---|
[209] | 655 | _reader_bits::ValueStorageBase* storage = |
---|
| 656 | new _reader_bits::ValueStorage<Value>(value); |
---|
[127] | 657 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 658 | return *this; |
---|
| 659 | } |
---|
| 660 | |
---|
[156] | 661 | /// \brief Attribute reading rule |
---|
| 662 | /// |
---|
| 663 | /// Add an attribute reading rule with specialized converter to the |
---|
| 664 | /// reader. |
---|
[127] | 665 | template <typename Value, typename Converter> |
---|
[209] | 666 | DigraphReader& attribute(const std::string& caption, Value& value, |
---|
| 667 | const Converter& converter = Converter()) { |
---|
| 668 | _reader_bits::ValueStorageBase* storage = |
---|
| 669 | new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
---|
[127] | 670 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 671 | return *this; |
---|
| 672 | } |
---|
| 673 | |
---|
[156] | 674 | /// \brief Node reading rule |
---|
| 675 | /// |
---|
| 676 | /// Add a node reading rule to reader. |
---|
[127] | 677 | DigraphReader& node(const std::string& caption, Node& node) { |
---|
| 678 | typedef _reader_bits::MapLookUpConverter<Node> Converter; |
---|
| 679 | Converter converter(_node_index); |
---|
[209] | 680 | _reader_bits::ValueStorageBase* storage = |
---|
| 681 | new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
---|
[127] | 682 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 683 | return *this; |
---|
| 684 | } |
---|
| 685 | |
---|
[156] | 686 | /// \brief Arc reading rule |
---|
| 687 | /// |
---|
| 688 | /// Add an arc reading rule to reader. |
---|
[127] | 689 | DigraphReader& arc(const std::string& caption, Arc& arc) { |
---|
| 690 | typedef _reader_bits::MapLookUpConverter<Arc> Converter; |
---|
| 691 | Converter converter(_arc_index); |
---|
[209] | 692 | _reader_bits::ValueStorageBase* storage = |
---|
| 693 | new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
---|
[127] | 694 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 695 | return *this; |
---|
| 696 | } |
---|
| 697 | |
---|
[156] | 698 | /// @} |
---|
| 699 | |
---|
[631] | 700 | /// \name Select Section by Name |
---|
[156] | 701 | /// @{ |
---|
| 702 | |
---|
| 703 | /// \brief Set \c \@nodes section to be read |
---|
| 704 | /// |
---|
| 705 | /// Set \c \@nodes section to be read |
---|
[127] | 706 | DigraphReader& nodes(const std::string& caption) { |
---|
| 707 | _nodes_caption = caption; |
---|
| 708 | return *this; |
---|
| 709 | } |
---|
| 710 | |
---|
[156] | 711 | /// \brief Set \c \@arcs section to be read |
---|
| 712 | /// |
---|
| 713 | /// Set \c \@arcs section to be read |
---|
[127] | 714 | DigraphReader& arcs(const std::string& caption) { |
---|
| 715 | _arcs_caption = caption; |
---|
| 716 | return *this; |
---|
| 717 | } |
---|
| 718 | |
---|
[156] | 719 | /// \brief Set \c \@attributes section to be read |
---|
| 720 | /// |
---|
| 721 | /// Set \c \@attributes section to be read |
---|
[127] | 722 | DigraphReader& attributes(const std::string& caption) { |
---|
| 723 | _attributes_caption = caption; |
---|
| 724 | return *this; |
---|
| 725 | } |
---|
| 726 | |
---|
[156] | 727 | /// @} |
---|
| 728 | |
---|
[631] | 729 | /// \name Using Previously Constructed Node or Arc Set |
---|
[156] | 730 | /// @{ |
---|
| 731 | |
---|
| 732 | /// \brief Use previously constructed node set |
---|
| 733 | /// |
---|
| 734 | /// Use previously constructed node set, and specify the node |
---|
| 735 | /// label map. |
---|
[127] | 736 | template <typename Map> |
---|
| 737 | DigraphReader& useNodes(const Map& map) { |
---|
| 738 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 739 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[127] | 740 | _use_nodes = true; |
---|
| 741 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 742 | for (NodeIt n(_digraph); n != INVALID; ++n) { |
---|
[209] | 743 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[127] | 744 | } |
---|
| 745 | return *this; |
---|
| 746 | } |
---|
| 747 | |
---|
[156] | 748 | /// \brief Use previously constructed node set |
---|
| 749 | /// |
---|
| 750 | /// Use previously constructed node set, and specify the node |
---|
| 751 | /// label map and a functor which converts the label map values to |
---|
[192] | 752 | /// \c std::string. |
---|
[127] | 753 | template <typename Map, typename Converter> |
---|
[209] | 754 | DigraphReader& useNodes(const Map& map, |
---|
| 755 | const Converter& converter = Converter()) { |
---|
[127] | 756 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 757 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[127] | 758 | _use_nodes = true; |
---|
| 759 | for (NodeIt n(_digraph); n != INVALID; ++n) { |
---|
[209] | 760 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[127] | 761 | } |
---|
| 762 | return *this; |
---|
| 763 | } |
---|
| 764 | |
---|
[156] | 765 | /// \brief Use previously constructed arc set |
---|
| 766 | /// |
---|
| 767 | /// Use previously constructed arc set, and specify the arc |
---|
| 768 | /// label map. |
---|
[127] | 769 | template <typename Map> |
---|
| 770 | DigraphReader& useArcs(const Map& map) { |
---|
| 771 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
| 772 | LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
---|
| 773 | _use_arcs = true; |
---|
| 774 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 775 | for (ArcIt a(_digraph); a != INVALID; ++a) { |
---|
[209] | 776 | _arc_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[127] | 777 | } |
---|
| 778 | return *this; |
---|
| 779 | } |
---|
| 780 | |
---|
[156] | 781 | /// \brief Use previously constructed arc set |
---|
| 782 | /// |
---|
| 783 | /// Use previously constructed arc set, and specify the arc |
---|
| 784 | /// label map and a functor which converts the label map values to |
---|
[192] | 785 | /// \c std::string. |
---|
[127] | 786 | template <typename Map, typename Converter> |
---|
[209] | 787 | DigraphReader& useArcs(const Map& map, |
---|
| 788 | const Converter& converter = Converter()) { |
---|
[127] | 789 | checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 790 | LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
---|
[127] | 791 | _use_arcs = true; |
---|
| 792 | for (ArcIt a(_digraph); a != INVALID; ++a) { |
---|
[209] | 793 | _arc_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[127] | 794 | } |
---|
| 795 | return *this; |
---|
| 796 | } |
---|
| 797 | |
---|
[188] | 798 | /// \brief Skips the reading of node section |
---|
| 799 | /// |
---|
| 800 | /// Omit the reading of the node section. This implies that each node |
---|
[192] | 801 | /// map reading rule will be abandoned, and the nodes of the graph |
---|
[188] | 802 | /// will not be constructed, which usually cause that the arc set |
---|
[192] | 803 | /// could not be read due to lack of node name resolving. |
---|
| 804 | /// Therefore \c skipArcs() function should also be used, or |
---|
| 805 | /// \c useNodes() should be used to specify the label of the nodes. |
---|
[188] | 806 | DigraphReader& skipNodes() { |
---|
[209] | 807 | LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
---|
[188] | 808 | _skip_nodes = true; |
---|
| 809 | return *this; |
---|
| 810 | } |
---|
| 811 | |
---|
| 812 | /// \brief Skips the reading of arc section |
---|
| 813 | /// |
---|
| 814 | /// Omit the reading of the arc section. This implies that each arc |
---|
[192] | 815 | /// map reading rule will be abandoned, and the arcs of the graph |
---|
[188] | 816 | /// will not be constructed. |
---|
| 817 | DigraphReader& skipArcs() { |
---|
[209] | 818 | LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); |
---|
[188] | 819 | _skip_arcs = true; |
---|
| 820 | return *this; |
---|
| 821 | } |
---|
| 822 | |
---|
[156] | 823 | /// @} |
---|
| 824 | |
---|
[127] | 825 | private: |
---|
| 826 | |
---|
| 827 | bool readLine() { |
---|
| 828 | std::string str; |
---|
| 829 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 830 | line.clear(); line.str(str); |
---|
| 831 | char c; |
---|
| 832 | if (line >> std::ws >> c && c != '#') { |
---|
| 833 | line.putback(c); |
---|
| 834 | return true; |
---|
| 835 | } |
---|
[127] | 836 | } |
---|
| 837 | return false; |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | bool readSuccess() { |
---|
| 841 | return static_cast<bool>(*_is); |
---|
| 842 | } |
---|
[209] | 843 | |
---|
[127] | 844 | void skipSection() { |
---|
| 845 | char c; |
---|
| 846 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 847 | readLine(); |
---|
[127] | 848 | } |
---|
[446] | 849 | if (readSuccess()) { |
---|
| 850 | line.putback(c); |
---|
| 851 | } |
---|
[127] | 852 | } |
---|
| 853 | |
---|
| 854 | void readNodes() { |
---|
| 855 | |
---|
| 856 | std::vector<int> map_index(_node_maps.size()); |
---|
| 857 | int map_num, label_index; |
---|
| 858 | |
---|
[186] | 859 | char c; |
---|
| 860 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 861 | if (readSuccess() && line) line.putback(c); |
---|
| 862 | if (!_node_maps.empty()) |
---|
[290] | 863 | throw FormatError("Cannot find map names"); |
---|
[209] | 864 | return; |
---|
[186] | 865 | } |
---|
| 866 | line.putback(c); |
---|
| 867 | |
---|
[127] | 868 | { |
---|
[209] | 869 | std::map<std::string, int> maps; |
---|
| 870 | |
---|
| 871 | std::string map; |
---|
| 872 | int index = 0; |
---|
| 873 | while (_reader_bits::readToken(line, map)) { |
---|
| 874 | if (maps.find(map) != maps.end()) { |
---|
| 875 | std::ostringstream msg; |
---|
| 876 | msg << "Multiple occurence of node map: " << map; |
---|
[290] | 877 | throw FormatError(msg.str()); |
---|
[209] | 878 | } |
---|
| 879 | maps.insert(std::make_pair(map, index)); |
---|
| 880 | ++index; |
---|
| 881 | } |
---|
| 882 | |
---|
| 883 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 884 | std::map<std::string, int>::iterator jt = |
---|
| 885 | maps.find(_node_maps[i].first); |
---|
| 886 | if (jt == maps.end()) { |
---|
| 887 | std::ostringstream msg; |
---|
[291] | 888 | msg << "Map not found: " << _node_maps[i].first; |
---|
[290] | 889 | throw FormatError(msg.str()); |
---|
[209] | 890 | } |
---|
| 891 | map_index[i] = jt->second; |
---|
| 892 | } |
---|
| 893 | |
---|
| 894 | { |
---|
| 895 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 896 | if (jt != maps.end()) { |
---|
| 897 | label_index = jt->second; |
---|
| 898 | } else { |
---|
| 899 | label_index = -1; |
---|
| 900 | } |
---|
| 901 | } |
---|
| 902 | map_num = maps.size(); |
---|
[127] | 903 | } |
---|
| 904 | |
---|
| 905 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 906 | line.putback(c); |
---|
| 907 | |
---|
| 908 | std::vector<std::string> tokens(map_num); |
---|
| 909 | for (int i = 0; i < map_num; ++i) { |
---|
| 910 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 911 | std::ostringstream msg; |
---|
| 912 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 913 | throw FormatError(msg.str()); |
---|
[209] | 914 | } |
---|
| 915 | } |
---|
| 916 | if (line >> std::ws >> c) |
---|
[291] | 917 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 918 | |
---|
| 919 | Node n; |
---|
| 920 | if (!_use_nodes) { |
---|
| 921 | n = _digraph.addNode(); |
---|
| 922 | if (label_index != -1) |
---|
| 923 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
---|
| 924 | } else { |
---|
| 925 | if (label_index == -1) |
---|
[291] | 926 | throw FormatError("Label map not found"); |
---|
[209] | 927 | typename std::map<std::string, Node>::iterator it = |
---|
| 928 | _node_index.find(tokens[label_index]); |
---|
| 929 | if (it == _node_index.end()) { |
---|
| 930 | std::ostringstream msg; |
---|
| 931 | msg << "Node with label not found: " << tokens[label_index]; |
---|
[290] | 932 | throw FormatError(msg.str()); |
---|
[209] | 933 | } |
---|
| 934 | n = it->second; |
---|
| 935 | } |
---|
| 936 | |
---|
| 937 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 938 | _node_maps[i].second->set(n, tokens[map_index[i]]); |
---|
| 939 | } |
---|
[127] | 940 | |
---|
| 941 | } |
---|
| 942 | if (readSuccess()) { |
---|
[209] | 943 | line.putback(c); |
---|
[127] | 944 | } |
---|
| 945 | } |
---|
| 946 | |
---|
| 947 | void readArcs() { |
---|
| 948 | |
---|
| 949 | std::vector<int> map_index(_arc_maps.size()); |
---|
| 950 | int map_num, label_index; |
---|
| 951 | |
---|
[186] | 952 | char c; |
---|
| 953 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 954 | if (readSuccess() && line) line.putback(c); |
---|
| 955 | if (!_arc_maps.empty()) |
---|
[290] | 956 | throw FormatError("Cannot find map names"); |
---|
[209] | 957 | return; |
---|
[186] | 958 | } |
---|
| 959 | line.putback(c); |
---|
[209] | 960 | |
---|
[127] | 961 | { |
---|
[209] | 962 | std::map<std::string, int> maps; |
---|
| 963 | |
---|
| 964 | std::string map; |
---|
| 965 | int index = 0; |
---|
| 966 | while (_reader_bits::readToken(line, map)) { |
---|
[1067] | 967 | if(map == "-") { |
---|
| 968 | if(index!=0) |
---|
| 969 | throw FormatError("'-' is not allowed as a map name"); |
---|
| 970 | else if (line >> std::ws >> c) |
---|
| 971 | throw FormatError("Extra character at the end of line"); |
---|
| 972 | else break; |
---|
| 973 | } |
---|
[209] | 974 | if (maps.find(map) != maps.end()) { |
---|
| 975 | std::ostringstream msg; |
---|
| 976 | msg << "Multiple occurence of arc map: " << map; |
---|
[290] | 977 | throw FormatError(msg.str()); |
---|
[209] | 978 | } |
---|
| 979 | maps.insert(std::make_pair(map, index)); |
---|
| 980 | ++index; |
---|
| 981 | } |
---|
| 982 | |
---|
| 983 | for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
---|
| 984 | std::map<std::string, int>::iterator jt = |
---|
| 985 | maps.find(_arc_maps[i].first); |
---|
| 986 | if (jt == maps.end()) { |
---|
| 987 | std::ostringstream msg; |
---|
[291] | 988 | msg << "Map not found: " << _arc_maps[i].first; |
---|
[290] | 989 | throw FormatError(msg.str()); |
---|
[209] | 990 | } |
---|
| 991 | map_index[i] = jt->second; |
---|
| 992 | } |
---|
| 993 | |
---|
| 994 | { |
---|
| 995 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 996 | if (jt != maps.end()) { |
---|
| 997 | label_index = jt->second; |
---|
| 998 | } else { |
---|
| 999 | label_index = -1; |
---|
| 1000 | } |
---|
| 1001 | } |
---|
| 1002 | map_num = maps.size(); |
---|
[127] | 1003 | } |
---|
| 1004 | |
---|
| 1005 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1006 | line.putback(c); |
---|
| 1007 | |
---|
| 1008 | std::string source_token; |
---|
| 1009 | std::string target_token; |
---|
| 1010 | |
---|
| 1011 | if (!_reader_bits::readToken(line, source_token)) |
---|
[290] | 1012 | throw FormatError("Source not found"); |
---|
[209] | 1013 | |
---|
| 1014 | if (!_reader_bits::readToken(line, target_token)) |
---|
[290] | 1015 | throw FormatError("Target not found"); |
---|
[209] | 1016 | |
---|
| 1017 | std::vector<std::string> tokens(map_num); |
---|
| 1018 | for (int i = 0; i < map_num; ++i) { |
---|
| 1019 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 1020 | std::ostringstream msg; |
---|
| 1021 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 1022 | throw FormatError(msg.str()); |
---|
[209] | 1023 | } |
---|
| 1024 | } |
---|
| 1025 | if (line >> std::ws >> c) |
---|
[291] | 1026 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 1027 | |
---|
| 1028 | Arc a; |
---|
| 1029 | if (!_use_arcs) { |
---|
[127] | 1030 | |
---|
| 1031 | typename NodeIndex::iterator it; |
---|
[209] | 1032 | |
---|
[127] | 1033 | it = _node_index.find(source_token); |
---|
| 1034 | if (it == _node_index.end()) { |
---|
| 1035 | std::ostringstream msg; |
---|
| 1036 | msg << "Item not found: " << source_token; |
---|
[290] | 1037 | throw FormatError(msg.str()); |
---|
[127] | 1038 | } |
---|
| 1039 | Node source = it->second; |
---|
| 1040 | |
---|
| 1041 | it = _node_index.find(target_token); |
---|
[209] | 1042 | if (it == _node_index.end()) { |
---|
| 1043 | std::ostringstream msg; |
---|
[127] | 1044 | msg << "Item not found: " << target_token; |
---|
[290] | 1045 | throw FormatError(msg.str()); |
---|
[209] | 1046 | } |
---|
| 1047 | Node target = it->second; |
---|
| 1048 | |
---|
| 1049 | a = _digraph.addArc(source, target); |
---|
| 1050 | if (label_index != -1) |
---|
| 1051 | _arc_index.insert(std::make_pair(tokens[label_index], a)); |
---|
| 1052 | } else { |
---|
| 1053 | if (label_index == -1) |
---|
[291] | 1054 | throw FormatError("Label map not found"); |
---|
[209] | 1055 | typename std::map<std::string, Arc>::iterator it = |
---|
| 1056 | _arc_index.find(tokens[label_index]); |
---|
| 1057 | if (it == _arc_index.end()) { |
---|
| 1058 | std::ostringstream msg; |
---|
| 1059 | msg << "Arc with label not found: " << tokens[label_index]; |
---|
[290] | 1060 | throw FormatError(msg.str()); |
---|
[209] | 1061 | } |
---|
| 1062 | a = it->second; |
---|
| 1063 | } |
---|
| 1064 | |
---|
| 1065 | for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) { |
---|
| 1066 | _arc_maps[i].second->set(a, tokens[map_index[i]]); |
---|
| 1067 | } |
---|
[127] | 1068 | |
---|
| 1069 | } |
---|
| 1070 | if (readSuccess()) { |
---|
[209] | 1071 | line.putback(c); |
---|
[127] | 1072 | } |
---|
| 1073 | } |
---|
| 1074 | |
---|
| 1075 | void readAttributes() { |
---|
| 1076 | |
---|
| 1077 | std::set<std::string> read_attr; |
---|
| 1078 | |
---|
| 1079 | char c; |
---|
| 1080 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1081 | line.putback(c); |
---|
| 1082 | |
---|
| 1083 | std::string attr, token; |
---|
| 1084 | if (!_reader_bits::readToken(line, attr)) |
---|
[290] | 1085 | throw FormatError("Attribute name not found"); |
---|
[209] | 1086 | if (!_reader_bits::readToken(line, token)) |
---|
[290] | 1087 | throw FormatError("Attribute value not found"); |
---|
[209] | 1088 | if (line >> c) |
---|
[291] | 1089 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 1090 | |
---|
| 1091 | { |
---|
| 1092 | std::set<std::string>::iterator it = read_attr.find(attr); |
---|
| 1093 | if (it != read_attr.end()) { |
---|
| 1094 | std::ostringstream msg; |
---|
[291] | 1095 | msg << "Multiple occurence of attribute: " << attr; |
---|
[290] | 1096 | throw FormatError(msg.str()); |
---|
[209] | 1097 | } |
---|
| 1098 | read_attr.insert(attr); |
---|
| 1099 | } |
---|
| 1100 | |
---|
| 1101 | { |
---|
| 1102 | typename Attributes::iterator it = _attributes.lower_bound(attr); |
---|
| 1103 | while (it != _attributes.end() && it->first == attr) { |
---|
| 1104 | it->second->set(token); |
---|
| 1105 | ++it; |
---|
| 1106 | } |
---|
| 1107 | } |
---|
[127] | 1108 | |
---|
| 1109 | } |
---|
| 1110 | if (readSuccess()) { |
---|
[209] | 1111 | line.putback(c); |
---|
[127] | 1112 | } |
---|
| 1113 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
[209] | 1114 | it != _attributes.end(); ++it) { |
---|
| 1115 | if (read_attr.find(it->first) == read_attr.end()) { |
---|
| 1116 | std::ostringstream msg; |
---|
[291] | 1117 | msg << "Attribute not found: " << it->first; |
---|
[290] | 1118 | throw FormatError(msg.str()); |
---|
[209] | 1119 | } |
---|
[127] | 1120 | } |
---|
| 1121 | } |
---|
| 1122 | |
---|
| 1123 | public: |
---|
[156] | 1124 | |
---|
[631] | 1125 | /// \name Execution of the Reader |
---|
[156] | 1126 | /// @{ |
---|
| 1127 | |
---|
| 1128 | /// \brief Start the batch processing |
---|
| 1129 | /// |
---|
| 1130 | /// This function starts the batch processing |
---|
[127] | 1131 | void run() { |
---|
| 1132 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
---|
[209] | 1133 | |
---|
[188] | 1134 | bool nodes_done = _skip_nodes; |
---|
| 1135 | bool arcs_done = _skip_arcs; |
---|
[127] | 1136 | bool attributes_done = false; |
---|
| 1137 | |
---|
[209] | 1138 | line_num = 0; |
---|
[127] | 1139 | readLine(); |
---|
[172] | 1140 | skipSection(); |
---|
[127] | 1141 | |
---|
| 1142 | while (readSuccess()) { |
---|
[209] | 1143 | try { |
---|
| 1144 | char c; |
---|
| 1145 | std::string section, caption; |
---|
| 1146 | line >> c; |
---|
| 1147 | _reader_bits::readToken(line, section); |
---|
| 1148 | _reader_bits::readToken(line, caption); |
---|
| 1149 | |
---|
| 1150 | if (line >> c) |
---|
[291] | 1151 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 1152 | |
---|
| 1153 | if (section == "nodes" && !nodes_done) { |
---|
| 1154 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
---|
| 1155 | readNodes(); |
---|
| 1156 | nodes_done = true; |
---|
| 1157 | } |
---|
| 1158 | } else if ((section == "arcs" || section == "edges") && |
---|
| 1159 | !arcs_done) { |
---|
| 1160 | if (_arcs_caption.empty() || _arcs_caption == caption) { |
---|
| 1161 | readArcs(); |
---|
| 1162 | arcs_done = true; |
---|
| 1163 | } |
---|
| 1164 | } else if (section == "attributes" && !attributes_done) { |
---|
| 1165 | if (_attributes_caption.empty() || _attributes_caption == caption) { |
---|
| 1166 | readAttributes(); |
---|
| 1167 | attributes_done = true; |
---|
| 1168 | } |
---|
| 1169 | } else { |
---|
| 1170 | readLine(); |
---|
| 1171 | skipSection(); |
---|
| 1172 | } |
---|
[290] | 1173 | } catch (FormatError& error) { |
---|
[209] | 1174 | error.line(line_num); |
---|
[290] | 1175 | error.file(_filename); |
---|
[209] | 1176 | throw; |
---|
| 1177 | } |
---|
[127] | 1178 | } |
---|
| 1179 | |
---|
| 1180 | if (!nodes_done) { |
---|
[290] | 1181 | throw FormatError("Section @nodes not found"); |
---|
[127] | 1182 | } |
---|
| 1183 | |
---|
| 1184 | if (!arcs_done) { |
---|
[290] | 1185 | throw FormatError("Section @arcs not found"); |
---|
[127] | 1186 | } |
---|
| 1187 | |
---|
| 1188 | if (!attributes_done && !_attributes.empty()) { |
---|
[290] | 1189 | throw FormatError("Section @attributes not found"); |
---|
[127] | 1190 | } |
---|
| 1191 | |
---|
| 1192 | } |
---|
[156] | 1193 | |
---|
| 1194 | /// @} |
---|
[209] | 1195 | |
---|
[127] | 1196 | }; |
---|
[956] | 1197 | |
---|
[645] | 1198 | /// \ingroup lemon_io |
---|
| 1199 | /// |
---|
| 1200 | /// \brief Return a \ref DigraphReader class |
---|
| 1201 | /// |
---|
| 1202 | /// This function just returns a \ref DigraphReader class. |
---|
| 1203 | /// |
---|
[956] | 1204 | /// With this function a digraph can be read from an |
---|
[645] | 1205 | /// \ref lgf-format "LGF" file or input stream with several maps and |
---|
| 1206 | /// attributes. For example, there is network flow problem on a |
---|
| 1207 | /// digraph, i.e. a digraph with a \e capacity map on the arcs and |
---|
| 1208 | /// \e source and \e target nodes. This digraph can be read with the |
---|
| 1209 | /// following code: |
---|
| 1210 | /// |
---|
| 1211 | ///\code |
---|
| 1212 | ///ListDigraph digraph; |
---|
| 1213 | ///ListDigraph::ArcMap<int> cm(digraph); |
---|
| 1214 | ///ListDigraph::Node src, trg; |
---|
| 1215 | ///digraphReader(digraph, std::cin). |
---|
| 1216 | /// arcMap("capacity", cap). |
---|
| 1217 | /// node("source", src). |
---|
| 1218 | /// node("target", trg). |
---|
| 1219 | /// run(); |
---|
| 1220 | ///\endcode |
---|
| 1221 | /// |
---|
| 1222 | /// For a complete documentation, please see the \ref DigraphReader |
---|
| 1223 | /// class documentation. |
---|
| 1224 | /// \warning Don't forget to put the \ref DigraphReader::run() "run()" |
---|
| 1225 | /// to the end of the parameter list. |
---|
| 1226 | /// \relates DigraphReader |
---|
| 1227 | /// \sa digraphReader(TDGR& digraph, const std::string& fn) |
---|
| 1228 | /// \sa digraphReader(TDGR& digraph, const char* fn) |
---|
| 1229 | template <typename TDGR> |
---|
| 1230 | DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is) { |
---|
| 1231 | DigraphReader<TDGR> tmp(digraph, is); |
---|
| 1232 | return tmp; |
---|
| 1233 | } |
---|
[127] | 1234 | |
---|
[517] | 1235 | /// \brief Return a \ref DigraphReader class |
---|
| 1236 | /// |
---|
| 1237 | /// This function just returns a \ref DigraphReader class. |
---|
| 1238 | /// \relates DigraphReader |
---|
[645] | 1239 | /// \sa digraphReader(TDGR& digraph, std::istream& is) |
---|
| 1240 | template <typename TDGR> |
---|
| 1241 | DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn) { |
---|
| 1242 | DigraphReader<TDGR> tmp(digraph, fn); |
---|
[517] | 1243 | return tmp; |
---|
| 1244 | } |
---|
| 1245 | |
---|
| 1246 | /// \brief Return a \ref DigraphReader class |
---|
| 1247 | /// |
---|
| 1248 | /// This function just returns a \ref DigraphReader class. |
---|
| 1249 | /// \relates DigraphReader |
---|
[645] | 1250 | /// \sa digraphReader(TDGR& digraph, std::istream& is) |
---|
| 1251 | template <typename TDGR> |
---|
| 1252 | DigraphReader<TDGR> digraphReader(TDGR& digraph, const char* fn) { |
---|
| 1253 | DigraphReader<TDGR> tmp(digraph, fn); |
---|
[517] | 1254 | return tmp; |
---|
| 1255 | } |
---|
| 1256 | |
---|
[645] | 1257 | template <typename GR> |
---|
[303] | 1258 | class GraphReader; |
---|
[956] | 1259 | |
---|
[645] | 1260 | template <typename TGR> |
---|
| 1261 | GraphReader<TGR> graphReader(TGR& graph, std::istream& is = std::cin); |
---|
| 1262 | template <typename TGR> |
---|
| 1263 | GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); |
---|
| 1264 | template <typename TGR> |
---|
| 1265 | GraphReader<TGR> graphReader(TGR& graph, const char *fn); |
---|
[165] | 1266 | |
---|
| 1267 | /// \ingroup lemon_io |
---|
[209] | 1268 | /// |
---|
[192] | 1269 | /// \brief \ref lgf-format "LGF" reader for undirected graphs |
---|
[165] | 1270 | /// |
---|
| 1271 | /// This utility reads an \ref lgf-format "LGF" file. |
---|
[192] | 1272 | /// |
---|
| 1273 | /// It can be used almost the same way as \c DigraphReader. |
---|
| 1274 | /// The only difference is that this class can handle edges and |
---|
| 1275 | /// edge maps as well as arcs and arc maps. |
---|
[201] | 1276 | /// |
---|
| 1277 | /// The columns in the \c \@edges (or \c \@arcs) section are the |
---|
| 1278 | /// edge maps. However, if there are two maps with the same name |
---|
| 1279 | /// prefixed with \c '+' and \c '-', then these can be read into an |
---|
| 1280 | /// arc map. Similarly, an attribute can be read into an arc, if |
---|
| 1281 | /// it's value is an edge label prefixed with \c '+' or \c '-'. |
---|
[606] | 1282 | template <typename GR> |
---|
[165] | 1283 | class GraphReader { |
---|
| 1284 | public: |
---|
| 1285 | |
---|
[606] | 1286 | typedef GR Graph; |
---|
| 1287 | |
---|
| 1288 | private: |
---|
| 1289 | |
---|
[645] | 1290 | TEMPLATE_GRAPH_TYPEDEFS(GR); |
---|
[209] | 1291 | |
---|
[165] | 1292 | std::istream* _is; |
---|
| 1293 | bool local_is; |
---|
[290] | 1294 | std::string _filename; |
---|
[165] | 1295 | |
---|
[645] | 1296 | GR& _graph; |
---|
[165] | 1297 | |
---|
| 1298 | std::string _nodes_caption; |
---|
| 1299 | std::string _edges_caption; |
---|
| 1300 | std::string _attributes_caption; |
---|
| 1301 | |
---|
| 1302 | typedef std::map<std::string, Node> NodeIndex; |
---|
| 1303 | NodeIndex _node_index; |
---|
| 1304 | typedef std::map<std::string, Edge> EdgeIndex; |
---|
| 1305 | EdgeIndex _edge_index; |
---|
[209] | 1306 | |
---|
| 1307 | typedef std::vector<std::pair<std::string, |
---|
| 1308 | _reader_bits::MapStorageBase<Node>*> > NodeMaps; |
---|
| 1309 | NodeMaps _node_maps; |
---|
[165] | 1310 | |
---|
| 1311 | typedef std::vector<std::pair<std::string, |
---|
| 1312 | _reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
---|
| 1313 | EdgeMaps _edge_maps; |
---|
| 1314 | |
---|
[209] | 1315 | typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
---|
[165] | 1316 | Attributes; |
---|
| 1317 | Attributes _attributes; |
---|
| 1318 | |
---|
| 1319 | bool _use_nodes; |
---|
| 1320 | bool _use_edges; |
---|
| 1321 | |
---|
[188] | 1322 | bool _skip_nodes; |
---|
| 1323 | bool _skip_edges; |
---|
| 1324 | |
---|
[165] | 1325 | int line_num; |
---|
| 1326 | std::istringstream line; |
---|
| 1327 | |
---|
| 1328 | public: |
---|
| 1329 | |
---|
| 1330 | /// \brief Constructor |
---|
| 1331 | /// |
---|
[192] | 1332 | /// Construct an undirected graph reader, which reads from the given |
---|
[165] | 1333 | /// input stream. |
---|
[645] | 1334 | GraphReader(GR& graph, std::istream& is = std::cin) |
---|
[165] | 1335 | : _is(&is), local_is(false), _graph(graph), |
---|
[209] | 1336 | _use_nodes(false), _use_edges(false), |
---|
| 1337 | _skip_nodes(false), _skip_edges(false) {} |
---|
[165] | 1338 | |
---|
| 1339 | /// \brief Constructor |
---|
| 1340 | /// |
---|
[192] | 1341 | /// Construct an undirected graph reader, which reads from the given |
---|
[165] | 1342 | /// file. |
---|
[645] | 1343 | GraphReader(GR& graph, const std::string& fn) |
---|
[290] | 1344 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
---|
| 1345 | _filename(fn), _graph(graph), |
---|
[212] | 1346 | _use_nodes(false), _use_edges(false), |
---|
[290] | 1347 | _skip_nodes(false), _skip_edges(false) { |
---|
[295] | 1348 | if (!(*_is)) { |
---|
| 1349 | delete _is; |
---|
| 1350 | throw IoError("Cannot open file", fn); |
---|
| 1351 | } |
---|
[290] | 1352 | } |
---|
[209] | 1353 | |
---|
[165] | 1354 | /// \brief Constructor |
---|
| 1355 | /// |
---|
[192] | 1356 | /// Construct an undirected graph reader, which reads from the given |
---|
[165] | 1357 | /// file. |
---|
[645] | 1358 | GraphReader(GR& graph, const char* fn) |
---|
[290] | 1359 | : _is(new std::ifstream(fn)), local_is(true), |
---|
| 1360 | _filename(fn), _graph(graph), |
---|
[212] | 1361 | _use_nodes(false), _use_edges(false), |
---|
[290] | 1362 | _skip_nodes(false), _skip_edges(false) { |
---|
[295] | 1363 | if (!(*_is)) { |
---|
| 1364 | delete _is; |
---|
| 1365 | throw IoError("Cannot open file", fn); |
---|
| 1366 | } |
---|
[290] | 1367 | } |
---|
[165] | 1368 | |
---|
| 1369 | /// \brief Destructor |
---|
| 1370 | ~GraphReader() { |
---|
[209] | 1371 | for (typename NodeMaps::iterator it = _node_maps.begin(); |
---|
| 1372 | it != _node_maps.end(); ++it) { |
---|
| 1373 | delete it->second; |
---|
[165] | 1374 | } |
---|
| 1375 | |
---|
[209] | 1376 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
| 1377 | it != _edge_maps.end(); ++it) { |
---|
| 1378 | delete it->second; |
---|
[165] | 1379 | } |
---|
| 1380 | |
---|
[209] | 1381 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 1382 | it != _attributes.end(); ++it) { |
---|
| 1383 | delete it->second; |
---|
[165] | 1384 | } |
---|
| 1385 | |
---|
| 1386 | if (local_is) { |
---|
[209] | 1387 | delete _is; |
---|
[165] | 1388 | } |
---|
| 1389 | |
---|
| 1390 | } |
---|
| 1391 | |
---|
| 1392 | private: |
---|
[645] | 1393 | template <typename TGR> |
---|
| 1394 | friend GraphReader<TGR> graphReader(TGR& graph, std::istream& is); |
---|
| 1395 | template <typename TGR> |
---|
[956] | 1396 | friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); |
---|
[645] | 1397 | template <typename TGR> |
---|
| 1398 | friend GraphReader<TGR> graphReader(TGR& graph, const char *fn); |
---|
[209] | 1399 | |
---|
| 1400 | GraphReader(GraphReader& other) |
---|
[190] | 1401 | : _is(other._is), local_is(other.local_is), _graph(other._graph), |
---|
[209] | 1402 | _use_nodes(other._use_nodes), _use_edges(other._use_edges), |
---|
| 1403 | _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
---|
[190] | 1404 | |
---|
| 1405 | other._is = 0; |
---|
| 1406 | other.local_is = false; |
---|
[209] | 1407 | |
---|
[190] | 1408 | _node_index.swap(other._node_index); |
---|
| 1409 | _edge_index.swap(other._edge_index); |
---|
| 1410 | |
---|
| 1411 | _node_maps.swap(other._node_maps); |
---|
| 1412 | _edge_maps.swap(other._edge_maps); |
---|
| 1413 | _attributes.swap(other._attributes); |
---|
| 1414 | |
---|
| 1415 | _nodes_caption = other._nodes_caption; |
---|
| 1416 | _edges_caption = other._edges_caption; |
---|
| 1417 | _attributes_caption = other._attributes_caption; |
---|
| 1418 | |
---|
| 1419 | } |
---|
| 1420 | |
---|
[165] | 1421 | GraphReader& operator=(const GraphReader&); |
---|
| 1422 | |
---|
| 1423 | public: |
---|
| 1424 | |
---|
[631] | 1425 | /// \name Reading Rules |
---|
[165] | 1426 | /// @{ |
---|
[209] | 1427 | |
---|
[165] | 1428 | /// \brief Node map reading rule |
---|
| 1429 | /// |
---|
| 1430 | /// Add a node map reading rule to the reader. |
---|
| 1431 | template <typename Map> |
---|
| 1432 | GraphReader& nodeMap(const std::string& caption, Map& map) { |
---|
| 1433 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1434 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 1435 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
[165] | 1436 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1437 | return *this; |
---|
| 1438 | } |
---|
| 1439 | |
---|
| 1440 | /// \brief Node map reading rule |
---|
| 1441 | /// |
---|
| 1442 | /// Add a node map reading rule with specialized converter to the |
---|
| 1443 | /// reader. |
---|
| 1444 | template <typename Map, typename Converter> |
---|
[209] | 1445 | GraphReader& nodeMap(const std::string& caption, Map& map, |
---|
| 1446 | const Converter& converter = Converter()) { |
---|
[165] | 1447 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1448 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 1449 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
[165] | 1450 | _node_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1451 | return *this; |
---|
| 1452 | } |
---|
| 1453 | |
---|
| 1454 | /// \brief Edge map reading rule |
---|
| 1455 | /// |
---|
| 1456 | /// Add an edge map reading rule to the reader. |
---|
| 1457 | template <typename Map> |
---|
| 1458 | GraphReader& edgeMap(const std::string& caption, Map& map) { |
---|
| 1459 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1460 | _reader_bits::MapStorageBase<Edge>* storage = |
---|
| 1461 | new _reader_bits::MapStorage<Edge, Map>(map); |
---|
[165] | 1462 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1463 | return *this; |
---|
| 1464 | } |
---|
| 1465 | |
---|
| 1466 | /// \brief Edge map reading rule |
---|
| 1467 | /// |
---|
| 1468 | /// Add an edge map reading rule with specialized converter to the |
---|
| 1469 | /// reader. |
---|
| 1470 | template <typename Map, typename Converter> |
---|
[209] | 1471 | GraphReader& edgeMap(const std::string& caption, Map& map, |
---|
| 1472 | const Converter& converter = Converter()) { |
---|
[165] | 1473 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1474 | _reader_bits::MapStorageBase<Edge>* storage = |
---|
| 1475 | new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
---|
[165] | 1476 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 1477 | return *this; |
---|
| 1478 | } |
---|
| 1479 | |
---|
| 1480 | /// \brief Arc map reading rule |
---|
| 1481 | /// |
---|
| 1482 | /// Add an arc map reading rule to the reader. |
---|
| 1483 | template <typename Map> |
---|
| 1484 | GraphReader& arcMap(const std::string& caption, Map& map) { |
---|
| 1485 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 1486 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
---|
| 1487 | new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
---|
[165] | 1488 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
[209] | 1489 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
---|
[645] | 1490 | new _reader_bits::GraphArcMapStorage<GR, false, Map>(_graph, map); |
---|
[165] | 1491 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 1492 | return *this; |
---|
| 1493 | } |
---|
| 1494 | |
---|
| 1495 | /// \brief Arc map reading rule |
---|
| 1496 | /// |
---|
| 1497 | /// Add an arc map reading rule with specialized converter to the |
---|
| 1498 | /// reader. |
---|
| 1499 | template <typename Map, typename Converter> |
---|
[209] | 1500 | GraphReader& arcMap(const std::string& caption, Map& map, |
---|
| 1501 | const Converter& converter = Converter()) { |
---|
[165] | 1502 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
[209] | 1503 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
---|
[645] | 1504 | new _reader_bits::GraphArcMapStorage<GR, true, Map, Converter> |
---|
[209] | 1505 | (_graph, map, converter); |
---|
[165] | 1506 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
[209] | 1507 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
---|
[645] | 1508 | new _reader_bits::GraphArcMapStorage<GR, false, Map, Converter> |
---|
[209] | 1509 | (_graph, map, converter); |
---|
[165] | 1510 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 1511 | return *this; |
---|
| 1512 | } |
---|
| 1513 | |
---|
| 1514 | /// \brief Attribute reading rule |
---|
| 1515 | /// |
---|
| 1516 | /// Add an attribute reading rule to the reader. |
---|
| 1517 | template <typename Value> |
---|
| 1518 | GraphReader& attribute(const std::string& caption, Value& value) { |
---|
[209] | 1519 | _reader_bits::ValueStorageBase* storage = |
---|
| 1520 | new _reader_bits::ValueStorage<Value>(value); |
---|
[165] | 1521 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1522 | return *this; |
---|
| 1523 | } |
---|
| 1524 | |
---|
| 1525 | /// \brief Attribute reading rule |
---|
| 1526 | /// |
---|
| 1527 | /// Add an attribute reading rule with specialized converter to the |
---|
| 1528 | /// reader. |
---|
| 1529 | template <typename Value, typename Converter> |
---|
[209] | 1530 | GraphReader& attribute(const std::string& caption, Value& value, |
---|
| 1531 | const Converter& converter = Converter()) { |
---|
| 1532 | _reader_bits::ValueStorageBase* storage = |
---|
| 1533 | new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
---|
[165] | 1534 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1535 | return *this; |
---|
| 1536 | } |
---|
| 1537 | |
---|
| 1538 | /// \brief Node reading rule |
---|
| 1539 | /// |
---|
| 1540 | /// Add a node reading rule to reader. |
---|
| 1541 | GraphReader& node(const std::string& caption, Node& node) { |
---|
| 1542 | typedef _reader_bits::MapLookUpConverter<Node> Converter; |
---|
| 1543 | Converter converter(_node_index); |
---|
[209] | 1544 | _reader_bits::ValueStorageBase* storage = |
---|
| 1545 | new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
---|
[165] | 1546 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1547 | return *this; |
---|
| 1548 | } |
---|
| 1549 | |
---|
| 1550 | /// \brief Edge reading rule |
---|
| 1551 | /// |
---|
| 1552 | /// Add an edge reading rule to reader. |
---|
| 1553 | GraphReader& edge(const std::string& caption, Edge& edge) { |
---|
| 1554 | typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
---|
| 1555 | Converter converter(_edge_index); |
---|
[209] | 1556 | _reader_bits::ValueStorageBase* storage = |
---|
| 1557 | new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
---|
[165] | 1558 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1559 | return *this; |
---|
| 1560 | } |
---|
| 1561 | |
---|
| 1562 | /// \brief Arc reading rule |
---|
| 1563 | /// |
---|
| 1564 | /// Add an arc reading rule to reader. |
---|
| 1565 | GraphReader& arc(const std::string& caption, Arc& arc) { |
---|
[645] | 1566 | typedef _reader_bits::GraphArcLookUpConverter<GR> Converter; |
---|
[165] | 1567 | Converter converter(_graph, _edge_index); |
---|
[209] | 1568 | _reader_bits::ValueStorageBase* storage = |
---|
| 1569 | new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
---|
[165] | 1570 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 1571 | return *this; |
---|
| 1572 | } |
---|
| 1573 | |
---|
| 1574 | /// @} |
---|
| 1575 | |
---|
[631] | 1576 | /// \name Select Section by Name |
---|
[165] | 1577 | /// @{ |
---|
| 1578 | |
---|
| 1579 | /// \brief Set \c \@nodes section to be read |
---|
| 1580 | /// |
---|
[192] | 1581 | /// Set \c \@nodes section to be read. |
---|
[165] | 1582 | GraphReader& nodes(const std::string& caption) { |
---|
| 1583 | _nodes_caption = caption; |
---|
| 1584 | return *this; |
---|
| 1585 | } |
---|
| 1586 | |
---|
| 1587 | /// \brief Set \c \@edges section to be read |
---|
| 1588 | /// |
---|
[192] | 1589 | /// Set \c \@edges section to be read. |
---|
[165] | 1590 | GraphReader& edges(const std::string& caption) { |
---|
| 1591 | _edges_caption = caption; |
---|
| 1592 | return *this; |
---|
| 1593 | } |
---|
| 1594 | |
---|
| 1595 | /// \brief Set \c \@attributes section to be read |
---|
| 1596 | /// |
---|
[192] | 1597 | /// Set \c \@attributes section to be read. |
---|
[165] | 1598 | GraphReader& attributes(const std::string& caption) { |
---|
| 1599 | _attributes_caption = caption; |
---|
| 1600 | return *this; |
---|
| 1601 | } |
---|
| 1602 | |
---|
| 1603 | /// @} |
---|
| 1604 | |
---|
[631] | 1605 | /// \name Using Previously Constructed Node or Edge Set |
---|
[165] | 1606 | /// @{ |
---|
| 1607 | |
---|
| 1608 | /// \brief Use previously constructed node set |
---|
| 1609 | /// |
---|
| 1610 | /// Use previously constructed node set, and specify the node |
---|
| 1611 | /// label map. |
---|
| 1612 | template <typename Map> |
---|
| 1613 | GraphReader& useNodes(const Map& map) { |
---|
| 1614 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1615 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[165] | 1616 | _use_nodes = true; |
---|
| 1617 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 1618 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
[209] | 1619 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[165] | 1620 | } |
---|
| 1621 | return *this; |
---|
| 1622 | } |
---|
| 1623 | |
---|
| 1624 | /// \brief Use previously constructed node set |
---|
| 1625 | /// |
---|
| 1626 | /// Use previously constructed node set, and specify the node |
---|
| 1627 | /// label map and a functor which converts the label map values to |
---|
[192] | 1628 | /// \c std::string. |
---|
[165] | 1629 | template <typename Map, typename Converter> |
---|
[209] | 1630 | GraphReader& useNodes(const Map& map, |
---|
| 1631 | const Converter& converter = Converter()) { |
---|
[165] | 1632 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
[209] | 1633 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
[165] | 1634 | _use_nodes = true; |
---|
| 1635 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
[209] | 1636 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
[165] | 1637 | } |
---|
| 1638 | return *this; |
---|
| 1639 | } |
---|
| 1640 | |
---|
| 1641 | /// \brief Use previously constructed edge set |
---|
| 1642 | /// |
---|
| 1643 | /// Use previously constructed edge set, and specify the edge |
---|
| 1644 | /// label map. |
---|
| 1645 | template <typename Map> |
---|
| 1646 | GraphReader& useEdges(const Map& map) { |
---|
| 1647 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
| 1648 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
---|
| 1649 | _use_edges = true; |
---|
| 1650 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 1651 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
---|
[209] | 1652 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[165] | 1653 | } |
---|
| 1654 | return *this; |
---|
| 1655 | } |
---|
| 1656 | |
---|
| 1657 | /// \brief Use previously constructed edge set |
---|
| 1658 | /// |
---|
| 1659 | /// Use previously constructed edge set, and specify the edge |
---|
| 1660 | /// label map and a functor which converts the label map values to |
---|
[192] | 1661 | /// \c std::string. |
---|
[165] | 1662 | template <typename Map, typename Converter> |
---|
[209] | 1663 | GraphReader& useEdges(const Map& map, |
---|
| 1664 | const Converter& converter = Converter()) { |
---|
[165] | 1665 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
[209] | 1666 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
---|
[165] | 1667 | _use_edges = true; |
---|
| 1668 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
---|
[209] | 1669 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
---|
[165] | 1670 | } |
---|
| 1671 | return *this; |
---|
| 1672 | } |
---|
| 1673 | |
---|
[192] | 1674 | /// \brief Skip the reading of node section |
---|
[188] | 1675 | /// |
---|
| 1676 | /// Omit the reading of the node section. This implies that each node |
---|
[192] | 1677 | /// map reading rule will be abandoned, and the nodes of the graph |
---|
[188] | 1678 | /// will not be constructed, which usually cause that the edge set |
---|
| 1679 | /// could not be read due to lack of node name |
---|
[192] | 1680 | /// could not be read due to lack of node name resolving. |
---|
| 1681 | /// Therefore \c skipEdges() function should also be used, or |
---|
| 1682 | /// \c useNodes() should be used to specify the label of the nodes. |
---|
[188] | 1683 | GraphReader& skipNodes() { |
---|
[209] | 1684 | LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
---|
[188] | 1685 | _skip_nodes = true; |
---|
| 1686 | return *this; |
---|
| 1687 | } |
---|
| 1688 | |
---|
[192] | 1689 | /// \brief Skip the reading of edge section |
---|
[188] | 1690 | /// |
---|
| 1691 | /// Omit the reading of the edge section. This implies that each edge |
---|
[192] | 1692 | /// map reading rule will be abandoned, and the edges of the graph |
---|
[188] | 1693 | /// will not be constructed. |
---|
| 1694 | GraphReader& skipEdges() { |
---|
[209] | 1695 | LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
---|
[188] | 1696 | _skip_edges = true; |
---|
| 1697 | return *this; |
---|
| 1698 | } |
---|
| 1699 | |
---|
[165] | 1700 | /// @} |
---|
| 1701 | |
---|
| 1702 | private: |
---|
| 1703 | |
---|
| 1704 | bool readLine() { |
---|
| 1705 | std::string str; |
---|
| 1706 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 1707 | line.clear(); line.str(str); |
---|
| 1708 | char c; |
---|
| 1709 | if (line >> std::ws >> c && c != '#') { |
---|
| 1710 | line.putback(c); |
---|
| 1711 | return true; |
---|
| 1712 | } |
---|
[165] | 1713 | } |
---|
| 1714 | return false; |
---|
| 1715 | } |
---|
| 1716 | |
---|
| 1717 | bool readSuccess() { |
---|
| 1718 | return static_cast<bool>(*_is); |
---|
| 1719 | } |
---|
[209] | 1720 | |
---|
[165] | 1721 | void skipSection() { |
---|
| 1722 | char c; |
---|
| 1723 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 1724 | readLine(); |
---|
[165] | 1725 | } |
---|
[446] | 1726 | if (readSuccess()) { |
---|
| 1727 | line.putback(c); |
---|
| 1728 | } |
---|
[165] | 1729 | } |
---|
| 1730 | |
---|
| 1731 | void readNodes() { |
---|
| 1732 | |
---|
| 1733 | std::vector<int> map_index(_node_maps.size()); |
---|
| 1734 | int map_num, label_index; |
---|
| 1735 | |
---|
[186] | 1736 | char c; |
---|
| 1737 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 1738 | if (readSuccess() && line) line.putback(c); |
---|
| 1739 | if (!_node_maps.empty()) |
---|
[290] | 1740 | throw FormatError("Cannot find map names"); |
---|
[209] | 1741 | return; |
---|
[186] | 1742 | } |
---|
| 1743 | line.putback(c); |
---|
[209] | 1744 | |
---|
[165] | 1745 | { |
---|
[209] | 1746 | std::map<std::string, int> maps; |
---|
| 1747 | |
---|
| 1748 | std::string map; |
---|
| 1749 | int index = 0; |
---|
| 1750 | while (_reader_bits::readToken(line, map)) { |
---|
| 1751 | if (maps.find(map) != maps.end()) { |
---|
| 1752 | std::ostringstream msg; |
---|
| 1753 | msg << "Multiple occurence of node map: " << map; |
---|
[290] | 1754 | throw FormatError(msg.str()); |
---|
[209] | 1755 | } |
---|
| 1756 | maps.insert(std::make_pair(map, index)); |
---|
| 1757 | ++index; |
---|
| 1758 | } |
---|
| 1759 | |
---|
| 1760 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 1761 | std::map<std::string, int>::iterator jt = |
---|
| 1762 | maps.find(_node_maps[i].first); |
---|
| 1763 | if (jt == maps.end()) { |
---|
| 1764 | std::ostringstream msg; |
---|
[291] | 1765 | msg << "Map not found: " << _node_maps[i].first; |
---|
[290] | 1766 | throw FormatError(msg.str()); |
---|
[209] | 1767 | } |
---|
| 1768 | map_index[i] = jt->second; |
---|
| 1769 | } |
---|
| 1770 | |
---|
| 1771 | { |
---|
| 1772 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 1773 | if (jt != maps.end()) { |
---|
| 1774 | label_index = jt->second; |
---|
| 1775 | } else { |
---|
| 1776 | label_index = -1; |
---|
| 1777 | } |
---|
| 1778 | } |
---|
| 1779 | map_num = maps.size(); |
---|
[165] | 1780 | } |
---|
| 1781 | |
---|
| 1782 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1783 | line.putback(c); |
---|
| 1784 | |
---|
| 1785 | std::vector<std::string> tokens(map_num); |
---|
| 1786 | for (int i = 0; i < map_num; ++i) { |
---|
| 1787 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 1788 | std::ostringstream msg; |
---|
| 1789 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 1790 | throw FormatError(msg.str()); |
---|
[209] | 1791 | } |
---|
| 1792 | } |
---|
| 1793 | if (line >> std::ws >> c) |
---|
[291] | 1794 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 1795 | |
---|
| 1796 | Node n; |
---|
| 1797 | if (!_use_nodes) { |
---|
| 1798 | n = _graph.addNode(); |
---|
| 1799 | if (label_index != -1) |
---|
| 1800 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
---|
| 1801 | } else { |
---|
| 1802 | if (label_index == -1) |
---|
[291] | 1803 | throw FormatError("Label map not found"); |
---|
[209] | 1804 | typename std::map<std::string, Node>::iterator it = |
---|
| 1805 | _node_index.find(tokens[label_index]); |
---|
| 1806 | if (it == _node_index.end()) { |
---|
| 1807 | std::ostringstream msg; |
---|
| 1808 | msg << "Node with label not found: " << tokens[label_index]; |
---|
[290] | 1809 | throw FormatError(msg.str()); |
---|
[209] | 1810 | } |
---|
| 1811 | n = it->second; |
---|
| 1812 | } |
---|
| 1813 | |
---|
| 1814 | for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) { |
---|
| 1815 | _node_maps[i].second->set(n, tokens[map_index[i]]); |
---|
| 1816 | } |
---|
[165] | 1817 | |
---|
| 1818 | } |
---|
| 1819 | if (readSuccess()) { |
---|
[209] | 1820 | line.putback(c); |
---|
[165] | 1821 | } |
---|
| 1822 | } |
---|
| 1823 | |
---|
| 1824 | void readEdges() { |
---|
| 1825 | |
---|
| 1826 | std::vector<int> map_index(_edge_maps.size()); |
---|
| 1827 | int map_num, label_index; |
---|
| 1828 | |
---|
[186] | 1829 | char c; |
---|
| 1830 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 1831 | if (readSuccess() && line) line.putback(c); |
---|
| 1832 | if (!_edge_maps.empty()) |
---|
[290] | 1833 | throw FormatError("Cannot find map names"); |
---|
[209] | 1834 | return; |
---|
[186] | 1835 | } |
---|
| 1836 | line.putback(c); |
---|
[209] | 1837 | |
---|
[165] | 1838 | { |
---|
[209] | 1839 | std::map<std::string, int> maps; |
---|
| 1840 | |
---|
| 1841 | std::string map; |
---|
| 1842 | int index = 0; |
---|
| 1843 | while (_reader_bits::readToken(line, map)) { |
---|
[1067] | 1844 | if(map == "-") { |
---|
| 1845 | if(index!=0) |
---|
| 1846 | throw FormatError("'-' is not allowed as a map name"); |
---|
| 1847 | else if (line >> std::ws >> c) |
---|
| 1848 | throw FormatError("Extra character at the end of line"); |
---|
| 1849 | else break; |
---|
| 1850 | } |
---|
[209] | 1851 | if (maps.find(map) != maps.end()) { |
---|
| 1852 | std::ostringstream msg; |
---|
| 1853 | msg << "Multiple occurence of edge map: " << map; |
---|
[290] | 1854 | throw FormatError(msg.str()); |
---|
[209] | 1855 | } |
---|
| 1856 | maps.insert(std::make_pair(map, index)); |
---|
| 1857 | ++index; |
---|
| 1858 | } |
---|
| 1859 | |
---|
| 1860 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
---|
| 1861 | std::map<std::string, int>::iterator jt = |
---|
| 1862 | maps.find(_edge_maps[i].first); |
---|
| 1863 | if (jt == maps.end()) { |
---|
| 1864 | std::ostringstream msg; |
---|
[291] | 1865 | msg << "Map not found: " << _edge_maps[i].first; |
---|
[290] | 1866 | throw FormatError(msg.str()); |
---|
[209] | 1867 | } |
---|
| 1868 | map_index[i] = jt->second; |
---|
| 1869 | } |
---|
| 1870 | |
---|
| 1871 | { |
---|
| 1872 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 1873 | if (jt != maps.end()) { |
---|
| 1874 | label_index = jt->second; |
---|
| 1875 | } else { |
---|
| 1876 | label_index = -1; |
---|
| 1877 | } |
---|
| 1878 | } |
---|
| 1879 | map_num = maps.size(); |
---|
[165] | 1880 | } |
---|
| 1881 | |
---|
| 1882 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1883 | line.putback(c); |
---|
| 1884 | |
---|
| 1885 | std::string source_token; |
---|
| 1886 | std::string target_token; |
---|
| 1887 | |
---|
| 1888 | if (!_reader_bits::readToken(line, source_token)) |
---|
[290] | 1889 | throw FormatError("Node u not found"); |
---|
[209] | 1890 | |
---|
| 1891 | if (!_reader_bits::readToken(line, target_token)) |
---|
[290] | 1892 | throw FormatError("Node v not found"); |
---|
[209] | 1893 | |
---|
| 1894 | std::vector<std::string> tokens(map_num); |
---|
| 1895 | for (int i = 0; i < map_num; ++i) { |
---|
| 1896 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 1897 | std::ostringstream msg; |
---|
| 1898 | msg << "Column not found (" << i + 1 << ")"; |
---|
[290] | 1899 | throw FormatError(msg.str()); |
---|
[209] | 1900 | } |
---|
| 1901 | } |
---|
| 1902 | if (line >> std::ws >> c) |
---|
[291] | 1903 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 1904 | |
---|
| 1905 | Edge e; |
---|
| 1906 | if (!_use_edges) { |
---|
[165] | 1907 | |
---|
| 1908 | typename NodeIndex::iterator it; |
---|
[209] | 1909 | |
---|
[165] | 1910 | it = _node_index.find(source_token); |
---|
| 1911 | if (it == _node_index.end()) { |
---|
| 1912 | std::ostringstream msg; |
---|
| 1913 | msg << "Item not found: " << source_token; |
---|
[290] | 1914 | throw FormatError(msg.str()); |
---|
[165] | 1915 | } |
---|
| 1916 | Node source = it->second; |
---|
| 1917 | |
---|
| 1918 | it = _node_index.find(target_token); |
---|
[209] | 1919 | if (it == _node_index.end()) { |
---|
| 1920 | std::ostringstream msg; |
---|
[165] | 1921 | msg << "Item not found: " << target_token; |
---|
[290] | 1922 | throw FormatError(msg.str()); |
---|
[209] | 1923 | } |
---|
| 1924 | Node target = it->second; |
---|
| 1925 | |
---|
| 1926 | e = _graph.addEdge(source, target); |
---|
| 1927 | if (label_index != -1) |
---|
| 1928 | _edge_index.insert(std::make_pair(tokens[label_index], e)); |
---|
| 1929 | } else { |
---|
| 1930 | if (label_index == -1) |
---|
[291] | 1931 | throw FormatError("Label map not found"); |
---|
[209] | 1932 | typename std::map<std::string, Edge>::iterator it = |
---|
| 1933 | _edge_index.find(tokens[label_index]); |
---|
| 1934 | if (it == _edge_index.end()) { |
---|
| 1935 | std::ostringstream msg; |
---|
| 1936 | msg << "Edge with label not found: " << tokens[label_index]; |
---|
[290] | 1937 | throw FormatError(msg.str()); |
---|
[209] | 1938 | } |
---|
| 1939 | e = it->second; |
---|
| 1940 | } |
---|
| 1941 | |
---|
| 1942 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
---|
| 1943 | _edge_maps[i].second->set(e, tokens[map_index[i]]); |
---|
| 1944 | } |
---|
[165] | 1945 | |
---|
| 1946 | } |
---|
| 1947 | if (readSuccess()) { |
---|
[209] | 1948 | line.putback(c); |
---|
[165] | 1949 | } |
---|
| 1950 | } |
---|
| 1951 | |
---|
| 1952 | void readAttributes() { |
---|
| 1953 | |
---|
| 1954 | std::set<std::string> read_attr; |
---|
| 1955 | |
---|
| 1956 | char c; |
---|
| 1957 | while (readLine() && line >> c && c != '@') { |
---|
[209] | 1958 | line.putback(c); |
---|
| 1959 | |
---|
| 1960 | std::string attr, token; |
---|
| 1961 | if (!_reader_bits::readToken(line, attr)) |
---|
[290] | 1962 | throw FormatError("Attribute name not found"); |
---|
[209] | 1963 | if (!_reader_bits::readToken(line, token)) |
---|
[290] | 1964 | throw FormatError("Attribute value not found"); |
---|
[209] | 1965 | if (line >> c) |
---|
[291] | 1966 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 1967 | |
---|
| 1968 | { |
---|
| 1969 | std::set<std::string>::iterator it = read_attr.find(attr); |
---|
| 1970 | if (it != read_attr.end()) { |
---|
| 1971 | std::ostringstream msg; |
---|
[291] | 1972 | msg << "Multiple occurence of attribute: " << attr; |
---|
[290] | 1973 | throw FormatError(msg.str()); |
---|
[209] | 1974 | } |
---|
| 1975 | read_attr.insert(attr); |
---|
| 1976 | } |
---|
| 1977 | |
---|
| 1978 | { |
---|
| 1979 | typename Attributes::iterator it = _attributes.lower_bound(attr); |
---|
| 1980 | while (it != _attributes.end() && it->first == attr) { |
---|
| 1981 | it->second->set(token); |
---|
| 1982 | ++it; |
---|
| 1983 | } |
---|
| 1984 | } |
---|
[165] | 1985 | |
---|
| 1986 | } |
---|
| 1987 | if (readSuccess()) { |
---|
[209] | 1988 | line.putback(c); |
---|
[165] | 1989 | } |
---|
| 1990 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
[209] | 1991 | it != _attributes.end(); ++it) { |
---|
| 1992 | if (read_attr.find(it->first) == read_attr.end()) { |
---|
| 1993 | std::ostringstream msg; |
---|
[291] | 1994 | msg << "Attribute not found: " << it->first; |
---|
[290] | 1995 | throw FormatError(msg.str()); |
---|
[209] | 1996 | } |
---|
[165] | 1997 | } |
---|
| 1998 | } |
---|
| 1999 | |
---|
| 2000 | public: |
---|
| 2001 | |
---|
[631] | 2002 | /// \name Execution of the Reader |
---|
[165] | 2003 | /// @{ |
---|
| 2004 | |
---|
| 2005 | /// \brief Start the batch processing |
---|
| 2006 | /// |
---|
| 2007 | /// This function starts the batch processing |
---|
| 2008 | void run() { |
---|
[209] | 2009 | |
---|
[165] | 2010 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
---|
[209] | 2011 | |
---|
[188] | 2012 | bool nodes_done = _skip_nodes; |
---|
| 2013 | bool edges_done = _skip_edges; |
---|
[165] | 2014 | bool attributes_done = false; |
---|
| 2015 | |
---|
[209] | 2016 | line_num = 0; |
---|
[165] | 2017 | readLine(); |
---|
[172] | 2018 | skipSection(); |
---|
[165] | 2019 | |
---|
| 2020 | while (readSuccess()) { |
---|
[209] | 2021 | try { |
---|
| 2022 | char c; |
---|
| 2023 | std::string section, caption; |
---|
| 2024 | line >> c; |
---|
| 2025 | _reader_bits::readToken(line, section); |
---|
| 2026 | _reader_bits::readToken(line, caption); |
---|
| 2027 | |
---|
| 2028 | if (line >> c) |
---|
[291] | 2029 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 2030 | |
---|
| 2031 | if (section == "nodes" && !nodes_done) { |
---|
| 2032 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
---|
| 2033 | readNodes(); |
---|
| 2034 | nodes_done = true; |
---|
| 2035 | } |
---|
| 2036 | } else if ((section == "edges" || section == "arcs") && |
---|
| 2037 | !edges_done) { |
---|
| 2038 | if (_edges_caption.empty() || _edges_caption == caption) { |
---|
| 2039 | readEdges(); |
---|
| 2040 | edges_done = true; |
---|
| 2041 | } |
---|
| 2042 | } else if (section == "attributes" && !attributes_done) { |
---|
| 2043 | if (_attributes_caption.empty() || _attributes_caption == caption) { |
---|
| 2044 | readAttributes(); |
---|
| 2045 | attributes_done = true; |
---|
| 2046 | } |
---|
| 2047 | } else { |
---|
| 2048 | readLine(); |
---|
| 2049 | skipSection(); |
---|
| 2050 | } |
---|
[290] | 2051 | } catch (FormatError& error) { |
---|
[209] | 2052 | error.line(line_num); |
---|
[290] | 2053 | error.file(_filename); |
---|
[209] | 2054 | throw; |
---|
| 2055 | } |
---|
[165] | 2056 | } |
---|
| 2057 | |
---|
| 2058 | if (!nodes_done) { |
---|
[290] | 2059 | throw FormatError("Section @nodes not found"); |
---|
[165] | 2060 | } |
---|
| 2061 | |
---|
| 2062 | if (!edges_done) { |
---|
[290] | 2063 | throw FormatError("Section @edges not found"); |
---|
[165] | 2064 | } |
---|
| 2065 | |
---|
| 2066 | if (!attributes_done && !_attributes.empty()) { |
---|
[290] | 2067 | throw FormatError("Section @attributes not found"); |
---|
[165] | 2068 | } |
---|
| 2069 | |
---|
| 2070 | } |
---|
| 2071 | |
---|
| 2072 | /// @} |
---|
[209] | 2073 | |
---|
[165] | 2074 | }; |
---|
| 2075 | |
---|
[645] | 2076 | /// \ingroup lemon_io |
---|
| 2077 | /// |
---|
| 2078 | /// \brief Return a \ref GraphReader class |
---|
| 2079 | /// |
---|
[956] | 2080 | /// This function just returns a \ref GraphReader class. |
---|
[645] | 2081 | /// |
---|
[956] | 2082 | /// With this function a graph can be read from an |
---|
[645] | 2083 | /// \ref lgf-format "LGF" file or input stream with several maps and |
---|
| 2084 | /// attributes. For example, there is weighted matching problem on a |
---|
| 2085 | /// graph, i.e. a graph with a \e weight map on the edges. This |
---|
| 2086 | /// graph can be read with the following code: |
---|
| 2087 | /// |
---|
| 2088 | ///\code |
---|
| 2089 | ///ListGraph graph; |
---|
| 2090 | ///ListGraph::EdgeMap<int> weight(graph); |
---|
| 2091 | ///graphReader(graph, std::cin). |
---|
| 2092 | /// edgeMap("weight", weight). |
---|
| 2093 | /// run(); |
---|
| 2094 | ///\endcode |
---|
| 2095 | /// |
---|
| 2096 | /// For a complete documentation, please see the \ref GraphReader |
---|
| 2097 | /// class documentation. |
---|
| 2098 | /// \warning Don't forget to put the \ref GraphReader::run() "run()" |
---|
| 2099 | /// to the end of the parameter list. |
---|
| 2100 | /// \relates GraphReader |
---|
| 2101 | /// \sa graphReader(TGR& graph, const std::string& fn) |
---|
| 2102 | /// \sa graphReader(TGR& graph, const char* fn) |
---|
| 2103 | template <typename TGR> |
---|
| 2104 | GraphReader<TGR> graphReader(TGR& graph, std::istream& is) { |
---|
| 2105 | GraphReader<TGR> tmp(graph, is); |
---|
| 2106 | return tmp; |
---|
| 2107 | } |
---|
| 2108 | |
---|
[517] | 2109 | /// \brief Return a \ref GraphReader class |
---|
| 2110 | /// |
---|
| 2111 | /// This function just returns a \ref GraphReader class. |
---|
| 2112 | /// \relates GraphReader |
---|
[645] | 2113 | /// \sa graphReader(TGR& graph, std::istream& is) |
---|
| 2114 | template <typename TGR> |
---|
| 2115 | GraphReader<TGR> graphReader(TGR& graph, const std::string& fn) { |
---|
| 2116 | GraphReader<TGR> tmp(graph, fn); |
---|
[517] | 2117 | return tmp; |
---|
| 2118 | } |
---|
| 2119 | |
---|
| 2120 | /// \brief Return a \ref GraphReader class |
---|
| 2121 | /// |
---|
| 2122 | /// This function just returns a \ref GraphReader class. |
---|
| 2123 | /// \relates GraphReader |
---|
[645] | 2124 | /// \sa graphReader(TGR& graph, std::istream& is) |
---|
| 2125 | template <typename TGR> |
---|
| 2126 | GraphReader<TGR> graphReader(TGR& graph, const char* fn) { |
---|
| 2127 | GraphReader<TGR> tmp(graph, fn); |
---|
[517] | 2128 | return tmp; |
---|
| 2129 | } |
---|
| 2130 | |
---|
[1192] | 2131 | template <typename BGR> |
---|
| 2132 | class BpGraphReader; |
---|
| 2133 | |
---|
| 2134 | template <typename TBGR> |
---|
| 2135 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is = std::cin); |
---|
| 2136 | template <typename TBGR> |
---|
| 2137 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const std::string& fn); |
---|
| 2138 | template <typename TBGR> |
---|
| 2139 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char *fn); |
---|
| 2140 | |
---|
| 2141 | /// \ingroup lemon_io |
---|
| 2142 | /// |
---|
| 2143 | /// \brief \ref lgf-format "LGF" reader for bipartite graphs |
---|
| 2144 | /// |
---|
| 2145 | /// This utility reads an \ref lgf-format "LGF" file. |
---|
| 2146 | /// |
---|
| 2147 | /// It can be used almost the same way as \c GraphReader, but it |
---|
| 2148 | /// reads the red and blue nodes from separate sections, and these |
---|
| 2149 | /// sections can contain different set of maps. |
---|
| 2150 | /// |
---|
[1194] | 2151 | /// The red and blue node maps are read from the corresponding |
---|
[1192] | 2152 | /// sections. If a map is defined with the same name in both of |
---|
| 2153 | /// these sections, then it can be read as a node map. |
---|
| 2154 | template <typename BGR> |
---|
| 2155 | class BpGraphReader { |
---|
| 2156 | public: |
---|
| 2157 | |
---|
| 2158 | typedef BGR Graph; |
---|
| 2159 | |
---|
| 2160 | private: |
---|
| 2161 | |
---|
| 2162 | TEMPLATE_BPGRAPH_TYPEDEFS(BGR); |
---|
| 2163 | |
---|
| 2164 | std::istream* _is; |
---|
| 2165 | bool local_is; |
---|
| 2166 | std::string _filename; |
---|
| 2167 | |
---|
| 2168 | BGR& _graph; |
---|
| 2169 | |
---|
| 2170 | std::string _nodes_caption; |
---|
| 2171 | std::string _edges_caption; |
---|
| 2172 | std::string _attributes_caption; |
---|
| 2173 | |
---|
| 2174 | typedef std::map<std::string, Node> NodeIndex; |
---|
| 2175 | NodeIndex _node_index; |
---|
| 2176 | typedef std::map<std::string, Edge> EdgeIndex; |
---|
| 2177 | EdgeIndex _edge_index; |
---|
| 2178 | |
---|
| 2179 | typedef std::vector<std::pair<std::string, |
---|
| 2180 | _reader_bits::MapStorageBase<Node>*> > NodeMaps; |
---|
| 2181 | NodeMaps _red_maps; |
---|
| 2182 | NodeMaps _blue_maps; |
---|
| 2183 | |
---|
| 2184 | typedef std::vector<std::pair<std::string, |
---|
| 2185 | _reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
---|
| 2186 | EdgeMaps _edge_maps; |
---|
| 2187 | |
---|
| 2188 | typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
---|
| 2189 | Attributes; |
---|
| 2190 | Attributes _attributes; |
---|
| 2191 | |
---|
| 2192 | bool _use_nodes; |
---|
| 2193 | bool _use_edges; |
---|
| 2194 | |
---|
| 2195 | bool _skip_nodes; |
---|
| 2196 | bool _skip_edges; |
---|
| 2197 | |
---|
| 2198 | int line_num; |
---|
| 2199 | std::istringstream line; |
---|
| 2200 | |
---|
| 2201 | public: |
---|
| 2202 | |
---|
| 2203 | /// \brief Constructor |
---|
| 2204 | /// |
---|
| 2205 | /// Construct an undirected graph reader, which reads from the given |
---|
| 2206 | /// input stream. |
---|
| 2207 | BpGraphReader(BGR& graph, std::istream& is = std::cin) |
---|
| 2208 | : _is(&is), local_is(false), _graph(graph), |
---|
| 2209 | _use_nodes(false), _use_edges(false), |
---|
| 2210 | _skip_nodes(false), _skip_edges(false) {} |
---|
| 2211 | |
---|
| 2212 | /// \brief Constructor |
---|
| 2213 | /// |
---|
| 2214 | /// Construct an undirected graph reader, which reads from the given |
---|
| 2215 | /// file. |
---|
| 2216 | BpGraphReader(BGR& graph, const std::string& fn) |
---|
| 2217 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
---|
| 2218 | _filename(fn), _graph(graph), |
---|
| 2219 | _use_nodes(false), _use_edges(false), |
---|
| 2220 | _skip_nodes(false), _skip_edges(false) { |
---|
| 2221 | if (!(*_is)) { |
---|
| 2222 | delete _is; |
---|
| 2223 | throw IoError("Cannot open file", fn); |
---|
| 2224 | } |
---|
| 2225 | } |
---|
| 2226 | |
---|
| 2227 | /// \brief Constructor |
---|
| 2228 | /// |
---|
| 2229 | /// Construct an undirected graph reader, which reads from the given |
---|
| 2230 | /// file. |
---|
| 2231 | BpGraphReader(BGR& graph, const char* fn) |
---|
| 2232 | : _is(new std::ifstream(fn)), local_is(true), |
---|
| 2233 | _filename(fn), _graph(graph), |
---|
| 2234 | _use_nodes(false), _use_edges(false), |
---|
| 2235 | _skip_nodes(false), _skip_edges(false) { |
---|
| 2236 | if (!(*_is)) { |
---|
| 2237 | delete _is; |
---|
| 2238 | throw IoError("Cannot open file", fn); |
---|
| 2239 | } |
---|
| 2240 | } |
---|
| 2241 | |
---|
| 2242 | /// \brief Destructor |
---|
| 2243 | ~BpGraphReader() { |
---|
| 2244 | for (typename NodeMaps::iterator it = _red_maps.begin(); |
---|
| 2245 | it != _red_maps.end(); ++it) { |
---|
| 2246 | delete it->second; |
---|
| 2247 | } |
---|
| 2248 | |
---|
| 2249 | for (typename NodeMaps::iterator it = _blue_maps.begin(); |
---|
| 2250 | it != _blue_maps.end(); ++it) { |
---|
| 2251 | delete it->second; |
---|
| 2252 | } |
---|
| 2253 | |
---|
| 2254 | for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
---|
| 2255 | it != _edge_maps.end(); ++it) { |
---|
| 2256 | delete it->second; |
---|
| 2257 | } |
---|
| 2258 | |
---|
| 2259 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 2260 | it != _attributes.end(); ++it) { |
---|
| 2261 | delete it->second; |
---|
| 2262 | } |
---|
| 2263 | |
---|
| 2264 | if (local_is) { |
---|
| 2265 | delete _is; |
---|
| 2266 | } |
---|
| 2267 | |
---|
| 2268 | } |
---|
| 2269 | |
---|
| 2270 | private: |
---|
| 2271 | template <typename TBGR> |
---|
| 2272 | friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is); |
---|
| 2273 | template <typename TBGR> |
---|
| 2274 | friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, |
---|
| 2275 | const std::string& fn); |
---|
| 2276 | template <typename TBGR> |
---|
| 2277 | friend BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char *fn); |
---|
| 2278 | |
---|
| 2279 | BpGraphReader(BpGraphReader& other) |
---|
| 2280 | : _is(other._is), local_is(other.local_is), _graph(other._graph), |
---|
| 2281 | _use_nodes(other._use_nodes), _use_edges(other._use_edges), |
---|
| 2282 | _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
---|
| 2283 | |
---|
| 2284 | other._is = 0; |
---|
| 2285 | other.local_is = false; |
---|
| 2286 | |
---|
| 2287 | _node_index.swap(other._node_index); |
---|
| 2288 | _edge_index.swap(other._edge_index); |
---|
| 2289 | |
---|
| 2290 | _red_maps.swap(other._red_maps); |
---|
| 2291 | _blue_maps.swap(other._blue_maps); |
---|
| 2292 | _edge_maps.swap(other._edge_maps); |
---|
| 2293 | _attributes.swap(other._attributes); |
---|
| 2294 | |
---|
| 2295 | _nodes_caption = other._nodes_caption; |
---|
| 2296 | _edges_caption = other._edges_caption; |
---|
| 2297 | _attributes_caption = other._attributes_caption; |
---|
| 2298 | |
---|
| 2299 | } |
---|
| 2300 | |
---|
| 2301 | BpGraphReader& operator=(const BpGraphReader&); |
---|
| 2302 | |
---|
| 2303 | public: |
---|
| 2304 | |
---|
| 2305 | /// \name Reading Rules |
---|
| 2306 | /// @{ |
---|
| 2307 | |
---|
| 2308 | /// \brief Node map reading rule |
---|
| 2309 | /// |
---|
| 2310 | /// Add a node map reading rule to the reader. |
---|
| 2311 | template <typename Map> |
---|
| 2312 | BpGraphReader& nodeMap(const std::string& caption, Map& map) { |
---|
| 2313 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
| 2314 | _reader_bits::MapStorageBase<Node>* red_storage = |
---|
| 2315 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
| 2316 | _red_maps.push_back(std::make_pair(caption, red_storage)); |
---|
| 2317 | _reader_bits::MapStorageBase<Node>* blue_storage = |
---|
| 2318 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
| 2319 | _blue_maps.push_back(std::make_pair(caption, blue_storage)); |
---|
| 2320 | return *this; |
---|
| 2321 | } |
---|
| 2322 | |
---|
| 2323 | /// \brief Node map reading rule |
---|
| 2324 | /// |
---|
| 2325 | /// Add a node map reading rule with specialized converter to the |
---|
| 2326 | /// reader. |
---|
| 2327 | template <typename Map, typename Converter> |
---|
| 2328 | BpGraphReader& nodeMap(const std::string& caption, Map& map, |
---|
| 2329 | const Converter& converter = Converter()) { |
---|
| 2330 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
| 2331 | _reader_bits::MapStorageBase<Node>* red_storage = |
---|
| 2332 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
| 2333 | _red_maps.push_back(std::make_pair(caption, red_storage)); |
---|
| 2334 | _reader_bits::MapStorageBase<Node>* blue_storage = |
---|
| 2335 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
| 2336 | _blue_maps.push_back(std::make_pair(caption, blue_storage)); |
---|
| 2337 | return *this; |
---|
| 2338 | } |
---|
| 2339 | |
---|
[1194] | 2340 | /// Add a red node map reading rule to the reader. |
---|
[1192] | 2341 | template <typename Map> |
---|
[1194] | 2342 | BpGraphReader& redNodeMap(const std::string& caption, Map& map) { |
---|
[1192] | 2343 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
| 2344 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 2345 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
| 2346 | _red_maps.push_back(std::make_pair(caption, storage)); |
---|
| 2347 | return *this; |
---|
| 2348 | } |
---|
| 2349 | |
---|
[1194] | 2350 | /// \brief Red node map reading rule |
---|
[1192] | 2351 | /// |
---|
[1194] | 2352 | /// Add a red node map node reading rule with specialized converter to |
---|
| 2353 | /// the reader. |
---|
[1192] | 2354 | template <typename Map, typename Converter> |
---|
[1194] | 2355 | BpGraphReader& redNodeMap(const std::string& caption, Map& map, |
---|
| 2356 | const Converter& converter = Converter()) { |
---|
[1192] | 2357 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
| 2358 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 2359 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
| 2360 | _red_maps.push_back(std::make_pair(caption, storage)); |
---|
| 2361 | return *this; |
---|
| 2362 | } |
---|
| 2363 | |
---|
[1194] | 2364 | /// Add a blue node map reading rule to the reader. |
---|
[1192] | 2365 | template <typename Map> |
---|
[1194] | 2366 | BpGraphReader& blueNodeMap(const std::string& caption, Map& map) { |
---|
[1192] | 2367 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
| 2368 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 2369 | new _reader_bits::MapStorage<Node, Map>(map); |
---|
| 2370 | _blue_maps.push_back(std::make_pair(caption, storage)); |
---|
| 2371 | return *this; |
---|
| 2372 | } |
---|
| 2373 | |
---|
[1194] | 2374 | /// \brief Blue node map reading rule |
---|
[1192] | 2375 | /// |
---|
[1194] | 2376 | /// Add a blue node map reading rule with specialized converter to |
---|
| 2377 | /// the reader. |
---|
[1192] | 2378 | template <typename Map, typename Converter> |
---|
[1194] | 2379 | BpGraphReader& blueNodeMap(const std::string& caption, Map& map, |
---|
| 2380 | const Converter& converter = Converter()) { |
---|
[1192] | 2381 | checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
---|
| 2382 | _reader_bits::MapStorageBase<Node>* storage = |
---|
| 2383 | new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
---|
| 2384 | _blue_maps.push_back(std::make_pair(caption, storage)); |
---|
| 2385 | return *this; |
---|
| 2386 | } |
---|
| 2387 | |
---|
| 2388 | /// \brief Edge map reading rule |
---|
| 2389 | /// |
---|
| 2390 | /// Add an edge map reading rule to the reader. |
---|
| 2391 | template <typename Map> |
---|
| 2392 | BpGraphReader& edgeMap(const std::string& caption, Map& map) { |
---|
| 2393 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
---|
| 2394 | _reader_bits::MapStorageBase<Edge>* storage = |
---|
| 2395 | new _reader_bits::MapStorage<Edge, Map>(map); |
---|
| 2396 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 2397 | return *this; |
---|
| 2398 | } |
---|
| 2399 | |
---|
| 2400 | /// \brief Edge map reading rule |
---|
| 2401 | /// |
---|
| 2402 | /// Add an edge map reading rule with specialized converter to the |
---|
| 2403 | /// reader. |
---|
| 2404 | template <typename Map, typename Converter> |
---|
| 2405 | BpGraphReader& edgeMap(const std::string& caption, Map& map, |
---|
| 2406 | const Converter& converter = Converter()) { |
---|
| 2407 | checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
---|
| 2408 | _reader_bits::MapStorageBase<Edge>* storage = |
---|
| 2409 | new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
---|
| 2410 | _edge_maps.push_back(std::make_pair(caption, storage)); |
---|
| 2411 | return *this; |
---|
| 2412 | } |
---|
| 2413 | |
---|
| 2414 | /// \brief Arc map reading rule |
---|
| 2415 | /// |
---|
| 2416 | /// Add an arc map reading rule to the reader. |
---|
| 2417 | template <typename Map> |
---|
| 2418 | BpGraphReader& arcMap(const std::string& caption, Map& map) { |
---|
| 2419 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
| 2420 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
---|
| 2421 | new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
---|
| 2422 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
| 2423 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
---|
| 2424 | new _reader_bits::GraphArcMapStorage<BGR, false, Map>(_graph, map); |
---|
| 2425 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 2426 | return *this; |
---|
| 2427 | } |
---|
| 2428 | |
---|
| 2429 | /// \brief Arc map reading rule |
---|
| 2430 | /// |
---|
| 2431 | /// Add an arc map reading rule with specialized converter to the |
---|
| 2432 | /// reader. |
---|
| 2433 | template <typename Map, typename Converter> |
---|
| 2434 | BpGraphReader& arcMap(const std::string& caption, Map& map, |
---|
| 2435 | const Converter& converter = Converter()) { |
---|
| 2436 | checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
---|
| 2437 | _reader_bits::MapStorageBase<Edge>* forward_storage = |
---|
| 2438 | new _reader_bits::GraphArcMapStorage<BGR, true, Map, Converter> |
---|
| 2439 | (_graph, map, converter); |
---|
| 2440 | _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); |
---|
| 2441 | _reader_bits::MapStorageBase<Edge>* backward_storage = |
---|
| 2442 | new _reader_bits::GraphArcMapStorage<BGR, false, Map, Converter> |
---|
| 2443 | (_graph, map, converter); |
---|
| 2444 | _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); |
---|
| 2445 | return *this; |
---|
| 2446 | } |
---|
| 2447 | |
---|
| 2448 | /// \brief Attribute reading rule |
---|
| 2449 | /// |
---|
| 2450 | /// Add an attribute reading rule to the reader. |
---|
| 2451 | template <typename Value> |
---|
| 2452 | BpGraphReader& attribute(const std::string& caption, Value& value) { |
---|
| 2453 | _reader_bits::ValueStorageBase* storage = |
---|
| 2454 | new _reader_bits::ValueStorage<Value>(value); |
---|
| 2455 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 2456 | return *this; |
---|
| 2457 | } |
---|
| 2458 | |
---|
| 2459 | /// \brief Attribute reading rule |
---|
| 2460 | /// |
---|
| 2461 | /// Add an attribute reading rule with specialized converter to the |
---|
| 2462 | /// reader. |
---|
| 2463 | template <typename Value, typename Converter> |
---|
| 2464 | BpGraphReader& attribute(const std::string& caption, Value& value, |
---|
| 2465 | const Converter& converter = Converter()) { |
---|
| 2466 | _reader_bits::ValueStorageBase* storage = |
---|
| 2467 | new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
---|
| 2468 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 2469 | return *this; |
---|
| 2470 | } |
---|
| 2471 | |
---|
| 2472 | /// \brief Node reading rule |
---|
| 2473 | /// |
---|
| 2474 | /// Add a node reading rule to reader. |
---|
| 2475 | BpGraphReader& node(const std::string& caption, Node& node) { |
---|
| 2476 | typedef _reader_bits::MapLookUpConverter<Node> Converter; |
---|
| 2477 | Converter converter(_node_index); |
---|
| 2478 | _reader_bits::ValueStorageBase* storage = |
---|
| 2479 | new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
---|
| 2480 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 2481 | return *this; |
---|
| 2482 | } |
---|
| 2483 | |
---|
| 2484 | /// \brief Edge reading rule |
---|
| 2485 | /// |
---|
| 2486 | /// Add an edge reading rule to reader. |
---|
| 2487 | BpGraphReader& edge(const std::string& caption, Edge& edge) { |
---|
| 2488 | typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
---|
| 2489 | Converter converter(_edge_index); |
---|
| 2490 | _reader_bits::ValueStorageBase* storage = |
---|
| 2491 | new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
---|
| 2492 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 2493 | return *this; |
---|
| 2494 | } |
---|
| 2495 | |
---|
| 2496 | /// \brief Arc reading rule |
---|
| 2497 | /// |
---|
| 2498 | /// Add an arc reading rule to reader. |
---|
| 2499 | BpGraphReader& arc(const std::string& caption, Arc& arc) { |
---|
| 2500 | typedef _reader_bits::GraphArcLookUpConverter<BGR> Converter; |
---|
| 2501 | Converter converter(_graph, _edge_index); |
---|
| 2502 | _reader_bits::ValueStorageBase* storage = |
---|
| 2503 | new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
---|
| 2504 | _attributes.insert(std::make_pair(caption, storage)); |
---|
| 2505 | return *this; |
---|
| 2506 | } |
---|
| 2507 | |
---|
| 2508 | /// @} |
---|
| 2509 | |
---|
| 2510 | /// \name Select Section by Name |
---|
| 2511 | /// @{ |
---|
| 2512 | |
---|
| 2513 | /// \brief Set \c \@nodes section to be read |
---|
| 2514 | /// |
---|
| 2515 | /// Set \c \@nodes section to be read. |
---|
| 2516 | BpGraphReader& nodes(const std::string& caption) { |
---|
| 2517 | _nodes_caption = caption; |
---|
| 2518 | return *this; |
---|
| 2519 | } |
---|
| 2520 | |
---|
| 2521 | /// \brief Set \c \@edges section to be read |
---|
| 2522 | /// |
---|
| 2523 | /// Set \c \@edges section to be read. |
---|
| 2524 | BpGraphReader& edges(const std::string& caption) { |
---|
| 2525 | _edges_caption = caption; |
---|
| 2526 | return *this; |
---|
| 2527 | } |
---|
| 2528 | |
---|
| 2529 | /// \brief Set \c \@attributes section to be read |
---|
| 2530 | /// |
---|
| 2531 | /// Set \c \@attributes section to be read. |
---|
| 2532 | BpGraphReader& attributes(const std::string& caption) { |
---|
| 2533 | _attributes_caption = caption; |
---|
| 2534 | return *this; |
---|
| 2535 | } |
---|
| 2536 | |
---|
| 2537 | /// @} |
---|
| 2538 | |
---|
| 2539 | /// \name Using Previously Constructed Node or Edge Set |
---|
| 2540 | /// @{ |
---|
| 2541 | |
---|
| 2542 | /// \brief Use previously constructed node set |
---|
| 2543 | /// |
---|
| 2544 | /// Use previously constructed node set, and specify the node |
---|
| 2545 | /// label map. |
---|
| 2546 | template <typename Map> |
---|
| 2547 | BpGraphReader& useNodes(const Map& map) { |
---|
| 2548 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
| 2549 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
| 2550 | _use_nodes = true; |
---|
| 2551 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 2552 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 2553 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
| 2554 | } |
---|
| 2555 | return *this; |
---|
| 2556 | } |
---|
| 2557 | |
---|
| 2558 | /// \brief Use previously constructed node set |
---|
| 2559 | /// |
---|
| 2560 | /// Use previously constructed node set, and specify the node |
---|
| 2561 | /// label map and a functor which converts the label map values to |
---|
| 2562 | /// \c std::string. |
---|
| 2563 | template <typename Map, typename Converter> |
---|
| 2564 | BpGraphReader& useNodes(const Map& map, |
---|
| 2565 | const Converter& converter = Converter()) { |
---|
| 2566 | checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
---|
| 2567 | LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
---|
| 2568 | _use_nodes = true; |
---|
| 2569 | for (NodeIt n(_graph); n != INVALID; ++n) { |
---|
| 2570 | _node_index.insert(std::make_pair(converter(map[n]), n)); |
---|
| 2571 | } |
---|
| 2572 | return *this; |
---|
| 2573 | } |
---|
| 2574 | |
---|
| 2575 | /// \brief Use previously constructed edge set |
---|
| 2576 | /// |
---|
| 2577 | /// Use previously constructed edge set, and specify the edge |
---|
| 2578 | /// label map. |
---|
| 2579 | template <typename Map> |
---|
| 2580 | BpGraphReader& useEdges(const Map& map) { |
---|
| 2581 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
| 2582 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
---|
| 2583 | _use_edges = true; |
---|
| 2584 | _writer_bits::DefaultConverter<typename Map::Value> converter; |
---|
| 2585 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
---|
| 2586 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
---|
| 2587 | } |
---|
| 2588 | return *this; |
---|
| 2589 | } |
---|
| 2590 | |
---|
| 2591 | /// \brief Use previously constructed edge set |
---|
| 2592 | /// |
---|
| 2593 | /// Use previously constructed edge set, and specify the edge |
---|
| 2594 | /// label map and a functor which converts the label map values to |
---|
| 2595 | /// \c std::string. |
---|
| 2596 | template <typename Map, typename Converter> |
---|
| 2597 | BpGraphReader& useEdges(const Map& map, |
---|
| 2598 | const Converter& converter = Converter()) { |
---|
| 2599 | checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
---|
| 2600 | LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
---|
| 2601 | _use_edges = true; |
---|
| 2602 | for (EdgeIt a(_graph); a != INVALID; ++a) { |
---|
| 2603 | _edge_index.insert(std::make_pair(converter(map[a]), a)); |
---|
| 2604 | } |
---|
| 2605 | return *this; |
---|
| 2606 | } |
---|
| 2607 | |
---|
| 2608 | /// \brief Skip the reading of node section |
---|
| 2609 | /// |
---|
| 2610 | /// Omit the reading of the node section. This implies that each node |
---|
| 2611 | /// map reading rule will be abandoned, and the nodes of the graph |
---|
| 2612 | /// will not be constructed, which usually cause that the edge set |
---|
| 2613 | /// could not be read due to lack of node name |
---|
| 2614 | /// could not be read due to lack of node name resolving. |
---|
| 2615 | /// Therefore \c skipEdges() function should also be used, or |
---|
| 2616 | /// \c useNodes() should be used to specify the label of the nodes. |
---|
| 2617 | BpGraphReader& skipNodes() { |
---|
| 2618 | LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
---|
| 2619 | _skip_nodes = true; |
---|
| 2620 | return *this; |
---|
| 2621 | } |
---|
| 2622 | |
---|
| 2623 | /// \brief Skip the reading of edge section |
---|
| 2624 | /// |
---|
| 2625 | /// Omit the reading of the edge section. This implies that each edge |
---|
| 2626 | /// map reading rule will be abandoned, and the edges of the graph |
---|
| 2627 | /// will not be constructed. |
---|
| 2628 | BpGraphReader& skipEdges() { |
---|
| 2629 | LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
---|
| 2630 | _skip_edges = true; |
---|
| 2631 | return *this; |
---|
| 2632 | } |
---|
| 2633 | |
---|
| 2634 | /// @} |
---|
| 2635 | |
---|
| 2636 | private: |
---|
| 2637 | |
---|
| 2638 | bool readLine() { |
---|
| 2639 | std::string str; |
---|
| 2640 | while(++line_num, std::getline(*_is, str)) { |
---|
| 2641 | line.clear(); line.str(str); |
---|
| 2642 | char c; |
---|
| 2643 | if (line >> std::ws >> c && c != '#') { |
---|
| 2644 | line.putback(c); |
---|
| 2645 | return true; |
---|
| 2646 | } |
---|
| 2647 | } |
---|
| 2648 | return false; |
---|
| 2649 | } |
---|
| 2650 | |
---|
| 2651 | bool readSuccess() { |
---|
| 2652 | return static_cast<bool>(*_is); |
---|
| 2653 | } |
---|
| 2654 | |
---|
| 2655 | void skipSection() { |
---|
| 2656 | char c; |
---|
| 2657 | while (readSuccess() && line >> c && c != '@') { |
---|
| 2658 | readLine(); |
---|
| 2659 | } |
---|
| 2660 | if (readSuccess()) { |
---|
| 2661 | line.putback(c); |
---|
| 2662 | } |
---|
| 2663 | } |
---|
| 2664 | |
---|
| 2665 | void readRedNodes() { |
---|
| 2666 | |
---|
| 2667 | std::vector<int> map_index(_red_maps.size()); |
---|
| 2668 | int map_num, label_index; |
---|
| 2669 | |
---|
| 2670 | char c; |
---|
| 2671 | if (!readLine() || !(line >> c) || c == '@') { |
---|
| 2672 | if (readSuccess() && line) line.putback(c); |
---|
| 2673 | if (!_red_maps.empty()) |
---|
| 2674 | throw FormatError("Cannot find map names"); |
---|
| 2675 | return; |
---|
| 2676 | } |
---|
| 2677 | line.putback(c); |
---|
| 2678 | |
---|
| 2679 | { |
---|
| 2680 | std::map<std::string, int> maps; |
---|
| 2681 | |
---|
| 2682 | std::string map; |
---|
| 2683 | int index = 0; |
---|
| 2684 | while (_reader_bits::readToken(line, map)) { |
---|
| 2685 | if (maps.find(map) != maps.end()) { |
---|
| 2686 | std::ostringstream msg; |
---|
[1194] | 2687 | msg << "Multiple occurence of red node map: " << map; |
---|
[1192] | 2688 | throw FormatError(msg.str()); |
---|
| 2689 | } |
---|
| 2690 | maps.insert(std::make_pair(map, index)); |
---|
| 2691 | ++index; |
---|
| 2692 | } |
---|
| 2693 | |
---|
| 2694 | for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) { |
---|
| 2695 | std::map<std::string, int>::iterator jt = |
---|
| 2696 | maps.find(_red_maps[i].first); |
---|
| 2697 | if (jt == maps.end()) { |
---|
| 2698 | std::ostringstream msg; |
---|
| 2699 | msg << "Map not found: " << _red_maps[i].first; |
---|
| 2700 | throw FormatError(msg.str()); |
---|
| 2701 | } |
---|
| 2702 | map_index[i] = jt->second; |
---|
| 2703 | } |
---|
| 2704 | |
---|
| 2705 | { |
---|
| 2706 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 2707 | if (jt != maps.end()) { |
---|
| 2708 | label_index = jt->second; |
---|
| 2709 | } else { |
---|
| 2710 | label_index = -1; |
---|
| 2711 | } |
---|
| 2712 | } |
---|
| 2713 | map_num = maps.size(); |
---|
| 2714 | } |
---|
| 2715 | |
---|
| 2716 | while (readLine() && line >> c && c != '@') { |
---|
| 2717 | line.putback(c); |
---|
| 2718 | |
---|
| 2719 | std::vector<std::string> tokens(map_num); |
---|
| 2720 | for (int i = 0; i < map_num; ++i) { |
---|
| 2721 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 2722 | std::ostringstream msg; |
---|
| 2723 | msg << "Column not found (" << i + 1 << ")"; |
---|
| 2724 | throw FormatError(msg.str()); |
---|
| 2725 | } |
---|
| 2726 | } |
---|
| 2727 | if (line >> std::ws >> c) |
---|
| 2728 | throw FormatError("Extra character at the end of line"); |
---|
| 2729 | |
---|
| 2730 | Node n; |
---|
| 2731 | if (!_use_nodes) { |
---|
| 2732 | n = _graph.addRedNode(); |
---|
| 2733 | if (label_index != -1) |
---|
| 2734 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
---|
| 2735 | } else { |
---|
| 2736 | if (label_index == -1) |
---|
| 2737 | throw FormatError("Label map not found"); |
---|
| 2738 | typename std::map<std::string, Node>::iterator it = |
---|
| 2739 | _node_index.find(tokens[label_index]); |
---|
| 2740 | if (it == _node_index.end()) { |
---|
| 2741 | std::ostringstream msg; |
---|
| 2742 | msg << "Node with label not found: " << tokens[label_index]; |
---|
| 2743 | throw FormatError(msg.str()); |
---|
| 2744 | } |
---|
| 2745 | n = it->second; |
---|
| 2746 | } |
---|
| 2747 | |
---|
| 2748 | for (int i = 0; i < static_cast<int>(_red_maps.size()); ++i) { |
---|
| 2749 | _red_maps[i].second->set(n, tokens[map_index[i]]); |
---|
| 2750 | } |
---|
| 2751 | |
---|
| 2752 | } |
---|
| 2753 | if (readSuccess()) { |
---|
| 2754 | line.putback(c); |
---|
| 2755 | } |
---|
| 2756 | } |
---|
| 2757 | |
---|
| 2758 | void readBlueNodes() { |
---|
| 2759 | |
---|
| 2760 | std::vector<int> map_index(_blue_maps.size()); |
---|
| 2761 | int map_num, label_index; |
---|
| 2762 | |
---|
| 2763 | char c; |
---|
| 2764 | if (!readLine() || !(line >> c) || c == '@') { |
---|
| 2765 | if (readSuccess() && line) line.putback(c); |
---|
| 2766 | if (!_blue_maps.empty()) |
---|
| 2767 | throw FormatError("Cannot find map names"); |
---|
| 2768 | return; |
---|
| 2769 | } |
---|
| 2770 | line.putback(c); |
---|
| 2771 | |
---|
| 2772 | { |
---|
| 2773 | std::map<std::string, int> maps; |
---|
| 2774 | |
---|
| 2775 | std::string map; |
---|
| 2776 | int index = 0; |
---|
| 2777 | while (_reader_bits::readToken(line, map)) { |
---|
| 2778 | if (maps.find(map) != maps.end()) { |
---|
| 2779 | std::ostringstream msg; |
---|
[1194] | 2780 | msg << "Multiple occurence of blue node map: " << map; |
---|
[1192] | 2781 | throw FormatError(msg.str()); |
---|
| 2782 | } |
---|
| 2783 | maps.insert(std::make_pair(map, index)); |
---|
| 2784 | ++index; |
---|
| 2785 | } |
---|
| 2786 | |
---|
| 2787 | for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) { |
---|
| 2788 | std::map<std::string, int>::iterator jt = |
---|
| 2789 | maps.find(_blue_maps[i].first); |
---|
| 2790 | if (jt == maps.end()) { |
---|
| 2791 | std::ostringstream msg; |
---|
| 2792 | msg << "Map not found: " << _blue_maps[i].first; |
---|
| 2793 | throw FormatError(msg.str()); |
---|
| 2794 | } |
---|
| 2795 | map_index[i] = jt->second; |
---|
| 2796 | } |
---|
| 2797 | |
---|
| 2798 | { |
---|
| 2799 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 2800 | if (jt != maps.end()) { |
---|
| 2801 | label_index = jt->second; |
---|
| 2802 | } else { |
---|
| 2803 | label_index = -1; |
---|
| 2804 | } |
---|
| 2805 | } |
---|
| 2806 | map_num = maps.size(); |
---|
| 2807 | } |
---|
| 2808 | |
---|
| 2809 | while (readLine() && line >> c && c != '@') { |
---|
| 2810 | line.putback(c); |
---|
| 2811 | |
---|
| 2812 | std::vector<std::string> tokens(map_num); |
---|
| 2813 | for (int i = 0; i < map_num; ++i) { |
---|
| 2814 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 2815 | std::ostringstream msg; |
---|
| 2816 | msg << "Column not found (" << i + 1 << ")"; |
---|
| 2817 | throw FormatError(msg.str()); |
---|
| 2818 | } |
---|
| 2819 | } |
---|
| 2820 | if (line >> std::ws >> c) |
---|
| 2821 | throw FormatError("Extra character at the end of line"); |
---|
| 2822 | |
---|
| 2823 | Node n; |
---|
| 2824 | if (!_use_nodes) { |
---|
| 2825 | n = _graph.addBlueNode(); |
---|
| 2826 | if (label_index != -1) |
---|
| 2827 | _node_index.insert(std::make_pair(tokens[label_index], n)); |
---|
| 2828 | } else { |
---|
| 2829 | if (label_index == -1) |
---|
| 2830 | throw FormatError("Label map not found"); |
---|
| 2831 | typename std::map<std::string, Node>::iterator it = |
---|
| 2832 | _node_index.find(tokens[label_index]); |
---|
| 2833 | if (it == _node_index.end()) { |
---|
| 2834 | std::ostringstream msg; |
---|
| 2835 | msg << "Node with label not found: " << tokens[label_index]; |
---|
| 2836 | throw FormatError(msg.str()); |
---|
| 2837 | } |
---|
| 2838 | n = it->second; |
---|
| 2839 | } |
---|
| 2840 | |
---|
| 2841 | for (int i = 0; i < static_cast<int>(_blue_maps.size()); ++i) { |
---|
| 2842 | _blue_maps[i].second->set(n, tokens[map_index[i]]); |
---|
| 2843 | } |
---|
| 2844 | |
---|
| 2845 | } |
---|
| 2846 | if (readSuccess()) { |
---|
| 2847 | line.putback(c); |
---|
| 2848 | } |
---|
| 2849 | } |
---|
| 2850 | |
---|
| 2851 | void readEdges() { |
---|
| 2852 | |
---|
| 2853 | std::vector<int> map_index(_edge_maps.size()); |
---|
| 2854 | int map_num, label_index; |
---|
| 2855 | |
---|
| 2856 | char c; |
---|
| 2857 | if (!readLine() || !(line >> c) || c == '@') { |
---|
| 2858 | if (readSuccess() && line) line.putback(c); |
---|
| 2859 | if (!_edge_maps.empty()) |
---|
| 2860 | throw FormatError("Cannot find map names"); |
---|
| 2861 | return; |
---|
| 2862 | } |
---|
| 2863 | line.putback(c); |
---|
| 2864 | |
---|
| 2865 | { |
---|
| 2866 | std::map<std::string, int> maps; |
---|
| 2867 | |
---|
| 2868 | std::string map; |
---|
| 2869 | int index = 0; |
---|
| 2870 | while (_reader_bits::readToken(line, map)) { |
---|
| 2871 | if (maps.find(map) != maps.end()) { |
---|
| 2872 | std::ostringstream msg; |
---|
| 2873 | msg << "Multiple occurence of edge map: " << map; |
---|
| 2874 | throw FormatError(msg.str()); |
---|
| 2875 | } |
---|
| 2876 | maps.insert(std::make_pair(map, index)); |
---|
| 2877 | ++index; |
---|
| 2878 | } |
---|
| 2879 | |
---|
| 2880 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
---|
| 2881 | std::map<std::string, int>::iterator jt = |
---|
| 2882 | maps.find(_edge_maps[i].first); |
---|
| 2883 | if (jt == maps.end()) { |
---|
| 2884 | std::ostringstream msg; |
---|
| 2885 | msg << "Map not found: " << _edge_maps[i].first; |
---|
| 2886 | throw FormatError(msg.str()); |
---|
| 2887 | } |
---|
| 2888 | map_index[i] = jt->second; |
---|
| 2889 | } |
---|
| 2890 | |
---|
| 2891 | { |
---|
| 2892 | std::map<std::string, int>::iterator jt = maps.find("label"); |
---|
| 2893 | if (jt != maps.end()) { |
---|
| 2894 | label_index = jt->second; |
---|
| 2895 | } else { |
---|
| 2896 | label_index = -1; |
---|
| 2897 | } |
---|
| 2898 | } |
---|
| 2899 | map_num = maps.size(); |
---|
| 2900 | } |
---|
| 2901 | |
---|
| 2902 | while (readLine() && line >> c && c != '@') { |
---|
| 2903 | line.putback(c); |
---|
| 2904 | |
---|
| 2905 | std::string source_token; |
---|
| 2906 | std::string target_token; |
---|
| 2907 | |
---|
| 2908 | if (!_reader_bits::readToken(line, source_token)) |
---|
| 2909 | throw FormatError("Red node not found"); |
---|
| 2910 | |
---|
| 2911 | if (!_reader_bits::readToken(line, target_token)) |
---|
| 2912 | throw FormatError("Blue node not found"); |
---|
| 2913 | |
---|
| 2914 | std::vector<std::string> tokens(map_num); |
---|
| 2915 | for (int i = 0; i < map_num; ++i) { |
---|
| 2916 | if (!_reader_bits::readToken(line, tokens[i])) { |
---|
| 2917 | std::ostringstream msg; |
---|
| 2918 | msg << "Column not found (" << i + 1 << ")"; |
---|
| 2919 | throw FormatError(msg.str()); |
---|
| 2920 | } |
---|
| 2921 | } |
---|
| 2922 | if (line >> std::ws >> c) |
---|
| 2923 | throw FormatError("Extra character at the end of line"); |
---|
| 2924 | |
---|
| 2925 | Edge e; |
---|
| 2926 | if (!_use_edges) { |
---|
| 2927 | |
---|
| 2928 | typename NodeIndex::iterator it; |
---|
| 2929 | |
---|
| 2930 | it = _node_index.find(source_token); |
---|
| 2931 | if (it == _node_index.end()) { |
---|
| 2932 | std::ostringstream msg; |
---|
| 2933 | msg << "Item not found: " << source_token; |
---|
| 2934 | throw FormatError(msg.str()); |
---|
| 2935 | } |
---|
| 2936 | Node source = it->second; |
---|
| 2937 | if (!_graph.red(source)) { |
---|
| 2938 | std::ostringstream msg; |
---|
| 2939 | msg << "Item is not red node: " << source_token; |
---|
| 2940 | throw FormatError(msg.str()); |
---|
| 2941 | } |
---|
| 2942 | |
---|
| 2943 | it = _node_index.find(target_token); |
---|
| 2944 | if (it == _node_index.end()) { |
---|
| 2945 | std::ostringstream msg; |
---|
| 2946 | msg << "Item not found: " << target_token; |
---|
| 2947 | throw FormatError(msg.str()); |
---|
| 2948 | } |
---|
| 2949 | Node target = it->second; |
---|
| 2950 | if (!_graph.blue(target)) { |
---|
| 2951 | std::ostringstream msg; |
---|
| 2952 | msg << "Item is not red node: " << source_token; |
---|
| 2953 | throw FormatError(msg.str()); |
---|
| 2954 | } |
---|
| 2955 | |
---|
| 2956 | e = _graph.addEdge(source, target); |
---|
| 2957 | if (label_index != -1) |
---|
| 2958 | _edge_index.insert(std::make_pair(tokens[label_index], e)); |
---|
| 2959 | } else { |
---|
| 2960 | if (label_index == -1) |
---|
| 2961 | throw FormatError("Label map not found"); |
---|
| 2962 | typename std::map<std::string, Edge>::iterator it = |
---|
| 2963 | _edge_index.find(tokens[label_index]); |
---|
| 2964 | if (it == _edge_index.end()) { |
---|
| 2965 | std::ostringstream msg; |
---|
| 2966 | msg << "Edge with label not found: " << tokens[label_index]; |
---|
| 2967 | throw FormatError(msg.str()); |
---|
| 2968 | } |
---|
| 2969 | e = it->second; |
---|
| 2970 | } |
---|
| 2971 | |
---|
| 2972 | for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) { |
---|
| 2973 | _edge_maps[i].second->set(e, tokens[map_index[i]]); |
---|
| 2974 | } |
---|
| 2975 | |
---|
| 2976 | } |
---|
| 2977 | if (readSuccess()) { |
---|
| 2978 | line.putback(c); |
---|
| 2979 | } |
---|
| 2980 | } |
---|
| 2981 | |
---|
| 2982 | void readAttributes() { |
---|
| 2983 | |
---|
| 2984 | std::set<std::string> read_attr; |
---|
| 2985 | |
---|
| 2986 | char c; |
---|
| 2987 | while (readLine() && line >> c && c != '@') { |
---|
| 2988 | line.putback(c); |
---|
| 2989 | |
---|
| 2990 | std::string attr, token; |
---|
| 2991 | if (!_reader_bits::readToken(line, attr)) |
---|
| 2992 | throw FormatError("Attribute name not found"); |
---|
| 2993 | if (!_reader_bits::readToken(line, token)) |
---|
| 2994 | throw FormatError("Attribute value not found"); |
---|
| 2995 | if (line >> c) |
---|
| 2996 | throw FormatError("Extra character at the end of line"); |
---|
| 2997 | |
---|
| 2998 | { |
---|
| 2999 | std::set<std::string>::iterator it = read_attr.find(attr); |
---|
| 3000 | if (it != read_attr.end()) { |
---|
| 3001 | std::ostringstream msg; |
---|
| 3002 | msg << "Multiple occurence of attribute: " << attr; |
---|
| 3003 | throw FormatError(msg.str()); |
---|
| 3004 | } |
---|
| 3005 | read_attr.insert(attr); |
---|
| 3006 | } |
---|
| 3007 | |
---|
| 3008 | { |
---|
| 3009 | typename Attributes::iterator it = _attributes.lower_bound(attr); |
---|
| 3010 | while (it != _attributes.end() && it->first == attr) { |
---|
| 3011 | it->second->set(token); |
---|
| 3012 | ++it; |
---|
| 3013 | } |
---|
| 3014 | } |
---|
| 3015 | |
---|
| 3016 | } |
---|
| 3017 | if (readSuccess()) { |
---|
| 3018 | line.putback(c); |
---|
| 3019 | } |
---|
| 3020 | for (typename Attributes::iterator it = _attributes.begin(); |
---|
| 3021 | it != _attributes.end(); ++it) { |
---|
| 3022 | if (read_attr.find(it->first) == read_attr.end()) { |
---|
| 3023 | std::ostringstream msg; |
---|
| 3024 | msg << "Attribute not found: " << it->first; |
---|
| 3025 | throw FormatError(msg.str()); |
---|
| 3026 | } |
---|
| 3027 | } |
---|
| 3028 | } |
---|
| 3029 | |
---|
| 3030 | public: |
---|
| 3031 | |
---|
| 3032 | /// \name Execution of the Reader |
---|
| 3033 | /// @{ |
---|
| 3034 | |
---|
| 3035 | /// \brief Start the batch processing |
---|
| 3036 | /// |
---|
| 3037 | /// This function starts the batch processing |
---|
| 3038 | void run() { |
---|
| 3039 | |
---|
| 3040 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
---|
| 3041 | |
---|
| 3042 | bool red_nodes_done = _skip_nodes; |
---|
| 3043 | bool blue_nodes_done = _skip_nodes; |
---|
| 3044 | bool edges_done = _skip_edges; |
---|
| 3045 | bool attributes_done = false; |
---|
| 3046 | |
---|
| 3047 | line_num = 0; |
---|
| 3048 | readLine(); |
---|
| 3049 | skipSection(); |
---|
| 3050 | |
---|
| 3051 | while (readSuccess()) { |
---|
| 3052 | try { |
---|
| 3053 | char c; |
---|
| 3054 | std::string section, caption; |
---|
| 3055 | line >> c; |
---|
| 3056 | _reader_bits::readToken(line, section); |
---|
| 3057 | _reader_bits::readToken(line, caption); |
---|
| 3058 | |
---|
| 3059 | if (line >> c) |
---|
| 3060 | throw FormatError("Extra character at the end of line"); |
---|
| 3061 | |
---|
| 3062 | if (section == "red_nodes" && !red_nodes_done) { |
---|
| 3063 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
---|
| 3064 | readRedNodes(); |
---|
| 3065 | red_nodes_done = true; |
---|
| 3066 | } |
---|
| 3067 | } else if (section == "blue_nodes" && !blue_nodes_done) { |
---|
| 3068 | if (_nodes_caption.empty() || _nodes_caption == caption) { |
---|
| 3069 | readBlueNodes(); |
---|
| 3070 | blue_nodes_done = true; |
---|
| 3071 | } |
---|
| 3072 | } else if ((section == "edges" || section == "arcs") && |
---|
| 3073 | !edges_done) { |
---|
| 3074 | if (_edges_caption.empty() || _edges_caption == caption) { |
---|
| 3075 | readEdges(); |
---|
| 3076 | edges_done = true; |
---|
| 3077 | } |
---|
| 3078 | } else if (section == "attributes" && !attributes_done) { |
---|
| 3079 | if (_attributes_caption.empty() || _attributes_caption == caption) { |
---|
| 3080 | readAttributes(); |
---|
| 3081 | attributes_done = true; |
---|
| 3082 | } |
---|
| 3083 | } else { |
---|
| 3084 | readLine(); |
---|
| 3085 | skipSection(); |
---|
| 3086 | } |
---|
| 3087 | } catch (FormatError& error) { |
---|
| 3088 | error.line(line_num); |
---|
| 3089 | error.file(_filename); |
---|
| 3090 | throw; |
---|
| 3091 | } |
---|
| 3092 | } |
---|
| 3093 | |
---|
| 3094 | if (!red_nodes_done) { |
---|
| 3095 | throw FormatError("Section @red_nodes not found"); |
---|
| 3096 | } |
---|
| 3097 | |
---|
| 3098 | if (!blue_nodes_done) { |
---|
| 3099 | throw FormatError("Section @blue_nodes not found"); |
---|
| 3100 | } |
---|
| 3101 | |
---|
| 3102 | if (!edges_done) { |
---|
| 3103 | throw FormatError("Section @edges not found"); |
---|
| 3104 | } |
---|
| 3105 | |
---|
| 3106 | if (!attributes_done && !_attributes.empty()) { |
---|
| 3107 | throw FormatError("Section @attributes not found"); |
---|
| 3108 | } |
---|
| 3109 | |
---|
| 3110 | } |
---|
| 3111 | |
---|
| 3112 | /// @} |
---|
| 3113 | |
---|
| 3114 | }; |
---|
| 3115 | |
---|
| 3116 | /// \ingroup lemon_io |
---|
| 3117 | /// |
---|
| 3118 | /// \brief Return a \ref BpGraphReader class |
---|
| 3119 | /// |
---|
| 3120 | /// This function just returns a \ref BpGraphReader class. |
---|
| 3121 | /// |
---|
| 3122 | /// With this function a graph can be read from an |
---|
| 3123 | /// \ref lgf-format "LGF" file or input stream with several maps and |
---|
| 3124 | /// attributes. For example, there is bipartite weighted matching problem |
---|
| 3125 | /// on a graph, i.e. a graph with a \e weight map on the edges. This |
---|
| 3126 | /// graph can be read with the following code: |
---|
| 3127 | /// |
---|
| 3128 | ///\code |
---|
| 3129 | ///ListBpGraph graph; |
---|
| 3130 | ///ListBpGraph::EdgeMap<int> weight(graph); |
---|
| 3131 | ///bpGraphReader(graph, std::cin). |
---|
| 3132 | /// edgeMap("weight", weight). |
---|
| 3133 | /// run(); |
---|
| 3134 | ///\endcode |
---|
| 3135 | /// |
---|
| 3136 | /// For a complete documentation, please see the \ref BpGraphReader |
---|
| 3137 | /// class documentation. |
---|
| 3138 | /// \warning Don't forget to put the \ref BpGraphReader::run() "run()" |
---|
| 3139 | /// to the end of the parameter list. |
---|
| 3140 | /// \relates BpGraphReader |
---|
| 3141 | /// \sa bpGraphReader(TBGR& graph, const std::string& fn) |
---|
| 3142 | /// \sa bpGraphReader(TBGR& graph, const char* fn) |
---|
| 3143 | template <typename TBGR> |
---|
| 3144 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, std::istream& is) { |
---|
| 3145 | BpGraphReader<TBGR> tmp(graph, is); |
---|
| 3146 | return tmp; |
---|
| 3147 | } |
---|
| 3148 | |
---|
| 3149 | /// \brief Return a \ref BpGraphReader class |
---|
| 3150 | /// |
---|
| 3151 | /// This function just returns a \ref BpGraphReader class. |
---|
| 3152 | /// \relates BpGraphReader |
---|
| 3153 | /// \sa bpGraphReader(TBGR& graph, std::istream& is) |
---|
| 3154 | template <typename TBGR> |
---|
| 3155 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const std::string& fn) { |
---|
| 3156 | BpGraphReader<TBGR> tmp(graph, fn); |
---|
| 3157 | return tmp; |
---|
| 3158 | } |
---|
| 3159 | |
---|
| 3160 | /// \brief Return a \ref BpGraphReader class |
---|
| 3161 | /// |
---|
| 3162 | /// This function just returns a \ref BpGraphReader class. |
---|
| 3163 | /// \relates BpGraphReader |
---|
| 3164 | /// \sa bpGraphReader(TBGR& graph, std::istream& is) |
---|
| 3165 | template <typename TBGR> |
---|
| 3166 | BpGraphReader<TBGR> bpGraphReader(TBGR& graph, const char* fn) { |
---|
| 3167 | BpGraphReader<TBGR> tmp(graph, fn); |
---|
| 3168 | return tmp; |
---|
| 3169 | } |
---|
| 3170 | |
---|
[190] | 3171 | class SectionReader; |
---|
| 3172 | |
---|
| 3173 | SectionReader sectionReader(std::istream& is); |
---|
| 3174 | SectionReader sectionReader(const std::string& fn); |
---|
| 3175 | SectionReader sectionReader(const char* fn); |
---|
[209] | 3176 | |
---|
[192] | 3177 | /// \ingroup lemon_io |
---|
| 3178 | /// |
---|
[189] | 3179 | /// \brief Section reader class |
---|
| 3180 | /// |
---|
[209] | 3181 | /// In the \ref lgf-format "LGF" file extra sections can be placed, |
---|
[192] | 3182 | /// which contain any data in arbitrary format. Such sections can be |
---|
[209] | 3183 | /// read with this class. A reading rule can be added to the class |
---|
[192] | 3184 | /// with two different functions. With the \c sectionLines() function a |
---|
| 3185 | /// functor can process the section line-by-line, while with the \c |
---|
[189] | 3186 | /// sectionStream() member the section can be read from an input |
---|
| 3187 | /// stream. |
---|
| 3188 | class SectionReader { |
---|
| 3189 | private: |
---|
[209] | 3190 | |
---|
[189] | 3191 | std::istream* _is; |
---|
| 3192 | bool local_is; |
---|
[290] | 3193 | std::string _filename; |
---|
[189] | 3194 | |
---|
| 3195 | typedef std::map<std::string, _reader_bits::Section*> Sections; |
---|
| 3196 | Sections _sections; |
---|
| 3197 | |
---|
| 3198 | int line_num; |
---|
| 3199 | std::istringstream line; |
---|
| 3200 | |
---|
| 3201 | public: |
---|
| 3202 | |
---|
| 3203 | /// \brief Constructor |
---|
| 3204 | /// |
---|
| 3205 | /// Construct a section reader, which reads from the given input |
---|
| 3206 | /// stream. |
---|
[209] | 3207 | SectionReader(std::istream& is) |
---|
[189] | 3208 | : _is(&is), local_is(false) {} |
---|
| 3209 | |
---|
| 3210 | /// \brief Constructor |
---|
| 3211 | /// |
---|
| 3212 | /// Construct a section reader, which reads from the given file. |
---|
[209] | 3213 | SectionReader(const std::string& fn) |
---|
[290] | 3214 | : _is(new std::ifstream(fn.c_str())), local_is(true), |
---|
| 3215 | _filename(fn) { |
---|
[295] | 3216 | if (!(*_is)) { |
---|
| 3217 | delete _is; |
---|
| 3218 | throw IoError("Cannot open file", fn); |
---|
| 3219 | } |
---|
[290] | 3220 | } |
---|
[209] | 3221 | |
---|
[189] | 3222 | /// \brief Constructor |
---|
| 3223 | /// |
---|
| 3224 | /// Construct a section reader, which reads from the given file. |
---|
[209] | 3225 | SectionReader(const char* fn) |
---|
[290] | 3226 | : _is(new std::ifstream(fn)), local_is(true), |
---|
| 3227 | _filename(fn) { |
---|
[295] | 3228 | if (!(*_is)) { |
---|
| 3229 | delete _is; |
---|
| 3230 | throw IoError("Cannot open file", fn); |
---|
| 3231 | } |
---|
[290] | 3232 | } |
---|
[189] | 3233 | |
---|
| 3234 | /// \brief Destructor |
---|
| 3235 | ~SectionReader() { |
---|
[209] | 3236 | for (Sections::iterator it = _sections.begin(); |
---|
| 3237 | it != _sections.end(); ++it) { |
---|
| 3238 | delete it->second; |
---|
[189] | 3239 | } |
---|
| 3240 | |
---|
| 3241 | if (local_is) { |
---|
[209] | 3242 | delete _is; |
---|
[189] | 3243 | } |
---|
| 3244 | |
---|
| 3245 | } |
---|
| 3246 | |
---|
| 3247 | private: |
---|
[190] | 3248 | |
---|
| 3249 | friend SectionReader sectionReader(std::istream& is); |
---|
| 3250 | friend SectionReader sectionReader(const std::string& fn); |
---|
| 3251 | friend SectionReader sectionReader(const char* fn); |
---|
| 3252 | |
---|
[209] | 3253 | SectionReader(SectionReader& other) |
---|
[190] | 3254 | : _is(other._is), local_is(other.local_is) { |
---|
| 3255 | |
---|
| 3256 | other._is = 0; |
---|
| 3257 | other.local_is = false; |
---|
[209] | 3258 | |
---|
[190] | 3259 | _sections.swap(other._sections); |
---|
| 3260 | } |
---|
[209] | 3261 | |
---|
[189] | 3262 | SectionReader& operator=(const SectionReader&); |
---|
| 3263 | |
---|
| 3264 | public: |
---|
| 3265 | |
---|
[631] | 3266 | /// \name Section Readers |
---|
[189] | 3267 | /// @{ |
---|
| 3268 | |
---|
| 3269 | /// \brief Add a section processor with line oriented reading |
---|
| 3270 | /// |
---|
| 3271 | /// The first parameter is the type descriptor of the section, the |
---|
| 3272 | /// second is a functor, which takes just one \c std::string |
---|
| 3273 | /// parameter. At the reading process, each line of the section |
---|
| 3274 | /// will be given to the functor object. However, the empty lines |
---|
| 3275 | /// and the comment lines are filtered out, and the leading |
---|
| 3276 | /// whitespaces are trimmed from each processed string. |
---|
| 3277 | /// |
---|
[833] | 3278 | /// For example, let's see a section, which contain several |
---|
[189] | 3279 | /// integers, which should be inserted into a vector. |
---|
| 3280 | ///\code |
---|
| 3281 | /// @numbers |
---|
| 3282 | /// 12 45 23 |
---|
| 3283 | /// 4 |
---|
| 3284 | /// 23 6 |
---|
| 3285 | ///\endcode |
---|
| 3286 | /// |
---|
[192] | 3287 | /// The functor is implemented as a struct: |
---|
[189] | 3288 | ///\code |
---|
| 3289 | /// struct NumberSection { |
---|
| 3290 | /// std::vector<int>& _data; |
---|
| 3291 | /// NumberSection(std::vector<int>& data) : _data(data) {} |
---|
| 3292 | /// void operator()(const std::string& line) { |
---|
| 3293 | /// std::istringstream ls(line); |
---|
| 3294 | /// int value; |
---|
| 3295 | /// while (ls >> value) _data.push_back(value); |
---|
| 3296 | /// } |
---|
| 3297 | /// }; |
---|
| 3298 | /// |
---|
| 3299 | /// // ... |
---|
| 3300 | /// |
---|
[209] | 3301 | /// reader.sectionLines("numbers", NumberSection(vec)); |
---|
[189] | 3302 | ///\endcode |
---|
| 3303 | template <typename Functor> |
---|
| 3304 | SectionReader& sectionLines(const std::string& type, Functor functor) { |
---|
[192] | 3305 | LEMON_ASSERT(!type.empty(), "Type is empty."); |
---|
[209] | 3306 | LEMON_ASSERT(_sections.find(type) == _sections.end(), |
---|
| 3307 | "Multiple reading of section."); |
---|
| 3308 | _sections.insert(std::make_pair(type, |
---|
[189] | 3309 | new _reader_bits::LineSection<Functor>(functor))); |
---|
| 3310 | return *this; |
---|
| 3311 | } |
---|
| 3312 | |
---|
| 3313 | |
---|
| 3314 | /// \brief Add a section processor with stream oriented reading |
---|
| 3315 | /// |
---|
| 3316 | /// The first parameter is the type of the section, the second is |
---|
[192] | 3317 | /// a functor, which takes an \c std::istream& and an \c int& |
---|
[189] | 3318 | /// parameter, the latter regard to the line number of stream. The |
---|
| 3319 | /// functor can read the input while the section go on, and the |
---|
| 3320 | /// line number should be modified accordingly. |
---|
| 3321 | template <typename Functor> |
---|
| 3322 | SectionReader& sectionStream(const std::string& type, Functor functor) { |
---|
[192] | 3323 | LEMON_ASSERT(!type.empty(), "Type is empty."); |
---|
[209] | 3324 | LEMON_ASSERT(_sections.find(type) == _sections.end(), |
---|
| 3325 | "Multiple reading of section."); |
---|
| 3326 | _sections.insert(std::make_pair(type, |
---|
| 3327 | new _reader_bits::StreamSection<Functor>(functor))); |
---|
[189] | 3328 | return *this; |
---|
[209] | 3329 | } |
---|
| 3330 | |
---|
[189] | 3331 | /// @} |
---|
| 3332 | |
---|
| 3333 | private: |
---|
| 3334 | |
---|
| 3335 | bool readLine() { |
---|
| 3336 | std::string str; |
---|
| 3337 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 3338 | line.clear(); line.str(str); |
---|
| 3339 | char c; |
---|
| 3340 | if (line >> std::ws >> c && c != '#') { |
---|
| 3341 | line.putback(c); |
---|
| 3342 | return true; |
---|
| 3343 | } |
---|
[189] | 3344 | } |
---|
| 3345 | return false; |
---|
| 3346 | } |
---|
| 3347 | |
---|
| 3348 | bool readSuccess() { |
---|
| 3349 | return static_cast<bool>(*_is); |
---|
| 3350 | } |
---|
[209] | 3351 | |
---|
[189] | 3352 | void skipSection() { |
---|
| 3353 | char c; |
---|
| 3354 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 3355 | readLine(); |
---|
[189] | 3356 | } |
---|
[446] | 3357 | if (readSuccess()) { |
---|
| 3358 | line.putback(c); |
---|
| 3359 | } |
---|
[189] | 3360 | } |
---|
| 3361 | |
---|
| 3362 | public: |
---|
| 3363 | |
---|
| 3364 | |
---|
[631] | 3365 | /// \name Execution of the Reader |
---|
[189] | 3366 | /// @{ |
---|
| 3367 | |
---|
| 3368 | /// \brief Start the batch processing |
---|
| 3369 | /// |
---|
[192] | 3370 | /// This function starts the batch processing. |
---|
[189] | 3371 | void run() { |
---|
[209] | 3372 | |
---|
[189] | 3373 | LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
---|
[209] | 3374 | |
---|
[189] | 3375 | std::set<std::string> extra_sections; |
---|
| 3376 | |
---|
[209] | 3377 | line_num = 0; |
---|
[189] | 3378 | readLine(); |
---|
| 3379 | skipSection(); |
---|
| 3380 | |
---|
| 3381 | while (readSuccess()) { |
---|
[209] | 3382 | try { |
---|
| 3383 | char c; |
---|
| 3384 | std::string section, caption; |
---|
| 3385 | line >> c; |
---|
| 3386 | _reader_bits::readToken(line, section); |
---|
| 3387 | _reader_bits::readToken(line, caption); |
---|
| 3388 | |
---|
| 3389 | if (line >> c) |
---|
[291] | 3390 | throw FormatError("Extra character at the end of line"); |
---|
[209] | 3391 | |
---|
| 3392 | if (extra_sections.find(section) != extra_sections.end()) { |
---|
| 3393 | std::ostringstream msg; |
---|
[291] | 3394 | msg << "Multiple occurence of section: " << section; |
---|
[290] | 3395 | throw FormatError(msg.str()); |
---|
[209] | 3396 | } |
---|
| 3397 | Sections::iterator it = _sections.find(section); |
---|
| 3398 | if (it != _sections.end()) { |
---|
| 3399 | extra_sections.insert(section); |
---|
| 3400 | it->second->process(*_is, line_num); |
---|
| 3401 | } |
---|
| 3402 | readLine(); |
---|
| 3403 | skipSection(); |
---|
[290] | 3404 | } catch (FormatError& error) { |
---|
[209] | 3405 | error.line(line_num); |
---|
[290] | 3406 | error.file(_filename); |
---|
[209] | 3407 | throw; |
---|
| 3408 | } |
---|
[189] | 3409 | } |
---|
| 3410 | for (Sections::iterator it = _sections.begin(); |
---|
[209] | 3411 | it != _sections.end(); ++it) { |
---|
| 3412 | if (extra_sections.find(it->first) == extra_sections.end()) { |
---|
| 3413 | std::ostringstream os; |
---|
| 3414 | os << "Cannot find section: " << it->first; |
---|
[290] | 3415 | throw FormatError(os.str()); |
---|
[209] | 3416 | } |
---|
[189] | 3417 | } |
---|
| 3418 | } |
---|
| 3419 | |
---|
| 3420 | /// @} |
---|
[209] | 3421 | |
---|
[189] | 3422 | }; |
---|
| 3423 | |
---|
[645] | 3424 | /// \ingroup lemon_io |
---|
| 3425 | /// |
---|
| 3426 | /// \brief Return a \ref SectionReader class |
---|
| 3427 | /// |
---|
| 3428 | /// This function just returns a \ref SectionReader class. |
---|
| 3429 | /// |
---|
| 3430 | /// Please see SectionReader documentation about the custom section |
---|
| 3431 | /// input. |
---|
| 3432 | /// |
---|
| 3433 | /// \relates SectionReader |
---|
| 3434 | /// \sa sectionReader(const std::string& fn) |
---|
| 3435 | /// \sa sectionReader(const char *fn) |
---|
| 3436 | inline SectionReader sectionReader(std::istream& is) { |
---|
| 3437 | SectionReader tmp(is); |
---|
| 3438 | return tmp; |
---|
| 3439 | } |
---|
| 3440 | |
---|
[192] | 3441 | /// \brief Return a \ref SectionReader class |
---|
[209] | 3442 | /// |
---|
[192] | 3443 | /// This function just returns a \ref SectionReader class. |
---|
[189] | 3444 | /// \relates SectionReader |
---|
[645] | 3445 | /// \sa sectionReader(std::istream& is) |
---|
| 3446 | inline SectionReader sectionReader(const std::string& fn) { |
---|
| 3447 | SectionReader tmp(fn); |
---|
[189] | 3448 | return tmp; |
---|
| 3449 | } |
---|
| 3450 | |
---|
[192] | 3451 | /// \brief Return a \ref SectionReader class |
---|
[209] | 3452 | /// |
---|
[192] | 3453 | /// This function just returns a \ref SectionReader class. |
---|
[189] | 3454 | /// \relates SectionReader |
---|
[645] | 3455 | /// \sa sectionReader(std::istream& is) |
---|
[189] | 3456 | inline SectionReader sectionReader(const char* fn) { |
---|
| 3457 | SectionReader tmp(fn); |
---|
| 3458 | return tmp; |
---|
| 3459 | } |
---|
| 3460 | |
---|
[173] | 3461 | /// \ingroup lemon_io |
---|
| 3462 | /// |
---|
[209] | 3463 | /// \brief Reader for the contents of the \ref lgf-format "LGF" file |
---|
[173] | 3464 | /// |
---|
| 3465 | /// This class can be used to read the sections, the map names and |
---|
[236] | 3466 | /// the attributes from a file. Usually, the LEMON programs know |
---|
[173] | 3467 | /// that, which type of graph, which maps and which attributes |
---|
| 3468 | /// should be read from a file, but in general tools (like glemon) |
---|
[179] | 3469 | /// the contents of an LGF file should be guessed somehow. This class |
---|
[173] | 3470 | /// reads the graph and stores the appropriate information for |
---|
| 3471 | /// reading the graph. |
---|
| 3472 | /// |
---|
[209] | 3473 | ///\code |
---|
| 3474 | /// LgfContents contents("graph.lgf"); |
---|
[179] | 3475 | /// contents.run(); |
---|
[173] | 3476 | /// |
---|
[192] | 3477 | /// // Does it contain any node section and arc section? |
---|
[179] | 3478 | /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) { |
---|
[192] | 3479 | /// std::cerr << "Failure, cannot find graph." << std::endl; |
---|
[173] | 3480 | /// return -1; |
---|
| 3481 | /// } |
---|
[209] | 3482 | /// std::cout << "The name of the default node section: " |
---|
[179] | 3483 | /// << contents.nodeSection(0) << std::endl; |
---|
[209] | 3484 | /// std::cout << "The number of the arc maps: " |
---|
[179] | 3485 | /// << contents.arcMaps(0).size() << std::endl; |
---|
[209] | 3486 | /// std::cout << "The name of second arc map: " |
---|
[179] | 3487 | /// << contents.arcMaps(0)[1] << std::endl; |
---|
[173] | 3488 | ///\endcode |
---|
[209] | 3489 | class LgfContents { |
---|
[173] | 3490 | private: |
---|
| 3491 | |
---|
| 3492 | std::istream* _is; |
---|
| 3493 | bool local_is; |
---|
| 3494 | |
---|
| 3495 | std::vector<std::string> _node_sections; |
---|
| 3496 | std::vector<std::string> _edge_sections; |
---|
| 3497 | std::vector<std::string> _attribute_sections; |
---|
| 3498 | std::vector<std::string> _extra_sections; |
---|
| 3499 | |
---|
| 3500 | std::vector<bool> _arc_sections; |
---|
| 3501 | |
---|
| 3502 | std::vector<std::vector<std::string> > _node_maps; |
---|
| 3503 | std::vector<std::vector<std::string> > _edge_maps; |
---|
| 3504 | |
---|
| 3505 | std::vector<std::vector<std::string> > _attributes; |
---|
| 3506 | |
---|
| 3507 | |
---|
| 3508 | int line_num; |
---|
| 3509 | std::istringstream line; |
---|
[209] | 3510 | |
---|
[173] | 3511 | public: |
---|
| 3512 | |
---|
| 3513 | /// \brief Constructor |
---|
| 3514 | /// |
---|
[179] | 3515 | /// Construct an \e LGF contents reader, which reads from the given |
---|
[173] | 3516 | /// input stream. |
---|
[209] | 3517 | LgfContents(std::istream& is) |
---|
[173] | 3518 | : _is(&is), local_is(false) {} |
---|
| 3519 | |
---|
| 3520 | /// \brief Constructor |
---|
| 3521 | /// |
---|
[179] | 3522 | /// Construct an \e LGF contents reader, which reads from the given |
---|
[173] | 3523 | /// file. |
---|
[209] | 3524 | LgfContents(const std::string& fn) |
---|
[290] | 3525 | : _is(new std::ifstream(fn.c_str())), local_is(true) { |
---|
[295] | 3526 | if (!(*_is)) { |
---|
| 3527 | delete _is; |
---|
| 3528 | throw IoError("Cannot open file", fn); |
---|
| 3529 | } |
---|
[290] | 3530 | } |
---|
[173] | 3531 | |
---|
| 3532 | /// \brief Constructor |
---|
| 3533 | /// |
---|
[179] | 3534 | /// Construct an \e LGF contents reader, which reads from the given |
---|
[173] | 3535 | /// file. |
---|
[179] | 3536 | LgfContents(const char* fn) |
---|
[290] | 3537 | : _is(new std::ifstream(fn)), local_is(true) { |
---|
[295] | 3538 | if (!(*_is)) { |
---|
| 3539 | delete _is; |
---|
| 3540 | throw IoError("Cannot open file", fn); |
---|
| 3541 | } |
---|
[290] | 3542 | } |
---|
[209] | 3543 | |
---|
[173] | 3544 | /// \brief Destructor |
---|
[179] | 3545 | ~LgfContents() { |
---|
[173] | 3546 | if (local_is) delete _is; |
---|
| 3547 | } |
---|
| 3548 | |
---|
[190] | 3549 | private: |
---|
[209] | 3550 | |
---|
[190] | 3551 | LgfContents(const LgfContents&); |
---|
| 3552 | LgfContents& operator=(const LgfContents&); |
---|
| 3553 | |
---|
| 3554 | public: |
---|
| 3555 | |
---|
[173] | 3556 | |
---|
[631] | 3557 | /// \name Node Sections |
---|
[173] | 3558 | /// @{ |
---|
| 3559 | |
---|
| 3560 | /// \brief Gives back the number of node sections in the file. |
---|
| 3561 | /// |
---|
| 3562 | /// Gives back the number of node sections in the file. |
---|
| 3563 | int nodeSectionNum() const { |
---|
| 3564 | return _node_sections.size(); |
---|
| 3565 | } |
---|
| 3566 | |
---|
[209] | 3567 | /// \brief Returns the node section name at the given position. |
---|
[173] | 3568 | /// |
---|
[209] | 3569 | /// Returns the node section name at the given position. |
---|
[173] | 3570 | const std::string& nodeSection(int i) const { |
---|
| 3571 | return _node_sections[i]; |
---|
| 3572 | } |
---|
| 3573 | |
---|
| 3574 | /// \brief Gives back the node maps for the given section. |
---|
| 3575 | /// |
---|
| 3576 | /// Gives back the node maps for the given section. |
---|
[182] | 3577 | const std::vector<std::string>& nodeMapNames(int i) const { |
---|
[173] | 3578 | return _node_maps[i]; |
---|
| 3579 | } |
---|
| 3580 | |
---|
| 3581 | /// @} |
---|
| 3582 | |
---|
[631] | 3583 | /// \name Arc/Edge Sections |
---|
[173] | 3584 | /// @{ |
---|
| 3585 | |
---|
[181] | 3586 | /// \brief Gives back the number of arc/edge sections in the file. |
---|
[173] | 3587 | /// |
---|
[181] | 3588 | /// Gives back the number of arc/edge sections in the file. |
---|
| 3589 | /// \note It is synonym of \c edgeSectionNum(). |
---|
[173] | 3590 | int arcSectionNum() const { |
---|
| 3591 | return _edge_sections.size(); |
---|
| 3592 | } |
---|
| 3593 | |
---|
[209] | 3594 | /// \brief Returns the arc/edge section name at the given position. |
---|
[173] | 3595 | /// |
---|
[209] | 3596 | /// Returns the arc/edge section name at the given position. |
---|
[181] | 3597 | /// \note It is synonym of \c edgeSection(). |
---|
[173] | 3598 | const std::string& arcSection(int i) const { |
---|
| 3599 | return _edge_sections[i]; |
---|
| 3600 | } |
---|
| 3601 | |
---|
[181] | 3602 | /// \brief Gives back the arc/edge maps for the given section. |
---|
[173] | 3603 | /// |
---|
[181] | 3604 | /// Gives back the arc/edge maps for the given section. |
---|
[182] | 3605 | /// \note It is synonym of \c edgeMapNames(). |
---|
| 3606 | const std::vector<std::string>& arcMapNames(int i) const { |
---|
[173] | 3607 | return _edge_maps[i]; |
---|
| 3608 | } |
---|
| 3609 | |
---|
| 3610 | /// @} |
---|
| 3611 | |
---|
[181] | 3612 | /// \name Synonyms |
---|
[173] | 3613 | /// @{ |
---|
| 3614 | |
---|
[181] | 3615 | /// \brief Gives back the number of arc/edge sections in the file. |
---|
[173] | 3616 | /// |
---|
[181] | 3617 | /// Gives back the number of arc/edge sections in the file. |
---|
| 3618 | /// \note It is synonym of \c arcSectionNum(). |
---|
[173] | 3619 | int edgeSectionNum() const { |
---|
| 3620 | return _edge_sections.size(); |
---|
| 3621 | } |
---|
| 3622 | |
---|
[209] | 3623 | /// \brief Returns the section name at the given position. |
---|
[173] | 3624 | /// |
---|
[209] | 3625 | /// Returns the section name at the given position. |
---|
[181] | 3626 | /// \note It is synonym of \c arcSection(). |
---|
[173] | 3627 | const std::string& edgeSection(int i) const { |
---|
| 3628 | return _edge_sections[i]; |
---|
| 3629 | } |
---|
| 3630 | |
---|
| 3631 | /// \brief Gives back the edge maps for the given section. |
---|
| 3632 | /// |
---|
| 3633 | /// Gives back the edge maps for the given section. |
---|
[182] | 3634 | /// \note It is synonym of \c arcMapNames(). |
---|
| 3635 | const std::vector<std::string>& edgeMapNames(int i) const { |
---|
[173] | 3636 | return _edge_maps[i]; |
---|
| 3637 | } |
---|
| 3638 | |
---|
| 3639 | /// @} |
---|
| 3640 | |
---|
[631] | 3641 | /// \name Attribute Sections |
---|
[173] | 3642 | /// @{ |
---|
| 3643 | |
---|
| 3644 | /// \brief Gives back the number of attribute sections in the file. |
---|
| 3645 | /// |
---|
| 3646 | /// Gives back the number of attribute sections in the file. |
---|
| 3647 | int attributeSectionNum() const { |
---|
| 3648 | return _attribute_sections.size(); |
---|
| 3649 | } |
---|
| 3650 | |
---|
[209] | 3651 | /// \brief Returns the attribute section name at the given position. |
---|
[173] | 3652 | /// |
---|
[209] | 3653 | /// Returns the attribute section name at the given position. |
---|
[182] | 3654 | const std::string& attributeSectionNames(int i) const { |
---|
[173] | 3655 | return _attribute_sections[i]; |
---|
| 3656 | } |
---|
| 3657 | |
---|
| 3658 | /// \brief Gives back the attributes for the given section. |
---|
| 3659 | /// |
---|
| 3660 | /// Gives back the attributes for the given section. |
---|
| 3661 | const std::vector<std::string>& attributes(int i) const { |
---|
| 3662 | return _attributes[i]; |
---|
| 3663 | } |
---|
| 3664 | |
---|
| 3665 | /// @} |
---|
| 3666 | |
---|
[631] | 3667 | /// \name Extra Sections |
---|
[173] | 3668 | /// @{ |
---|
| 3669 | |
---|
| 3670 | /// \brief Gives back the number of extra sections in the file. |
---|
| 3671 | /// |
---|
| 3672 | /// Gives back the number of extra sections in the file. |
---|
| 3673 | int extraSectionNum() const { |
---|
| 3674 | return _extra_sections.size(); |
---|
| 3675 | } |
---|
| 3676 | |
---|
[209] | 3677 | /// \brief Returns the extra section type at the given position. |
---|
[173] | 3678 | /// |
---|
[209] | 3679 | /// Returns the section type at the given position. |
---|
[173] | 3680 | const std::string& extraSection(int i) const { |
---|
| 3681 | return _extra_sections[i]; |
---|
| 3682 | } |
---|
| 3683 | |
---|
| 3684 | /// @} |
---|
| 3685 | |
---|
| 3686 | private: |
---|
| 3687 | |
---|
| 3688 | bool readLine() { |
---|
| 3689 | std::string str; |
---|
| 3690 | while(++line_num, std::getline(*_is, str)) { |
---|
[209] | 3691 | line.clear(); line.str(str); |
---|
| 3692 | char c; |
---|
| 3693 | if (line >> std::ws >> c && c != '#') { |
---|
| 3694 | line.putback(c); |
---|
| 3695 | return true; |
---|
| 3696 | } |
---|
[173] | 3697 | } |
---|
| 3698 | return false; |
---|
| 3699 | } |
---|
| 3700 | |
---|
| 3701 | bool readSuccess() { |
---|
| 3702 | return static_cast<bool>(*_is); |
---|
| 3703 | } |
---|
| 3704 | |
---|
| 3705 | void skipSection() { |
---|
| 3706 | char c; |
---|
| 3707 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 3708 | readLine(); |
---|
[173] | 3709 | } |
---|
[446] | 3710 | if (readSuccess()) { |
---|
| 3711 | line.putback(c); |
---|
| 3712 | } |
---|
[173] | 3713 | } |
---|
| 3714 | |
---|
| 3715 | void readMaps(std::vector<std::string>& maps) { |
---|
[186] | 3716 | char c; |
---|
| 3717 | if (!readLine() || !(line >> c) || c == '@') { |
---|
[209] | 3718 | if (readSuccess() && line) line.putback(c); |
---|
| 3719 | return; |
---|
[186] | 3720 | } |
---|
| 3721 | line.putback(c); |
---|
[173] | 3722 | std::string map; |
---|
| 3723 | while (_reader_bits::readToken(line, map)) { |
---|
[209] | 3724 | maps.push_back(map); |
---|
[173] | 3725 | } |
---|
| 3726 | } |
---|
| 3727 | |
---|
| 3728 | void readAttributes(std::vector<std::string>& attrs) { |
---|
| 3729 | readLine(); |
---|
| 3730 | char c; |
---|
| 3731 | while (readSuccess() && line >> c && c != '@') { |
---|
[209] | 3732 | line.putback(c); |
---|
| 3733 | std::string attr; |
---|
| 3734 | _reader_bits::readToken(line, attr); |
---|
| 3735 | attrs.push_back(attr); |
---|
| 3736 | readLine(); |
---|
[173] | 3737 | } |
---|
| 3738 | line.putback(c); |
---|
| 3739 | } |
---|
| 3740 | |
---|
| 3741 | public: |
---|
| 3742 | |
---|
[631] | 3743 | /// \name Execution of the Contents Reader |
---|
[173] | 3744 | /// @{ |
---|
| 3745 | |
---|
[192] | 3746 | /// \brief Starts the reading |
---|
[173] | 3747 | /// |
---|
[192] | 3748 | /// This function starts the reading. |
---|
[173] | 3749 | void run() { |
---|
| 3750 | |
---|
| 3751 | readLine(); |
---|
| 3752 | skipSection(); |
---|
| 3753 | |
---|
| 3754 | while (readSuccess()) { |
---|
| 3755 | |
---|
[209] | 3756 | char c; |
---|
| 3757 | line >> c; |
---|
| 3758 | |
---|
| 3759 | std::string section, caption; |
---|
| 3760 | _reader_bits::readToken(line, section); |
---|
| 3761 | _reader_bits::readToken(line, caption); |
---|
| 3762 | |
---|
| 3763 | if (section == "nodes") { |
---|
| 3764 | _node_sections.push_back(caption); |
---|
| 3765 | _node_maps.push_back(std::vector<std::string>()); |
---|
| 3766 | readMaps(_node_maps.back()); |
---|
| 3767 | readLine(); skipSection(); |
---|
| 3768 | } else if (section == "arcs" || section == "edges") { |
---|
| 3769 | _edge_sections.push_back(caption); |
---|
| 3770 | _arc_sections.push_back(section == "arcs"); |
---|
| 3771 | _edge_maps.push_back(std::vector<std::string>()); |
---|
| 3772 | readMaps(_edge_maps.back()); |
---|
| 3773 | readLine(); skipSection(); |
---|
| 3774 | } else if (section == "attributes") { |
---|
| 3775 | _attribute_sections.push_back(caption); |
---|
| 3776 | _attributes.push_back(std::vector<std::string>()); |
---|
| 3777 | readAttributes(_attributes.back()); |
---|
| 3778 | } else { |
---|
| 3779 | _extra_sections.push_back(section); |
---|
| 3780 | readLine(); skipSection(); |
---|
| 3781 | } |
---|
[173] | 3782 | } |
---|
| 3783 | } |
---|
| 3784 | |
---|
| 3785 | /// @} |
---|
[209] | 3786 | |
---|
[173] | 3787 | }; |
---|
[127] | 3788 | } |
---|
| 3789 | |
---|
| 3790 | #endif |
---|