graphviz.hpp

来自「CGAL is a collaborative effort of severa」· HPP 代码 · 共 463 行 · 第 1/2 页

HPP
463
字号
//=======================================================================// (C) Copyright Jeremy Siek 2003.// Permission to copy, use, modify,// sell and distribute this software is granted provided this// copyright notice appears in all copies. This software is provided// "as is" without express or implied warranty, and with no claim as// to its suitability for any purpose.//=======================================================================// Copyright 2001 University of Notre Dame.// Author: Lie-Quan Lee//// This file is part of the Boost Graph Library//// You should have received a copy of the License Agreement for the// Boost Graph Library along with the software; see the file LICENSE.// If not, contact Office of Research, University of Notre Dame, Notre// Dame, IN 46556.//// Permission to modify the code and to distribute modified code is// granted, provided the text of this NOTICE is retained, a notice that// the code was modified is included with the above COPYRIGHT NOTICE and// with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE// file is distributed with the modified code.//// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.// By way of example, but not limitation, Licensor MAKES NO// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS// OR OTHER RIGHTS.//=======================================================================#ifndef BOOST_GRAPHVIZ_HPP#define BOOST_GRAPHVIZ_HPP#include <boost/config.hpp>#include <string>#include <map>#include <iostream>#include <fstream>#include <stdio.h> // for FILE#include <boost/property_map.hpp>#include <boost/tuple/tuple.hpp>#include <boost/graph/graph_traits.hpp>#include <boost/graph/properties.hpp>#include <boost/graph/subgraph.hpp>#include <boost/graph/adjacency_list.hpp>namespace boost {  template <typename directed_category>  struct graphviz_io_traits {    static std::string name() {      return "digraph";    }    static std::string delimiter() {      return "->";    }  };  template <>  struct graphviz_io_traits <undirected_tag> {    static std::string name() {      return "graph";    }    static std::string delimiter() {      return "--";    }  };  struct default_writer {    void operator()(std::ostream&) const {    }    template <class VorE>    void operator()(std::ostream&, const VorE&) const {    }  };  template <class Name>  class label_writer {  public:    label_writer(Name _name) : name(_name) {}    template <class VertexOrEdge>    void operator()(std::ostream& out, const VertexOrEdge& v) const {      out << "[label=\"" << name[v] << "\"]";    }  private:    Name name;  };  template <class Name>  inline label_writer<Name>  make_label_writer(Name n) {    return label_writer<Name>(n);  }  enum edge_attribute_t        { edge_attribute        = 1111 };  enum vertex_attribute_t      { vertex_attribute      = 2222 };  enum graph_graph_attribute_t { graph_graph_attribute = 3333 };  enum graph_vertex_attribute_t  { graph_vertex_attribute  = 4444 };  enum graph_edge_attribute_t  { graph_edge_attribute  = 5555 };  BOOST_INSTALL_PROPERTY(edge, attribute);  BOOST_INSTALL_PROPERTY(vertex, attribute);  BOOST_INSTALL_PROPERTY(graph, graph_attribute);  BOOST_INSTALL_PROPERTY(graph, vertex_attribute);  BOOST_INSTALL_PROPERTY(graph, edge_attribute);  template <class Attribute>  inline void write_attributes(const Attribute& attr, std::ostream& out) {    typename Attribute::const_iterator i, iend;    i    = attr.begin();    iend = attr.end();    while ( i != iend ) {      out << i->first << "=\"" << i->second << "\"";      ++i;      if ( i != iend )        out << ", ";    }  }  template<typename Attributes>  inline void write_all_attributes(Attributes attributes,                                   const std::string& name,                                   std::ostream& out)  {    typename Attributes::const_iterator i = attributes.begin(),                                        end = attributes.end();    if (i != end) {      out << name << " [\n";      write_attributes(attributes, out);      out << "];\n";    }  }  inline void write_all_attributes(detail::error_property_not_found,                                   const std::string&,                                   std::ostream&)  {    // Do nothing - no attributes exist  }  template <typename GraphGraphAttributes,            typename GraphNodeAttributes,            typename GraphEdgeAttributes>  struct graph_attributes_writer  {    graph_attributes_writer(GraphGraphAttributes gg,                            GraphNodeAttributes gn,                            GraphEdgeAttributes ge)      : g_attributes(gg), n_attributes(gn), e_attributes(ge) { }    void operator()(std::ostream& out) const {      write_all_attributes(g_attributes, "graph", out);      write_all_attributes(n_attributes, "node", out);      write_all_attributes(e_attributes, "edge", out);    }    GraphGraphAttributes g_attributes;    GraphNodeAttributes n_attributes;    GraphEdgeAttributes e_attributes;  };  template <typename GAttrMap, typename NAttrMap, typename EAttrMap>  graph_attributes_writer<GAttrMap, NAttrMap, EAttrMap>  make_graph_attributes_writer(const GAttrMap& g_attr, const NAttrMap& n_attr,                              const EAttrMap& e_attr) {    return graph_attributes_writer<GAttrMap, NAttrMap, EAttrMap>      (g_attr, n_attr, e_attr);  }  template <typename Graph>  graph_attributes_writer    <typename graph_property<Graph, graph_graph_attribute_t>::type,     typename graph_property<Graph, graph_vertex_attribute_t>::type,     typename graph_property<Graph, graph_edge_attribute_t>::type>  make_graph_attributes_writer(const Graph& g)  {    typedef typename graph_property<Graph, graph_graph_attribute_t>::type      GAttrMap;    typedef typename graph_property<Graph, graph_vertex_attribute_t>::type      NAttrMap;    typedef typename graph_property<Graph, graph_edge_attribute_t>::type      EAttrMap;    GAttrMap gam = get_property(g, graph_graph_attribute);    NAttrMap nam = get_property(g, graph_vertex_attribute);    EAttrMap eam = get_property(g, graph_edge_attribute);    graph_attributes_writer<GAttrMap, NAttrMap, EAttrMap> writer(gam, nam, eam);    return writer;  }  template <typename AttributeMap>  struct attributes_writer {    attributes_writer(AttributeMap attr)      : attributes(attr) { }    template <class VorE>    void operator()(std::ostream& out, const VorE& e) const {      this->write_attribute(out, attributes[e]);    }    private:      template<typename AttributeSequence>      void write_attribute(std::ostream& out,                           const AttributeSequence& seq) const      {        if (!seq.empty()) {          out << "[";          write_attributes(seq, out);          out << "]";        }      }      void write_attribute(std::ostream&,                           detail::error_property_not_found) const      {      }    AttributeMap attributes;  };  template <typename Graph>  attributes_writer    <typename property_map<Graph, edge_attribute_t>::const_type>  make_edge_attributes_writer(const Graph& g)  {    typedef typename property_map<Graph, edge_attribute_t>::const_type      EdgeAttributeMap;    return attributes_writer<EdgeAttributeMap>(get(edge_attribute, g));  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?