matrix_as_graph.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <cstddef>
  15. #include <iterator>
  16. #include <boost/config.hpp>
  17. #include <boost/operators.hpp>
  18. #include <boost/pending/detail/int_iterator.hpp>
  19. #include <boost/graph/graph_traits.hpp>
  20. namespace boost
  21. {
  22. template < class Iter, class Vertex > class matrix_adj_iterator;
  23. template < class Iter, class Vertex > class matrix_incidence_iterator;
  24. }
  25. #define BOOST_GRAPH_ADAPT_MATRIX_TO_GRAPH(Matrix) \
  26. namespace boost \
  27. { \
  28. template <> struct graph_traits< Matrix > \
  29. { \
  30. typedef Matrix::OneD::const_iterator Iter; \
  31. typedef Matrix::size_type V; \
  32. typedef V vertex_descriptor; \
  33. typedef Iter E; \
  34. typedef E edge_descriptor; \
  35. typedef boost::matrix_incidence_iterator< Iter, V > \
  36. out_edge_iterator; \
  37. typedef boost::matrix_adj_iterator< Iter, V > adjacency_iterator; \
  38. typedef Matrix::size_type size_type; \
  39. typedef boost::int_iterator< size_type > vertex_iterator; \
  40. \
  41. friend std::pair< vertex_iterator, vertex_iterator > vertices( \
  42. const Matrix& g) \
  43. { \
  44. typedef vertex_iterator VIter; \
  45. return std::make_pair(VIter(0), VIter(g.nrows())); \
  46. } \
  47. \
  48. friend std::pair< out_edge_iterator, out_edge_iterator > \
  49. out_edges(V v, const Matrix& g) \
  50. { \
  51. typedef out_edge_iterator IncIter; \
  52. return std::make_pair( \
  53. IncIter(g[v].begin()), IncIter(g[v].end())); \
  54. } \
  55. friend std::pair< adjacency_iterator, adjacency_iterator > \
  56. adjacent_vertices(V v, const Matrix& g) \
  57. { \
  58. typedef adjacency_iterator AdjIter; \
  59. return std::make_pair( \
  60. AdjIter(g[v].begin()), AdjIter(g[v].end())); \
  61. } \
  62. friend vertex_descriptor source(E e, const Matrix& g) \
  63. { \
  64. return e.row(); \
  65. } \
  66. friend vertex_descriptor target(E e, const Matrix& g) \
  67. { \
  68. return e.column(); \
  69. } \
  70. friend size_type num_vertices(const Matrix& g) \
  71. { \
  72. return g.nrows(); \
  73. } \
  74. friend size_type num_edges(const Matrix& g) { return g.nnz(); } \
  75. friend size_type out_degree(V i, const Matrix& g) \
  76. { \
  77. return g[i].nnz(); \
  78. } \
  79. }; \
  80. }
  81. namespace boost
  82. {
  83. template < class Iter, class Vertex > class matrix_adj_iterator
  84. {
  85. typedef matrix_adj_iterator self;
  86. public:
  87. typedef std::input_iterator_tag iterator_category;
  88. typedef Vertex value_type;
  89. typedef std::ptrdiff_t difference_type;
  90. typedef Vertex* pointer;
  91. typedef Vertex& reference;
  92. matrix_adj_iterator() {}
  93. matrix_adj_iterator(Iter i) : _iter(i) {}
  94. matrix_adj_iterator(const self& x) : _iter(x._iter) {}
  95. self& operator=(const self& x)
  96. {
  97. _iter = x._iter;
  98. return *this;
  99. }
  100. Vertex operator*() { return _iter.column(); }
  101. self& operator++()
  102. {
  103. ++_iter;
  104. return *this;
  105. }
  106. self operator++(int)
  107. {
  108. self t = *this;
  109. ++_iter;
  110. return t;
  111. }
  112. bool operator==(const self& x) const { return _iter == x._iter; }
  113. bool operator!=(const self& x) const { return _iter != x._iter; }
  114. protected:
  115. Iter _iter;
  116. };
  117. template < class Iter, class Vertex > class matrix_incidence_iterator
  118. {
  119. typedef matrix_incidence_iterator self;
  120. public:
  121. typedef std::input_iterator_tag iterator_category;
  122. typedef Iter value_type;
  123. typedef std::ptrdiff_t difference_type;
  124. typedef Iter* pointer;
  125. typedef Iter& reference;
  126. matrix_incidence_iterator() {}
  127. matrix_incidence_iterator(Iter i) : _iter(i) {}
  128. matrix_incidence_iterator(const self& x) : _iter(x._iter) {}
  129. self& operator=(const self& x)
  130. {
  131. _iter = x._iter;
  132. return *this;
  133. }
  134. Iter operator*() { return _iter; }
  135. self& operator++()
  136. {
  137. ++_iter;
  138. return *this;
  139. }
  140. self operator++(int)
  141. {
  142. self t = *this;
  143. ++_iter;
  144. return t;
  145. }
  146. bool operator==(const self& x) const { return _iter == x._iter; }
  147. bool operator!=(const self& x) const { return _iter != x._iter; }
  148. protected:
  149. Iter _iter;
  150. };
  151. } /* namespace boost */
  152. #endif /* BOOST_GRAPH_MATRIX2GRAPH_HPP*/