9
9
#include < iostream>
10
10
#include < algorithm>
11
11
#include < boost/graph/adjacency_list.hpp>
12
+ #include < boost/graph/adjacency_matrix.hpp>
12
13
#include < boost/graph/dominator_tree.hpp>
13
14
14
15
using namespace std ;
@@ -24,13 +25,51 @@ struct DominatorCorrectnessTestSet
24
25
25
26
using namespace boost ;
26
27
27
- typedef adjacency_list< listS, listS, bidirectionalS,
28
- property< vertex_index_t , std::size_t >, no_property >
29
- G;
28
+ // a workaround for the C++ standard before C++17, after switching to C++17,
29
+ // the method may be just inlined into the run_test() with constexpr if.
30
+ namespace detail
31
+ {
30
32
31
- int main (int , char *[])
33
+ template < typename Graph >
34
+ void index_graph (Graph&, std::true_type /* IsRandomAccessAdjacentList*/ )
35
+ {
36
+ // nothing to do for already indexed adjacent list
37
+ }
38
+
39
+ template < typename Graph >
40
+ void index_graph (Graph& g, std::false_type /* IsRandomAccessAdjacentList*/ )
32
41
{
33
- typedef DominatorCorrectnessTestSet::edge edge;
42
+ using IndexMap = typename property_map< Graph, vertex_index_t >::type;
43
+ IndexMap indexMap (get (vertex_index, g));
44
+ typename graph_traits< Graph >::vertex_iterator uItr, uEnd;
45
+ int j = 0 ;
46
+ for (boost::tie (uItr, uEnd) = vertices (g); uItr != uEnd; ++uItr, ++j)
47
+ {
48
+ put (indexMap, *uItr, j);
49
+ }
50
+ }
51
+
52
+ } // namespace detail
53
+
54
+ template < typename OEL, typename VL, typename D, typename VP, typename EP,
55
+ typename GP, typename EL >
56
+ void index_graph (adjacency_list< OEL, VL, D, VP, EP, GP, EL >& g)
57
+ {
58
+ using Traits = adjacency_list_traits< OEL, VL, D, EL >;
59
+ ::detail::index_graph (
60
+ g, std::integral_constant< bool , Traits::is_rand_access::value > {});
61
+ }
62
+
63
+ template < typename D, typename VP, typename EP, typename GP, typename A >
64
+ void index_graph (adjacency_matrix<D, VP, EP, GP, A>&)
65
+ {
66
+ // nothing to do for already indexed adjacent matrix
67
+ }
68
+
69
+ template < typename Graph >
70
+ void run_test ()
71
+ {
72
+ using edge = DominatorCorrectnessTestSet::edge;
34
73
35
74
DominatorCorrectnessTestSet testSet[7 ];
36
75
@@ -217,34 +256,32 @@ int main(int, char*[])
217
256
{
218
257
const int numOfVertices = testSet[i].numOfVertices ;
219
258
220
- G g (testSet[i].edges .begin (), testSet[i].edges .end (), numOfVertices);
259
+ Graph g (testSet[i].edges .begin (), testSet[i].edges .end (), numOfVertices);
260
+
261
+ using Vertex = typename graph_traits< Graph >::vertex_descriptor;
262
+ using IndexMap = typename property_map< Graph, vertex_index_t >::type;
263
+ IndexMap indexMap (get (vertex_index, g));
264
+ using PredMap
265
+ = iterator_property_map< typename vector< Vertex >::iterator, IndexMap >;
221
266
222
- typedef graph_traits< G >::vertex_descriptor Vertex;
223
- typedef property_map< G, vertex_index_t >::type IndexMap;
224
- typedef iterator_property_map< vector< Vertex >::iterator, IndexMap >
225
- PredMap;
267
+ index_graph (g);
226
268
227
269
vector< Vertex > domTreePredVector, domTreePredVector2;
228
- IndexMap indexMap (get (vertex_index, g));
229
- graph_traits< G >::vertex_iterator uItr, uEnd;
230
- int j = 0 ;
231
- for (boost::tie (uItr, uEnd) = vertices (g); uItr != uEnd; ++uItr, ++j)
232
- {
233
- put (indexMap, *uItr, j);
234
- }
235
270
236
271
// Lengauer-Tarjan dominator tree algorithm
237
272
domTreePredVector = vector< Vertex >(
238
- num_vertices (g), graph_traits< G >::null_vertex ());
273
+ num_vertices (g), graph_traits< Graph >::null_vertex ());
239
274
PredMap domTreePredMap
240
275
= make_iterator_property_map (domTreePredVector.begin (), indexMap);
241
276
242
277
lengauer_tarjan_dominator_tree (g, vertex (0 , g), domTreePredMap);
243
278
244
279
vector< int > idom (num_vertices (g));
280
+ typename graph_traits< Graph >::vertex_iterator uItr, uEnd;
245
281
for (boost::tie (uItr, uEnd) = vertices (g); uItr != uEnd; ++uItr)
246
282
{
247
- if (get (domTreePredMap, *uItr) != graph_traits< G >::null_vertex ())
283
+ if (get (domTreePredMap, *uItr)
284
+ != graph_traits< Graph >::null_vertex ())
248
285
idom[get (indexMap, *uItr)]
249
286
= get (indexMap, get (domTreePredMap, *uItr));
250
287
else
@@ -260,7 +297,7 @@ int main(int, char*[])
260
297
261
298
// compare results of fast version and slow version of dominator tree
262
299
domTreePredVector2 = vector< Vertex >(
263
- num_vertices (g), graph_traits< G >::null_vertex ());
300
+ num_vertices (g), graph_traits< Graph >::null_vertex ());
264
301
domTreePredMap
265
302
= make_iterator_property_map (domTreePredVector2.begin (), indexMap);
266
303
@@ -269,7 +306,8 @@ int main(int, char*[])
269
306
vector< int > idom2 (num_vertices (g));
270
307
for (boost::tie (uItr, uEnd) = vertices (g); uItr != uEnd; ++uItr)
271
308
{
272
- if (get (domTreePredMap, *uItr) != graph_traits< G >::null_vertex ())
309
+ if (get (domTreePredMap, *uItr)
310
+ != graph_traits< Graph >::null_vertex ())
273
311
idom2[get (indexMap, *uItr)]
274
312
= get (indexMap, get (domTreePredMap, *uItr));
275
313
else
@@ -283,6 +321,21 @@ int main(int, char*[])
283
321
for (k = 0 ; k < num_vertices (g); ++k)
284
322
BOOST_TEST (domTreePredVector[k] == domTreePredVector2[k]);
285
323
}
324
+ cout << endl;
325
+ }
326
+
327
+ int main (int , char *[])
328
+ {
329
+ using AdjacencyListList = adjacency_list< listS, listS, bidirectionalS,
330
+ property< vertex_index_t , std::size_t >, no_property >;
331
+
332
+ using AdjacencyListVec = adjacency_list< listS, vecS, bidirectionalS >;
333
+
334
+ using AdjacencyMatrix = adjacency_matrix< directedS >;
335
+
336
+ run_test< AdjacencyListList >();
337
+ run_test< AdjacencyListVec >();
338
+ run_test< AdjacencyMatrix >();
286
339
287
340
return boost::report_errors ();
288
341
}
0 commit comments