matrix_as_graph.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. #ifndef BOOST_GRAPH_MATRIX2GRAPH_HPP
  12. #define BOOST_GRAPH_MATRIX2GRAPH_HPP
  13. #include <utility>
  14. #include <boost/config.hpp>
  15. #include <boost/operators.hpp>
  16. #include <boost/pending/detail/int_iterator.hpp>
  17. #include <boost/graph/graph_traits.hpp>
  18. namespace boost {
  19. template <class Iter, class Vertex>
  20. class matrix_adj_iterator;
  21. template <class Iter, class Vertex>
  22. class matrix_incidence_iterator;
  23. }
  24. #define BOOST_GRAPH_ADAPT_MATRIX_TO_GRAPH(Matrix) \
  25. namespace boost { \
  26. template <> \
  27. struct graph_traits< Matrix > { \
  28. typedef Matrix::OneD::const_iterator Iter; \
  29. typedef Matrix::size_type V; \
  30. typedef V vertex_descriptor; \
  31. typedef Iter E; \
  32. typedef E edge_descriptor; \
  33. typedef boost::matrix_incidence_iterator<Iter, V> out_edge_iterator; \
  34. typedef boost::matrix_adj_iterator<Iter, V> adjacency_iterator; \
  35. typedef Matrix::size_type size_type; \
  36. typedef boost::int_iterator<size_type> vertex_iterator; \
  37. \
  38. friend std::pair<vertex_iterator, vertex_iterator> \
  39. vertices(const Matrix& g) { \
  40. typedef vertex_iterator VIter; \
  41. return std::make_pair(VIter(0), VIter(g.nrows())); \
  42. } \
  43. \
  44. friend std::pair<out_edge_iterator, out_edge_iterator> \
  45. out_edges(V v, const Matrix& g) { \
  46. typedef out_edge_iterator IncIter; \
  47. return std::make_pair(IncIter(g[v].begin()), \
  48. IncIter(g[v].end())); \
  49. } \
  50. friend std::pair<adjacency_iterator, adjacency_iterator> \
  51. adjacent_vertices(V v, const Matrix& g) { \
  52. typedef adjacency_iterator AdjIter; \
  53. return std::make_pair(AdjIter(g[v].begin()), \
  54. AdjIter(g[v].end())); \
  55. } \
  56. friend vertex_descriptor \
  57. source(E e, const Matrix& g) { \
  58. return e.row(); \
  59. } \
  60. friend vertex_descriptor \
  61. target(E e, const Matrix& g) { \
  62. return e.column(); \
  63. } \
  64. friend size_type \
  65. num_vertices(const Matrix& g) { \
  66. return g.nrows(); \
  67. } \
  68. friend size_type \
  69. num_edges(const Matrix& g) { \
  70. return g.nnz(); \
  71. } \
  72. friend size_type \
  73. out_degree(V i, const Matrix& g) { \
  74. return g[i].nnz(); \
  75. } \
  76. }; \
  77. }
  78. namespace boost {
  79. template <class Iter, class Vertex>
  80. class matrix_adj_iterator
  81. : public std::iterator<std::input_iterator_tag, Vertex >
  82. {
  83. typedef matrix_adj_iterator self;
  84. public:
  85. matrix_adj_iterator() { }
  86. matrix_adj_iterator(Iter i) : _iter(i) { }
  87. matrix_adj_iterator(const self& x) : _iter(x._iter) { }
  88. self& operator=(const self& x) { _iter = x._iter; return *this; }
  89. Vertex operator*() { return _iter.column(); }
  90. self& operator++() { ++_iter; return *this; }
  91. self operator++(int) { self t = *this; ++_iter; return t; }
  92. bool operator==(const self& x) const { return _iter == x._iter; }
  93. bool operator!=(const self& x) const { return _iter != x._iter; }
  94. protected:
  95. Iter _iter;
  96. };
  97. template <class Iter, class Vertex>
  98. class matrix_incidence_iterator
  99. : public std::iterator<std::input_iterator_tag, Iter >
  100. {
  101. typedef matrix_incidence_iterator self;
  102. public:
  103. matrix_incidence_iterator() { }
  104. matrix_incidence_iterator(Iter i) : _iter(i) { }
  105. matrix_incidence_iterator(const self& x) : _iter(x._iter) { }
  106. self& operator=(const self& x) { _iter = x._iter; return *this; }
  107. Iter operator*() { return _iter; }
  108. self& operator++() { ++_iter; return *this; }
  109. self operator++(int) { self t = *this; ++_iter; return t; }
  110. bool operator==(const self& x) const { return _iter == x._iter; }
  111. bool operator!=(const self& x) const { return _iter != x._iter; }
  112. protected:
  113. Iter _iter;
  114. };
  115. } /* namespace boost */
  116. #endif /* BOOST_GRAPH_MATRIX2GRAPH_HPP*/