graph_properties_imp1.cc 3.28 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-2017 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
//
// 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"

25
#ifdef _OPENMP
26
27
28
#include <omp.h>
#endif

29
30
31
32
33
34
35
36
37
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,
38
                    boost::any aeprop, size_t edge_index_range) const
39
40
41
42
43
44
45
    {
        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);
46
        eprop.reserve(edge_index_range);
47

48
        #ifdef _OPENMP
49
50
51
52
53
54
55
56
        size_t __attribute__ ((unused)) nt = omp_get_num_threads();
        if (std::is_convertible<val_t,python::object>::value)
            nt = 1; // python is not thread-safe
        #endif

        #pragma omp parallel if (num_vertices(g) > OPENMP_MIN_THRESH) \
            num_threads(nt)
        parallel_vertex_loop_no_spawn
Tiago Peixoto's avatar
Tiago Peixoto committed
57
58
59
60
61
62
63
            (g,
             [&](auto v)
             {
                 for (const auto& e : out_edges_range(v, g))
                 {
                     auto s = v;
                     auto t = target(e, g);
64
                     if (!graph_tool::is_directed(g) && s > t)
Tiago Peixoto's avatar
Tiago Peixoto committed
65
66
67
68
69
70
71
                         continue;
                     if (src)
                         eprop[e] = prop[s];
                     else
                         eprop[e] = prop[t];
                 }
             });
72
73
74
75
76
77
    }
};

void edge_endpoint(GraphInterface& gi, boost::any prop,
                   boost::any eprop, std::string endpoint)
{
78
    size_t edge_index_range = gi.get_edge_index_range();
79
    if (endpoint == "source")
80
81
        run_action<>()(gi, std::bind(do_edge_endpoint<true>(), std::placeholders::_1,
                                     gi.get_edge_index(), std::placeholders::_2, eprop,
82
                                     edge_index_range),
83
84
                       vertex_properties())(prop);
    else
85
86
        run_action<>()(gi, std::bind(do_edge_endpoint<false>(), std::placeholders::_1,
                                     gi.get_edge_index(), std::placeholders::_2, eprop,
87
                                     edge_index_range),
88
89
                       vertex_properties())(prop);
}