⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphviz.hpp

📁 support vector clustering for vc++
💻 HPP
📖 第 1 页 / 共 2 页
字号:
          eawriter(out, *ei);
#else
          make_edge_attributes_writer(g)(out, *ei); //print edge properties
#endif
          out << ";" << std::endl;
        }
      }
      out << "}" << std::endl;
    }
  } // namespace detail

  // requires graph_name graph property
  template <typename Graph>
  void write_graphviz(std::ostream& out, const subgraph<Graph>& g) {
    std::vector<bool> edge_marker(num_edges(g), true);
    std::vector<bool> vertex_marker(num_vertices(g), true);

    detail::write_graphviz_subgraph(out, g,
                                    vertex_marker.begin(),
                                    edge_marker.begin(), 
                                    get(vertex_index, g));
  }

  template <typename Graph>
  void write_graphviz(const std::string& filename, const subgraph<Graph>& g) {
    std::ofstream out(filename.c_str());
    std::vector<bool> edge_marker(num_edges(g), true);
    std::vector<bool> vertex_marker(num_vertices(g), true);

    detail::write_graphviz_subgraph(out, g,
                                    vertex_marker.begin(),
                                    edge_marker.begin(),
                                    get(vertex_index, g));
  }

  template <typename Graph, typename VertexID>
  void write_graphviz(std::ostream& out, const subgraph<Graph>& g,
                      VertexID vertex_id) 
  {
    std::vector<bool> edge_marker(num_edges(g), true);
    std::vector<bool> vertex_marker(num_vertices(g), true);

    detail::write_graphviz_subgraph(out, g,
                                    vertex_marker.begin(),
                                    edge_marker.begin(), 
                                    vertex_id);
  }

  template <typename Graph, typename VertexID>
  void write_graphviz(const std::string& filename, const subgraph<Graph>& g,
                      VertexID vertex_id) 
  {
    std::ofstream out(filename.c_str());
    std::vector<bool> edge_marker(num_edges(g), true);
    std::vector<bool> vertex_marker(num_vertices(g), true);

    detail::write_graphviz_subgraph(out, g,
                                    vertex_marker.begin(),
                                    edge_marker.begin(),
                                    vertex_id);
  }

  typedef std::map<std::string, std::string> GraphvizAttrList;

  typedef property<vertex_attribute_t, GraphvizAttrList>
          GraphvizVertexProperty;

  typedef property<edge_attribute_t, GraphvizAttrList,
                   property<edge_index_t, int> >
          GraphvizEdgeProperty;

  typedef property<graph_graph_attribute_t, GraphvizAttrList,
                   property<graph_vertex_attribute_t, GraphvizAttrList,
                   property<graph_edge_attribute_t, GraphvizAttrList,
                   property<graph_name_t, std::string> > > >
          GraphvizGraphProperty;

  typedef subgraph<adjacency_list<vecS,
                   vecS, directedS,
                   GraphvizVertexProperty,
                   GraphvizEdgeProperty,
                   GraphvizGraphProperty> >
          GraphvizDigraph;

  typedef subgraph<adjacency_list<vecS,
                   vecS, undirectedS,
                   GraphvizVertexProperty,
                   GraphvizEdgeProperty,
                   GraphvizGraphProperty> >
          GraphvizGraph;


  // These four require linking the BGL-Graphviz library: libbgl-viz.a
  // from the /src directory.
  extern void read_graphviz(const std::string& file, GraphvizDigraph& g);
  extern void read_graphviz(FILE* file, GraphvizDigraph& g);

  extern void read_graphviz(const std::string& file, GraphvizGraph& g);
  extern void read_graphviz(FILE* file, GraphvizGraph& g);

  class dynamic_properties_writer
  {
  public:
    dynamic_properties_writer(const dynamic_properties& dp) : dp(&dp) { }

    template<typename Descriptor>
    void operator()(std::ostream& out, Descriptor key) const
    {
      bool first = true;
      for (dynamic_properties::const_iterator i = dp->begin(); 
           i != dp->end(); ++i) {
        if (typeid(key) == i->second->key()) {
          if (first) out << " [";
          else out << ", ";
          first = false;

          out << i->first << "=\"" << i->second->get_string(key) << "\"";
        }
      }

      if (!first) out << "]";
    }

  private:
    const dynamic_properties* dp;
  };

  class dynamic_vertex_properties_writer
  {
  public:
    dynamic_vertex_properties_writer(const dynamic_properties& dp,
                                     const std::string& node_id) 
      : dp(&dp), node_id(&node_id) { }

    template<typename Descriptor>
    void operator()(std::ostream& out, Descriptor key) const
    {
      bool first = true;
      for (dynamic_properties::const_iterator i = dp->begin(); 
           i != dp->end(); ++i) {
        if (typeid(key) == i->second->key()
            && i->first != *node_id) {
          if (first) out << " [";
          else out << ", ";
          first = false;

          out << i->first << "=\"" << i->second->get_string(key) << "\"";
        }
      }

      if (!first) out << "]";
    }

  private:
    const dynamic_properties* dp;
    const std::string* node_id;
  };

  namespace graph { namespace detail {

    template<typename Vertex>
    struct node_id_property_map
    {
      typedef std::string value_type;
      typedef value_type reference;
      typedef Vertex key_type;
      typedef readable_property_map_tag category;

      node_id_property_map() {}

      node_id_property_map(const dynamic_properties& dp,
                           const std::string& node_id)
        : dp(&dp), node_id(&node_id) { }

      const dynamic_properties* dp;
      const std::string* node_id;
    };

    template<typename Vertex>
    inline std::string 
    get(node_id_property_map<Vertex> pm, 
        typename node_id_property_map<Vertex>::key_type v)
    { return get(*pm.node_id, *pm.dp, v); }

  } } // end namespace graph::detail

  template<typename Graph>
  inline void
  write_graphviz(std::ostream& out, const Graph& g,
                 const dynamic_properties& dp, 
                 const std::string& node_id = "node_id")
  {
    typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
    write_graphviz(out, g, dp, node_id,
                   graph::detail::node_id_property_map<Vertex>(dp, node_id));
  }

  template<typename Graph, typename VertexID>
  void
  write_graphviz(std::ostream& out, const Graph& g,
                 const dynamic_properties& dp, const std::string& node_id,
                 VertexID id)
  {
    write_graphviz
      (out, g,
       /*vertex_writer=*/dynamic_vertex_properties_writer(dp, node_id),
       /*edge_writer=*/dynamic_properties_writer(dp),
       /*graph_writer=*/default_writer(),
       id);
  }

/////////////////////////////////////////////////////////////////////////////
// Graph reader exceptions
/////////////////////////////////////////////////////////////////////////////
struct graph_exception : public std::exception {
  virtual ~graph_exception() throw() {}
  virtual const char* what() const throw() = 0;
};

struct bad_parallel_edge : public graph_exception {
  std::string from;
  std::string to;
  mutable std::string statement;
  bad_parallel_edge(const std::string& i, const std::string& j) :
    from(i), to(j) {}

  virtual ~bad_parallel_edge() throw() {}
  const char* what() const throw() {
    if(statement.empty())
      statement =
        std::string("Failed to add parallel edge: (")
        + from +  "," + to + ")\n";

    return statement.c_str();
  }
};

struct directed_graph_error : public graph_exception {
  virtual ~directed_graph_error() throw() {}
  virtual const char* what() const throw() {
    return
      "read_graphviz: "
      "Tried to read a directed graph into an undirected graph.";
  }
};

struct undirected_graph_error : public graph_exception {
  virtual ~undirected_graph_error() throw() {}
  virtual const char* what() const throw() {
    return
      "read_graphviz: "
      "Tried to read an undirected graph into a directed graph.";
  }
};

namespace detail { namespace graph {

typedef std::string id_t;
typedef id_t node_t;

// edges are not uniquely determined by adjacent nodes
class edge_t {
  int idx_;
  explicit edge_t(int i) : idx_(i) {}
public:
  static edge_t new_edge() {
    static int idx = 0;
    return edge_t(idx++);
  };
  
  bool operator==(const edge_t& rhs) const {
    return idx_ == rhs.idx_;
  }
  bool operator<(const edge_t& rhs) const {
    return idx_ < rhs.idx_;
  }
};

class mutate_graph
{
 public:
  virtual ~mutate_graph() {}
  virtual bool is_directed() const = 0;
  virtual void do_add_vertex(const node_t& node) = 0;

  virtual void 
  do_add_edge(const edge_t& edge, const node_t& source, const node_t& target)
    = 0;

  virtual void 
  set_node_property(const id_t& key, const node_t& node, const id_t& value) = 0;

  virtual void 
  set_edge_property(const id_t& key, const edge_t& edge, const id_t& value) = 0;
};

template<typename MutableGraph>
class mutate_graph_impl : public mutate_graph
{
  typedef typename graph_traits<MutableGraph>::vertex_descriptor bgl_vertex_t;
  typedef typename graph_traits<MutableGraph>::edge_descriptor   bgl_edge_t;

 public:
  mutate_graph_impl(MutableGraph& graph, dynamic_properties& dp,
                    std::string node_id_prop)
    : graph_(graph), dp_(dp), node_id_prop_(node_id_prop) { }

  ~mutate_graph_impl() {}

  bool is_directed() const
  {
    return 
      boost::is_convertible<
        typename boost::graph_traits<MutableGraph>::directed_category,
        boost::directed_tag>::value;
  }

  virtual void do_add_vertex(const node_t& node)
  {
    // Add the node to the graph.
    bgl_vertex_t v = add_vertex(graph_);

    // Set up a mapping from name to BGL vertex.
    bgl_nodes.insert(std::make_pair(node, v));
    
    // node_id_prop_ allows the caller to see the real id names for nodes.
    put(node_id_prop_, dp_, v, node);
  }

  void 
  do_add_edge(const edge_t& edge, const node_t& source, const node_t& target)
  {
    std::pair<bgl_edge_t, bool> result =
     add_edge(bgl_nodes[source], bgl_nodes[target], graph_);
    
    if(!result.second) {
      // In the case of no parallel edges allowed
      throw bad_parallel_edge(source, target);
    } else {
      bgl_edges.insert(std::make_pair(edge, result.first));
    }
  }

  void
  set_node_property(const id_t& key, const node_t& node, const id_t& value)
  {
    put(key, dp_, bgl_nodes[node], value);
  }

  void
  set_edge_property(const id_t& key, const edge_t& edge, const id_t& value)
  {
    put(key, dp_, bgl_edges[edge], value);
  }

 protected:
  MutableGraph& graph_;
  dynamic_properties& dp_;
  std::string node_id_prop_;
  std::map<node_t, bgl_vertex_t> bgl_nodes;
  std::map<edge_t, bgl_edge_t> bgl_edges;
};

BOOST_GRAPH_DECL
bool read_graphviz(std::istream& in, mutate_graph& graph);

} } // end namespace detail::graph

// Parse the passed stream as a GraphViz dot file.
template <typename MutableGraph>
bool read_graphviz(std::istream& in, MutableGraph& graph,
                   dynamic_properties& dp,
                   std::string const& node_id = "node_id") 
{
  detail::graph::mutate_graph_impl<MutableGraph> m_graph(graph, dp, node_id);
  return detail::graph::read_graphviz(in, m_graph);
}

} // namespace boost

#ifdef BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS
#  include <boost/graph/detail/read_graphviz_spirit.hpp>
#endif // BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS

#endif // BOOST_GRAPHVIZ_HPP

⌨️ 快捷键说明

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