//======================================================================= // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. // Copyright 2006 The Trustees of Indiana University. // Copyright (C) 2001 Vladimir Prus // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek, Douglas Gregor // // 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) //======================================================================= // The mutating functions (add_edge, etc.) were added by Vladimir Prus. #ifndef BOOST_VECTOR_AS_GRAPH_HPP #define BOOST_VECTOR_AS_GRAPH_HPP #include #include #include #include #include #include #include #include #include #include #include /* This module implements the VertexListGraph concept using a std::vector as the "back-bone" of the graph (the vector *is* the graph object). The edge-lists type of the graph is templated, so the user can choose any STL container, so long as the value_type of the container is convertible to the size_type of the vector. For now any graph properties must be stored seperately. This module requires the C++ compiler to support partial specialization for the graph_traits class, so this is not portable to VC++. */ #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION #error The vector-as-graph module requires a compiler that supports partial specialization #endif namespace boost { namespace detail { template struct val_out_edge_ret; template struct val_out_edge_iter; template struct val_edge; } } #if !defined BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION namespace boost { struct vector_as_graph_traversal_tag : public vertex_list_graph_tag, public adjacency_graph_tag, public incidence_graph_tag { }; template struct graph_traits< std::vector > { typedef typename EdgeList::value_type V; typedef V vertex_descriptor; typedef typename detail::val_edge::type edge_descriptor; typedef typename EdgeList::const_iterator adjacency_iterator; typedef typename detail::val_out_edge_iter::type out_edge_iterator; typedef void in_edge_iterator; typedef void edge_iterator; typedef counting_iterator vertex_iterator; typedef directed_tag directed_category; typedef allow_parallel_edge_tag edge_parallel_category; typedef vector_as_graph_traversal_tag traversal_category; typedef typename std::vector::size_type vertices_size_type; typedef void edges_size_type; typedef typename EdgeList::size_type degree_size_type; static V null_vertex() {return V(-1);} }; template struct edge_property_type< std::vector > { typedef void type; }; template struct vertex_property_type< std::vector > { typedef void type; }; template struct graph_property_type< std::vector > { typedef void type; }; } #endif namespace boost { namespace detail { // "val" is short for Vector Adjacency List template struct val_edge { typedef typename EdgeList::value_type V; typedef std::pair type; }; // need rewrite this using boost::iterator_adaptor template class val_out_edge_iterator : public boost::iterator, std::ptrdiff_t, std::pair*, const std::pair > { typedef val_out_edge_iterator self; typedef std::pair Edge; public: val_out_edge_iterator() { } val_out_edge_iterator(V s, Iter i) : _source(s), _iter(i) { } Edge operator*() const { return Edge(_source, *_iter); } self& operator++() { ++_iter; return *this; } self operator++(int) { self t = *this; ++_iter; return t; } bool operator==(const self& x) const { return _iter == x._iter; } bool operator!=(const self& x) const { return _iter != x._iter; } protected: V _source; Iter _iter; }; template struct val_out_edge_iter { typedef typename EdgeList::value_type V; typedef typename EdgeList::const_iterator Iter; typedef val_out_edge_iterator type; }; template struct val_out_edge_ret { typedef typename val_out_edge_iter::type IncIter; typedef std::pair type; }; } // namesapce detail template typename detail::val_out_edge_ret::type out_edges(typename EdgeList::value_type v, const std::vector& g) { typedef typename detail::val_out_edge_iter::type Iter; typedef typename detail::val_out_edge_ret::type return_type; return return_type(Iter(v, g[v].begin()), Iter(v, g[v].end())); } template typename EdgeList::size_type out_degree(typename EdgeList::value_type v, const std::vector& g) { return g[v].size(); } template std::pair adjacent_vertices(typename EdgeList::value_type v, const std::vector& g) { return std::make_pair(g[v].begin(), g[v].end()); } // source() and target() already provided for pairs in graph_traits.hpp template std::pair, boost::counting_iterator > vertices(const std::vector& v) { typedef boost::counting_iterator Iter; return std::make_pair(Iter(0), Iter(v.size())); } template typename std::vector::size_type num_vertices(const std::vector& v) { return v.size(); } template typename std::pair::type, bool> add_edge(typename EdgeList::value_type u, typename EdgeList::value_type v, std::vector& g) { typedef typename detail::val_edge::type edge_type; g[u].insert(g[u].end(), v); return std::make_pair(edge_type(u, v), true); } template typename std::pair::type, bool> edge(typename EdgeList::value_type u, typename EdgeList::value_type v, std::vector& g) { typedef typename detail::val_edge::type edge_type; typename EdgeList::iterator i = g[u].begin(), end = g[u].end(); for (; i != end; ++i) if (*i == v) return std::make_pair(edge_type(u, v), true); return std::make_pair(edge_type(), false); } template void remove_edge(typename EdgeList::value_type u, typename EdgeList::value_type v, std::vector& g) { typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v); if (i != g[u].end()) g[u].erase(i, g[u].end()); } template void remove_edge(typename detail::val_edge::type e, std::vector& g) { typename EdgeList::value_type u, v; u = e.first; v = e.second; // FIXME: edge type does not fully specify the edge to be deleted typename EdgeList::iterator i = std::remove(g[u].begin(), g[u].end(), v); if (i != g[u].end()) g[u].erase(i, g[u].end()); } template void remove_edge_if(Predicate p, std::vector& g) { for (std::size_t u = 0; u < g.size(); ++u) { // Oops! gcc gets internal compiler error on compose_....... typedef typename EdgeList::iterator iterator; iterator b = g[u].begin(), e = g[u].end(); if (!g[u].empty()) { for(; b != e;) { if (p(std::make_pair(u, *b))) { --e; if (b == e) break; else iter_swap(b, e); } else { ++b; } } } if (e != g[u].end()) g[u].erase(e, g[u].end()); } } template typename EdgeList::value_type add_vertex(std::vector& g) { g.resize(g.size()+1); return g.size()-1; } template void clear_vertex(typename EdgeList::value_type u, std::vector& g) { g[u].clear(); for (std::size_t i = 0; i < g.size(); ++i) remove_edge(i, u, g); } template void remove_vertex(typename EdgeList::value_type u, std::vector& g) { typedef typename EdgeList::iterator iterator; clear_vertex(u, g); g.erase(g.begin() + u); for (std::size_t i = 0; i < g.size(); ++i) for ( iterator it = g[i].begin(); it != g[i].end(); ++it ) // after clear_vertex *it is never equal to u if ( *it > u ) --*it; } template struct property_map, vertex_index_t> { typedef identity_property_map type; typedef type const_type; }; template identity_property_map get(vertex_index_t, const std::vector&) { return identity_property_map(); } template identity_property_map get(vertex_index_t, std::vector&) { return identity_property_map(); } } // namespace boost #endif // BOOST_VECTOR_AS_GRAPH_HPP