graph_selectors.hh 14.3 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2007  Tiago de Paula Peixoto <tiago@forked.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
7
// as published by the Free Software Foundation; either version 3
Tiago Peixoto's avatar
Tiago Peixoto committed
8
9
10
11
12
13
14
15
// 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
16
// along with this program. If not, see <http://www.gnu.org/licenses/>.
Tiago Peixoto's avatar
Tiago Peixoto committed
17
18
19
20
21
22
23

#ifndef GRAPH_SELECTORS_HH
#define GRAPH_SELECTORS_HH

#include <utility>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/map.hpp>
24
#include <boost/mpl/at.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
25
26
27
28
29
#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/logical.hpp>

30
#include "graph_adaptor.hh"
31
#include "graph_properties.hh"
32
#include "graph.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
33
34
35
36

namespace graph_tool
{

37
38
39
40
41
// This file contain selector for degree types for different types of
// graphs. Namely, they will return the in degree, out degree, and total degree
// for both directed and undirected graphs. There is also an scalar selector,
// which will return a specific scalar property

42
43
44
45
46
// This file also contains selectors for in_edge iterators of graphs, which
// return an empty range for undirected graphs

struct total_degreeS
{
47
48
    typedef size_t value_type;

Tiago Peixoto's avatar
Tiago Peixoto committed
49
    total_degreeS() {}
50
51
    template <class Graph, class Vertex>
    size_t operator()(const Vertex& v, const Graph &g) const
Tiago Peixoto's avatar
Tiago Peixoto committed
52
    {
53
        using namespace boost;
54
        typedef typename is_convertible
55
            <typename graph_traits<Graph>::directed_category,
56
             directed_tag>::type is_directed;
57
        return get_total_degree(v,g,is_directed());
58
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
59
60

    template <class Graph, class Vertex>
61
62
    size_t get_total_degree(const Vertex& v, const Graph &g, boost::true_type)
        const
Tiago Peixoto's avatar
Tiago Peixoto committed
63
    {
64
        return in_degree(v,g)+out_degree(v,g);
Tiago Peixoto's avatar
Tiago Peixoto committed
65
66
67
    }

    template <class Graph, class Vertex>
68
69
    size_t get_total_degree(const Vertex& v, const Graph &g, boost::false_type)
        const
Tiago Peixoto's avatar
Tiago Peixoto committed
70
    {
71
        return out_degree(v,g);
Tiago Peixoto's avatar
Tiago Peixoto committed
72
73
74
    }
};

75
76
struct in_degreeS
{
77
78
    typedef size_t value_type;

Tiago Peixoto's avatar
Tiago Peixoto committed
79
    in_degreeS() {}
80
81
    template <class Graph, class Vertex>
    size_t operator()(const Vertex& v, const Graph &g) const
Tiago Peixoto's avatar
Tiago Peixoto committed
82
    {
83
        using namespace boost;
84
        typedef typename is_convertible
85
            <typename graph_traits<Graph>::directed_category,
86
             directed_tag>::type is_directed;
87
        return get_in_degree(v,g,is_directed());
88
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
89
90

    template <class Graph, class Vertex>
91
92
    size_t get_in_degree(const Vertex& v, const Graph &g, boost::true_type)
        const
Tiago Peixoto's avatar
Tiago Peixoto committed
93
    {
94
        return in_degree(v,g);
Tiago Peixoto's avatar
Tiago Peixoto committed
95
96
97
    }

    template <class Graph, class Vertex>
98
99
    size_t get_in_degree(const Vertex& v, const Graph &g, boost::false_type)
        const
Tiago Peixoto's avatar
Tiago Peixoto committed
100
    {
101
        return 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
102
103
104
    }
};

105
106
struct out_degreeS
{
107
108
    typedef size_t value_type;

Tiago Peixoto's avatar
Tiago Peixoto committed
109
    out_degreeS() {}
110
111
    template <class Graph, class Vertex>
    size_t operator()(const Vertex& v, const Graph &g) const
Tiago Peixoto's avatar
Tiago Peixoto committed
112
    {
113
        return out_degree(v,g);
114
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
115
116
};

117
template <class PropertyMap>
Tiago Peixoto's avatar
Tiago Peixoto committed
118
119
struct scalarS
{
120
121
    typedef typename property_traits<PropertyMap>::value_type value_type;

122
123
    scalarS() {}
    scalarS(PropertyMap pmap): _pmap(pmap) {}
124

125
    template <class Descriptor, class Graph>
126
    typename property_traits<PropertyMap>::value_type
127
    operator()(const Descriptor& d, const Graph &g) const
Tiago Peixoto's avatar
Tiago Peixoto committed
128
    {
129
        return get(_pmap, d);
130
131
    }

132
133
    PropertyMap _pmap;
};
134

135
136
137
138
139
140
141
142
143
144
struct get_degree_selector
{
    typedef boost::mpl::map
        <boost::mpl::pair<in_degreeS,
                          boost::mpl::int_<GraphInterface::IN_DEGREE> >,
         boost::mpl::pair<out_degreeS,
                          boost::mpl::int_<GraphInterface::OUT_DEGREE> >,
         boost::mpl::pair<total_degreeS,
                          boost::mpl::int_<GraphInterface::TOTAL_DEGREE> > >
        degree_selector_index;
145

146
    template <class Selector>
147
        void operator()(Selector, int deg_index, boost::any& deg) const
148
    {
149
150
        if (mpl::at<degree_selector_index, Selector>::type::value == deg_index)
            deg = Selector();
Tiago Peixoto's avatar
Tiago Peixoto committed
151
    }
152
};
Tiago Peixoto's avatar
Tiago Peixoto committed
153

154
155
156
157
158
159
160
161
162
163
164
165
166
167
struct get_scalar_selector
{
    template <class PropertyMap>
    void operator()(PropertyMap, boost::any prop, boost::any& sec, bool& found)
        const
    {
        try
        {
            PropertyMap map = any_cast<PropertyMap>(prop);
            sec = scalarS<PropertyMap>(map);
            found = true;
        }
        catch (bad_any_cast&) {}
    }
168
169
170
171
172
173
174
175
176
};

struct scalar_selector_type
{
    template <class PropertyMap>
    struct apply
    {
        typedef scalarS<PropertyMap> type;
    };
Tiago Peixoto's avatar
Tiago Peixoto committed
177
178
};

179
struct selectors:
180
    boost::mpl::vector<out_degreeS, in_degreeS, total_degreeS> {};
Tiago Peixoto's avatar
Tiago Peixoto committed
181

182
// retrieves the appropriate degree selector
183
boost::any degree_selector(GraphInterface::deg_t deg);
Tiago Peixoto's avatar
Tiago Peixoto committed
184

185
186
187
188
189
190
191
192
193
194
195
196
// helper types for in_edge_iteratorS

template <class Graph, class IsDirected>
struct get_in_edges
{
    BOOST_MPL_ASSERT((is_same<IsDirected,boost::true_type>));
    BOOST_MPL_ASSERT((is_convertible
                      <typename graph_traits<Graph>::directed_category,
                      boost::directed_tag>));
    typedef typename graph_traits<Graph>::vertex_descriptor
        vertex_descriptor;
    typedef typename graph_traits<Graph>::in_edge_iterator type;
197
198
    static std::pair<type,type> get_edges(vertex_descriptor v,
                                          const Graph& g)
199
200
    {
        using namespace boost;
201
        return in_edges(v, g);
202
203
204
205
206
207
208
209
210
211
212
213
    }
};

template <class Graph>
struct get_in_edges<Graph,boost::false_type>
{
    BOOST_MPL_ASSERT((is_convertible
                      <typename graph_traits<Graph>::directed_category,
                      boost::undirected_tag>));
    typedef typename graph_traits<Graph>::vertex_descriptor
        vertex_descriptor;
    typedef typename graph_traits<Graph>::out_edge_iterator type;
214
215
    static std::pair<type,type> get_edges(vertex_descriptor v,
                                          const Graph& g)
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    {
        return make_pair(type(), type());
    }
};

// this in-edge iterator selector returns the in-edge range for directed graphs
// and an empty out-edge range for undirected graphs. The iterator type is given
// by in_edge_iteratorS<Graph>::type.
template <class Graph>
struct in_edge_iteratorS
{
    typedef typename boost::graph_traits<Graph>::directed_category
        directed_category;
    typedef typename is_convertible<directed_category,
                                    boost::directed_tag>::type is_directed;
    typedef typename get_in_edges<Graph,is_directed>::type type;

    typedef typename boost::graph_traits<Graph>::vertex_descriptor
        vertex_descriptor;
235
236
    static std::pair<type,type> get_edges(vertex_descriptor v,
                                          const Graph& g)
237
    {
238
        return get_in_edges<Graph,is_directed>::get_edges(v, g);
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// helper types for all_edges_iteratorS
template <class Graph, class IsDirected>
struct get_all_edges
{
    BOOST_MPL_ASSERT((is_same<IsDirected,boost::true_type>));
    BOOST_MPL_ASSERT((is_convertible
                      <typename graph_traits<Graph>::directed_category,
                      boost::directed_tag>));
    typedef typename graph_traits<Graph>::vertex_descriptor
        vertex_descriptor;
    typedef typename graph_traits<UndirectedAdaptor<Graph> >::out_edge_iterator
        type;
    static std::pair<type,type> get_edges(vertex_descriptor v,
                                          const Graph& g)
    {
        using namespace boost;
        const UndirectedAdaptor<Graph> ug(g);
        return out_edges(v, ug);
    }
};

template <class Graph>
struct get_all_edges<Graph,boost::false_type>
{
    BOOST_MPL_ASSERT((is_convertible
                      <typename graph_traits<Graph>::directed_category,
                      boost::undirected_tag>));
    typedef typename graph_traits<Graph>::vertex_descriptor
        vertex_descriptor;
    typedef typename graph_traits<Graph>::out_edge_iterator type;
    static std::pair<type,type> get_edges(vertex_descriptor v,
                                          const Graph& g)
    {
        using namespace boost;
        return out_edges(v, g);
    }
};

// this "all edges" iterator selector returns the in-edge + out-edge ranges for
// directed graphs and the out-edge range for undirected graphs. The
// iterator type is given by all_edges_iteratorS<Graph>::type.
template <class Graph>
struct all_edges_iteratorS
{
    typedef typename boost::graph_traits<Graph>::directed_category
        directed_category;
    typedef typename is_convertible<directed_category,
                                    boost::directed_tag>::type is_directed;
    typedef typename get_all_edges<Graph,is_directed>::type type;

    typedef typename boost::graph_traits<Graph>::vertex_descriptor
        vertex_descriptor;
    static std::pair<type,type> get_edges(vertex_descriptor v,
                                          const Graph& g)
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
297
        return get_all_edges<Graph,is_directed>::get_edges(v, g);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    }
};

// helper types for in_or_out_edge_iteratorS
template <class Graph, class IsDirected>
struct get_in_or_out_edges
    : public get_in_edges<Graph,IsDirected>
{};

template <class Graph>
struct get_in_or_out_edges<Graph,boost::false_type>
    : public get_all_edges<Graph,boost::false_type>
{};

// this "in or out" iterator selector returns the in-edge range for directed
// graphs and the out-edge range for undirected graphs. The iterator type is
// given by in_or_out_edges_iteratorS<Graph>::type
template <class Graph>
struct in_or_out_edge_iteratorS
{
    typedef typename boost::graph_traits<Graph>::directed_category
        directed_category;
    typedef typename is_convertible<directed_category,
                                    boost::directed_tag>::type is_directed;
    typedef typename get_in_or_out_edges<Graph,is_directed>::type type;

    typedef typename boost::graph_traits<Graph>::vertex_descriptor
        vertex_descriptor;
    static std::pair<type,type> get_edges(vertex_descriptor v,
                                          const Graph& g)
    {
        return get_in_or_out_edges<Graph,is_directed>::get_edges(v, g);
    }
};

333
334
335
336
337
// useful type lists

typedef mpl::vector<in_degreeS, out_degreeS, total_degreeS>
    degree_selectors;

338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
typedef property_map_types::apply<value_types,
                                  GraphInterface::vertex_index_map_t>::type
    vertex_properties;
typedef property_map_types::apply<value_types,
                                  GraphInterface::vertex_index_map_t,
                                  mpl::bool_<false> >::type
    writable_vertex_properties;

typedef property_map_types::apply<value_types,
                                  GraphInterface::edge_index_map_t>::type
    edge_properties;
typedef property_map_types::apply<value_types,
                                  GraphInterface::edge_index_map_t,
                                  mpl::bool_<false> >::type
    writable_edge_properties;

354
355
356
typedef property_map_types::apply<scalar_types,
                                  GraphInterface::vertex_index_map_t>::type
    vertex_scalar_properties;
357
358
359
360
typedef property_map_types::apply<scalar_types,
                                  GraphInterface::vertex_index_map_t,
                                  mpl::bool_<false> >::type
    writable_vertex_scalar_properties;
361
362
363
364
365
366
367
368
369
370
371

typedef property_map_types::apply<integer_types,
                                  GraphInterface::vertex_index_map_t>::type
    vertex_integer_properties;

typedef property_map_types::apply<floating_types,
                                  GraphInterface::vertex_index_map_t,
                                  mpl::bool_<false> >::type
    vertex_floating_properties;

typedef property_map_types::apply<scalar_vector_types,
372
373
                                  GraphInterface::vertex_index_map_t,
                                  mpl::bool_<false> >::type
374
375
376
    vertex_scalar_vector_properties;

typedef property_map_types::apply<integer_vector_types,
377
378
                                  GraphInterface::vertex_index_map_t,
                                  mpl::bool_<false> >::type
379
380
381
382
383
384
385
386
387
388
389
    vertex_integer_vector_properties;

typedef property_map_types::apply<floating_vector_types,
                                  GraphInterface::vertex_index_map_t,
                                  mpl::bool_<false> >::type
    vertex_floating_vector_properties;

typedef property_map_types::apply<scalar_types,
                                  GraphInterface::edge_index_map_t>::type
    edge_scalar_properties;

390
391
392
393
394
typedef property_map_types::apply<scalar_types,
                                  GraphInterface::edge_index_map_t,
                                  mpl::bool_<false> >::type
    writable_edge_scalar_properties;

395
396
397
398
399
400
401
402
403
404
typedef property_map_types::apply<integer_types,
                                  GraphInterface::edge_index_map_t>::type
    edge_integer_properties;

typedef property_map_types::apply<floating_types,
                                  GraphInterface::edge_index_map_t,
                                  mpl::bool_<false> >::type
    edge_floating_properties;

typedef property_map_types::apply<scalar_vector_types,
405
406
                                  GraphInterface::edge_index_map_t,
                                  mpl::bool_<false> >::type
407
408
409
    edge_scalar_vector_properties;

typedef property_map_types::apply<integer_vector_types,
410
411
                                  GraphInterface::edge_index_map_t,
                                  mpl::bool_<false> >::type
412
413
414
415
416
417
418
419
420
421
    edge_integer_vector_properties;

typedef property_map_types::apply<floating_vector_types,
                                  GraphInterface::edge_index_map_t,
                                  mpl::bool_<false> >::type
    edge_floating_vector_properties;

struct vertex_scalar_selectors:
    mpl::transform<vertex_scalar_properties,
                   scalar_selector_type>::type {};
422

423
struct all_selectors:
424
425
426
427
428
    mpl::transform<vertex_properties,
                   scalar_selector_type,
                   mpl::back_inserter<degree_selectors> >::type {};

struct scalar_selectors:
429
430
431
    mpl::transform<vertex_scalar_properties,
                   scalar_selector_type,
                   mpl::back_inserter<degree_selectors> >::type {};
432

Tiago Peixoto's avatar
Tiago Peixoto committed
433
434
435
} //namespace graph_tool

#endif