OpenMS  2.7.0
AreaIterator.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2021.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 // OpenMS includes
38 #include <OpenMS/CONCEPT/Types.h>
40 
41 // STL includes
42 #include <iterator>
43 
44 namespace OpenMS
45 {
46  namespace Internal
47  {
56  template <class ValueT, class ReferenceT, class PointerT, class SpectrumIteratorT, class PeakIteratorT>
57  class AreaIterator :
58  public std::iterator<std::forward_iterator_tag, ValueT>
59  {
60 public:
61  typedef double CoordinateType;
62  typedef ValueT PeakType;
63  typedef SpectrumIteratorT SpectrumIteratorType;
64  typedef PeakIteratorT PeakIteratorType;
65 
70  typedef ValueT value_type;
72  typedef ReferenceT reference;
74  typedef PointerT pointer;
76  typedef unsigned int difference_type;
78 
81  first_(first),
82  current_scan_(begin),
83  end_scan_(end),
84  low_mz_(low_mz),
85  high_mz_(high_mz),
86  is_end_(false)
87  {
88  nextScan_();
89  }
90 
93  first_(),
94  current_scan_(),
95  end_scan_(),
96  current_peak_(),
97  end_peak_(),
98  low_mz_(0.0),
99  high_mz_(0.0),
100  is_end_(true)
101  {}
102 
105  {}
106 
108  AreaIterator(const AreaIterator & rhs) :
109  first_(rhs.first_),
111  end_scan_(rhs.end_scan_),
113  end_peak_(rhs.end_peak_),
114  low_mz_(rhs.low_mz_),
115  high_mz_(rhs.high_mz_),
116  is_end_(rhs.is_end_)
117  {}
118 
121  {
122  if (&rhs == this) return *this;
123 
124  is_end_ = rhs.is_end_;
125  //only copy iterators, if the assigned iterator is not the end iterator
126  if (!is_end_)
127  {
128  first_ = rhs.first_;
130  end_scan_ = rhs.end_scan_;
132  end_peak_ = rhs.end_peak_;
133  low_mz_ = rhs.low_mz_;
134  high_mz_ = rhs.high_mz_;
135  }
136 
137  return *this;
138  }
139 
141  bool operator==(const AreaIterator & rhs) const
142  {
143  //Both end iterators => equal
144  if (is_end_ && rhs.is_end_) return true;
145 
146  //Normal and end iterator => not equal
147  if (!is_end_ && rhs.is_end_) return false;
148 
149  if (is_end_ && !rhs.is_end_) return false;
150 
151  //Equality of pointed to peak addresses
152  return &(*current_peak_) == &(*(rhs.current_peak_));
153  }
154 
156  bool operator!=(const AreaIterator & rhs) const
157  {
158  return !(*this == rhs);
159  }
160 
163  {
164  //no increment if this is the end iterator
165  if (is_end_) return *this;
166 
167  ++current_peak_;
168  // test whether we arrived at the end of the current scan
169  if (current_peak_ == end_peak_)
170  {
171  ++current_scan_;
172  nextScan_();
173  }
174  return *this;
175  }
176 
179  {
180  AreaIterator tmp(*this);
181  ++(*this);
182  return tmp;
183  }
184 
187  {
188  return current_peak_.operator*();
189  }
190 
193  {
194  return current_peak_.operator->();
195  }
196 
199  {
200  return current_scan_->getRT();
201  }
202 
204  inline PeakIndex getPeakIndex() const
205  {
206  if (is_end_)
207  {
208  return PeakIndex();
209  }
210  else
211  {
213  }
214  }
215 
216 private:
217  //Advances to the iterator to the next valid peak in the next valid spectrum
218  void nextScan_()
219  {
220  while (true)
221  {
222  //if (current_scan_ != end_scan_) std::cout << "RT: " << current_scan_->getRT() << std::endl;
223  while (current_scan_ != end_scan_ && current_scan_->getMSLevel() != 1)
224  {
225  ++current_scan_;
226  }
227  if (current_scan_ == end_scan_)
228  {
229  is_end_ = true;
230  return;
231  }
232  current_peak_ = current_scan_->MZBegin(low_mz_);
233  end_peak_ = current_scan_->MZEnd(high_mz_);
234  if (current_peak_ != end_peak_)
235  {
236  return;
237  }
238  ++current_scan_;
239  }
240  }
241 
257  bool is_end_;
258 
259  };
260 
261  }
262 }
263 
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:59
SpectrumIteratorType current_scan_
Iterator to the current spectrum.
Definition: AreaIterator.h:245
bool operator!=(const AreaIterator &rhs) const
Test for inequality.
Definition: AreaIterator.h:156
PointerT pointer
The pointer type as returned by operator->()
Definition: AreaIterator.h:74
CoordinateType low_mz_
low m/z boundary
Definition: AreaIterator.h:253
double CoordinateType
Definition: AreaIterator.h:61
CoordinateType high_mz_
high m/z boundary
Definition: AreaIterator.h:255
PeakIteratorType end_peak_
Past-the-end iterator of peaks in the current spectrum.
Definition: AreaIterator.h:251
ValueT value_type
The iterator's value type.
Definition: AreaIterator.h:70
AreaIterator & operator++()
Step forward by one (prefix operator)
Definition: AreaIterator.h:162
AreaIterator()
Default constructor (for the end iterator)
Definition: AreaIterator.h:92
unsigned int difference_type
The difference type.
Definition: AreaIterator.h:76
bool is_end_
Flag that indicates that this iterator is the end iterator.
Definition: AreaIterator.h:257
PeakIteratorType current_peak_
Iterator to the current peak.
Definition: AreaIterator.h:249
PeakIteratorT PeakIteratorType
Definition: AreaIterator.h:64
SpectrumIteratorType first_
Iterator to the first scan of the map (needed to calculate the index)
Definition: AreaIterator.h:243
AreaIterator operator++(int)
Step forward by one (postfix operator)
Definition: AreaIterator.h:178
SpectrumIteratorType end_scan_
Past-the-end iterator of spectra.
Definition: AreaIterator.h:247
bool operator==(const AreaIterator &rhs) const
Test for equality.
Definition: AreaIterator.h:141
reference operator*() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:186
ReferenceT reference
The reference type as returned by operator*()
Definition: AreaIterator.h:72
AreaIterator(const AreaIterator &rhs)
Copy constructor.
Definition: AreaIterator.h:108
void nextScan_()
Definition: AreaIterator.h:218
ValueT PeakType
Definition: AreaIterator.h:62
AreaIterator(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end, CoordinateType low_mz, CoordinateType high_mz)
Constructor for the begin iterator.
Definition: AreaIterator.h:80
SpectrumIteratorT SpectrumIteratorType
Definition: AreaIterator.h:63
AreaIterator & operator=(const AreaIterator &rhs)
Assignment operator.
Definition: AreaIterator.h:120
PeakIndex getPeakIndex() const
returns the PeakIndex corresponding to the current iterator position
Definition: AreaIterator.h:204
pointer operator->() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:192
~AreaIterator()
Destructor.
Definition: AreaIterator.h:104
CoordinateType getRT() const
returns the retention time of the current scan
Definition: AreaIterator.h:198
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Index of a peak or feature.
Definition: PeakIndex.h:51