From d436514b16edfd8f8191bf2a2e5f5e3e4d57a375 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <cedric.traizet@c-s.fr>
Date: Mon, 17 Sep 2018 10:35:39 +0200
Subject: [PATCH] ENH : replaced vector by unordered maps

---
 .../app/otbSmallRegionsMerging.cxx            | 20 ++++++++++---------
 .../otbLabelImageSmallRegionMergingFilter.h   | 17 +++++++++-------
 .../otbLabelImageSmallRegionMergingFilter.hxx |  1 -
 3 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
index a9b4e4fc74..d7391db9ee 100644
--- a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
+++ b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
@@ -125,20 +125,22 @@ private:
     labelStatsFilter->SetInputLabelImage(labelIn);
     AddProcess(labelStatsFilter->GetStreamer() , "Computing stats on input image ...");
     labelStatsFilter->Update();
-    
-    // Convert Map to Vector
+
+    // Convert Map to Unordered map
+
     auto labelPopulationMap = labelStatsFilter->GetLabelPopulationMap();
-    std::vector<double> labelPopulation;
-    for (unsigned int i =0; i <= labelPopulationMap.rbegin()->first; i++)
+    std::unordered_map< unsigned int,double> labelPopulation;
+    for (population : labelPopulationMap)
       {
-      labelPopulation.push_back(labelPopulationMap[i]);
+      labelPopulation[population.first]=population.second;
       }
     auto meanValueMap = labelStatsFilter->GetMeanValueMap();
-    std::vector<itk::VariableLengthVector<double> > meanValues;
-    for (unsigned int i =0; i <= meanValueMap.rbegin()->first; i++)
+    std::unordered_map< unsigned int, itk::VariableLengthVector<double> > meanValues;
+    for (mean : meanValueMap)
       {
-      meanValues.push_back(meanValueMap[i]);
-      }
+      meanValues[mean.first] = mean.second;
+      }  
+      
     
     // Compute the LUT from the original label image to the merged output label image.
     auto regionMergingFilter = LabelImageSmallRegionMergingFilterType::New();
diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
index e13b8894ff..15a91b8785 100644
--- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
+++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
@@ -24,6 +24,8 @@
 #include "otbPersistentImageFilter.h"
 #include "otbPersistentFilterStreamingDecorator.h"
 
+#include <unordered_map>
+
 namespace otb
 {
 
@@ -76,9 +78,10 @@ public:
   typedef itk::VariableLengthVector<double>                             RealVectorPixelType;
   
   typedef std::map<InputLabelType, std::set<InputLabelType> >                 NeigboursMapType;
-  typedef std::vector<RealVectorPixelType >                                   LabelStatisticType;
-  typedef std::vector<double>                                                 LabelPopulationType;
-  typedef std::vector<InputLabelType>                                          LUTType;
+
+  typedef std::unordered_map<InputLabelType , RealVectorPixelType >                                   LabelStatisticType;
+  typedef std::unordered_map<InputLabelType , double>                                                 LabelPopulationType;
+  typedef std::unordered_map<InputLabelType , InputLabelType>                                          LUTType;
   
   /** Set/Get size of segments to be merged */
   itkGetMacro(Size , unsigned int);
@@ -88,12 +91,12 @@ public:
   void SetLabelPopulation( LabelPopulationType const & labelPopulation )
   {
     m_LabelPopulation = labelPopulation; 
+    
     // Initialize m_CorrespondingMap to the identity (i.e. m[label] = label)
-    m_LUT.resize( labelPopulation.size() );
-    for (unsigned int i =0; i <labelPopulation.size(); i++)
+    for (label : m_LabelPopulation)
       {
-      m_LUT[ i ] = i;
-      }
+      m_LUT[label.first] = label.first;
+      }  
   }
   
   /** Get the Label population */
diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
index 7c152b8c00..2926790498 100644
--- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
+++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
@@ -210,7 +210,6 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
     {
     assert( !itN.IsAtEnd() );
     int currentLabel = FindCorrespondingLabel(it.Get());
-    
     if ( m_LabelPopulation[currentLabel] == m_Size )
       {
       for (auto ci = itN.Begin() ; !ci.IsAtEnd(); ci++)
-- 
GitLab