graph_union.hh 3.55 KB
Newer Older
Tiago Peixoto's avatar
Tiago Peixoto committed
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2007-2012 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
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
//
// 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/>.

#ifndef GRAPH_REWIRING_HH
#define GRAPH_REWIRING_HH

#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_util.hh"

namespace graph_tool
{
using namespace std;
using namespace boost;

struct graph_union
{
    template <class UnionGraph, class Graph, class VertexMap, class EdgeMap>
    void operator()(UnionGraph& ug, Graph* gp, VertexMap vmap, EdgeMap emap)
        const
    {
        Graph& g = *gp;
        typename graph_traits<Graph>::vertex_iterator v, v_end;
        for (tie(v,v_end) = vertices(g); v != v_end; ++v)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        {
            if (vmap[*v] == 0)
            {
                vmap[*v] = add_vertex(ug);
            }
            else
            {
                typename graph_traits<UnionGraph>::vertex_descriptor w =
                    vertex(vmap[*v] - 1, ug);
                if (w == graph_traits<UnionGraph>::null_vertex() ||
                    w >= num_vertices(g))
                    vmap[*v] = add_vertex(ug);
                else
                    vmap[*v] = w;
            }
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
55 56 57

        typename graph_traits<Graph>::edge_iterator e, e_end;
        for (tie(e,e_end) = edges(g); e != e_end; ++e)
58 59
            emap[*e] = add_edge(vertex(vmap[source(*e,g)], g),
                                vertex(vmap[target(*e,g)], g), ug).first;
Tiago Peixoto's avatar
Tiago Peixoto committed
60 61 62 63 64 65 66 67 68 69 70 71
    }
};


struct property_union
{
    template <class UnionGraph, class Graph, class VertexMap, class EdgeMap,
              class UnionProp>
    void operator()(UnionGraph& ug, Graph* gp, VertexMap vmap, EdgeMap emap,
                    UnionProp uprop, boost::any aprop) const
    {
        Graph& g = *gp;
72 73
        typename UnionProp::checked_t prop =
            any_cast<typename UnionProp::checked_t>(aprop);
Tiago Peixoto's avatar
Tiago Peixoto committed
74 75 76 77 78 79 80 81 82 83 84 85
        dispatch(ug, g, vmap, emap, uprop, prop,
                 is_same<typename property_traits<UnionProp>::key_type,
                         typename graph_traits<Graph>::vertex_descriptor>());
    }

    template <class UnionGraph, class Graph, class VertexMap, class EdgeMap,
              class UnionProp, class Prop>
    void dispatch(UnionGraph& ug, Graph& g, VertexMap vmap, EdgeMap emap,
                  UnionProp uprop, Prop prop, mpl::true_) const
    {
        typename graph_traits<Graph>::vertex_iterator v, v_end;
        for (tie(v,v_end) = vertices(g); v != v_end; ++v)
86
            uprop[vertex(vmap[*v], g)] = prop[*v];
Tiago Peixoto's avatar
Tiago Peixoto committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    }

    template <class UnionGraph, class Graph, class VertexMap, class EdgeMap,
              class UnionProp, class Prop>
    void dispatch(UnionGraph& ug, Graph& g, VertexMap vmap, EdgeMap emap,
                  UnionProp uprop, Prop prop, mpl::false_) const
    {
        typename graph_traits<Graph>::edge_iterator e, e_end;
        for (tie(e,e_end) = edges(g); e != e_end; ++e)
            uprop[emap[*e]] = prop[*e];
    }

};

} // graph_tool namespace

#endif // GRAPH_REWIRING_HH