Commit e0153068 authored by Tiago Peixoto's avatar Tiago Peixoto

Implement ProperyMap.swap()

parent dd46ba20
......@@ -112,6 +112,11 @@ public:
std::vector<T>& get_storage() const { return (*store); }
void swap(checked_vector_property_map& other)
{
store->swap(*other.store);
}
unchecked_t get_unchecked(size_t size = 0) const
{
reserve(size);
......@@ -183,23 +188,26 @@ public:
void resize(size_t size) const { _checked.resize(size); }
void shrink_to_fit() const { _checked.shrink_to_fit(); }
__attribute__((always_inline)) __attribute__((flatten))
reference operator[](const key_type& v) const
{
auto i = get(_checked.index, v);
return (*_checked.store)[i];
return (*_checked.store)[get(_checked.index, v)];
}
std::vector<T>& get_storage() const { return _checked.get_storage(); }
void swap(unchecked_vector_property_map& other)
{
get_storage().swap(other.get_storage());
}
checked_t get_checked() {return _checked;}
// deep copy
unchecked_vector_property_map copy() const
{
unchecked_vector_property_map pmap(_checked.index,
_checked.store->size());
*(pmap._checked.store) = *(_checked.store);
return pmap;
return _checked.copy().get_unchecked();
}
private:
......
......@@ -603,6 +603,23 @@ public:
{
}
void swap(PythonPropertyMap& other)
{
swap_dispatch(other,
std::is_convertible<typename boost::property_traits<PropertyMap>::category,
boost::writable_property_map_tag>());
}
void swap_dispatch(PythonPropertyMap& other, std::true_type)
{
_pmap.swap(other._pmap);
}
void swap_dispatch(PythonPropertyMap& other, std::false_type)
{
throw ValueException("Read-only property map cannot be swapped.");
}
size_t data_ptr()
{
typename boost::mpl::or_<
......
......@@ -66,6 +66,7 @@ struct export_vertex_property_map
.def("reserve", &pmap_t::reserve)
.def("resize", &pmap_t::resize)
.def("shrink_to_fit", &pmap_t::shrink_to_fit)
.def("swap", &pmap_t::swap)
.def("data_ptr", &pmap_t::data_ptr);
typedef boost::mpl::transform<graph_tool::all_graph_views,
......@@ -149,6 +150,7 @@ struct export_edge_property_map
.def("reserve", &pmap_t::reserve)
.def("resize", &pmap_t::resize)
.def("shrink_to_fit", &pmap_t::shrink_to_fit)
.def("swap", &pmap_t::swap)
.def("data_ptr", &pmap_t::data_ptr);
......
......@@ -830,6 +830,14 @@ class PropertyMap(object):
self.__map.resize(size)
self.__map.shrink_to_fit()
def swap(self, other):
"""Swap internal storage with ``other``."""
if self.key_type() != other.key_type():
raise ValueError("property maps must have the same key type")
if self.value_type() != other.value_type():
raise ValueError("property maps must have the same value type")
self.__map.swap(other.__map)
def data_ptr(self):
"""Return the pointer to memory where the data resides."""
return self.__map.data_ptr()
......
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