graph.hh 6.86 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-2012 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

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

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

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

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

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

45 46 47
namespace detail
{
// Generic graph_action functor. See graph_filtering.hh for details.
48
template <class Action, class GraphViews, class Wrap = mpl::false_,
49 50 51 52 53
          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
54 55 56
class GraphInterface
{
public:
57
    GraphInterface();
Tiago Peixoto's avatar
Tiago Peixoto committed
58
    GraphInterface(const GraphInterface& g, bool keep_ref);
Tiago Peixoto's avatar
Tiago Peixoto committed
59 60
    ~GraphInterface();

61 62 63
    // useful enums

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

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

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

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

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

    // graph modification
92
    void InsertPropertyMap(string name, boost::any map);
93
    void ReIndexEdges();
94
    void PurgeVertices(boost::any old_index); // removes filtered vertices
95
    void PurgeEdges();    // removes filtered edges
96
    void Clear();
97 98
    void ClearEdges();
    void ShiftVertexProperty(boost::any map, size_t index) const;
99
    void ReIndexVertexProperty(boost::any map, boost::any old_index) const;
100 101 102 103
    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);
104 105

    //
106
    // python interface
107
    //
108 109
    python::object DegreeMap(string deg) const;

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

114 115 116 117
    // 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
118

119 120 121 122
    //
    // Internal types
    //

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

    // this is the main graph type
127 128 129 130 131 132 133
    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
134

135 136
    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;
137
    typedef ConstantPropertyMap<size_t,graph_property_tag> graph_index_map_t;
138

139 140
    // internal access

Tiago Peixoto's avatar
Tiago Peixoto committed
141
    multigraph_t& GetGraph() {return _state->_mg;}
142 143
    vertex_index_map_t GetVertexIndex() {return _vertex_index;}
    edge_index_map_t   GetEdgeIndex()   {return _edge_index;}
Tiago Peixoto's avatar
Tiago Peixoto committed
144
    size_t             GetMaxEdgeIndex(){return _state->_max_edge_index;}
145

146
    graph_index_map_t  GetGraphIndex()  {return graph_index_map_t(0);}
Tiago Peixoto's avatar
Tiago Peixoto committed
147

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

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

Tiago Peixoto's avatar
Tiago Peixoto committed
154 155
private:

156 157 158 159 160
    // 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
161

162 163 164 165 166
    // python interface
    friend class PythonVertex;
    template <class Graph>
    friend class PythonEdge;

Tiago Peixoto's avatar
Tiago Peixoto committed
167 168 169 170
    struct state_t
    {
        // this is the main graph
        multigraph_t _mg;
Tiago Peixoto's avatar
Tiago Peixoto committed
171

Tiago Peixoto's avatar
Tiago Peixoto committed
172 173 174
        // keep track of the number of edges, since num_edges() is O(V) in
        // adjacency_list... :-(
        size_t _nedges;
175

Tiago Peixoto's avatar
Tiago Peixoto committed
176 177 178 179 180
        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;
    };
181

Tiago Peixoto's avatar
Tiago Peixoto committed
182
    shared_ptr<state_t> _state;
Tiago Peixoto's avatar
Tiago Peixoto committed
183 184 185 186 187 188

    // 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
189 190 191 192 193 194 195

    // 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
196

197 198
    // graph index map
    graph_index_map_t _graph_index;
Tiago Peixoto's avatar
Tiago Peixoto committed
199 200

    // vertex filter
201
    typedef unchecked_vector_property_map<uint8_t,vertex_index_map_t>
202
        vertex_filter_t;
203 204 205
    vertex_filter_t _vertex_filter_map;
    bool _vertex_filter_invert;
    bool _vertex_filter_active;
Tiago Peixoto's avatar
Tiago Peixoto committed
206 207

    // edge filter
Tiago Peixoto's avatar
Tiago Peixoto committed
208 209
    typedef unchecked_vector_property_map<uint8_t,edge_index_map_t>
        edge_filter_t;
210 211 212
    edge_filter_t _edge_filter_map;
    bool _edge_filter_invert;
    bool _edge_filter_active;
Tiago Peixoto's avatar
Tiago Peixoto committed
213 214 215 216 217
};

} //namespace graph_tool

#endif