graph_properties_imp1.cc 2.91 KB
Newer Older
1
2
3
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
// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2015 Tiago de Paula Peixoto <tiago@skewed.de>
//
// 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/>.

#include "graph_python_interface.hh"
#include "graph.hh"
#include "graph_properties.hh"
#include "graph_filtering.hh"
#include "graph_selectors.hh"
#include "graph_util.hh"

using namespace std;
using namespace boost;
using namespace graph_tool;

template <bool src>
struct do_edge_endpoint
{
    template <class Graph, class EdgeIndexMap, class VertexPropertyMap>
    void operator()(Graph& g, EdgeIndexMap, VertexPropertyMap prop,
                    boost::any aeprop) const
    {
        typedef typename property_traits<VertexPropertyMap>::value_type vval_t;
        typedef typename boost::mpl::if_<std::is_same<vval_t, size_t>, int64_t, vval_t>::type
            val_t;
        typedef typename property_map_type::apply<val_t, EdgeIndexMap>::type
            eprop_t;
        eprop_t eprop = any_cast<eprop_t>(aeprop);
        eprop.reserve(num_edges(g));

        int i, N = num_vertices(g);
Tiago Peixoto's avatar
Tiago Peixoto committed
45
46
        #pragma omp parallel for default(shared) private(i)     \
            schedule(runtime) if (N > 100)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        for (i = 0; i < N; ++i)
        {
            typename graph_traits<Graph>::vertex_descriptor v = vertex(i, g);
            if (v == graph_traits<Graph>::null_vertex())
                continue;
            for (auto e : out_edges_range(v, g))
            {
                auto s = v;
                auto t = target(e, g);
                if (!is_directed::apply<Graph>::type::value && s > t)
                    continue;
                if (src)
                    eprop[e] = prop[s];
                else
                    eprop[e] = prop[t];
            }
        }
    }
};

void edge_endpoint(GraphInterface& gi, boost::any prop,
                   boost::any eprop, std::string endpoint)
{
    if (endpoint == "source")
Tiago Peixoto's avatar
Tiago Peixoto committed
71
72
        run_action<>()(gi, std::bind(do_edge_endpoint<true>(), placeholders::_1,
                                     gi.GetEdgeIndex(), placeholders::_2, eprop),
73
74
                       vertex_properties())(prop);
    else
Tiago Peixoto's avatar
Tiago Peixoto committed
75
76
        run_action<>()(gi, std::bind(do_edge_endpoint<false>(), placeholders::_1,
                                     gi.GetEdgeIndex(), placeholders::_2, eprop),
77
78
                       vertex_properties())(prop);
}