lemon/graph_reader.h
changeset 2550 f26368148b9c
parent 2467 2025a571895e
child 2553 bfced05fa852
equal deleted inserted replaced
19:03614bc310bd 20:e258aa1f42e0
   723     UEdgeReader<Graph> uedge_reader;
   723     UEdgeReader<Graph> uedge_reader;
   724     
   724     
   725     AttributeReader<ReaderTraits> attribute_reader;
   725     AttributeReader<ReaderTraits> attribute_reader;
   726   };
   726   };
   727 
   727 
       
   728   /// \brief The bipartite graph reader class.
       
   729   ///
       
   730   /// The \c BpUGraphReader class provides the graph input. 
       
   731   /// Before you read this documentation it might be useful to read the general
       
   732   /// description of  \ref graph-io-page "Graph Input-Output".
       
   733   ///
       
   734   /// The given file format may contain several maps and labeled nodes or 
       
   735   /// edges.
       
   736   ///
       
   737   /// If you read a graph you need not read all the maps and items just those
       
   738   /// that you need. The interface of the \c BpUGraphReader is very similar
       
   739   /// to the BpUGraphWriter but the reading method does not depend on the
       
   740   /// order of the given commands.
       
   741   ///
       
   742   /// The reader object suppose that each not read value does not contain 
       
   743   /// whitespaces, therefore it has some extra possibilities to control how
       
   744   /// it should skip the values when the string representation contains spaces.
       
   745   ///
       
   746   ///\code
       
   747   /// BpUGraphReader<ListBpUGraph> reader(std::cin, graph);
       
   748   ///\endcode
       
   749   ///
       
   750   /// The \c readANodeMap() function reads a map from the A-part of
       
   751   /// the\c \@bpnodeset section, while the \c readBNodeMap() reads
       
   752   /// from the B-part of the section.  If you use the \c readNodeMap()
       
   753   /// function, then the given map should appear in both part of the
       
   754   /// section. If there is a map that you do not want to read from the
       
   755   /// file and there is whitespace in the string represenation of the
       
   756   /// values then you should call the \c skipANodeMap(), \c
       
   757   /// skipBNodeMap() or \c skipNodeMap() template member function with
       
   758   /// proper parameters.
       
   759   ///
       
   760   ///\code
       
   761   /// reader.readNodeMap("coords", coords);
       
   762   /// reader.readANodeMap("range", range);
       
   763   /// reader.readANodeMap("benefit", benefit);
       
   764   ///
       
   765   /// reader.skipNodeMap("description", desc);
       
   766   ///
       
   767   /// reader.readNodeMap("color", colorMap);
       
   768   ///\endcode
       
   769   ///
       
   770   /// With the \c readUEdgeMap() member function you can give an 
       
   771   /// uedge map reading command similar to the NodeMaps. 
       
   772   ///
       
   773   ///\code
       
   774   /// reader.readUEdgeMap("capacity", capacityMap);
       
   775   /// reader.readEdgeMap("flow", flowMap);
       
   776   ///\endcode 
       
   777   ///
       
   778   /// With \c readNode() and \c readUEdge() functions you can read 
       
   779   /// labeled Nodes and UEdges.
       
   780   ///
       
   781   ///\code
       
   782   /// reader.readNode("source", sourceNode);
       
   783   /// reader.readNode("target", targetNode);
       
   784   ///
       
   785   /// reader.readUEdge("observed", uEdge);
       
   786   ///\endcode
       
   787   ///
       
   788   /// With the \c readAttribute() functions you can read an attribute
       
   789   /// in a variable. You can specify the reader for the attribute as
       
   790   /// the nodemaps.
       
   791   ///
       
   792   /// After you give all read commands you must call the \c run() member
       
   793   /// function, which execute all the commands.
       
   794   ///
       
   795   ///\code
       
   796   /// reader.run();
       
   797   ///\endcode
       
   798   ///
       
   799   /// \see GraphReader
       
   800   /// \see DefaultReaderTraits
       
   801   /// \see \ref UGraphWriter
       
   802   /// \see \ref graph-io-page
       
   803   ///
       
   804   /// \author Balazs Dezso
       
   805   template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 
       
   806   class BpUGraphReader {
       
   807   public:
       
   808     
       
   809     typedef _Graph Graph;
       
   810     typedef typename Graph::Node Node;
       
   811     typedef typename Graph::Edge Edge;
       
   812     typedef typename Graph::UEdge UEdge;
       
   813 
       
   814     typedef _ReaderTraits ReaderTraits;
       
   815     typedef typename ReaderTraits::Skipper DefaultSkipper;
       
   816 
       
   817     /// \brief Construct a new BpUGraphReader.
       
   818     ///
       
   819     /// Construct a new BpUGraphReader. It reads into the given graph
       
   820     /// and it use the given reader as the default skipper.
       
   821     BpUGraphReader(std::istream& _is, Graph& _graph, 
       
   822 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
       
   823       : reader(new LemonReader(_is)), own_reader(true), skipper(_skipper),
       
   824 	nodeset_reader(*reader, _graph, std::string(), skipper),
       
   825 	uedgeset_reader(*reader, _graph, nodeset_reader, 
       
   826 			     std::string(), skipper),
       
   827 	node_reader(*reader, nodeset_reader, std::string()),
       
   828 	uedge_reader(*reader, uedgeset_reader, std::string()),
       
   829 	attribute_reader(*reader, std::string()) {}
       
   830 
       
   831     /// \brief Construct a new BpUGraphReader.
       
   832     ///
       
   833     /// Construct a new BpUGraphReader. It reads into the given graph
       
   834     /// and it use the given reader as the default skipper.
       
   835     BpUGraphReader(const std::string& _filename, Graph& _graph, 
       
   836 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
       
   837       : reader(new LemonReader(_filename)), own_reader(true), 
       
   838 	skipper(_skipper),
       
   839 	nodeset_reader(*reader, _graph, std::string(), skipper),
       
   840 	uedgeset_reader(*reader, _graph, nodeset_reader, 
       
   841 			     std::string(), skipper),
       
   842 	node_reader(*reader, nodeset_reader, std::string()),
       
   843 	uedge_reader(*reader, uedgeset_reader, std::string()),
       
   844 	attribute_reader(*reader, std::string()) {}
       
   845 
       
   846     /// \brief Construct a new BpUGraphReader.
       
   847     ///
       
   848     /// Construct a new BpUGraphReader. It reads into the given graph
       
   849     /// and it use the given reader as the default skipper.
       
   850     BpUGraphReader(LemonReader& _reader, Graph& _graph, 
       
   851 		     const DefaultSkipper& _skipper = DefaultSkipper()) 
       
   852       : reader(_reader), own_reader(false), skipper(_skipper),
       
   853 	nodeset_reader(*reader, _graph, std::string(), skipper),
       
   854 	uedgeset_reader(*reader, _graph, nodeset_reader, 
       
   855 			     std::string(), skipper),
       
   856 	node_reader(*reader, nodeset_reader, std::string()),
       
   857 	uedge_reader(*reader, uedgeset_reader, std::string()),
       
   858 	attribute_reader(*reader, std::string()) {}
       
   859 
       
   860     /// \brief Destruct the graph reader.
       
   861     ///
       
   862     /// Destruct the graph reader.
       
   863     ~BpUGraphReader() {
       
   864       if (own_reader) 
       
   865 	delete reader;
       
   866     }
       
   867 
       
   868     /// \brief Give a new node map reading command to the reader.
       
   869     ///
       
   870     /// Give a new node map reading command to the reader.
       
   871     template <typename Map>
       
   872     BpUGraphReader& readNodeMap(std::string name, Map& map) {
       
   873       nodeset_reader.readNodeMap(name, map);
       
   874       return *this;
       
   875     }
       
   876 
       
   877     template <typename Map>
       
   878     BpUGraphReader& readNodeMap(std::string name, const Map& map) {
       
   879       nodeset_reader.readNodeMap(name, map);
       
   880       return *this;
       
   881     }
       
   882 
       
   883     /// \brief Give a new node map reading command to the reader.
       
   884     ///
       
   885     /// Give a new node map reading command to the reader.
       
   886     template <typename ItemReader, typename Map>
       
   887     BpUGraphReader& readNodeMap(std::string name, Map& map, 
       
   888                               const ItemReader& ir = ItemReader()) {
       
   889       nodeset_reader.readNodeMap(name, map, ir);
       
   890       return *this;
       
   891     }
       
   892 
       
   893     template <typename ItemReader, typename Map>
       
   894     BpUGraphReader& readNodeMap(std::string name, const Map& map, 
       
   895                               const ItemReader& ir = ItemReader()) {
       
   896       nodeset_reader.readNodeMap(name, map, ir);
       
   897       return *this;
       
   898     }
       
   899 
       
   900     /// \brief Give a new node map skipping command to the reader.
       
   901     ///
       
   902     /// Give a new node map skipping command to the reader.
       
   903     template <typename ItemReader>
       
   904     BpUGraphReader& skipNodeMap(std::string name, 
       
   905                               const ItemReader& ir = ItemReader()) {
       
   906       nodeset_reader.skipNodeMap(name, ir);
       
   907       return *this;
       
   908     }
       
   909 
       
   910     /// \brief Give a new A-node map reading command to the reader.
       
   911     ///
       
   912     /// Give a new A-node map reading command to the reader.
       
   913     template <typename Map>
       
   914     BpUGraphReader& readANodeMap(std::string name, Map& map) {
       
   915       nodeset_reader.readANodeMap(name, map);
       
   916       return *this;
       
   917     }
       
   918 
       
   919     template <typename Map>
       
   920     BpUGraphReader& readANodeMap(std::string name, const Map& map) {
       
   921       nodeset_reader.readANodeMap(name, map);
       
   922       return *this;
       
   923     }
       
   924 
       
   925     /// \brief Give a new A-node map reading command to the reader.
       
   926     ///
       
   927     /// Give a new A-node map reading command to the reader.
       
   928     template <typename ItemReader, typename Map>
       
   929     BpUGraphReader& readANodeMap(std::string name, Map& map, 
       
   930                               const ItemReader& ir = ItemReader()) {
       
   931       nodeset_reader.readANodeMap(name, map, ir);
       
   932       return *this;
       
   933     }
       
   934 
       
   935     template <typename ItemReader, typename Map>
       
   936     BpUGraphReader& readANodeMap(std::string name, const Map& map, 
       
   937                               const ItemReader& ir = ItemReader()) {
       
   938       nodeset_reader.readNodeMap(name, map, ir);
       
   939       return *this;
       
   940     }
       
   941 
       
   942     /// \brief Give a new A-node map skipping command to the reader.
       
   943     ///
       
   944     /// Give a new A-node map skipping command to the reader.
       
   945     template <typename ItemReader>
       
   946     BpUGraphReader& skipANodeMap(std::string name, 
       
   947                               const ItemReader& ir = ItemReader()) {
       
   948       nodeset_reader.skipANodeMap(name, ir);
       
   949       return *this;
       
   950     }
       
   951 
       
   952     /// \brief Give a new B-node map reading command to the reader.
       
   953     ///
       
   954     /// Give a new B-node map reading command to the reader.
       
   955     template <typename Map>
       
   956     BpUGraphReader& readBNodeMap(std::string name, Map& map) {
       
   957       nodeset_reader.readBNodeMap(name, map);
       
   958       return *this;
       
   959     }
       
   960 
       
   961     template <typename Map>
       
   962     BpUGraphReader& readBNodeMap(std::string name, const Map& map) {
       
   963       nodeset_reader.readBNodeMap(name, map);
       
   964       return *this;
       
   965     }
       
   966 
       
   967     /// \brief Give a new B-node map reading command to the reader.
       
   968     ///
       
   969     /// Give a new B-node map reading command to the reader.
       
   970     template <typename ItemReader, typename Map>
       
   971     BpUGraphReader& readBNodeMap(std::string name, Map& map, 
       
   972                               const ItemReader& ir = ItemReader()) {
       
   973       nodeset_reader.readBNodeMap(name, map, ir);
       
   974       return *this;
       
   975     }
       
   976 
       
   977     template <typename ItemReader, typename Map>
       
   978     BpUGraphReader& readBNodeMap(std::string name, const Map& map, 
       
   979                               const ItemReader& ir = ItemReader()) {
       
   980       nodeset_reader.readNodeMap(name, map, ir);
       
   981       return *this;
       
   982     }
       
   983 
       
   984     /// \brief Give a new B-node map skipping command to the reader.
       
   985     ///
       
   986     /// Give a new B-node map skipping command to the reader.
       
   987     template <typename ItemReader>
       
   988     BpUGraphReader& skipBNodeMap(std::string name, 
       
   989                               const ItemReader& ir = ItemReader()) {
       
   990       nodeset_reader.skipBNodeMap(name, ir);
       
   991       return *this;
       
   992     }
       
   993 
       
   994     /// \brief Give a new undirected edge map reading command to the reader.
       
   995     ///
       
   996     /// Give a new undirected edge map reading command to the reader.
       
   997     template <typename Map>
       
   998     BpUGraphReader& readUEdgeMap(std::string name, Map& map) { 
       
   999       uedgeset_reader.readUEdgeMap(name, map);
       
  1000       return *this;
       
  1001     }
       
  1002 
       
  1003     template <typename Map>
       
  1004     BpUGraphReader& readUEdgeMap(std::string name, const Map& map) { 
       
  1005       uedgeset_reader.readUEdgeMap(name, map);
       
  1006       return *this;
       
  1007     }
       
  1008 
       
  1009 
       
  1010     /// \brief Give a new undirected edge map reading command to the reader.
       
  1011     ///
       
  1012     /// Give a new undirected edge map reading command to the reader.
       
  1013     template <typename ItemReader, typename Map>
       
  1014     BpUGraphReader& readUEdgeMap(std::string name, Map& map,
       
  1015                                const ItemReader& ir = ItemReader()) {
       
  1016       uedgeset_reader.readUEdgeMap(name, map, ir);
       
  1017       return *this;
       
  1018     }
       
  1019 
       
  1020     template <typename ItemReader, typename Map>
       
  1021     BpUGraphReader& readUEdgeMap(std::string name, const Map& map,
       
  1022                                const ItemReader& ir = ItemReader()) {
       
  1023       uedgeset_reader.readUEdgeMap(name, map, ir);
       
  1024       return *this;
       
  1025     }
       
  1026 
       
  1027     /// \brief Give a new undirected edge map skipping command to the reader.
       
  1028     ///
       
  1029     /// Give a new undirected edge map skipping command to the reader.
       
  1030     template <typename ItemReader>
       
  1031     BpUGraphReader& skipUEdgeMap(std::string name,
       
  1032 				       const ItemReader& ir = ItemReader()) {
       
  1033       uedgeset_reader.skipUMap(name, ir);
       
  1034       return *this;
       
  1035     }
       
  1036 
       
  1037 
       
  1038     /// \brief Give a new edge map reading command to the reader.
       
  1039     ///
       
  1040     /// Give a new edge map reading command to the reader.
       
  1041     template <typename Map>
       
  1042     BpUGraphReader& readEdgeMap(std::string name, Map& map) { 
       
  1043       uedgeset_reader.readEdgeMap(name, map);
       
  1044       return *this;
       
  1045     }
       
  1046 
       
  1047     template <typename Map>
       
  1048     BpUGraphReader& readEdgeMap(std::string name, const Map& map) { 
       
  1049       uedgeset_reader.readEdgeMap(name, map);
       
  1050       return *this;
       
  1051     }
       
  1052 
       
  1053 
       
  1054     /// \brief Give a new edge map reading command to the reader.
       
  1055     ///
       
  1056     /// Give a new edge map reading command to the reader.
       
  1057     template <typename ItemReader, typename Map>
       
  1058     BpUGraphReader& readEdgeMap(std::string name, Map& map,
       
  1059                               const ItemReader& ir = ItemReader()) {
       
  1060       uedgeset_reader.readEdgeMap(name, map, ir);
       
  1061       return *this;
       
  1062     }
       
  1063 
       
  1064     template <typename ItemReader, typename Map>
       
  1065     BpUGraphReader& readEdgeMap(std::string name, const Map& map,
       
  1066                               const ItemReader& ir = ItemReader()) {
       
  1067       uedgeset_reader.readEdgeMap(name, map, ir);
       
  1068       return *this;
       
  1069     }
       
  1070 
       
  1071     /// \brief Give a new edge map skipping command to the reader.
       
  1072     ///
       
  1073     /// Give a new edge map skipping command to the reader.
       
  1074     template <typename ItemReader>
       
  1075     BpUGraphReader& skipEdgeMap(std::string name,
       
  1076                               const ItemReader& ir = ItemReader()) {
       
  1077       uedgeset_reader.skipEdgeMap(name, ir);
       
  1078       return *this;
       
  1079     }
       
  1080 
       
  1081     /// \brief Give a new labeled node reading command to the reader.
       
  1082     ///
       
  1083     /// Give a new labeled node reading command to the reader.
       
  1084     BpUGraphReader& readNode(std::string name, Node& node) {
       
  1085       node_reader.readNode(name, node);
       
  1086       return *this;
       
  1087     }
       
  1088 
       
  1089     /// \brief Give a new labeled edge reading command to the reader.
       
  1090     ///
       
  1091     /// Give a new labeled edge reading command to the reader.
       
  1092     BpUGraphReader& readEdge(std::string name, Edge& edge) {
       
  1093       uedge_reader.readEdge(name, edge);
       
  1094     }
       
  1095 
       
  1096     /// \brief Give a new labeled undirected edge reading command to the
       
  1097     /// reader.
       
  1098     ///
       
  1099     /// Give a new labeled undirected edge reading command to the reader.
       
  1100     BpUGraphReader& readUEdge(std::string name, UEdge& edge) {
       
  1101       uedge_reader.readUEdge(name, edge);
       
  1102     }
       
  1103 
       
  1104     /// \brief Give a new attribute reading command.
       
  1105     ///
       
  1106     ///  Give a new attribute reading command.
       
  1107     template <typename Value>
       
  1108     BpUGraphReader& readAttribute(std::string name, Value& value) {
       
  1109       attribute_reader.readAttribute(name, value);
       
  1110       return *this;
       
  1111     }
       
  1112     
       
  1113     /// \brief Give a new attribute reading command.
       
  1114     ///
       
  1115     ///  Give a new attribute reading command.
       
  1116     template <typename ItemReader, typename Value>
       
  1117     BpUGraphReader& readAttribute(std::string name, Value& value, 
       
  1118 			       const ItemReader& ir = ItemReader()) {
       
  1119       attribute_reader.readAttribute(name, value, ir);
       
  1120       return *this;
       
  1121     }
       
  1122 
       
  1123     /// \brief Conversion operator to LemonReader.
       
  1124     ///
       
  1125     /// Conversion operator to LemonReader. It make possible
       
  1126     /// to access the encapsulated \e LemonReader, this way
       
  1127     /// you can attach to this reader new instances of 
       
  1128     /// \e LemonReader::SectionReader.
       
  1129     operator LemonReader&() {
       
  1130       return *reader;
       
  1131     }
       
  1132 
       
  1133     /// \brief Executes the reading commands.
       
  1134     ///
       
  1135     /// Executes the reading commands.
       
  1136     void run() {
       
  1137       reader->run();
       
  1138     }
       
  1139 
       
  1140 
       
  1141     /// \brief Returns true if the reader can give back the items by its label.
       
  1142     ///
       
  1143     /// Returns true if the reader can give back the items by its label.
       
  1144     bool isLabelReader() const {
       
  1145       return nodeset_reader.isLabelReader() && 
       
  1146         uedgeset_reader.isLabelReader();
       
  1147     }
       
  1148 
       
  1149     /// \brief Gives back the node by its label.
       
  1150     ///
       
  1151     /// It reads an label from the stream and gives back which node belongs to
       
  1152     /// it. It is possible only if there was read a "label" named node map.
       
  1153     void readLabel(std::istream& is, Node& node) const {
       
  1154       return nodeset_reader.readLabel(is, node);
       
  1155     } 
       
  1156 
       
  1157     /// \brief Gives back the edge by its label
       
  1158     ///
       
  1159     /// It reads an label from the stream and gives back which edge belongs to
       
  1160     /// it. It is possible only if there was read a "label" named edge map.
       
  1161     void readLabel(std::istream& is, Edge& edge) const {
       
  1162       return uedgeset_reader.readLabel(is, edge);
       
  1163     } 
       
  1164 
       
  1165     /// \brief Gives back the undirected edge by its label.
       
  1166     ///
       
  1167     /// It reads an label from the stream and gives back which undirected edge 
       
  1168     /// belongs to it. It is possible only if there was read a "label" named 
       
  1169     /// edge map.
       
  1170     void readLabel(std::istream& is, UEdge& uedge) const {
       
  1171       return uedgeset_reader.readLabel(is, uedge);
       
  1172     } 
       
  1173     
       
  1174 
       
  1175   private:
       
  1176 
       
  1177     LemonReader* reader;
       
  1178     bool own_reader;
       
  1179 
       
  1180     DefaultSkipper skipper;
       
  1181 
       
  1182     BpNodeSetReader<Graph, ReaderTraits> nodeset_reader;
       
  1183     UEdgeSetReader<Graph, ReaderTraits> uedgeset_reader;
       
  1184 
       
  1185     NodeReader<Graph> node_reader;
       
  1186     UEdgeReader<Graph> uedge_reader;
       
  1187     
       
  1188     AttributeReader<ReaderTraits> attribute_reader;
       
  1189   };
       
  1190 
   728 
  1191 
   729   /// @}
  1192   /// @}
   730 }
  1193 }
   731 
  1194 
   732 #endif
  1195 #endif