graph.cc 2.87 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
#include <iostream>
Tiago Peixoto's avatar
Tiago Peixoto committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

#include <unistd.h>    /* standard unix functions, like getpid()         */
#include <sys/types.h> /* various type definitions, like pid_t           */
#include <signal.h>    /* signal name macros, and the signal() prototype */

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

using namespace std;
using namespace boost;
using namespace boost::lambda;
using namespace graph_tool;


36
// this is the constructor for the graph interface
Tiago Peixoto's avatar
Tiago Peixoto committed
37
GraphInterface::GraphInterface()
38
39
40
41
42
    :_mg(),
     _reversed(false),
     _directed(true),
     _vertex_index(get(vertex_index,_mg)),
     _edge_index(get(edge_index_t(),_mg)),
43
     _graph_index(0),
44
     _vertex_filter_map(_vertex_index),
45
46
     _vertex_filter_invert(false),
     _vertex_filter_active(false),
47
     _edge_filter_map(_edge_index),
48
49
     _edge_filter_invert(false),
     _edge_filter_active(false)
Tiago Peixoto's avatar
Tiago Peixoto committed
50
51
52
53
{

}

54
// the destructor
Tiago Peixoto's avatar
Tiago Peixoto committed
55
56
57
58
GraphInterface::~GraphInterface()
{
}

59
60
// 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
Tiago Peixoto's avatar
Tiago Peixoto committed
61
62
63
64
size_t GraphInterface::GetNumberOfVertices() const
{
    size_t n = 0;
    if (IsVertexFilterActive())
65
        run_action<>()(*this, var(n)=bind<size_t>(HardNumVertices(),_1))();
Tiago Peixoto's avatar
Tiago Peixoto committed
66
    else
67
        run_action<>()(*this, var(n)=bind<size_t>(SoftNumVertices(),_1))();
Tiago Peixoto's avatar
Tiago Peixoto committed
68
69
70
    return n;
}

71
72
73
// 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
Tiago Peixoto's avatar
Tiago Peixoto committed
74
75
76
77
size_t GraphInterface::GetNumberOfEdges() const
{
    size_t n = 0;
    if (IsEdgeFilterActive() || IsVertexFilterActive())
78
        run_action<>()(*this, var(n)=bind<size_t>(HardNumEdges(),_1))();
Tiago Peixoto's avatar
Tiago Peixoto committed
79
    else
80
        n = _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
{
86
    _mg.clear();
Tiago Peixoto's avatar
Tiago Peixoto committed
87
88
}

89
void GraphInterface::ClearEdges()
Tiago Peixoto's avatar
Tiago Peixoto committed
90
{
Tiago Peixoto's avatar
Tiago Peixoto committed
91
92
93
    graph_traits<multigraph_t>::vertex_iterator v, v_end;
    for (tie(v, v_end) = vertices(_mg); v != v_end; ++v)
        clear_vertex(*v, _mg);
Tiago Peixoto's avatar
Tiago Peixoto committed
94
}
95