graph_filtering.hh 23.5 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-2012 Tiago de Paula Peixoto <tiago@skewed.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

#ifndef FILTERING_HH
#define FILTERING_HH

21
#include "graph.hh"
22
#include <boost/version.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
23
24
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/filtered_graph.hpp>
25
26
27
28
29
#if (BOOST_VERSION / 100 % 1000 >= 48)
    #include <boost/graph/reverse_graph_alt.hpp>
#else
    #include <boost/graph/reverse_graph.hpp>
#endif
Tiago Peixoto's avatar
Tiago Peixoto committed
30
#include <boost/mpl/vector.hpp>
31
32
#include <boost/mpl/erase.hpp>
#include <boost/mpl/clear.hpp>
Tiago Peixoto's avatar
Tiago Peixoto committed
33
34
35
36
37
38
#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>
39
40
41
42
43
44
45
46
47
48
#include <boost/mpl/inserter.hpp>
#include <boost/mpl/insert_range.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/divides.hpp>
#include <boost/mpl/arithmetic.hpp>
#include <boost/mpl/greater_equal.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/transform_view.hpp>
#include <boost/mpl/quote.hpp>
49
#include <boost/mpl/range_c.hpp>
50

Tiago Peixoto's avatar
Tiago Peixoto committed
51
52
#include "graph_adaptor.hh"
#include "graph_selectors.hh"
53
#include "graph_util.hh"
54
#include "graph_wrap.hh"
55
#include "mpl_nested_loop.hh"
56

57
namespace graph_tool
Tiago Peixoto's avatar
   
Tiago Peixoto committed
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
using namespace boost;

// Graph filtering
// ---------------
//
// We want to generate versions of a template algorithm for every possible type
// of graph views. The types of graph views are the following:
//
//    - The original directed multigraph
//
//    - Filtered graphs, based on MaskFilter below. This amounts to a
//      filtered_graph for every combination of filtered and unfiltered vertex
//      or edge, i.e., 3.
//
//    - A reversed view of each directed graph (original + filtered)
//
//    - An undirected view of each directed (unreversed) graph (original +
//      filtered)
//
// The total number of graph views is then: 3 * 4 = 12
//
// The specific specialization can be called at run time (and generated at
// compile time) with the run_action() function, which takes as arguments the
// GraphInterface worked on, and the template functor to be specialized, which
// must take as first argument a pointer to a graph type, which itself must be a
// template parameter. Additionally, the function can be called optionally with
// up to 4 extra arguments, which must be MPL sequence instances corresponding
// to the type range of the extra arguments which must be passed to the template
// functor. The run_action() will not run the algorithm itself, but will instead
// return a functor (graph_action) which must be called either with no
// arguments, or with parameters of type boost::any which hold internally the
// type and values of the extra parameters to be passed to the action functor,
// and which will define the specialization to be chosen.
//
// Example:
//
// struct my_algorithm
// {
//     template <class Graph, class ValueType>
99
//     void operator()(Graph& g, ValueType val) const
100
101
102
103
104
105
106
//     {
//         ... do something ...
//     }
// };
//
// ...
//
107
// GraphInterface g;
108
109
// typedef mpl::vector<int, double, string> value_types;
// double foo = 42.0;
110
// run_action(g, my_algorithm(), value_types)(boost::any(foo));
111
112
113
114
//
// The above line will run my_algorithm::operator() with Graph being the
// appropriate graph view type and ValueType being 'double' and val = 42.0.

115
116
117
118
// Whenever no implementation is called, the following exception is thrown
class ActionNotFound: public GraphException
{
public:
119
120
    ActionNotFound(const boost::any& graph_view, const std::type_info& action,
                   const vector<const std::type_info*>& args);
121
122
123
124
    virtual const char * what () const throw ();
    virtual ~ActionNotFound() throw () {}
private:
    boost::any _graph_view;
125
126
    const std::type_info& _action;
    vector<const std::type_info*> _args;
127
};
128
129

namespace detail
130
{
131
132
133
134
135
136

// Implementation
// --------------
//
// The class MaskFilter below is the main filter predicate for the filtered
// graph view, based on descriptor property maps.  It filters out edges or
137
138
// vertices which are masked according to a property map with bool (actually
// uint8_t) value type.
139
140
141

template <class DescriptorProperty>
class MaskFilter
142
{
143
144
145
146
147
public:
    typedef typename property_traits<DescriptorProperty>::value_type value_t;
    MaskFilter(){}
    MaskFilter(DescriptorProperty filtered_property, bool invert)
        : _filtered_property(filtered_property), _invert(invert) {}
148

149
    template <class Descriptor>
150
    __attribute__((always_inline)) inline bool operator() (Descriptor d) const
151
152
    {
        // ignore if masked
Tiago Peixoto's avatar
   
Tiago Peixoto committed
153

154
        return get(_filtered_property, d) ^ _invert;
155

156
157
158
159
160
        // TODO: This is a critical section. It will be called for every vertex
        //       or edge in the graph, every time they're iterated
        //       through. Therefore, it must be guaranteed this is as optimized
        //       as possible.
    }
161

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
private:
    DescriptorProperty _filtered_property;
    bool _invert;
};


// Metaprogramming
// ---------------
//
// We need to generate a type sequence with all the filtered graph views, which
// will be called all_graph_views.

// metafunction class to get the correct filter predicate
template <class Property>
struct get_predicate
Tiago Peixoto's avatar
Tiago Peixoto committed
177
{
178
179
    typedef MaskFilter<Property> type;
};
Tiago Peixoto's avatar
Tiago Peixoto committed
180

181
182
template <>
struct get_predicate<keep_all>
Tiago Peixoto's avatar
Tiago Peixoto committed
183
{
184
    typedef keep_all type;
Tiago Peixoto's avatar
Tiago Peixoto committed
185
186
};

187
188
// metafunction to get the filtered graph type
struct graph_filter
Tiago Peixoto's avatar
Tiago Peixoto committed
189
{
190
191
192
    template <class Graph, class EdgeProperty, class VertexProperty>
    struct apply
    {
193

194
195
196
197
198
        typedef typename get_predicate<EdgeProperty>::type edge_predicate;
        typedef typename get_predicate<VertexProperty>::type vertex_predicate;

        typedef filtered_graph<Graph,
                               edge_predicate,
Tiago Peixoto's avatar
Tiago Peixoto committed
199
                               vertex_predicate> filtered_graph_t;
200
201
202
203
204
205
206
207

        // If both predicates are keep_all, then return the original graph
        // type. Otherwise return the filtered_graph type.
        typedef typename mpl::if_<
            typename mpl::and_<
                is_same<edge_predicate,
                        keep_all>,
                is_same<vertex_predicate,
208
                        keep_all>
209
210
                >::type,
            Graph,
Tiago Peixoto's avatar
Tiago Peixoto committed
211
            filtered_graph_t>::type type;
212
    };
Tiago Peixoto's avatar
Tiago Peixoto committed
213
214
};

215
216
// metafunction to get the undirected graph type
struct graph_undirect
Tiago Peixoto's avatar
Tiago Peixoto committed
217
218
{
    template <class Graph>
219
    struct apply
Tiago Peixoto's avatar
Tiago Peixoto committed
220
    {
221
222
        typedef UndirectedAdaptor<Graph> type;
    };
Tiago Peixoto's avatar
Tiago Peixoto committed
223
224
};

225
226
// metafunction to get the reversed graph type
struct graph_reverse
Tiago Peixoto's avatar
Tiago Peixoto committed
227
{
228
    template <class Graph>
229
230
231
232
    struct apply
    {
        typedef reverse_graph<Graph> type;
    };
Tiago Peixoto's avatar
Tiago Peixoto committed
233
234
};

235
236
237
238
239
240
241
242
243
244
// this wraps an unary metafunction class Bind into a unary metafunction,
// i.e., it is an identity operation. I'm not sure why it's necessary, but
// using pure unary bind expressions below didn't work for me, and this
// fixed it.
template <class Bind>
struct bind_wrap1
{
    template <class T1> struct apply
    { typedef typename Bind::template apply<T1>::type type; };
};
245

246
247
248
// metafunction which returns a mpl::vector containing all the pair combinations
// of two given type sequences
struct get_all_pairs
Tiago Peixoto's avatar
Tiago Peixoto committed
249
{
250
    struct make_pair
Tiago Peixoto's avatar
Tiago Peixoto committed
251
    {
252
253
254
255
256
257
        template <class T1, class T2>
        struct apply
        {
            typedef mpl::pair<T1,T2> type;
        };
    };
258

259
260
    template <class TR1, class TR2>
    struct apply
261
    {
262
        struct get_second_types
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
            template <class T1>
            struct apply
            {
                typedef typename mpl::transform<
                    TR2,
                    bind_wrap1<
                        mpl::bind2<make_pair, T1, mpl::_1>
                    > >::type type;
            };
        };

        typedef typename mpl::transform<
            TR1,
            get_second_types,
            mpl::back_inserter<mpl::vector<> >
            >::type pair_combinations; // nested sequence (vector of vectors) of
                                       // pair combinations

        // joins two sequences
        struct join
        {
            template <class Seq1, class Seq2>
            struct apply
            {
                typedef typename boost::mpl::copy<
                    Seq2,
                    boost::mpl::back_inserter<Seq1>
                    >::type type;
            };
        };

        // flattens a nested sequence
        template<class Sequence>
        struct flatten
        {
            typedef typename boost::mpl::fold<
                Sequence,
                typename boost::mpl::clear<Sequence>::type,
                join
                >::type type;
        };

        // the complete list of combinations
        typedef typename flatten<pair_combinations>::type type;
    };
};

// metafunction class to get the correct property map type
template <class Scalar, class IndexMap>
struct get_property_map_type
{
315
316
    typedef typename property_map_type::apply<Scalar, IndexMap>
        ::type::unchecked_t type;
317
318
319
320
321
322
323
};

template <class IndexMap>
struct get_property_map_type<keep_all, IndexMap>
{
    typedef keep_all type;
};
324

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// this metafunction returns a filtered graph type, given the scalar types to be
// used in the property maps
struct get_graph_filtered
{
    template <class TypePair>
    struct apply
    {
        typedef typename TypePair::first edge_scalar;
        typedef typename TypePair::second vertex_scalar;

        // if the 'scalar' is the index map itself, use simply that, otherwise
        // get the specific property map
        typedef typename mpl::if_<
            is_same<edge_scalar,
                    GraphInterface::edge_index_map_t>,
            GraphInterface::edge_index_map_t,
            typename get_property_map_type<
                edge_scalar,
                GraphInterface::edge_index_map_t>::type
            >::type edge_property_map;

        typedef typename mpl::if_<
            is_same<vertex_scalar,
                    GraphInterface::vertex_index_map_t>,
            GraphInterface::vertex_index_map_t,
            typename get_property_map_type<
                vertex_scalar,
                GraphInterface::vertex_index_map_t>::type
            >::type vertex_property_map;

        typedef typename graph_filter::apply<GraphInterface::multigraph_t,
                                             edge_property_map,
                                             vertex_property_map>::type type;
358
    };
359
};
360

361
362
363
// this metafunction returns all the possible graph views
struct get_all_graph_views
{
364
    template <class TypePairs,
365
366
367
368
369
370
              class AlwaysDirected = mpl::bool_<false>,
              class NeverDirected = mpl::bool_<false>,
              class AlwaysReversed = mpl::bool_<false>,
              class NeverReversed = mpl::bool_<false>,
              class NeverFiltered = mpl::bool_<false> >
    struct apply
371
    {
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
        // filtered graphs
        struct filtered_graphs:
            mpl::if_
            <NeverFiltered,
             mpl::vector<GraphInterface::multigraph_t>,
             typename mpl::transform<TypePairs,
                                     get_graph_filtered>::type>::type {};

        // filtered + reversed graphs
        struct reversed_graphs:
            mpl::if_<AlwaysReversed,
                     typename mpl::transform<filtered_graphs,
                                             graph_reverse>::type,
                     typename mpl::if_<
                         NeverReversed,
                         filtered_graphs,
                         typename mpl::transform<
                             filtered_graphs,
                             graph_reverse,
                             mpl::back_inserter<filtered_graphs>
                             >::type
                         >::type
                >::type {};

        // undirected + filtereed + reversed graphs
        struct undirected_graphs:
            mpl::if_<AlwaysDirected,
399
                     reversed_graphs,
400
401
402
403
404
405
406
407
408
409
410
411
412
                     typename mpl::if_<
                         NeverDirected,
                         typename mpl::transform<filtered_graphs,
                                                 graph_undirect>::type,
                         typename mpl::transform<
                             filtered_graphs,
                             graph_undirect,
                             mpl::back_inserter<reversed_graphs>
                             >::type
                         >::type
                     >::type {};
        typedef undirected_graphs type;
    };
Tiago Peixoto's avatar
Tiago Peixoto committed
413
414
};

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// useful metafunction to split sequences in half
struct split
{
    template <class Sequence>
    struct get_element
    {
        template <class Index>
        struct apply
        {
            typedef typename mpl::at<Sequence,Index>::type type;
        };
    };

    template <class Sequence>
    struct apply
    {
        typedef typename mpl::size<Sequence>::type size;
        typedef typename mpl::divides<size, mpl::int_<2> >::type half_size;
        typedef typename mpl::transform<mpl::range_c<int, 0, half_size::value>,
                                        get_element<Sequence>,
                                        mpl::back_inserter<mpl::vector<> > >
            ::type first_part;
        typedef typename mpl::transform<mpl::range_c<int, half_size::value,
                                                     size::value>,
                                        get_element<Sequence>,
                                        mpl::back_inserter<mpl::vector<> > >
            ::type second_part;
        typedef typename mpl::pair<first_part,second_part> type;
    };
};



448
449
// all scalar types plus edge and vertex index property (we actually only use
// bool)
450

451
452
#ifndef NO_GRAPH_FILTERING
struct edge_scalars:
453
    mpl::vector<keep_all, uint8_t> {};
454
struct vertex_scalars:
455
    mpl::vector<keep_all, uint8_t> {};
456
457
458
459
460
461
#else
struct edge_scalars:
    mpl::vector<keep_all> {};
struct vertex_scalars:
    mpl::vector<keep_all> {};
#endif
462

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// all scalar pairs
struct scalar_pairs: get_all_pairs::apply<edge_scalars,vertex_scalars>::type {};

// finally, this type should hold all the possible graph views
struct all_graph_views:
    get_all_graph_views::apply<scalar_pairs>::type {};

// restricted graph views
struct always_directed:
    get_all_graph_views::apply<scalar_pairs,mpl::bool_<true> >::type {};

struct never_directed:
    get_all_graph_views::apply<scalar_pairs,mpl::bool_<false>,
                               mpl::bool_<true> >::type {};

struct always_reversed:
    get_all_graph_views::apply<scalar_pairs,mpl::bool_<true>,
                               mpl::bool_<false>,mpl::bool_<true> >::type {};

struct never_reversed:
483
    get_all_graph_views::apply<scalar_pairs,mpl::bool_<false>,
484
485
                               mpl::bool_<false>,mpl::bool_<false>,
                               mpl::bool_<true> >::type {};
486
487
488
489
490
491

struct always_directed_never_reversed:
    get_all_graph_views::apply<scalar_pairs,mpl::bool_<true>,
                               mpl::bool_<false>,mpl::bool_<false>,
                               mpl::bool_<true> >::type {};

492
493
494
495
496
497
498
499
500
501
502
503
struct never_filtered:
    get_all_graph_views::apply<scalar_pairs,mpl::bool_<false>,
                               mpl::bool_<false>,mpl::bool_<false>,
                               mpl::bool_<false>,mpl::bool_<true> >::type {};

// sanity check
typedef mpl::size<all_graph_views>::type n_views;
#ifndef NO_GRAPH_FILTERING
BOOST_MPL_ASSERT_RELATION(n_views::value, == , mpl::int_<12>::value);
#else
BOOST_MPL_ASSERT_RELATION(n_views::value, == , mpl::int_<3>::value);
#endif
Tiago Peixoto's avatar
Tiago Peixoto committed
504

505
506
// run_action() implementation
// ===========================
507
508
509

// wrap action to be called, to deal with property maps, i.e., return version
// with no bounds checking.
510
template <class Action, class Wrap>
511
512
struct action_wrap
{
513
514
    action_wrap(Action a, GraphInterface& g, size_t max_v, size_t max_e)
        : _a(a), _g(g), _max_v(max_v), _max_e(max_e) {}
515
516

    template <class Type>
Tiago Peixoto's avatar
Tiago Peixoto committed
517
    checked_vector_property_map<Type,GraphInterface::vertex_index_map_t>
518
    uncheck(checked_vector_property_map
Tiago Peixoto's avatar
Tiago Peixoto committed
519
520
521
522
523
524
525
526
527
            <Type,GraphInterface::vertex_index_map_t> a, mpl::true_) const
    {
        return a;
    }

    template <class Type>
    unchecked_vector_property_map<Type,GraphInterface::vertex_index_map_t>
    uncheck(checked_vector_property_map
            <Type,GraphInterface::vertex_index_map_t> a, mpl::false_) const
528
529
530
531
532
    {
        return a.get_unchecked(_max_v);
    }

    template <class Type>
Tiago Peixoto's avatar
Tiago Peixoto committed
533
    checked_vector_property_map<Type,GraphInterface::edge_index_map_t>
534
    uncheck(checked_vector_property_map
Tiago Peixoto's avatar
Tiago Peixoto committed
535
536
537
538
539
540
541
542
543
            <Type,GraphInterface::edge_index_map_t> a, mpl::true_) const
    {
        return a;
    }

    template <class Type>
    unchecked_vector_property_map<Type,GraphInterface::edge_index_map_t>
    uncheck(checked_vector_property_map
            <Type,GraphInterface::edge_index_map_t> a, mpl::false_) const
544
545
546
547
548
549
    {
        return a.get_unchecked(_max_e);
    }

    template <class Type>
    scalarS<typename Type::unchecked_t>
Tiago Peixoto's avatar
Tiago Peixoto committed
550
    uncheck(scalarS<Type> a, mpl::false_) const
551
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
552
553
        return scalarS<typename Type::unchecked_t>(uncheck(a._pmap,
                                                           mpl::false_()));
554
555
556
    }

    //no op
Tiago Peixoto's avatar
Tiago Peixoto committed
557
558
    template <class Type, class DoWrap>
    Type uncheck(Type a, DoWrap) const { return a; }
559

560
561
562
563
564
565
566
567
568
569
570
571
    template <class Graph>
    GraphWrap<Graph> wrap(Graph* g, mpl::true_) const
    {
        return graph_wrap(*g, _g);
    }

    template <class Graph>
    Graph& wrap(Graph* g, mpl::false_) const
    {
        return *g;
    }

572
    void operator()() const {};
Tiago Peixoto's avatar
Tiago Peixoto committed
573
574
    template <class T1> void operator()(const T1& a1) const
    { _a(wrap(a1, Wrap())); }
575
576
    template <class T1, class T2>
    void operator()(const T1& a1, const T2& a2) const
Tiago Peixoto's avatar
Tiago Peixoto committed
577
    { _a(wrap(a1,Wrap()), uncheck(a2, Wrap())); }
578
579
    template <class T1, class T2, class T3>
    void operator()(const T1& a1, const T2& a2, const T3& a3) const
Tiago Peixoto's avatar
Tiago Peixoto committed
580
    { _a(wrap(a1,Wrap()), uncheck(a2, Wrap()), uncheck(a3, Wrap()));}
581
582
583
    template <class T1, class T2, class T3, class T4>
    void operator()(const T1& a1, const T2& a2, const T3& a3, const T4& a4)
        const
Tiago Peixoto's avatar
Tiago Peixoto committed
584
585
    { _a(wrap(a1,Wrap()), uncheck(a2, Wrap()), uncheck(a3, Wrap()),
         uncheck(a4, Wrap())); }
586
587
588
    template <class T1, class T2, class T3, class T4, class T5>
    void operator()(const T1& a1, const T2& a2, const T3& a3, const T4& a4,
                    const T5& a5) const
Tiago Peixoto's avatar
Tiago Peixoto committed
589
    { _a(wrap(a1,Wrap()), uncheck(a2, Wrap()), uncheck(a3, Wrap()),
590
         uncheck(a4, Wrap()), uncheck(a5, Wrap())); }
591
592

    Action _a;
593
    reference_wrapper<GraphInterface> _g;
594
595
596
    size_t _max_v, _max_e;
};

597
598
// this functor encapsulates another functor Action, which takes a pointer to a
// graph view as first argument
599
600
template <class Action, class GraphViews, class Wrap, class TR1, class TR2,
          class TR3, class TR4 >
601
struct graph_action
Tiago Peixoto's avatar
Tiago Peixoto committed
602
{
603
604
605
    struct graph_view_pointers:
        mpl::transform<GraphViews, mpl::quote1<add_pointer> >::type {};

606
    graph_action(GraphInterface& g, Action a)
Tiago Peixoto's avatar
Tiago Peixoto committed
607
608
        : _g(g), _a(a, g, num_vertices(g._state->_mg),
                    g._state->_max_edge_index + 1) {}
Tiago Peixoto's avatar
Tiago Peixoto committed
609

610
    void operator()() const
Tiago Peixoto's avatar
Tiago Peixoto committed
611
    {
612
613
614
615
616
        bool found = false;
        boost::any gview = _g.GetGraphView();
        boost::mpl::for_each<graph_view_pointers>
            (boost::mpl::select_types(_a, found, gview));
        if (!found)
617
        {
618
            throw ActionNotFound(gview, typeid(Action),
619
                                 vector<const std::type_info*>());
620
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
621
622
    }

623
    void operator()(boost::any a1) const
624
    {
625
626
627
628
629
        bool found = false;
        boost::any gview = _g.GetGraphView();
        boost::mpl::nested_for_each<graph_view_pointers,TR1>()
            (boost::mpl::select_types(_a, found, gview, a1));
        if (!found)
630
        {
631
            vector<const std::type_info*> args;
632
            args.push_back(&a1.type());
633
            throw ActionNotFound(gview, typeid(Action), args);
634
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
635
636
    }

637
638
639
640
641
642
643
644
    void operator()(boost::any a1, boost::any a2) const
    {
        bool found = false;
        boost::any gview = _g.GetGraphView();
        boost::mpl::nested_for_each<graph_view_pointers,TR1,TR2>()
            (boost::mpl::select_types(_a, found, gview, a1, a2));
        if (!found)
        {
645
            vector<const std::type_info*> args;
646
647
            args.push_back(&a1.type());
            args.push_back(&a2.type());
648
649
650
            throw ActionNotFound(gview, typeid(Action), args);
        }
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
651

652
    void operator()(boost::any a1, boost::any a2, boost::any a3) const
Tiago Peixoto's avatar
Tiago Peixoto committed
653
    {
654
655
656
657
658
659
        bool found = false;
        boost::any gview = _g.GetGraphView();
        boost::mpl::nested_for_each<graph_view_pointers,TR1,TR2,TR3>()
            (boost::mpl::select_types(_a, found, gview, a1, a2, a3));
        if (!found)
        {
660
            vector<const std::type_info*> args;
661
662
663
            args.push_back(&a1.type());
            args.push_back(&a2.type());
            args.push_back(&a3.type());
664
665
            throw ActionNotFound(gview, typeid(Action), args);
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
666
667
    }

668
669
    void operator()(boost::any a1, boost::any a2, boost::any a3,
                    boost::any a4) const
670
    {
671
672
673
674
675
        bool found = false;
        boost::any gview = _g.GetGraphView();
        boost::mpl::nested_for_each<graph_view_pointers,TR1,TR2,TR3,TR4>()
            (boost::mpl::select_types(_a, found, gview, a1, a2, a3,a4));
        if (!found)
676
        {
677
            vector<const std::type_info*> args;
678
679
680
681
            args.push_back(&a1.type());
            args.push_back(&a2.type());
            args.push_back(&a3.type());
            args.push_back(&a4.type());
682
            throw ActionNotFound(gview, typeid(Action), args);
683
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
684
685
    }

686
    const GraphInterface &_g;
687
    action_wrap<Action, Wrap> _a;
Tiago Peixoto's avatar
Tiago Peixoto committed
688
};
689
} // details namespace
Tiago Peixoto's avatar
Tiago Peixoto committed
690

691

692
// all definitions of run_action with different arity
693
template <class GraphViews = detail::all_graph_views, class Wrap = mpl::false_>
694
struct run_action
Tiago Peixoto's avatar
Tiago Peixoto committed
695
{
696
    template <class Action>
697
698
    detail::graph_action<Action,GraphViews,Wrap>
    operator()(GraphInterface &g, Action a)
699
    {
700
        return detail::graph_action<Action,GraphViews,Wrap>(g, a);
701
    }
702
703

    template <class Action, class TR1>
704
705
    detail::graph_action<Action,GraphViews,Wrap,TR1>
    operator()(GraphInterface &g, Action a, TR1)
706
    {
707
        return detail::graph_action<Action,GraphViews,Wrap,TR1>(g, a);
708
    }
Tiago Peixoto's avatar
Tiago Peixoto committed
709

710
    template <class Action, class TR1, class TR2>
711
712
    detail::graph_action<Action,GraphViews,Wrap,TR1,TR2>
    operator()(GraphInterface &g, Action a, TR1, TR2)
713
    {
714
        return detail::graph_action<Action,GraphViews,Wrap,TR1,TR2>(g, a);
715
    }
716

717
    template <class Action, class TR1, class TR2, class TR3>
718
719
    detail::graph_action<Action,GraphViews,Wrap,TR1,TR2,TR3>
    operator()(GraphInterface &g, Action a, TR1, TR2, TR3)
720
    {
721
        return detail::graph_action<Action,GraphViews,Wrap,TR1,TR2,TR3>(g, a);
722
    }
723

724
    template <class Action, class TR1, class TR2, class TR3, class TR4>
725
726
    detail::graph_action<Action,GraphViews,Wrap,TR1,TR2,TR3,TR4>
    operator()(GraphInterface &g, Action a, TR1, TR2, TR3, TR4)
727
    {
728
        return detail::graph_action<Action,GraphViews,Wrap,TR1,TR2,TR3,TR4>(g, a);
729
730
    }
};
731

732
733
734
// returns true if graph filtering was enabled at compile time
bool graph_filtering_enabled();

735
} //graph_tool namespace
736

Tiago Peixoto's avatar
   
Tiago Peixoto committed
737
#endif // FILTERING_HH