From 888d57442db4671720eee458e5f0d846d32d2877 Mon Sep 17 00:00:00 2001
From: Walter Van Herck <w.van.herck@fz-juelich.de>
Date: Fri, 16 Nov 2012 15:07:39 +0100
Subject: [PATCH] Changed masking in iterators and DWBASimulation
 (multithreading) to using the new Mask class hierarchy

---
 Core/Algorithms/inc/DWBASimulation.h          |  7 +--
 Core/Algorithms/inc/Mask.h                    |  1 +
 Core/Algorithms/src/DWBASimulation.cpp        | 20 +++++----
 Core/Algorithms/src/DiffuseDWBASimulation.cpp |  2 +-
 Core/Algorithms/src/GISASExperiment.cpp       |  1 -
 .../src/LayerDecoratorDWBASimulation.cpp      |  2 +-
 Core/Algorithms/src/Mask.cpp                  |  3 +-
 .../src/MultiLayerRoughnessDWBASimulation.cpp |  2 +-
 Core/Core.pro                                 |  2 -
 Core/Tools/inc/OutputDataIterator.h           | 43 +++++++++++++++++--
 10 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/Core/Algorithms/inc/DWBASimulation.h b/Core/Algorithms/inc/DWBASimulation.h
index e5ea21e6211..aa1d470059f 100644
--- a/Core/Algorithms/inc/DWBASimulation.h
+++ b/Core/Algorithms/inc/DWBASimulation.h
@@ -16,7 +16,6 @@
 
 #include "ISimulation.h"
 #include "OutputData.h"
-#include "MaskedOutputDataIterator.h"
 #include "ThreadInfo.h"
 #include "Types.h"
 
@@ -53,14 +52,12 @@ public:
 
     typedef OutputDataIterator<double, OutputData<double> > iterator;
     typedef OutputDataIterator<const double, const OutputData<double> > const_iterator;
-    typedef MaskedOutputDataIterator<double, OutputData<double> > masked_iterator;
-    typedef MaskedOutputDataIterator<const double, const OutputData<double> > const_masked_iterator;
 
     //! return a read/write iterator that points to the first element
-    masked_iterator begin();
+    iterator begin();
 
     //! return a read-only iterator that points to the first element
-    const_masked_iterator begin() const;
+    const_iterator begin() const;
 
     //! return a read/write iterator that points to the one past last element
     const iterator end() { return m_dwba_intensity.end(); }
diff --git a/Core/Algorithms/inc/Mask.h b/Core/Algorithms/inc/Mask.h
index 4ea92419ba6..3f3b446fd61 100644
--- a/Core/Algorithms/inc/Mask.h
+++ b/Core/Algorithms/inc/Mask.h
@@ -22,6 +22,7 @@
 class Mask
 {
 public:
+    template <class TValue, class TContainer> friend class OutputDataIterator;
     Mask(Mask *p_submask=0);
     virtual ~Mask() { delete mp_submask; }
     virtual Mask *clone() const;
diff --git a/Core/Algorithms/src/DWBASimulation.cpp b/Core/Algorithms/src/DWBASimulation.cpp
index 444ffe9755f..35aaa3ae7b2 100644
--- a/Core/Algorithms/src/DWBASimulation.cpp
+++ b/Core/Algorithms/src/DWBASimulation.cpp
@@ -35,19 +35,21 @@ DWBASimulation *DWBASimulation::clone()
     return p_result;
 }
 
-DWBASimulation::masked_iterator DWBASimulation::begin()
+DWBASimulation::iterator DWBASimulation::begin()
 {
     if (m_thread_info.n_threads<2) {
         m_thread_info.n_threads = 1;
         m_thread_info.i_thread = 0;
     }
-    masked_iterator result(m_dwba_intensity.begin());
-    OutputDataMaskStrategyIndexModulus strategy(m_thread_info.n_threads, m_thread_info.i_thread);
-    result.setStrategy(strategy);
+    iterator result(m_dwba_intensity.begin());
+    if (m_thread_info.n_threads>1) {
+        MaskIndexModulus thread_mask(m_thread_info.n_threads, m_thread_info.i_thread);
+        result.addMask(thread_mask);
+    }
     return result;
 }
 
-DWBASimulation::const_masked_iterator DWBASimulation::begin() const
+DWBASimulation::const_iterator DWBASimulation::begin() const
 {
     size_t n_threads = m_thread_info.n_threads;
     size_t i_thread = m_thread_info.i_thread;
@@ -55,9 +57,11 @@ DWBASimulation::const_masked_iterator DWBASimulation::begin() const
         n_threads = 1;
         i_thread = 0;
     }
-    const_masked_iterator result(m_dwba_intensity.begin());
-    OutputDataMaskStrategyIndexModulus strategy(n_threads, i_thread);
-    result.setStrategy(strategy);
+    const_iterator result(m_dwba_intensity.begin());
+    if (n_threads>1) {
+        MaskIndexModulus thread_mask(n_threads, i_thread);
+        result.addMask(thread_mask);
+    }
     return result;
 }
 
diff --git a/Core/Algorithms/src/DiffuseDWBASimulation.cpp b/Core/Algorithms/src/DiffuseDWBASimulation.cpp
index 03ecc744080..b722f4f6338 100644
--- a/Core/Algorithms/src/DiffuseDWBASimulation.cpp
+++ b/Core/Algorithms/src/DiffuseDWBASimulation.cpp
@@ -22,7 +22,7 @@ void DiffuseDWBASimulation::run()
     initDiffuseFormFactorTerms(diffuse_terms, nbr_heights, samples_per_particle);
     double wavevector_scattering_factor = M_PI/getWaveLength()/getWaveLength();
 
-    DWBASimulation::masked_iterator it_intensity = begin();
+    DWBASimulation::iterator it_intensity = begin();
     while ( it_intensity != end() ) {
         double phi_f = getDWBAIntensity().getValueOfAxis<double>("phi_f", it_intensity.getIndex());
         double alpha_f = getDWBAIntensity().getValueOfAxis<double>("alpha_f", it_intensity.getIndex());
diff --git a/Core/Algorithms/src/GISASExperiment.cpp b/Core/Algorithms/src/GISASExperiment.cpp
index 66c932f6bbc..9af59f30cec 100644
--- a/Core/Algorithms/src/GISASExperiment.cpp
+++ b/Core/Algorithms/src/GISASExperiment.cpp
@@ -6,7 +6,6 @@
 #include "MathFunctions.h"
 #include "ProgramOptions.h"
 #include "ConvolutionDetectorResolution.h"
-#include "MaskedOutputDataIterator.h"
 
 #include <boost/thread.hpp>
 
diff --git a/Core/Algorithms/src/LayerDecoratorDWBASimulation.cpp b/Core/Algorithms/src/LayerDecoratorDWBASimulation.cpp
index 8b3c3ad8b57..8d116d2af6a 100644
--- a/Core/Algorithms/src/LayerDecoratorDWBASimulation.cpp
+++ b/Core/Algorithms/src/LayerDecoratorDWBASimulation.cpp
@@ -84,7 +84,7 @@ void LayerDecoratorDWBASimulation::calculateCoherentIntensity(IInterferenceFunct
     double wavelength = getWaveLength();
     double total_surface_density = mp_layer_decorator->getTotalParticleSurfaceDensity();
 
-    DWBASimulation::masked_iterator it_intensity = begin();
+    DWBASimulation::iterator it_intensity = begin();
     while ( it_intensity != end() )
     {
         double phi_f = getDWBAIntensity().getValueOfAxis<double>("phi_f", it_intensity.getIndex());
diff --git a/Core/Algorithms/src/Mask.cpp b/Core/Algorithms/src/Mask.cpp
index 18b44fd482c..f3b6a2be611 100644
--- a/Core/Algorithms/src/Mask.cpp
+++ b/Core/Algorithms/src/Mask.cpp
@@ -76,5 +76,6 @@ MaskIndexModulus* MaskIndexModulus::clone() const
 
 bool MaskIndexModulus::isMasked(size_t total_index) const
 {
-    return m_mask_index % m_modulus == m_remainder;
+    (void)total_index;
+    return m_mask_index % m_modulus != m_remainder;
 }
diff --git a/Core/Algorithms/src/MultiLayerRoughnessDWBASimulation.cpp b/Core/Algorithms/src/MultiLayerRoughnessDWBASimulation.cpp
index a95d4c0a9c4..4384bc8239c 100644
--- a/Core/Algorithms/src/MultiLayerRoughnessDWBASimulation.cpp
+++ b/Core/Algorithms/src/MultiLayerRoughnessDWBASimulation.cpp
@@ -26,7 +26,7 @@ void MultiLayerRoughnessDWBASimulation::run()
     kvector_t m_ki_real(m_ki.x().real(), m_ki.y().real(), m_ki.z().real());
     double lambda = 2.0*M_PI/m_ki_real.mag();
 
-    DWBASimulation::masked_iterator it_intensity = begin();
+    DWBASimulation::iterator it_intensity = begin();
     while ( it_intensity != m_dwba_intensity.end() )
     {
         double phi_f = getDWBAIntensity().getValueOfAxis<double>("phi_f", it_intensity.getIndex());
diff --git a/Core/Core.pro b/Core/Core.pro
index 9dda300728b..bada46dcbdd 100644
--- a/Core/Core.pro
+++ b/Core/Core.pro
@@ -229,14 +229,12 @@ HEADERS += \
     Tools/inc/IStochasticParameter.h \
     Tools/inc/LLData.h \
     Tools/inc/Macros.h \
-    Tools/inc/MaskedOutputDataIterator.h \
     Tools/inc/MathFunctions.h \
     Tools/inc/NamedVector.h \
     Tools/inc/Numeric.h \
     Tools/inc/OutputData.h \
     Tools/inc/OutputDataIOFactory.h \
     Tools/inc/OutputDataIterator.h \
-    Tools/inc/OutputDataMaskStrategies.h \
     Tools/inc/OutputDataReader.h \
     Tools/inc/OutputDataWriter.h \
     Tools/inc/ParameterPool.h \
diff --git a/Core/Tools/inc/OutputDataIterator.h b/Core/Tools/inc/OutputDataIterator.h
index 0ea367d12d5..99799c94808 100644
--- a/Core/Tools/inc/OutputDataIterator.h
+++ b/Core/Tools/inc/OutputDataIterator.h
@@ -14,6 +14,8 @@
 //! @author Scientific Computing Group at FRM II
 //! @date   Nov 12, 2012
 
+#include "Mask.h"
+
 //- -------------------------------------------------------------------
 //! @class OutputDataIterator
 //! @brief Definition of iterator for underlying OutputData container
@@ -22,8 +24,7 @@ template <class TValue, class TContainer> class OutputDataIterator
 {
 public:
     //! constructor
-    OutputDataIterator(TContainer *p_output_data, size_t start_at_index=0)
-        : m_current_index(start_at_index), mp_output_data(p_output_data) {}
+    OutputDataIterator(TContainer *p_output_data, size_t start_at_index=0);
 
     //! templated copy construction
     template<class TValue2, class TContainer2> OutputDataIterator(
@@ -53,6 +54,12 @@ public:
     //! get container pointer
     TContainer *const getContainer() const { return mp_output_data; }
 
+    //! get mask
+    Mask *const getMask() const { return mp_mask; }
+
+    //! add mask (also resets index to first available element)
+    void addMask(const Mask &mask);
+
     //! comparison
     template <class TValue2, class TContainer2> bool operator==(
             const OutputDataIterator<TValue2, TContainer2> &other);
@@ -63,16 +70,27 @@ protected:
     virtual void swapContents(OutputDataIterator<TValue, TContainer> &other);
     size_t m_current_index;
     TContainer *mp_output_data;
+    Mask *mp_mask;
 };
 
+template<class TValue, class TContainer> OutputDataIterator<TValue, TContainer>::OutputDataIterator(
+        TContainer *p_output_data, size_t start_at_index)
+: m_current_index(start_at_index)
+, mp_output_data(p_output_data)
+, mp_mask(0)
+{
+}
+
 template<class TValue, class TContainer>
 template<class TValue2, class TContainer2>
 OutputDataIterator<TValue, TContainer>::OutputDataIterator(const OutputDataIterator<TValue2, TContainer2> &other)
 : m_current_index(0)
 , mp_output_data(0)
+, mp_mask(0)
 {
     mp_output_data = other.getContainer();
     m_current_index = other.getIndex();
+    mp_mask = other.getMask() ? other.getMask()->clone() : 0;
 }
 
 template<class TValue, class TContainer>
@@ -86,11 +104,17 @@ OutputDataIterator<TValue, TContainer> &OutputDataIterator<TValue, TContainer>::
 
 template<class TValue, class TContainer> OutputDataIterator<TValue, TContainer>::~OutputDataIterator()
 {
+    delete mp_mask;
 }
 
 template<class TValue, class TContainer> OutputDataIterator<TValue, TContainer> &OutputDataIterator<TValue, TContainer>::operator++()
 {
-    ++m_current_index;
+    if (mp_mask) {
+        m_current_index = mp_mask->getNextIndex(m_current_index);
+    }
+    else {
+        ++m_current_index;
+    }
     return *this;
 }
 
@@ -112,6 +136,18 @@ template<class TValue, class TContainer> TValue* OutputDataIterator<TValue, TCon
     return &((*mp_output_data)[m_current_index]);
 }
 
+template<class TValue, class TContainer> void OutputDataIterator<TValue, TContainer>::addMask(const Mask &mask)
+{
+    if (mask.mp_submask) {
+        throw RuntimeErrorException("One can only add single masks to OUtputDataIterator at a time");
+    }
+    Mask *p_old_mask = getMask();
+    mp_mask = mask.clone();
+    mp_mask->mp_submask = p_old_mask;
+    mp_mask->setMaxIndex(mp_output_data->getAllocatedSize());
+    m_current_index = mp_mask->getFirstValidIndex();
+}
+
 template<class TValue, class TContainer>
 template<class TValue2, class TContainer2> bool OutputDataIterator<TValue, TContainer>::operator==(
         const OutputDataIterator<TValue2, TContainer2>& other)
@@ -123,6 +159,7 @@ template<class TValue, class TContainer> void OutputDataIterator<TValue, TContai
 {
     std::swap(this->m_current_index, other.m_current_index);
     std::swap(this->mp_output_data, other.mp_output_data);
+    std::swap(this->mp_mask, other.mp_mask);
 }
 
 #endif /* OUTPUTDATAITERATOR_H_ */
-- 
GitLab