📄 compressed_sparse_row_graph.hpp
字号:
return g.m_rowstart.size() - 1;
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
std::pair<counting_iterator<Vertex>, counting_iterator<Vertex> >
inline vertices(const BOOST_CSR_GRAPH_TYPE& g) {
return std::make_pair(counting_iterator<Vertex>(0),
counting_iterator<Vertex>(num_vertices(g)));
}
// From IncidenceGraph
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
class BOOST_CSR_GRAPH_TYPE::out_edge_iterator
: public iterator_facade<typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator,
typename BOOST_CSR_GRAPH_TYPE::edge_descriptor,
std::random_access_iterator_tag,
const typename BOOST_CSR_GRAPH_TYPE::edge_descriptor&,
typename int_t<CHAR_BIT * sizeof(EdgeIndex)>::fast>
{
public:
typedef typename int_t<CHAR_BIT * sizeof(EdgeIndex)>::fast difference_type;
out_edge_iterator() {}
// Implicit copy constructor OK
explicit out_edge_iterator(edge_descriptor edge) : m_edge(edge) { }
private:
// iterator_facade requirements
const edge_descriptor& dereference() const { return m_edge; }
bool equal(const out_edge_iterator& other) const
{ return m_edge == other.m_edge; }
void increment() { ++m_edge.idx; }
void decrement() { ++m_edge.idx; }
void advance(difference_type n) { m_edge.idx += n; }
difference_type distance_to(const out_edge_iterator& other) const
{ return other.m_edge.idx - m_edge.idx; }
edge_descriptor m_edge;
friend class iterator_core_access;
};
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline Vertex
source(typename BOOST_CSR_GRAPH_TYPE::edge_descriptor e,
const BOOST_CSR_GRAPH_TYPE&)
{
return e.src;
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline Vertex
target(typename BOOST_CSR_GRAPH_TYPE::edge_descriptor e,
const BOOST_CSR_GRAPH_TYPE& g)
{
return g.m_column[e.idx];
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline std::pair<typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator,
typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator>
out_edges(Vertex v, const BOOST_CSR_GRAPH_TYPE& g)
{
typedef typename BOOST_CSR_GRAPH_TYPE::edge_descriptor ed;
typedef typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator it;
EdgeIndex v_row_start = g.m_rowstart[v];
EdgeIndex next_row_start = g.m_rowstart[v + 1];
return std::make_pair(it(ed(v, v_row_start)),
it(ed(v, (std::max)(v_row_start, next_row_start))));
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline EdgeIndex
out_degree(Vertex v, const BOOST_CSR_GRAPH_TYPE& g)
{
EdgeIndex v_row_start = g.m_rowstart[v];
EdgeIndex next_row_start = g.m_rowstart[v + 1];
return (std::max)(v_row_start, next_row_start) - v_row_start;
}
// From AdjacencyGraph
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline std::pair<typename BOOST_CSR_GRAPH_TYPE::adjacency_iterator,
typename BOOST_CSR_GRAPH_TYPE::adjacency_iterator>
adjacent_vertices(Vertex v, const BOOST_CSR_GRAPH_TYPE& g)
{
EdgeIndex v_row_start = g.m_rowstart[v];
EdgeIndex next_row_start = g.m_rowstart[v + 1];
return std::make_pair(g.m_column.begin() + v_row_start,
g.m_column.begin() +
(std::max)(v_row_start, next_row_start));
}
// Extra, common functions
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline typename graph_traits<BOOST_CSR_GRAPH_TYPE>::vertex_descriptor
vertex(typename graph_traits<BOOST_CSR_GRAPH_TYPE>::vertex_descriptor i,
const BOOST_CSR_GRAPH_TYPE&)
{
return i;
}
// Unlike for an adjacency_matrix, edge_range and edge take lg(out_degree(i))
// time
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline std::pair<typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator,
typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator>
edge_range(Vertex i, Vertex j, const BOOST_CSR_GRAPH_TYPE& g)
{
typedef typename std::vector<Vertex>::const_iterator adj_iter;
typedef typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator out_edge_iter;
typedef typename BOOST_CSR_GRAPH_TYPE::edge_descriptor edge_desc;
std::pair<adj_iter, adj_iter> raw_adjacencies = adjacent_vertices(i, g);
std::pair<adj_iter, adj_iter> adjacencies =
std::equal_range(raw_adjacencies.first, raw_adjacencies.second, j);
EdgeIndex idx_begin = adjacencies.first - g.m_column.begin();
EdgeIndex idx_end = adjacencies.second - g.m_column.begin();
return std::make_pair(out_edge_iter(edge_desc(i, idx_begin)),
out_edge_iter(edge_desc(i, idx_end)));
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline std::pair<typename BOOST_CSR_GRAPH_TYPE::edge_descriptor, bool>
edge(Vertex i, Vertex j, const BOOST_CSR_GRAPH_TYPE& g)
{
typedef typename BOOST_CSR_GRAPH_TYPE::out_edge_iterator out_edge_iter;
std::pair<out_edge_iter, out_edge_iter> range = edge_range(i, j, g);
if (range.first == range.second)
return std::make_pair(typename BOOST_CSR_GRAPH_TYPE::edge_descriptor(),
false);
else
return std::make_pair(*range.first, true);
}
// Find an edge given its index in the graph
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline typename BOOST_CSR_GRAPH_TYPE::edge_descriptor
edge_from_index(EdgeIndex idx, const BOOST_CSR_GRAPH_TYPE& g)
{
typedef typename std::vector<EdgeIndex>::const_iterator row_start_iter;
assert (idx < num_edges(g));
row_start_iter src_plus_1 =
std::upper_bound(g.m_rowstart.begin(),
g.m_rowstart.begin() + g.m_last_source + 1,
idx);
// Get last source whose rowstart is at most idx
// upper_bound returns this position plus 1
Vertex src = (src_plus_1 - g.m_rowstart.begin()) - 1;
return typename BOOST_CSR_GRAPH_TYPE::edge_descriptor(src, idx);
}
// From EdgeListGraph
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
class BOOST_CSR_GRAPH_TYPE::edge_iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef edge_descriptor value_type;
typedef const edge_descriptor* pointer;
typedef edge_descriptor reference;
typedef typename int_t<CHAR_BIT * sizeof(EdgeIndex)>::fast difference_type;
edge_iterator() : rowstart_array(0), current_edge(), end_of_this_vertex(0) {}
edge_iterator(const compressed_sparse_row_graph& graph,
edge_descriptor current_edge,
EdgeIndex end_of_this_vertex)
: rowstart_array(&graph.m_rowstart[0]), current_edge(current_edge),
end_of_this_vertex(end_of_this_vertex) {}
// From InputIterator
reference operator*() const { return current_edge; }
pointer operator->() const { return ¤t_edge; }
bool operator==(const edge_iterator& o) const {
return current_edge == o.current_edge;
}
bool operator!=(const edge_iterator& o) const {
return current_edge != o.current_edge;
}
edge_iterator& operator++() {
++current_edge.idx;
while (current_edge.idx == end_of_this_vertex) {
++current_edge.src;
end_of_this_vertex = rowstart_array[current_edge.src + 1];
}
return *this;
}
edge_iterator operator++(int) {
edge_iterator temp = *this;
++*this;
return temp;
}
private:
const EdgeIndex* rowstart_array;
edge_descriptor current_edge;
EdgeIndex end_of_this_vertex;
};
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline EdgeIndex
num_edges(const BOOST_CSR_GRAPH_TYPE& g)
{
return g.m_column.size();
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
std::pair<typename BOOST_CSR_GRAPH_TYPE::edge_iterator,
typename BOOST_CSR_GRAPH_TYPE::edge_iterator>
edges(const BOOST_CSR_GRAPH_TYPE& g)
{
typedef typename BOOST_CSR_GRAPH_TYPE::edge_iterator ei;
typedef typename BOOST_CSR_GRAPH_TYPE::edge_descriptor edgedesc;
if (g.m_rowstart.size() == 1 || g.m_column.empty()) {
return std::make_pair(ei(), ei());
} else {
// Find the first vertex that has outgoing edges
Vertex src = 0;
while (g.m_rowstart[src + 1] == 0) ++src;
return std::make_pair(ei(g, edgedesc(src, 0), g.m_rowstart[src + 1]),
ei(g, edgedesc(num_vertices(g), g.m_column.size()), 0));
}
}
// For Property Graph
// Graph properties
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, class Tag, class Value>
inline void
set_property(BOOST_CSR_GRAPH_TYPE& g, Tag, const Value& value)
{
get_property_value(g.m_property, Tag()) = value;
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, class Tag>
inline
typename graph_property<BOOST_CSR_GRAPH_TYPE, Tag>::type&
get_property(BOOST_CSR_GRAPH_TYPE& g, Tag)
{
return get_property_value(g.m_property, Tag());
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, class Tag>
inline
const
typename graph_property<BOOST_CSR_GRAPH_TYPE, Tag>::type&
get_property(const BOOST_CSR_GRAPH_TYPE& g, Tag)
{
return get_property_value(g.m_property, Tag());
}
// Add edge_index property map
template<typename Index, typename Descriptor>
struct csr_edge_index_map
{
typedef Index value_type;
typedef Index reference;
typedef Descriptor key_type;
typedef readable_property_map_tag category;
};
template<typename Index, typename Descriptor>
inline Index
get(const csr_edge_index_map<Index, Descriptor>&,
const typename csr_edge_index_map<Index, Descriptor>::key_type& key)
{
return key.idx;
}
// Doing the right thing here (by unifying with vertex_index_t and
// edge_index_t) breaks GCC.
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, typename Tag>
struct property_map<BOOST_CSR_GRAPH_TYPE, Tag>
{
private:
typedef identity_property_map vertex_index_type;
typedef typename graph_traits<BOOST_CSR_GRAPH_TYPE>::edge_descriptor
edge_descriptor;
typedef csr_edge_index_map<EdgeIndex, edge_descriptor> edge_index_type;
typedef typename mpl::if_<is_same<Tag, edge_index_t>,
edge_index_type,
detail::error_property_not_found>::type
edge_or_none;
public:
typedef typename mpl::if_<is_same<Tag, vertex_index_t>,
vertex_index_type,
edge_or_none>::type type;
typedef type const_type;
};
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline identity_property_map
get(vertex_index_t, const BOOST_CSR_GRAPH_TYPE&)
{
return identity_property_map();
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline Vertex
get(vertex_index_t,
const BOOST_CSR_GRAPH_TYPE&, Vertex v)
{
return v;
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline typename property_map<BOOST_CSR_GRAPH_TYPE, edge_index_t>::const_type
get(edge_index_t, const BOOST_CSR_GRAPH_TYPE&)
{
typedef typename property_map<BOOST_CSR_GRAPH_TYPE, edge_index_t>::const_type
result_type;
return result_type();
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS>
inline EdgeIndex
get(edge_index_t, const BOOST_CSR_GRAPH_TYPE&,
typename BOOST_CSR_GRAPH_TYPE::edge_descriptor e)
{
return e.idx;
}
// Support for bundled properties
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, typename T, typename Bundle>
struct property_map<BOOST_CSR_GRAPH_TYPE, T Bundle::*>
{
private:
typedef graph_traits<BOOST_CSR_GRAPH_TYPE> traits;
typedef VertexProperty vertex_bundled;
typedef EdgeProperty edge_bundled;
typedef typename ct_if<(detail::is_vertex_bundle<vertex_bundled, edge_bundled, Bundle>::value),
typename traits::vertex_descriptor,
typename traits::edge_descriptor>::type
descriptor;
public:
typedef bundle_property_map<BOOST_CSR_GRAPH_TYPE, descriptor, Bundle, T>
type;
typedef bundle_property_map<const BOOST_CSR_GRAPH_TYPE, descriptor, Bundle,
const T> const_type;
};
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, typename T, typename Bundle>
inline
typename property_map<BOOST_CSR_GRAPH_TYPE, T Bundle::*>::type
get(T Bundle::* p, BOOST_CSR_GRAPH_TYPE& g)
{
typedef typename property_map<BOOST_CSR_GRAPH_TYPE,
T Bundle::*>::type
result_type;
return result_type(&g, p);
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, typename T, typename Bundle>
inline
typename property_map<BOOST_CSR_GRAPH_TYPE, T Bundle::*>::const_type
get(T Bundle::* p, BOOST_CSR_GRAPH_TYPE const & g)
{
typedef typename property_map<BOOST_CSR_GRAPH_TYPE,
T Bundle::*>::const_type
result_type;
return result_type(&g, p);
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, typename T, typename Bundle,
typename Key>
inline T
get(T Bundle::* p, BOOST_CSR_GRAPH_TYPE const & g,
const Key& key)
{
return get(get(p, g), key);
}
template<BOOST_CSR_GRAPH_TEMPLATE_PARMS, typename T, typename Bundle,
typename Key>
inline void
put(T Bundle::* p, BOOST_CSR_GRAPH_TYPE& g,
const Key& key, const T& value)
{
put(get(p, g), key, value);
}
#undef BOOST_CSR_GRAPH_TYPE
#undef BOOST_CSR_GRAPH_TEMPLATE_PARMS
} // end namespace boost
#endif // BOOST_GRAPH_COMPRESSED_SPARSE_ROW_GRAPH_HPP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -