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