graph_filtering.hh 8.99 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
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006  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 2
// 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#ifndef FILTERING_HH
#define FILTERING_HH

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/filtered_graph.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/logical.hpp>
#include <boost/python/object.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/extract.hpp>

#include "graph_adaptor.hh"
#include "graph_selectors.hh"
38
#include "graph_python_filtering.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

namespace graph_tool
{
using namespace boost;

//==============================================================================
// HardNumVertices()
//==============================================================================
struct HardNumVertices
{
    template <class Graph>
    size_t operator()(const Graph &g) const
    {
	size_t n = 0;
	typename graph_traits<Graph>::vertex_iterator v_iter, v_begin, v_end;
	tie(v_begin, v_end) = vertices(g);
	for (v_iter = v_begin; v_iter != v_end; ++v_iter)
	    n++;
	return n;
    }
};

//==============================================================================
// SoftNumVertices()
//==============================================================================
struct SoftNumVertices
{
    template <class Graph>
    size_t operator()(const Graph &g) const { return num_vertices(g); }
};

//==============================================================================
// HardNumEdges()
//==============================================================================
struct HardNumEdges
{
    template <class Graph>
    size_t operator()(const Graph &g) const
    {
	size_t n = 0;
	typename graph_traits<Graph>::edge_iterator e_iter, e_begin, e_end;
	tie(e_begin, e_end) = edges(g);
	for (e_iter = e_begin; e_iter != e_end; ++e_iter)
	    n++;
	return n;
    }
};

//==============================================================================
// SoftNumEdges()
//==============================================================================
struct SoftNumEdges
{
    template <class Graph> 
    size_t operator()(const Graph &g) const { return num_edges(g); }
};

//==============================================================================
// RangeFilter
//==============================================================================
template <class FilteredPropertyMap>
class RangeFilter
{
public:
    RangeFilter(){}
    typedef typename property_traits<FilteredPropertyMap>::value_type value_type;
    typedef typename property_traits<FilteredPropertyMap>::key_type key_type;
    RangeFilter(FilteredPropertyMap filtered_property, std::pair<value_type, value_type> range)
        : _filtered_property(filtered_property), _range(range) {}
    template <class VertexOrEdge>
    bool operator() (VertexOrEdge e) const 
    {
        // ignore if outside allowed range
        if ( _filtered_property[e] < _range.first || _filtered_property[e] > _range.second)
            return false;
        return true;
    }
private:
    FilteredPropertyMap _filtered_property;
    std::pair<value_type, value_type> _range;
};

typedef mpl::vector<mpl::bool_<true>, mpl::bool_<false> > reverse_check;
typedef mpl::vector<mpl::bool_<false> > never_reversed;
typedef mpl::vector<mpl::bool_<true> > always_reversed;
typedef mpl::vector<mpl::bool_<true>, mpl::bool_<false> > directed_check;
typedef mpl::vector<mpl::bool_<true> > always_directed;
typedef mpl::vector<mpl::bool_<false> > always_undirected;

template <class Graph, class Action>
struct check_reverse
{
    check_reverse(const Graph &g, Action a, bool reverse, bool& found): _g(g), _a(a), _reverse(reverse), _found(found) {}

    template <class Reverse>
    void operator()(Reverse) const
    {
	if (_reverse)
	{
	    reverse_graph<Graph> rg(_g);
	    _a(rg);
	    _found = true;
	}
    }

    void operator()(mpl::bool_<false>) const
    { 
	if (!_reverse)
	{
	    _a(_g);
	    _found = true;
	}
    }

    const Graph &_g;
    Action _a;
    bool _reverse;
    bool& _found;
};

template <class Graph, class Action, class ReverseCheck>
struct check_directed
{
    check_directed(const Graph &g, Action a, bool reverse, bool directed, bool& found)
	: _g(g), _a(a), _reverse(reverse), _directed(directed), _found(found) {}

    template <class Directed>
    void operator()(Directed)
    {
	if (_directed)
	    mpl::for_each<ReverseCheck>(check_reverse<Graph, Action>(_g, _a, _reverse, _found));
    }

    void operator()(mpl::bool_<false>)
    { 
	if (!_directed)
	{
	    UndirectedAdaptor<Graph> ug(_g);
	    _a(ug);
	    _found = true;
	}
    }

    const Graph &_g;
    Action _a;
    bool _reverse;
    bool _directed;
    bool& _found;
};

template <class Graph, class Action, class ReverseCheck, class DirectedCheck> 
void check_python_filter(const Graph& g, const GraphInterface &gi, Action a, bool& found, ReverseCheck, DirectedCheck)
{
192
193
    typedef PythonFilter<Graph,typename graph_traits<Graph>::vertex_descriptor> vertex_filter_t;
    typedef PythonFilter<Graph,typename graph_traits<Graph>::edge_descriptor> edge_filter_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
194

195
    if (gi._edge_python_filter != python::object())
Tiago Peixoto's avatar
Tiago Peixoto committed
196
    {
197
	typedef filtered_graph<Graph, edge_filter_t, keep_all> efg_t;
198
	efg_t efg(g,edge_filter_t(g, gi._properties, gi._edge_python_filter), keep_all());
199
200
201

	if (gi._vertex_python_filter != python::object())
	{
202
	    typedef PythonFilter<efg_t, typename graph_traits<efg_t>::vertex_descriptor, mpl::bool_<true> > vertex_filter_t;
203
	    typedef filtered_graph<efg_t,keep_all,vertex_filter_t> vefg_t;
204
	    vefg_t vefg(efg,keep_all(),vertex_filter_t(efg, gi._properties, gi._vertex_python_filter));
205
206
207
208
209
210
	    mpl::for_each<DirectedCheck>(check_directed<vefg_t,Action,ReverseCheck>(vefg, a, gi._reversed, gi._directed, found));
	}
	else
	{
	    mpl::for_each<DirectedCheck>(check_directed<efg_t,Action,ReverseCheck>(efg, a, gi._reversed, gi._directed, found));
	}
Tiago Peixoto's avatar
Tiago Peixoto committed
211
212
213
    }
    else if (gi._vertex_python_filter != python::object())
    {
214
	typedef filtered_graph<Graph,keep_all,vertex_filter_t> vfg_t;
215
	vfg_t vfg(g,keep_all(),vertex_filter_t(g, gi._properties, gi._vertex_python_filter));
216
	mpl::for_each<DirectedCheck>(check_directed<vfg_t,Action,ReverseCheck>(vfg, a, gi._reversed, gi._directed, found));
Tiago Peixoto's avatar
Tiago Peixoto committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    } 
    else
    {
	mpl::for_each<DirectedCheck>(check_directed<Graph,Action,ReverseCheck>(g, a, gi._reversed, gi._directed, found));
    }
}

template <class Action, class ReverseCheck, class DirectedCheck> 
void check_filter(const GraphInterface &g, Action a, ReverseCheck, DirectedCheck)
{
    typedef RangeFilter<GraphInterface::vertex_filter_map_t> vertex_filter_t;
    typedef RangeFilter<GraphInterface::edge_filter_map_t> edge_filter_t;
    
    bool found = false;

    if (g._edge_python_filter == python::object() && g._vertex_python_filter == python::object())
    {
	if (g._vertex_filter_property != "" && g._edge_filter_property != "")
	{	
	    typedef filtered_graph<GraphInterface::multigraph_t, edge_filter_t, vertex_filter_t> fg_t;
	    fg_t fg(g._mg, edge_filter_t(g._edge_filter_map, g._edge_range), vertex_filter_t(g._vertex_filter_map, g._vertex_range));
	    mpl::for_each<DirectedCheck>(check_directed<fg_t,Action,ReverseCheck>(fg, a, g._reversed, g._directed, found));
	}
	else if (g._vertex_filter_property != "")
	{
	    typedef filtered_graph<GraphInterface::multigraph_t, keep_all, vertex_filter_t> fg_t;
	    fg_t fg(g._mg, keep_all(), vertex_filter_t(g._vertex_filter_map, g._vertex_range));
	    mpl::for_each<DirectedCheck>(check_directed<fg_t,Action,ReverseCheck>(fg, a, g._reversed, g._directed, found));
	} 
	else if (g._edge_filter_property != "")
	{
	    typedef filtered_graph<GraphInterface::multigraph_t, edge_filter_t, keep_all> fg_t;
	    fg_t fg(g._mg, edge_filter_t(g._edge_filter_map, g._edge_range), keep_all());
	    mpl::for_each<DirectedCheck>(check_directed<fg_t,Action,ReverseCheck>(fg, a, g._reversed, g._directed, found));
	}
	else
	{
	    mpl::for_each<DirectedCheck>(check_directed<GraphInterface::multigraph_t,Action,ReverseCheck>(g._mg, a, g._reversed, g._directed, found));
	}
    }
    else
    {
	check_python_filter(g._mg, g, a, found, ReverseCheck(), DirectedCheck());
    }

    if (!found)
	throw GraphException("graph filtering error: filter not found");
    
}

267
} //graph_tool namespace 
268

Tiago Peixoto's avatar
Tiago Peixoto committed
269
#endif