From 56f2be8cff32a698330b351b8f4c291141f015e3 Mon Sep 17 00:00:00 2001 From: Sylvain Thery Date: Tue, 24 Jun 2014 17:06:27 +0200 Subject: [PATCH] optimization of map compacting --- include/Container/attributeContainer.h | 7 +++ include/Container/attributeContainer.hpp | 5 +++ include/Topology/generic/genericmap.h | 11 ++++- include/Topology/generic/genericmap.hpp | 7 +++ include/Topology/generic/mapImpl/mapMulti.h | 2 +- include/Topology/generic/mapImpl/mapMulti.hpp | 42 ----------------- src/Container/attributeContainer.cpp | 11 +++++ src/Topology/generic/genericmap.cpp | 37 ++++++--------- src/Topology/generic/mapMulti.cpp | 45 +++++++++++++++++++ 9 files changed, 99 insertions(+), 68 deletions(-) diff --git a/include/Container/attributeContainer.h b/include/Container/attributeContainer.h index fa34fb61..53b58fb8 100644 --- a/include/Container/attributeContainer.h +++ b/include/Container/attributeContainer.h @@ -337,6 +337,13 @@ public: */ void compact(std::vector& mapOldNew); + /** + * Test the fragmentation of container, + * in fact just size/max_size + * @return 1 if full filled - 0 is lots of holes + */ + inline float fragmentation(); + /************************************** * LINES MANAGEMENT * **************************************/ diff --git a/include/Container/attributeContainer.hpp b/include/Container/attributeContainer.hpp index 9982f785..e8cd1feb 100644 --- a/include/Container/attributeContainer.hpp +++ b/include/Container/attributeContainer.hpp @@ -227,6 +227,11 @@ inline bool AttributeContainer::used(unsigned int index) const return m_holesBlocks[index / _BLOCKSIZE_]->used(index % _BLOCKSIZE_) != 0; } +inline float AttributeContainer::fragmentation() +{ + return float(m_size) / float(m_maxSize); +} + /************************************** * CONTAINER TRAVERSAL * **************************************/ diff --git a/include/Topology/generic/genericmap.h b/include/Topology/generic/genericmap.h index 68302240..abc54e5a 100644 --- a/include/Topology/generic/genericmap.h +++ b/include/Topology/generic/genericmap.h @@ -430,8 +430,17 @@ protected: public: /** * compact the map + * @warning the quickTraversals needs to be updated */ - void compact() ; + void compact(bool topoOnly = false) ; + + + /** + * test if containers are fragmented + * ~1.0 no need to compact + * ~0.0 need to compact + */ + inline float fragmentation(unsigned int orbit); /** * @brief dump all attributes of map in CSV format (; separated columns) diff --git a/include/Topology/generic/genericmap.hpp b/include/Topology/generic/genericmap.hpp index 21b3ef08..425e3bd9 100644 --- a/include/Topology/generic/genericmap.hpp +++ b/include/Topology/generic/genericmap.hpp @@ -322,4 +322,11 @@ inline AttributeMultiVector* GenericMap::getRelation(const std::string& na return amv ; } +inline float GenericMap::fragmentation(unsigned int orbit) +{ + if (isOrbitEmbedded(orbit)) + return m_attribs[orbit].fragmentation(); + return 1.0f; +} + } //namespace CGoGN diff --git a/include/Topology/generic/mapImpl/mapMulti.h b/include/Topology/generic/mapImpl/mapMulti.h index 4f483133..30f525b1 100644 --- a/include/Topology/generic/mapImpl/mapMulti.h +++ b/include/Topology/generic/mapImpl/mapMulti.h @@ -165,7 +165,7 @@ protected: template inline void permutationUnsew(Dart d); - inline virtual void compactTopo(); + virtual void compactTopo(); /**************************************** * MR CONTAINER MANAGEMENT * diff --git a/include/Topology/generic/mapImpl/mapMulti.hpp b/include/Topology/generic/mapImpl/mapMulti.hpp index dcb08e8d..52d09d66 100644 --- a/include/Topology/generic/mapImpl/mapMulti.hpp +++ b/include/Topology/generic/mapImpl/mapMulti.hpp @@ -297,48 +297,6 @@ inline void MapMulti::permutationUnsew(Dart d) (*m_permutation_inv[I])[e_index] = e ; } -inline void MapMulti::compactTopo() -{ - std::vector oldnewMR; - m_mrattribs.compact(oldnewMR); - - std::vector oldnew; - m_attribs[DART].compact(oldnew); - - unsigned int nbl = m_mrDarts.size(); - for (unsigned int i = m_mrattribs.begin(); i != m_mrattribs.end(); m_mrattribs.next(i)) - { - for (unsigned int level = 0; level < nbl; ++level) - { - unsigned int& d = m_mrDarts[level]->operator[](i); - if (d != oldnew[d]) - d = oldnew[d]; - } - } - - for (unsigned int i = m_attribs[DART].begin(); i != m_attribs[DART].end(); m_attribs[DART].next(i)) - { - for (unsigned int j = 0; j < m_permutation.size(); ++j) - { - Dart d = (*m_permutation[j])[i]; - if (d.index != oldnewMR[d.index]) - (*m_permutation[j])[i] = Dart(oldnewMR[d.index]); - } - for (unsigned int j = 0; j < m_permutation_inv.size(); ++j) - { - Dart d = (*m_permutation_inv[j])[i]; - if (d.index != oldnewMR[d.index]) - (*m_permutation_inv[j])[i] = Dart(oldnewMR[d.index]); - } - for (unsigned int j = 0; j < m_involution.size(); ++j) - { - Dart d = (*m_involution[j])[i]; - if (d.index != oldnewMR[d.index]) - (*m_involution[j])[i] = Dart(oldnewMR[d.index]); - } - } -} - /**************************************** * MR CONTAINER MANAGEMENT * ****************************************/ diff --git a/src/Container/attributeContainer.cpp b/src/Container/attributeContainer.cpp index 33629eec..57b023e8 100644 --- a/src/Container/attributeContainer.cpp +++ b/src/Container/attributeContainer.cpp @@ -216,6 +216,17 @@ void AttributeContainer::compact(std::vector& mapOldNew) mapOldNew.clear(); mapOldNew.resize(realEnd(),0xffffffff); +//VERSION THAT PRESERVE ORDER OF ELEMENTS ? +// unsigned int down = 0; +// for (unsigned int occup = realBegin(); occup != realEnd(); next(occup)) +// { +// mapOldNew[occup] = down; +// copyLine(down,occup); +// // copy ref counter +// setNbRefs(down,getNbRefs(occup)); +// down++; +// } + // fill the holes with data & create the map Old->New unsigned int up = realRBegin(); unsigned int down = 0; diff --git a/src/Topology/generic/genericmap.cpp b/src/Topology/generic/genericmap.cpp index 83c7948e..ed93a24b 100644 --- a/src/Topology/generic/genericmap.cpp +++ b/src/Topology/generic/genericmap.cpp @@ -424,44 +424,33 @@ void GenericMap::dumpAttributesAndMarkers() // } } -void GenericMap::compact() + +void GenericMap::compact(bool topoOnly) { - // compact embedding attribs - std::vector< std::vector* > oldnews(NB_ORBITS,NULL); + compactTopo(); + + if (topoOnly) + return; + + std::vector oldnew; for (unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit) { if ((orbit != DART) && (isOrbitEmbedded(orbit))) { - oldnews[orbit] = new std::vector; - m_attribs[orbit].compact(*(oldnews[orbit])); - } - } - - // update embedding indices of topo - for (unsigned int i = m_attribs[DART].begin(); i != m_attribs[DART].end(); m_attribs[DART].next(i)) - { - for (unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit) - { - if ((orbit != DART) && (isOrbitEmbedded(orbit))) + m_attribs[orbit].compact(oldnew); + for (unsigned int i = m_attribs[DART].begin(); i != m_attribs[DART].end(); m_attribs[DART].next(i)) { unsigned int& idx = m_embeddings[orbit]->operator[](i); - unsigned int jdx = oldnews[orbit]->operator[](idx); - if ((jdx != 0xffffffff) && (jdx != idx)) + unsigned int jdx = oldnew[idx]; + if (jdx != 0xffffffff) idx = jdx; } } } - - // delete allocated vectors - for (unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit) - if (oldnews[orbit] != NULL) - delete oldnews[orbit]; - - // compact topo (depends on map implementation) - compactTopo(); } + void GenericMap::dumpCSV() const { for (unsigned int orbit = 0; orbit < NB_ORBITS; ++orbit) diff --git a/src/Topology/generic/mapMulti.cpp b/src/Topology/generic/mapMulti.cpp index 6d0193d1..d36251eb 100644 --- a/src/Topology/generic/mapMulti.cpp +++ b/src/Topology/generic/mapMulti.cpp @@ -416,4 +416,49 @@ void MapMulti::restore_topo_shortcuts() } } + +// TODO A VERIFIER ET A TESTER +void MapMulti::compactTopo() +{ + std::vector oldnewMR; + + m_mrattribs.compact(oldnewMR); + + unsigned int nbl = m_mrDarts.size(); + for (unsigned int i = m_mrattribs.begin(); i != m_mrattribs.end(); m_mrattribs.next(i)) + { + for (unsigned int level = 0; level < nbl; ++level) + { + unsigned int& d = m_mrDarts[level]->operator[](i); + if (oldnewMR[d] != AttributeContainer::UNKNOWN) + d = oldnewMR[d]; + } + } + + m_attribs[DART].compact(oldnewMR); + + for (unsigned int i = m_attribs[DART].begin(); i != m_attribs[DART].end(); m_attribs[DART].next(i)) + { + for (unsigned int j = 0; j < m_permutation.size(); ++j) + { + Dart d = (*m_permutation[j])[i]; + if (oldnewMR[d.index] != AttributeContainer::UNKNOWN) + (*m_permutation[j])[i] = Dart(oldnewMR[d.index]); + } + for (unsigned int j = 0; j < m_permutation_inv.size(); ++j) + { + Dart d = (*m_permutation_inv[j])[i]; + if (oldnewMR[d.index] != AttributeContainer::UNKNOWN) + (*m_permutation_inv[j])[i] = Dart(oldnewMR[d.index]); + } + for (unsigned int j = 0; j < m_involution.size(); ++j) + { + Dart d = (*m_involution[j])[i]; + if (oldnewMR[d.index] != AttributeContainer::UNKNOWN) + (*m_involution[j])[i] = Dart(oldnewMR[d.index]); + } + } +} + + } //namespace CGoGN -- GitLab