vector_as_graph.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. 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. {
  97. typedef val_out_edge_iterator self;
  98. typedef std::pair<V,V> Edge;
  99. public:
  100. typedef std::input_iterator_tag iterator_category;
  101. typedef std::pair<V,V> value_type;
  102. typedef std::ptrdiff_t difference_type;
  103. typedef std::pair<V,V>* pointer;
  104. typedef const std::pair<V,V> reference;
  105. val_out_edge_iterator() { }
  106. val_out_edge_iterator(V s, Iter i) : _source(s), _iter(i) { }
  107. Edge operator*() const { return Edge(_source, *_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. V _source;
  114. Iter _iter;
  115. };
  116. template <class EdgeList>
  117. struct val_out_edge_iter
  118. {
  119. typedef typename EdgeList::value_type V;
  120. typedef typename EdgeList::const_iterator Iter;
  121. typedef val_out_edge_iterator<V,Iter> type;
  122. };
  123. template <class EdgeList>
  124. struct val_out_edge_ret
  125. {
  126. typedef typename val_out_edge_iter<EdgeList>::type IncIter;
  127. typedef std::pair<IncIter,IncIter> type;
  128. };
  129. } // namesapce detail
  130. template <class EdgeList, class Alloc>
  131. typename detail::val_out_edge_ret<EdgeList>::type
  132. out_edges(typename EdgeList::value_type v,
  133. const std::vector<EdgeList, Alloc>& g)
  134. {
  135. typedef typename detail::val_out_edge_iter<EdgeList>::type Iter;
  136. typedef typename detail::val_out_edge_ret<EdgeList>::type return_type;
  137. return return_type(Iter(v, g[v].begin()), Iter(v, g[v].end()));
  138. }
  139. template <class EdgeList, class Alloc>
  140. typename EdgeList::size_type
  141. out_degree(typename EdgeList::value_type v,
  142. const std::vector<EdgeList, Alloc>& g)
  143. {
  144. return g[v].size();
  145. }
  146. template <class EdgeList, class Alloc>
  147. std::pair<typename EdgeList::const_iterator,
  148. typename EdgeList::const_iterator>
  149. adjacent_vertices(typename EdgeList::value_type v,
  150. const std::vector<EdgeList, Alloc>& g)
  151. {
  152. return std::make_pair(g[v].begin(), g[v].end());
  153. }
  154. // source() and target() already provided for pairs in graph_traits.hpp
  155. template <class EdgeList, class Alloc>
  156. std::pair<boost::counting_iterator<typename EdgeList::value_type>,
  157. boost::counting_iterator<typename EdgeList::value_type> >
  158. vertices(const std::vector<EdgeList, Alloc>& v)
  159. {
  160. typedef boost::counting_iterator<typename EdgeList::value_type> Iter;
  161. return std::make_pair(Iter(0), Iter(v.size()));
  162. }
  163. template <class EdgeList, class Alloc>
  164. typename std::vector<EdgeList, Alloc>::size_type
  165. num_vertices(const std::vector<EdgeList, Alloc>& v)
  166. {
  167. return v.size();
  168. }
  169. template<class EdgeList, class Allocator>
  170. typename std::pair<typename detail::val_edge<EdgeList>::type, bool>
  171. add_edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
  172. std::vector<EdgeList, Allocator>& g)
  173. {
  174. typedef typename detail::val_edge<EdgeList>::type edge_type;
  175. g[u].insert(g[u].end(), v);
  176. return std::make_pair(edge_type(u, v), true);
  177. }
  178. template<class EdgeList, class Allocator>
  179. typename std::pair<typename detail::val_edge<EdgeList>::type, bool>
  180. edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
  181. std::vector<EdgeList, Allocator>& g)
  182. {
  183. typedef typename detail::val_edge<EdgeList>::type edge_type;
  184. typename EdgeList::iterator i = g[u].begin(), end = g[u].end();
  185. for (; i != end; ++i)
  186. if (*i == v)
  187. return std::make_pair(edge_type(u, v), true);
  188. return std::make_pair(edge_type(), false);
  189. }
  190. template<class EdgeList, class Allocator>
  191. void
  192. remove_edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
  193. std::vector<EdgeList, Allocator>& g)
  194. {
  195. typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
  196. if (i != g[u].end())
  197. g[u].erase(i, g[u].end());
  198. }
  199. template<class EdgeList, class Allocator>
  200. void
  201. remove_edge(typename detail::val_edge<EdgeList>::type e,
  202. std::vector<EdgeList, Allocator>& g)
  203. {
  204. typename EdgeList::value_type u, v;
  205. u = e.first;
  206. v = e.second;
  207. // FIXME: edge type does not fully specify the edge to be deleted
  208. typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
  209. if (i != g[u].end())
  210. g[u].erase(i, g[u].end());
  211. }
  212. template<class EdgeList, class Allocator, class Predicate>
  213. void
  214. remove_edge_if(Predicate p,
  215. std::vector<EdgeList, Allocator>& g)
  216. {
  217. for (std::size_t u = 0; u < g.size(); ++u) {
  218. // Oops! gcc gets internal compiler error on compose_.......
  219. typedef typename EdgeList::iterator iterator;
  220. iterator b = g[u].begin(), e = g[u].end();
  221. if (!g[u].empty()) {
  222. for(; b != e;) {
  223. if (p(std::make_pair(u, *b))) {
  224. --e;
  225. if (b == e)
  226. break;
  227. else
  228. iter_swap(b, e);
  229. } else {
  230. ++b;
  231. }
  232. }
  233. }
  234. if (e != g[u].end())
  235. g[u].erase(e, g[u].end());
  236. }
  237. }
  238. template<class EdgeList, class Allocator>
  239. typename EdgeList::value_type
  240. add_vertex(std::vector<EdgeList, Allocator>& g)
  241. {
  242. g.resize(g.size()+1);
  243. return g.size()-1;
  244. }
  245. template<class EdgeList, class Allocator>
  246. void
  247. clear_vertex(typename EdgeList::value_type u,
  248. std::vector<EdgeList, Allocator>& g)
  249. {
  250. g[u].clear();
  251. for (std::size_t i = 0; i < g.size(); ++i)
  252. remove_edge(i, u, g);
  253. }
  254. template<class EdgeList, class Allocator>
  255. void
  256. remove_vertex(typename EdgeList::value_type u,
  257. std::vector<EdgeList, Allocator>& g)
  258. {
  259. typedef typename EdgeList::iterator iterator;
  260. clear_vertex(u, g);
  261. g.erase(g.begin() + u);
  262. for (std::size_t i = 0; i < g.size(); ++i)
  263. for ( iterator it = g[i].begin(); it != g[i].end(); ++it )
  264. // after clear_vertex *it is never equal to u
  265. if ( *it > u )
  266. --*it;
  267. }
  268. template<typename EdgeList, typename Allocator>
  269. struct property_map<std::vector<EdgeList, Allocator>, vertex_index_t>
  270. {
  271. typedef identity_property_map type;
  272. typedef type const_type;
  273. };
  274. template<typename EdgeList, typename Allocator>
  275. identity_property_map
  276. get(vertex_index_t, const std::vector<EdgeList, Allocator>&)
  277. {
  278. return identity_property_map();
  279. }
  280. template<typename EdgeList, typename Allocator>
  281. identity_property_map
  282. get(vertex_index_t, std::vector<EdgeList, Allocator>&)
  283. {
  284. return identity_property_map();
  285. }
  286. } // namespace boost
  287. #endif // BOOST_VECTOR_AS_GRAPH_HPP