graph.hh 6.57 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-2015 Tiago de Paula Peixoto <tiago@skewed.de>
Tiago Peixoto's avatar
Tiago Peixoto committed
4
5
6
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
7
// as published by the Free Software Foundation; either version 3
Tiago Peixoto's avatar
Tiago Peixoto committed
8
9
10
11
12
13
14
15
// 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
16
// along with this program. If not, see <http://www.gnu.org/licenses/>.
Tiago Peixoto's avatar
Tiago Peixoto committed
17
18
19

#ifndef GRAPH_HH
#define GRAPH_HH
20
#include "config.h"
Tiago Peixoto's avatar
Tiago Peixoto committed
21

22
23
24
25
#include <Python.h>
#include <boost/python/object.hpp>
#include <boost/python/dict.hpp>

Tiago Peixoto's avatar
Tiago Peixoto committed
26
27
#include <deque>

28
29
#include "graph_adjacency.hh"

Tiago Peixoto's avatar
Tiago Peixoto committed
30
#include <boost/graph/graph_traits.hpp>
31

32
#include "fast_vector_property_map.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
33
#include <boost/variant.hpp>
34
#include <boost/mpl/vector.hpp>
35
#include "graph_properties.hh"
36
#include "graph_exceptions.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
37
38
39

namespace graph_tool
{
40
using namespace std;
Tiago Peixoto's avatar
Tiago Peixoto committed
41
42

// GraphInterface
43
44
45
// this class is the main interface to the internally kept graph. This is how
// the external world will manipulate the graph. All the algorithms should be
// registered here. This class will be exported to python in graph_bind.hh
Tiago Peixoto's avatar
Tiago Peixoto committed
46

47
48
49
namespace detail
{
// Generic graph_action functor. See graph_filtering.hh for details.
Tiago Peixoto's avatar
Tiago Peixoto committed
50
template <class Action, class GraphViews, class Wrap = boost::mpl::false_,
51
          class... TRS>
52
53
54
struct graph_action;
}

Tiago Peixoto's avatar
Tiago Peixoto committed
55
56
57
class GraphInterface
{
public:
58
    GraphInterface();
59
    GraphInterface(const GraphInterface& g, bool keep_ref,
Tiago Peixoto's avatar
Tiago Peixoto committed
60
61
                   boost::python::object ovprops, boost::python::object oeprops,
                   boost::python::object vorder);
Tiago Peixoto's avatar
Tiago Peixoto committed
62
63
    ~GraphInterface();

64
65
66
    // useful enums

    typedef enum
Tiago Peixoto's avatar
Tiago Peixoto committed
67
    {
68
69
        IN_DEGREE,
        OUT_DEGREE,
70
71
        TOTAL_DEGREE
    } degree_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
72

73
74
75
    // general "degree" type, i.e., either a degree_t above or a string
    // representing a scalar vertex property
    typedef boost::variant<degree_t, boost::any> deg_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
76

77
78
79
    //
    // Basic manipulation
    //
Tiago Peixoto's avatar
Tiago Peixoto committed
80

81
82
83
84
85
86
87
88
    size_t get_num_vertices(bool filtered = true);
    size_t get_num_edges(bool filtered = true);
    void set_directed(bool directed) {_directed = directed;}
    bool get_directed() {return _directed;}
    void set_reversed(bool reversed) {_reversed = reversed;}
    bool get_reversed() {return _reversed;}
    void set_keep_epos(bool keep) {_mg->set_keep_epos(keep);}
    bool get_keep_epos() {return _mg->get_keep_epos();}
89

90
91

    // graph filtering
92
93
94
95
    void set_vertex_filter_property(boost::any prop, bool invert);
    bool is_vertex_filter_active() const;
    void set_edge_filter_property(boost::any prop, bool invert);
    bool is_edge_filter_active() const;
96
97

    // graph modification
98
99
100
101
102
103
104
105
106
107
108
    void re_index_edges();
    void purge_vertices(boost::any old_index); // removes filtered vertices
    void purge_edges();    // removes filtered edges
    void clear();
    void clear_edges();
    void shift_vertex_property(boost::any map, boost::python::object oindex) const;
    void move_vertex_property(boost::any map, boost::python::object oindex) const;
    void re_index_vertex_property(boost::any map, boost::any old_index) const;
    void copy_vertex_property(const GraphInterface& src, boost::any prop_src,
                              boost::any prop_tgt);
    void copy_edge_property(const GraphInterface& src, boost::any prop_src,
109
                            boost::any prop_tgt);
110
    void shrink_to_fit() { _mg->shrink_to_fit(); }
111
112

    //
113
    // python interface
114
    //
115
    boost::python::object degree_map(string deg, boost::any weight) const;
116

117
    // used for graph properties
118
119
    boost::graph_property_tag get_descriptor() const { return boost::graph_property_tag(); }
    bool check_valid() const {return true;}
120

121
    // I/O
122
123
124
125
126
127
128
    void write_to_file(string s, boost::python::object pf, string format,
                       boost::python::list properties);
    boost::python::tuple read_from_file(string s, boost::python::object pf,
                                        string format,
                                        boost::python::list ignore_vp,
                                        boost::python::list ignore_ep,
                                        boost::python::list ignore_gp);
Tiago Peixoto's avatar
Tiago Peixoto committed
129

130
131
132
133
    //
    // Internal types
    //

Tiago Peixoto's avatar
Tiago Peixoto committed
134
135
136
    typedef boost::adj_list<size_t> multigraph_t;
    typedef boost::graph_traits<multigraph_t>::vertex_descriptor vertex_t;
    typedef boost::graph_traits<multigraph_t>::edge_descriptor edge_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
137

Tiago Peixoto's avatar
Tiago Peixoto committed
138
139
140
    typedef boost::property_map<multigraph_t, boost::vertex_index_t>::type vertex_index_map_t;
    typedef boost::property_map<multigraph_t, boost::edge_index_t>::type edge_index_map_t;
    typedef ConstantPropertyMap<size_t, boost::graph_property_tag> graph_index_map_t;
141

142
143
    // internal access

144
145
146
147
    multigraph_t&      get_graph() {return *_mg;}
    std::shared_ptr<multigraph_t> get_graph_ptr() {return _mg;}
    vertex_index_map_t get_vertex_index()   {return _vertex_index;}
    edge_index_map_t   get_edge_index()     {return _edge_index;}
148
    size_t             get_edge_index_range() {return _mg->get_edge_index_range();}
149

150
    graph_index_map_t  get_graph_index()  {return graph_index_map_t(0);}
Tiago Peixoto's avatar
Tiago Peixoto committed
151

152
    // Gets the encapsulated graph view. See graph_filtering.cc for details
153
154
    boost::any get_graph_view() const;
    vector<boost::any>& get_graph_views() {return _graph_views;}
155

Tiago Peixoto's avatar
Tiago Peixoto committed
156
157
private:

158
    // Generic graph_action functor. See graph_filtering.hh for details.
159
    template <class Action, class GraphViews, class Wrap, class... TRS>
160
    friend struct detail::graph_action;
Tiago Peixoto's avatar
Tiago Peixoto committed
161

162
163
    // this is the main graph
    shared_ptr<multigraph_t> _mg;
Tiago Peixoto's avatar
Tiago Peixoto committed
164
165
166
167
168
169

    // vertex index map
    vertex_index_map_t _vertex_index;

    // edge index map
    edge_index_map_t _edge_index;
Tiago Peixoto's avatar
Tiago Peixoto committed
170
171
172
173
174
175
176

    // this will hold an instance of the graph views at run time
    vector<boost::any> _graph_views;

    // reverse and directed states
    bool _reversed;
    bool _directed;
Tiago Peixoto's avatar
Tiago Peixoto committed
177

178
179
    // graph index map
    graph_index_map_t _graph_index;
Tiago Peixoto's avatar
Tiago Peixoto committed
180
181

    // vertex filter
Tiago Peixoto's avatar
Tiago Peixoto committed
182
    typedef boost::unchecked_vector_property_map<uint8_t,vertex_index_map_t>
183
        vertex_filter_t;
184
185
186
    vertex_filter_t _vertex_filter_map;
    bool _vertex_filter_invert;
    bool _vertex_filter_active;
Tiago Peixoto's avatar
Tiago Peixoto committed
187
188

    // edge filter
Tiago Peixoto's avatar
Tiago Peixoto committed
189
    typedef boost::unchecked_vector_property_map<uint8_t,edge_index_map_t>
Tiago Peixoto's avatar
Tiago Peixoto committed
190
        edge_filter_t;
191
192
193
    edge_filter_t _edge_filter_map;
    bool _edge_filter_invert;
    bool _edge_filter_active;
Tiago Peixoto's avatar
Tiago Peixoto committed
194
195
};

196

Tiago Peixoto's avatar
Tiago Peixoto committed
197
198
199
} //namespace graph_tool

#endif