graph_wrap.hh 7.65 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2007  Tiago de Paula Peixoto <tiago@forked.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_WRAP_HH
#define GRAPH_WRAP_HH

#include<utility>

// Graph wrapper which takes care of edge index housekeeping

namespace boost
{
using namespace graph_tool;

template <class Graph>
class GraphWrap
{
  public:
    GraphWrap(Graph& g, GraphInterface& gi)
        : _g(g), _gi(gi) {}

    typedef typename Graph::vertex_property_type vertex_property_type;
    typedef typename Graph::edge_property_type edge_property_type;
    typedef typename Graph::graph_tag graph_tag;
    typedef Graph orig_graph_t;

    Graph& _g;
    GraphInterface& _gi;
};

template <class Graph>
GraphWrap<Graph> graph_wrap(Graph& g, GraphInterface& gi)
{
    return GraphWrap<Graph>(g, gi);
}

template <class Graph>
struct graph_traits<GraphWrap<Graph> >: public graph_traits<Graph> {};

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::vertex_descriptor
source(typename graph_traits<GraphWrap<Graph> >::edge_descriptor e,
       const GraphWrap<Graph>& g)
{
    return source(e, g._g);
}

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::vertex_descriptor
target(typename graph_traits<GraphWrap<Graph> >::edge_descriptor e,
       const GraphWrap<Graph>& g)
{
    return target(e, g._g);
}

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::vertex_descriptor
vertex(typename graph_traits<GraphWrap<Graph> >::vertices_size_type n,
       const GraphWrap<Graph>& g)
{
    return vertex(n, g._g);
}

template <class Graph>
inline std::pair<typename graph_traits<GraphWrap<Graph> >::vertex_iterator,
                 typename graph_traits<GraphWrap<Graph> >::vertex_iterator>
vertices(const GraphWrap<Graph>& g)
{
    return vertices(g._g);
}

template <class Graph>
inline std::pair<typename graph_traits<GraphWrap<Graph> >::edge_iterator,
                 typename graph_traits<GraphWrap<Graph> >::edge_iterator>
edges(const GraphWrap<Graph>& g)
{
    return edges(g._g);
}

template <class Graph>
inline std::pair<typename graph_traits<GraphWrap<Graph> >::out_edge_iterator,
                 typename graph_traits<GraphWrap<Graph> >::out_edge_iterator >
out_edges(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
          const GraphWrap<Graph>& g)
{
    return out_edges(u, g._g);
}

template <class Graph>
inline std::pair<typename graph_traits<GraphWrap<Graph> >::in_edge_iterator,
                 typename graph_traits<GraphWrap<Graph> >::in_edge_iterator >
in_edges(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
         const GraphWrap<Graph>& g)
{
    return in_edges(u, g._g);
}

template <class Graph>
inline
std::pair<typename graph_traits<GraphWrap<Graph> >::adjacency_iterator,
          typename graph_traits<GraphWrap<Graph> >::adjacency_iterator>
adjacent_vertices
    (typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
     const GraphWrap<Graph>& g)
{
    return adjacent_vertices(u, g._g);
}

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::vertices_size_type
num_vertices(const GraphWrap<Graph>& g)
{
    return num_vertices(g._g);
}

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::edges_size_type
num_edges(const GraphWrap<Graph>& g)
{
    return g._gi.GetNumberOfEdges();
}

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::degree_size_type
out_degree(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
           const GraphWrap<Graph>& g)
{
    return out_degree(u, g._g);
}

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::degree_size_type
in_degree(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
          const GraphWrap<Graph>& g)
{
    return in_degree(u, g._g);
}

template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::degree_size_type
degree(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
       const GraphWrap<Graph>& g)
{
    return degree(u, g._g);
}

template <class Graph>
inline std::pair<typename graph_traits<GraphWrap<Graph> >::edge_descriptor, bool>
add_edge(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
         typename graph_traits<GraphWrap<Graph> >::vertex_descriptor v,
         GraphWrap<Graph>& g)
{
    std::pair<typename graph_traits<GraphWrap<Graph> >::edge_descriptor, bool> retval =
        add_edge(u, v, g._g);
    g._gi.AddEdgeIndex(retval.first);
    return retval;
}

template <class Graph>
inline void remove_edge(typename graph_traits<GraphWrap<Graph> >::edge_descriptor e,
                        GraphWrap<Graph>& g)
{
177
    g._gi.RemoveEdgeIndex(e);
Tiago Peixoto's avatar
Tiago Peixoto committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
}

template <class Graph>
inline void remove_edge(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
                        typename graph_traits<GraphWrap<Graph> >::vertex_descriptor v,
                        Graph& g)
{
    vector<typename graph_traits<GraphWrap<Graph> >::edge_descriptor> removed_edges;

    typename graph_traits<GraphWrap<Graph> >::out_edge_iterator e, e_end;
    for(tie(e, e_end) = out_edges(u, g); e != e_end; ++e)
        if (target(e, g) == v)
            removed_edges.push_back(*e);
    for (typeof(removed_edges.begin()) iter = removed_edges.begin();
         iter != removed_edges.end(); ++iter)
        remove_edge(*iter, g);
}


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
template <class Graph>
inline typename graph_traits<GraphWrap<Graph> >::vertex_descriptor
add_vertex(GraphWrap<Graph>& g)
{
    return add_vertex(g._g);
}

template <class Graph>
inline void clear_vertex(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
                         GraphWrap<Graph>& g)
{
    typedef GraphWrap<Graph> graph_t;
    vector<typename graph_traits<graph_t>::edge_descriptor> del_es;
    typename graph_traits<graph_t>::out_edge_iterator e, e_end;
    for (tie(e,e_end) == out_edges(u, g); e != e_end; ++e)
        del_es.push_back(*e);
    if (is_directed::apply<graph_t>::type::value)
    {
        typename in_edge_iteratorS<graph_t>::type e, e_end;
        for (tie(e,e_end) == in_edge_iteratorS<graph_t>::get_edges(u, g);
             e != e_end; ++e)
            del_es.push_back(*e);
    }
    for (size_t i = 0; i < del_es.size(); ++i)
        remove_edge(del_es[i], g);
}

template <class Graph>
inline void remove_vertex(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
                          GraphWrap<Graph>& g)
{
    clear_vertex(u, g);
    remove_vertex(u, g._g);
}

Tiago Peixoto's avatar
Tiago Peixoto committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
template <class Graph, class Predicate>
inline void
remove_out_edge_if(typename graph_traits<GraphWrap<Graph> >::vertex_descriptor u,
                   Predicate predicate, Graph& g)
{
    vector<typename graph_traits<GraphWrap<Graph> >::edge_descriptor> removed_edges;

    typename graph_traits<GraphWrap<Graph> >::out_edge_iterator e, e_end;
    for(tie(e, e_end) = out_edges(u, g); e != e_end; ++e)
        if (predicate(*e))
            removed_edges.push_back(*e);
    for (typeof(removed_edges.begin()) iter = removed_edges.begin();
         iter != removed_edges.end(); ++iter)
        remove_edge(*iter, g);
}

}

#endif // GRAPH_WRAP_HH