stanford_graph.hpp
来自「CGAL is a collaborative effort of severa」· HPP 代码 · 共 582 行 · 第 1/2 页
HPP
582 行
//=======================================================================// Copyright 1997-2001 University of Notre Dame.// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek//// 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_GRAPH_SGB_GRAPH_HPP#define BOOST_GRAPH_SGB_GRAPH_HPP#include <boost/config.hpp>#include <boost/iterator.hpp>#include <boost/operators.hpp>#include <boost/property_map.hpp>#include <boost/graph/graph_traits.hpp>#include <boost/graph/properties.hpp>// Thanks to Andreas Scherer for numerous suggestions and fixes!// This file adapts a Stanford GraphBase (SGB) Graph pointer into a// VertexListGraph. Note that a graph adaptor class is not needed, // SGB's Graph* is used as is. The VertexListGraph concept is fulfilled by// defining the appropriate non-member functions for Graph*.//// The PROTOTYPES change file extensions to SGB must be applied so// that the SGB functions have real prototypes which are necessary for// the C++ compiler. To apply the PROTOTYPES extensions, before you do// "make tests install" for SGB do "ln -s PROTOTYPES/* ." to the SGB// root directory (or just copy all the files from the PROTOTYPES// directory to the SGB root directory).//extern "C" { // We include all global definitions for the general stuff // of The Stanford GraphBase and its various graph generator // functions by reading all SGB headerfiles as in section 2 of // the "test_sample" program.#include <gb_graph.h> /* SGB data structures */#include <gb_io.h> /* SGB input/output routines */#include <gb_flip.h> /* random number generator */#include <gb_dijk.h> /* routines for shortest paths */#include <gb_basic.h> /* the basic graph operations */#undef empty /* avoid name clash with C++ standard library */ inline Graph* empty( long n ) /* and provide workaround */ { return board(n,0L,0L,0L,2L,0L,0L); }#include <gb_books.h> /* graphs based on literature */#include <gb_econ.h> /* graphs based on economic data */#include <gb_games.h> /* graphs based on football scores */#include <gb_gates.h> /* graphs based on logic circuits */#undef val /* avoid name clash with g++ headerfile stl_tempbuf.h */ // val ==> Vertex::x.I#include <gb_lisa.h> /* graphs based on Mona Lisa */#include <gb_miles.h> /* graphs based on mileage data */#include <gb_plane.h> /* planar graphs */#include <gb_raman.h> /* Ramanujan graphs */#include <gb_rand.h> /* random graphs */#include <gb_roget.h> /* graphs based on Roget's Thesaurus */#include <gb_save.h> /* we save results in ASCII format */#include <gb_words.h> /* five-letter-word graphs */#undef weight /* avoid name clash with BGL parameter */ // weight ==> Vertex::u.I}namespace boost { class sgb_edge;}class sgb_out_edge_iterator;class sgb_adj_iterator;class sgb_vertex_iterator;namespace boost { typedef Graph* sgb_graph_ptr; typedef const Graph* sgb_const_graph_ptr; struct sgb_traversal_tag : public virtual vertex_list_graph_tag, public virtual incidence_graph_tag, public virtual adjacency_graph_tag { }; template <> struct graph_traits<sgb_graph_ptr> { typedef Vertex* vertex_descriptor; typedef boost::sgb_edge edge_descriptor; typedef sgb_out_edge_iterator out_edge_iterator; typedef void in_edge_iterator; typedef sgb_adj_iterator adjacency_iterator; typedef sgb_vertex_iterator vertex_iterator; typedef void edge_iterator; typedef long vertices_size_type; typedef long edge_size_type; typedef long degree_size_type; typedef directed_tag directed_category; typedef sgb_traversal_tag traversal_category; typedef allow_parallel_edge_tag edge_parallel_category; }; template <> struct graph_traits<sgb_const_graph_ptr> { typedef Vertex* vertex_descriptor; typedef boost::sgb_edge edge_descriptor; typedef sgb_out_edge_iterator out_edge_iterator; typedef void in_edge_iterator; typedef sgb_adj_iterator adjacency_iterator; typedef sgb_vertex_iterator vertex_iterator; typedef void edge_iterator; typedef long vertices_size_type; typedef long edge_size_type; typedef long degree_size_type; typedef directed_tag directed_category; typedef sgb_traversal_tag traversal_category; typedef allow_parallel_edge_tag edge_parallel_category; };}namespace boost { struct edge_length_t { typedef edge_property_tag kind; }; // We could just use Arc* as the edge descriptor type, but // we want to add the source(e,g) function which requires // that we carry along a pointer to the source vertex. class sgb_edge { typedef sgb_edge self; public: sgb_edge() : _arc(0), _src(0) { } sgb_edge(Arc* a, Vertex* s) : _arc(a), _src(s) { } friend Vertex* source(self e, sgb_const_graph_ptr) { return e._src; } friend Vertex* target(self e, sgb_const_graph_ptr) { return e._arc->tip; } friend bool operator==(const self& a, const self& b) { return a._arc == b._arc; } friend bool operator!=(const self& a, const self& b) { return a._arc != b._arc; }#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) template <class Ref> friend class sgb_edge_length_map; template <class Tag, class Ref> friend class sgb_edge_util_map; friend long get(edge_length_t, const sgb_graph_ptr&, const sgb_edge& key); friend long get(edge_length_t, const sgb_const_graph_ptr&, const sgb_edge& key); friend void put(edge_length_t, sgb_graph_ptr&, const sgb_edge& key, long value); protected:#endif Arc* _arc; Vertex* _src; };} // namespace boost class sgb_out_edge_iterator : public boost::forward_iterator_helper< sgb_out_edge_iterator, boost::sgb_edge, std::ptrdiff_t, boost::sgb_edge*, boost::sgb_edge> { typedef sgb_out_edge_iterator self; public: sgb_out_edge_iterator() : _src(0), _arc(0) {} sgb_out_edge_iterator(Vertex* s, Arc* d) : _src(s), _arc(d) {} boost::sgb_edge operator*() { return boost::sgb_edge(_arc, _src); } self& operator++() { _arc = _arc->next; return *this; } friend bool operator==(const self& x, const self& y) { return x._arc == y._arc; } protected: Vertex* _src; Arc* _arc; }; class sgb_adj_iterator : public boost::forward_iterator_helper< sgb_adj_iterator, Vertex*, std::ptrdiff_t, Vertex**,Vertex*> { typedef sgb_adj_iterator self; public: sgb_adj_iterator() : _arc(0) {} sgb_adj_iterator(Arc* d) : _arc(d) {} Vertex* operator*() { return _arc->tip; } self& operator++() { _arc = _arc->next; return *this; } friend bool operator==(const self& x, const self& y) { return x._arc == y._arc; } protected: Arc* _arc; }; // The reason we have this instead of just using Vertex* is that we // want to use Vertex* as the vertex_descriptor instead of just // Vertex, which avoids problems with boost passing vertex descriptors // by value and how that interacts with the sgb_vertex_id_map. class sgb_vertex_iterator : public boost::forward_iterator_helper< sgb_vertex_iterator, Vertex*, std::ptrdiff_t, Vertex**, Vertex*> { typedef sgb_vertex_iterator self; public: sgb_vertex_iterator() : _v(0) { } sgb_vertex_iterator(Vertex* v) : _v(v) { } Vertex* operator*() { return _v; } self& operator++() { ++_v; return *this; } friend bool operator==(const self& x, const self& y) { return x._v == y._v; } protected: Vertex* _v; };namespace boost { inline std::pair<sgb_vertex_iterator,sgb_vertex_iterator> vertices(sgb_const_graph_ptr g) { return std::make_pair(sgb_vertex_iterator(g->vertices), sgb_vertex_iterator(g->vertices + g->n)); } inline std::pair<sgb_out_edge_iterator,sgb_out_edge_iterator> out_edges(Vertex* u, sgb_const_graph_ptr) { return std::make_pair( sgb_out_edge_iterator(u, u->arcs), sgb_out_edge_iterator(u, 0) ); } inline boost::graph_traits<sgb_graph_ptr>::degree_size_type out_degree(Vertex* u, sgb_const_graph_ptr g) { boost::graph_traits<sgb_graph_ptr>::out_edge_iterator i, i_end; boost::tie(i, i_end) = out_edges(u, g); return std::distance(i, i_end); } // in_edges? inline std::pair<sgb_adj_iterator,sgb_adj_iterator> adjacent_vertices(Vertex* u, sgb_const_graph_ptr) { return std::make_pair( sgb_adj_iterator(u->arcs), sgb_adj_iterator(0) ); } inline long num_vertices(sgb_const_graph_ptr g) { return g->n; } inline long num_edges(sgb_const_graph_ptr g) { return g->m; } inline Vertex* vertex(long v, sgb_const_graph_ptr g) { return g->vertices + v; } // Various Property Maps // Vertex ID class sgb_vertex_id_map : public boost::put_get_helper<long, sgb_vertex_id_map> { public: typedef boost::readable_property_map_tag category; typedef long value_type; typedef long reference; typedef Vertex* key_type; sgb_vertex_id_map() : _g(0) { } sgb_vertex_id_map(sgb_graph_ptr g) : _g(g) { } long operator[](Vertex* v) const { return v - _g->vertices; } protected: sgb_graph_ptr _g; }; inline sgb_vertex_id_map get(vertex_index_t, sgb_graph_ptr g) { return sgb_vertex_id_map(g); } // Vertex Name class sgb_vertex_name_map : public boost::put_get_helper<char*, sgb_vertex_name_map> { public: typedef boost::readable_property_map_tag category; typedef char* value_type; typedef char* reference; typedef Vertex* key_type; char* operator[](Vertex* v) const { return v->name; } }; inline sgb_vertex_name_map get(vertex_name_t, sgb_graph_ptr) { return sgb_vertex_name_map(); } // Vertex Property Tags
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?