graph.hh 7.28 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  Tiago de Paula Peixoto <tiago@forked.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
20

#ifndef GRAPH_HH
#define GRAPH_HH

Tiago Peixoto's avatar
Tiago Peixoto committed
21
22
#include <deque>

Tiago Peixoto's avatar
Tiago Peixoto committed
23
24
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
25

26
#include "fast_vector_property_map.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
27
28
#include <boost/variant.hpp>
#include <boost/python/object.hpp>
29
#include <boost/python/dict.hpp>
30
#include <boost/mpl/vector.hpp>
31
#include "graph_properties.hh"
Tiago Peixoto's avatar
Tiago Peixoto committed
32
33
34
35
#include "config.h"

namespace graph_tool
{
36
37
using namespace std;
using namespace boost;
Tiago Peixoto's avatar
Tiago Peixoto committed
38
39

// GraphInterface
40
41
42
// 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
43

44
45
46
namespace detail
{
// Generic graph_action functor. See graph_filtering.hh for details.
47
template <class Action, class GraphViews, class Wrap = mpl::false_,
48
49
50
51
52
          class TR1=boost::mpl::vector<>, class TR2=boost::mpl::vector<>,
          class TR3=boost::mpl::vector<>, class TR4=boost::mpl::vector<> >
struct graph_action;
}

Tiago Peixoto's avatar
Tiago Peixoto committed
53
54
55
class GraphInterface
{
public:
56
    GraphInterface();
57
    GraphInterface(const GraphInterface& gi);
Tiago Peixoto's avatar
Tiago Peixoto committed
58
59
    ~GraphInterface();

60
61
62
    // useful enums

    typedef enum
Tiago Peixoto's avatar
Tiago Peixoto committed
63
    {
64
65
        IN_DEGREE,
        OUT_DEGREE,
66
67
        TOTAL_DEGREE
    } degree_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
68

69
70
71
    // 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
72

73
74
75
    //
    // Basic manipulation
    //
Tiago Peixoto's avatar
Tiago Peixoto committed
76

77
78
    size_t GetNumberOfVertices();
    size_t GetNumberOfEdges();
79
    void SetDirected(bool directed) {_directed = directed;}
80
    bool GetDirected() {return _directed;}
81
    void SetReversed(bool reversed) {_reversed = reversed;}
82
    bool GetReversed() {return _reversed;}
83
84

    // graph filtering
85
    void SetVertexFilterProperty(boost::any prop, bool invert);
86
    bool IsVertexFilterActive() const;
87
    void SetEdgeFilterProperty(boost::any prop, bool invert);
88
89
90
    bool IsEdgeFilterActive() const;

    // graph modification
91
    void InsertPropertyMap(string name, boost::any map);
92
93
94
    void ReIndexEdges();
    void PurgeVertices(); // removes filtered vertices
    void PurgeEdges();    // removes filtered edges
95
    void Clear();
96
97
98
99
100
101
    void ClearEdges();
    void ShiftVertexProperty(boost::any map, size_t index) const;
    void CopyVertexProperty(const GraphInterface& src, boost::any prop_src,
                            boost::any prop_tgt);
    void CopyEdgeProperty(const GraphInterface& src, boost::any prop_src,
                          boost::any prop_tgt);
102
103

    //
104
    // python interface
105
    //
106
107
    python::object DegreeMap(string deg) const;

108
109
    // used for graph properties
    graph_property_tag GetDescriptor() const { return graph_property_tag(); }
110
    bool CheckValid() const {return true;}
111

112
113
114
115
    // I/O
    void WriteToFile(string s, python::object pf, string format,
                     python::list properties);
    python::tuple ReadFromFile(string s, python::object pf, string format);
Tiago Peixoto's avatar
Tiago Peixoto committed
116

117
118
119
120
    //
    // Internal types
    //

Tiago Peixoto's avatar
Tiago Peixoto committed
121
    // the following defines the edges' internal properties
122
    typedef property<edge_index_t, size_t> EdgeProperty;
Tiago Peixoto's avatar
Tiago Peixoto committed
123
124

    // this is the main graph type
125
126
127
128
129
130
131
    typedef adjacency_list <vecS, // edges
                            vecS, // vertices
                            bidirectionalS,
                            no_property,
                            EdgeProperty>  multigraph_t;
    typedef graph_traits<multigraph_t>::vertex_descriptor vertex_t;
    typedef graph_traits<multigraph_t>::edge_descriptor edge_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
132

133
134
    typedef property_map<multigraph_t,vertex_index_t>::type vertex_index_map_t;
    typedef property_map<multigraph_t,edge_index_t>::type edge_index_map_t;
135
    typedef ConstantPropertyMap<size_t,graph_property_tag> graph_index_map_t;
136

137
138
139
140
141
    // internal access

    multigraph_t& GetGraph() {return _mg;}
    vertex_index_map_t GetVertexIndex() {return _vertex_index;}
    edge_index_map_t   GetEdgeIndex()   {return _edge_index;}
142
143
    size_t             GetMaxEdgeIndex(){return _max_edge_index;}

144
    graph_index_map_t  GetGraphIndex()  {return graph_index_map_t(0);}
Tiago Peixoto's avatar
Tiago Peixoto committed
145

Tiago Peixoto's avatar
Tiago Peixoto committed
146
147
148
    void           AddEdgeIndex(const edge_t& e);
    void           RemoveEdgeIndex(const edge_t& e);

149
150
    // Gets the encapsulated graph view. See graph_filtering.cc for details
    boost::any GetGraphView() const;
151

Tiago Peixoto's avatar
Tiago Peixoto committed
152
153
private:

154
155
156
157
158
    // Generic graph_action functor. See graph_filtering.hh for details.
    template <class Action,
              class TR1=boost::mpl::vector<>, class TR2=boost::mpl::vector<>,
              class TR3=boost::mpl::vector<>, class TR4=boost::mpl::vector<> >
    friend struct detail::graph_action;
Tiago Peixoto's avatar
Tiago Peixoto committed
159

160
161
162
163
164
    // python interface
    friend class PythonVertex;
    template <class Graph>
    friend class PythonEdge;

165
    // this is the main graph
Tiago Peixoto's avatar
Tiago Peixoto committed
166
167
    multigraph_t _mg;

168
169
170
171
    // keep track of the number of edges, since num_edges() is O(V) in
    // adjacency_list... :-(
    size_t _nedges;

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

175
    // reverse and directed states
Tiago Peixoto's avatar
Tiago Peixoto committed
176
177
178
179
180
181
182
183
    bool _reversed;
    bool _directed;

    // 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
184
185
186
187
    deque<size_t> _free_indexes; // indexes of deleted edges to be used up for
                                 // new edges to avoid very large indexes, and
                                 // property map memory usage
    size_t _max_edge_index;
Tiago Peixoto's avatar
Tiago Peixoto committed
188

189
190
    // graph index map
    graph_index_map_t _graph_index;
Tiago Peixoto's avatar
Tiago Peixoto committed
191
192

    // vertex filter
193
    typedef unchecked_vector_property_map<uint8_t,vertex_index_map_t>
194
        vertex_filter_t;
195
196
197
    vertex_filter_t _vertex_filter_map;
    bool _vertex_filter_invert;
    bool _vertex_filter_active;
Tiago Peixoto's avatar
Tiago Peixoto committed
198
199

    // edge filter
200
    typedef unchecked_vector_property_map<uint8_t,edge_index_map_t> edge_filter_t;
201
202
203
    edge_filter_t _edge_filter_map;
    bool _edge_filter_invert;
    bool _edge_filter_active;
Tiago Peixoto's avatar
Tiago Peixoto committed
204
205
};

206
207
208
// Exceptions
// ==========
//
209
210
// This is the main exception which will be thrown the outside world, when
// things go wrong
Tiago Peixoto's avatar
Tiago Peixoto committed
211

212
class GraphException : public std::exception
Tiago Peixoto's avatar
Tiago Peixoto committed
213
{
214
    string _error;
Tiago Peixoto's avatar
Tiago Peixoto committed
215
public:
216
    GraphException(const string& error) {_error = error;}
Tiago Peixoto's avatar
Tiago Peixoto committed
217
218
    virtual ~GraphException() throw () {}
    virtual const char * what () const throw () {return _error.c_str();}
219
220
protected:
    virtual void SetError(const string& error) {_error = error;}
Tiago Peixoto's avatar
Tiago Peixoto committed
221
222
};

Tiago Peixoto's avatar
Tiago Peixoto committed
223
224
225
226
227
228
229
230
231
232
233
234
235
class IOException : public GraphException
{
public:
    IOException(const string& error): GraphException(error) {}
};

class ValueException : public GraphException
{
public:
    ValueException(const string& error): GraphException(error) {}
};


Tiago Peixoto's avatar
Tiago Peixoto committed
236
237
238
239
240
241
} //namespace graph_tool

#endif