Commit 4d212c1b authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Switch from boost::adjacency_list<> to custom made and lightweight adj_list<>

Dumping boost::adjacency_list<> improves memory usage by a factor of
two, and also slightly improves performance in some cases.
parent 5b67db19
...@@ -30,23 +30,6 @@ namespace graph_tool ...@@ -30,23 +30,6 @@ namespace graph_tool
using namespace std; using namespace std;
using namespace boost; using namespace boost;
// this is to avoid using GraphWrap as the underlying graph type
struct get_graph_t
{
template <class Graph>
struct apply
{
typedef Graph type;
};
template <class Graph>
struct apply<GraphWrap<Graph> >
{
typedef Graph type;
};
};
class AStarVisitorWrapper class AStarVisitorWrapper
{ {
public: public:
...@@ -54,54 +37,54 @@ public: ...@@ -54,54 +37,54 @@ public:
: _gi(gi), _vis(vis) {} : _gi(gi), _vis(vis) {}
template <class Vertex, class Graph> template <class Vertex, class Graph>
void initialize_vertex(Vertex u, Graph& g) void initialize_vertex(Vertex u, const Graph&)
{ {
_vis.attr("initialize_vertex")(PythonVertex(_gi, u)); _vis.attr("initialize_vertex")(PythonVertex(_gi, u));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void discover_vertex(Vertex u, Graph& g) void discover_vertex(Vertex u, const Graph&)
{ {
_vis.attr("discover_vertex")(PythonVertex(_gi, u)); _vis.attr("discover_vertex")(PythonVertex(_gi, u));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void examine_vertex(Vertex u, Graph& g) void examine_vertex(Vertex u, const Graph&)
{ {
_vis.attr("examine_vertex")(PythonVertex(_gi, u)); _vis.attr("examine_vertex")(PythonVertex(_gi, u));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void examine_edge(Edge e, Graph& g) void examine_edge(Edge e, const Graph&)
{ {
_vis.attr("examine_edge") _vis.attr("examine_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_relaxed(Edge e, Graph& g) void edge_relaxed(Edge e, const Graph&)
{ {
_vis.attr("edge_relaxed") _vis.attr("edge_relaxed")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_not_relaxed(Edge e, Graph& g) void edge_not_relaxed(Edge e, const Graph& g)
{ {
_vis.attr("edge_not_relaxed") _vis.attr("edge_not_relaxed")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void black_target(Edge e, Graph& g) void black_target(Edge e, const Graph&)
{ {
_vis.attr("black_target") _vis.attr("black_target")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void finish_vertex(Vertex u, Graph& g) void finish_vertex(Vertex u, const Graph&)
{ {
_vis.attr("finish_vertex")(PythonVertex(_gi, u)); _vis.attr("finish_vertex")(PythonVertex(_gi, u));
} }
......
...@@ -48,7 +48,7 @@ struct do_astar_search ...@@ -48,7 +48,7 @@ struct do_astar_search
checked_vector_property_map<default_color_type, checked_vector_property_map<default_color_type,
typeof(get(vertex_index, g))> typeof(get(vertex_index, g))>
color(get(vertex_index, g._g)); color(get(vertex_index, g));
typedef typename property_map_type:: typedef typename property_map_type::
apply<int32_t, typeof(get(vertex_index, g))>::type pred_t; apply<int32_t, typeof(get(vertex_index, g))>::type pred_t;
typedef typename graph_traits<Graph>::edge_descriptor edge_t; typedef typename graph_traits<Graph>::edge_descriptor edge_t;
......
...@@ -37,38 +37,38 @@ public: ...@@ -37,38 +37,38 @@ public:
: _gi(gi), _vis(vis) {} : _gi(gi), _vis(vis) {}
template <class Edge, class Graph> template <class Edge, class Graph>
void examine_edge(Edge e, Graph& g) void examine_edge(Edge e, const Graph& g)
{ {
_vis.attr("examine_edge") _vis.attr("examine_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_relaxed(Edge e, Graph& g) void edge_relaxed(Edge e, const Graph& g)
{ {
_vis.attr("edge_relaxed") _vis.attr("edge_relaxed")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_not_relaxed(Edge e, Graph& g) void edge_not_relaxed(Edge e, const Graph& g)
{ {
_vis.attr("edge_not_relaxed") _vis.attr("edge_not_relaxed")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_minimized(Edge e, Graph& g) void edge_minimized(Edge e, const Graph& g)
{ {
_vis.attr("edge_minimized") _vis.attr("edge_minimized")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_not_minimized(Edge e, Graph& g) void edge_not_minimized(Edge e, const Graph& g)
{ {
_vis.attr("edge_not_minimized") _vis.attr("edge_not_minimized")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
private: private:
......
...@@ -37,60 +37,60 @@ public: ...@@ -37,60 +37,60 @@ public:
: _gi(gi), _vis(vis) {} : _gi(gi), _vis(vis) {}
template <class Vertex, class Graph> template <class Vertex, class Graph>
void initialize_vertex(Vertex u, Graph& g) void initialize_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("initialize_vertex")(PythonVertex(_gi, u)); _vis.attr("initialize_vertex")(PythonVertex(_gi, u));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void discover_vertex(Vertex u, Graph& g) void discover_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("discover_vertex")(PythonVertex(_gi, u)); _vis.attr("discover_vertex")(PythonVertex(_gi, u));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void examine_vertex(Vertex u, Graph& g) void examine_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("examine_vertex")(PythonVertex(_gi, u)); _vis.attr("examine_vertex")(PythonVertex(_gi, u));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void examine_edge(Edge e, Graph& g) void examine_edge(Edge e, const Graph& g)
{ {
_vis.attr("examine_edge") _vis.attr("examine_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void tree_edge(Edge e, Graph& g) void tree_edge(Edge e, const Graph& g)
{ {
_vis.attr("tree_edge") _vis.attr("tree_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void non_tree_edge(Edge e, Graph& g) void non_tree_edge(Edge e, const Graph& g)
{ {
_vis.attr("non_tree_edge") _vis.attr("non_tree_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void gray_target(Edge e, Graph& g) void gray_target(Edge e, const Graph& g)
{ {
_vis.attr("gray_target") _vis.attr("gray_target")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void black_target(Edge e, Graph& g) void black_target(Edge e, const Graph& g)
{ {
_vis.attr("black_target") _vis.attr("black_target")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void finish_vertex(Vertex u, Graph& g) void finish_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("finish_vertex")(PythonVertex(_gi, u)); _vis.attr("finish_vertex")(PythonVertex(_gi, u));
} }
......
...@@ -48,28 +48,28 @@ public: ...@@ -48,28 +48,28 @@ public:
void examine_edge(Edge e, const Graph& g) void examine_edge(Edge e, const Graph& g)
{ {
_vis.attr("examine_edge") _vis.attr("examine_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void tree_edge(Edge e, const Graph& g) void tree_edge(Edge e, const Graph& g)
{ {
_vis.attr("tree_edge") _vis.attr("tree_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void back_edge(Edge e, const Graph& g) void back_edge(Edge e, const Graph& g)
{ {
_vis.attr("back_edge") _vis.attr("back_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void forward_or_cross_edge(Edge e, const Graph& g) void forward_or_cross_edge(Edge e, const Graph& g)
{ {
_vis.attr("forward_or_cross_edge") _vis.attr("forward_or_cross_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
......
...@@ -37,46 +37,46 @@ public: ...@@ -37,46 +37,46 @@ public:
: _gi(gi), _vis(vis) {} : _gi(gi), _vis(vis) {}
template <class Vertex, class Graph> template <class Vertex, class Graph>
void initialize_vertex(Vertex u, Graph& g) void initialize_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("initialize_vertex")(PythonVertex(_gi, u)); _vis.attr("initialize_vertex")(PythonVertex(_gi, u));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void discover_vertex(Vertex u, Graph& g) void discover_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("discover_vertex")(PythonVertex(_gi, u)); _vis.attr("discover_vertex")(PythonVertex(_gi, u));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void examine_vertex(Vertex u, Graph& g) void examine_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("examine_vertex")(PythonVertex(_gi, u)); _vis.attr("examine_vertex")(PythonVertex(_gi, u));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void examine_edge(Edge e, Graph& g) void examine_edge(Edge e, const Graph& g)
{ {
_vis.attr("examine_edge") _vis.attr("examine_edge")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_relaxed(Edge e, Graph& g) void edge_relaxed(Edge e, const Graph& g)
{ {
_vis.attr("edge_relaxed") _vis.attr("edge_relaxed")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Edge, class Graph> template <class Edge, class Graph>
void edge_not_relaxed(Edge e, Graph& g) void edge_not_relaxed(Edge e, const Graph& g)
{ {
_vis.attr("edge_not_relaxed") _vis.attr("edge_not_relaxed")
(PythonEdge<typename Graph::orig_graph_t>(_gi, e)); (PythonEdge<Graph>(_gi, e));
} }
template <class Vertex, class Graph> template <class Vertex, class Graph>
void finish_vertex(Vertex u, Graph& g) void finish_vertex(Vertex u, const Graph& g)
{ {
_vis.attr("finish_vertex")(PythonVertex(_gi, u)); _vis.attr("finish_vertex")(PythonVertex(_gi, u));
} }
......
...@@ -94,10 +94,11 @@ struct get_sampled_distance_histogram ...@@ -94,10 +94,11 @@ struct get_sampled_distance_histogram
vertex_t v; vertex_t v;
tr1::uniform_int<size_t> randint(0, sources.size()-1); tr1::uniform_int<size_t> randint(0, sources.size()-1);
{ {
size_t j;
#pragma omp critical #pragma omp critical
size_t i = randint(rng); j = randint(rng);
v = sources[i]; v = sources[j];
swap(sources[i], sources.back()); swap(sources[j], sources.back());
sources.pop_back(); sources.pop_back();
} }
......
...@@ -25,17 +25,25 @@ using namespace std; ...@@ -25,17 +25,25 @@ using namespace std;
using namespace boost; using namespace boost;
using namespace graph_tool; using namespace graph_tool;
struct deg_check : public boost::static_visitor<>
{
void operator()(boost::any a) const
{
if (!belongs<vertex_scalar_properties>()(a))
throw ValueException("Vertex property must be of scalar type.");
}
template<class T>
void operator()(const T&) const {}
};
// this will return the vertex histogram of degrees or scalar properties // this will return the vertex histogram of degrees or scalar properties
python::object python::object
get_vertex_histogram(GraphInterface& gi, GraphInterface::deg_t deg, get_vertex_histogram(GraphInterface& gi, GraphInterface::deg_t deg,
const vector<long double>& bins) const vector<long double>& bins)
{ {
try boost::apply_visitor(deg_check(), deg);
{
if (!belongs<vertex_scalar_properties>()(get<boost::any>(deg)))
throw ValueException("Vertex property must be of scalar type.");
}
catch (boost::bad_get&){}
python::object hist; python::object hist;
python::object ret_bins; python::object ret_bins;
......
...@@ -30,21 +30,15 @@ using namespace graph_tool; ...@@ -30,21 +30,15 @@ using namespace graph_tool;
void do_label_parallel_edges(GraphInterface& gi, boost::any property, void do_label_parallel_edges(GraphInterface& gi, boost::any property,
bool mark_only, bool count_all) bool mark_only, bool count_all)
{ {
GraphInterface::edge_index_map_t edge_index = run_action<>()(gi, bind<void>(label_parallel_edges(), _1, _2, mark_only, count_all),
any_cast<GraphInterface::edge_index_map_t>(gi.GetEdgeIndex()); writable_edge_scalar_properties())(property);
run_action<>()(gi, bind<void>(label_parallel_edges(), _1,
edge_index, _2, mark_only, count_all),
edge_scalar_properties())(property);
} }
void do_label_self_loops(GraphInterface& gi, boost::any property, void do_label_self_loops(GraphInterface& gi, boost::any property,
bool mark_only) bool mark_only)
{ {
GraphInterface::edge_index_map_t edge_index = run_action<>()(gi, bind<void>(label_self_loops(), _1, _2, mark_only),
any_cast<GraphInterface::edge_index_map_t>(gi.GetEdgeIndex()); writable_edge_scalar_properties())(property);
run_action<>()(gi, bind<void>(label_self_loops(), _1,
edge_index, _2, mark_only),
edge_scalar_properties())(property);
} }
void do_remove_labeled_edges(GraphInterface& gi, boost::any property) void do_remove_labeled_edges(GraphInterface& gi, boost::any property)
......
...@@ -29,9 +29,9 @@ using namespace boost; ...@@ -29,9 +29,9 @@ using namespace boost;
// label parallel edges in the order they are found, starting from 1 // label parallel edges in the order they are found, starting from 1
struct label_parallel_edges struct label_parallel_edges
{ {
template <class Graph, class EdgeIndexMap, class ParallelMap> template <class Graph, class ParallelMap>
void operator()(const Graph& g, EdgeIndexMap edge_index, void operator()(const Graph& g, ParallelMap parallel, bool mark_only,
ParallelMap parallel, bool mark_only, bool count_all) const bool count_all) const
{ {
typedef typename graph_traits<Graph>::edge_descriptor edge_t; typedef typename graph_traits<Graph>::edge_descriptor edge_t;
...@@ -68,9 +68,8 @@ struct label_parallel_edges ...@@ -68,9 +68,8 @@ struct label_parallel_edges
// label self loops edges in the order they are found, starting from 1 // label self loops edges in the order they are found, starting from 1
struct label_self_loops struct label_self_loops
{ {
template <class Graph, class EdgeIndexMap, class SelfMap> template <class Graph, class SelfMap>
void operator()(const Graph& g, EdgeIndexMap edge_index, void operator()(const Graph& g, SelfMap self, bool mark_only) const
SelfMap self, bool mark_only) const
{ {
typedef typename graph_traits<Graph>::edge_descriptor edge_t; typedef typename graph_traits<Graph>::edge_descriptor edge_t;
......