os/ossrv/ossrv_pub/boost_apis/boost/graph/properties.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
//=======================================================================
sl@0
     2
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
sl@0
     3
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
sl@0
     4
//
sl@0
     5
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     6
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     7
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     8
//=======================================================================
sl@0
     9
#ifndef BOOST_GRAPH_PROPERTIES_HPP
sl@0
    10
#define BOOST_GRAPH_PROPERTIES_HPP
sl@0
    11
sl@0
    12
#include <boost/config.hpp>
sl@0
    13
#include <cassert>
sl@0
    14
#include <boost/pending/property.hpp>
sl@0
    15
#include <boost/property_map.hpp>
sl@0
    16
#include <boost/graph/graph_traits.hpp>
sl@0
    17
#include <boost/type_traits/is_convertible.hpp>
sl@0
    18
sl@0
    19
namespace boost {
sl@0
    20
sl@0
    21
  enum default_color_type { white_color, gray_color, green_color, red_color, black_color };
sl@0
    22
sl@0
    23
  template <class ColorValue>
sl@0
    24
  struct color_traits {
sl@0
    25
    static default_color_type white() { return white_color; }
sl@0
    26
    static default_color_type gray() { return gray_color; }
sl@0
    27
    static default_color_type green() { return green_color; }
sl@0
    28
    static default_color_type red() { return red_color; }
sl@0
    29
    static default_color_type black() { return black_color; }
sl@0
    30
  };
sl@0
    31
  
sl@0
    32
  // These functions are now obsolete, replaced by color_traits.
sl@0
    33
  inline default_color_type white(default_color_type) { return white_color; }
sl@0
    34
  inline default_color_type gray(default_color_type) { return gray_color; }
sl@0
    35
  inline default_color_type green(default_color_type) { return green_color; }
sl@0
    36
  inline default_color_type red(default_color_type) { return red_color; } 
sl@0
    37
  inline default_color_type black(default_color_type) { return black_color; }
sl@0
    38
sl@0
    39
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
sl@0
    40
  template <>
sl@0
    41
  struct property_traits<default_color_type*> {
sl@0
    42
    typedef default_color_type value_type;
sl@0
    43
    typedef std::ptrdiff_t key_type;
sl@0
    44
    typedef default_color_type& reference;
sl@0
    45
    typedef lvalue_property_map_tag category;
sl@0
    46
  };
sl@0
    47
  // get/put already defined for T*
sl@0
    48
#endif
sl@0
    49
sl@0
    50
  struct graph_property_tag { };
sl@0
    51
  struct vertex_property_tag { };
sl@0
    52
  struct edge_property_tag { };
sl@0
    53
sl@0
    54
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
sl@0
    55
  // See examples/edge_property.cpp for how to use this.
sl@0
    56
#define BOOST_INSTALL_PROPERTY(KIND, NAME) \
sl@0
    57
  template <> struct property_kind<KIND##_##NAME##_t> { \
sl@0
    58
    typedef KIND##_property_tag type; \
sl@0
    59
  }
sl@0
    60
#else
sl@0
    61
#define BOOST_INSTALL_PROPERTY(KIND, NAME) \
sl@0
    62
  template <> struct property_kind<KIND##_##NAME##_t> { \
sl@0
    63
    typedef KIND##_property_tag type; \
sl@0
    64
  }
sl@0
    65
#endif
sl@0
    66
sl@0
    67
#define BOOST_DEF_PROPERTY(KIND, NAME) \
sl@0
    68
  enum KIND##_##NAME##_t { KIND##_##NAME }; \
sl@0
    69
  BOOST_INSTALL_PROPERTY(KIND, NAME)
sl@0
    70
sl@0
    71
  BOOST_DEF_PROPERTY(vertex, all);
sl@0
    72
  BOOST_DEF_PROPERTY(edge, all);
sl@0
    73
  BOOST_DEF_PROPERTY(graph, all);
sl@0
    74
  BOOST_DEF_PROPERTY(vertex, index);
sl@0
    75
  BOOST_DEF_PROPERTY(vertex, index1);
sl@0
    76
  BOOST_DEF_PROPERTY(vertex, index2);
sl@0
    77
  BOOST_DEF_PROPERTY(vertex, root);
sl@0
    78
  BOOST_DEF_PROPERTY(edge, index);
sl@0
    79
  BOOST_DEF_PROPERTY(edge, name);
sl@0
    80
  BOOST_DEF_PROPERTY(edge, weight);
sl@0
    81
  BOOST_DEF_PROPERTY(edge, weight2);
sl@0
    82
  BOOST_DEF_PROPERTY(edge, color);
sl@0
    83
  BOOST_DEF_PROPERTY(vertex, name);
sl@0
    84
  BOOST_DEF_PROPERTY(graph, name);
sl@0
    85
  BOOST_DEF_PROPERTY(vertex, distance);
sl@0
    86
  BOOST_DEF_PROPERTY(vertex, color);
sl@0
    87
  BOOST_DEF_PROPERTY(vertex, degree);
sl@0
    88
  BOOST_DEF_PROPERTY(vertex, in_degree);
sl@0
    89
  BOOST_DEF_PROPERTY(vertex, out_degree);
sl@0
    90
  BOOST_DEF_PROPERTY(vertex, current_degree);
sl@0
    91
  BOOST_DEF_PROPERTY(vertex, priority); 
sl@0
    92
  BOOST_DEF_PROPERTY(vertex, discover_time);
sl@0
    93
  BOOST_DEF_PROPERTY(vertex, finish_time);
sl@0
    94
  BOOST_DEF_PROPERTY(vertex, predecessor);
sl@0
    95
  BOOST_DEF_PROPERTY(vertex, rank);
sl@0
    96
  BOOST_DEF_PROPERTY(vertex, centrality);
sl@0
    97
  BOOST_DEF_PROPERTY(vertex, lowpoint);
sl@0
    98
  BOOST_DEF_PROPERTY(edge, reverse);
sl@0
    99
  BOOST_DEF_PROPERTY(edge, capacity);
sl@0
   100
  BOOST_DEF_PROPERTY(edge, residual_capacity);
sl@0
   101
  BOOST_DEF_PROPERTY(edge, centrality);
sl@0
   102
  BOOST_DEF_PROPERTY(graph, visitor);
sl@0
   103
sl@0
   104
  // These tags are used for property bundles
sl@0
   105
  BOOST_DEF_PROPERTY(vertex, bundle);
sl@0
   106
  BOOST_DEF_PROPERTY(edge, bundle);
sl@0
   107
sl@0
   108
#undef BOOST_DEF_PROPERTY
sl@0
   109
sl@0
   110
  namespace detail {
sl@0
   111
sl@0
   112
    struct dummy_edge_property_selector {
sl@0
   113
      template <class Graph, class Property, class Tag>
sl@0
   114
      struct bind_ {
sl@0
   115
        typedef identity_property_map type;
sl@0
   116
        typedef identity_property_map const_type;
sl@0
   117
      };
sl@0
   118
    };
sl@0
   119
    struct dummy_vertex_property_selector {
sl@0
   120
      template <class Graph, class Property, class Tag>
sl@0
   121
      struct bind_ {
sl@0
   122
        typedef identity_property_map type;
sl@0
   123
        typedef identity_property_map const_type;
sl@0
   124
      };
sl@0
   125
    };
sl@0
   126
sl@0
   127
  } // namespace detail
sl@0
   128
sl@0
   129
  // Graph classes can either partially specialize property_map
sl@0
   130
  // or they can specialize these two selector classes.
sl@0
   131
  template <class GraphTag>
sl@0
   132
  struct edge_property_selector {
sl@0
   133
    typedef detail::dummy_edge_property_selector type;
sl@0
   134
  };
sl@0
   135
sl@0
   136
  template <class GraphTag>
sl@0
   137
  struct vertex_property_selector {
sl@0
   138
    typedef detail::dummy_vertex_property_selector type;
sl@0
   139
  };
sl@0
   140
sl@0
   141
  namespace detail {
sl@0
   142
sl@0
   143
    template <class Graph, class PropertyTag>
sl@0
   144
    struct edge_property_map {
sl@0
   145
      typedef typename Graph::edge_property_type Property;
sl@0
   146
      typedef typename Graph::graph_tag graph_tag;
sl@0
   147
      typedef typename edge_property_selector<graph_tag>::type Selector;
sl@0
   148
      typedef typename Selector::template bind_<Graph,Property,PropertyTag>
sl@0
   149
        Bind;
sl@0
   150
      typedef typename Bind::type type;
sl@0
   151
      typedef typename Bind::const_type const_type;
sl@0
   152
    };
sl@0
   153
    template <class Graph, class PropertyTag>
sl@0
   154
    class vertex_property_map {
sl@0
   155
      typedef typename Graph::vertex_property_type Property;
sl@0
   156
      typedef typename Graph::graph_tag graph_tag;
sl@0
   157
      typedef typename vertex_property_selector<graph_tag>::type Selector;
sl@0
   158
      typedef typename Selector::template bind_<Graph,Property,PropertyTag>
sl@0
   159
        Bind;
sl@0
   160
    public:
sl@0
   161
      typedef typename Bind::type type;
sl@0
   162
      typedef typename Bind::const_type const_type;
sl@0
   163
    };
sl@0
   164
sl@0
   165
    // This selects the kind of property map, whether is maps from
sl@0
   166
    // edges or from vertices.
sl@0
   167
    //
sl@0
   168
    // It is overly complicated because it's a workaround for
sl@0
   169
    // partial specialization.
sl@0
   170
    struct choose_vertex_property_map {
sl@0
   171
      template <class Graph, class Property>
sl@0
   172
      struct bind_ {
sl@0
   173
        typedef vertex_property_map<Graph, Property> type;
sl@0
   174
      };
sl@0
   175
    };
sl@0
   176
    struct choose_edge_property_map {
sl@0
   177
      template <class Graph, class Property>
sl@0
   178
      struct bind_ {
sl@0
   179
        typedef edge_property_map<Graph, Property> type;
sl@0
   180
      };
sl@0
   181
    };
sl@0
   182
    template <class Kind>
sl@0
   183
    struct property_map_kind_selector {
sl@0
   184
      // VC++ gets confused if this isn't defined, even though
sl@0
   185
      // this never gets used.
sl@0
   186
      typedef choose_vertex_property_map type;
sl@0
   187
    };
sl@0
   188
    template <> struct property_map_kind_selector<vertex_property_tag> {
sl@0
   189
      typedef choose_vertex_property_map type;
sl@0
   190
    };
sl@0
   191
    template <> struct property_map_kind_selector<edge_property_tag> {
sl@0
   192
      typedef choose_edge_property_map type;
sl@0
   193
    };
sl@0
   194
  } // namespace detail
sl@0
   195
sl@0
   196
  template <class Graph, class Property>
sl@0
   197
  struct property_map {
sl@0
   198
  private:
sl@0
   199
    typedef typename property_kind<Property>::type Kind;
sl@0
   200
    typedef typename detail::property_map_kind_selector<Kind>::type Selector;
sl@0
   201
    typedef typename Selector::template bind_<Graph, Property> Bind;
sl@0
   202
    typedef typename Bind::type Map;
sl@0
   203
  public:
sl@0
   204
    typedef typename Map::type type;
sl@0
   205
    typedef typename Map::const_type const_type;
sl@0
   206
  };
sl@0
   207
sl@0
   208
  // shortcut for accessing the value type of the property map
sl@0
   209
  template <class Graph, class Property>
sl@0
   210
  class property_map_value {
sl@0
   211
    typedef typename property_map<Graph, Property>::const_type PMap;
sl@0
   212
  public:
sl@0
   213
    typedef typename property_traits<PMap>::value_type type;
sl@0
   214
  };
sl@0
   215
sl@0
   216
  template <class Graph, class Property>
sl@0
   217
  class graph_property {
sl@0
   218
  public:
sl@0
   219
    typedef typename property_value<typename Graph::graph_property_type, 
sl@0
   220
      Property>::type type;
sl@0
   221
  };
sl@0
   222
sl@0
   223
  template <class Graph>
sl@0
   224
  class vertex_property {
sl@0
   225
  public:
sl@0
   226
    typedef typename Graph::vertex_property_type type;
sl@0
   227
  };
sl@0
   228
  template <class Graph>
sl@0
   229
  class edge_property {
sl@0
   230
  public:
sl@0
   231
    typedef typename Graph::edge_property_type type;
sl@0
   232
  };
sl@0
   233
sl@0
   234
  template <typename Graph>
sl@0
   235
  class degree_property_map 
sl@0
   236
    : public put_get_helper<typename graph_traits<Graph>::degree_size_type,
sl@0
   237
                            degree_property_map<Graph> >                  
sl@0
   238
  {
sl@0
   239
  public:
sl@0
   240
    typedef typename graph_traits<Graph>::vertex_descriptor key_type;
sl@0
   241
    typedef typename graph_traits<Graph>::degree_size_type value_type;
sl@0
   242
    typedef value_type reference;
sl@0
   243
    typedef readable_property_map_tag category;
sl@0
   244
    degree_property_map(const Graph& g) : m_g(g) { }
sl@0
   245
    value_type operator[](const key_type& v) const {
sl@0
   246
      return degree(v, m_g);
sl@0
   247
    }
sl@0
   248
  private:
sl@0
   249
    const Graph& m_g;
sl@0
   250
  };
sl@0
   251
  template <typename Graph>
sl@0
   252
  inline degree_property_map<Graph>
sl@0
   253
  make_degree_map(const Graph& g) {
sl@0
   254
    return degree_property_map<Graph>(g);
sl@0
   255
  }
sl@0
   256
sl@0
   257
  //========================================================================
sl@0
   258
  // Iterator Property Map Generating Functions contributed by 
sl@0
   259
  // Kevin Vanhorn. (see also the property map generating functions
sl@0
   260
  // in boost/property_map.hpp)
sl@0
   261
sl@0
   262
#if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
sl@0
   263
  // A helper function for creating a vertex property map out of a
sl@0
   264
  // random access iterator and the internal vertex index map from a
sl@0
   265
  // graph.
sl@0
   266
  template <class PropertyGraph, class RandomAccessIterator>
sl@0
   267
  inline
sl@0
   268
  iterator_property_map<
sl@0
   269
    RandomAccessIterator,
sl@0
   270
    typename property_map<PropertyGraph, vertex_index_t>::type,
sl@0
   271
    typename std::iterator_traits<RandomAccessIterator>::value_type,
sl@0
   272
    typename std::iterator_traits<RandomAccessIterator>::reference
sl@0
   273
  >
sl@0
   274
  make_iterator_vertex_map(RandomAccessIterator iter, const PropertyGraph& g)
sl@0
   275
  {
sl@0
   276
    return make_iterator_property_map(iter, get(vertex_index, g));
sl@0
   277
  }  
sl@0
   278
  
sl@0
   279
  // Use this next function when vertex_descriptor is known to be an
sl@0
   280
  // integer type, with values ranging from 0 to num_vertices(g).
sl@0
   281
  //
sl@0
   282
  template <class RandomAccessIterator>
sl@0
   283
  inline
sl@0
   284
  iterator_property_map<
sl@0
   285
    RandomAccessIterator,
sl@0
   286
    identity_property_map,
sl@0
   287
    typename std::iterator_traits<RandomAccessIterator>::value_type,
sl@0
   288
    typename std::iterator_traits<RandomAccessIterator>::reference
sl@0
   289
  >
sl@0
   290
  make_iterator_vertex_map(RandomAccessIterator iter)
sl@0
   291
  {
sl@0
   292
    return make_iterator_property_map(iter, identity_property_map());
sl@0
   293
  }      
sl@0
   294
#endif
sl@0
   295
sl@0
   296
  template <class PropertyGraph, class RandomAccessContainer>
sl@0
   297
  inline
sl@0
   298
  iterator_property_map<
sl@0
   299
    typename RandomAccessContainer::iterator,
sl@0
   300
    typename property_map<PropertyGraph, vertex_index_t>::type,
sl@0
   301
    typename RandomAccessContainer::value_type,
sl@0
   302
    typename RandomAccessContainer::reference
sl@0
   303
  >
sl@0
   304
  make_container_vertex_map(RandomAccessContainer& c, const PropertyGraph& g)
sl@0
   305
  {
sl@0
   306
    assert(c.size() >= num_vertices(g));
sl@0
   307
    return make_iterator_vertex_map(c.begin(), g);
sl@0
   308
  }   
sl@0
   309
sl@0
   310
  template <class RandomAccessContainer> inline
sl@0
   311
  iterator_property_map<
sl@0
   312
    typename RandomAccessContainer::iterator,
sl@0
   313
    identity_property_map,
sl@0
   314
    typename RandomAccessContainer::value_type,
sl@0
   315
    typename RandomAccessContainer::reference
sl@0
   316
  >
sl@0
   317
  make_container_vertex_map(RandomAccessContainer& c)
sl@0
   318
  {
sl@0
   319
    return make_iterator_vertex_map(c.begin());
sl@0
   320
  }
sl@0
   321
sl@0
   322
#if defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
sl@0
   323
#  define BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   324
#endif
sl@0
   325
sl@0
   326
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   327
  template<typename Graph, typename Descriptor, typename Bundle, typename T>
sl@0
   328
  struct bundle_property_map
sl@0
   329
    : put_get_helper<T&, bundle_property_map<Graph, Descriptor, Bundle, T> >
sl@0
   330
  {
sl@0
   331
    typedef Descriptor key_type;
sl@0
   332
    typedef T value_type;
sl@0
   333
    typedef T& reference;
sl@0
   334
    typedef lvalue_property_map_tag category;
sl@0
   335
 
sl@0
   336
    bundle_property_map() { }
sl@0
   337
    bundle_property_map(Graph* g_, T Bundle::* pm_) : g(g_), pm(pm_) {}
sl@0
   338
sl@0
   339
    reference operator[](key_type k) const { return (*g)[k].*pm; }
sl@0
   340
  private:
sl@0
   341
    Graph* g;
sl@0
   342
    T Bundle::* pm;
sl@0
   343
  };
sl@0
   344
sl@0
   345
  namespace detail {
sl@0
   346
    template<typename VertexBundle, typename EdgeBundle, typename Bundle>
sl@0
   347
      struct is_vertex_bundle : is_convertible<VertexBundle*, Bundle*> {};
sl@0
   348
  }
sl@0
   349
  
sl@0
   350
  template <typename Graph, typename T, typename Bundle>
sl@0
   351
  struct property_map<Graph, T Bundle::*>  
sl@0
   352
  {
sl@0
   353
  private:
sl@0
   354
    typedef graph_traits<Graph> traits;
sl@0
   355
    typedef typename Graph::vertex_bundled vertex_bundled;
sl@0
   356
    typedef typename Graph::edge_bundled edge_bundled;
sl@0
   357
    typedef typename ct_if<(detail::is_vertex_bundle<vertex_bundled, edge_bundled, Bundle>::value),
sl@0
   358
                       typename traits::vertex_descriptor,
sl@0
   359
                       typename traits::edge_descriptor>::type
sl@0
   360
      descriptor;
sl@0
   361
    typedef typename ct_if<(detail::is_vertex_bundle<vertex_bundled, edge_bundled, Bundle>::value),
sl@0
   362
                       vertex_bundled,
sl@0
   363
                       edge_bundled>::type
sl@0
   364
      actual_bundle;
sl@0
   365
    
sl@0
   366
  public:
sl@0
   367
    typedef bundle_property_map<Graph, descriptor, actual_bundle, T> type;
sl@0
   368
    typedef bundle_property_map<const Graph, descriptor, actual_bundle, const T>
sl@0
   369
      const_type;
sl@0
   370
  };
sl@0
   371
#endif
sl@0
   372
sl@0
   373
} // namespace boost
sl@0
   374
sl@0
   375
#endif /* BOOST_GRAPH_PROPERTIES_HPPA */