Commit 6c264a5c authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add new function: Graph.shrink_to_fit()

parent 70f17f21
......@@ -58,6 +58,11 @@
.. automethod:: clear_vertex
.. automethod:: clear_edges
After the removal of many edges and/or vertices, the underlying
containers may have a capacity that significantly exceeds the size
of the graph. The function below corrects this.
.. automethod:: shrink_to_fit
.. container:: sec_title
......
......@@ -107,6 +107,7 @@ public:
boost::any prop_tgt);
void copy_edge_property(const GraphInterface& src, boost::any prop_src,
boost::any prop_tgt);
void shrink_to_fit() { _mg->shrink_to_fit(); }
//
// python interface
......@@ -130,16 +131,6 @@ public:
// Internal types
//
// // the following defines the edges' internal properties
// typedef property<edge_index_t, size_t> EdgeProperty;
// // this is the main graph type
// typedef adjacency_list <vecS, // edges
// vecS, // vertices
// bidirectionalS,
// no_property,
// EdgeProperty,
// vecS> multigraph_t;
typedef boost::adj_list<size_t> multigraph_t;
typedef boost::graph_traits<multigraph_t>::vertex_descriptor vertex_t;
typedef boost::graph_traits<multigraph_t>::edge_descriptor edge_t;
......
......@@ -305,6 +305,33 @@ public:
static Vertex null_vertex() { return std::numeric_limits<Vertex>::max(); }
void shrink_to_fit()
{
_in_edges.shrink_to_fit();
_out_edges.shrink_to_fit();
std::for_each(_in_edges.begin(), _in_edges.end(),
[](auto &es){es.shrink_to_fit();});
std::for_each(_out_edges.begin(), _out_edges.end(),
[](auto &es){es.shrink_to_fit();});
auto erange = boost::edges(*this);
auto iter = std::max_element(erange.first, erange.second,
[](const auto &a, const auto& b) -> bool
{return a.idx < b.idx;});
if (iter == erange.second)
_last_idx = 0;
else
_last_idx = iter->idx;
auto iter_idx = std::remove_if(_free_indexes.begin(),
_free_indexes.end(),
[&](auto idx) -> bool
{return idx > _last_idx;});
_free_indexes.erase(iter_idx, _free_indexes.end());
_free_indexes.shrink_to_fit();
if (_keep_epos)
_epos.resize(_last_idx + 1);
_epos.shrink_to_fit();
}
private:
typedef std::vector<edge_list_t> vertex_list_t;
vertex_list_t _out_edges;
......
......@@ -472,6 +472,7 @@ BOOST_PYTHON_MODULE(libgraph_tool_core)
.def("get_edge_index", &GraphInterface::get_edge_index)
.def("get_max_edge_index", &GraphInterface::get_max_edge_index)
.def("re_index_edges", &GraphInterface::re_index_edges)
.def("shrink_to_fit", &GraphInterface::shrink_to_fit)
.def("get_graph_index", &GraphInterface::get_graph_index)
.def("copy_vertex_property", &GraphInterface::copy_vertex_property)
.def("copy_edge_property", &GraphInterface::copy_edge_property);
......
......@@ -2066,9 +2066,16 @@ class Graph(object):
"""
self.__graph.re_index_edges()
def shrink_to_fit(self):
"""Force the physical capacity of the underlying containers to match the graph's
actual size, potentially freeing memory back to the system."""
self.__graph.shrink_to_fit()
# Property map creation
def new_property(self, key_type, value_type, vals=None):
"""Create a new (uninitialized) vertex property map of key type
``key_type`` (``v``, ``e`` or ``g``), value type ``value_type``, and
return it. If provided, the values will be initialized by ``vals``,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment