annotate DEPENDENCIES/generic/include/boost/graph/vector_as_graph.hpp @ 133:4acb5d8d80b6 tip

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