vector_as_graph.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 <boost/iterator.hpp>
  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. #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  37. #error The vector-as-graph module requires a compiler that supports partial specialization
  38. #endif
  39. namespace boost {
  40. namespace detail {
  41. template <class EdgeList> struct val_out_edge_ret;
  42. template <class EdgeList> struct val_out_edge_iter;
  43. template <class EdgeList> struct val_edge;
  44. }
  45. }
  46. #if !defined BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  47. namespace boost {
  48. struct vector_as_graph_traversal_tag
  49. : public vertex_list_graph_tag,
  50. public adjacency_graph_tag,
  51. public incidence_graph_tag { };
  52. template <class EdgeList>
  53. struct graph_traits< std::vector<EdgeList> >
  54. {
  55. typedef typename EdgeList::value_type V;
  56. typedef V vertex_descriptor;
  57. typedef typename detail::val_edge<EdgeList>::type edge_descriptor;
  58. typedef typename EdgeList::const_iterator adjacency_iterator;
  59. typedef typename detail::val_out_edge_iter<EdgeList>::type
  60. out_edge_iterator;
  61. typedef void in_edge_iterator;
  62. typedef void edge_iterator;
  63. typedef counting_iterator<V> vertex_iterator;
  64. typedef directed_tag directed_category;
  65. typedef allow_parallel_edge_tag edge_parallel_category;
  66. typedef vector_as_graph_traversal_tag traversal_category;
  67. typedef typename std::vector<EdgeList>::size_type vertices_size_type;
  68. typedef void edges_size_type;
  69. typedef typename EdgeList::size_type degree_size_type;
  70. static V null_vertex() {return V(-1);}
  71. };
  72. template <class EdgeList>
  73. struct edge_property_type< std::vector<EdgeList> >
  74. {
  75. typedef void type;
  76. };
  77. template <class EdgeList>
  78. struct vertex_property_type< std::vector<EdgeList> >
  79. {
  80. typedef void type;
  81. };
  82. template <class EdgeList>
  83. struct graph_property_type< std::vector<EdgeList> >
  84. {
  85. typedef void type;
  86. };
  87. }
  88. #endif
  89. namespace boost {
  90. namespace detail {
  91. // "val" is short for Vector Adjacency List
  92. template <class EdgeList>
  93. struct val_edge
  94. {
  95. typedef typename EdgeList::value_type V;
  96. typedef std::pair<V,V> type;
  97. };
  98. // need rewrite this using boost::iterator_adaptor
  99. template <class V, class Iter>
  100. class val_out_edge_iterator
  101. : public boost::iterator<std::input_iterator_tag, std::pair<V,V>,
  102. std::ptrdiff_t, std::pair<V,V>*, const std::pair<V,V> >
  103. {
  104. typedef val_out_edge_iterator self;
  105. typedef std::pair<V,V> Edge;
  106. public:
  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++() { ++_iter; return *this; }
  111. self operator++(int) { self t = *this; ++_iter; return t; }
  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. V _source;
  116. Iter _iter;
  117. };
  118. template <class EdgeList>
  119. struct val_out_edge_iter
  120. {
  121. typedef typename EdgeList::value_type V;
  122. typedef typename EdgeList::const_iterator Iter;
  123. typedef val_out_edge_iterator<V,Iter> type;
  124. };
  125. template <class EdgeList>
  126. struct val_out_edge_ret
  127. {
  128. typedef typename val_out_edge_iter<EdgeList>::type IncIter;
  129. typedef std::pair<IncIter,IncIter> type;
  130. };
  131. } // namesapce detail
  132. template <class EdgeList, class Alloc>
  133. typename detail::val_out_edge_ret<EdgeList>::type
  134. out_edges(typename EdgeList::value_type v,
  135. const std::vector<EdgeList, Alloc>& g)
  136. {
  137. typedef typename detail::val_out_edge_iter<EdgeList>::type Iter;
  138. typedef typename detail::val_out_edge_ret<EdgeList>::type return_type;
  139. return return_type(Iter(v, g[v].begin()), Iter(v, g[v].end()));
  140. }
  141. template <class EdgeList, class Alloc>
  142. typename EdgeList::size_type
  143. out_degree(typename EdgeList::value_type v,
  144. const std::vector<EdgeList, Alloc>& g)
  145. {
  146. return g[v].size();
  147. }
  148. template <class EdgeList, class Alloc>
  149. std::pair<typename EdgeList::const_iterator,
  150. typename EdgeList::const_iterator>
  151. adjacent_vertices(typename EdgeList::value_type v,
  152. const std::vector<EdgeList, Alloc>& g)
  153. {
  154. return std::make_pair(g[v].begin(), g[v].end());
  155. }
  156. // source() and target() already provided for pairs in graph_traits.hpp
  157. template <class EdgeList, class Alloc>
  158. std::pair<boost::counting_iterator<typename EdgeList::value_type>,
  159. boost::counting_iterator<typename EdgeList::value_type> >
  160. vertices(const std::vector<EdgeList, Alloc>& v)
  161. {
  162. typedef boost::counting_iterator<typename EdgeList::value_type> Iter;
  163. return std::make_pair(Iter(0), Iter(v.size()));
  164. }
  165. template <class EdgeList, class Alloc>
  166. typename std::vector<EdgeList, Alloc>::size_type
  167. num_vertices(const std::vector<EdgeList, Alloc>& v)
  168. {
  169. return v.size();
  170. }
  171. template<class EdgeList, class Allocator>
  172. typename std::pair<typename detail::val_edge<EdgeList>::type, bool>
  173. add_edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
  174. std::vector<EdgeList, Allocator>& g)
  175. {
  176. typedef typename detail::val_edge<EdgeList>::type edge_type;
  177. g[u].insert(g[u].end(), v);
  178. return std::make_pair(edge_type(u, v), true);
  179. }
  180. template<class EdgeList, class Allocator>
  181. typename std::pair<typename detail::val_edge<EdgeList>::type, bool>
  182. edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
  183. std::vector<EdgeList, Allocator>& g)
  184. {
  185. typedef typename detail::val_edge<EdgeList>::type edge_type;
  186. typename EdgeList::iterator i = g[u].begin(), end = g[u].end();
  187. for (; i != end; ++i)
  188. if (*i == v)
  189. return std::make_pair(edge_type(u, v), true);
  190. return std::make_pair(edge_type(), false);
  191. }
  192. template<class EdgeList, class Allocator>
  193. void
  194. remove_edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
  195. std::vector<EdgeList, Allocator>& g)
  196. {
  197. typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
  198. if (i != g[u].end())
  199. g[u].erase(i, g[u].end());
  200. }
  201. template<class EdgeList, class Allocator>
  202. void
  203. remove_edge(typename detail::val_edge<EdgeList>::type e,
  204. std::vector<EdgeList, Allocator>& g)
  205. {
  206. typename EdgeList::value_type u, v;
  207. u = e.first;
  208. v = e.second;
  209. // FIXME: edge type does not fully specify the edge to be deleted
  210. typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
  211. if (i != g[u].end())
  212. g[u].erase(i, g[u].end());
  213. }
  214. template<class EdgeList, class Allocator, class Predicate>
  215. void
  216. remove_edge_if(Predicate p,
  217. std::vector<EdgeList, Allocator>& g)
  218. {
  219. for (std::size_t u = 0; u < g.size(); ++u) {
  220. // Oops! gcc gets internal compiler error on compose_.......
  221. typedef typename EdgeList::iterator iterator;
  222. iterator b = g[u].begin(), e = g[u].end();
  223. if (!g[u].empty()) {
  224. for(; b != e;) {
  225. if (p(std::make_pair(u, *b))) {
  226. --e;
  227. if (b == e)
  228. break;
  229. else
  230. iter_swap(b, e);
  231. } else {
  232. ++b;
  233. }
  234. }
  235. }
  236. if (e != g[u].end())
  237. g[u].erase(e, g[u].end());
  238. }
  239. }
  240. template<class EdgeList, class Allocator>
  241. typename EdgeList::value_type
  242. add_vertex(std::vector<EdgeList, Allocator>& g)
  243. {
  244. g.resize(g.size()+1);
  245. return g.size()-1;
  246. }
  247. template<class EdgeList, class Allocator>
  248. void
  249. clear_vertex(typename EdgeList::value_type u,
  250. std::vector<EdgeList, Allocator>& g)
  251. {
  252. g[u].clear();
  253. for (std::size_t i = 0; i < g.size(); ++i)
  254. remove_edge(i, u, g);
  255. }
  256. template<class EdgeList, class Allocator>
  257. void
  258. remove_vertex(typename EdgeList::value_type u,
  259. std::vector<EdgeList, Allocator>& g)
  260. {
  261. typedef typename EdgeList::iterator iterator;
  262. clear_vertex(u, g);
  263. g.erase(g.begin() + u);
  264. for (std::size_t i = 0; i < g.size(); ++i)
  265. for ( iterator it = g[i].begin(); it != g[i].end(); ++it )
  266. // after clear_vertex *it is never equal to u
  267. if ( *it > u )
  268. --*it;
  269. }
  270. template<typename EdgeList, typename Allocator>
  271. struct property_map<std::vector<EdgeList, Allocator>, vertex_index_t>
  272. {
  273. typedef identity_property_map type;
  274. typedef type const_type;
  275. };
  276. template<typename EdgeList, typename Allocator>
  277. identity_property_map
  278. get(vertex_index_t, const std::vector<EdgeList, Allocator>&)
  279. {
  280. return identity_property_map();
  281. }
  282. template<typename EdgeList, typename Allocator>
  283. identity_property_map
  284. get(vertex_index_t, std::vector<EdgeList, Allocator>&)
  285. {
  286. return identity_property_map();
  287. }
  288. } // namespace boost
  289. #endif // BOOST_VECTOR_AS_GRAPH_HPP