os/ossrv/ossrv_pub/boost_apis/boost/graph/adjacency_matrix.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 2001 University of Notre Dame.
sl@0
     3
// Copyright 2006 Trustees of Indiana University
sl@0
     4
// Authors: Jeremy G. Siek and Douglas Gregor <dgregor@cs.indiana.edu>
sl@0
     5
//
sl@0
     6
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     7
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     8
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     9
//=======================================================================
sl@0
    10
sl@0
    11
#ifndef BOOST_ADJACENCY_MATRIX_HPP
sl@0
    12
#define BOOST_ADJACENCY_MATRIX_HPP
sl@0
    13
sl@0
    14
#include <boost/config.hpp>
sl@0
    15
#include <vector>
sl@0
    16
#include <memory>
sl@0
    17
#include <cassert>
sl@0
    18
#include <boost/limits.hpp>
sl@0
    19
#include <boost/iterator.hpp>
sl@0
    20
#include <boost/graph/graph_traits.hpp>
sl@0
    21
#include <boost/graph/graph_selectors.hpp>
sl@0
    22
#include <boost/pending/ct_if.hpp>
sl@0
    23
#include <boost/graph/adjacency_iterator.hpp>
sl@0
    24
#include <boost/graph/detail/edge.hpp>
sl@0
    25
#include <boost/iterator/iterator_adaptor.hpp>
sl@0
    26
#include <boost/iterator/filter_iterator.hpp>
sl@0
    27
#include <boost/pending/integer_range.hpp>
sl@0
    28
#include <boost/graph/properties.hpp>
sl@0
    29
#include <boost/tuple/tuple.hpp>
sl@0
    30
#include <boost/static_assert.hpp>
sl@0
    31
#include <boost/type_traits/ice.hpp>
sl@0
    32
sl@0
    33
namespace boost {
sl@0
    34
  
sl@0
    35
  namespace detail {
sl@0
    36
sl@0
    37
    template <class Directed, class Vertex>
sl@0
    38
    class matrix_edge_desc_impl : public edge_desc_impl<Directed,Vertex>
sl@0
    39
    {
sl@0
    40
      typedef edge_desc_impl<Directed,Vertex> Base;
sl@0
    41
    public:
sl@0
    42
      matrix_edge_desc_impl() { }
sl@0
    43
      matrix_edge_desc_impl(bool exists, Vertex s, Vertex d, 
sl@0
    44
                            const void* ep = 0)
sl@0
    45
        : Base(s, d, ep), m_exists(exists) { }
sl@0
    46
      bool exists() const { return m_exists; }
sl@0
    47
    private:
sl@0
    48
      bool m_exists;
sl@0
    49
    };
sl@0
    50
sl@0
    51
    struct does_edge_exist {
sl@0
    52
      template <class Edge>
sl@0
    53
      bool operator()(const Edge& e) const { return e.exists(); }
sl@0
    54
    };
sl@0
    55
sl@0
    56
    template <typename EdgeProperty>
sl@0
    57
    bool get_edge_exists(const std::pair<bool, EdgeProperty>& stored_edge, int) {
sl@0
    58
      return stored_edge.first;
sl@0
    59
    }
sl@0
    60
    template <typename EdgeProperty>
sl@0
    61
    void set_edge_exists(
sl@0
    62
        std::pair<bool, EdgeProperty>& stored_edge, 
sl@0
    63
        bool flag,
sl@0
    64
        int
sl@0
    65
        ) {
sl@0
    66
      stored_edge.first = flag;
sl@0
    67
    }
sl@0
    68
sl@0
    69
    template <typename EdgeProxy>
sl@0
    70
    bool get_edge_exists(const EdgeProxy& edge_proxy, ...) {
sl@0
    71
      return edge_proxy;
sl@0
    72
    }
sl@0
    73
    template <typename EdgeProxy>
sl@0
    74
    EdgeProxy& set_edge_exists(EdgeProxy& edge_proxy, bool flag, ...) {
sl@0
    75
      edge_proxy = flag;
sl@0
    76
      return edge_proxy; // just to avoid never used warning
sl@0
    77
    }
sl@0
    78
sl@0
    79
sl@0
    80
sl@0
    81
    template <typename EdgeProperty>
sl@0
    82
    const EdgeProperty&
sl@0
    83
    get_property(const std::pair<bool, EdgeProperty>& stored_edge) {
sl@0
    84
      return stored_edge.second;
sl@0
    85
    }
sl@0
    86
    template <typename EdgeProperty>
sl@0
    87
    EdgeProperty&
sl@0
    88
    get_property(std::pair<bool, EdgeProperty>& stored_edge) {
sl@0
    89
      return stored_edge.second;
sl@0
    90
    }
sl@0
    91
sl@0
    92
    template <typename StoredEdgeProperty, typename EdgeProperty>
sl@0
    93
    inline void
sl@0
    94
    set_property(std::pair<bool, StoredEdgeProperty>& stored_edge, 
sl@0
    95
                 const EdgeProperty& ep, int) {
sl@0
    96
      stored_edge.second = ep;
sl@0
    97
    }
sl@0
    98
sl@0
    99
    inline const no_property& get_property(const char&) {
sl@0
   100
      static no_property s_prop;
sl@0
   101
      return s_prop;
sl@0
   102
    }
sl@0
   103
    inline no_property& get_property(char&) {
sl@0
   104
      static no_property s_prop;
sl@0
   105
      return s_prop;
sl@0
   106
    }
sl@0
   107
    template <typename EdgeProxy, typename EdgeProperty>
sl@0
   108
    inline void
sl@0
   109
    set_property(EdgeProxy, const EdgeProperty&, ...) {}
sl@0
   110
    
sl@0
   111
    //=======================================================================
sl@0
   112
    // Directed Out Edge Iterator
sl@0
   113
sl@0
   114
    template <
sl@0
   115
        typename VertexDescriptor, typename MatrixIter
sl@0
   116
      , typename VerticesSizeType, typename EdgeDescriptor
sl@0
   117
    >
sl@0
   118
    struct dir_adj_matrix_out_edge_iter
sl@0
   119
      : iterator_adaptor<
sl@0
   120
            dir_adj_matrix_out_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   121
          , MatrixIter
sl@0
   122
          , EdgeDescriptor
sl@0
   123
          , use_default
sl@0
   124
          , EdgeDescriptor
sl@0
   125
          , std::ptrdiff_t
sl@0
   126
        >
sl@0
   127
    {
sl@0
   128
        typedef iterator_adaptor<
sl@0
   129
            dir_adj_matrix_out_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   130
          , MatrixIter
sl@0
   131
          , EdgeDescriptor
sl@0
   132
          , use_default
sl@0
   133
          , EdgeDescriptor
sl@0
   134
          , std::ptrdiff_t
sl@0
   135
        > super_t;
sl@0
   136
        
sl@0
   137
        dir_adj_matrix_out_edge_iter() { }
sl@0
   138
        
sl@0
   139
        dir_adj_matrix_out_edge_iter(
sl@0
   140
            const MatrixIter& i
sl@0
   141
          , const VertexDescriptor& src
sl@0
   142
          , const VerticesSizeType& n
sl@0
   143
           )
sl@0
   144
            : super_t(i), m_src(src), m_targ(0), m_n(n)
sl@0
   145
        { }
sl@0
   146
sl@0
   147
        void increment() {
sl@0
   148
            ++this->base_reference();
sl@0
   149
            ++m_targ;
sl@0
   150
        }
sl@0
   151
        
sl@0
   152
        inline EdgeDescriptor
sl@0
   153
        dereference() const 
sl@0
   154
        {
sl@0
   155
            return EdgeDescriptor(get_edge_exists(*this->base(), 0), m_src, m_targ, 
sl@0
   156
                                  &get_property(*this->base()));
sl@0
   157
        }
sl@0
   158
        VertexDescriptor m_src, m_targ;
sl@0
   159
        VerticesSizeType m_n;
sl@0
   160
    };
sl@0
   161
sl@0
   162
    //=======================================================================
sl@0
   163
    // Directed In Edge Iterator
sl@0
   164
sl@0
   165
    template <
sl@0
   166
        typename VertexDescriptor, typename MatrixIter
sl@0
   167
      , typename VerticesSizeType, typename EdgeDescriptor
sl@0
   168
    >
sl@0
   169
    struct dir_adj_matrix_in_edge_iter
sl@0
   170
      : iterator_adaptor<
sl@0
   171
            dir_adj_matrix_in_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   172
          , MatrixIter
sl@0
   173
          , EdgeDescriptor
sl@0
   174
          , use_default
sl@0
   175
          , EdgeDescriptor
sl@0
   176
          , std::ptrdiff_t
sl@0
   177
        >
sl@0
   178
    {
sl@0
   179
        typedef iterator_adaptor<
sl@0
   180
            dir_adj_matrix_in_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   181
          , MatrixIter
sl@0
   182
          , EdgeDescriptor
sl@0
   183
          , use_default
sl@0
   184
          , EdgeDescriptor
sl@0
   185
          , std::ptrdiff_t
sl@0
   186
        > super_t;
sl@0
   187
        
sl@0
   188
        dir_adj_matrix_in_edge_iter() { }
sl@0
   189
        
sl@0
   190
        dir_adj_matrix_in_edge_iter(
sl@0
   191
            const MatrixIter& i
sl@0
   192
          , const MatrixIter& last
sl@0
   193
          , const VertexDescriptor& tgt
sl@0
   194
          , const VerticesSizeType& n
sl@0
   195
           )
sl@0
   196
          : super_t(i), m_last(last), m_src(0), m_targ(tgt), m_n(n)
sl@0
   197
        { }
sl@0
   198
sl@0
   199
        void increment() {
sl@0
   200
          if (VerticesSizeType(m_last - this->base_reference()) >= m_n) {
sl@0
   201
            this->base_reference() += m_n;
sl@0
   202
            ++m_src;
sl@0
   203
          } else {
sl@0
   204
            this->base_reference() = m_last;
sl@0
   205
          }
sl@0
   206
        }
sl@0
   207
        
sl@0
   208
        inline EdgeDescriptor
sl@0
   209
        dereference() const 
sl@0
   210
        {
sl@0
   211
            return EdgeDescriptor(get_edge_exists(*this->base(), 0), m_src, m_targ, 
sl@0
   212
                                  &get_property(*this->base()));
sl@0
   213
        }
sl@0
   214
        MatrixIter m_last;
sl@0
   215
        VertexDescriptor m_src, m_targ;
sl@0
   216
        VerticesSizeType m_n;
sl@0
   217
    };
sl@0
   218
sl@0
   219
    //=======================================================================
sl@0
   220
    // Undirected Out Edge Iterator
sl@0
   221
sl@0
   222
    template <
sl@0
   223
        typename VertexDescriptor, typename MatrixIter
sl@0
   224
      , typename VerticesSizeType, typename EdgeDescriptor
sl@0
   225
    >
sl@0
   226
    struct undir_adj_matrix_out_edge_iter 
sl@0
   227
      : iterator_adaptor<
sl@0
   228
            undir_adj_matrix_out_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   229
          , MatrixIter
sl@0
   230
          , EdgeDescriptor
sl@0
   231
          , use_default
sl@0
   232
          , EdgeDescriptor
sl@0
   233
          , std::ptrdiff_t
sl@0
   234
        >
sl@0
   235
    {
sl@0
   236
        typedef iterator_adaptor<
sl@0
   237
            undir_adj_matrix_out_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   238
          , MatrixIter
sl@0
   239
          , EdgeDescriptor
sl@0
   240
          , use_default
sl@0
   241
          , EdgeDescriptor
sl@0
   242
          , std::ptrdiff_t
sl@0
   243
        > super_t;
sl@0
   244
        
sl@0
   245
        undir_adj_matrix_out_edge_iter() { }
sl@0
   246
        
sl@0
   247
        undir_adj_matrix_out_edge_iter(
sl@0
   248
            const MatrixIter& i
sl@0
   249
          , const VertexDescriptor& src
sl@0
   250
          , const VerticesSizeType& n
sl@0
   251
        )
sl@0
   252
          : super_t(i), m_src(src), m_inc(src), m_targ(0), m_n(n)
sl@0
   253
        {}
sl@0
   254
sl@0
   255
        void increment()
sl@0
   256
        {
sl@0
   257
            if (m_targ < m_src)     // first half
sl@0
   258
            {
sl@0
   259
                ++this->base_reference();
sl@0
   260
            }
sl@0
   261
            else if (m_targ < m_n - 1)
sl@0
   262
            {                  // second half
sl@0
   263
                ++m_inc;
sl@0
   264
                this->base_reference() += m_inc;
sl@0
   265
            }
sl@0
   266
            else
sl@0
   267
            {                  // past-the-end
sl@0
   268
                this->base_reference() += m_n - m_src;
sl@0
   269
            }
sl@0
   270
            ++m_targ;
sl@0
   271
        }
sl@0
   272
        
sl@0
   273
        inline EdgeDescriptor
sl@0
   274
        dereference() const 
sl@0
   275
        {
sl@0
   276
            return EdgeDescriptor(
sl@0
   277
                get_edge_exists(*this->base(), 0), m_src, m_targ
sl@0
   278
              , &get_property(*this->base())
sl@0
   279
            );
sl@0
   280
        }
sl@0
   281
        
sl@0
   282
        VertexDescriptor m_src, m_inc, m_targ;
sl@0
   283
        VerticesSizeType m_n;
sl@0
   284
    };
sl@0
   285
sl@0
   286
    //=======================================================================
sl@0
   287
    // Undirected In Edge Iterator
sl@0
   288
sl@0
   289
    template <
sl@0
   290
        typename VertexDescriptor, typename MatrixIter
sl@0
   291
      , typename VerticesSizeType, typename EdgeDescriptor
sl@0
   292
    >
sl@0
   293
    struct undir_adj_matrix_in_edge_iter 
sl@0
   294
      : iterator_adaptor<
sl@0
   295
            undir_adj_matrix_in_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   296
          , MatrixIter
sl@0
   297
          , EdgeDescriptor
sl@0
   298
          , use_default
sl@0
   299
          , EdgeDescriptor
sl@0
   300
          , std::ptrdiff_t
sl@0
   301
        >
sl@0
   302
    {
sl@0
   303
        typedef iterator_adaptor<
sl@0
   304
            undir_adj_matrix_in_edge_iter<VertexDescriptor, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   305
          , MatrixIter
sl@0
   306
          , EdgeDescriptor
sl@0
   307
          , use_default
sl@0
   308
          , EdgeDescriptor
sl@0
   309
          , std::ptrdiff_t
sl@0
   310
        > super_t;
sl@0
   311
        
sl@0
   312
        undir_adj_matrix_in_edge_iter() { }
sl@0
   313
        
sl@0
   314
        undir_adj_matrix_in_edge_iter(
sl@0
   315
            const MatrixIter& i
sl@0
   316
          , const VertexDescriptor& src
sl@0
   317
          , const VerticesSizeType& n
sl@0
   318
        )
sl@0
   319
          : super_t(i), m_src(src), m_inc(src), m_targ(0), m_n(n)
sl@0
   320
        {}
sl@0
   321
sl@0
   322
        void increment()
sl@0
   323
        {
sl@0
   324
            if (m_targ < m_src)     // first half
sl@0
   325
            {
sl@0
   326
                ++this->base_reference();
sl@0
   327
            }
sl@0
   328
            else if (m_targ < m_n - 1)
sl@0
   329
            {                  // second half
sl@0
   330
                ++m_inc;
sl@0
   331
                this->base_reference() += m_inc;
sl@0
   332
            }
sl@0
   333
            else
sl@0
   334
            {                  // past-the-end
sl@0
   335
                this->base_reference() += m_n - m_src;
sl@0
   336
            }
sl@0
   337
            ++m_targ;
sl@0
   338
        }
sl@0
   339
        
sl@0
   340
        inline EdgeDescriptor
sl@0
   341
        dereference() const 
sl@0
   342
        {
sl@0
   343
            return EdgeDescriptor(
sl@0
   344
                     get_edge_exists(*this->base(), 0), m_targ, m_src
sl@0
   345
              , &get_property(*this->base())
sl@0
   346
            );
sl@0
   347
        }
sl@0
   348
        
sl@0
   349
        VertexDescriptor m_src, m_inc, m_targ;
sl@0
   350
        VerticesSizeType m_n;
sl@0
   351
    };
sl@0
   352
sl@0
   353
    //=======================================================================
sl@0
   354
    // Edge Iterator
sl@0
   355
sl@0
   356
    template <typename Directed, typename MatrixIter, 
sl@0
   357
              typename VerticesSizeType, typename EdgeDescriptor>
sl@0
   358
    struct adj_matrix_edge_iter
sl@0
   359
      : iterator_adaptor<
sl@0
   360
            adj_matrix_edge_iter<Directed, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   361
          , MatrixIter
sl@0
   362
          , EdgeDescriptor
sl@0
   363
          , use_default
sl@0
   364
          , EdgeDescriptor
sl@0
   365
          , std::ptrdiff_t
sl@0
   366
        >
sl@0
   367
    {
sl@0
   368
        typedef iterator_adaptor<
sl@0
   369
            adj_matrix_edge_iter<Directed, MatrixIter,  VerticesSizeType, EdgeDescriptor>
sl@0
   370
          , MatrixIter
sl@0
   371
          , EdgeDescriptor
sl@0
   372
          , use_default
sl@0
   373
          , EdgeDescriptor
sl@0
   374
          , std::ptrdiff_t
sl@0
   375
        > super_t;
sl@0
   376
        
sl@0
   377
        adj_matrix_edge_iter() { }
sl@0
   378
        
sl@0
   379
        adj_matrix_edge_iter(const MatrixIter& i, const MatrixIter& start, const VerticesSizeType& n) 
sl@0
   380
            : super_t(i), m_start(start), m_src(0), m_targ(0), m_n(n) { }
sl@0
   381
sl@0
   382
        void increment()
sl@0
   383
        {
sl@0
   384
            increment_dispatch(this->base_reference(), Directed());
sl@0
   385
        }
sl@0
   386
        
sl@0
   387
        void increment_dispatch(MatrixIter& i, directedS)
sl@0
   388
        {
sl@0
   389
            ++i;
sl@0
   390
            if (m_targ == m_n - 1)
sl@0
   391
            {
sl@0
   392
                m_targ = 0;
sl@0
   393
                ++m_src;
sl@0
   394
            }
sl@0
   395
            else
sl@0
   396
            {
sl@0
   397
                ++m_targ;
sl@0
   398
            }
sl@0
   399
        }
sl@0
   400
        
sl@0
   401
        void increment_dispatch(MatrixIter& i, undirectedS)
sl@0
   402
        {
sl@0
   403
            ++i;
sl@0
   404
            if (m_targ == m_src)
sl@0
   405
            {
sl@0
   406
                m_targ = 0;
sl@0
   407
                ++m_src;
sl@0
   408
            }
sl@0
   409
            else
sl@0
   410
            {
sl@0
   411
                ++m_targ;
sl@0
   412
            }
sl@0
   413
        }
sl@0
   414
sl@0
   415
        inline EdgeDescriptor
sl@0
   416
        dereference() const 
sl@0
   417
        {
sl@0
   418
            return EdgeDescriptor(
sl@0
   419
                get_edge_exists(
sl@0
   420
                    *this->base(), 0), m_src, m_targ, &get_property(*this->base())
sl@0
   421
            );
sl@0
   422
        }
sl@0
   423
      
sl@0
   424
        MatrixIter m_start;
sl@0
   425
        VerticesSizeType m_src, m_targ, m_n;
sl@0
   426
    };
sl@0
   427
sl@0
   428
  } // namespace detail
sl@0
   429
sl@0
   430
  //=========================================================================
sl@0
   431
  // Adjacency Matrix Traits
sl@0
   432
  template <typename Directed = directedS>
sl@0
   433
  class adjacency_matrix_traits {
sl@0
   434
    typedef typename Directed::is_directed_t is_directed;
sl@0
   435
  public:
sl@0
   436
    // The bidirectionalS tag is not allowed with the adjacency_matrix
sl@0
   437
    // graph type. Instead, use directedS, which also provides the
sl@0
   438
    // functionality required for a Bidirectional Graph (in_edges,
sl@0
   439
    // in_degree, etc.).
sl@0
   440
#if !defined(_MSC_VER) || _MSC_VER > 1300
sl@0
   441
    BOOST_STATIC_ASSERT(type_traits::ice_not<(is_same<Directed, bidirectionalS>::value)>::value);
sl@0
   442
#endif
sl@0
   443
sl@0
   444
    typedef typename boost::ct_if_t<is_directed,
sl@0
   445
                                    bidirectional_tag, undirected_tag>::type
sl@0
   446
      directed_category;
sl@0
   447
    
sl@0
   448
    typedef disallow_parallel_edge_tag edge_parallel_category;
sl@0
   449
    
sl@0
   450
    typedef std::size_t vertex_descriptor;
sl@0
   451
sl@0
   452
    typedef detail::matrix_edge_desc_impl<directed_category,
sl@0
   453
      vertex_descriptor> edge_descriptor;
sl@0
   454
  };
sl@0
   455
sl@0
   456
  struct adjacency_matrix_class_tag { };
sl@0
   457
sl@0
   458
  struct adj_matrix_traversal_tag :
sl@0
   459
    public virtual adjacency_matrix_tag,
sl@0
   460
    public virtual vertex_list_graph_tag,
sl@0
   461
    public virtual incidence_graph_tag,
sl@0
   462
    public virtual adjacency_graph_tag,
sl@0
   463
    public virtual edge_list_graph_tag { };
sl@0
   464
  
sl@0
   465
  //=========================================================================
sl@0
   466
  // Adjacency Matrix Class
sl@0
   467
  template <typename Directed = directedS,
sl@0
   468
            typename VertexProperty = no_property,
sl@0
   469
            typename EdgeProperty = no_property,
sl@0
   470
            typename GraphProperty = no_property,
sl@0
   471
            typename Allocator = std::allocator<bool> >
sl@0
   472
  class adjacency_matrix {
sl@0
   473
    typedef adjacency_matrix self;
sl@0
   474
    typedef adjacency_matrix_traits<Directed> Traits;
sl@0
   475
    
sl@0
   476
  public:
sl@0
   477
#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
sl@0
   478
    // The bidirectionalS tag is not allowed with the adjacency_matrix
sl@0
   479
    // graph type. Instead, use directedS, which also provides the
sl@0
   480
    // functionality required for a Bidirectional Graph (in_edges,
sl@0
   481
    // in_degree, etc.).
sl@0
   482
    BOOST_STATIC_ASSERT(!(is_same<Directed, bidirectionalS>::value));
sl@0
   483
#endif
sl@0
   484
sl@0
   485
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   486
    typedef typename detail::retag_property_list<vertex_bundle_t, VertexProperty>::type
sl@0
   487
      vertex_property_type;
sl@0
   488
    typedef typename detail::retag_property_list<edge_bundle_t, EdgeProperty>::type
sl@0
   489
      edge_property_type;
sl@0
   490
      
sl@0
   491
  private:
sl@0
   492
    typedef typename detail::retag_property_list<vertex_bundle_t, VertexProperty>::retagged
sl@0
   493
      maybe_vertex_bundled;
sl@0
   494
sl@0
   495
    typedef typename detail::retag_property_list<edge_bundle_t, EdgeProperty>::retagged
sl@0
   496
      maybe_edge_bundled;
sl@0
   497
    
sl@0
   498
  public:
sl@0
   499
    // The types that are actually bundled
sl@0
   500
    typedef typename ct_if<(is_same<maybe_vertex_bundled, no_property>::value),
sl@0
   501
                           no_vertex_bundle,
sl@0
   502
                           maybe_vertex_bundled>::type vertex_bundled;
sl@0
   503
    typedef typename ct_if<(is_same<maybe_edge_bundled, no_property>::value),
sl@0
   504
                           no_edge_bundle,
sl@0
   505
                           maybe_edge_bundled>::type edge_bundled;
sl@0
   506
#else
sl@0
   507
    typedef EdgeProperty     edge_property_type;
sl@0
   508
    typedef VertexProperty   vertex_property_type;
sl@0
   509
    typedef no_vertex_bundle vertex_bundled;
sl@0
   510
    typedef no_edge_bundle   edge_bundled;
sl@0
   511
#endif
sl@0
   512
sl@0
   513
  public: // should be private
sl@0
   514
    typedef typename ct_if_t<typename has_property<edge_property_type>::type,
sl@0
   515
      std::pair<bool, edge_property_type>, char>::type StoredEdge;
sl@0
   516
#if (defined(BOOST_MSVC) && BOOST_MSVC <= 1300) || defined(BOOST_NO_STD_ALLOCATOR)
sl@0
   517
    typedef std::vector<StoredEdge> Matrix;
sl@0
   518
#else
sl@0
   519
    // This causes internal compiler error for MSVC
sl@0
   520
    typedef typename Allocator::template rebind<StoredEdge>::other Alloc;
sl@0
   521
    typedef std::vector<StoredEdge, Alloc> Matrix;
sl@0
   522
#endif
sl@0
   523
    typedef typename Matrix::iterator MatrixIter;
sl@0
   524
    typedef typename Matrix::size_type size_type;
sl@0
   525
  public:
sl@0
   526
    // Graph concept required types
sl@0
   527
    typedef typename Traits::vertex_descriptor vertex_descriptor;
sl@0
   528
    typedef typename Traits::edge_descriptor edge_descriptor;
sl@0
   529
    typedef typename Traits::directed_category directed_category;
sl@0
   530
    typedef typename Traits::edge_parallel_category edge_parallel_category;
sl@0
   531
    typedef adj_matrix_traversal_tag traversal_category;
sl@0
   532
sl@0
   533
    static vertex_descriptor null_vertex()
sl@0
   534
    {
sl@0
   535
      return (std::numeric_limits<vertex_descriptor>::max)();
sl@0
   536
    }
sl@0
   537
      
sl@0
   538
    //private: if friends worked, these would be private
sl@0
   539
sl@0
   540
    typedef detail::dir_adj_matrix_out_edge_iter<
sl@0
   541
        vertex_descriptor, MatrixIter, size_type, edge_descriptor
sl@0
   542
    > DirOutEdgeIter;
sl@0
   543
sl@0
   544
    typedef detail::undir_adj_matrix_out_edge_iter<
sl@0
   545
        vertex_descriptor, MatrixIter, size_type, edge_descriptor
sl@0
   546
    > UnDirOutEdgeIter;
sl@0
   547
sl@0
   548
    typedef typename ct_if_t<
sl@0
   549
        typename Directed::is_directed_t, DirOutEdgeIter, UnDirOutEdgeIter
sl@0
   550
    >::type unfiltered_out_edge_iter;
sl@0
   551
sl@0
   552
    typedef detail::dir_adj_matrix_in_edge_iter<
sl@0
   553
        vertex_descriptor, MatrixIter, size_type, edge_descriptor
sl@0
   554
    > DirInEdgeIter;
sl@0
   555
sl@0
   556
    typedef detail::undir_adj_matrix_in_edge_iter<
sl@0
   557
        vertex_descriptor, MatrixIter, size_type, edge_descriptor
sl@0
   558
    > UnDirInEdgeIter;
sl@0
   559
sl@0
   560
    typedef typename ct_if_t<
sl@0
   561
        typename Directed::is_directed_t, DirInEdgeIter, UnDirInEdgeIter
sl@0
   562
    >::type unfiltered_in_edge_iter;
sl@0
   563
    
sl@0
   564
    typedef detail::adj_matrix_edge_iter<
sl@0
   565
        Directed, MatrixIter, size_type, edge_descriptor
sl@0
   566
    > unfiltered_edge_iter;
sl@0
   567
    
sl@0
   568
  public:
sl@0
   569
sl@0
   570
    // IncidenceGraph concept required types
sl@0
   571
    typedef filter_iterator<detail::does_edge_exist, unfiltered_out_edge_iter>
sl@0
   572
      out_edge_iterator;
sl@0
   573
sl@0
   574
    typedef size_type degree_size_type;
sl@0
   575
sl@0
   576
    // BidirectionalGraph required types
sl@0
   577
    typedef filter_iterator<detail::does_edge_exist, unfiltered_in_edge_iter>
sl@0
   578
      in_edge_iterator;
sl@0
   579
sl@0
   580
    // AdjacencyGraph required types
sl@0
   581
     typedef typename adjacency_iterator_generator<self,
sl@0
   582
       vertex_descriptor, out_edge_iterator>::type adjacency_iterator;
sl@0
   583
sl@0
   584
    // VertexListGraph required types
sl@0
   585
    typedef size_type vertices_size_type;
sl@0
   586
    typedef integer_range<vertex_descriptor> VertexList;
sl@0
   587
    typedef typename VertexList::iterator vertex_iterator;
sl@0
   588
sl@0
   589
    // EdgeListGraph required types
sl@0
   590
    typedef size_type edges_size_type;
sl@0
   591
    typedef filter_iterator<
sl@0
   592
        detail::does_edge_exist, unfiltered_edge_iter
sl@0
   593
    > edge_iterator;
sl@0
   594
sl@0
   595
    // PropertyGraph required types
sl@0
   596
    typedef adjacency_matrix_class_tag graph_tag;
sl@0
   597
sl@0
   598
    // Constructor required by MutableGraph
sl@0
   599
    adjacency_matrix(vertices_size_type n_vertices) 
sl@0
   600
      : m_matrix(Directed::is_directed ? 
sl@0
   601
                 (n_vertices * n_vertices)
sl@0
   602
                 : (n_vertices * (n_vertices + 1) / 2)),
sl@0
   603
      m_vertex_set(0, n_vertices),
sl@0
   604
      m_vertex_properties(n_vertices),
sl@0
   605
      m_num_edges(0) { }
sl@0
   606
sl@0
   607
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
   608
    // Directly access a vertex or edge bundle
sl@0
   609
    vertex_bundled& operator[](vertex_descriptor v)
sl@0
   610
    { return get(vertex_bundle, *this)[v]; }
sl@0
   611
sl@0
   612
    const vertex_bundled& operator[](vertex_descriptor v) const
sl@0
   613
    { return get(vertex_bundle, *this)[v]; }
sl@0
   614
sl@0
   615
    edge_bundled& operator[](edge_descriptor e)
sl@0
   616
    { return get(edge_bundle, *this)[e]; }
sl@0
   617
sl@0
   618
    const edge_bundled& operator[](edge_descriptor e) const
sl@0
   619
    { return get(edge_bundle, *this)[e]; }
sl@0
   620
#endif
sl@0
   621
    
sl@0
   622
    //private: if friends worked, these would be private
sl@0
   623
sl@0
   624
    typename Matrix::const_reference
sl@0
   625
    get_edge(vertex_descriptor u, vertex_descriptor v) const {
sl@0
   626
      if (Directed::is_directed)
sl@0
   627
        return m_matrix[u * m_vertex_set.size() + v];
sl@0
   628
      else {
sl@0
   629
        if (v > u)
sl@0
   630
          std::swap(u, v);
sl@0
   631
        return m_matrix[u * (u + 1)/2 + v];
sl@0
   632
      }
sl@0
   633
    }
sl@0
   634
    typename Matrix::reference
sl@0
   635
    get_edge(vertex_descriptor u, vertex_descriptor v) {
sl@0
   636
      if (Directed::is_directed)
sl@0
   637
        return m_matrix[u * m_vertex_set.size() + v];
sl@0
   638
      else {
sl@0
   639
        if (v > u)
sl@0
   640
          std::swap(u, v);
sl@0
   641
        return m_matrix[u * (u + 1)/2 + v];
sl@0
   642
      }
sl@0
   643
    }
sl@0
   644
sl@0
   645
    Matrix m_matrix;
sl@0
   646
    VertexList m_vertex_set;
sl@0
   647
    std::vector<vertex_property_type> m_vertex_properties;
sl@0
   648
    size_type m_num_edges;
sl@0
   649
  };
sl@0
   650
  
sl@0
   651
  //=========================================================================
sl@0
   652
  // Functions required by the AdjacencyMatrix concept 
sl@0
   653
sl@0
   654
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   655
  std::pair<typename adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor,
sl@0
   656
            bool>
sl@0
   657
  edge(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   658
       typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor v,
sl@0
   659
       const adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   660
  {
sl@0
   661
    bool exists = detail::get_edge_exists(g.get_edge(u,v), 0);
sl@0
   662
    typename adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor
sl@0
   663
      e(exists, u, v, &detail::get_property(g.get_edge(u,v)));
sl@0
   664
    return std::make_pair(e, exists);
sl@0
   665
  }
sl@0
   666
sl@0
   667
  //=========================================================================
sl@0
   668
  // Functions required by the IncidenceGraph concept 
sl@0
   669
sl@0
   670
  // O(1)
sl@0
   671
  template <typename VP, typename EP, typename GP, typename A>
sl@0
   672
  std::pair<typename adjacency_matrix<directedS,VP,EP,GP,A>::out_edge_iterator,
sl@0
   673
            typename adjacency_matrix<directedS,VP,EP,GP,A>::out_edge_iterator>
sl@0
   674
  out_edges
sl@0
   675
    (typename adjacency_matrix<directedS,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   676
     const adjacency_matrix<directedS,VP,EP,GP,A>& g_)
sl@0
   677
  {
sl@0
   678
    typedef adjacency_matrix<directedS,VP,EP,GP,A> Graph;
sl@0
   679
    Graph& g = const_cast<Graph&>(g_);
sl@0
   680
    typename Graph::vertices_size_type offset = u * g.m_vertex_set.size();
sl@0
   681
    typename Graph::MatrixIter f = g.m_matrix.begin() + offset;
sl@0
   682
    typename Graph::MatrixIter l = f + g.m_vertex_set.size();
sl@0
   683
    typename Graph::unfiltered_out_edge_iter
sl@0
   684
          first(f, u, g.m_vertex_set.size())
sl@0
   685
        , last(l, u, g.m_vertex_set.size());
sl@0
   686
    detail::does_edge_exist pred;
sl@0
   687
    typedef typename Graph::out_edge_iterator out_edge_iterator;
sl@0
   688
    return std::make_pair(out_edge_iterator(pred, first, last), 
sl@0
   689
                          out_edge_iterator(pred, last, last));
sl@0
   690
  }
sl@0
   691
sl@0
   692
  // O(1)
sl@0
   693
  template <typename VP, typename EP, typename GP, typename A>
sl@0
   694
  std::pair<
sl@0
   695
    typename adjacency_matrix<undirectedS,VP,EP,GP,A>::out_edge_iterator,
sl@0
   696
    typename adjacency_matrix<undirectedS,VP,EP,GP,A>::out_edge_iterator>
sl@0
   697
  out_edges
sl@0
   698
    (typename adjacency_matrix<undirectedS,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   699
     const adjacency_matrix<undirectedS,VP,EP,GP,A>& g_)
sl@0
   700
  {
sl@0
   701
    typedef adjacency_matrix<undirectedS,VP,EP,GP,A> Graph;
sl@0
   702
    Graph& g = const_cast<Graph&>(g_);
sl@0
   703
    typename Graph::vertices_size_type offset = u * (u + 1) / 2;
sl@0
   704
    typename Graph::MatrixIter f = g.m_matrix.begin() + offset;
sl@0
   705
    typename Graph::MatrixIter l = g.m_matrix.end();
sl@0
   706
sl@0
   707
    typename Graph::unfiltered_out_edge_iter
sl@0
   708
        first(f, u, g.m_vertex_set.size())
sl@0
   709
      , last(l, u, g.m_vertex_set.size());
sl@0
   710
    
sl@0
   711
    detail::does_edge_exist pred;
sl@0
   712
    typedef typename Graph::out_edge_iterator out_edge_iterator;
sl@0
   713
    return std::make_pair(out_edge_iterator(pred, first, last), 
sl@0
   714
                          out_edge_iterator(pred, last, last));
sl@0
   715
  }
sl@0
   716
  
sl@0
   717
  // O(N)
sl@0
   718
  template <typename D, typename VP, typename EP, typename GP, typename A>  
sl@0
   719
  typename adjacency_matrix<D,VP,EP,GP,A>::degree_size_type
sl@0
   720
  out_degree(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   721
             const adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   722
  {
sl@0
   723
    typename adjacency_matrix<D,VP,EP,GP,A>::degree_size_type n = 0;
sl@0
   724
    typename adjacency_matrix<D,VP,EP,GP,A>::out_edge_iterator f, l;
sl@0
   725
    for (tie(f, l) = out_edges(u, g); f != l; ++f)
sl@0
   726
      ++n;
sl@0
   727
    return n;
sl@0
   728
  }
sl@0
   729
sl@0
   730
  // O(1)
sl@0
   731
  template <typename D, typename VP, typename EP, typename GP, typename A,
sl@0
   732
    typename Dir, typename Vertex>  
sl@0
   733
  typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor
sl@0
   734
  source(const detail::matrix_edge_desc_impl<Dir,Vertex>& e,
sl@0
   735
         const adjacency_matrix<D,VP,EP,GP,A>&)
sl@0
   736
  {
sl@0
   737
    return e.m_source;
sl@0
   738
  }
sl@0
   739
sl@0
   740
  // O(1)
sl@0
   741
  template <typename D, typename VP, typename EP, typename GP, typename A,
sl@0
   742
    typename Dir, typename Vertex>  
sl@0
   743
  typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor
sl@0
   744
  target(const detail::matrix_edge_desc_impl<Dir,Vertex>& e,
sl@0
   745
         const adjacency_matrix<D,VP,EP,GP,A>&)
sl@0
   746
  {
sl@0
   747
    return e.m_target;
sl@0
   748
  }
sl@0
   749
sl@0
   750
  //=========================================================================
sl@0
   751
  // Functions required by the BidirectionalGraph concept 
sl@0
   752
sl@0
   753
  // O(1)
sl@0
   754
  template <typename VP, typename EP, typename GP, typename A>
sl@0
   755
  std::pair<typename adjacency_matrix<directedS,VP,EP,GP,A>::in_edge_iterator,
sl@0
   756
            typename adjacency_matrix<directedS,VP,EP,GP,A>::in_edge_iterator>
sl@0
   757
  in_edges
sl@0
   758
    (typename adjacency_matrix<directedS,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   759
     const adjacency_matrix<directedS,VP,EP,GP,A>& g_)
sl@0
   760
  {
sl@0
   761
    typedef adjacency_matrix<directedS,VP,EP,GP,A> Graph;
sl@0
   762
    Graph& g = const_cast<Graph&>(g_);
sl@0
   763
    typename Graph::MatrixIter f = g.m_matrix.begin() + u;
sl@0
   764
    typename Graph::MatrixIter l = g.m_matrix.end();
sl@0
   765
    typename Graph::unfiltered_in_edge_iter
sl@0
   766
        first(f, l, u, g.m_vertex_set.size())
sl@0
   767
      , last(l, l, u, g.m_vertex_set.size());
sl@0
   768
    detail::does_edge_exist pred;
sl@0
   769
    typedef typename Graph::in_edge_iterator in_edge_iterator;
sl@0
   770
    return std::make_pair(in_edge_iterator(pred, first, last), 
sl@0
   771
                          in_edge_iterator(pred, last, last));
sl@0
   772
  }
sl@0
   773
sl@0
   774
  // O(1)
sl@0
   775
  template <typename VP, typename EP, typename GP, typename A>
sl@0
   776
  std::pair<
sl@0
   777
    typename adjacency_matrix<undirectedS,VP,EP,GP,A>::in_edge_iterator,
sl@0
   778
    typename adjacency_matrix<undirectedS,VP,EP,GP,A>::in_edge_iterator>
sl@0
   779
  in_edges
sl@0
   780
    (typename adjacency_matrix<undirectedS,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   781
     const adjacency_matrix<undirectedS,VP,EP,GP,A>& g_)
sl@0
   782
  {
sl@0
   783
    typedef adjacency_matrix<undirectedS,VP,EP,GP,A> Graph;
sl@0
   784
    Graph& g = const_cast<Graph&>(g_);
sl@0
   785
    typename Graph::vertices_size_type offset = u * (u + 1) / 2;
sl@0
   786
    typename Graph::MatrixIter f = g.m_matrix.begin() + offset;
sl@0
   787
    typename Graph::MatrixIter l = g.m_matrix.end();
sl@0
   788
sl@0
   789
    typename Graph::unfiltered_in_edge_iter
sl@0
   790
        first(f, u, g.m_vertex_set.size())
sl@0
   791
      , last(l, u, g.m_vertex_set.size());
sl@0
   792
    
sl@0
   793
    detail::does_edge_exist pred;
sl@0
   794
    typedef typename Graph::in_edge_iterator in_edge_iterator;
sl@0
   795
    return std::make_pair(in_edge_iterator(pred, first, last), 
sl@0
   796
                          in_edge_iterator(pred, last, last));
sl@0
   797
  }
sl@0
   798
  
sl@0
   799
  // O(N)
sl@0
   800
  template <typename D, typename VP, typename EP, typename GP, typename A>  
sl@0
   801
  typename adjacency_matrix<D,VP,EP,GP,A>::degree_size_type
sl@0
   802
  in_degree(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   803
             const adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   804
  {
sl@0
   805
    typename adjacency_matrix<D,VP,EP,GP,A>::degree_size_type n = 0;
sl@0
   806
    typename adjacency_matrix<D,VP,EP,GP,A>::in_edge_iterator f, l;
sl@0
   807
    for (tie(f, l) = in_edges(u, g); f != l; ++f)
sl@0
   808
      ++n;
sl@0
   809
    return n;
sl@0
   810
  }
sl@0
   811
sl@0
   812
  //=========================================================================
sl@0
   813
  // Functions required by the AdjacencyGraph concept 
sl@0
   814
sl@0
   815
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   816
  std::pair<typename adjacency_matrix<D,VP,EP,GP,A>::adjacency_iterator,
sl@0
   817
            typename adjacency_matrix<D,VP,EP,GP,A>::adjacency_iterator>
sl@0
   818
  adjacent_vertices
sl@0
   819
    (typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   820
     const adjacency_matrix<D,VP,EP,GP,A>& g_)
sl@0
   821
  {
sl@0
   822
      typedef adjacency_matrix<D,VP,EP,GP,A> Graph;
sl@0
   823
      const Graph& cg = static_cast<const Graph&>(g_);
sl@0
   824
      Graph& g = const_cast<Graph&>(cg);
sl@0
   825
      typedef typename Graph::adjacency_iterator adjacency_iterator;
sl@0
   826
      typename Graph::out_edge_iterator first, last;
sl@0
   827
      boost::tie(first, last) = out_edges(u, g);
sl@0
   828
      return std::make_pair(adjacency_iterator(first, &g),
sl@0
   829
                            adjacency_iterator(last, &g));
sl@0
   830
  }
sl@0
   831
sl@0
   832
  //=========================================================================
sl@0
   833
  // Functions required by the VertexListGraph concept 
sl@0
   834
sl@0
   835
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   836
  std::pair<typename adjacency_matrix<D,VP,EP,GP,A>::vertex_iterator,
sl@0
   837
            typename adjacency_matrix<D,VP,EP,GP,A>::vertex_iterator>
sl@0
   838
  vertices(const adjacency_matrix<D,VP,EP,GP,A>& g_) {
sl@0
   839
    typedef adjacency_matrix<D,VP,EP,GP,A> Graph;
sl@0
   840
    Graph& g = const_cast<Graph&>(g_);
sl@0
   841
    return std::make_pair(g.m_vertex_set.begin(), g.m_vertex_set.end());
sl@0
   842
  }
sl@0
   843
sl@0
   844
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   845
  typename adjacency_matrix<D,VP,EP,GP,A>::vertices_size_type
sl@0
   846
  num_vertices(const adjacency_matrix<D,VP,EP,GP,A>& g) {
sl@0
   847
    return g.m_vertex_set.size();
sl@0
   848
  }
sl@0
   849
  
sl@0
   850
  //=========================================================================
sl@0
   851
  // Functions required by the EdgeListGraph concept 
sl@0
   852
  
sl@0
   853
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   854
  std::pair<typename adjacency_matrix<D,VP,EP,GP,A>::edge_iterator,
sl@0
   855
            typename adjacency_matrix<D,VP,EP,GP,A>::edge_iterator>
sl@0
   856
  edges(const adjacency_matrix<D,VP,EP,GP,A>& g_)
sl@0
   857
  {
sl@0
   858
    typedef adjacency_matrix<D,VP,EP,GP,A> Graph;
sl@0
   859
    Graph& g = const_cast<Graph&>(g_);
sl@0
   860
    
sl@0
   861
    typename Graph::unfiltered_edge_iter
sl@0
   862
      first(g.m_matrix.begin(), g.m_matrix.begin(), 
sl@0
   863
                                    g.m_vertex_set.size()),
sl@0
   864
      last(g.m_matrix.end(), g.m_matrix.begin(), 
sl@0
   865
                                    g.m_vertex_set.size());
sl@0
   866
    detail::does_edge_exist pred;
sl@0
   867
    typedef typename Graph::edge_iterator edge_iterator;
sl@0
   868
    return std::make_pair(edge_iterator(pred, first, last),
sl@0
   869
                          edge_iterator(pred, last, last));
sl@0
   870
  }
sl@0
   871
sl@0
   872
  // O(1)
sl@0
   873
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   874
  typename adjacency_matrix<D,VP,EP,GP,A>::edges_size_type
sl@0
   875
  num_edges(const adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   876
  {
sl@0
   877
    return g.m_num_edges;
sl@0
   878
  }
sl@0
   879
  
sl@0
   880
  //=========================================================================
sl@0
   881
  // Functions required by the MutableGraph concept 
sl@0
   882
sl@0
   883
  // O(1)
sl@0
   884
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   885
  std::pair<typename adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor, bool>
sl@0
   886
  add_edge(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   887
           typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor v,
sl@0
   888
           const EP& ep,
sl@0
   889
           adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   890
  {
sl@0
   891
    typedef typename adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor 
sl@0
   892
      edge_descriptor;
sl@0
   893
    if (detail::get_edge_exists(g.get_edge(u,v), 0) == false) {
sl@0
   894
      ++(g.m_num_edges);
sl@0
   895
      detail::set_property(g.get_edge(u,v), ep, 0);
sl@0
   896
      detail::set_edge_exists(g.get_edge(u,v), true, 0);
sl@0
   897
      return std::make_pair
sl@0
   898
        (edge_descriptor(true, u, v, &detail::get_property(g.get_edge(u,v))), 
sl@0
   899
         true);
sl@0
   900
    } else
sl@0
   901
      return std::make_pair
sl@0
   902
        (edge_descriptor(true, u, v, &detail::get_property(g.get_edge(u,v))),
sl@0
   903
         false);
sl@0
   904
  }
sl@0
   905
  // O(1)
sl@0
   906
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   907
  std::pair<typename adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor, bool>
sl@0
   908
  add_edge(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   909
           typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor v,
sl@0
   910
           adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   911
  {
sl@0
   912
    EP ep;
sl@0
   913
    return add_edge(u, v, ep, g);
sl@0
   914
  }
sl@0
   915
sl@0
   916
  // O(1)  
sl@0
   917
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   918
  void
sl@0
   919
  remove_edge(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   920
              typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor v,
sl@0
   921
              adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   922
  {
sl@0
   923
    --(g.m_num_edges);
sl@0
   924
    detail::set_edge_exists(g.get_edge(u,v), false, 0);
sl@0
   925
  }
sl@0
   926
sl@0
   927
  // O(1)
sl@0
   928
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   929
  void
sl@0
   930
  remove_edge(typename adjacency_matrix<D,VP,EP,GP,A>::edge_descriptor e,
sl@0
   931
              adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   932
  {
sl@0
   933
    remove_edge(source(e, g), target(e, g), g);
sl@0
   934
  }
sl@0
   935
sl@0
   936
  
sl@0
   937
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   938
  inline typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor
sl@0
   939
  add_vertex(adjacency_matrix<D,VP,EP,GP,A>& g) {
sl@0
   940
    // UNDER CONSTRUCTION
sl@0
   941
    assert(false);
sl@0
   942
    return *vertices(g).first;
sl@0
   943
  }
sl@0
   944
sl@0
   945
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   946
  inline typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor
sl@0
   947
  add_vertex(const VP& vp, adjacency_matrix<D,VP,EP,GP,A>& g) {
sl@0
   948
    // UNDER CONSTRUCTION
sl@0
   949
    assert(false);
sl@0
   950
    return *vertices(g).first;
sl@0
   951
  }
sl@0
   952
sl@0
   953
  template <typename D, typename VP, typename EP, typename GP, typename A>
sl@0
   954
  inline void
sl@0
   955
  remove_vertex(typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   956
                adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
   957
  {
sl@0
   958
    // UNDER CONSTRUCTION
sl@0
   959
    assert(false);
sl@0
   960
  }
sl@0
   961
sl@0
   962
  // O(V)
sl@0
   963
  template <typename VP, typename EP, typename GP, typename A>
sl@0
   964
  void
sl@0
   965
  clear_vertex
sl@0
   966
    (typename adjacency_matrix<directedS,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   967
     adjacency_matrix<directedS,VP,EP,GP,A>& g)
sl@0
   968
  {
sl@0
   969
    typename adjacency_matrix<directedS,VP,EP,GP,A>::vertex_iterator 
sl@0
   970
      vi, vi_end;
sl@0
   971
    for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
sl@0
   972
      remove_edge(u, *vi, g);
sl@0
   973
    for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
sl@0
   974
      remove_edge(*vi, u, g);
sl@0
   975
  }
sl@0
   976
sl@0
   977
  // O(V)
sl@0
   978
  template <typename VP, typename EP, typename GP, typename A>
sl@0
   979
  void
sl@0
   980
  clear_vertex
sl@0
   981
    (typename adjacency_matrix<undirectedS,VP,EP,GP,A>::vertex_descriptor u,
sl@0
   982
     adjacency_matrix<undirectedS,VP,EP,GP,A>& g)
sl@0
   983
  {
sl@0
   984
    typename adjacency_matrix<undirectedS,VP,EP,GP,A>::vertex_iterator
sl@0
   985
      vi, vi_end;
sl@0
   986
    for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
sl@0
   987
      remove_edge(u, *vi, g);
sl@0
   988
  }
sl@0
   989
sl@0
   990
  //=========================================================================
sl@0
   991
  // Vertex Property Map
sl@0
   992
  
sl@0
   993
  template <typename GraphPtr, typename Vertex, typename T, typename R, 
sl@0
   994
    typename Tag> 
sl@0
   995
  class adj_matrix_vertex_property_map 
sl@0
   996
    : public put_get_helper<R,
sl@0
   997
         adj_matrix_vertex_property_map<GraphPtr, Vertex, T, R, Tag> >
sl@0
   998
  {
sl@0
   999
  public:
sl@0
  1000
    typedef T value_type;
sl@0
  1001
    typedef R reference;
sl@0
  1002
    typedef Vertex key_type;
sl@0
  1003
    typedef boost::lvalue_property_map_tag category;
sl@0
  1004
    adj_matrix_vertex_property_map() { }
sl@0
  1005
    adj_matrix_vertex_property_map(GraphPtr g) : m_g(g) { }
sl@0
  1006
    inline reference operator[](key_type v) const {
sl@0
  1007
      return get_property_value(m_g->m_vertex_properties[v], Tag());
sl@0
  1008
    }
sl@0
  1009
    GraphPtr m_g;
sl@0
  1010
  };
sl@0
  1011
sl@0
  1012
  template <class Property, class Vertex>
sl@0
  1013
  struct adj_matrix_vertex_id_map
sl@0
  1014
    : public boost::put_get_helper<Vertex,
sl@0
  1015
        adj_matrix_vertex_id_map<Property, Vertex> >
sl@0
  1016
  {
sl@0
  1017
    typedef Vertex value_type;
sl@0
  1018
    typedef Vertex reference;
sl@0
  1019
    typedef Vertex key_type;
sl@0
  1020
    typedef boost::readable_property_map_tag category;
sl@0
  1021
     adj_matrix_vertex_id_map() { }
sl@0
  1022
    template <class Graph>
sl@0
  1023
    inline adj_matrix_vertex_id_map(const Graph&) { }
sl@0
  1024
    inline value_type operator[](key_type v) const { return v; }
sl@0
  1025
  };
sl@0
  1026
sl@0
  1027
  namespace detail {
sl@0
  1028
sl@0
  1029
    struct adj_matrix_any_vertex_pa {
sl@0
  1030
      template <class Tag, class Graph, class Property>
sl@0
  1031
      struct bind_ {
sl@0
  1032
        typedef typename property_value<Property,Tag>::type Value;
sl@0
  1033
        typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
sl@0
  1034
        
sl@0
  1035
        typedef adj_matrix_vertex_property_map<Graph*, Vertex, Value, Value&,
sl@0
  1036
          Tag> type;
sl@0
  1037
        typedef adj_matrix_vertex_property_map<const Graph*, Vertex, Value, 
sl@0
  1038
          const Value&, Tag> const_type;
sl@0
  1039
      };
sl@0
  1040
    };
sl@0
  1041
    struct adj_matrix_id_vertex_pa {
sl@0
  1042
      template <class Tag, class Graph, class Property>
sl@0
  1043
      struct bind_ {
sl@0
  1044
        typedef typename Graph::vertex_descriptor Vertex;
sl@0
  1045
        typedef adj_matrix_vertex_id_map<Property, Vertex> type;
sl@0
  1046
        typedef adj_matrix_vertex_id_map<Property, Vertex> const_type;
sl@0
  1047
      };
sl@0
  1048
    };
sl@0
  1049
sl@0
  1050
    template <class Tag>
sl@0
  1051
    struct adj_matrix_choose_vertex_pa_helper {
sl@0
  1052
      typedef adj_matrix_any_vertex_pa type;
sl@0
  1053
    };
sl@0
  1054
    template <>
sl@0
  1055
    struct adj_matrix_choose_vertex_pa_helper<vertex_index_t> {
sl@0
  1056
      typedef adj_matrix_id_vertex_pa type;
sl@0
  1057
    };
sl@0
  1058
sl@0
  1059
    template <class Tag, class Graph, class Property>
sl@0
  1060
    struct adj_matrix_choose_vertex_pa {
sl@0
  1061
      typedef typename adj_matrix_choose_vertex_pa_helper<Tag>::type Helper;
sl@0
  1062
      typedef typename Helper::template bind_<Tag,Graph,Property> Bind;
sl@0
  1063
      typedef typename Bind::type type;
sl@0
  1064
      typedef typename Bind::const_type const_type;
sl@0
  1065
    };
sl@0
  1066
sl@0
  1067
    struct adj_matrix_vertex_property_selector {
sl@0
  1068
      template <class Graph, class Property, class Tag>
sl@0
  1069
      struct bind_ {
sl@0
  1070
        typedef adj_matrix_choose_vertex_pa<Tag,Graph,Property> Choice;
sl@0
  1071
        typedef typename Choice::type type;
sl@0
  1072
        typedef typename Choice::const_type const_type;
sl@0
  1073
      };
sl@0
  1074
    };
sl@0
  1075
sl@0
  1076
  } // namespace detail
sl@0
  1077
sl@0
  1078
  template <>
sl@0
  1079
  struct vertex_property_selector<adjacency_matrix_class_tag> {
sl@0
  1080
    typedef detail::adj_matrix_vertex_property_selector type;
sl@0
  1081
  };
sl@0
  1082
  
sl@0
  1083
  //=========================================================================
sl@0
  1084
  // Edge Property Map
sl@0
  1085
sl@0
  1086
sl@0
  1087
  template <typename Directed, typename Property, typename Vertex, 
sl@0
  1088
    typename T, typename R, typename Tag> 
sl@0
  1089
  class adj_matrix_edge_property_map 
sl@0
  1090
    : public put_get_helper<R,
sl@0
  1091
         adj_matrix_edge_property_map<Directed, Property, Vertex, T, R, Tag> >
sl@0
  1092
  {
sl@0
  1093
  public:
sl@0
  1094
    typedef T value_type;
sl@0
  1095
    typedef R reference;
sl@0
  1096
    typedef detail::matrix_edge_desc_impl<Directed, Vertex> key_type;
sl@0
  1097
    typedef boost::lvalue_property_map_tag category;
sl@0
  1098
    inline reference operator[](key_type e) const {
sl@0
  1099
      Property& p = *(Property*)e.get_property();
sl@0
  1100
      return get_property_value(p, Tag());
sl@0
  1101
    }
sl@0
  1102
  };
sl@0
  1103
  struct adj_matrix_edge_property_selector {
sl@0
  1104
    template <class Graph, class Property, class Tag>
sl@0
  1105
    struct bind_ {
sl@0
  1106
      typedef typename property_value<Property,Tag>::type T;
sl@0
  1107
      typedef typename Graph::vertex_descriptor Vertex;
sl@0
  1108
      typedef adj_matrix_edge_property_map<typename Graph::directed_category,
sl@0
  1109
        Property, Vertex, T, T&, Tag> type;
sl@0
  1110
      typedef adj_matrix_edge_property_map<typename Graph::directed_category,
sl@0
  1111
        Property, Vertex, T, const T&, Tag> const_type;
sl@0
  1112
    };
sl@0
  1113
  };
sl@0
  1114
  template <>
sl@0
  1115
  struct edge_property_selector<adjacency_matrix_class_tag> {
sl@0
  1116
    typedef adj_matrix_edge_property_selector type;
sl@0
  1117
  };
sl@0
  1118
sl@0
  1119
  //=========================================================================
sl@0
  1120
  // Functions required by PropertyGraph
sl@0
  1121
sl@0
  1122
  namespace detail {
sl@0
  1123
sl@0
  1124
    template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1125
              typename GP, typename A>
sl@0
  1126
    typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1127
      Property>::type
sl@0
  1128
    get_dispatch(adjacency_matrix<D,VP,EP,GP,A>& g, Property,
sl@0
  1129
                 vertex_property_tag)
sl@0
  1130
    {
sl@0
  1131
      typedef adjacency_matrix<D,VP,EP,GP,A> Graph;
sl@0
  1132
      typedef typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1133
        Property>::type PA;
sl@0
  1134
      return PA(&g);
sl@0
  1135
    }
sl@0
  1136
    template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1137
              typename GP, typename A>
sl@0
  1138
    typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1139
      Property>::type
sl@0
  1140
    get_dispatch(adjacency_matrix<D,VP,EP,GP,A>&, Property,
sl@0
  1141
                 edge_property_tag)
sl@0
  1142
    {
sl@0
  1143
      typedef typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1144
        Property>::type PA;
sl@0
  1145
      return PA();
sl@0
  1146
    }
sl@0
  1147
    template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1148
              typename GP, typename A>
sl@0
  1149
    typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1150
      Property>::const_type
sl@0
  1151
    get_dispatch(const adjacency_matrix<D,VP,EP,GP,A>& g, Property,
sl@0
  1152
                 vertex_property_tag)
sl@0
  1153
    {
sl@0
  1154
      typedef adjacency_matrix<D,VP,EP,GP,A> Graph;
sl@0
  1155
      typedef typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1156
        Property>::const_type PA;
sl@0
  1157
      return PA(&g);
sl@0
  1158
    }
sl@0
  1159
    template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1160
              typename GP, typename A>
sl@0
  1161
    typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1162
      Property>::const_type
sl@0
  1163
    get_dispatch(const adjacency_matrix<D,VP,EP,GP,A>&, Property,
sl@0
  1164
                 edge_property_tag)
sl@0
  1165
    {
sl@0
  1166
      typedef typename boost::property_map<adjacency_matrix<D,VP,EP,GP,A>, 
sl@0
  1167
        Property>::const_type PA;
sl@0
  1168
      return PA();
sl@0
  1169
    }
sl@0
  1170
sl@0
  1171
  } // namespace detail
sl@0
  1172
sl@0
  1173
  template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1174
            typename GP, typename A>
sl@0
  1175
  inline
sl@0
  1176
  typename property_map<adjacency_matrix<D,VP,EP,GP,A>, Property>::type
sl@0
  1177
  get(Property p, adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
  1178
  {
sl@0
  1179
    typedef typename property_kind<Property>::type Kind;
sl@0
  1180
    return detail::get_dispatch(g, p, Kind());
sl@0
  1181
  }
sl@0
  1182
sl@0
  1183
  template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1184
            typename GP, typename A>
sl@0
  1185
  inline
sl@0
  1186
  typename property_map<adjacency_matrix<D,VP,EP,GP,A>, Property>::const_type
sl@0
  1187
  get(Property p, const adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
  1188
  {
sl@0
  1189
    typedef typename property_kind<Property>::type Kind;
sl@0
  1190
    return detail::get_dispatch(g, p, Kind());
sl@0
  1191
  }
sl@0
  1192
sl@0
  1193
  template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1194
            typename GP, typename A, typename Key>
sl@0
  1195
  inline
sl@0
  1196
  typename property_traits<
sl@0
  1197
    typename property_map<adjacency_matrix<D,VP,EP,GP,A>, Property>::const_type
sl@0
  1198
  >::value_type
sl@0
  1199
  get(Property p, const adjacency_matrix<D,VP,EP,GP,A>& g,
sl@0
  1200
      const Key& key)
sl@0
  1201
  {
sl@0
  1202
    return get(get(p, g), key);
sl@0
  1203
  }
sl@0
  1204
sl@0
  1205
  template <typename Property, typename D, typename VP, typename EP, 
sl@0
  1206
            typename GP, typename A, typename Key, typename Value>
sl@0
  1207
  inline void
sl@0
  1208
  put(Property p, adjacency_matrix<D,VP,EP,GP,A>& g,
sl@0
  1209
      const Key& key, const Value& value)
sl@0
  1210
  {
sl@0
  1211
    typedef adjacency_matrix<D,VP,EP,GP,A> Graph;
sl@0
  1212
    typedef typename boost::property_map<Graph, Property>::type Map;
sl@0
  1213
    Map pmap = get(p, g);
sl@0
  1214
    put(pmap, key, value);
sl@0
  1215
  }
sl@0
  1216
  
sl@0
  1217
  //=========================================================================
sl@0
  1218
  // Other Functions
sl@0
  1219
sl@0
  1220
  template <typename D, typename VP, typename EP, typename GP, typename A>  
sl@0
  1221
  typename adjacency_matrix<D,VP,EP,GP,A>::vertex_descriptor
sl@0
  1222
  vertex(typename adjacency_matrix<D,VP,EP,GP,A>::vertices_size_type n,
sl@0
  1223
         const adjacency_matrix<D,VP,EP,GP,A>& g)
sl@0
  1224
  {
sl@0
  1225
    return n;
sl@0
  1226
  }
sl@0
  1227
sl@0
  1228
  // Support for bundled properties
sl@0
  1229
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
sl@0
  1230
  template <typename Directed, typename VertexProperty, typename EdgeProperty, typename GraphProperty,
sl@0
  1231
            typename Allocator, typename T, typename Bundle>
sl@0
  1232
  inline
sl@0
  1233
  typename property_map<adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator>,
sl@0
  1234
                        T Bundle::*>::type
sl@0
  1235
  get(T Bundle::* p, adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator>& g)
sl@0
  1236
  {
sl@0
  1237
    typedef typename property_map<adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator>,
sl@0
  1238
                                  T Bundle::*>::type
sl@0
  1239
      result_type;
sl@0
  1240
    return result_type(&g, p);
sl@0
  1241
  }
sl@0
  1242
sl@0
  1243
  template <typename Directed, typename VertexProperty, typename EdgeProperty, typename GraphProperty,
sl@0
  1244
            typename Allocator, typename T, typename Bundle>
sl@0
  1245
  inline
sl@0
  1246
  typename property_map<adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator>,
sl@0
  1247
                        T Bundle::*>::const_type
sl@0
  1248
  get(T Bundle::* p, adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator> const & g)
sl@0
  1249
  {
sl@0
  1250
    typedef typename property_map<adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator>,
sl@0
  1251
                                  T Bundle::*>::const_type
sl@0
  1252
      result_type;
sl@0
  1253
    return result_type(&g, p);
sl@0
  1254
  }
sl@0
  1255
    
sl@0
  1256
  template <typename Directed, typename VertexProperty, typename EdgeProperty, typename GraphProperty,
sl@0
  1257
            typename Allocator, typename T, typename Bundle, typename Key>
sl@0
  1258
  inline T
sl@0
  1259
  get(T Bundle::* p, adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator> const & g,
sl@0
  1260
      const Key& key)
sl@0
  1261
  {
sl@0
  1262
    return get(get(p, g), key);
sl@0
  1263
  }
sl@0
  1264
sl@0
  1265
  template <typename Directed, typename VertexProperty, typename EdgeProperty, typename GraphProperty,
sl@0
  1266
            typename Allocator, typename T, typename Bundle, typename Key>
sl@0
  1267
  inline void
sl@0
  1268
  put(T Bundle::* p, adjacency_matrix<Directed, VertexProperty, EdgeProperty, GraphProperty, Allocator>& g,
sl@0
  1269
      const Key& key, const T& value)
sl@0
  1270
  {
sl@0
  1271
    put(get(p, g), key, value);
sl@0
  1272
  }
sl@0
  1273
sl@0
  1274
#endif
sl@0
  1275
sl@0
  1276
} // namespace boost
sl@0
  1277
sl@0
  1278
#endif // BOOST_ADJACENCY_MATRIX_HPP