📄 filtered_graph.hpp
字号:
//=======================================================================
// 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 BOOST_FILTERED_GRAPH_HPP
#define BOOST_FILTERED_GRAPH_HPP
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <boost/iterator/filter_iterator.hpp>
namespace boost {
//=========================================================================
// Some predicate classes.
struct keep_all {
template <typename T>
bool operator()(const T&) const { return true; }
};
// Keep residual edges (used in maximum-flow algorithms).
template <typename ResidualCapacityEdgeMap>
struct is_residual_edge {
is_residual_edge() { }
is_residual_edge(ResidualCapacityEdgeMap rcap) : m_rcap(rcap) { }
template <typename Edge>
bool operator()(const Edge& e) const {
return 0 < get(m_rcap, e);
}
ResidualCapacityEdgeMap m_rcap;
};
template <typename Set>
struct is_in_subset {
is_in_subset() : m_s(0) { }
is_in_subset(const Set& s) : m_s(&s) { }
template <typename Elt>
bool operator()(const Elt& x) const {
return set_contains(*m_s, x);
}
const Set* m_s;
};
template <typename Set>
struct is_not_in_subset {
is_not_in_subset() : m_s(0) { }
is_not_in_subset(const Set& s) : m_s(&s) { }
template <typename Elt>
bool operator()(const Elt& x) const {
return !set_contains(*m_s, x);
}
const Set* m_s;
};
namespace detail {
template <typename EdgePredicate, typename VertexPredicate, typename Graph>
struct out_edge_predicate {
out_edge_predicate() { }
out_edge_predicate(EdgePredicate ep, VertexPredicate vp,
const Graph& g)
: m_edge_pred(ep), m_vertex_pred(vp), m_g(&g) { }
template <typename Edge>
bool operator()(const Edge& e) const {
return m_edge_pred(e) && m_vertex_pred(target(e, *m_g));
}
EdgePredicate m_edge_pred;
VertexPredicate m_vertex_pred;
const Graph* m_g;
};
template <typename EdgePredicate, typename VertexPredicate, typename Graph>
struct in_edge_predicate {
in_edge_predicate() { }
in_edge_predicate(EdgePredicate ep, VertexPredicate vp,
const Graph& g)
: m_edge_pred(ep), m_vertex_pred(vp), m_g(&g) { }
template <typename Edge>
bool operator()(const Edge& e) const {
return m_edge_pred(e) && m_vertex_pred(source(e, *m_g));
}
EdgePredicate m_edge_pred;
VertexPredicate m_vertex_pred;
const Graph* m_g;
};
template <typename EdgePredicate, typename VertexPredicate, typename Graph>
struct edge_predicate {
edge_predicate() { }
edge_predicate(EdgePredicate ep, VertexPredicate vp,
const Graph& g)
: m_edge_pred(ep), m_vertex_pred(vp), m_g(&g) { }
template <typename Edge>
bool operator()(const Edge& e) const {
return m_edge_pred(e)
&& m_vertex_pred(source(e, *m_g)) && m_vertex_pred(target(e, *m_g));
}
EdgePredicate m_edge_pred;
VertexPredicate m_vertex_pred;
const Graph* m_g;
};
} // namespace detail
//===========================================================================
// Filtered Graph
struct filtered_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 filtered_graph_base {
typedef graph_traits<G> Traits;
typedef typename Traits::vertex_descriptor vertex_descriptor;
typedef typename Traits::edge_descriptor edge_descriptor;
filtered_graph_base(const G& g) : m_g(g) { }
//protected:
const G& m_g;
};
template <typename Graph,
typename EdgePredicate,
typename VertexPredicate = keep_all>
class filtered_graph : public filtered_graph_base<Graph> {
typedef filtered_graph_base<Graph> Base;
typedef graph_traits<Graph> Traits;
typedef filtered_graph self;
public:
typedef Graph graph_type;
typedef detail::out_edge_predicate<EdgePredicate,
VertexPredicate, self> OutEdgePred;
typedef detail::in_edge_predicate<EdgePredicate,
VertexPredicate, self> InEdgePred;
typedef detail::edge_predicate<EdgePredicate,
VertexPredicate, self> EdgePred;
// Constructors
filtered_graph(const Graph& g, EdgePredicate ep)
: Base(g), m_edge_pred(ep) { }
filtered_graph(const Graph& g, EdgePredicate ep, VertexPredicate vp)
: Base(g), m_edge_pred(ep), m_vertex_pred(vp) { }
// 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;
// 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 typename ::boost::edge_property_type<Graph>::type edge_property_type;
typedef typename ::boost::vertex_property_type<Graph>::type vertex_property_type;
typedef filtered_graph_tag graph_tag;
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
// Bundled properties support
template<typename Descriptor>
typename graph::detail::bundled_result<Graph, Descriptor>::type&
operator[](Descriptor x)
{ return const_cast<Graph&>(this->m_g)[x]; }
template<typename Descriptor>
typename graph::detail::bundled_result<Graph, Descriptor>::type const&
operator[](Descriptor x) const
{ return this->m_g[x]; }
#endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES
static vertex_descriptor null_vertex()
{
return Graph::null_vertex();
}
//private:
EdgePredicate m_edge_pred;
VertexPredicate m_vertex_pred;
};
#ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
template<typename Graph, typename EdgePredicate, typename VertexPredicate>
struct vertex_bundle_type<filtered_graph<Graph, EdgePredicate,
VertexPredicate> >
: vertex_bundle_type<Graph> { };
template<typename Graph, typename EdgePredicate, typename VertexPredicate>
struct edge_bundle_type<filtered_graph<Graph, EdgePredicate,
VertexPredicate> >
: edge_bundle_type<Graph> { };
#endif // BOOST_GRAPH_NO_BUNDLED_PROPERTIES
//===========================================================================
// Non-member functions for the Filtered Edge Graph
// Helper functions
template <typename Graph, typename EdgePredicate>
inline filtered_graph<Graph, EdgePredicate>
make_filtered_graph(Graph& g, EdgePredicate ep) {
return filtered_graph<Graph, EdgePredicate>(g, ep);
}
template <typename Graph, typename EdgePredicate, typename VertexPredicate>
inline filtered_graph<Graph, EdgePredicate, VertexPredicate>
make_filtered_graph(Graph& g, EdgePredicate ep, VertexPredicate vp) {
return filtered_graph<Graph, EdgePredicate, VertexPredicate>(g, ep, vp);
}
template <typename Graph, typename EdgePredicate>
inline filtered_graph<const Graph, EdgePredicate>
make_filtered_graph(const Graph& g, EdgePredicate ep) {
return filtered_graph<const Graph, EdgePredicate>(g, ep);
}
template <typename Graph, typename EdgePredicate, typename VertexPredicate>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -