File indexing completed on 2025-01-18 09:37:39
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef BOOST_VECTOR_AS_GRAPH_HPP
0015 #define BOOST_VECTOR_AS_GRAPH_HPP
0016
0017 #include <cassert>
0018 #include <utility>
0019 #include <vector>
0020 #include <cstddef>
0021 #include <iterator>
0022 #include <boost/iterator/counting_iterator.hpp>
0023 #include <boost/range/irange.hpp>
0024 #include <boost/graph/graph_traits.hpp>
0025 #include <boost/property_map/property_map.hpp>
0026 #include <boost/graph/properties.hpp>
0027 #include <algorithm>
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 namespace boost
0044 {
0045 namespace detail
0046 {
0047 template < class EdgeList > struct val_out_edge_ret;
0048 template < class EdgeList > struct val_out_edge_iter;
0049 template < class EdgeList > struct val_edge;
0050 }
0051 }
0052
0053 namespace boost
0054 {
0055
0056 struct vector_as_graph_traversal_tag : public vertex_list_graph_tag,
0057 public adjacency_graph_tag,
0058 public incidence_graph_tag
0059 {
0060 };
0061
0062 template < class EdgeList > struct graph_traits< std::vector< EdgeList > >
0063 {
0064 typedef typename EdgeList::value_type V;
0065 typedef V vertex_descriptor;
0066 typedef typename detail::val_edge< EdgeList >::type edge_descriptor;
0067 typedef typename EdgeList::const_iterator adjacency_iterator;
0068 typedef
0069 typename detail::val_out_edge_iter< EdgeList >::type out_edge_iterator;
0070 typedef void in_edge_iterator;
0071 typedef void edge_iterator;
0072 typedef counting_iterator< V > vertex_iterator;
0073 typedef directed_tag directed_category;
0074 typedef allow_parallel_edge_tag edge_parallel_category;
0075 typedef vector_as_graph_traversal_tag traversal_category;
0076 typedef typename std::vector< EdgeList >::size_type vertices_size_type;
0077 typedef void edges_size_type;
0078 typedef typename EdgeList::size_type degree_size_type;
0079 static V null_vertex() { return V(-1); }
0080 };
0081 template < class EdgeList > struct edge_property_type< std::vector< EdgeList > >
0082 {
0083 typedef void type;
0084 };
0085 template < class EdgeList >
0086 struct vertex_property_type< std::vector< EdgeList > >
0087 {
0088 typedef void type;
0089 };
0090 template < class EdgeList >
0091 struct graph_property_type< std::vector< EdgeList > >
0092 {
0093 typedef void type;
0094 };
0095 }
0096
0097 namespace boost
0098 {
0099
0100 namespace detail
0101 {
0102
0103
0104
0105 template < class EdgeList > struct val_edge
0106 {
0107 typedef typename EdgeList::value_type V;
0108 typedef std::pair< V, V > type;
0109 };
0110
0111
0112 template < class V, class Iter > class val_out_edge_iterator
0113 {
0114 typedef val_out_edge_iterator self;
0115 typedef std::pair< V, V > Edge;
0116
0117 public:
0118 typedef std::input_iterator_tag iterator_category;
0119 typedef std::pair< V, V > value_type;
0120 typedef std::ptrdiff_t difference_type;
0121 typedef std::pair< V, V >* pointer;
0122 typedef const std::pair< V, V > reference;
0123 val_out_edge_iterator() {}
0124 val_out_edge_iterator(V s, Iter i) : _source(s), _iter(i) {}
0125 Edge operator*() const { return Edge(_source, *_iter); }
0126 self& operator++()
0127 {
0128 ++_iter;
0129 return *this;
0130 }
0131 self operator++(int)
0132 {
0133 self t = *this;
0134 ++_iter;
0135 return t;
0136 }
0137 bool operator==(const self& x) const { return _iter == x._iter; }
0138 bool operator!=(const self& x) const { return _iter != x._iter; }
0139
0140 protected:
0141 V _source;
0142 Iter _iter;
0143 };
0144
0145 template < class EdgeList > struct val_out_edge_iter
0146 {
0147 typedef typename EdgeList::value_type V;
0148 typedef typename EdgeList::const_iterator Iter;
0149 typedef val_out_edge_iterator< V, Iter > type;
0150 };
0151
0152 template < class EdgeList > struct val_out_edge_ret
0153 {
0154 typedef typename val_out_edge_iter< EdgeList >::type IncIter;
0155 typedef std::pair< IncIter, IncIter > type;
0156 };
0157
0158 }
0159
0160 template < class EdgeList, class Alloc >
0161 typename detail::val_out_edge_ret< EdgeList >::type out_edges(
0162 typename EdgeList::value_type v, const std::vector< EdgeList, Alloc >& g)
0163 {
0164 typedef typename detail::val_out_edge_iter< EdgeList >::type Iter;
0165 typedef typename detail::val_out_edge_ret< EdgeList >::type return_type;
0166 return return_type(Iter(v, g[v].begin()), Iter(v, g[v].end()));
0167 }
0168
0169 template < class EdgeList, class Alloc >
0170 typename EdgeList::size_type out_degree(
0171 typename EdgeList::value_type v, const std::vector< EdgeList, Alloc >& g)
0172 {
0173 return g[v].size();
0174 }
0175
0176 template < class EdgeList, class Alloc >
0177 std::pair< typename EdgeList::const_iterator,
0178 typename EdgeList::const_iterator >
0179 adjacent_vertices(
0180 typename EdgeList::value_type v, const std::vector< EdgeList, Alloc >& g)
0181 {
0182 return std::make_pair(g[v].begin(), g[v].end());
0183 }
0184
0185
0186
0187 template < class EdgeList, class Alloc >
0188 std::pair< boost::counting_iterator< typename EdgeList::value_type >,
0189 boost::counting_iterator< typename EdgeList::value_type > >
0190 vertices(const std::vector< EdgeList, Alloc >& v)
0191 {
0192 typedef boost::counting_iterator< typename EdgeList::value_type > Iter;
0193 return std::make_pair(Iter(0), Iter(v.size()));
0194 }
0195
0196 template < class EdgeList, class Alloc >
0197 typename std::vector< EdgeList, Alloc >::size_type num_vertices(
0198 const std::vector< EdgeList, Alloc >& v)
0199 {
0200 return v.size();
0201 }
0202
0203 template < class EdgeList, class Allocator >
0204 typename std::pair< typename detail::val_edge< EdgeList >::type, bool >
0205 add_edge(typename EdgeList::value_type u, typename EdgeList::value_type v,
0206 std::vector< EdgeList, Allocator >& g)
0207 {
0208 typedef typename detail::val_edge< EdgeList >::type edge_type;
0209 g[u].insert(g[u].end(), v);
0210 return std::make_pair(edge_type(u, v), true);
0211 }
0212
0213 template < class EdgeList, class Allocator >
0214 typename std::pair< typename detail::val_edge< EdgeList >::type, bool > edge(
0215 typename EdgeList::value_type u, typename EdgeList::value_type v,
0216 std::vector< EdgeList, Allocator >& g)
0217 {
0218 typedef typename detail::val_edge< EdgeList >::type edge_type;
0219 typename EdgeList::iterator i = g[u].begin(), end = g[u].end();
0220 for (; i != end; ++i)
0221 if (*i == v)
0222 return std::make_pair(edge_type(u, v), true);
0223 return std::make_pair(edge_type(), false);
0224 }
0225
0226 template < class EdgeList, class Allocator >
0227 void remove_edge(typename EdgeList::value_type u,
0228 typename EdgeList::value_type v, std::vector< EdgeList, Allocator >& g)
0229 {
0230 typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
0231 if (i != g[u].end())
0232 g[u].erase(i, g[u].end());
0233 }
0234
0235 template < class EdgeList, class Allocator >
0236 void remove_edge(typename detail::val_edge< EdgeList >::type e,
0237 std::vector< EdgeList, Allocator >& g)
0238 {
0239 typename EdgeList::value_type u, v;
0240 u = e.first;
0241 v = e.second;
0242
0243 typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v);
0244 if (i != g[u].end())
0245 g[u].erase(i, g[u].end());
0246 }
0247
0248 template < class EdgeList, class Allocator, class Predicate >
0249 void remove_edge_if(Predicate p, std::vector< EdgeList, Allocator >& g)
0250 {
0251 for (std::size_t u = 0; u < g.size(); ++u)
0252 {
0253
0254
0255 typedef typename EdgeList::iterator iterator;
0256 iterator b = g[u].begin(), e = g[u].end();
0257
0258 if (!g[u].empty())
0259 {
0260
0261 for (; b != e;)
0262 {
0263 if (p(std::make_pair(u, *b)))
0264 {
0265 --e;
0266 if (b == e)
0267 break;
0268 else
0269 iter_swap(b, e);
0270 }
0271 else
0272 {
0273 ++b;
0274 }
0275 }
0276 }
0277
0278 if (e != g[u].end())
0279 g[u].erase(e, g[u].end());
0280 }
0281 }
0282
0283 template < class EdgeList, class Allocator >
0284 typename EdgeList::value_type add_vertex(std::vector< EdgeList, Allocator >& g)
0285 {
0286 g.resize(g.size() + 1);
0287 return g.size() - 1;
0288 }
0289
0290 template < class EdgeList, class Allocator >
0291 void clear_vertex(
0292 typename EdgeList::value_type u, std::vector< EdgeList, Allocator >& g)
0293 {
0294 g[u].clear();
0295 for (std::size_t i = 0; i < g.size(); ++i)
0296 remove_edge(i, u, g);
0297 }
0298
0299 template < class EdgeList, class Allocator >
0300 void remove_vertex(
0301 typename EdgeList::value_type u, std::vector< EdgeList, Allocator >& g)
0302 {
0303 typedef typename EdgeList::iterator iterator;
0304 clear_vertex(u, g);
0305 g.erase(g.begin() + u);
0306 for (std::size_t i = 0; i < g.size(); ++i)
0307 for (iterator it = g[i].begin(); it != g[i].end(); ++it)
0308
0309 if (*it > u)
0310 --*it;
0311 }
0312
0313 template < typename EdgeList, typename Allocator >
0314 struct property_map< std::vector< EdgeList, Allocator >, vertex_index_t >
0315 {
0316 typedef identity_property_map type;
0317 typedef type const_type;
0318 };
0319
0320 template < typename EdgeList, typename Allocator >
0321 identity_property_map get(
0322 vertex_index_t, const std::vector< EdgeList, Allocator >&)
0323 {
0324 return identity_property_map();
0325 }
0326
0327 template < typename EdgeList, typename Allocator >
0328 identity_property_map get(vertex_index_t, std::vector< EdgeList, Allocator >&)
0329 {
0330 return identity_property_map();
0331 }
0332 }
0333
0334 #endif