Copy methods are now able to keep mappings from copy to origin, and from origin to copy.

This commit is contained in:
Guillaume Damiand 2018-12-20 08:51:04 +01:00
parent a3b2c55f88
commit a211e558ee
3 changed files with 79 additions and 41 deletions

View File

@ -208,7 +208,7 @@ namespace CGAL {
CGAL_assertion(number_of_darts()==0); CGAL_assertion(number_of_darts()==0);
} }
/** Copy the given combinatorial map into *this. /** Copy the given combinatorial map 'amap' into *this.
* Note that both CMap can have different dimensions and/or non void attributes. * Note that both CMap can have different dimensions and/or non void attributes.
* @param amap the combinatorial map to copy. * @param amap the combinatorial map to copy.
* @post *this is valid. * @post *this is valid.
@ -223,7 +223,10 @@ namespace CGAL {
const PointConverter& pointconverter, const PointConverter& pointconverter,
boost::unordered_map boost::unordered_map
<typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2, Storage2>:: <typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2, Storage2>::
Dart_const_handle, Dart_handle>* dart_mapping=NULL) Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Combinatorial_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
typedef Combinatorial_map_base<d2, Refs2, Items2, Alloc2, Storage2> CMap2; typedef Combinatorial_map_base<d2, Refs2, Items2, Alloc2, Storage2> CMap2;
this->clear(); this->clear();
@ -248,26 +251,31 @@ namespace CGAL {
// (here we cannot use CGAL::Unique_hash_map because it does not provide // (here we cannot use CGAL::Unique_hash_map because it does not provide
// iterators... // iterators...
boost::unordered_map<typename CMap2::Dart_const_handle, Dart_handle> boost::unordered_map<typename CMap2::Dart_const_handle, Dart_handle>
local_dartmap; local_dartmap;
if (dart_mapping==NULL) if (origin_to_copy==NULL) // Used local_dartmap if user does not provides its own unordered_map
{ dart_mapping=&local_dartmap; } { origin_to_copy=&local_dartmap; }
Dart_handle new_dart;
for (typename CMap2::Dart_const_range::const_iterator for (typename CMap2::Dart_const_range::const_iterator
it=amap.darts().begin(), itend=amap.darts().end(); it=amap.darts().begin(), itend=amap.darts().end();
it!=itend; ++it) it!=itend; ++it)
{ {
(*dart_mapping)[it]=mdarts.emplace(); new_dart=mdarts.emplace();
init_dart((*dart_mapping)[it], amap.get_marks(it)); init_dart(new_dart, amap.get_marks(it));
(*origin_to_copy)[it]=new_dart;
if (copy_to_origin!=NULL) { (*copy_to_origin)[new_dart]=it; }
internal::Copy_dart_info_functor<Refs2, Refs, DartInfoConverter>::run internal::Copy_dart_info_functor<Refs2, Refs, DartInfoConverter>::run
(static_cast<const Refs2&>(amap), static_cast<Refs&>(*this), (static_cast<const Refs2&>(amap), static_cast<Refs&>(*this),
it, (*dart_mapping)[it], dartinfoconverter); it, new_dart, dartinfoconverter);
} }
unsigned int min_dim=(dimension<amap.dimension?dimension:amap.dimension); unsigned int min_dim=(dimension<amap.dimension?dimension:amap.dimension);
typename boost::unordered_map<typename CMap2::Dart_const_handle,Dart_handle> typename boost::unordered_map<typename CMap2::Dart_const_handle,Dart_handle>
::iterator dartmap_iter, dartmap_iter_end=dart_mapping->end(); ::iterator dartmap_iter, dartmap_iter_end=origin_to_copy->end();
for (dartmap_iter=dart_mapping->begin(); dartmap_iter!=dartmap_iter_end; for (dartmap_iter=origin_to_copy->begin(); dartmap_iter!=dartmap_iter_end;
++dartmap_iter) ++dartmap_iter)
{ {
for (unsigned int i=0; i<=min_dim; i++) for (unsigned int i=0; i<=min_dim; i++)
@ -276,13 +284,13 @@ namespace CGAL {
(dartmap_iter->first)<(amap.beta(dartmap_iter->first,i))) (dartmap_iter->first)<(amap.beta(dartmap_iter->first,i)))
{ {
basic_link_beta(dartmap_iter->second, basic_link_beta(dartmap_iter->second,
(*dart_mapping)[amap.beta(dartmap_iter->first,i)], i); (*origin_to_copy)[amap.beta(dartmap_iter->first,i)], i);
} }
} }
} }
/** Copy attributes */ /** Copy attributes */
for (dartmap_iter=dart_mapping->begin(); dartmap_iter!=dartmap_iter_end; for (dartmap_iter=origin_to_copy->begin(); dartmap_iter!=dartmap_iter_end;
++dartmap_iter) ++dartmap_iter)
{ {
Helper::template Foreach_enabled_attributes Helper::template Foreach_enabled_attributes
@ -301,13 +309,17 @@ namespace CGAL {
void copy(const Combinatorial_map_base<d2, Refs2, Items2, Alloc2, Storage2>& amap, void copy(const Combinatorial_map_base<d2, Refs2, Items2, Alloc2, Storage2>& amap,
boost::unordered_map boost::unordered_map
<typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2, <typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2,
Storage2>::Dart_const_handle, Dart_handle>* dart_mapping=NULL) Storage2>::Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Combinatorial_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
CGAL::cpp11::tuple<> converters; CGAL::cpp11::tuple<> converters;
Default_converter_dart_info<Refs2, Refs> dartinfoconverter; Default_converter_dart_info<Refs2, Refs> dartinfoconverter;
Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter; Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter;
copy(amap, converters, dartinfoconverter, pointconverter, dart_mapping); copy(amap, converters, dartinfoconverter, pointconverter,
origin_to_copy, copy_to_origin);
} }
template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2, template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2,
@ -317,11 +329,15 @@ namespace CGAL {
const Converters& converters, const Converters& converters,
boost::unordered_map boost::unordered_map
<typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2, <typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2,
Storage2>::Dart_const_handle, Dart_handle>* dart_mapping=NULL) Storage2>::Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Combinatorial_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter; Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter;
Default_converter_dart_info<Refs2, Refs> dartinfoconverter; Default_converter_dart_info<Refs2, Refs> dartinfoconverter;
copy(amap, converters, dartinfoconverter, pointconverter, dart_mapping); copy(amap, converters, dartinfoconverter, pointconverter,
origin_to_copy, copy_to_origin);
} }
template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2, template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2,
@ -333,10 +349,14 @@ namespace CGAL {
const DartInfoConverter& dartinfoconverter, const DartInfoConverter& dartinfoconverter,
boost::unordered_map boost::unordered_map
<typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2, <typename Combinatorial_map_base<d2, Refs2, Items2, Alloc2,
Storage2>::Dart_const_handle, Dart_handle>* dart_mapping=NULL) Storage2>::Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Combinatorial_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter; Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter;
copy(amap, converters, dartinfoconverter, pointconverter, dart_mapping); copy(amap, converters, dartinfoconverter, pointconverter,
origin_to_copy, copy_to_origin);
} }
// Copy constructor from a map having exactly the same type. // Copy constructor from a map having exactly the same type.

View File

@ -200,7 +200,10 @@ namespace CGAL {
const PointConverter& pointconverter, const PointConverter& pointconverter,
boost::unordered_map boost::unordered_map
<typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>:: <typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>::
Dart_const_handle, Dart_handle>* dart_mapping=NULL) Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Generalized_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
typedef Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2> GMap2; typedef Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2> GMap2;
this->clear(); this->clear();
@ -224,25 +227,30 @@ namespace CGAL {
// iterators... // iterators...
boost::unordered_map<typename GMap2::Dart_const_handle, Dart_handle> boost::unordered_map<typename GMap2::Dart_const_handle, Dart_handle>
local_dartmap; local_dartmap;
if (dart_mapping==NULL) if (origin_to_copy==NULL) // Used local_dartmap if user does not provides its own unordered_map
{ dart_mapping=&local_dartmap; } { origin_to_copy=&local_dartmap; }
Dart_handle new_dart;
for (typename GMap2::Dart_const_range::const_iterator for (typename GMap2::Dart_const_range::const_iterator
it=amap.darts().begin(), itend=amap.darts().end(); it=amap.darts().begin(), itend=amap.darts().end();
it!=itend; ++it) it!=itend; ++it)
{ {
(*dart_mapping)[it]=mdarts.emplace(); new_dart=mdarts.emplace();
init_dart((*dart_mapping)[it], amap.get_marks(it)); init_dart(new_dart, amap.get_marks(it));
(*origin_to_copy)[it]=new_dart;
if (copy_to_origin!=NULL) { (*copy_to_origin)[new_dart]=it; }
internal::Copy_dart_info_functor<Refs2, Refs, DartInfoConverter>:: internal::Copy_dart_info_functor<Refs2, Refs, DartInfoConverter>::
run(static_cast<const Refs2&>(amap), static_cast<Refs&>(*this), run(static_cast<const Refs2&>(amap), static_cast<Refs&>(*this),
it, (*dart_mapping)[it], dartinfoconverter); it, new_dart, dartinfoconverter);
} }
unsigned int min_dim=(dimension<amap.dimension?dimension:amap.dimension); unsigned int min_dim=(dimension<amap.dimension?dimension:amap.dimension);
typename boost::unordered_map<typename GMap2::Dart_const_handle,Dart_handle> typename boost::unordered_map<typename GMap2::Dart_const_handle,Dart_handle>
::iterator dartmap_iter, dartmap_iter_end=dart_mapping->end(); ::iterator dartmap_iter, dartmap_iter_end=origin_to_copy->end();
for (dartmap_iter=dart_mapping->begin(); dartmap_iter!=dartmap_iter_end; for (dartmap_iter=origin_to_copy->begin(); dartmap_iter!=dartmap_iter_end;
++dartmap_iter) ++dartmap_iter)
{ {
for (unsigned int i=0; i<=min_dim; i++) for (unsigned int i=0; i<=min_dim; i++)
@ -251,13 +259,13 @@ namespace CGAL {
(dartmap_iter->first)<(amap.alpha(dartmap_iter->first,i))) (dartmap_iter->first)<(amap.alpha(dartmap_iter->first,i)))
{ {
basic_link_alpha(dartmap_iter->second, basic_link_alpha(dartmap_iter->second,
(*dart_mapping)[amap.alpha(dartmap_iter->first,i)], i); (*origin_to_copy)[amap.alpha(dartmap_iter->first,i)], i);
} }
} }
} }
/** Copy attributes */ /** Copy attributes */
for (dartmap_iter=dart_mapping->begin(); dartmap_iter!=dartmap_iter_end; for (dartmap_iter=origin_to_copy->begin(); dartmap_iter!=dartmap_iter_end;
++dartmap_iter) ++dartmap_iter)
{ {
Helper::template Foreach_enabled_attributes Helper::template Foreach_enabled_attributes
@ -268,7 +276,7 @@ namespace CGAL {
converters, pointconverter); converters, pointconverter);
} }
CGAL_assertion (is_valid () == 1); CGAL_assertion(is_valid());
} }
template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2, template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2,
@ -276,12 +284,16 @@ namespace CGAL {
void copy(const Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>& amap, void copy(const Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>& amap,
boost::unordered_map boost::unordered_map
<typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>:: <typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>::
Dart_const_handle, Dart_handle>* dart_mapping=NULL) Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Generalized_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
CGAL::cpp11::tuple<> converters; CGAL::cpp11::tuple<> converters;
Default_converter_dart_info<Refs2, Refs> dartinfoconverter; Default_converter_dart_info<Refs2, Refs> dartinfoconverter;
Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter; Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter;
copy(amap, converters, dartinfoconverter, pointconverter, dart_mapping); copy(amap, converters, dartinfoconverter, pointconverter,
origin_to_copy, copy_to_origin);
} }
template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2, template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2,
@ -290,11 +302,15 @@ namespace CGAL {
const Converters& converters, const Converters& converters,
boost::unordered_map boost::unordered_map
<typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>:: <typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>::
Dart_const_handle, Dart_handle>* dart_mapping=NULL) Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Generalized_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter; Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter;
Default_converter_dart_info<Refs2, Refs> dartinfoconverter; Default_converter_dart_info<Refs2, Refs> dartinfoconverter;
copy(amap, converters, dartinfoconverter, pointconverter, dart_mapping); copy(amap, converters, dartinfoconverter, pointconverter,
origin_to_copy, copy_to_origin);
} }
template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2, template <unsigned int d2, typename Refs2, typename Items2, typename Alloc2,
@ -305,10 +321,14 @@ namespace CGAL {
const DartInfoConverter& dartinfoconverter, const DartInfoConverter& dartinfoconverter,
boost::unordered_map boost::unordered_map
<typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>:: <typename Generalized_map_base<d2, Refs2, Items2, Alloc2, Storage2>::
Dart_const_handle, Dart_handle>* dart_mapping=NULL) Dart_const_handle, Dart_handle>* origin_to_copy=NULL,
boost::unordered_map
<Dart_handle, typename Generalized_map_base<d2, Refs2, Items2,
Alloc2, Storage2>::Dart_const_handle>* copy_to_origin=NULL)
{ {
Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter; Default_converter_cmap_0attributes_with_point<Refs2, Refs> pointconverter;
copy(amap, converters, dartinfoconverter, pointconverter, dart_mapping); copy(amap, converters, dartinfoconverter, pointconverter,
origin_to_copy, copy_to_origin);
} }
// Copy constructor from a map having exactly the same type. // Copy constructor from a map having exactly the same type.

View File

@ -100,14 +100,12 @@ namespace CGAL {
boost::unordered_map<typename Map::Dart_const_handle, Dart_handle> boost::unordered_map<typename Map::Dart_const_handle, Dart_handle>
origin_to_copy; origin_to_copy;
// We copy the original map, while keeping a mapping between darts.
m_map.copy(m_original_map, &origin_to_copy);
// The mapping between darts of the copy into darts of the original map. // The mapping between darts of the copy into darts of the original map.
boost::unordered_map<Dart_handle, typename Map::Dart_const_handle> boost::unordered_map<Dart_handle, typename Map::Dart_const_handle>
copy_to_origin; copy_to_origin;
for (auto it=origin_to_copy.begin(); it!=origin_to_copy.end(); ++it)
{ copy_to_origin[it->second]=it->first; } // We copy the original map, while keeping mappings between darts.
m_map.copy(m_original_map, &origin_to_copy, &copy_to_origin);
if (display_time) if (display_time)
{ {