graph_filtered.hh 23.5 KB
Newer Older
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2020 Tiago de Paula Peixoto <tiago@skewed.de>
4
//
5
6
7
8
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
9
//
10
11
12
13
// 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 Lesser General Public License for more
// details.
14
//
15
// You should have received a copy of the GNU Lesser General Public License
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
// along with this program. If not, see <http://www.gnu.org/licenses/>.

//=======================================================================
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#ifndef GRAPH_FILTERED_HH
#define GRAPH_FILTERED_HH

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <boost/iterator/filter_iterator.hpp>
#include <boost/graph/filtered_graph.hpp> // for predicate classes

namespace boost {

namespace detail {

    template <typename EdgePredicate, typename VertexPredicate, typename Graph>
    struct out_edge_pred
    {
        out_edge_pred() { }
        out_edge_pred(EdgePredicate ep, VertexPredicate vp,
                      const Graph& g)
            : _edge_pred(ep), _vertex_pred(vp), _g(&g) { }

        template <typename Edge>
        bool operator()(const Edge& e) const
        {
            return _edge_pred(e) && _vertex_pred(target(e, *_g));
        }
        EdgePredicate _edge_pred;
        VertexPredicate _vertex_pred;
        const Graph* _g;
    };

    template <typename EdgePredicate, typename VertexPredicate, typename Graph>
    struct in_edge_pred
    {
        in_edge_pred() { }
        in_edge_pred(EdgePredicate ep, VertexPredicate vp,
                     const Graph& g)
            : _edge_pred(ep), _vertex_pred(vp), _g(&g) { }

        template <typename Edge>
        bool operator()(const Edge& e) const
        {
            return _edge_pred(e) && _vertex_pred(source(e, *_g));
        }
        EdgePredicate _edge_pred;
        VertexPredicate _vertex_pred;
        const Graph* _g;
    };

    template <typename EdgePredicate, typename VertexPredicate, typename Graph>
    struct edge_pred
    {
        edge_pred() { }
        edge_pred(EdgePredicate ep, VertexPredicate vp,
                  const Graph& g)
            : _edge_pred(ep), _vertex_pred(vp), _g(&g) { }

        template <typename Edge>
        bool operator()(const Edge& e) const
        {
            return _edge_pred(e)
                && _vertex_pred(source(e, *_g)) && _vertex_pred(target(e, *_g));
        }
        EdgePredicate _edge_pred;
        VertexPredicate _vertex_pred;
        const Graph* _g;
    };

} // namespace detail


//===========================================================================
// Filtered Graph

struct filt_graph_tag { };

// This base class is a stupid hack to change overload resolution
// rules for the source and target functions so that they are a
// worse match than the source and target functions defined for
// pairs in graph_traits.hpp. I feel dirty. -JGS
template <class G>
struct filt_graph_base
{
  typedef graph_traits<G> Traits;
  typedef typename Traits::vertex_descriptor          vertex_descriptor;
  typedef typename Traits::edge_descriptor            edge_descriptor;
  filt_graph_base(const G& g) : _g(g) { }
  //protected:
  const G& _g;
};

template <typename Graph,
          typename EdgePredicate,
          typename VertexPredicate = keep_all>
class filt_graph : public filt_graph_base<Graph>
{
    typedef filt_graph_base<Graph> Base;
    typedef graph_traits<Graph> Traits;
    typedef filt_graph self;
public:
    typedef Graph graph_type;
    typedef detail::out_edge_pred<EdgePredicate, VertexPredicate,
                                  Graph> OutEdgePred;
    typedef detail::in_edge_pred<EdgePredicate, VertexPredicate,
                                 Graph> InEdgePred;
    typedef detail::edge_pred<EdgePredicate, VertexPredicate, Graph>
        EdgePred;

    // Constructors
    filt_graph(const Graph& g, EdgePredicate ep)
        : Base(g), _edge_pred(ep),
          _out_edge_pred(_edge_pred, _vertex_pred, g),
          _in_edge_pred(_edge_pred, _vertex_pred, g),
          _all_edge_pred(_edge_pred, _vertex_pred, g) {}

    filt_graph(const Graph& g, EdgePredicate ep, VertexPredicate vp)
        : Base(g), _edge_pred(ep), _vertex_pred(vp),
          _out_edge_pred(_edge_pred, _vertex_pred, g),
          _in_edge_pred(_edge_pred, _vertex_pred, g),
          _all_edge_pred(_edge_pred, _vertex_pred, g) {}

    // Graph requirements
    typedef typename Traits::vertex_descriptor          vertex_descriptor;
    typedef typename Traits::edge_descriptor            edge_descriptor;
    typedef typename Traits::directed_category          directed_category;
    typedef typename Traits::edge_parallel_category     edge_parallel_category;
    typedef typename Traits::traversal_category         traversal_category;

    // IncidenceGraph requirements
    typedef filter_iterator<
        OutEdgePred, typename Traits::out_edge_iterator
        > out_edge_iterator;

    typedef typename Traits::degree_size_type          degree_size_type;

    // AdjacencyGraph requirements
    typedef typename adjacency_iterator_generator<self,
                                                  vertex_descriptor,
                                                  out_edge_iterator>::type
        adjacency_iterator;

    // BidirectionalGraph requirements
    typedef filter_iterator<
        InEdgePred, typename Traits::in_edge_iterator
        > in_edge_iterator;

173
174
175
176
177
178
179
180
    typedef filter_iterator<
        EdgePred, typename Graph::all_edge_iterator
        > all_edge_iterator;

    typedef filter_iterator<
        EdgePred, typename Graph::all_edge_iterator_reversed
        > all_edge_iterator_reversed;

181
182
183
184
    typedef typename inv_adjacency_iterator_generator<self,
                                                      vertex_descriptor,
                                                      in_edge_iterator>::type
        in_adjacency_iterator;
185
186
187
188
189
190
191
192
193
194
195
196
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
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
267
268
269
270
271
272
273
274
275
276

    // VertexListGraph requirements
    typedef filter_iterator<
        VertexPredicate, typename Traits::vertex_iterator
        > vertex_iterator;
    typedef typename Traits::vertices_size_type        vertices_size_type;

    // EdgeListGraph requirements
    typedef filter_iterator<
        EdgePred, typename Traits::edge_iterator
        > edge_iterator;
    typedef typename Traits::edges_size_type           edges_size_type;

    typedef filt_graph_tag graph_tag;

    static vertex_descriptor null_vertex()
    { return Traits::null_vertex(); }

    //private:
    EdgePredicate _edge_pred;
    VertexPredicate _vertex_pred;

    OutEdgePred _out_edge_pred;
    InEdgePred _in_edge_pred;
    EdgePred _all_edge_pred;
};

// Do not instantiate these unless needed
template <typename Graph,
          typename EdgePredicate,
          typename VertexPredicate>
struct vertex_property_type<filt_graph<Graph, EdgePredicate, VertexPredicate> >:
    vertex_property_type<Graph> {};

template <typename Graph,
          typename EdgePredicate,
          typename VertexPredicate>
struct edge_property_type<filt_graph<Graph, EdgePredicate, VertexPredicate> >:
    edge_property_type<Graph> {};

template <typename Graph,
          typename EdgePredicate,
          typename VertexPredicate>
struct graph_property_type<filt_graph<Graph, EdgePredicate, VertexPredicate> >:
    graph_property_type<Graph> {};

template<typename Graph, typename EdgePredicate, typename VertexPredicate>
struct vertex_bundle_type<filt_graph<Graph, EdgePredicate,
                                       VertexPredicate> >
  : vertex_bundle_type<Graph> { };

template<typename Graph, typename EdgePredicate, typename VertexPredicate>
struct edge_bundle_type<filt_graph<Graph, EdgePredicate,
                                     VertexPredicate> >
  : edge_bundle_type<Graph> { };

template<typename Graph, typename EdgePredicate, typename VertexPredicate>
struct graph_bundle_type<filt_graph<Graph, EdgePredicate,
                                      VertexPredicate> >
  : graph_bundle_type<Graph> { };

//===========================================================================
// Non-member functions for the Filtered Edge Graph

// Helper functions
template <typename Graph, typename EdgePredicate>
inline filt_graph<Graph, EdgePredicate>
make_filt_graph(Graph& g, EdgePredicate ep)
{
    return filt_graph<Graph, EdgePredicate>(g, ep);
}
template <typename Graph, typename EdgePredicate, typename VertexPredicate>
inline filt_graph<Graph, EdgePredicate, VertexPredicate>
make_filt_graph(Graph& g, EdgePredicate ep, VertexPredicate vp)
{
    return filt_graph<Graph, EdgePredicate, VertexPredicate>(g, ep, vp);
}

template <typename Graph, typename EdgePredicate>
inline filt_graph<const Graph, EdgePredicate>
make_filt_graph(const Graph& g, EdgePredicate ep)
{
    return filt_graph<const Graph, EdgePredicate>(g, ep);
}
template <typename Graph, typename EdgePredicate, typename VertexPredicate>
inline filt_graph<const Graph, EdgePredicate, VertexPredicate>
make_filt_graph(const Graph& g, EdgePredicate ep, VertexPredicate vp)
{
    return filt_graph<const Graph, EdgePredicate, VertexPredicate>(g, ep, vp);
}

template <typename G, typename EP, typename VP>
277
[[gnu::always_inline]] inline
278
279
280
281
282
283
284
285
286
287
288
289
290
std::pair<typename filt_graph<G, EP, VP>::vertex_iterator,
        typename filt_graph<G, EP, VP>::vertex_iterator>
vertices(const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typename graph_traits<G>::vertex_iterator f, l;
    boost::tie(f, l) = vertices(g._g);
    typedef typename Graph::vertex_iterator iter;
    return std::make_pair(iter(g._vertex_pred, f, l),
                          iter(g._vertex_pred, l, l));
}

template <typename G, typename EP, typename VP>
291
[[gnu::always_inline]] inline
292
293
294
295
296
297
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
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
358
359
360
361
362
363
std::pair<typename filt_graph<G, EP, VP>::edge_iterator,
        typename filt_graph<G, EP, VP>::edge_iterator>
edges(const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typename graph_traits<G>::edge_iterator f, l;
    boost::tie(f, l) = edges(g._g);
    typedef typename Graph::edge_iterator iter;
    return std::make_pair(iter(g._all_edge_pred, f, l),
                          iter(g._all_edge_pred, l, l));
}

// An alternative for num_vertices() and num_edges() would be to
// count the number in the filtered graph. This is problematic
// because of the interaction with the vertex indices...  they would
// no longer go from 0 to num_vertices(), which would cause trouble
// for algorithms allocating property storage in an array. We could
// try to create a mapping to new recalibrated indices, but I don't
// see an efficient way to do this.
//
// However, the current solution is still unsatisfactory because
// the following semantic constraints no longer hold:
// boost::tie(vi, viend) = vertices(g);
// assert(std::distance(vi, viend) == num_vertices(g));

template <typename G, typename EP, typename VP>
inline
typename filt_graph<G, EP, VP>::vertices_size_type
num_vertices(const filt_graph<G, EP, VP>& g)
{
    return num_vertices(g._g);
}

template <typename G, typename EP, typename VP>
inline
typename filt_graph<G, EP, VP>::edges_size_type
num_edges(const filt_graph<G, EP, VP>& g)
{
    return num_edges(g._g);
}

template <class G, class EP, class VP>
inline
typename filt_graph_base<G>::vertex_descriptor
vertex(size_t i, const filt_graph<G,EP,VP>& g)
{
    auto v = vertex(i, g._g);
    if (g._vertex_pred(v))
        return v;
    else
        return graph_traits<G>::null_vertex();
}

template <typename G>
inline
typename filt_graph_base<G>::vertex_descriptor
source(const typename filt_graph_base<G>::edge_descriptor& e,
       const filt_graph_base<G>& g)
{
    return source(e, g._g);
}

template <typename G>
inline
typename filt_graph_base<G>::vertex_descriptor
target(const typename filt_graph_base<G>::edge_descriptor& e,
       const filt_graph_base<G>& g)
{
    return target(e, g._g);
}

template <typename G, typename EP, typename VP>
364
[[gnu::always_inline]] [[gnu::flatten]] inline
365
366
367
368
369
370
371
372
373
374
375
376
377
std::pair<typename filt_graph<G, EP, VP>::out_edge_iterator,
          typename filt_graph<G, EP, VP>::out_edge_iterator>
out_edges(typename filt_graph<G, EP, VP>::vertex_descriptor u,
          const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typedef typename Graph::out_edge_iterator iter;
    auto range = out_edges(u, g._g);
    return std::make_pair(iter(g._out_edge_pred, range.first, range.second),
                          iter(g._out_edge_pred, range.second, range.second));
}

template <typename G, typename EP, typename VP>
378
[[gnu::always_inline]] [[gnu::flatten]] inline
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
std::pair<typename filt_graph<G, EP, VP>::out_edge_iterator,
          typename filt_graph<G, EP, VP>::out_edge_iterator>
_all_edges_out(typename filt_graph<G, EP, VP>::vertex_descriptor u,
               const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typedef typename Graph::out_edge_iterator iter;
    auto range = _all_edges_out(u, g._g);
    return std::make_pair(iter(g._out_edge_pred, range.first, range.second),
                          iter(g._out_edge_pred, range.second, range.second));
}

template <typename G, typename EP, typename VP>
inline
typename filt_graph<G, EP, VP>::degree_size_type
out_degree(typename filt_graph<G, EP, VP>::vertex_descriptor u,
           const filt_graph<G, EP, VP>& g)
{
  typename filt_graph<G, EP, VP>::degree_size_type n = 0;
  for (auto range = out_edges(u, g); range.first != range.second;
       ++range.first)
      ++n;
  return n;
}

template <typename G, typename EP, typename VP>
inline
typename filt_graph<G, EP, VP>::degree_size_type
degree(typename filt_graph<G, EP, VP>::vertex_descriptor u,
       const filt_graph<G, EP, VP>& g)
{
410
    return in_degree(u, g) + out_degree(u, g);
411
412
413
}

template <typename G, typename EP, typename VP>
414
[[gnu::always_inline]] [[gnu::flatten]] inline
415
416
std::pair<typename filt_graph<G, EP, VP>::adjacency_iterator,
          typename filt_graph<G, EP, VP>::adjacency_iterator>
417
out_neighbors(typename filt_graph<G, EP, VP>::vertex_descriptor u,
418
419
420
421
422
423
424
425
426
427
               const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typedef typename Graph::adjacency_iterator adjacency_iterator;
    auto range = out_edges(u, g);
    return std::make_pair(adjacency_iterator(range.first, const_cast<Graph*>(&g)),
                          adjacency_iterator(range.second, const_cast<Graph*>(&g)));
}

template <typename G, typename EP, typename VP>
428
[[gnu::always_inline]] [[gnu::flatten]] inline
429
430
std::pair<typename filt_graph<G, EP, VP>::in_adjacency_iterator,
          typename filt_graph<G, EP, VP>::in_adjacency_iterator>
431
in_neighbors(typename filt_graph<G, EP, VP>::vertex_descriptor u,
432
433
434
435
436
437
438
439
440
              const filt_graph<G, EP, VP>& g)
{
  typedef filt_graph<G, EP, VP> Graph;
  typedef typename Graph::in_adjacency_iterator adjacency_iterator;
  auto range = in_edges(u, g);
  return std::make_pair(adjacency_iterator(range.first, const_cast<Graph*>(&g)),
                        adjacency_iterator(range.second, const_cast<Graph*>(&g)));
}

441
template <typename G, typename EP, typename VP>
442
[[gnu::always_inline]] [[gnu::flatten]] inline
443
444
std::pair<typename filt_graph<G, EP, VP>::adjacency_iterator,
          typename filt_graph<G, EP, VP>::adjacency_iterator>
445
all_neighbors(typename filt_graph<G, EP, VP>::vertex_descriptor u,
446
447
448
449
450
451
452
453
454
               const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typedef typename Graph::adjacency_iterator adjacency_iterator;
    auto range = _all_edges_out(u, g);
    return std::make_pair(adjacency_iterator(range.first, const_cast<Graph*>(&g)),
                          adjacency_iterator(range.second, const_cast<Graph*>(&g)));
}

455
template <typename G, typename EP, typename VP>
456
[[gnu::always_inline]] [[gnu::flatten]] inline
457
458
459
460
461
std::pair<typename filt_graph<G, EP, VP>::adjacency_iterator,
          typename filt_graph<G, EP, VP>::adjacency_iterator>
adjacent_vertices(typename filt_graph<G, EP, VP>::vertex_descriptor u,
                  const filt_graph<G, EP, VP>& g)
{
462
    return out_neighbors(u, g);
463
464
465
}

template <typename G, typename EP, typename VP>
466
[[gnu::always_inline]] [[gnu::flatten]] inline
467
468
469
std::pair<typename filt_graph<G, EP, VP>::in_edge_iterator,
        typename filt_graph<G, EP, VP>::in_edge_iterator>
in_edges(typename filt_graph<G, EP, VP>::vertex_descriptor u,
470
         const filt_graph<G, EP, VP>& g)
471
472
473
474
475
476
477
478
479
{
    typedef filt_graph<G, EP, VP> Graph;
    typedef typename Graph::in_edge_iterator iter;
    auto range = in_edges(u, g._g);
    return std::make_pair(iter(g._in_edge_pred, range.first, range.second),
                          iter(g._in_edge_pred, range.second, range.second));
}

template <typename G, typename EP, typename VP>
480
[[gnu::always_inline]] [[gnu::flatten]] inline
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
std::pair<typename filt_graph<G, EP, VP>::in_edge_iterator,
        typename filt_graph<G, EP, VP>::in_edge_iterator>
_all_edges_in(typename filt_graph<G, EP, VP>::vertex_descriptor u,
            const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typedef typename Graph::in_edge_iterator iter;
    auto range = _all_edges_in(u, g._g);
    return std::make_pair(iter(g._in_edge_pred, range.first, range.second),
                          iter(g._in_edge_pred, range.second, range.second));
}

template <typename G, typename EP, typename VP>
inline
typename filt_graph<G, EP, VP>::degree_size_type
in_degree(typename filt_graph<G, EP, VP>::vertex_descriptor u,
          const filt_graph<G, EP, VP>& g)
{
    typename filt_graph<G, EP, VP>::degree_size_type n = 0;
    for (auto range = in_edges(u, g); range.first != range.second;
         ++range.first)
        ++n;
    return n;
}

506
template <typename G, typename EP, typename VP>
507
[[gnu::always_inline]] [[gnu::flatten]] inline
508
509
510
511
512
513
514
515
516
517
518
519
std::pair<typename filt_graph<G, EP, VP>::all_edge_iterator,
          typename filt_graph<G, EP, VP>::all_edge_iterator>
all_edges(typename filt_graph<G, EP, VP>::vertex_descriptor u,
          const filt_graph<G, EP, VP>& g)
{
    typedef typename filt_graph<G, EP, VP>::all_edge_iterator iter;
    auto range = all_edges(u, g._g);
    return std::make_pair(iter(g._all_edge_pred, range.first, range.second),
                          iter(g._all_edge_pred, range.second, range.second));
}


520
521
522
523
524
525
526
527
528
529
530
531
532
533
template <typename G, typename EP, typename VP>
inline
std::pair<typename filt_graph<G, EP, VP>::edge_descriptor, bool>
edge(typename filt_graph<G, EP, VP>::vertex_descriptor u,
     typename filt_graph<G, EP, VP>::vertex_descriptor v,
     const filt_graph<G, EP, VP>& g)
{
    typename graph_traits<G>::edge_descriptor e;
    bool exists;
    boost::tie(e, exists) = edge(u, v, g._g);
    return std::make_pair(e, exists && g._edge_pred(e));
}

template <typename G, typename EP, typename VP>
534
[[gnu::always_inline]] inline
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
std::pair<typename filt_graph<G, EP, VP>::out_edge_iterator,
          typename filt_graph<G, EP, VP>::out_edge_iterator>
edge_range(typename filt_graph<G, EP, VP>::vertex_descriptor u,
           typename filt_graph<G, EP, VP>::vertex_descriptor v,
           const filt_graph<G, EP, VP>& g)
{
    typedef filt_graph<G, EP, VP> Graph;
    typedef typename Graph::out_edge_iterator iter;
    typename graph_traits<G>::out_edge_iterator f, l;
    boost::tie(f, l) = edge_range(u, v, g._g);
    return std::make_pair(iter(g._out_edge_pred, f, l),
                          iter(g._out_edge_pred, l, l));
}

template <class G, class EP, class VP>
inline
std::pair<typename graph_traits
            <filt_graph<G,EP,VP>>::edge_descriptor, bool>
add_edge(typename graph_traits
             <filt_graph<G,EP,VP>>::vertex_descriptor u,
         typename graph_traits
             <filt_graph<G,EP,VP>>::vertex_descriptor v,
         filt_graph<G,EP,VP>& g)
{
    auto ret = add_edge(u, v, const_cast<G&>(g._g));
    auto filt = g._edge_pred.get_filter().get_checked();
    filt[ret.first] = !g._edge_pred.is_inverted();
    return ret;
}

template <class G, class EP, class VP, class Pred>
inline void
clear_vertex(typename boost::graph_traits
             <filt_graph<G,EP,VP>>::vertex_descriptor v,
             filt_graph<G,EP,VP>& g, Pred&& pred)
{
    clear_vertex(v, const_cast<G&>(g._g),
                 [&](auto&& e){ return (((g._edge_pred(e) &&
                                          g._vertex_pred(source(e, g._g)) &&
                                          g._vertex_pred(target(e, g._g)))) &&
                                        pred(e)); });
}

template <class G, class EP, class VP>
inline void
clear_vertex(typename boost::graph_traits
                 <filt_graph<G,EP,VP>>::vertex_descriptor v,
             filt_graph<G,EP,VP>& g)
{
    clear_vertex(v, g, [&](auto&&){ return true; });
}

template <class G, class EP, class VP>
inline
void remove_edge(const typename boost::graph_traits
                   <filt_graph<G,EP,VP>>::edge_descriptor& e,
                 filt_graph<G,EP,VP>& g)
{
    return remove_edge(e, const_cast<G&>(g._g));
}

template <class G, class EP, class VP>
inline
void remove_vertex(typename boost::graph_traits
                     <filt_graph<G,EP,VP>>::vertex_descriptor v,
                  filt_graph<G,EP,VP>& g)
{
    auto& filt = g._vertex_pred.get_filter();
    for (size_t i = v; i < num_vertices(g) - 1; ++i)
        filt[i] = filt[i + 1];
    return remove_vertex(v,const_cast<G&>(g._g));
}

template <class G, class EP, class VP>
inline
void remove_vertex_fast(typename boost::graph_traits
                          <filt_graph<G,EP,VP>>::vertex_descriptor v,
                       filt_graph<G,EP,VP>& g)
{
   size_t back = num_vertices(g) - 1;
   auto& filt = g._vertex_pred.get_filter();
   filt[v] = filt[back];
   return remove_vertex_fast(v,const_cast<G&>(g._g));
}

template <class G, class EP, class VP>
inline
typename boost::graph_traits<filt_graph<G,EP,VP>>::vertex_descriptor
add_vertex(filt_graph<G,EP,VP>& g)
{
    auto filt = g._vertex_pred.get_filter().get_checked();
    auto v = add_vertex(const_cast<G&>(g._g));
    filt[v] = !g._vertex_pred.is_inverted();
    return v;
}

//===========================================================================
// Property map

template <typename G, typename EP, typename VP, typename Property>
struct property_map<filt_graph<G, EP, VP>, Property>
    : property_map<G, Property> {};

template <typename G, typename EP, typename VP, typename Property>
inline
typename property_map<G, Property>::type
get(Property p, filt_graph<G, EP, VP>& g)
{
    return get(p, const_cast<G&>(g._g));
}

template <typename G, typename EP, typename VP,typename Property>
inline
typename property_map<G, Property>::const_type
get(Property p, const filt_graph<G, EP, VP>& g)
{
    return get(p, (const G&)g._g);
}

template <typename G, typename EP, typename VP, typename Property,
          typename Key>
inline
auto&&
get(Property p, const filt_graph<G, EP, VP>& g, const Key& k)
{
    return get(p, (const G&)g._g, k);
}

template <typename G, typename EP, typename VP, typename Property,
          typename Key, typename Value>
inline
void
put(Property p, const filt_graph<G, EP, VP>& g, const Key& k,
    const Value& val)
{
    put(p, const_cast<G&>(g._g), k, val);
}

} // namespace boost


#endif // GRAPH_FILTERED_HH