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. |
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)); |