Commit b7b68451 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Implement PropertyMap.shrink_to_fit()

parent 6c264a5c
......@@ -96,10 +96,20 @@ public:
void reserve(size_t size) const
{
if (store->size() < size)
if (size > store->size())
store->resize(size);
}
void resize(size_t size) const
{
store->resize(size);
}
void shrink_to_fit() const
{
store->shrink_to_fit();
}
std::vector<T>& get_storage() const { return (*store); }
unchecked_t get_unchecked(size_t size = 0) const
......@@ -170,6 +180,8 @@ public:
}
void reserve(size_t size) const { _checked.reserve(size); }
void resize(size_t size) const { _checked.resize(size); }
void shrink_to_fit() const { _checked.shrink_to_fit(); }
reference operator[](const key_type& v) const
{
......
......@@ -511,6 +511,63 @@ public:
boost::writable_property_map_tag>::value;
}
void reserve(size_t size)
{
typename boost::mpl::or_<
std::is_same<PropertyMap,
GraphInterface::vertex_index_map_t>,
std::is_same<PropertyMap,
GraphInterface::edge_index_map_t> >::type is_index;
reserve_dispatch(size, is_index);
}
void reserve_dispatch(size_t size, boost::mpl::bool_<false>)
{
_pmap.reserve(size);
}
void reserve_dispatch(size_t, boost::mpl::bool_<true>)
{
}
void resize(size_t size)
{
typename boost::mpl::or_<
std::is_same<PropertyMap,
GraphInterface::vertex_index_map_t>,
std::is_same<PropertyMap,
GraphInterface::edge_index_map_t> >::type is_index;
resize_dispatch(size, is_index);
}
void resize_dispatch(size_t size, boost::mpl::bool_<false>)
{
_pmap.resize(size);
}
void resize_dispatch(size_t, boost::mpl::bool_<true>)
{
}
void shrink_to_fit()
{
typename boost::mpl::or_<
std::is_same<PropertyMap,
GraphInterface::vertex_index_map_t>,
std::is_same<PropertyMap,
GraphInterface::edge_index_map_t> >::type is_index;
shrink_to_fit_dispatch(is_index);
}
void shrink_to_fit_dispatch(boost::mpl::bool_<false>)
{
_pmap.shrink_to_fit();
}
void shrink_to_fit_dispatch(boost::mpl::bool_<true>)
{
}
private:
PropertyMap _pmap; // hold an internal copy, since it's cheap
};
......
......@@ -64,7 +64,10 @@ struct export_vertex_property_map
.def("get_map", &pmap_t::get_map)
.def("get_dynamic_map", &pmap_t::get_dynamic_map)
.def("get_array", &pmap_t::get_array)
.def("is_writable", &pmap_t::is_writable);
.def("is_writable", &pmap_t::is_writable)
.def("reserve", &pmap_t::reserve)
.def("resize", &pmap_t::resize)
.def("shrink_to_fit", &pmap_t::shrink_to_fit);
typedef boost::mpl::transform<graph_tool::detail::all_graph_views,
boost::mpl::quote1<std::add_const> >::type const_graph_views;
......@@ -145,7 +148,10 @@ struct export_edge_property_map
.def("get_map", &pmap_t::get_map)
.def("get_dynamic_map", &pmap_t::get_dynamic_map)
.def("get_array", &pmap_t::get_array)
.def("is_writable", &pmap_t::is_writable);
.def("is_writable", &pmap_t::is_writable)
.def("reserve", &pmap_t::reserve)
.def("resize", &pmap_t::resize)
.def("shrink_to_fit", &pmap_t::shrink_to_fit);
typedef boost::mpl::transform<graph_tool::detail::all_graph_views,
......@@ -190,7 +196,10 @@ struct export_graph_property_map
.def("get_map", &pmap_t::get_map)
.def("get_dynamic_map", &pmap_t::get_dynamic_map)
.def("get_array", &pmap_t::get_array)
.def("is_writable", &pmap_t::is_writable);
.def("is_writable", &pmap_t::is_writable)
.def("reserve", &pmap_t::reserve)
.def("resize", &pmap_t::resize)
.def("shrink_to_fit", &pmap_t::shrink_to_fit);
}
};
......
......@@ -831,6 +831,25 @@ class PropertyMap(object):
else:
self[g] = val
def reserve(self, size):
"""Reserve enough space for ``size`` elements in underlying container. If the
original size is larger, it will be reduced, and the remaining memory
will be freed."""
self.__map.reserve(size)
def shrink_to_fit(self):
"""Shrink size of underlying container to accommodate only the necessary amount,
and thus potentially freeing memory."""
g = self.get_graph()
if self.key_type() == "v":
size = g.num_vertices(True)
elif self.key_type() == "e":
size = g.max_edge_index
else:
size = 1
self.__map.resize(size)
self.__map.shrink_to_fit()
def __call__(self, a):
p = self.copy()
p.fa = a
......@@ -2301,8 +2320,7 @@ class Graph(object):
if "_Graph__reversed" in self.graph_properties:
self.set_reversed(True)
del self.graph_properties["_Graph__reversed"]
self.shrink_to_fit()
def save(self, file_name, fmt="auto"):
"""Save graph to ``file_name`` (which can be either a string or a file-like
......
Markdown is supported
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