271 typedef CommonSectionWriterBase Parent; |
271 typedef CommonSectionWriterBase Parent; |
272 public: |
272 public: |
273 |
273 |
274 typedef _Graph Graph; |
274 typedef _Graph Graph; |
275 typedef _Traits Traits; |
275 typedef _Traits Traits; |
276 typedef typename Graph::Node Item; |
276 typedef typename Graph::Node Node; |
277 |
277 |
278 /// \brief Constructor. |
278 /// \brief Constructor. |
279 /// |
279 /// |
280 /// Constructor for NodeSetWriter. It creates the NodeSetWriter and |
280 /// Constructor for NodeSetWriter. It creates the NodeSetWriter and |
281 /// attach it into the given LemonWriter. If the \c _forceIdMap |
281 /// attach it into the given LemonWriter. If the \c _forceIdMap |
317 /// Add a new node map writer command for the writer. |
317 /// Add a new node map writer command for the writer. |
318 template <typename Writer, typename Map> |
318 template <typename Writer, typename Map> |
319 NodeSetWriter& writeNodeMap(std::string name, const Map& map, |
319 NodeSetWriter& writeNodeMap(std::string name, const Map& map, |
320 const Writer& writer = Writer()) { |
320 const Writer& writer = Writer()) { |
321 writers.push_back( |
321 writers.push_back( |
322 make_pair(name, new MapWriter<Item, Map, Writer>(map, writer))); |
322 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer))); |
323 return *this; |
323 return *this; |
324 } |
324 } |
325 |
325 |
326 protected: |
326 protected: |
327 |
327 |
377 /// |
377 /// |
378 /// It writes the id of the given node. If there was written an "id" |
378 /// It writes the id of the given node. If there was written an "id" |
379 /// named map then it will write the map value belongs to the node. |
379 /// named map then it will write the map value belongs to the node. |
380 /// Otherwise if the \c forceId parameter was true it will write |
380 /// Otherwise if the \c forceId parameter was true it will write |
381 /// its id in the graph. |
381 /// its id in the graph. |
382 void writeId(std::ostream& os, const Item& item) const { |
382 void writeId(std::ostream& os, const Node& item) const { |
383 if (forceIdMap) { |
383 if (forceIdMap) { |
384 os << graph.id(item); |
384 os << graph.id(item); |
385 } else { |
385 } else { |
386 idMap->write(os, item); |
386 idMap->write(os, item); |
387 } |
387 } |
388 } |
388 } |
389 |
389 |
390 private: |
390 private: |
391 |
391 |
392 typedef std::vector<std::pair<std::string, WriterBase<Item>*> > MapWriters; |
392 typedef std::vector<std::pair<std::string, WriterBase<Node>*> > MapWriters; |
393 MapWriters writers; |
393 MapWriters writers; |
394 |
394 |
395 WriterBase<Item>* idMap; |
395 WriterBase<Node>* idMap; |
396 bool forceIdMap; |
396 bool forceIdMap; |
397 |
397 |
398 typename SmartConstReference<Graph>::Type graph; |
398 typename SmartConstReference<Graph>::Type graph; |
399 std::string id; |
399 std::string id; |
400 |
400 |
428 typedef CommonSectionWriterBase Parent; |
428 typedef CommonSectionWriterBase Parent; |
429 public: |
429 public: |
430 |
430 |
431 typedef _Graph Graph; |
431 typedef _Graph Graph; |
432 typedef _Traits Traits; |
432 typedef _Traits Traits; |
433 typedef typename Graph::Edge Item; |
433 typedef typename Graph::Node Node; |
|
434 typedef typename Graph::Edge Edge; |
434 |
435 |
435 /// \brief Constructor. |
436 /// \brief Constructor. |
436 /// |
437 /// |
437 /// Constructor for EdgeSetWriter. It creates the EdgeSetWriter and |
438 /// Constructor for EdgeSetWriter. It creates the EdgeSetWriter and |
438 /// attach it into the given LemonWriter. It will write node ids by |
439 /// attach it into the given LemonWriter. It will write node ids by |
444 const NodeIdWriter& _nodeIdWriter, |
445 const NodeIdWriter& _nodeIdWriter, |
445 const std::string& _id = std::string(), |
446 const std::string& _id = std::string(), |
446 bool _forceIdMap = true) |
447 bool _forceIdMap = true) |
447 : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), |
448 : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), |
448 graph(_graph), id(_id), |
449 graph(_graph), id(_id), |
449 nodeIdWriter(new IdWriter<typename Graph::Node, NodeIdWriter> |
450 nodeIdWriter(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter)) {} |
450 (_nodeIdWriter)) {} |
|
451 |
451 |
452 /// \brief Destructor. |
452 /// \brief Destructor. |
453 /// |
453 /// |
454 /// Destructor for EdgeSetWriter. |
454 /// Destructor for EdgeSetWriter. |
455 virtual ~EdgeSetWriter() { |
455 virtual ~EdgeSetWriter() { |
479 /// Add a new edge map writer command for the writer. |
479 /// Add a new edge map writer command for the writer. |
480 template <typename Writer, typename Map> |
480 template <typename Writer, typename Map> |
481 EdgeSetWriter& writeEdgeMap(std::string name, const Map& map, |
481 EdgeSetWriter& writeEdgeMap(std::string name, const Map& map, |
482 const Writer& writer = Writer()) { |
482 const Writer& writer = Writer()) { |
483 writers.push_back( |
483 writers.push_back( |
484 make_pair(name, new MapWriter<Item, Map, Writer>(map, writer))); |
484 make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer))); |
485 return *this; |
485 return *this; |
486 } |
486 } |
487 |
487 |
488 protected: |
488 protected: |
489 |
489 |
544 /// |
544 /// |
545 /// It writes the id of the given edge. If there was written an "id" |
545 /// It writes the id of the given edge. If there was written an "id" |
546 /// named map then it will write the map value belongs to the edge. |
546 /// named map then it will write the map value belongs to the edge. |
547 /// Otherwise if the \c forceId parameter was true it will write |
547 /// Otherwise if the \c forceId parameter was true it will write |
548 /// its id in the graph. |
548 /// its id in the graph. |
549 void writeId(std::ostream& os, const Item& item) const { |
549 void writeId(std::ostream& os, const Edge& item) const { |
550 if (forceIdMap) { |
550 if (forceIdMap) { |
551 os << graph.id(item); |
551 os << graph.id(item); |
552 } else { |
552 } else { |
553 idMap->write(os, item); |
553 idMap->write(os, item); |
554 } |
554 } |
555 } |
555 } |
556 |
556 |
557 private: |
557 private: |
558 |
558 |
559 typedef std::vector<std::pair<std::string, WriterBase<Item>*> > MapWriters; |
559 typedef std::vector<std::pair<std::string, WriterBase<Edge>*> > MapWriters; |
560 MapWriters writers; |
560 MapWriters writers; |
561 |
561 |
562 WriterBase<Item>* idMap; |
562 WriterBase<Edge>* idMap; |
563 bool forceIdMap; |
563 bool forceIdMap; |
564 |
564 |
565 typename SmartConstReference<Graph>::Type graph; |
565 typename SmartConstReference<Graph>::Type graph; |
566 std::string id; |
566 std::string id; |
567 |
567 |
568 std::auto_ptr<IdWriterBase<typename Graph::Node> > nodeIdWriter; |
568 std::auto_ptr<IdWriterBase<Node> > nodeIdWriter; |
569 }; |
569 }; |
570 |
570 |
571 /// \ingroup io_group |
571 /// \ingroup io_group |
572 /// \brief SectionWriter for writing a undirected edgeset. |
572 /// \brief SectionWriter for writing a undirected edgeset. |
573 /// |
573 /// |
602 typedef CommonSectionWriterBase Parent; |
602 typedef CommonSectionWriterBase Parent; |
603 public: |
603 public: |
604 |
604 |
605 typedef _Graph Graph; |
605 typedef _Graph Graph; |
606 typedef _Traits Traits; |
606 typedef _Traits Traits; |
607 typedef typename Graph::UndirEdge Item; |
607 typedef typename Graph::Node Node; |
|
608 typedef typename Graph::Edge Edge; |
|
609 typedef typename Graph::UndirEdge UndirEdge; |
608 |
610 |
609 /// \brief Constructor. |
611 /// \brief Constructor. |
610 /// |
612 /// |
611 /// Constructor for UndirEdgeSetWriter. It creates the UndirEdgeSetWriter |
613 /// Constructor for UndirEdgeSetWriter. It creates the UndirEdgeSetWriter |
612 /// and attach it into the given LemonWriter. It will write node ids by |
614 /// and attach it into the given LemonWriter. It will write node ids by |
618 const NodeIdWriter& _nodeIdWriter, |
620 const NodeIdWriter& _nodeIdWriter, |
619 const std::string& _id = std::string(), |
621 const std::string& _id = std::string(), |
620 bool _forceIdMap = true) |
622 bool _forceIdMap = true) |
621 : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), |
623 : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), |
622 graph(_graph), id(_id), |
624 graph(_graph), id(_id), |
623 nodeIdWriter(new IdWriter<typename Graph::Node, NodeIdWriter> |
625 nodeIdWriter(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter)) {} |
624 (_nodeIdWriter)) {} |
|
625 |
626 |
626 /// \brief Destructor. |
627 /// \brief Destructor. |
627 /// |
628 /// |
628 /// Destructor for UndirEdgeSetWriter. |
629 /// Destructor for UndirEdgeSetWriter. |
629 virtual ~UndirEdgeSetWriter() { |
630 virtual ~UndirEdgeSetWriter() { |
653 /// Add a new undirected map writer command for the writer. |
654 /// Add a new undirected map writer command for the writer. |
654 template <typename Writer, typename Map> |
655 template <typename Writer, typename Map> |
655 UndirEdgeSetWriter& writeUndirEdgeMap(std::string name, const Map& map, |
656 UndirEdgeSetWriter& writeUndirEdgeMap(std::string name, const Map& map, |
656 const Writer& writer = Writer()) { |
657 const Writer& writer = Writer()) { |
657 writers.push_back( |
658 writers.push_back( |
658 make_pair(name, new MapWriter<Item, Map, Writer>(map, writer))); |
659 make_pair(name, new MapWriter<UndirEdge, Map, Writer>(map, writer))); |
659 return *this; |
660 return *this; |
660 } |
661 } |
661 |
662 |
662 /// \brief Add a new directed edge map writer command for the writer. |
663 /// \brief Add a new directed edge map writer command for the writer. |
663 /// |
664 /// |
740 /// |
741 /// |
741 /// It writes the id of the given undirected edge. If there was written |
742 /// It writes the id of the given undirected edge. If there was written |
742 /// an "id" named map then it will write the map value belongs to the |
743 /// an "id" named map then it will write the map value belongs to the |
743 /// undirected edge. Otherwise if the \c forceId parameter was true it |
744 /// undirected edge. Otherwise if the \c forceId parameter was true it |
744 /// will write its id in the graph. |
745 /// will write its id in the graph. |
745 void writeId(std::ostream& os, const Item& item) const { |
746 void writeId(std::ostream& os, const UndirEdge& item) const { |
746 if (forceIdMap) { |
747 if (forceIdMap) { |
747 os << graph.id(item); |
748 os << graph.id(item); |
748 } else { |
749 } else { |
749 idMap->write(os, item); |
750 idMap->write(os, item); |
750 } |
751 } |
751 } |
752 } |
752 |
753 |
753 private: |
754 /// \brief Write the id of the given edge. |
754 |
755 /// |
755 typedef std::vector<std::pair<std::string, WriterBase<Item>*> > MapWriters; |
756 /// It writes the id of the given edge. If there was written |
|
757 /// an "id" named map then it will write the map value belongs to the |
|
758 /// edge. Otherwise if the \c forceId parameter was true it |
|
759 /// will write its id in the graph. If the edge is forward map |
|
760 /// then its prefix character is \c '+' elsewhere \c '-'. |
|
761 void writeId(std::ostream& os, const Edge& item) const { |
|
762 if (graph.forward(item)) { |
|
763 os << "+ "; |
|
764 } else { |
|
765 os << "- "; |
|
766 } |
|
767 if (forceIdMap) { |
|
768 os << graph.id(item); |
|
769 } else { |
|
770 idMap->write(os, item); |
|
771 } |
|
772 } |
|
773 |
|
774 private: |
|
775 |
|
776 typedef std::vector<std::pair<std::string, |
|
777 WriterBase<UndirEdge>*> > MapWriters; |
756 MapWriters writers; |
778 MapWriters writers; |
757 |
779 |
758 WriterBase<Item>* idMap; |
780 WriterBase<UndirEdge>* idMap; |
759 bool forceIdMap; |
781 bool forceIdMap; |
760 |
782 |
761 typename SmartConstReference<Graph>::Type graph; |
783 typename SmartConstReference<Graph>::Type graph; |
762 std::string id; |
784 std::string id; |
763 |
785 |
764 std::auto_ptr<IdWriterBase<typename Graph::Node> > nodeIdWriter; |
786 std::auto_ptr<IdWriterBase<Node> > nodeIdWriter; |
765 }; |
787 }; |
766 |
788 |
767 /// \ingroup io_group |
789 /// \ingroup io_group |
768 /// \brief SectionWriter for writing labeled nodes. |
790 /// \brief SectionWriter for writing labeled nodes. |
769 /// |
791 /// |
776 /// \relates LemonWriter |
798 /// \relates LemonWriter |
777 template <typename _Graph> |
799 template <typename _Graph> |
778 class NodeWriter : public CommonSectionWriterBase { |
800 class NodeWriter : public CommonSectionWriterBase { |
779 typedef CommonSectionWriterBase Parent; |
801 typedef CommonSectionWriterBase Parent; |
780 typedef _Graph Graph; |
802 typedef _Graph Graph; |
781 typedef typename Graph::Node Item; |
803 typedef typename Graph::Node Node; |
782 public: |
804 public: |
783 |
805 |
784 /// \brief Constructor. |
806 /// \brief Constructor. |
785 /// |
807 /// |
786 /// Constructor for NodeWriter. It creates the NodeWriter and |
808 /// Constructor for NodeWriter. It creates the NodeWriter and |
852 /// \relates LemonWriter |
874 /// \relates LemonWriter |
853 template <typename _Graph> |
875 template <typename _Graph> |
854 class EdgeWriter : public CommonSectionWriterBase { |
876 class EdgeWriter : public CommonSectionWriterBase { |
855 typedef CommonSectionWriterBase Parent; |
877 typedef CommonSectionWriterBase Parent; |
856 typedef _Graph Graph; |
878 typedef _Graph Graph; |
857 typedef typename Graph::Edge Item; |
879 typedef typename Graph::Edge Edge; |
858 public: |
880 public: |
859 |
881 |
860 /// \brief Constructor. |
882 /// \brief Constructor. |
861 /// |
883 /// |
862 /// Constructor for EdgeWriter. It creates the EdgeWriter and |
884 /// Constructor for EdgeWriter. It creates the EdgeWriter and |
928 /// \relates LemonWriter |
950 /// \relates LemonWriter |
929 template <typename _Graph> |
951 template <typename _Graph> |
930 class UndirEdgeWriter : public CommonSectionWriterBase { |
952 class UndirEdgeWriter : public CommonSectionWriterBase { |
931 typedef CommonSectionWriterBase Parent; |
953 typedef CommonSectionWriterBase Parent; |
932 typedef _Graph Graph; |
954 typedef _Graph Graph; |
933 typedef typename Graph::UndirEdge Item; |
955 typedef typename Graph::Node Node; |
|
956 typedef typename Graph::Edge Edge; |
|
957 typedef typename Graph::UndirEdge UndirEdge; |
934 public: |
958 public: |
935 |
959 |
936 /// \brief Constructor. |
960 /// \brief Constructor. |
937 /// |
961 /// |
938 /// Constructor for UndirEdgeWriter. It creates the UndirEdgeWriter and |
962 /// Constructor for UndirEdgeWriter. It creates the UndirEdgeWriter and |
941 /// edgeset writer. |
965 /// edgeset writer. |
942 template <typename _IdWriter> |
966 template <typename _IdWriter> |
943 UndirEdgeWriter(LemonWriter& _writer, const _IdWriter& _idWriter, |
967 UndirEdgeWriter(LemonWriter& _writer, const _IdWriter& _idWriter, |
944 const std::string& _id = std::string()) |
968 const std::string& _id = std::string()) |
945 : Parent(_writer), id(_id), |
969 : Parent(_writer), id(_id), |
946 idWriter(new IdWriter<typename Graph::UndirEdge, _IdWriter> |
970 undirEdgeIdWriter(new IdWriter<UndirEdge, _IdWriter>(_idWriter)), |
947 (_idWriter)) {} |
971 edgeIdWriter(new IdWriter<Edge, _IdWriter>(_idWriter)) {} |
948 |
972 |
949 /// \brief Destructor. |
973 /// \brief Destructor. |
950 /// |
974 /// |
951 /// Destructor for UndirEdgeWriter. |
975 /// Destructor for UndirEdgeWriter. |
952 virtual ~UndirEdgeWriter() {} |
976 virtual ~UndirEdgeWriter() {} |
954 UndirEdgeWriter(const UndirEdgeWriter&); |
978 UndirEdgeWriter(const UndirEdgeWriter&); |
955 void operator=(const UndirEdgeWriter&); |
979 void operator=(const UndirEdgeWriter&); |
956 |
980 |
957 public: |
981 public: |
958 |
982 |
|
983 /// \brief Add an edge writer command for the UndirEdgeWriter. |
|
984 /// |
|
985 /// Add an edge writer command for the UndirEdgeWriter. |
|
986 void writeEdge(const std::string& name, const Edge& item) { |
|
987 edgeWriters.push_back(make_pair(name, &item)); |
|
988 } |
|
989 |
959 /// \brief Add an undirected edge writer command for the UndirEdgeWriter. |
990 /// \brief Add an undirected edge writer command for the UndirEdgeWriter. |
960 /// |
991 /// |
961 /// Add an edge writer command for the UndirEdgeWriter. |
992 /// Add an undirected edge writer command for the UndirEdgeWriter. |
962 void writeUndirEdge(const std::string& name, const Item& item) { |
993 void writeUndirEdge(const std::string& name, const UndirEdge& item) { |
963 writers.push_back(make_pair(name, &item)); |
994 undirEdgeWriters.push_back(make_pair(name, &item)); |
964 } |
995 } |
965 |
996 |
966 protected: |
997 protected: |
967 |
998 |
968 /// \brief Header checking function. |
999 /// \brief Header checking function. |
969 /// |
1000 /// |
970 /// It gives back true when the header line start with \c \@undiredges, |
1001 /// It gives back true when the header line start with \c \@undiredges, |
971 /// and the header line's id and the writer's id are the same. |
1002 /// and the header line's id and the writer's id are the same. |
972 virtual std::string header() { |
1003 virtual std::string header() { |
973 return "@edges " + id; |
1004 return "@undiredges " + id; |
974 } |
1005 } |
975 |
1006 |
976 /// \brief Writer function of the section. |
1007 /// \brief Writer function of the section. |
977 /// |
1008 /// |
978 /// Write the content of the section. |
1009 /// Write the content of the section. |
979 virtual void write(std::ostream& os) { |
1010 virtual void write(std::ostream& os) { |
980 for (int i = 0; i < (int)writers.size(); ++i) { |
1011 for (int i = 0; i < (int)undirEdgeWriters.size(); ++i) { |
981 os << writers[i].first << ' '; |
1012 os << undirEdgeWriters[i].first << ' '; |
982 idWriter->write(os, *(writers[i].second)); |
1013 undirEdgeIdWriter->write(os, *(undirEdgeWriters[i].second)); |
983 os << std::endl; |
1014 os << std::endl; |
984 } |
1015 } |
|
1016 for (int i = 0; i < (int)edgeWriters.size(); ++i) { |
|
1017 os << edgeWriters[i].first << ' '; |
|
1018 edgeIdWriter->write(os, *(edgeWriters[i].second)); |
|
1019 os << std::endl; |
|
1020 } |
985 } |
1021 } |
986 |
1022 |
987 private: |
1023 private: |
988 |
1024 |
989 std::string id; |
1025 std::string id; |
990 |
1026 |
991 typedef std::vector<std::pair<std::string, const Item*> > ItemWriters; |
1027 typedef std::vector<std::pair<std::string, |
992 ItemWriters writers; |
1028 const UndirEdge*> > UndirEdgeWriters; |
993 |
1029 UndirEdgeWriters undirEdgeWriters; |
994 std::auto_ptr<IdWriterBase<Item> > idWriter; |
1030 std::auto_ptr<IdWriterBase<UndirEdge> > undirEdgeIdWriter; |
|
1031 |
|
1032 typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters; |
|
1033 EdgeWriters edgeWriters; |
|
1034 std::auto_ptr<IdWriterBase<Edge> > edgeIdWriter; |
|
1035 |
995 }; |
1036 }; |
996 |
1037 |
997 /// \ingroup io_group |
1038 /// \ingroup io_group |
998 /// \brief SectionWriter for attributes. |
1039 /// \brief SectionWriter for attributes. |
999 /// |
1040 /// |