vector_as_graph.hpp 10 KB

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