graph.cc 3.08 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
#include <iostream>
Tiago Peixoto's avatar
Tiago Peixoto committed
19 20 21 22 23

#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_properties.hh"

24 25 26
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

Tiago Peixoto's avatar
Tiago Peixoto committed
27 28 29 30 31
using namespace std;
using namespace boost;
using namespace graph_tool;


32
// this is the constructor for the graph interface
Tiago Peixoto's avatar
Tiago Peixoto committed
33
GraphInterface::GraphInterface()
Tiago Peixoto's avatar
Tiago Peixoto committed
34 35 36
    :_state(new state_t()),
     _vertex_index(get(vertex_index, _state->_mg)),
     _edge_index(get(edge_index_t(), _state->_mg)),
37 38
     _reversed(false),
     _directed(true),
39
     _graph_index(0),
40
     _vertex_filter_map(_vertex_index),
41 42
     _vertex_filter_invert(false),
     _vertex_filter_active(false),
43
     _edge_filter_map(_edge_index),
44 45
     _edge_filter_invert(false),
     _edge_filter_active(false)
Tiago Peixoto's avatar
Tiago Peixoto committed
46
{
Tiago Peixoto's avatar
Tiago Peixoto committed
47
    _state->_nedges = _state->_max_edge_index = 0;
Tiago Peixoto's avatar
Tiago Peixoto committed
48 49
}

50
// the destructor
Tiago Peixoto's avatar
Tiago Peixoto committed
51 52 53 54
GraphInterface::~GraphInterface()
{
}

55 56
// this will get the number of vertices, either the "soft" O(1) way, or the hard
// O(V) way, which is necessary if the graph is filtered
57
size_t GraphInterface::GetNumberOfVertices()
Tiago Peixoto's avatar
Tiago Peixoto committed
58 59 60
{
    size_t n = 0;
    if (IsVertexFilterActive())
61 62
        run_action<>()(*this, lambda::var(n) =
                       lambda::bind<size_t>(HardNumVertices(),lambda::_1))();
Tiago Peixoto's avatar
Tiago Peixoto committed
63
    else
64 65
        run_action<>()(*this, lambda::var(n) =
                       lambda::bind<size_t>(SoftNumVertices(),lambda::_1))();
Tiago Peixoto's avatar
Tiago Peixoto committed
66 67 68
    return n;
}

69 70 71
// this will get the number of edges, either the "soft" O(E) way, or the hard
// O(E) way, which is necessary if the graph is filtered. Both cases are of
// linear complexity, since num_edges() is O(E) in Boost's adjacency_list
72
size_t GraphInterface::GetNumberOfEdges()
Tiago Peixoto's avatar
Tiago Peixoto committed
73
{
74
    using namespace boost::lambda;
Tiago Peixoto's avatar
Tiago Peixoto committed
75 76
    size_t n = 0;
    if (IsEdgeFilterActive() || IsVertexFilterActive())
77 78
        run_action<>()(*this, lambda::var(n) =
                       lambda::bind<size_t>(HardNumEdges(),lambda::_1))();
Tiago Peixoto's avatar
Tiago Peixoto committed
79
    else
Tiago Peixoto's avatar
Tiago Peixoto committed
80
        n = _state->_nedges;
Tiago Peixoto's avatar
Tiago Peixoto committed
81 82 83
    return n;
}

84
void GraphInterface::Clear()
Tiago Peixoto's avatar
Tiago Peixoto committed
85
{
Tiago Peixoto's avatar
Tiago Peixoto committed
86 87 88 89
    _state->_mg.clear();
    _state->_nedges = 0;
    _state->_max_edge_index = 0;
    _state->_free_indexes.clear();
Tiago Peixoto's avatar
Tiago Peixoto committed
90 91
}

92
void GraphInterface::ClearEdges()
Tiago Peixoto's avatar
Tiago Peixoto committed
93
{
Tiago Peixoto's avatar
Tiago Peixoto committed
94
    graph_traits<multigraph_t>::vertex_iterator v, v_end;
Tiago Peixoto's avatar
Tiago Peixoto committed
95 96 97 98 99
    for (tie(v, v_end) = vertices(_state->_mg); v != v_end; ++v)
        clear_vertex(*v, _state->_mg);
    _state->_nedges = 0;
    _state->_max_edge_index = 0;
    _state->_free_indexes.clear();
Tiago Peixoto's avatar
Tiago Peixoto committed
100
}
101