vector_as_graph.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //=======================================================================
  2. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  3. // Copyright 2006 The Trustees of Indiana University.
  4. // Copyright (C) 2001 Vladimir Prus <ghost@cs.msu.su>
  5. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Douglas Gregor
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //=======================================================================
  11. // The mutating functions (add_edge, etc.) were added by Vladimir Prus.
  12. #ifndef BOOST_VECTOR_AS_GRAPH_HPP
  13. #define BOOST_VECTOR_AS_GRAPH_HPP
  14. #include <cassert>
  15. #include <utility>
  16. #include <vector>
  17. #include <cstddef>
  18. #include <iterator>
  19. #include <boost/iterator/counting_iterator.hpp>
  20. #include <boost/range/irange.hpp>
  21. #include <boost/graph/graph_traits.hpp>
  22. #include <boost/property_map/property_map.hpp>
  23. #include <boost/graph/properties.hpp>
  24. #include <algorithm>
  25. /*
  26. This module implements the VertexListGraph concept using a
  27. std::vector as the "back-bone" of the graph (the vector *is* the
  28. graph object). The edge-lists type of the graph is templated, so the
  29. user can choose any STL container, so long as the value_type of the
  30. container is convertible to the size_type of the vector. For now any
  31. graph properties must be stored seperately.
  32. This module requires the C++ compiler to support partial
  33. specialization for the graph_traits class, so this is not portable
  34. to VC++.
  35. */
  36. namespace boost
  37. {
  38. namespace detail
  39. {
  40. template < class EdgeList > struct val_out_edge_ret;
  41. template < class EdgeList > struct val_out_edge_iter;
  42. template < class EdgeList > struct val_edge;
  43. }
  44. }
  45. namespace boost
  46. {
  47. struct vector_as_graph_traversal_tag : public vertex_list_graph_tag,
  48. public adjacency_graph_tag,
  49. public incidence_graph_tag
  50. {
  51. };
  52. template < class EdgeList > struct graph_traits< std::vector< EdgeList > >
  53. {
  54. typedef typename EdgeList::value_type V;
  55. typedef V vertex_descriptor;
  56. typedef typename detail::val_edge< EdgeList >::type edge_descriptor;
  57. typedef typename EdgeList::const_iterator adjacency_iterator;
  58. typedef
  59. typename detail::val_out_edge_iter< EdgeList >::type out_edge_iterator;
  60. typedef void in_edge_iterator;
  61. typedef void edge_iterator;
  62. typedef counting_iterator< V > vertex_iterator;
  63. typedef directed_tag directed_category;
  64. typedef allow_parallel_edge_tag edge_parallel_category;
  65. typedef vector_as_graph_traversal_tag traversal_category;
  66. typedef typename std::vector< EdgeList >::size_type vertices_size_type;
  67. typedef void edges_size_type;
  68. typedef typename EdgeList::size_type degree_size_type;
  69. static V null_vertex() { return V(-1); }
  70. };
  71. template < class EdgeList > struct edge_property_type< std::vector< EdgeList > >
  72. {
  73. typedef void type;
  74. };
  75. template < class EdgeList >
  76. struct vertex_property_type< std::vector< EdgeList > >
  77. {
  78. typedef void type;
  79. };
  80. template < class EdgeList >
  81. struct graph_property_type< std::vector< EdgeList > >
  82. {
  83. typedef void type;
  84. };
  85. }
  86. namespace boost
  87. {
  88. namespace detail
  89. {
  90. // "val" is short for Vector Adjacency List
  91. template < class EdgeList > struct val_edge
  92. {
  93. typedef typename EdgeList::value_type V;
  94. typedef std::pair< V, V > type;
  95. };
  96. // need rewrite this using boost::iterator_adaptor
  97. template < class V, class Iter > class val_out_edge_iterator
  98. {
  99. typedef val_out_edge_iterator self;
  100. typedef std::pair< V, V > Edge;
  101. public:
  102. typedef std::input_iterator_tag iterator_category;
  103. typedef std::pair< V, V > value_type;
  104. typedef std::ptrdiff_t difference_type;
  105. typedef std::pair< V, V >* pointer;
  106. typedef const std::pair< V, V > reference;
  107. val_out_edge_iterator() {}
  108. val_out_edge_iterator(V s, Iter i) : _source(s), _iter(i) {}
  109. Edge operator*() const { return Edge(_source, *_iter); }
  110. self& operator++()
  111. {
  112. ++_iter;
  113. return *this;
  114. }
  115. self operator++(int)
  116. {
  117. self t = *this;
  118. ++_iter;
  119. return t;
  120. }
  121. bool operator==(const self& x) const { return _iter == x._iter; }
  122. bool operator!=(const self& x) const { return _iter != x._iter; }
  123. protected:
  124. V _source;
  125. Iter _iter;
  126. };
  127. template < class EdgeList > struct val_out_edge_iter
  128. {
  129. typedef typename EdgeList::value_type V;
  130. typedef typename EdgeList::const_iterator Iter;
  131. typedef val_out_edge_iterator< V, Iter > type;
  132. };
  133. template < class EdgeList > struct val_out_edge_ret
  134. {
  135. typedef typename val_out_edge_iter< EdgeList >::type IncIter;
  136. typedef std::pair< IncIter, IncIter > type;
  137. };
  138. } // namesapce detail
  139. template < class EdgeList, class Alloc >
  140. typename detail::val_out_edge_ret< EdgeList >::type out_edges(
  141. typename EdgeList::value_type v, const std::vector< EdgeList, Alloc >& g)
  142. {
  143. typedef typename detail::val_out_edge_iter< EdgeList >::type Iter;
  144. typedef typename detail::val_out_edge_ret< EdgeList >::type return_type;
  145. return return_type(Iter(v, g[v].begin()), Iter(v, g[v].end()));
  146. }
  147. template < class EdgeList, class Alloc >
  148. typename EdgeList::size_type out_degree(
  149. typename EdgeList::value_type v, const std::vector< EdgeList, Alloc >& g)
  150. {
  151. return g[v].size();
  152. }
  153. template < class EdgeList, class Alloc >
  154. std::pair< typename EdgeList::const_iterator,
  155. typename EdgeList::const_iterator >
  156. adjacent_vertices(
  157. typename EdgeList::value_type v, const std::vector< EdgeList, Alloc >& g)
  158. {
  159. return std::make_pair(g[v].begin(), g[v].end());
  160. }
  161. // source() and target() already provided for pairs in graph_traits.hpp
  162. template < class EdgeList, class Alloc >
  163. std::pair< boost::counting_iterator< typename EdgeList::value_type >,
  164. boost::counting_iterator< typename EdgeList::value_type > >
  165. vertices(const std::vector< EdgeList, Alloc >& v)
  166. {
  167. typedef boost::counting_iterator< typename EdgeList::value_type > Iter;
  168. return std::make_pair(Iter(0), Iter(v.size()));
  169. }
  170. template < class EdgeList, class Alloc >
  171. typename std::vector< EdgeList, Alloc >::size_type num_vertices(
  172. const std::vector< EdgeList, Alloc >& v)
  173. {
  174. return v.size();
  175. }
  176. template < class EdgeList, class Allocator >
  177. typename std::pair< typename detail::val_edge< EdgeList >::type, bool >
  178. add_edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
  179. std::vector< EdgeList, Allocator >& g)
  180. {
  181. typedef typename detail::val_edge< EdgeList >::type edge_type;
  182. g[u].insert(g[u].end(), v);
  183. return std::make_pair(edge_type(u, v), true);
  184. }
  185. template < class EdgeList, class Allocator >
  186. typename std::pair< typename detail::val_edge< EdgeList >::type, bool > edge(
  187. typename EdgeList::value_type u, typename EdgeList::value_type v,
  188. std::vector< EdgeList, Allocator >& g)
  189. {
  190. typedef typename detail::val_edge< EdgeList >::type edge_type;
  191. typename EdgeList::iterator i = g[u].begin(), end = g[u].end();
  192. for (; i != end; ++i)
  193. if (*i == v)
  194. return std::make_pair(edge_type(u, v), true);
  195. return std::make_pair(edge_type(), false);
  196. }
  197. template < class EdgeList, class Allocator >
  198. void remove_edge(typename EdgeList::value_type u,
  199. typename EdgeList::value_type v, std::vector< EdgeList, Allocator >& g)
  200. {
  201. typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
  202. if (i != g[u].end())
  203. g[u].erase(i, g[u].end());
  204. }
  205. template < class EdgeList, class Allocator >
  206. void remove_edge(typename detail::val_edge< EdgeList >::type e,
  207. std::vector< EdgeList, Allocator >& g)
  208. {
  209. typename EdgeList::value_type u, v;
  210. u = e.first;
  211. v = e.second;
  212. // FIXME: edge type does not fully specify the edge to be deleted
  213. typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
  214. if (i != g[u].end())
  215. g[u].erase(i, g[u].end());
  216. }
  217. template < class EdgeList, class Allocator, class Predicate >
  218. void remove_edge_if(Predicate p, std::vector< EdgeList, Allocator >& g)
  219. {
  220. for (std::size_t u = 0; u < g.size(); ++u)
  221. {
  222. // Oops! gcc gets internal compiler error on compose_.......
  223. typedef typename EdgeList::iterator iterator;
  224. iterator b = g[u].begin(), e = g[u].end();
  225. if (!g[u].empty())
  226. {
  227. for (; b != e;)
  228. {
  229. if (p(std::make_pair(u, *b)))
  230. {
  231. --e;
  232. if (b == e)
  233. break;
  234. else
  235. iter_swap(b, e);
  236. }
  237. else
  238. {
  239. ++b;
  240. }
  241. }
  242. }
  243. if (e != g[u].end())
  244. g[u].erase(e, g[u].end());
  245. }
  246. }
  247. template < class EdgeList, class Allocator >
  248. typename EdgeList::value_type add_vertex(std::vector< EdgeList, Allocator >& g)
  249. {
  250. g.resize(g.size() + 1);
  251. return g.size() - 1;
  252. }
  253. template < class EdgeList, class Allocator >
  254. void clear_vertex(
  255. typename EdgeList::value_type u, std::vector< EdgeList, Allocator >& g)
  256. {
  257. g[u].clear();
  258. for (std::size_t i = 0; i < g.size(); ++i)
  259. remove_edge(i, u, g);
  260. }
  261. template < class EdgeList, class Allocator >
  262. void remove_vertex(
  263. typename EdgeList::value_type u, std::vector< EdgeList, Allocator >& g)
  264. {
  265. typedef typename EdgeList::iterator iterator;
  266. clear_vertex(u, g);
  267. g.erase(g.begin() + u);
  268. for (std::size_t i = 0; i < g.size(); ++i)
  269. for (iterator it = g[i].begin(); it != g[i].end(); ++it)
  270. // after clear_vertex *it is never equal to u
  271. if (*it > u)
  272. --*it;
  273. }
  274. template < typename EdgeList, typename Allocator >
  275. struct property_map< std::vector< EdgeList, Allocator >, vertex_index_t >
  276. {
  277. typedef identity_property_map type;
  278. typedef type const_type;
  279. };
  280. template < typename EdgeList, typename Allocator >
  281. identity_property_map get(
  282. vertex_index_t, const std::vector< EdgeList, Allocator >&)
  283. {
  284. return identity_property_map();
  285. }
  286. template < typename EdgeList, typename Allocator >
  287. identity_property_map get(vertex_index_t, std::vector< EdgeList, Allocator >&)
  288. {
  289. return identity_property_map();
  290. }
  291. } // namespace boost
  292. #endif // BOOST_VECTOR_AS_GRAPH_HPP