... | ... |
@@ -841,193 +841,193 @@ |
841 | 841 |
if (!_skip_nodes) { |
842 | 842 |
writeNodes(); |
843 | 843 |
} else { |
844 | 844 |
createNodeIndex(); |
845 | 845 |
} |
846 | 846 |
if (!_skip_arcs) { |
847 | 847 |
writeArcs(); |
848 | 848 |
} else { |
849 | 849 |
createArcIndex(); |
850 | 850 |
} |
851 | 851 |
writeAttributes(); |
852 | 852 |
} |
853 | 853 |
|
854 | 854 |
/// \brief Give back the stream of the writer |
855 | 855 |
/// |
856 | 856 |
/// Give back the stream of the writer. |
857 | 857 |
std::ostream& ostream() { |
858 | 858 |
return *_os; |
859 | 859 |
} |
860 | 860 |
|
861 | 861 |
/// @} |
862 | 862 |
}; |
863 | 863 |
|
864 | 864 |
/// \brief Return a \ref DigraphWriter class |
865 | 865 |
/// |
866 | 866 |
/// This function just returns a \ref DigraphWriter class. |
867 | 867 |
/// \relates DigraphWriter |
868 | 868 |
template <typename Digraph> |
869 | 869 |
DigraphWriter<Digraph> digraphWriter(std::ostream& os, |
870 | 870 |
const Digraph& digraph) { |
871 | 871 |
DigraphWriter<Digraph> tmp(os, digraph); |
872 | 872 |
return tmp; |
873 | 873 |
} |
874 | 874 |
|
875 | 875 |
/// \brief Return a \ref DigraphWriter class |
876 | 876 |
/// |
877 | 877 |
/// This function just returns a \ref DigraphWriter class. |
878 | 878 |
/// \relates DigraphWriter |
879 | 879 |
template <typename Digraph> |
880 | 880 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn, |
881 | 881 |
const Digraph& digraph) { |
882 | 882 |
DigraphWriter<Digraph> tmp(fn, digraph); |
883 | 883 |
return tmp; |
884 | 884 |
} |
885 | 885 |
|
886 | 886 |
/// \brief Return a \ref DigraphWriter class |
887 | 887 |
/// |
888 | 888 |
/// This function just returns a \ref DigraphWriter class. |
889 | 889 |
/// \relates DigraphWriter |
890 | 890 |
template <typename Digraph> |
891 | 891 |
DigraphWriter<Digraph> digraphWriter(const char* fn, |
892 | 892 |
const Digraph& digraph) { |
893 | 893 |
DigraphWriter<Digraph> tmp(fn, digraph); |
894 | 894 |
return tmp; |
895 | 895 |
} |
896 | 896 |
|
897 | 897 |
template <typename Graph> |
898 | 898 |
class GraphWriter; |
899 | 899 |
|
900 | 900 |
template <typename Graph> |
901 | 901 |
GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph); |
902 | 902 |
|
903 | 903 |
template <typename Graph> |
904 | 904 |
GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph); |
905 | 905 |
|
906 | 906 |
template <typename Graph> |
907 | 907 |
GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph); |
908 | 908 |
|
909 | 909 |
/// \ingroup lemon_io |
910 | 910 |
/// |
911 | 911 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
912 | 912 |
/// |
913 | 913 |
/// This utility writes an \ref lgf-format "LGF" file. |
914 | 914 |
/// |
915 | 915 |
/// It can be used almost the same way as \c DigraphWriter. |
916 | 916 |
/// The only difference is that this class can handle edges and |
917 | 917 |
/// edge maps as well as arcs and arc maps. |
918 | 918 |
/// |
919 | 919 |
/// The arc maps are written into the file as two columns, the |
920 | 920 |
/// caption of the columns are the name of the map prefixed with \c |
921 | 921 |
/// '+' and \c '-'. The arcs are written into the \c \@attributes |
922 | 922 |
/// section as a \c '+' or a \c '-' prefix (depends on the direction |
923 | 923 |
/// of the arc) and the label of corresponding edge. |
924 | 924 |
template <typename _Graph> |
925 | 925 |
class GraphWriter { |
926 | 926 |
public: |
927 | 927 |
|
928 | 928 |
typedef _Graph Graph; |
929 | 929 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
930 | 930 |
|
931 | 931 |
private: |
932 | 932 |
|
933 | 933 |
|
934 | 934 |
std::ostream* _os; |
935 | 935 |
bool local_os; |
936 | 936 |
|
937 |
Graph& _graph; |
|
937 |
const Graph& _graph; |
|
938 | 938 |
|
939 | 939 |
std::string _nodes_caption; |
940 | 940 |
std::string _edges_caption; |
941 | 941 |
std::string _attributes_caption; |
942 | 942 |
|
943 | 943 |
typedef std::map<Node, std::string> NodeIndex; |
944 | 944 |
NodeIndex _node_index; |
945 | 945 |
typedef std::map<Edge, std::string> EdgeIndex; |
946 | 946 |
EdgeIndex _edge_index; |
947 | 947 |
|
948 | 948 |
typedef std::vector<std::pair<std::string, |
949 | 949 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
950 | 950 |
NodeMaps _node_maps; |
951 | 951 |
|
952 | 952 |
typedef std::vector<std::pair<std::string, |
953 | 953 |
_writer_bits::MapStorageBase<Edge>* > >EdgeMaps; |
954 | 954 |
EdgeMaps _edge_maps; |
955 | 955 |
|
956 | 956 |
typedef std::vector<std::pair<std::string, |
957 | 957 |
_writer_bits::ValueStorageBase*> > Attributes; |
958 | 958 |
Attributes _attributes; |
959 | 959 |
|
960 | 960 |
bool _skip_nodes; |
961 | 961 |
bool _skip_edges; |
962 | 962 |
|
963 | 963 |
public: |
964 | 964 |
|
965 | 965 |
/// \brief Constructor |
966 | 966 |
/// |
967 | 967 |
/// Construct a directed graph writer, which writes to the given |
968 | 968 |
/// output stream. |
969 | 969 |
GraphWriter(std::ostream& is, const Graph& graph) |
970 | 970 |
: _os(&is), local_os(false), _graph(graph), |
971 | 971 |
_skip_nodes(false), _skip_edges(false) {} |
972 | 972 |
|
973 | 973 |
/// \brief Constructor |
974 | 974 |
/// |
975 | 975 |
/// Construct a directed graph writer, which writes to the given |
976 | 976 |
/// output file. |
977 | 977 |
GraphWriter(const std::string& fn, const Graph& graph) |
978 | 978 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), |
979 | 979 |
_skip_nodes(false), _skip_edges(false) {} |
980 | 980 |
|
981 | 981 |
/// \brief Constructor |
982 | 982 |
/// |
983 | 983 |
/// Construct a directed graph writer, which writes to the given |
984 | 984 |
/// output file. |
985 | 985 |
GraphWriter(const char* fn, const Graph& graph) |
986 | 986 |
: _os(new std::ofstream(fn)), local_os(true), _graph(graph), |
987 | 987 |
_skip_nodes(false), _skip_edges(false) {} |
988 | 988 |
|
989 | 989 |
/// \brief Destructor |
990 | 990 |
~GraphWriter() { |
991 | 991 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
992 | 992 |
it != _node_maps.end(); ++it) { |
993 | 993 |
delete it->second; |
994 | 994 |
} |
995 | 995 |
|
996 | 996 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
997 | 997 |
it != _edge_maps.end(); ++it) { |
998 | 998 |
delete it->second; |
999 | 999 |
} |
1000 | 1000 |
|
1001 | 1001 |
for (typename Attributes::iterator it = _attributes.begin(); |
1002 | 1002 |
it != _attributes.end(); ++it) { |
1003 | 1003 |
delete it->second; |
1004 | 1004 |
} |
1005 | 1005 |
|
1006 | 1006 |
if (local_os) { |
1007 | 1007 |
delete _os; |
1008 | 1008 |
} |
1009 | 1009 |
} |
1010 | 1010 |
|
1011 | 1011 |
private: |
1012 | 1012 |
|
1013 | 1013 |
friend GraphWriter<Graph> graphWriter<>(std::ostream& os, |
1014 | 1014 |
const Graph& graph); |
1015 | 1015 |
friend GraphWriter<Graph> graphWriter<>(const std::string& fn, |
1016 | 1016 |
const Graph& graph); |
1017 | 1017 |
friend GraphWriter<Graph> graphWriter<>(const char *fn, |
1018 | 1018 |
const Graph& graph); |
1019 | 1019 |
|
1020 | 1020 |
GraphWriter(GraphWriter& other) |
1021 | 1021 |
: _os(other._os), local_os(other.local_os), _graph(other._graph), |
1022 | 1022 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
1023 | 1023 |
|
1024 | 1024 |
other._os = 0; |
1025 | 1025 |
other.local_os = false; |
1026 | 1026 |
|
1027 | 1027 |
_node_index.swap(other._node_index); |
1028 | 1028 |
_edge_index.swap(other._edge_index); |
1029 | 1029 |
|
1030 | 1030 |
_node_maps.swap(other._node_maps); |
1031 | 1031 |
_edge_maps.swap(other._edge_maps); |
1032 | 1032 |
_attributes.swap(other._attributes); |
1033 | 1033 |
|
0 comments (0 inline)