@@ -24,13 +24,48 @@ struct DominatorCorrectnessTestSet
2424
2525using namespace boost ;
2626
27- typedef adjacency_list< listS, listS, bidirectionalS ,
28- property< vertex_index_t , std:: size_t >, no_property >
29- G;
27+ // a workaround for the C++ standard before C++17, after switching to C++17 ,
28+ // the method may be just inlined into the run_test() with constexpr if.
29+ namespace detail {
3030
31- int main (int , char *[])
31+ template < bool IsRandomAccessAdjacentList = true >
32+ struct GraphIndexer {
33+ template <typename Graph>
34+ static void index_graph (Graph &g) {
35+ // nothing to do for already indexed adjacent list
36+ }
37+ };
38+
39+ template <>
40+ struct GraphIndexer <false > {
41+ template <typename Graph>
42+ static void index_graph (Graph &g) {
43+ using IndexMap = typename property_map< Graph, vertex_index_t >::type;
44+ IndexMap indexMap (get (vertex_index, g));
45+ typename graph_traits< Graph >::vertex_iterator uItr, uEnd;
46+ int j = 0 ;
47+ for (boost::tie (uItr, uEnd) = vertices (g); uItr != uEnd; ++uItr, ++j)
48+ {
49+ put (indexMap, *uItr, j);
50+ }
51+ }
52+ };
53+
54+ } // namespace detail
55+
56+ template < typename Graph >
57+ void index_graph (Graph &g) {
58+ using Traits = adjacency_list_traits< typename Graph::out_edge_list_selector,
59+ typename Graph::vertex_list_selector,
60+ typename Graph::directed_selector,
61+ typename Graph::edge_list_selector >;
62+ ::detail::GraphIndexer< Traits::is_rand_access::value >::index_graph (g);
63+ }
64+
65+ template < typename Graph >
66+ void run_test ()
3267{
33- typedef DominatorCorrectnessTestSet::edge edge;
68+ using edge = DominatorCorrectnessTestSet::edge;
3469
3570 DominatorCorrectnessTestSet testSet[7 ];
3671
@@ -217,34 +252,32 @@ int main(int, char*[])
217252 {
218253 const int numOfVertices = testSet[i].numOfVertices ;
219254
220- G g (testSet[i].edges .begin (), testSet[i].edges .end (), numOfVertices);
255+ Graph g (testSet[i].edges .begin (), testSet[i].edges .end (), numOfVertices);
256+
257+ using Vertex = typename graph_traits< Graph >::vertex_descriptor;
258+ using IndexMap = typename property_map< Graph, vertex_index_t >::type;
259+ IndexMap indexMap (get (vertex_index, g));
260+ using PredMap
261+ = iterator_property_map< typename vector< Vertex >::iterator, IndexMap >;
221262
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;
263+ index_graph (g);
226264
227265 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- }
235266
236267 // Lengauer-Tarjan dominator tree algorithm
237268 domTreePredVector = vector< Vertex >(
238- num_vertices (g), graph_traits< G >::null_vertex ());
269+ num_vertices (g), graph_traits< Graph >::null_vertex ());
239270 PredMap domTreePredMap
240271 = make_iterator_property_map (domTreePredVector.begin (), indexMap);
241272
242273 lengauer_tarjan_dominator_tree (g, vertex (0 , g), domTreePredMap);
243274
244275 vector< int > idom (num_vertices (g));
276+ typename graph_traits< Graph >::vertex_iterator uItr, uEnd;
245277 for (boost::tie (uItr, uEnd) = vertices (g); uItr != uEnd; ++uItr)
246278 {
247- if (get (domTreePredMap, *uItr) != graph_traits< G >::null_vertex ())
279+ if (get (domTreePredMap, *uItr)
280+ != graph_traits< Graph >::null_vertex ())
248281 idom[get (indexMap, *uItr)]
249282 = get (indexMap, get (domTreePredMap, *uItr));
250283 else
@@ -260,7 +293,7 @@ int main(int, char*[])
260293
261294 // compare results of fast version and slow version of dominator tree
262295 domTreePredVector2 = vector< Vertex >(
263- num_vertices (g), graph_traits< G >::null_vertex ());
296+ num_vertices (g), graph_traits< Graph >::null_vertex ());
264297 domTreePredMap
265298 = make_iterator_property_map (domTreePredVector2.begin (), indexMap);
266299
@@ -269,7 +302,8 @@ int main(int, char*[])
269302 vector< int > idom2 (num_vertices (g));
270303 for (boost::tie (uItr, uEnd) = vertices (g); uItr != uEnd; ++uItr)
271304 {
272- if (get (domTreePredMap, *uItr) != graph_traits< G >::null_vertex ())
305+ if (get (domTreePredMap, *uItr)
306+ != graph_traits< Graph >::null_vertex ())
273307 idom2[get (indexMap, *uItr)]
274308 = get (indexMap, get (domTreePredMap, *uItr));
275309 else
@@ -283,6 +317,17 @@ int main(int, char*[])
283317 for (k = 0 ; k < num_vertices (g); ++k)
284318 BOOST_TEST (domTreePredVector[k] == domTreePredVector2[k]);
285319 }
320+ }
321+
322+ int main (int , char *[])
323+ {
324+ using AdjacencyListList = adjacency_list< listS, listS, bidirectionalS,
325+ property< vertex_index_t , std::size_t >, no_property >;
326+
327+ using AdjacencyListVec = adjacency_list< listS, vecS, bidirectionalS >;
328+
329+ run_test< AdjacencyListList >();
330+ run_test< AdjacencyListVec >();
286331
287332 return boost::report_errors ();
288333}
0 commit comments