OpenMS  3.0.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-2022.
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>
41 
42 // STL includes
43 #include <iterator>
44 
45 namespace OpenMS
46 {
47  namespace Internal
48  {
60  template <class ValueT, class ReferenceT, class PointerT, class SpectrumIteratorT, class PeakIteratorT>
61  class AreaIterator :
62  public std::iterator<std::forward_iterator_tag, ValueT>
63  {
64 public:
65  typedef double CoordinateType;
66  typedef ValueT PeakType;
67  typedef SpectrumIteratorT SpectrumIteratorType;
68  typedef PeakIteratorT PeakIteratorType;
69 
72  class Param
73  {
74  friend AreaIterator; // allow access to private members (avoids writing get-accessors)
75  public:
84  : first_(first), current_scan_(begin), end_scan_(end), ms_level_(ms_level)
85  {
86  }
87 
89  static Param end()
90  {
91  static Param p;
92  p.is_end_ = true;
93  return p;
94  }
95 
97  Param& operator=(const Param& rhs) = default;
98 
102  Param& lowMZ(CoordinateType low_mz) { low_mz_ = low_mz; return *this;}
105  Param& highMZ(CoordinateType high_mz) { high_mz_ = high_mz; return *this;}
107  Param& lowIM(CoordinateType low_im) { low_im_ = low_im; return *this;}
109  Param& highIM(CoordinateType high_im) { high_im_ = high_im; return *this;}
111  Param& msLevel(int8_t ms_level) { ms_level_ = ms_level; return *this;}
113 
114  protected:
125  /* optional parameters */
127  CoordinateType low_mz_ = std::numeric_limits<CoordinateType>::lowest();
129  CoordinateType high_mz_ = std::numeric_limits<CoordinateType>::max();
131  CoordinateType low_im_ = std::numeric_limits<CoordinateType>::lowest();
133  CoordinateType high_im_ = std::numeric_limits<CoordinateType>::max();
135  int8_t ms_level_{};
137  bool is_end_ = false;
138 
139  private:
141  Param() = default;
142  };
143 
144 
145 
149  typedef ValueT value_type;
152  typedef ReferenceT reference;
154  typedef PointerT pointer;
156  typedef unsigned int difference_type;
158 
160  explicit AreaIterator(const Param& p) :
161  p_(p)
162  {
163  nextScan_();
164  }
165 
167  AreaIterator() : p_(Param::end()) {}
168 
170  ~AreaIterator() = default;
171 
173  AreaIterator(const AreaIterator& rhs) = default;
174 
177  {
178  p_.is_end_ = rhs.p_.is_end_;
179  // only copy iterators, if the assigned iterator is not the end iterator
180  if (!p_.is_end_)
181  {
182  p_ = rhs.p_;
183  }
184 
185  return *this;
186  }
187 
189  bool operator==(const AreaIterator & rhs) const
190  {
191  // Both end iterators => equal
192  if (p_.is_end_ && rhs.p_.is_end_) return true;
193 
194  // Normal and end iterator => not equal
195  if (p_.is_end_ ^ rhs.p_.is_end_)
196  return false;
197 
198  // Equality of pointed to peak addresses
199  return &(*(p_.current_peak_)) == &(*(rhs.p_.current_peak_));
200  }
201 
203  bool operator!=(const AreaIterator & rhs) const
204  {
205  return !(*this == rhs);
206  }
207 
210  {
211  //no increment if this is the end iterator
212  if (p_.is_end_) return *this;
213 
214  ++p_.current_peak_;
215  // test whether we arrived at the end of the current scan
216  if (p_.current_peak_ == p_.end_peak_)
217  {
218  ++p_.current_scan_;
219  nextScan_();
220  }
221  return *this;
222  }
223 
226  {
227  AreaIterator tmp(*this);
228  ++(*this);
229  return tmp;
230  }
231 
234  {
235  return p_.current_peak_.operator*();
236  }
237 
240  {
241  return p_.current_peak_.operator->();
242  }
243 
246  {
247  return p_.current_scan_->getRT();
248  }
249 
252  {
253  return p_.current_scan_->getDriftTime();
254  }
255 
257  inline PeakIndex getPeakIndex() const
258  {
259  if (p_.is_end_)
260  {
261  return {};
262  }
263  else
264  {
266  }
267  }
268 
269 private:
271  void nextScan_()
272  {
273  using MSLevelType = decltype(p_.current_scan_->getMSLevel());
275  while (true)
276  {
277  // skip over invalid MS levels and Mobility
278  while (p_.current_scan_ != p_.end_scan_ &&
279  (p_.current_scan_->getMSLevel() != (MSLevelType)p_.ms_level_ ||
280  !mb.containsMobility(p_.current_scan_->getDriftTime())))
281  {
282  ++p_.current_scan_;
283  }
284  if (p_.current_scan_ == p_.end_scan_)
285  {
286  p_.is_end_ = true;
287  return;
288  }
291  if (p_.current_peak_ != p_.end_peak_)
292  {
293  return;
294  }
295  ++p_.current_scan_;
296  }
297  }
298 
301  };
302 
303  }
304 }
305 
CoordinateType high_mz_
high m/z boundary
Definition: AreaIterator.h:129
SpectrumIteratorType current_scan_
Iterator to the current spectrum.
Definition: AreaIterator.h:118
CoordinateType getDriftTime() const
returns the ion mobility time of the current scan
Definition: AreaIterator.h:251
PeakIteratorT PeakIteratorType
Definition: AreaIterator.h:68
ValueT PeakType
Definition: AreaIterator.h:66
SpectrumIteratorType first_
Iterator to the first scan of the map (needed to calculate the index)
Definition: AreaIterator.h:116
AreaIterator & operator=(const AreaIterator &rhs)
Assignment operator.
Definition: AreaIterator.h:176
CoordinateType high_im_
high mobility boundary
Definition: AreaIterator.h:133
double CoordinateType
Definition: AreaIterator.h:65
void nextScan_()
advances the iterator to the next valid peak in the next valid spectrum
Definition: AreaIterator.h:271
Forward iterator for an area of peaks in an experiment.
Definition: AreaIterator.h:61
Param & lowIM(CoordinateType low_im)
low ion mobility boundary
Definition: AreaIterator.h:107
unsigned int difference_type
The difference type.
Definition: AreaIterator.h:156
Param & lowMZ(CoordinateType low_mz)
low m/z boundary
Definition: AreaIterator.h:103
Param & highMZ(CoordinateType high_mz)
high m/z boundary
Definition: AreaIterator.h:105
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Param p_
holds spectra iterators and area limits
Definition: AreaIterator.h:300
SpectrumIteratorType end_scan_
Past-the-end iterator of spectra.
Definition: AreaIterator.h:120
Param(SpectrumIteratorType first, SpectrumIteratorType begin, SpectrumIteratorType end, uint8_t ms_level)
C&#39;tor with mandatory parameters.
Definition: AreaIterator.h:83
Param & operator=(const Param &rhs)=default
Assignment operator.
CoordinateType low_im_
low mobility boundary
Definition: AreaIterator.h:131
AreaIterator operator++(int)
Step forward by one (postfix operator)
Definition: AreaIterator.h:225
AreaIterator()
Default constructor (for the end iterator)
Definition: AreaIterator.h:167
AreaIterator(const Param &p)
Constructor for the begin iterator.
Definition: AreaIterator.h:160
Param()=default
only used internally for end()
PeakIteratorType end_peak_
Past-the-end iterator of peaks in the current spectrum.
Definition: AreaIterator.h:124
SpectrumIteratorT SpectrumIteratorType
Definition: AreaIterator.h:67
~AreaIterator()=default
Destructor.
ReferenceT reference
The reference type as returned by operator*()
Definition: AreaIterator.h:152
pointer operator->() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:239
PeakIteratorType current_peak_
Iterator to the current peak.
Definition: AreaIterator.h:122
static Param end()
return the end-iterator
Definition: AreaIterator.h:89
friend AreaIterator
Definition: AreaIterator.h:74
CoordinateType getRT() const
returns the retention time of the current scan
Definition: AreaIterator.h:245
int8_t ms_level_
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:135
reference operator*() const
Dereferencing of this pointer yields the underlying peak.
Definition: AreaIterator.h:233
CoordinateType low_mz_
low m/z boundary
Definition: AreaIterator.h:127
bool operator==(const AreaIterator &rhs) const
Test for equality.
Definition: AreaIterator.h:189
PointerT pointer
The pointer type as returned by operator->()
Definition: AreaIterator.h:154
Definition: AreaIterator.h:72
ValueT value_type
The iterator&#39;s value type.
Definition: AreaIterator.h:150
Definition: RangeManager.h:482
Param & highIM(CoordinateType high_im)
high ion mobility boundary
Definition: AreaIterator.h:109
bool operator!=(const AreaIterator &rhs) const
Test for inequality.
Definition: AreaIterator.h:203
Param & msLevel(int8_t ms_level)
Only scans of this MS level are iterated over.
Definition: AreaIterator.h:111
bool is_end_
Flag that indicates that this iterator is the end iterator.
Definition: AreaIterator.h:137
PeakIndex getPeakIndex() const
returns the PeakIndex corresponding to the current iterator position
Definition: AreaIterator.h:257
AreaIterator & operator++()
Step forward by one (prefix operator)
Definition: AreaIterator.h:209
Index of a peak or feature.
Definition: PeakIndex.h:50