1 /*
2  * Copyright (c) 2015-2016, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *  * Neither the name of Intel Corporation nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /** \file
30  * \brief Convenience functions allowing range-based-for over BGL graphs.
31  *
32  * Normally with the BGL in C++98 you need to do this to iterate over graph
33  * elements:
34  *
35  *        Graph:out_edge_iterator ei, ee;
36  *        for (tie(ei, ee) = out_edges(v, g); ei != ee; ++ei) {
37  *            do_thing_with_edge(*ei, g);
38  *        }
39  *
40  * But now, with C++11 range-based-for and these functions, you can do this
41  * instead:
42  *
43  *        for (auto e : out_edges_range(v, g)) {
44  *            do_thing_with_edge(e, g);
45  *        }
46  *
47  * This syntax is much more compact and keeps the iterator vars from cluttering
48  * the outer scope.
49  */
50 
51 #ifndef UTIL_GRAPH_RANGE_H
52 #define UTIL_GRAPH_RANGE_H
53 
54 #include <boost/range/iterator_range.hpp>
55 
56 namespace ue2 {
57 
58 /** Adapts a pair of iterators into a range. */
59 template <class Iter>
pair_range(const std::pair<Iter,Iter> & p)60 inline boost::iterator_range<Iter> pair_range(const std::pair<Iter, Iter> &p) {
61     return boost::make_iterator_range(p.first, p.second);
62 }
63 
64 /** vertices(g) */
65 template <class Graph>
66 inline auto vertices_range(const Graph &g)
67     -> decltype(pair_range(vertices(g))) {
68     return pair_range(vertices(g));
69 }
70 
71 /** edges(g) */
72 template <class Graph>
73 inline auto edges_range(const Graph &g) -> decltype(pair_range(edges(g))) {
74     return pair_range(edges(g));
75 }
76 
77 /** out_edges(v, g) */
78 template <class Graph>
79 inline auto out_edges_range(const typename Graph::vertex_descriptor &v,
80                             const Graph &g)
81     -> decltype(pair_range(out_edges(v, g))) {
82     return pair_range(out_edges(v, g));
83 }
84 
85 /** in_edges(v, g) */
86 template <class Graph>
87 inline auto in_edges_range(const typename Graph::vertex_descriptor &v,
88                            const Graph &g)
89     -> decltype(pair_range(in_edges(v, g))) {
90     return pair_range(in_edges(v, g));
91 }
92 
93 /** adjacent_vertices(v, g) */
94 template <class Graph>
95 inline auto adjacent_vertices_range(const typename Graph::vertex_descriptor &v,
96                                     const Graph &g)
97     -> decltype(pair_range(adjacent_vertices(v, g))) {
98     return pair_range(adjacent_vertices(v, g));
99 }
100 
101 /** inv_adjacent_vertices(v, g) */
102 template <class Graph>
103 inline auto inv_adjacent_vertices_range(
104     const typename Graph::vertex_descriptor &v, const Graph &g)
105     -> decltype(pair_range(inv_adjacent_vertices(v, g))) {
106     return pair_range(inv_adjacent_vertices(v, g));
107 }
108 
109 } // namespace ue2
110 
111 #endif // UTIL_GRAPH_RANGE_H
112