Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

AreaIterator.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Stephan Aiche$
00032 // $Authors: Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_KERNEL_AREAITERATOR_H
00036 #define OPENMS_KERNEL_AREAITERATOR_H
00037 
00038 // OpenMS includes
00039 #include <OpenMS/CONCEPT/Types.h>
00040 #include <OpenMS/KERNEL/PeakIndex.h>
00041 
00042 // STL includes
00043 #include <iterator>
00044 
00045 namespace OpenMS
00046 {
00047   namespace Internal
00048   {
00057     template <class ValueT, class ReferenceT, class PointerT, class SpectrumIteratorT, class PeakIteratorT>
00058     class AreaIterator :
00059       public std::iterator<std::forward_iterator_tag, ValueT>
00060     {
00061 public:
00062       typedef DoubleReal CoordinateType;
00063       typedef ValueT PeakType;
00064       typedef SpectrumIteratorT SpectrumIteratorType;
00065       typedef PeakIteratorT PeakIteratorType;
00066 
00070 
00071       typedef ValueT value_type;
00073       typedef ReferenceT reference;
00075       typedef PointerT pointer;
00077       typedef unsigned int difference_type;
00079 
00081       AreaIterator(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end, CoordinateType low_mz, CoordinateType high_mz) :
00082         first_(first),
00083         current_scan_(begin),
00084         end_scan_(end),
00085         low_mz_(low_mz),
00086         high_mz_(high_mz),
00087         is_end_(false)
00088       {
00089         nextScan_();
00090       }
00091 
00093       AreaIterator() :
00094         first_(),
00095         current_scan_(),
00096         end_scan_(),
00097         current_peak_(),
00098         end_peak_(),
00099         low_mz_(0.0),
00100         high_mz_(0.0),
00101         is_end_(true)
00102       {}
00103 
00105       ~AreaIterator()
00106       {}
00107 
00109       AreaIterator(const AreaIterator & rhs) :
00110         first_(rhs.first_),
00111         current_scan_(rhs.current_scan_),
00112         end_scan_(rhs.end_scan_),
00113         current_peak_(rhs.current_peak_),
00114         end_peak_(rhs.end_peak_),
00115         low_mz_(rhs.low_mz_),
00116         high_mz_(rhs.high_mz_),
00117         is_end_(rhs.is_end_)
00118       {}
00119 
00121       AreaIterator & operator=(const AreaIterator & rhs)
00122       {
00123         if (&rhs == this) return *this;
00124 
00125         is_end_ = rhs.is_end_;
00126         //only copy iterators, if the assigned iterator is not the end iterator
00127         if (!is_end_)
00128         {
00129           first_ = rhs.first_;
00130           current_scan_ = rhs.current_scan_;
00131           end_scan_ = rhs.end_scan_;
00132           current_peak_ = rhs.current_peak_;
00133           end_peak_ = rhs.end_peak_;
00134           low_mz_ = rhs.low_mz_;
00135           high_mz_ = rhs.high_mz_;
00136         }
00137 
00138         return *this;
00139       }
00140 
00142       bool operator==(const AreaIterator & rhs) const
00143       {
00144         //Both end iterators => equal
00145         if (is_end_ && rhs.is_end_) return true;
00146 
00147         //Normal and end iterator => not equal
00148         if (!is_end_ && rhs.is_end_) return false;
00149 
00150         if (is_end_ && !rhs.is_end_) return false;
00151 
00152         //Equality of pointed to peak adresses
00153         return &(*current_peak_) == &(*(rhs.current_peak_));
00154       }
00155 
00157       bool operator!=(const AreaIterator & rhs) const
00158       {
00159         return !(*this == rhs);
00160       }
00161 
00163       AreaIterator & operator++()
00164       {
00165         //no increment if this is the end iterator
00166         if (is_end_) return *this;
00167 
00168         ++current_peak_;
00169         // test whether we arrived at the end of the current scan
00170         if (current_peak_ == end_peak_)
00171         {
00172           ++current_scan_;
00173           nextScan_();
00174         }
00175         return *this;
00176       }
00177 
00179       AreaIterator operator++(int)
00180       {
00181         AreaIterator tmp(*this);
00182         ++(*this);
00183         return tmp;
00184       }
00185 
00187       reference operator*() const
00188       {
00189         return current_peak_.operator*();
00190       }
00191 
00193       pointer operator->() const
00194       {
00195         return current_peak_.operator->();
00196       }
00197 
00199       CoordinateType getRT() const
00200       {
00201         return current_scan_->getRT();
00202       }
00203 
00205       inline PeakIndex getPeakIndex() const
00206       {
00207         if (is_end_)
00208         {
00209           return PeakIndex();
00210         }
00211         else
00212         {
00213           return PeakIndex(current_scan_ - first_, current_peak_ - current_scan_->begin());
00214         }
00215       }
00216 
00217 private:
00218       //Advances to the iterator to the next valid peak in the next valid spectrum
00219       void nextScan_()
00220       {
00221         while (true)
00222         {
00223           //if (current_scan_ != end_scan_) std::cout << "RT: " << current_scan_->getRT() << std::endl;
00224           while (current_scan_ != end_scan_ && current_scan_->getMSLevel() != 1)
00225           {
00226             ++current_scan_;
00227           }
00228           if (current_scan_ == end_scan_)
00229           {
00230             is_end_ = true;
00231             return;
00232           }
00233           current_peak_ = current_scan_->MZBegin(low_mz_);
00234           end_peak_ = current_scan_->MZEnd(high_mz_);
00235           if (current_peak_ != end_peak_)
00236           {
00237             return;
00238           }
00239           ++current_scan_;
00240         }
00241       }
00242 
00244       SpectrumIteratorType first_;
00246       SpectrumIteratorType current_scan_;
00248       SpectrumIteratorType end_scan_;
00250       PeakIteratorType current_peak_;
00252       PeakIteratorType end_peak_;
00254       CoordinateType low_mz_;
00256       CoordinateType high_mz_;
00258       bool is_end_;
00259 
00260     };
00261 
00262   }
00263 }
00264 
00265 #endif

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:37 using doxygen 1.7.1