Changeset 1333:2640cf6547ff in lemon-0.x for src/lemon/graph_reader.h
- Timestamp:
- 04/09/05 21:35:33 (19 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1772
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/lemon/graph_reader.h
r1311 r1333 35 35 namespace lemon { 36 36 37 /// \addtogroup io_group 38 /// @{ 37 39 38 40 /// \brief Standard ReaderTraits for the GraphReader class. … … 40 42 /// Standard ReaderTraits for the GraphReader class. 41 43 /// It defines standard reading method for all type of value. 44 /// \author Balazs Dezso 42 45 struct DefaultReaderTraits { 43 46 44 /// \brief Template class for reading an value.47 /// \brief Template class for reading an value. 45 48 /// 46 49 /// Template class for reading an value. 50 /// \author Balazs Dezso 47 51 template <typename _Value> 48 52 struct Reader { … … 58 62 }; 59 63 64 /// \brief Returns wheter this name is an ID map name. 65 /// 66 /// Returns wheter this name is an ID map name. 67 static bool idMapName(const std::string& name) { 68 return name == "id"; 69 } 70 60 71 /// The reader class for the not needed maps. 61 72 typedef Reader<std::string> DefaultReader; … … 67 78 /// Reader class for quoted strings. It can process the escape 68 79 /// sequences in the string. 80 /// \author Balazs Dezso 69 81 class QuotedStringReader { 70 82 public: … … 172 184 /// \brief The graph reader class. 173 185 /// 174 /// \ingroup io_group 175 /// The reader class for the graph input. 186 /// 187 /// The given file format may contain several maps and labeled nodes or 188 /// edges. 189 /// 190 /// If you read a graph you need not read all the maps and items just those 191 /// that you need. The interface of the \c GraphReader is very similar to 192 /// the GraphWriter but the reading method does not depend on the order the 193 /// given commands. 194 /// 195 /// The reader object suppose that each not readed value does not contain 196 /// whitespaces, therefore it has some extra possibilities to control how 197 /// it should skip the values when the string representation contains spaces. 198 /// 199 /// \code 200 /// GraphReader<ListGraph> reader(std::cin, graph); 201 /// \endcode 202 /// 203 /// The \c addNodeMap() function reads a map from the \c \@nodeset section. 204 /// If there is a map that you do not want to read from the file and there is 205 /// whitespace in the string represenation of the values then you should 206 /// call the \c skipNodeMap() template member function with proper 207 /// parameters. 208 /// 209 /// \code 210 /// reader.addNodeMap("x-coord", xCoordMap); 211 /// reader.addNodeMap("y-coord", yCoordMap); 212 /// 213 /// reader.addNodeMap<QuotedStringReader>("label", labelMap); 214 /// reader.skipNodeMap<QuotedStringReader>("description"); 215 /// 216 /// reader.addNodeMap("color", colorMap); 217 /// \endcode 218 /// 219 /// With the \c addEdgeMap() member function you can give an edge map 220 /// reading command similar to the NodeMaps. 221 /// 222 /// \code 223 /// reader.addEdgeMap("weight", weightMap); 224 /// reader.addEdgeMap("label", labelMap); 225 /// \endcode 226 /// 227 /// With \c addNode() and \c addEdge() functions you can read labeled Nodes 228 /// and Edges. 229 /// 230 /// \code 231 /// reader.addNode("source", sourceNode); 232 /// reader.addNode("target", targetNode); 233 /// 234 /// reader.addEdge("observed", edge); 235 /// \endcode 236 /// 237 /// After you give all read commands you must call the \c run() member 238 /// function, which execute all the commands. 239 /// 240 /// \code 241 /// reader.run(); 242 /// \endcode 243 /// 176 244 /// \see DefaultReaderTraits 177 245 /// \see QuotedStringReader 178 246 /// \see \ref GraphWriter 179 247 /// \see \ref graph-io-page 248 /// \author Balazs Dezso 180 249 template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 181 250 class GraphReader { … … 201 270 /// Destruct the graph reader. 202 271 ~GraphReader() { 203 204 272 for (typename NodeMapReaders::iterator it = node_map_readers.begin(); 205 273 it != node_map_readers.end(); ++it) { … … 365 433 std::istringstream ls(line); 366 434 while (ls >> id) { 367 if (id[0] == '#') break;368 435 typename NodeMapReaders::iterator it = node_map_readers.find(id); 369 436 if (it != node_map_readers.end()) { … … 373 440 index.push_back(&nodeSkipper); 374 441 } 375 } 376 } 377 378 if (index.size() == 0) { 379 throw DataFormatError("Cannot find node id map"); 380 } 381 382 nodeInverter = 383 std::auto_ptr<InverterBase<Node> >(index[0]->getInverter()); 442 if (ReaderTraits::idMapName(id) && nodeInverter.get() == 0) { 443 nodeInverter.reset(index.back()->getInverter()); 444 index.back() = nodeInverter.get(); 445 } 446 } 447 } 448 449 // if (index.size() == 0) { 450 // throw DataFormatError("Cannot find node id map"); 451 // } 452 453 // nodeInverter = 454 // std::auto_ptr<InverterBase<Node> >(index[0]->getInverter()); 384 455 std::string line; 385 456 while (line = readNotEmptyLine(is, line_num), line[0] != '@') { 386 457 Node node = graph.addNode(); 387 458 std::istringstream ls(line); 388 nodeInverter->read(ls, node); 389 for (int i = 1; i < (int)index.size(); ++i) { 459 for (int i = 0; i < (int)index.size(); ++i) { 390 460 index[i]->read(ls, node); 391 461 } … … 403 473 std::istringstream ls(line); 404 474 while (ls >> id) { 405 if (id[0] == '#') break;406 475 typename EdgeMapReaders::iterator it = edge_map_readers.find(id); 407 476 if (it != edge_map_readers.end()) { … … 411 480 index.push_back(&edgeSkipper); 412 481 } 413 } 414 } 415 416 if (index.size() == 0) { 417 throw DataFormatError("Cannot find edge id map"); 418 } 419 420 edgeInverter = 421 std::auto_ptr<InverterBase<Edge> >(index[0]->getInverter()); 482 if (ReaderTraits::idMapName(id) && edgeInverter.get() == 0) { 483 edgeInverter.reset(index.back()->getInverter()); 484 index.back() = edgeInverter.get(); 485 } 486 } 487 } 488 489 if (nodeInverter.get() == 0) { 490 throw DataFormatError("Cannot find node id map"); 491 } 492 // if (index.size() == 0) { 493 // throw DataFormatError("Cannot find edge id map"); 494 // } 495 496 // edgeInverter = 497 // std::auto_ptr<InverterBase<Edge> >(index[0]->getInverter()); 422 498 std::string line; 423 499 while (line = readNotEmptyLine(is, line_num), line[0] != '@') { … … 426 502 Node target = nodeInverter->read(ls); 427 503 Edge edge = graph.addEdge(source, target); 428 edgeInverter->read(ls, edge); 429 for (int i = 1; i < (int)index.size(); ++i) { 504 for (int i = 0; i < (int)index.size(); ++i) { 430 505 index[i]->read(ls, edge); 431 506 } … … 437 512 std::auto_ptr<InverterBase<Node> >& nodeInverter) { 438 513 std::string line; 514 if (nodeInverter.get() == 0) { 515 throw DataFormatError("Cannot find node id map"); 516 } 439 517 while (line = readNotEmptyLine(is, line_num), line[0] != '@') { 440 518 std::istringstream ls(line); … … 452 530 std::auto_ptr<InverterBase<Edge> >& edgeInverter) { 453 531 std::string line; 532 if (edgeInverter.get() == 0) { 533 throw DataFormatError("Cannot find edge id map"); 534 } 454 535 while (line = readNotEmptyLine(is, line_num), line[0] != '@') { 455 536 std::istringstream ls(line); … … 467 548 std::string line; 468 549 while (++line_num, getline(is, line)) { 469 int vi = line.find_first_not_of(" \t"); 470 if (vi != (int)std::string::npos && line[vi] != '#') { 550 int vi = line.find('#'); 551 if (vi != (int)::std::string::npos) { 552 line = line.substr(0, vi); 553 } 554 vi = line.find_first_not_of(" \t"); 555 if (vi != (int)std::string::npos) { 556 std::cerr << "Line: " << line.substr(vi) << std::endl; 471 557 return line.substr(vi); 472 558 } … … 474 560 throw DataFormatError("End of stream error"); 475 561 } 476 477 // Inverters store and give back the Item from the id,478 // and may put the ids into a map.479 562 480 563 template <typename _Item> 481 class InverterBase { 564 class ReaderBase; 565 566 template <typename _Item> 567 class InverterBase : public ReaderBase<_Item> { 482 568 public: 483 569 typedef _Item Item; 484 570 virtual void read(std::istream&, const Item&) = 0; 485 571 virtual Item read(std::istream&) = 0; 572 573 virtual InverterBase<_Item>* getInverter() { 574 return this; 575 } 486 576 }; 487 577 … … 652 742 }; 653 743 654 /// Ready to use reader function. 744 /// \brief Read a graph from the input. 745 /// 746 /// Read a graph from the input. 747 /// \param is The input stream. 748 /// \param g The graph. 749 /// \param capacity The capacity map. 750 /// \param s The source node. 751 /// \param t The target node. 752 /// \param cost The cost map. 655 753 template<typename Graph, typename CapacityMap, typename CostMap> 656 754 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, … … 665 763 } 666 764 765 /// \brief Read a graph from the input. 766 /// 767 /// Read a graph from the input. 768 /// \param is The input stream. 769 /// \param g The graph. 770 /// \param capacity The capacity map. 771 /// \param s The source node. 772 /// \param t The target node. 667 773 template<typename Graph, typename CapacityMap> 668 774 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, … … 675 781 } 676 782 783 /// \brief Read a graph from the input. 784 /// 785 /// Read a graph from the input. 786 /// \param is The input stream. 787 /// \param g The graph. 788 /// \param capacity The capacity map. 789 /// \param s The source node. 677 790 template<typename Graph, typename CapacityMap> 678 791 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity, … … 684 797 } 685 798 799 /// \brief Read a graph from the input. 800 /// 801 /// Read a graph from the input. 802 /// \param is The input stream. 803 /// \param g The graph. 804 /// \param capacity The capacity map. 686 805 template<typename Graph, typename CapacityMap> 687 806 void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) { … … 691 810 } 692 811 812 /// \brief Read a graph from the input. 813 /// 814 /// Read a graph from the input. 815 /// \param is The input stream. 816 /// \param g The graph. 693 817 template<typename Graph> 694 818 void readGraph(std::istream& is, Graph &g) { … … 697 821 } 698 822 823 /// @} 699 824 } 700 825
Note: See TracChangeset
for help on using the changeset viewer.