lemon/graph_writer.h
changeset 2502 9c23c3762bc5
parent 2467 2025a571895e
child 2553 bfced05fa852
     1.1 --- a/lemon/graph_writer.h	Sat Oct 20 14:29:12 2007 +0000
     1.2 +++ b/lemon/graph_writer.h	Wed Oct 24 16:31:49 2007 +0000
     1.3 @@ -156,7 +156,7 @@
     1.4  
     1.5      /// \brief Issue a new node map writing command for the writer.
     1.6      ///
     1.7 -   /// This function issues a new <i> node map writing command</i> to the writer.
     1.8 +    /// This function issues a new <i> node map writing command</i> to the writer.
     1.9      template <typename Map>
    1.10      GraphWriter& writeNodeMap(std::string label, const Map& map) {
    1.11        nodeset_writer.writeNodeMap(label, map);
    1.12 @@ -166,7 +166,7 @@
    1.13  
    1.14      /// \brief Issue a new node map writing command for the writer.
    1.15      ///
    1.16 -   /// This function issues a new <i> node map writing command</i> to the writer.
    1.17 +    /// This function issues a new <i> node map writing command</i> to the writer.
    1.18      template <typename ItemWriter, typename Map>
    1.19      GraphWriter& writeNodeMap(std::string label, const Map& map, 
    1.20  			      const ItemWriter& iw = ItemWriter()) {
    1.21 @@ -639,6 +639,376 @@
    1.22      AttributeWriter<WriterTraits> attribute_writer;
    1.23    };
    1.24  
    1.25 +  /// \brief The bipartite graph writer class.
    1.26 +  ///
    1.27 +  /// The \c BpUGraphWriter class provides the ugraph output. To write 
    1.28 +  /// a graph you should first give writing commands to the writer. You can 
    1.29 +  /// declare write command as \c NodeMap, \c EdgeMap or \c UEdgeMap 
    1.30 +  /// writing and labeled Node, Edge or UEdge writing.
    1.31 +  ///
    1.32 +  ///\code
    1.33 +  /// BpUGraphWriter<ListUGraph> writer(std::cout, graph);
    1.34 +  ///\endcode
    1.35 +  ///
    1.36 +  /// The \c writeNodeMap() function declares a \c NodeMap writing 
    1.37 +  /// command in the \c BpUGraphWriter. You should give as parameter 
    1.38 +  /// the name of the map and the map object. The NodeMap writing 
    1.39 +  /// command with name "label" should write a unique map because it 
    1.40 +  /// is regarded as label map.
    1.41 +  ///
    1.42 +  ///\code
    1.43 +  /// IdMap<ListUGraph, Node> nodeLabelMap;
    1.44 +  /// writer.writeNodeMap("label", nodeLabelMap);
    1.45 +  ///
    1.46 +  /// writer.writeNodeMap("coords", coords);
    1.47 +  /// writer.writeNodeMap("color", colorMap);
    1.48 +  ///\endcode
    1.49 +  ///
    1.50 +  /// With the \c writeUEdgeMap() member function you can give an 
    1.51 +  /// undirected edge map writing command similar to the NodeMaps.
    1.52 +  ///
    1.53 +  ///\code
    1.54 +  /// DescriptorMap<ListGraph, Edge, ListGraph::EdgeMap<int> > 
    1.55 +  ///   edgeDescMap(graph);
    1.56 +  /// writer.writeUEdgeMap("descriptor", edgeDescMap);
    1.57 +  ///
    1.58 +  /// writer.writeUEdgeMap("weight", weightMap);
    1.59 +  /// writer.writeUEdgeMap("label", labelMap);
    1.60 +  ///\endcode
    1.61 +  /// 
    1.62 +  /// The EdgeMap handling is just a syntactical sugar. It writes
    1.63 +  /// two undirected edge map with '+' and '-' prefix in the name.
    1.64 +  ///
    1.65 +  ///\code
    1.66 +  /// writer.writeEdgeMap("capacity", capacityMap);
    1.67 +  ///\endcode
    1.68 +  ///
    1.69 +  ///
    1.70 +  /// With \c writeNode() and \c writeUEdge() functions you can 
    1.71 +  /// designate nodes and undirected edges in the graph. For example, you can 
    1.72 +  /// write out the source and target of the graph.
    1.73 +  ///
    1.74 +  ///\code
    1.75 +  /// writer.writeNode("source", sourceNode);
    1.76 +  /// writer.writeNode("target", targetNode);
    1.77 +  ///
    1.78 +  /// writer.writeUEdge("observed", uEdge);
    1.79 +  ///\endcode
    1.80 +  ///
    1.81 +  /// After you give all write commands you must call the \c run() member
    1.82 +  /// function, which executes all the writing commands.
    1.83 +  ///
    1.84 +  ///\code
    1.85 +  /// writer.run();
    1.86 +  ///\endcode
    1.87 +  ///
    1.88 +  /// \see DefaultWriterTraits
    1.89 +  /// \see QuotedStringWriter
    1.90 +  /// \see IdMap
    1.91 +  /// \see DescriptorMap
    1.92 +  /// \see \ref GraphWriter
    1.93 +  /// \see \ref graph-io-page
    1.94 +  /// \author Balazs Dezso
    1.95 +  template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 
    1.96 +  class BpUGraphWriter {
    1.97 +  public:
    1.98 +    
    1.99 +    typedef _Graph Graph;
   1.100 +    typedef typename Graph::Node Node;
   1.101 +    typedef typename Graph::Edge Edge;
   1.102 +    typedef typename Graph::UEdge UEdge;
   1.103 +
   1.104 +    typedef _WriterTraits WriterTraits;
   1.105 +
   1.106 +    /// \brief Construct a new BpUGraphWriter.
   1.107 +    ///
   1.108 +    /// Construct a new BpUGraphWriter. It writes the given graph
   1.109 +    /// to the given stream.
   1.110 +    BpUGraphWriter(std::ostream& _os, const Graph& _graph) 
   1.111 +      : writer(new LemonWriter(_os)), own_writer(true), 
   1.112 +	nodeset_writer(*writer, _graph, std::string()),
   1.113 +	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   1.114 +	node_writer(*writer, nodeset_writer, std::string()),
   1.115 +	uedge_writer(*writer, uedgeset_writer, std::string()),
   1.116 +	attribute_writer(*writer, std::string()) {}
   1.117 +
   1.118 +    /// \brief Construct a new BpUGraphWriter.
   1.119 +    ///
   1.120 +    /// Construct a new BpUGraphWriter. It writes the given graph
   1.121 +    /// to the given file.
   1.122 +    BpUGraphWriter(const std::string& _filename, const Graph& _graph) 
   1.123 +      : writer(new LemonWriter(_filename)), own_writer(true), 
   1.124 +	nodeset_writer(*writer, _graph, std::string()),
   1.125 +	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   1.126 +	node_writer(*writer, nodeset_writer, std::string()),
   1.127 +	uedge_writer(*writer, uedgeset_writer, std::string()),
   1.128 +	attribute_writer(*writer, std::string()) {}
   1.129 +
   1.130 +    /// \brief Construct a new BpUGraphWriter.
   1.131 +    ///
   1.132 +    /// Construct a new BpUGraphWriter. It writes the given graph
   1.133 +    /// to given LemonWriter.
   1.134 +    BpUGraphWriter(LemonWriter& _writer, const Graph& _graph)
   1.135 +      : writer(_writer), own_writer(false), 
   1.136 +	nodeset_writer(*writer, _graph, std::string()),
   1.137 +	uedgeset_writer(*writer, _graph, nodeset_writer, std::string()),
   1.138 +	node_writer(*writer, nodeset_writer, std::string()),
   1.139 +	uedge_writer(*writer, uedgeset_writer, std::string()),
   1.140 +	attribute_writer(*writer, std::string()) {}
   1.141 +
   1.142 +    /// \brief Destruct the graph writer.
   1.143 +    ///
   1.144 +    /// Destruct the graph writer.
   1.145 +    ~BpUGraphWriter() {
   1.146 +      if (own_writer) 
   1.147 +	delete writer;
   1.148 +    }
   1.149 +
   1.150 +    /// \brief Issue a new node map writing command to the writer.
   1.151 +    ///
   1.152 +    /// This function issues a new <i> node map writing command</i> to
   1.153 +    /// the writer.
   1.154 +    template <typename Map>
   1.155 +    BpUGraphWriter& writeNodeMap(std::string label, const Map& map) {
   1.156 +      nodeset_writer.writeNodeMap(label, map);
   1.157 +      return *this;
   1.158 +    }
   1.159 +
   1.160 +    /// \brief Issue a new node map writing command to the writer.
   1.161 +    ///
   1.162 +    /// This function issues a new <i> node map writing command</i> to
   1.163 +    /// the writer.
   1.164 +    template <typename ItemWriter, typename Map>
   1.165 +    BpUGraphWriter& writeNodeMap(std::string label, const Map& map, 
   1.166 +			      const ItemWriter& iw = ItemWriter()) {
   1.167 +      nodeset_writer.writeNodeMap(label, map, iw);
   1.168 +      return *this;
   1.169 +    }
   1.170 +
   1.171 +    /// \brief Issue a new A-node map writing command to the writer.
   1.172 +    ///
   1.173 +    /// This function issues a new <i> A-node map writing command</i> to
   1.174 +    /// the writer.
   1.175 +    template <typename Map>
   1.176 +    BpUGraphWriter& writeANodeMap(std::string label, const Map& map) {
   1.177 +      nodeset_writer.writeANodeMap(label, map);
   1.178 +      return *this;
   1.179 +    }
   1.180 +
   1.181 +    /// \brief Issue a new A-node map writing command to the writer.
   1.182 +    ///
   1.183 +    /// This function issues a new <i> A-node map writing command</i> to
   1.184 +    /// the writer.
   1.185 +    template <typename ItemWriter, typename Map>
   1.186 +    BpUGraphWriter& writeANodeMap(std::string label, const Map& map, 
   1.187 +			      const ItemWriter& iw = ItemWriter()) {
   1.188 +      nodeset_writer.writeANodeMap(label, map, iw);
   1.189 +      return *this;
   1.190 +    }
   1.191 +    /// \brief Issue a new B-node map writing command to the writer.
   1.192 +    ///
   1.193 +    /// This function issues a new <i> B-node map writing command</i> to
   1.194 +    /// the writer.
   1.195 +    template <typename Map>
   1.196 +    BpUGraphWriter& writeBNodeMap(std::string label, const Map& map) {
   1.197 +      nodeset_writer.writeBNodeMap(label, map);
   1.198 +      return *this;
   1.199 +    }
   1.200 +
   1.201 +    /// \brief Issue a new B-node map writing command to the writer.
   1.202 +    ///
   1.203 +    /// This function issues a new <i> B-node map writing command</i> to
   1.204 +    /// the writer.
   1.205 +    template <typename ItemWriter, typename Map>
   1.206 +    BpUGraphWriter& writeBNodeMap(std::string label, const Map& map, 
   1.207 +			      const ItemWriter& iw = ItemWriter()) {
   1.208 +      nodeset_writer.writeBNodeMap(label, map, iw);
   1.209 +      return *this;
   1.210 +    }
   1.211 +
   1.212 +    /// \brief Issue a new edge map writing command to the writer.
   1.213 +    ///
   1.214 +    /// This function issues a new <i> edge map writing command</i> to
   1.215 +    /// the writer.
   1.216 +    template <typename Map>
   1.217 +    BpUGraphWriter& writeEdgeMap(std::string label, const Map& map) { 
   1.218 +      uedgeset_writer.writeEdgeMap(label, map);
   1.219 +      return *this;
   1.220 +    }
   1.221 +
   1.222 +    /// \brief Issue a new edge map writing command to the writer.
   1.223 +    ///
   1.224 +    /// This function issues a new <i> edge map writing command</i> to
   1.225 +    /// the writer.
   1.226 +    template <typename ItemWriter, typename Map>
   1.227 +    BpUGraphWriter& writeEdgeMap(std::string label, const Map& map,
   1.228 +				   const ItemWriter& iw = ItemWriter()) {
   1.229 +      uedgeset_writer.writeEdgeMap(label, map, iw);
   1.230 +      return *this;
   1.231 +    }
   1.232 +
   1.233 +    /// \brief Issue a new undirected edge map writing command to the writer.
   1.234 +    ///
   1.235 +    /// This function issues a new <i> undirected edge map writing
   1.236 +    /// command</i> to the writer.
   1.237 +    template <typename Map>
   1.238 +    BpUGraphWriter& writeUEdgeMap(std::string label, const Map& map) { 
   1.239 +      uedgeset_writer.writeUEdgeMap(label, map);
   1.240 +      return *this;
   1.241 +    }
   1.242 +
   1.243 +    /// \brief Issue a new undirected edge map writing command to the writer.
   1.244 +    ///
   1.245 +    /// This function issues a new <i> undirected edge map writing
   1.246 +    /// command</i> to the writer.
   1.247 +   template <typename ItemWriter, typename Map>
   1.248 +    BpUGraphWriter& writeUEdgeMap(std::string label, const Map& map,
   1.249 +					const ItemWriter& iw = ItemWriter()) {
   1.250 +      uedgeset_writer.writeUEdgeMap(label, map, iw);
   1.251 +      return *this;
   1.252 +    }
   1.253 +
   1.254 +    /// \brief Issue a new labeled node writer to the writer.
   1.255 +    ///
   1.256 +    /// This function issues a new <i> labeled node writing
   1.257 +    /// command</i> to the writer.
   1.258 +    BpUGraphWriter& writeNode(std::string label, const Node& node) {
   1.259 +      node_writer.writeNode(label, node);
   1.260 +      return *this;
   1.261 +    }
   1.262 +
   1.263 +    /// \brief Issue a new labeled edge writer to the writer.
   1.264 +    ///
   1.265 +    /// This function issues a new <i> labeled edge writing
   1.266 +    /// command</i> to the writer.
   1.267 +    BpUGraphWriter& writeEdge(std::string label, const Edge& edge) {
   1.268 +      uedge_writer.writeEdge(label, edge);
   1.269 +    }
   1.270 +
   1.271 +    /// \brief Issue a new labeled undirected edge writing command to
   1.272 +    /// the writer.
   1.273 +    ///
   1.274 +    /// Issue a new <i>labeled undirected edge writing command</i> to
   1.275 +    /// the writer.
   1.276 +    BpUGraphWriter& writeUEdge(std::string label, const UEdge& edge) {
   1.277 +      uedge_writer.writeUEdge(label, edge);
   1.278 +    }
   1.279 +
   1.280 +    /// \brief Issue a new attribute writing command.
   1.281 +    ///
   1.282 +    /// This function issues a new <i> attribute writing
   1.283 +    /// command</i> to the writer.
   1.284 +    template <typename Value>
   1.285 +    BpUGraphWriter& writeAttribute(std::string label, const Value& value) {
   1.286 +      attribute_writer.writeAttribute(label, value);
   1.287 +      return *this;
   1.288 +    }
   1.289 +    
   1.290 +    /// \brief Issue a new attribute writing command.
   1.291 +    ///
   1.292 +    /// This function issues a new <i> attribute writing
   1.293 +    /// command</i> to the writer.
   1.294 +    template <typename ItemWriter, typename Value>
   1.295 +    BpUGraphWriter& writeAttribute(std::string label, const Value& value, 
   1.296 +			       const ItemWriter& iw = ItemWriter()) {
   1.297 +      attribute_writer.writeAttribute(label, value, iw);
   1.298 +      return *this;
   1.299 +    }
   1.300 +
   1.301 +    /// \brief Conversion operator to LemonWriter.
   1.302 +    ///
   1.303 +    /// Conversion operator to LemonWriter. It makes possible
   1.304 +    /// to access the encapsulated \e LemonWriter, this way
   1.305 +    /// you can attach to this writer new instances of 
   1.306 +    /// \e LemonWriter::SectionWriter.
   1.307 +    operator LemonWriter&() {
   1.308 +      return *writer;
   1.309 +    }
   1.310 +
   1.311 +    /// \brief Executes the writing commands.
   1.312 +    ///
   1.313 +    /// Executes the writing commands.
   1.314 +    void run() {
   1.315 +      writer->run();
   1.316 +    }
   1.317 +
   1.318 +    /// \brief Returns true if the writer can give back the labels by the items.
   1.319 +    ///
   1.320 +    /// Returns true if the writer can give back the the labels by the items.
   1.321 +    bool isLabelWriter() const {
   1.322 +      return nodeset_writer.isLabelWriter() && 
   1.323 +        uedgeset_writer.isLabelWriter();
   1.324 +    }
   1.325 +
   1.326 +    /// \brief Write the label of the given node.
   1.327 +    ///
   1.328 +    /// It writes the label of the given node. If there was written a "label"
   1.329 +    /// named node map then it will write the map value belonging to the node.
   1.330 +    void writeLabel(std::ostream& os, const Node& item) const {
   1.331 +      nodeset_writer.writeLabel(os, item);
   1.332 +    } 
   1.333 +
   1.334 +    /// \brief Write the label of the given edge.
   1.335 +    ///
   1.336 +    /// It writes the label of the given edge. If there was written a "label"
   1.337 +    /// named edge map then it will write the map value belonging to the edge.
   1.338 +    void writeLabel(std::ostream& os, const Edge& item) const {
   1.339 +      uedgeset_writer.writeLabel(os, item);
   1.340 +    } 
   1.341 +
   1.342 +    /// \brief Write the label of the given undirected edge.
   1.343 +    ///
   1.344 +    /// It writes the label of the given undirected edge. If there was
   1.345 +    /// written a "label" named edge map then it will write the map
   1.346 +    /// value belonging to the edge.
   1.347 +    void writeLabel(std::ostream& os, const UEdge& item) const {
   1.348 +      uedgeset_writer.writeLabel(os, item);
   1.349 +    } 
   1.350 +
   1.351 +    /// \brief Sorts the given node vector by label.
   1.352 +    ///
   1.353 +    /// Sorts the given node vector by label. If there was written an
   1.354 +    /// "label" named map then the vector will be sorted by the values
   1.355 +    /// of this map. Otherwise if the \c forceLabel parameter was true
   1.356 +    /// it will be sorted by its id in the graph.
   1.357 +    void sortByLabel(std::vector<Node>& nodes) const {
   1.358 +      nodeset_writer.sortByLabel(nodes);
   1.359 +    }
   1.360 +
   1.361 +    /// \brief Sorts the given edge vector by label.
   1.362 +    ///
   1.363 +    /// Sorts the given edge vector by label. If there was written an
   1.364 +    /// "label" named map then the vector will be sorted by the values
   1.365 +    /// of this map. Otherwise if the \c forceLabel parameter was true
   1.366 +    /// it will be sorted by its id in the graph.
   1.367 +    void sortByLabel(std::vector<Edge>& edges) const {
   1.368 +      uedgeset_writer.sortByLabel(edges);
   1.369 +    }
   1.370 +
   1.371 +    /// \brief Sorts the given undirected edge vector by label.
   1.372 +    ///
   1.373 +    /// Sorts the given undirected edge vector by label. If there was
   1.374 +    /// written an "label" named map then the vector will be sorted by
   1.375 +    /// the values of this map. Otherwise if the \c forceLabel
   1.376 +    /// parameter was true it will be sorted by its id in the graph.
   1.377 +    void sortByLabel(std::vector<UEdge>& uedges) const {
   1.378 +      uedgeset_writer.sortByLabel(uedges);
   1.379 +    }
   1.380 +
   1.381 +  private:
   1.382 +
   1.383 +    LemonWriter* writer;
   1.384 +    bool own_writer;
   1.385 +
   1.386 +    BpNodeSetWriter<Graph, WriterTraits> nodeset_writer;
   1.387 +    UEdgeSetWriter<Graph, WriterTraits> uedgeset_writer;
   1.388 +
   1.389 +    NodeWriter<Graph> node_writer;
   1.390 +    UEdgeWriter<Graph> uedge_writer;
   1.391 +    
   1.392 +    AttributeWriter<WriterTraits> attribute_writer;
   1.393 +  };
   1.394 +
   1.395    /// @}
   1.396  
   1.397  }