OpenMS
Loading...
Searching...
No Matches
MatchedIterator.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Chris Bielow $
6// $Authors: Chris Bielow $
7// --------------------------------------------------------------------------
8//
9#pragma once
10
14
15#include <iterator>
16#include <type_traits>
17
18namespace OpenMS
19{
20
48 template <typename CONT_T, typename TRAIT, bool CONST_T = true >
50 {
51 public:
52 // define the 5 types required for an iterator. Deriving from std::iterator is deprecated in C++17.
53 using iterator_category = std::forward_iterator_tag;
54 using value_type = typename CONT_T::value_type; //< dereferences to an element in the target container
55 using difference_type = std::ptrdiff_t;
56 using pointer = typename std::conditional<CONST_T, typename CONT_T::value_type const*, typename CONT_T::value_type*>::type;
57 using reference = typename std::conditional<CONST_T, typename CONT_T::value_type const&, typename CONT_T::value_type&>::type;
58
59 using CONT_IT = typename std::conditional<CONST_T, typename CONT_T::const_iterator, typename CONT_T::iterator>::type; // for dereferencing etc
60 using CONST_CONT_IT = typename CONT_T::const_iterator; // for input containers
61
71 explicit MatchedIterator(const CONT_T& ref, const CONT_T& target, float tolerance)
72 : MatchedIterator(ref.begin(), ref.end(), target.begin(), target.end(), tolerance)
73 {
74 }
75
87 explicit MatchedIterator(const CONST_CONT_IT ref_begin, const CONST_CONT_IT ref_end,
88 const CONST_CONT_IT tgt_begin, const CONST_CONT_IT tgt_end,
89 float tolerance)
90 : ref_begin_(ref_begin), ref_end_(ref_end), tgt_begin_(tgt_begin), tgt_end_(tgt_end), it_ref_(ref_begin), it_tgt_(tgt_begin), tol_(tolerance), is_end_(false)
91 {
92 if (tgt_begin_ == tgt_end_)
93 { // nothing to iterate over in target (if ref_ were empty, isEnd_() is automatically true)
94 setToEnd_();
95 return;
96 }
98 }
99
104 : ref_begin_(), ref_end_(), tgt_begin_(), tgt_end_(), it_ref_(), it_tgt_(), tol_(), is_end_(false)
105 {
106 }
107
109 MatchedIterator(const MatchedIterator& rhs) = default;
110
113
114 bool operator==(const MatchedIterator& rhs) const
115 {
116 if (this == &rhs) return true;
117
118 if (isEnd_() || rhs.isEnd_())
119 {
120 return isEnd_() == rhs.isEnd_();
121 }
122
123 return (it_ref_ == rhs.it_ref_ &&
124 it_tgt_ == rhs.it_tgt_ &&
125 ref_begin_ == rhs.ref_begin_ &&
126 ref_end_ == rhs.ref_end_ &&
127 tgt_begin_ == rhs.tgt_begin_ &&
128 tgt_end_ == rhs.tgt_end_);
129 }
130 bool operator!=(const MatchedIterator& rhs) const
131 {
132 return !(*this == rhs);
133 }
134
136 template< bool _CONST = CONST_T >
137 typename std::enable_if< _CONST, reference >::type operator*() const
138 {
139 return *it_tgt_;
140 }
141 template< bool _CONST = CONST_T >
142 typename std::enable_if< !_CONST, reference >::type operator*()
143 {
144 return *it_tgt_;
145 }
146
148 template< bool _CONST = CONST_T >
149 typename std::enable_if< _CONST, pointer >::type operator->() const
150 {
151 return &(*it_tgt_);
152 }
153 template< bool _CONST = CONST_T >
154 typename std::enable_if< !_CONST, pointer >::type operator->()
155 {
156 return &(*it_tgt_);
157 }
158
160 const value_type& ref() const
161 {
162 return *it_ref_;
163 }
164
166 size_t refIdx() const
167 {
168 return it_ref_ - ref_begin_;
169 }
170
172 size_t tgtIdx() const
173 {
174 return it_tgt_ - tgt_begin_;
175 }
176
183 {
184 // are we at end already? --> wrong usage
185 OPENMS_PRECONDITION(!isEnd_(), "Tried to advance beyond end iterator!");
186 ++it_ref_;
188 return *this;
189 }
190
193 {
194 MatchedIterator n(*this);
195 ++(*this);
196 return n;
197 }
198
201 {
202 return MatchedIterator(true);
203 }
204
205 protected:
206
209 MatchedIterator(bool /*is_end*/)
210 : ref_begin_(), ref_end_(), tgt_begin_(), tgt_end_(), it_ref_(), it_tgt_(), tol_(), is_end_(true)
211 {
212 }
213
215 {
216 is_end_ = true;
217 }
218
219 bool isEnd_() const
220 {
221 return is_end_;
222 }
223
225 {
226 while (it_ref_ != ref_end_)
227 { // note: it_tgt_ always points to a valid element (unless the whole container was empty -- see CTor)
228
229 double max_dist = TRAIT::allowedTol(tol_, *it_ref_);
230
231 // forward iterate over elements in target data until distance gets worse.
232 // Equal distances (e.g. equal target values) do not stop the walk; the first (smaller) element is kept.
233 // Distances are compared in the TRAIT's own type (auto drops a returned reference), so equal target values always tie.
234 CONT_IT best = it_tgt_;
235 auto diff = TRAIT::getDiffAbsolute(*it_ref_, *it_tgt_);
236 while (++it_tgt_ != tgt_end_)
237 {
238 const auto d = TRAIT::getDiffAbsolute(*it_ref_, *it_tgt_);
239 if (d < diff) // getting better
240 {
241 diff = d;
242 best = it_tgt_;
243 }
244 else if (d > diff) // getting worse (overshot)
245 {
246 break;
247 }
248 }
249 it_tgt_ = best; // closest valid entry (never tgt_end_)
250
251 if (float(diff) <= max_dist) return; // ok, found match (in float precision, like the tolerance)
252
253 // try next ref peak
254 ++it_ref_;
255 }
256 // reached end of ref container
257 setToEnd_();
258 // i.e. isEnd() is true now
259 }
260
264 float tol_;
265 bool is_end_ = false;
266 };
267
270 {
271 template <typename T>
272 static float allowedTol(float tol, const T& /*mz_ref*/)
273 {
274 return tol;
275 }
277 template <typename T>
278 static T getDiffAbsolute(const T& elem_ref, const T& elem_tgt)
279 {
280 return fabs(elem_ref - elem_tgt);
281 }
282 };
283
286 struct PpmTrait
287 {
288 template <typename T>
289 static float allowedTol(float tol, const T& elem_ref)
290 {
291 return Math::ppmToMass(tol, (float)elem_ref.getMZ());
292 }
294 template <typename T>
295 static float getDiffAbsolute(const T& elem_ref, const T& elem_tgt)
296 {
297 return fabs(elem_ref.getMZ() - elem_tgt.getMZ());
298 }
299 };
300
303 struct DaTrait
304 {
305 template <typename T>
306 static float allowedTol(float tol, const T& /*mz_ref*/)
307 {
308 return tol;
309 }
311 template <typename T>
312 static float getDiffAbsolute(const T& elem_ref, const T& elem_tgt)
313 {
314 return fabs(elem_ref.getMZ() - elem_tgt.getMZ());
315 }
316 };
317
318} // namespace OpenMS
For each element in the reference container the closest peak in the target will be searched....
Definition MatchedIterator.h:50
CONT_IT it_ref_
Definition MatchedIterator.h:263
MatchedIterator & operator=(const MatchedIterator &rhs)=default
Assignment operator (default)
bool operator!=(const MatchedIterator &rhs) const
Definition MatchedIterator.h:130
MatchedIterator & operator++()
Advances to the next valid pair.
Definition MatchedIterator.h:182
CONST_CONT_IT ref_end_
Definition MatchedIterator.h:261
MatchedIterator(const MatchedIterator &rhs)=default
Copy CTor (default)
CONST_CONT_IT tgt_end_
Definition MatchedIterator.h:262
typename std::conditional< CONST_T, typename CONT_T::value_type const &, typename CONT_T::value_type & >::type reference
Definition MatchedIterator.h:57
CONT_IT it_tgt_
Definition MatchedIterator.h:263
MatchedIterator(const CONST_CONT_IT ref_begin, const CONST_CONT_IT ref_end, const CONST_CONT_IT tgt_begin, const CONST_CONT_IT tgt_end, float tolerance)
Constructs a MatchedIterator on two containers. The way a match is found, depends on the TRAIT type (...
Definition MatchedIterator.h:87
MatchedIterator(bool)
Definition MatchedIterator.h:209
size_t refIdx() const
index into reference container
Definition MatchedIterator.h:166
bool isEnd_() const
Definition MatchedIterator.h:219
MatchedIterator(const CONT_T &ref, const CONT_T &target, float tolerance)
Constructs a MatchedIterator on two containers. The way a match is found, depends on the TRAIT type (...
Definition MatchedIterator.h:71
std::forward_iterator_tag iterator_category
Definition MatchedIterator.h:53
typename CONT_T::const_iterator CONST_CONT_IT
Definition MatchedIterator.h:60
void advanceTarget_()
Definition MatchedIterator.h:224
bool operator==(const MatchedIterator &rhs) const
Definition MatchedIterator.h:114
size_t tgtIdx() const
index into target container
Definition MatchedIterator.h:172
MatchedIterator operator++(int)
post-increment
Definition MatchedIterator.h:192
bool is_end_
Definition MatchedIterator.h:265
typename CONT_T::value_type value_type
Definition MatchedIterator.h:54
const value_type & ref() const
current element in reference container
Definition MatchedIterator.h:160
std::enable_if<!_CONST, reference >::type operator*()
Definition MatchedIterator.h:142
std::enable_if< _CONST, reference >::type operator*() const
dereference current target element
Definition MatchedIterator.h:137
typename std::conditional< CONST_T, typename CONT_T::value_type const *, typename CONT_T::value_type * >::type pointer
Definition MatchedIterator.h:56
std::enable_if<!_CONST, pointer >::type operator->()
Definition MatchedIterator.h:154
static MatchedIterator end()
the end iterator
Definition MatchedIterator.h:200
CONST_CONT_IT tgt_begin_
Definition MatchedIterator.h:262
std::enable_if< _CONST, pointer >::type operator->() const
pointer to current target element
Definition MatchedIterator.h:149
std::ptrdiff_t difference_type
Definition MatchedIterator.h:55
MatchedIterator()
Default CTor; do not use this for anything other than assigning to it;.
Definition MatchedIterator.h:103
CONST_CONT_IT ref_begin_
Definition MatchedIterator.h:261
void setToEnd_()
Definition MatchedIterator.h:214
float tol_
Definition MatchedIterator.h:264
typename std::conditional< CONST_T, typename CONT_T::const_iterator, typename CONT_T::iterator >::type CONT_IT
Definition MatchedIterator.h:59
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition openms/include/OpenMS/CONCEPT/Macros.h:91
T ppmToMass(T ppm, T mz_ref)
Compute the mass diff in [Th], given a ppm value and a reference point.
Definition MathFunctions.h:405
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Definition MatchedIterator.h:304
static float getDiffAbsolute(const T &elem_ref, const T &elem_tgt)
for Peak1D & Co
Definition MatchedIterator.h:312
static float allowedTol(float tol, const T &)
Definition MatchedIterator.h:306
Definition MatchedIterator.h:287
static float getDiffAbsolute(const T &elem_ref, const T &elem_tgt)
for Peak1D & Co
Definition MatchedIterator.h:295
static float allowedTol(float tol, const T &elem_ref)
Definition MatchedIterator.h:289
Trait for MatchedIterator to find pairs with a certain distance, which is computed directly on the va...
Definition MatchedIterator.h:270
static T getDiffAbsolute(const T &elem_ref, const T &elem_tgt)
just use fabs on the value directly
Definition MatchedIterator.h:278
static float allowedTol(float tol, const T &)
Definition MatchedIterator.h:272