graph_astar.hh 3.88 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) 2006-2016 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
//
// 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_filtering.hh"
#include "graph_python_interface.hh"

#include <boost/python.hpp>

#include "graph.hh"
#include "graph_selectors.hh"
#include "graph_util.hh"

namespace graph_tool
{

using namespace std;
using namespace boost;

class AStarVisitorWrapper
{
public:
36
    AStarVisitorWrapper(GraphInterface& gi, python::object vis)
Tiago Peixoto's avatar
Tiago Peixoto committed
37
38
39
        : _gi(gi), _vis(vis) {}

    template <class Vertex, class Graph>
40
    void initialize_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
41
    {
42
        auto gp = retrieve_graph_view<Graph>(_gi, g);
43
        _vis.attr("initialize_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
44
45
46
    }

    template <class Vertex, class Graph>
47
    void discover_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
48
    {
49
        auto gp = retrieve_graph_view<Graph>(_gi, g);
50
        _vis.attr("discover_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
51
52
53
    }

    template <class Vertex, class Graph>
54
    void examine_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
55
    {
56
        auto gp = retrieve_graph_view<Graph>(_gi, g);
57
        _vis.attr("examine_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
58
59
60
    }

    template <class Edge, class Graph>
61
    void examine_edge(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
62
    {
63
        auto gp = retrieve_graph_view<Graph>(_gi, g);
64
        _vis.attr("examine_edge") (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
65
66
67
    }

    template <class Edge, class Graph>
68
    void edge_relaxed(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
69
    {
70
        auto gp = retrieve_graph_view<Graph>(_gi, g);
71
        _vis.attr("edge_relaxed") (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
72
73
74
    }

    template <class Edge, class Graph>
75
    void edge_not_relaxed(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
76
    {
77
        auto gp = retrieve_graph_view<Graph>(_gi, g);
78
        _vis.attr("edge_not_relaxed") (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
79
80
81
    }

    template <class Edge, class Graph>
82
    void black_target(Edge e, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
83
    {
84
        auto gp = retrieve_graph_view<Graph>(_gi, g);
85
        _vis.attr("black_target") (PythonEdge<Graph>(gp, e));
Tiago Peixoto's avatar
Tiago Peixoto committed
86
87
88
    }

    template <class Vertex, class Graph>
89
    void finish_vertex(Vertex u, Graph& g)
Tiago Peixoto's avatar
Tiago Peixoto committed
90
    {
91
        auto gp = retrieve_graph_view<Graph>(_gi, g);
92
        _vis.attr("finish_vertex")(PythonVertex<Graph>(gp, u));
Tiago Peixoto's avatar
Tiago Peixoto committed
93
94
95
    }

private:
96
97
    GraphInterface& _gi;
    python::object _vis;
Tiago Peixoto's avatar
Tiago Peixoto committed
98
99
100
101
102
103
};


class AStarCmp
{
public:
104
    AStarCmp() {}
Tiago Peixoto's avatar
Tiago Peixoto committed
105
106
107
108
109
    AStarCmp(python::object cmp): _cmp(cmp) {}

    template <class Value1, class Value2>
    bool operator()(const Value1& v1, const Value2& v2) const
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
110
        return python::extract<bool>(_cmp(v1, v2));
Tiago Peixoto's avatar
Tiago Peixoto committed
111
112
113
114
115
116
117
118
119
    }

private:
    python::object _cmp;
};

class AStarCmb
{
public:
120
    AStarCmb() {}
Tiago Peixoto's avatar
Tiago Peixoto committed
121
122
123
124
125
126
127
128
129
130
131
132
    AStarCmb(python::object cmb): _cmb(cmb) {}

    template <class Value1, class Value2 >
    Value1 operator()(const Value1& v1, const Value2& v2) const
    {
        return python::extract<Value1>(_cmb(v1, v2));
    }

private:
    python::object _cmb;
};

133
template <class Graph, class Value>
Tiago Peixoto's avatar
Tiago Peixoto committed
134
135
136
class AStarH
{
public:
137
    AStarH() {}
138
    AStarH(GraphInterface& gi, Graph& g, python::object h)
139
        : _h(h), _gp(retrieve_graph_view<Graph>(gi, g)) {}
Tiago Peixoto's avatar
Tiago Peixoto committed
140
141
142

    Value operator()(GraphInterface::vertex_t v) const
    {
143
        return python::extract<Value>(_h(PythonVertex<Graph>(_gp, v)));
Tiago Peixoto's avatar
Tiago Peixoto committed
144
145
146
    }

private:
147
148
    python::object _h;
    std::shared_ptr<Graph> _gp;
Tiago Peixoto's avatar
Tiago Peixoto committed
149
150
151
};

} // namespace graph_tool