lemon/lemon_writer.h
changeset 1921 fb4a2a84d363
parent 1901 723b2b81d900
child 1956 a055123339d5
equal deleted inserted replaced
9:f56ab4dfc450 10:02103afb1393
    89     struct Ref { typedef const Map& Type; };
    89     struct Ref { typedef const Map& Type; };
    90 
    90 
    91     template <typename Graph, typename Map>
    91     template <typename Graph, typename Map>
    92     class ForwardComposeMap {
    92     class ForwardComposeMap {
    93     public:
    93     public:
    94       typedef typename Graph::UndirEdge Key;
    94       typedef typename Graph::UEdge Key;
    95       typedef typename Map::Value Value;
    95       typedef typename Map::Value Value;
    96 
    96 
    97       ForwardComposeMap(const Graph& _graph, const Map& _map) 
    97       ForwardComposeMap(const Graph& _graph, const Map& _map) 
    98 	: graph(_graph), map(_map) {}
    98 	: graph(_graph), map(_map) {}
    99       
    99       
   113     }
   113     }
   114 
   114 
   115     template <typename Graph, typename Map>
   115     template <typename Graph, typename Map>
   116     class BackwardComposeMap {
   116     class BackwardComposeMap {
   117     public:
   117     public:
   118       typedef typename Graph::UndirEdge Key;
   118       typedef typename Graph::UEdge Key;
   119       typedef typename Map::Value Value;
   119       typedef typename Map::Value Value;
   120 
   120 
   121       BackwardComposeMap(const Graph& _graph, const Map& _map) 
   121       BackwardComposeMap(const Graph& _graph, const Map& _map) 
   122 	: graph(_graph), map(_map) {}
   122 	: graph(_graph), map(_map) {}
   123       
   123       
   706 
   706 
   707   /// \ingroup io_group
   707   /// \ingroup io_group
   708   /// \brief SectionWriter for writing a undirected edgeset.
   708   /// \brief SectionWriter for writing a undirected edgeset.
   709   ///
   709   ///
   710   /// The lemon format can store multiple undirected edgesets with several 
   710   /// The lemon format can store multiple undirected edgesets with several 
   711   /// maps. The undirected edgeset section's header line is \c \@undiredgeset 
   711   /// maps. The undirected edgeset section's header line is \c \@uedgeset 
   712   /// \c undiredgeset_name, but the \c undiredgeset_name may be empty.
   712   /// \c uedgeset_name, but the \c uedgeset_name may be empty.
   713   ///
   713   ///
   714   /// The first line of the section contains the names of the maps separated
   714   /// The first line of the section contains the names of the maps separated
   715   /// with white spaces. Each next lines describes an undirected edge in the 
   715   /// with white spaces. Each next lines describes an undirected edge in the 
   716   /// edgeset. The line contains the two connected nodes' label and the mapped 
   716   /// edgeset. The line contains the two connected nodes' label and the mapped 
   717   /// values for each undirected map.
   717   /// values for each undirected map.
   732   /// which nodes have to be connected. If a NodeSetWriter can write the 
   732   /// which nodes have to be connected. If a NodeSetWriter can write the 
   733   /// nodes' label, it will be able to use with this class.
   733   /// nodes' label, it will be able to use with this class.
   734   ///
   734   ///
   735   /// \relates LemonWriter
   735   /// \relates LemonWriter
   736   template <typename _Graph, typename _Traits = DefaultWriterTraits>
   736   template <typename _Graph, typename _Traits = DefaultWriterTraits>
   737   class UndirEdgeSetWriter : public LemonWriter::SectionWriter {
   737   class UEdgeSetWriter : public LemonWriter::SectionWriter {
   738     typedef LemonWriter::SectionWriter Parent;
   738     typedef LemonWriter::SectionWriter Parent;
   739   public:
   739   public:
   740 
   740 
   741     typedef _Graph Graph;
   741     typedef _Graph Graph;
   742     typedef _Traits Traits;
   742     typedef _Traits Traits;
   743     typedef typename Graph::Node Node;
   743     typedef typename Graph::Node Node;
   744     typedef typename Graph::Edge Edge;
   744     typedef typename Graph::Edge Edge;
   745     typedef typename Graph::UndirEdge UndirEdge;
   745     typedef typename Graph::UEdge UEdge;
   746 
   746 
   747     /// \brief Constructor.
   747     /// \brief Constructor.
   748     ///
   748     ///
   749     /// Constructor for UndirEdgeSetWriter. It creates the UndirEdgeSetWriter
   749     /// Constructor for UEdgeSetWriter. It creates the UEdgeSetWriter
   750     /// and attach it into the given LemonWriter. It will write node labels by
   750     /// and attach it into the given LemonWriter. It will write node labels by
   751     /// the \c _nodeLabelWriter. If the \c _forceLabelMap parameter is true 
   751     /// the \c _nodeLabelWriter. If the \c _forceLabelMap parameter is true 
   752     /// then the writer will write own label map if the user does not give 
   752     /// then the writer will write own label map if the user does not give 
   753     /// "label" named map.
   753     /// "label" named map.
   754     template <typename NodeLabelWriter>
   754     template <typename NodeLabelWriter>
   755     UndirEdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 
   755     UEdgeSetWriter(LemonWriter& _writer, const Graph& _graph, 
   756 		       const NodeLabelWriter& _nodeLabelWriter, 
   756 		       const NodeLabelWriter& _nodeLabelWriter, 
   757 		       const std::string& _name = std::string(),
   757 		       const std::string& _name = std::string(),
   758 		       bool _forceLabelMap = true)
   758 		       bool _forceLabelMap = true)
   759       : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
   759       : Parent(_writer), labelMap(0), forceLabelMap(_forceLabelMap),
   760 	graph(_graph), name(_name) {
   760 	graph(_graph), name(_name) {
   763 			 LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
   763 			 LabelWriter<Node, NodeLabelWriter>(_nodeLabelWriter));
   764     } 
   764     } 
   765 
   765 
   766     /// \brief Destructor.
   766     /// \brief Destructor.
   767     ///
   767     ///
   768     /// Destructor for UndirEdgeSetWriter.
   768     /// Destructor for UEdgeSetWriter.
   769     virtual ~UndirEdgeSetWriter() {
   769     virtual ~UEdgeSetWriter() {
   770       typename MapWriters::iterator it;
   770       typename MapWriters::iterator it;
   771       for (it = writers.begin(); it != writers.end(); ++it) {
   771       for (it = writers.begin(); it != writers.end(); ++it) {
   772 	delete it->second;
   772 	delete it->second;
   773       }
   773       }
   774     }
   774     }
   775 
   775 
   776   private:
   776   private:
   777     UndirEdgeSetWriter(const UndirEdgeSetWriter&);
   777     UEdgeSetWriter(const UEdgeSetWriter&);
   778     void operator=(const UndirEdgeSetWriter&);
   778     void operator=(const UEdgeSetWriter&);
   779 
   779 
   780   public:
   780   public:
   781 
   781 
   782     /// \brief Add a new undirected edge map writer command for the writer.
   782     /// \brief Add a new undirected edge map writer command for the writer.
   783     ///
   783     ///
   784     /// Add a new undirected map writer command for the writer.
   784     /// Add a new undirected map writer command for the writer.
   785     template <typename Map>
   785     template <typename Map>
   786     UndirEdgeSetWriter& writeUndirEdgeMap(std::string name, const Map& map) {
   786     UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map) {
   787       return writeUndirEdgeMap<typename Traits::
   787       return writeUEdgeMap<typename Traits::
   788 	template Writer<typename Map::Value>, Map>(name, map);
   788 	template Writer<typename Map::Value>, Map>(name, map);
   789     }
   789     }
   790 
   790 
   791     /// \brief Add a new undirected map writer command for the writer.
   791     /// \brief Add a new undirected map writer command for the writer.
   792     ///
   792     ///
   793     /// Add a new undirected map writer command for the writer.
   793     /// Add a new undirected map writer command for the writer.
   794     template <typename Writer, typename Map>
   794     template <typename Writer, typename Map>
   795     UndirEdgeSetWriter& writeUndirEdgeMap(std::string name, const Map& map, 
   795     UEdgeSetWriter& writeUEdgeMap(std::string name, const Map& map, 
   796 					  const Writer& writer = Writer()) {
   796 					  const Writer& writer = Writer()) {
   797       checkConcept<concept::ReadMap<UndirEdge, typename Map::Value>, Map>();
   797       checkConcept<concept::ReadMap<UEdge, typename Map::Value>, Map>();
   798       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   798       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   799       writers.push_back(
   799       writers.push_back(
   800 	make_pair(name, new _writer_bits::
   800 	make_pair(name, new _writer_bits::
   801 		  MapWriter<UndirEdge, Map, Writer>(map, writer)));
   801 		  MapWriter<UEdge, Map, Writer>(map, writer)));
   802       return *this;
   802       return *this;
   803     }
   803     }
   804 
   804 
   805     /// \brief Add a new directed edge map writer command for the writer.
   805     /// \brief Add a new directed edge map writer command for the writer.
   806     ///
   806     ///
   807     /// Add a new directed map writer command for the writer.
   807     /// Add a new directed map writer command for the writer.
   808     template <typename Map>
   808     template <typename Map>
   809     UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map) {
   809     UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map) {
   810       return writeEdgeMap<typename Traits::
   810       return writeEdgeMap<typename Traits::
   811 	template Writer<typename Map::Value>, Map>(name, map);
   811 	template Writer<typename Map::Value>, Map>(name, map);
   812     }
   812     }
   813 
   813 
   814     /// \brief Add a new directed map writer command for the writer.
   814     /// \brief Add a new directed map writer command for the writer.
   815     ///
   815     ///
   816     /// Add a new directed map writer command for the writer.
   816     /// Add a new directed map writer command for the writer.
   817     template <typename Writer, typename Map>
   817     template <typename Writer, typename Map>
   818     UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
   818     UEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, 
   819 				     const Writer& writer = Writer()) {
   819 				     const Writer& writer = Writer()) {
   820       checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>();
   820       checkConcept<concept::ReadMap<Edge, typename Map::Value>, Map>();
   821       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   821       checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
   822       writeUndirEdge("+" + name, 
   822       writeUEdge("+" + name, 
   823 		     _writer_bits::forwardComposeMap(graph, map), writer);
   823 		     _writer_bits::forwardComposeMap(graph, map), writer);
   824       writeUndirEdge("-" + name, 
   824       writeUEdge("-" + name, 
   825 		     _writer_bits::backwardComposeMap(graph, map), writer);
   825 		     _writer_bits::backwardComposeMap(graph, map), writer);
   826       return *this;
   826       return *this;
   827     }
   827     }
   828 
   828 
   829   protected:
   829   protected:
   830 
   830 
   831     /// \brief The header of the section.
   831     /// \brief The header of the section.
   832     ///
   832     ///
   833     /// It gives back the header of the section.
   833     /// It gives back the header of the section.
   834     virtual std::string header() {
   834     virtual std::string header() {
   835       return "@undiredgeset " + name;
   835       return "@uedgeset " + name;
   836     }
   836     }
   837 
   837 
   838     /// \brief  Writer function of the section.
   838     /// \brief  Writer function of the section.
   839     ///
   839     ///
   840     /// Write the content of the section.
   840     /// Write the content of the section.
   855       }
   855       }
   856       for (int i = 0; i < (int)writers.size(); ++i) {
   856       for (int i = 0; i < (int)writers.size(); ++i) {
   857 	os << writers[i].first << '\t';
   857 	os << writers[i].first << '\t';
   858       }
   858       }
   859       os << std::endl;
   859       os << std::endl;
   860       for (typename Graph::UndirEdgeIt it(graph); it != INVALID; ++it) {
   860       for (typename Graph::UEdgeIt it(graph); it != INVALID; ++it) {
   861 	nodeLabelWriter->write(os, graph.source(it));
   861 	nodeLabelWriter->write(os, graph.source(it));
   862 	os << '\t';
   862 	os << '\t';
   863 	nodeLabelWriter->write(os, graph.target(it));
   863 	nodeLabelWriter->write(os, graph.target(it));
   864 	os << '\t';
   864 	os << '\t';
   865 	if (forceLabelMap) {
   865 	if (forceLabelMap) {
   889     ///
   889     ///
   890     /// It writes the label of the given undirected edge. If there was written 
   890     /// It writes the label of the given undirected edge. If there was written 
   891     /// an "label" named map then it will write the map value belongs to the 
   891     /// an "label" named map then it will write the map value belongs to the 
   892     /// undirected edge. Otherwise if the \c forceLabel parameter was true it 
   892     /// undirected edge. Otherwise if the \c forceLabel parameter was true it 
   893     /// will write its id in the graph. 
   893     /// will write its id in the graph. 
   894     void writeLabel(std::ostream& os, const UndirEdge& item) const {
   894     void writeLabel(std::ostream& os, const UEdge& item) const {
   895       if (forceLabelMap) {
   895       if (forceLabelMap) {
   896 	os << graph.id(item);
   896 	os << graph.id(item);
   897       } else {
   897       } else {
   898 	labelMap->write(os, item);
   898 	labelMap->write(os, item);
   899       }
   899       }
   920     } 
   920     } 
   921 
   921 
   922   private:
   922   private:
   923 
   923 
   924     typedef std::vector<std::pair<std::string, _writer_bits::
   924     typedef std::vector<std::pair<std::string, _writer_bits::
   925 				  MapWriterBase<UndirEdge>*> > MapWriters;
   925 				  MapWriterBase<UEdge>*> > MapWriters;
   926     MapWriters writers;
   926     MapWriters writers;
   927 
   927 
   928     _writer_bits::MapWriterBase<UndirEdge>* labelMap;
   928     _writer_bits::MapWriterBase<UEdge>* labelMap;
   929     bool forceLabelMap;
   929     bool forceLabelMap;
   930    
   930    
   931     const Graph& graph;   
   931     const Graph& graph;   
   932     std::string name;
   932     std::string name;
   933 
   933 
  1097   };
  1097   };
  1098 
  1098 
  1099   /// \ingroup io_group
  1099   /// \ingroup io_group
  1100   /// \brief SectionWriter for writing named undirected edges.
  1100   /// \brief SectionWriter for writing named undirected edges.
  1101   ///
  1101   ///
  1102   /// The undirected edges section's header line is \c \@undiredges 
  1102   /// The undirected edges section's header line is \c \@uedges 
  1103   /// \c undiredges_name, but the \c undiredges_name may be empty.
  1103   /// \c uedges_name, but the \c uedges_name may be empty.
  1104   ///
  1104   ///
  1105   /// Each line in the section contains the name of the undirected edge and 
  1105   /// Each line in the section contains the name of the undirected edge and 
  1106   /// then the undirected edge label. 
  1106   /// then the undirected edge label. 
  1107   ///
  1107   ///
  1108   /// \relates LemonWriter
  1108   /// \relates LemonWriter
  1109   template <typename _Graph>
  1109   template <typename _Graph>
  1110   class UndirEdgeWriter : public LemonWriter::SectionWriter {
  1110   class UEdgeWriter : public LemonWriter::SectionWriter {
  1111     typedef LemonWriter::SectionWriter Parent;
  1111     typedef LemonWriter::SectionWriter Parent;
  1112     typedef _Graph Graph;
  1112     typedef _Graph Graph;
  1113     typedef typename Graph::Node Node;
  1113     typedef typename Graph::Node Node;
  1114     typedef typename Graph::Edge Edge;
  1114     typedef typename Graph::Edge Edge;
  1115     typedef typename Graph::UndirEdge UndirEdge;
  1115     typedef typename Graph::UEdge UEdge;
  1116   public:
  1116   public:
  1117     
  1117     
  1118     /// \brief Constructor.
  1118     /// \brief Constructor.
  1119     ///
  1119     ///
  1120     /// Constructor for UndirEdgeWriter. It creates the UndirEdgeWriter and
  1120     /// Constructor for UEdgeWriter. It creates the UEdgeWriter and
  1121     /// attach it into the given LemonWriter. The given \c _LabelWriter
  1121     /// attach it into the given LemonWriter. The given \c _LabelWriter
  1122     /// will write the undirected edges' label what can be an undirected 
  1122     /// will write the undirected edges' label what can be an undirected 
  1123     /// edgeset writer.
  1123     /// edgeset writer.
  1124     template <typename _LabelWriter>
  1124     template <typename _LabelWriter>
  1125     UndirEdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
  1125     UEdgeWriter(LemonWriter& _writer, const _LabelWriter& _labelWriter, 
  1126 	       const std::string& _name = std::string()) 
  1126 	       const std::string& _name = std::string()) 
  1127       : Parent(_writer), name(_name) {
  1127       : Parent(_writer), name(_name) {
  1128       checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
  1128       checkConcept<_writer_bits::ItemLabelWriter<Edge>, _LabelWriter>();
  1129       checkConcept<_writer_bits::ItemLabelWriter<UndirEdge>, _LabelWriter>();
  1129       checkConcept<_writer_bits::ItemLabelWriter<UEdge>, _LabelWriter>();
  1130       undirEdgeLabelWriter.reset(new _writer_bits::
  1130       uEdgeLabelWriter.reset(new _writer_bits::
  1131 			      LabelWriter<UndirEdge, _LabelWriter>(_labelWriter));
  1131 			      LabelWriter<UEdge, _LabelWriter>(_labelWriter));
  1132       edgeLabelWriter.reset(new _writer_bits::
  1132       edgeLabelWriter.reset(new _writer_bits::
  1133 			 LabelWriter<Edge, _LabelWriter>(_labelWriter));
  1133 			 LabelWriter<Edge, _LabelWriter>(_labelWriter));
  1134     }
  1134     }
  1135 
  1135 
  1136     /// \brief Destructor.
  1136     /// \brief Destructor.
  1137     ///
  1137     ///
  1138     /// Destructor for UndirEdgeWriter.
  1138     /// Destructor for UEdgeWriter.
  1139     virtual ~UndirEdgeWriter() {}
  1139     virtual ~UEdgeWriter() {}
  1140   private:
  1140   private:
  1141     UndirEdgeWriter(const UndirEdgeWriter&);
  1141     UEdgeWriter(const UEdgeWriter&);
  1142     void operator=(const UndirEdgeWriter&);
  1142     void operator=(const UEdgeWriter&);
  1143 
  1143 
  1144   public:
  1144   public:
  1145 
  1145 
  1146     /// \brief Add an edge writer command for the UndirEdgeWriter.
  1146     /// \brief Add an edge writer command for the UEdgeWriter.
  1147     ///
  1147     ///
  1148     /// Add an edge writer command for the UndirEdgeWriter.
  1148     /// Add an edge writer command for the UEdgeWriter.
  1149     void writeEdge(const std::string& name, const Edge& item) {
  1149     void writeEdge(const std::string& name, const Edge& item) {
  1150       edgeWriters.push_back(make_pair(name, &item));
  1150       edgeWriters.push_back(make_pair(name, &item));
  1151     }
  1151     }
  1152 
  1152 
  1153     /// \brief Add an undirected edge writer command for the UndirEdgeWriter.
  1153     /// \brief Add an undirected edge writer command for the UEdgeWriter.
  1154     ///
  1154     ///
  1155     /// Add an undirected edge writer command for the UndirEdgeWriter.
  1155     /// Add an undirected edge writer command for the UEdgeWriter.
  1156     void writeUndirEdge(const std::string& name, const UndirEdge& item) {
  1156     void writeUEdge(const std::string& name, const UEdge& item) {
  1157       undirEdgeWriters.push_back(make_pair(name, &item));
  1157       uEdgeWriters.push_back(make_pair(name, &item));
  1158     }
  1158     }
  1159 
  1159 
  1160   protected:
  1160   protected:
  1161 
  1161 
  1162     /// \brief The header of the section.
  1162     /// \brief The header of the section.
  1163     ///
  1163     ///
  1164     /// It gives back the header of the section.
  1164     /// It gives back the header of the section.
  1165     virtual std::string header() {
  1165     virtual std::string header() {
  1166       return "@undiredges " + name;
  1166       return "@uedges " + name;
  1167     }
  1167     }
  1168 
  1168 
  1169     /// \brief  Writer function of the section.
  1169     /// \brief  Writer function of the section.
  1170     ///
  1170     ///
  1171     /// Write the content of the section.
  1171     /// Write the content of the section.
  1172     virtual void write(std::ostream& os) {
  1172     virtual void write(std::ostream& os) {
  1173       if (!edgeLabelWriter->isLabelWriter()) {
  1173       if (!edgeLabelWriter->isLabelWriter()) {
  1174 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1174 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1175       }
  1175       }
  1176       if (!undirEdgeLabelWriter->isLabelWriter()) {
  1176       if (!uEdgeLabelWriter->isLabelWriter()) {
  1177 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1177 	throw DataFormatError("Cannot find undirected edgeset or label map");
  1178       }
  1178       }
  1179       for (int i = 0; i < (int)undirEdgeWriters.size(); ++i) {
  1179       for (int i = 0; i < (int)uEdgeWriters.size(); ++i) {
  1180 	os << undirEdgeWriters[i].first << ' ';
  1180 	os << uEdgeWriters[i].first << ' ';
  1181 	undirEdgeLabelWriter->write(os, *(undirEdgeWriters[i].second));
  1181 	uEdgeLabelWriter->write(os, *(uEdgeWriters[i].second));
  1182 	os << std::endl;
  1182 	os << std::endl;
  1183       }
  1183       }
  1184       for (int i = 0; i < (int)edgeWriters.size(); ++i) {
  1184       for (int i = 0; i < (int)edgeWriters.size(); ++i) {
  1185 	os << edgeWriters[i].first << ' ';
  1185 	os << edgeWriters[i].first << ' ';
  1186 	edgeLabelWriter->write(os, *(edgeWriters[i].second));
  1186 	edgeLabelWriter->write(os, *(edgeWriters[i].second));
  1191   private:
  1191   private:
  1192 
  1192 
  1193     std::string name;
  1193     std::string name;
  1194 
  1194 
  1195     typedef std::vector<std::pair<std::string, 
  1195     typedef std::vector<std::pair<std::string, 
  1196 				  const UndirEdge*> > UndirEdgeWriters;
  1196 				  const UEdge*> > UEdgeWriters;
  1197     UndirEdgeWriters undirEdgeWriters;
  1197     UEdgeWriters uEdgeWriters;
  1198     std::auto_ptr<_writer_bits::LabelWriterBase<UndirEdge> > undirEdgeLabelWriter;
  1198     std::auto_ptr<_writer_bits::LabelWriterBase<UEdge> > uEdgeLabelWriter;
  1199 
  1199 
  1200     typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters;
  1200     typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters;
  1201     EdgeWriters edgeWriters;
  1201     EdgeWriters edgeWriters;
  1202     std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > edgeLabelWriter;
  1202     std::auto_ptr<_writer_bits::LabelWriterBase<Edge> > edgeLabelWriter;
  1203 
  1203