graph_reverse.hh 14.7 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-2018 Tiago de Paula Peixoto <tiago@skewed.de>
4 5 6 7 8 9 10 11 12 13 14 15 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
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

//  (C) Copyright David Abrahams 2000.
// 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_REVERSE
# define GRAPH_REVERSE

#include <boost/graph/adjacency_iterator.hpp>
#include <boost/graph/properties.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/type_traits.hpp>
#include <boost/mpl/if.hpp>
#include <functional>

#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
// Stay out of the way of the concept checking class
# define BidirectionalGraph BidirectionalGraph_
#endif

namespace boost {

struct reversed_graph_tag { };

template <class BidirectionalGraph, class GraphRef = const BidirectionalGraph&>
class reversed_graph {
    typedef reversed_graph<BidirectionalGraph, GraphRef> Self;
    typedef graph_traits<BidirectionalGraph> Traits;
 public:
    typedef BidirectionalGraph base_type;

    // Constructor
    reversed_graph(GraphRef g) : _g(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 typename Traits::in_edge_iterator out_edge_iterator;
    typedef typename Traits::degree_size_type degree_size_type;

    // BidirectionalGraph requirements
    typedef typename Traits::out_edge_iterator in_edge_iterator;

67 68 69
    typedef typename BidirectionalGraph::all_edge_iterator_reversed all_edge_iterator;
    typedef typename BidirectionalGraph::all_edge_iterator all_edge_iterator_reversed;

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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    // AdjacencyGraph requirements
    typedef typename BidirectionalGraph::in_adjacency_iterator adjacency_iterator;
    typedef typename graph_traits<BidirectionalGraph>::adjacency_iterator in_adjacency_iterator;

    // VertexListGraph requirements
    typedef typename Traits::vertex_iterator vertex_iterator;

    // EdgeListGraph requirements
    typedef typename Traits::edge_iterator edge_iterator;
    typedef typename Traits::vertices_size_type vertices_size_type;
    typedef typename Traits::edges_size_type edges_size_type;

    typedef reversed_graph_tag graph_tag;

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

    // would be private, but template friends aren't portable enough.
 // private:
    GraphRef _g;
};


// These are separate so they are not instantiated unless used (see bug 1021)
template <class BidirectionalGraph, class GraphRef>
struct vertex_property_type<reversed_graph<BidirectionalGraph, GraphRef> > {
    typedef typename boost::vertex_property_type<BidirectionalGraph>::type type;
};

template <class BidirectionalGraph, class GraphRef>
struct edge_property_type<reversed_graph<BidirectionalGraph, GraphRef> > {
    typedef typename boost::edge_property_type<BidirectionalGraph>::type type;
};

template <class BidirectionalGraph, class GraphRef>
struct graph_property_type<reversed_graph<BidirectionalGraph, GraphRef> > {
    typedef typename boost::graph_property_type<BidirectionalGraph>::type type;
};

template <class BidirectionalGraph>
inline reversed_graph<BidirectionalGraph>
make_reversed_graph(const BidirectionalGraph& g)
{
    return reversed_graph<BidirectionalGraph>(g);
}

template <class BidirectionalGraph>
inline reversed_graph<BidirectionalGraph, BidirectionalGraph&>
make_reversed_graph(BidirectionalGraph& g)
{
    return reversed_graph<BidirectionalGraph, BidirectionalGraph&>(g);
}

template <class BidirectionalGraph, class GRef>
std::pair<typename reversed_graph<BidirectionalGraph>::vertex_iterator,
          typename reversed_graph<BidirectionalGraph>::vertex_iterator>
vertices(const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return vertices(g._g);
}

template <class BidirectionalGraph, class GRef>
std::pair<typename reversed_graph<BidirectionalGraph>::edge_iterator,
          typename reversed_graph<BidirectionalGraph>::edge_iterator>
edges(const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return edges(g._g);
}

template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph>::out_edge_iterator,
                 typename reversed_graph<BidirectionalGraph>::out_edge_iterator>
out_edges(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
          const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return in_edges(u, g._g);
}

template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::vertices_size_type
num_vertices(const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return num_vertices(g._g);
}

template <class BidirectionalGraph, class GRef>
inline typename reversed_graph<BidirectionalGraph>::edges_size_type
num_edges(const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return num_edges(g._g);
}

template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::degree_size_type
out_degree(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
           const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return in_degree(u, g._g);
}

template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::vertex_descriptor
vertex(const typename graph_traits<BidirectionalGraph>::vertices_size_type v,
       const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return vertex(v, g._g);
}

template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph>::edge_descriptor,
                 bool>
edge(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
     const typename graph_traits<BidirectionalGraph>::vertex_descriptor v,
     const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return edge(v, u, g._g);
}

template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph>::in_edge_iterator,
                 typename reversed_graph<BidirectionalGraph>::in_edge_iterator>
in_edges(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
         const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return out_edges(u, g._g);
}

197 198 199 200 201 202 203 204 205
template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph>::all_edge_iterator,
                 typename reversed_graph<BidirectionalGraph>::all_edge_iterator>
all_edges(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
          const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return _all_edges_reversed(u, g._g);
}

206 207 208
template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph,GRef>::adjacency_iterator,
                 typename reversed_graph<BidirectionalGraph,GRef>::adjacency_iterator>
209
out_neighbors(typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
210 211
               const reversed_graph<BidirectionalGraph,GRef>& g)
{
212
    return in_neighbors(u, g._g);
213 214 215 216 217
}

template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph,GRef>::in_adjacency_iterator,
                 typename reversed_graph<BidirectionalGraph,GRef>::in_adjacency_iterator>
218
in_neighbors(typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
219 220
              const reversed_graph<BidirectionalGraph,GRef>& g)
{
221
    return out_neighbors(u, g._g);
222 223
}

224 225 226
template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph,GRef>::adjacency_iterator,
                 typename reversed_graph<BidirectionalGraph,GRef>::adjacency_iterator>
227
all_neighbors(typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
228 229
               const reversed_graph<BidirectionalGraph,GRef>& g)
{
230
    return all_neighbors(u, g._g);
231 232
}

233 234 235 236 237 238
template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph,GRef>::adjacency_iterator,
                 typename reversed_graph<BidirectionalGraph,GRef>::adjacency_iterator>
adjacent_vertices(typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
                  const reversed_graph<BidirectionalGraph,GRef>& g)
{
239
    return out_neighbors(u, g);
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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
}


template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::degree_size_type
in_degree(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
          const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return out_degree(u, g._g);
}

template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::degree_size_type
degree(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
       const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return degree(u, g._g);
}


template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::vertex_descriptor
source(const typename reversed_graph<BidirectionalGraph,GRef>::edge_descriptor& e,
       const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return target(e, g._g);
}

template <class BidirectionalGraph, class GRef>
inline typename graph_traits<BidirectionalGraph>::vertex_descriptor
target(const typename reversed_graph<BidirectionalGraph,GRef>::edge_descriptor& e,
       const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return source(e, g._g);
}

template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph,GRef>::out_edge_iterator,
                 typename reversed_graph<BidirectionalGraph,GRef>::out_edge_iterator>
_all_edges_out(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
               const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return _all_edges_in(u, g._g);
}

template <class BidirectionalGraph, class GRef>
inline std::pair<typename reversed_graph<BidirectionalGraph,GRef>::in_edge_iterator,
                 typename reversed_graph<BidirectionalGraph,GRef>::in_edge_iterator>
_all_edges_in(const typename graph_traits<BidirectionalGraph>::vertex_descriptor u,
               const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return _all_edges_out(u, g._g);
}

template <class BidirectionalGraph, class GRef, class Pred>
inline void
clear_vertex(typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
                ::vertex_descriptor v,
             reversed_graph<BidirectionalGraph,GRef>& g, Pred&& pred)
{
    clear_vertex(v, const_cast<BidirectionalGraph&>(g._g), pred);
}

template <class BidirectionalGraph, class GRef>
inline
std::pair<typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
             ::edge_descriptor,bool>
add_edge(typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
             ::vertex_descriptor u,
         typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
             ::vertex_descriptor v,
         reversed_graph<BidirectionalGraph,GRef>& g)
{
    typedef typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
           ::edge_descriptor e_t;
    std::pair<typename boost::graph_traits<BidirectionalGraph>::edge_descriptor,
              bool> ret =
        add_edge(v, u, const_cast<BidirectionalGraph&>(g._g)); // insert reversed
    return std::make_pair(e_t(ret.first), ret.second);
}

template <class BidirectionalGraph, class GRef>
inline
void remove_edge(typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
                    ::edge_descriptor e,
                 reversed_graph<BidirectionalGraph,GRef>& g)
{
    return remove_edge(e,const_cast<BidirectionalGraph&>(g._g));
}

template <class BidirectionalGraph, class GRef>
inline
void remove_vertex(typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
                      ::vertex_descriptor v,
                   reversed_graph<BidirectionalGraph,GRef>& g)
{
    return remove_vertex(v,const_cast<BidirectionalGraph&>(g._g));
}

template <class BidirectionalGraph, class GRef>
inline
void remove_vertex_fast(typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
                            ::vertex_descriptor v,
                        reversed_graph<BidirectionalGraph,GRef>& g)
{
    return remove_vertex_fast(v,const_cast<BidirectionalGraph&>(g._g));
}

template <class BidirectionalGraph, class GRef>
inline
void clear_vertex(typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
                     ::vertex_descriptor v,
                  reversed_graph<BidirectionalGraph,GRef>& g)
{
    return clear_vertex(v,const_cast<BidirectionalGraph&>(g._g));
}

template <class BidirectionalGraph, class GRef>
inline
typename boost::graph_traits<reversed_graph<BidirectionalGraph,GRef>>
   ::vertex_descriptor
add_vertex(reversed_graph<BidirectionalGraph,GRef>& g)
{
    return add_vertex(const_cast<BidirectionalGraph&>(g._g));
}

//========================================================================
// Vertex and edge index property maps
//========================================================================

template <class BidirectionalGraph, class GRef, class Tag>
struct property_map<reversed_graph<BidirectionalGraph,GRef>, Tag>
{
    typedef typename property_map<BidirectionalGraph,Tag>::type type;
    typedef typename property_map<BidirectionalGraph,Tag>::const_type const_type;
};

template <class BidirectionalGraph, class GRef, class Tag>
struct property_map<const reversed_graph<BidirectionalGraph,GRef>, Tag>
{
    typedef typename property_map<const BidirectionalGraph,Tag>::type type;
    typedef typename property_map<const BidirectionalGraph,Tag>::const_type
        const_type;
};

template <class BidirectionalGraph, class GRef, class Tag>
inline auto
get(Tag t, reversed_graph<BidirectionalGraph,GRef>& g)
{
    return get(t, g._g);
}

template <class BidirectionalGraph, class GRef, class Tag>
inline auto
get(Tag t, const reversed_graph<BidirectionalGraph,GRef>& g)
{
    return get(t, g._g);
}

} // namespace boost

#endif