stanford_graph.hpp 17.6 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
//=======================================================================
// Copyright 1997-2001 University of Notre Dame.
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#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/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
#define SGB_PROPERTY_TAG(KIND,TAG) \
  template <class T> struct TAG##_property { \
    typedef KIND##_property_tag kind; \
    typedef T type; \
  };
  SGB_PROPERTY_TAG(vertex, u)
  SGB_PROPERTY_TAG(vertex, v)
  SGB_PROPERTY_TAG(vertex, w)
  SGB_PROPERTY_TAG(vertex, x)
  SGB_PROPERTY_TAG(vertex, y)
  SGB_PROPERTY_TAG(vertex, z)
 
  // Edge Property Tags
  SGB_PROPERTY_TAG(edge, a)
  SGB_PROPERTY_TAG(edge, b)

  // Various Utility Maps

  // helpers
  inline Vertex*& get_util(util& u, Vertex*) { return u.V; }
  inline Arc*& get_util(util& u, Arc*) { return u.A; }
  inline sgb_graph_ptr& get_util(util& u, sgb_graph_ptr) { return u.G; }
  inline char*& get_util(util& u, char*) { return u.S; }
  inline long& get_util(util& u, long) { return u.I; }

#define SGB_GET_UTIL_FIELD(KIND,X) \
  template <class T> \
  inline T& get_util_field(KIND* k, X##_property<T>) { \
    return get_util(k->X, T());  }

  SGB_GET_UTIL_FIELD(Vertex, u)
  SGB_GET_UTIL_FIELD(Vertex, v)
  SGB_GET_UTIL_FIELD(Vertex, w)
  SGB_GET_UTIL_FIELD(Vertex, x)
  SGB_GET_UTIL_FIELD(Vertex, y)
  SGB_GET_UTIL_FIELD(Vertex, z)

  SGB_GET_UTIL_FIELD(Arc, a)
  SGB_GET_UTIL_FIELD(Arc, b)

  // Vertex Utility Map
  template <class Tag, class Ref>
  class sgb_vertex_util_map
    : public boost::put_get_helper<Ref, sgb_vertex_util_map<Tag, Ref> >
  {
    Tag tag;
  public:
    explicit sgb_vertex_util_map(Tag tag = Tag()): tag(tag) {}
    typedef boost::lvalue_property_map_tag category;
    typedef typename Tag::type value_type;
    typedef Vertex* key_type;
    typedef Ref reference;
    reference operator[](Vertex* v) const {
      return get_util_field(v, tag); 
    }
  };

  // Edge Utility Map
  template <class Tag, class Ref>
  class sgb_edge_util_map
    : public boost::put_get_helper<Ref, sgb_edge_util_map<Tag, Ref> >
  {
    Tag tag;
  public:
    explicit sgb_edge_util_map(Tag tag = Tag()): tag(tag) {}
    typedef boost::lvalue_property_map_tag category;
    typedef typename Tag::type value_type;
    typedef Vertex* key_type;
    typedef Ref reference;
    reference operator[](const sgb_edge& e) const {
      return get_util_field(e._arc, tag); 
    }
  };


  template <class Tag>
  inline sgb_vertex_util_map<Tag, const typename Tag::type&>
  get_property_map(Tag, const sgb_graph_ptr& g, vertex_property_tag) {
    return sgb_vertex_util_map<Tag, const typename Tag::type&>();
  }
  template <class Tag>
  inline sgb_vertex_util_map<Tag, typename Tag::type&>
  get_property_map(Tag, sgb_graph_ptr& g, vertex_property_tag) {
    return sgb_vertex_util_map<Tag, typename Tag::type&>();
  }

  template <class Tag>
  inline sgb_edge_util_map<Tag, const typename Tag::type&> 
  get_property_map(Tag, const sgb_graph_ptr& g, edge_property_tag) {
    return sgb_edge_util_map<Tag, const typename Tag::type&>();
  }
  template <class Tag>
  inline sgb_edge_util_map<Tag, typename Tag::type&> 
  get_property_map(Tag, sgb_graph_ptr& g, edge_property_tag) {
    return sgb_edge_util_map<Tag, typename Tag::type&>();
  }


  // Edge Length Access
  template <class Ref>
  class sgb_edge_length_map
    : public boost::put_get_helper<Ref, sgb_edge_length_map<Ref> >
  {
  public:
    typedef boost::lvalue_property_map_tag category;
    typedef long value_type;
    typedef sgb_edge key_type;
    typedef Ref reference;
    reference operator[](const sgb_edge& e) const { 
      return e._arc->len; 
    }
  };

  inline sgb_edge_length_map<const long&>
  get(edge_length_t, const sgb_graph_ptr&) { 
    return sgb_edge_length_map<const long&>(); 
  }
  inline sgb_edge_length_map<const long&>
  get(edge_length_t, const sgb_const_graph_ptr&) { 
    return sgb_edge_length_map<const long&>(); 
  }
  inline sgb_edge_length_map<long&>
  get(edge_length_t, sgb_graph_ptr&) { 
    return sgb_edge_length_map<long&>(); 
  }
  inline long
  get(edge_length_t, const sgb_graph_ptr&, const sgb_edge& key) {
    return key._arc->len;
  }
  inline long
  get(edge_length_t, const sgb_const_graph_ptr&, const sgb_edge& key) {
    return key._arc->len;
  }
  inline void
  put(edge_length_t, sgb_graph_ptr&, const sgb_edge& key, long value)
  {
    key._arc->len = value;
  }

  // Property Map Traits Classes
  template <>
  struct property_map<sgb_graph_ptr, edge_length_t> {
    typedef sgb_edge_length_map<long&> type;
    typedef sgb_edge_length_map<const long&> const_type;
  };
  template <>
  struct property_map<sgb_graph_ptr, vertex_index_t> {
    typedef sgb_vertex_id_map type;
    typedef sgb_vertex_id_map const_type;
  };
  template <>
  struct property_map<sgb_graph_ptr, vertex_name_t> {
    typedef sgb_vertex_name_map type;
    typedef sgb_vertex_name_map const_type;
  };

  template <>
  struct property_map<sgb_const_graph_ptr, edge_length_t> {
    typedef sgb_edge_length_map<const long&> const_type;
  };
  template <>
  struct property_map<sgb_const_graph_ptr, vertex_index_t> {
    typedef sgb_vertex_id_map const_type;
  };
  template <>
  struct property_map<sgb_const_graph_ptr, vertex_name_t> {
    typedef sgb_vertex_name_map const_type;
  };


  namespace detail {
    template <class Kind, class PropertyTag>
    struct sgb_choose_property_map { };
    template <class PropertyTag>
    struct sgb_choose_property_map<vertex_property_tag, PropertyTag> {
      typedef typename PropertyTag::type value_type;
      typedef sgb_vertex_util_map<PropertyTag, value_type&> type;
      typedef sgb_vertex_util_map<PropertyTag, const value_type&> const_type;
    };
    template <class PropertyTag>
    struct sgb_choose_property_map<edge_property_tag, PropertyTag> {
      typedef typename PropertyTag::type value_type;
      typedef sgb_edge_util_map<PropertyTag, value_type&> type;
      typedef sgb_edge_util_map<PropertyTag, const value_type&> const_type;
    };
  } // namespace detail
  template <class PropertyTag>
  struct property_map<sgb_graph_ptr, PropertyTag> {
    typedef typename property_kind<PropertyTag>::type Kind;
    typedef detail::sgb_choose_property_map<Kind, PropertyTag> Choice;
    typedef typename Choice::type type;
    typedef typename Choice::const_type const_type;
  };
  template <class PropertyTag>
  struct property_map<sgb_const_graph_ptr, PropertyTag> {
    typedef typename property_kind<PropertyTag>::type Kind;
    typedef detail::sgb_choose_property_map<Kind, PropertyTag> Choice;
    typedef typename Choice::const_type const_type;
  };

#define SGB_UTIL_ACCESSOR(KIND,X) \
  template <class T> \
  inline sgb_##KIND##_util_map< X##_property<T>, T&> \
  get(X##_property<T>, sgb_graph_ptr&) { \
    return sgb_##KIND##_util_map< X##_property<T>, T&>(); \
  } \
  template <class T> \
  inline sgb_##KIND##_util_map< X##_property<T>, const T&> \
  get(X##_property<T>, const sgb_graph_ptr&) { \
    return sgb_##KIND##_util_map< X##_property<T>, const T&>(); \
  } \
  template <class T> \
  inline sgb_##KIND##_util_map< X##_property<T>, const T&> \
  get(X##_property<T>, const sgb_const_graph_ptr&) { \
    return sgb_##KIND##_util_map< X##_property<T>, const T&>(); \
  } \
  template <class T, class Key> \
  inline typename \
  sgb_##KIND##_util_map< X##_property<T>, const T&>::value_type \
  get(X##_property<T>, const sgb_graph_ptr&, const Key& key) { \
    return sgb_##KIND##_util_map< X##_property<T>, const T&>()[key]; \
  } \
  template <class T, class Key> \
  inline typename \
  sgb_##KIND##_util_map< X##_property<T>, const T&>::value_type \
  get(X##_property<T>, const sgb_const_graph_ptr&, const Key& key) { \
    return sgb_##KIND##_util_map< X##_property<T>, const T&>()[key]; \
  } \
  template <class T, class Key, class Value> \
  inline  void \
  put(X##_property<T>, sgb_graph_ptr&, const Key& key, const Value& value) { \
    sgb_##KIND##_util_map< X##_property<T>, T&>()[key] = value; \
  }


  SGB_UTIL_ACCESSOR(vertex, u)
  SGB_UTIL_ACCESSOR(vertex, v)
  SGB_UTIL_ACCESSOR(vertex, w)
  SGB_UTIL_ACCESSOR(vertex, x)
  SGB_UTIL_ACCESSOR(vertex, y)
  SGB_UTIL_ACCESSOR(vertex, z)

  SGB_UTIL_ACCESSOR(edge, a)
  SGB_UTIL_ACCESSOR(edge, b)

} // namespace boost

#endif // BOOST_GRAPH_SGB_GRAPH_HPP