OpenMS
Loading...
Searching...
No Matches
MatchedIterator< CONT_T, TRAIT, CONST_T > Class Template Reference

For each element in the reference container the closest peak in the target will be searched. If no match is found within the tolerance window, the peak will be skipped over. More...

#include <OpenMS/DATASTRUCTURES/MatchedIterator.h>

Collaboration diagram for MatchedIterator< CONT_T, TRAIT, CONST_T >:
[legend]

Public Types

using iterator_category = std::forward_iterator_tag
 
using value_type = typename CONT_T::value_type
 
using difference_type = std::ptrdiff_t
 
using pointer = typename std::conditional< CONST_T, typename CONT_T::value_type const *, typename CONT_T::value_type * >::type
 
using reference = typename std::conditional< CONST_T, typename CONT_T::value_type const &, typename CONT_T::value_type & >::type
 
using CONT_IT = typename std::conditional< CONST_T, typename CONT_T::const_iterator, typename CONT_T::iterator >::type
 
using CONST_CONT_IT = typename CONT_T::const_iterator
 

Public Member Functions

 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 (ppm or Da tolerance)
 
 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 (ppm or Da tolerance)
 
 MatchedIterator ()
 Default CTor; do not use this for anything other than assigning to it;.
 
 MatchedIterator (const MatchedIterator &rhs)=default
 Copy CTor (default)
 
MatchedIteratoroperator= (const MatchedIterator &rhs)=default
 Assignment operator (default)
 
bool operator== (const MatchedIterator &rhs) const
 
bool operator!= (const MatchedIterator &rhs) const
 
template<bool _CONST = CONST_T>
std::enable_if< _CONST, reference >::type operator* () const
 dereference current target element
 
template<bool _CONST = CONST_T>
std::enable_if<!_CONST, reference >::type operator* ()
 
template<bool _CONST = CONST_T>
std::enable_if< _CONST, pointer >::type operator-> () const
 pointer to current target element
 
template<bool _CONST = CONST_T>
std::enable_if<!_CONST, pointer >::type operator-> ()
 
const value_typeref () const
 current element in reference container
 
size_t refIdx () const
 index into reference container
 
size_t tgtIdx () const
 index into target container
 
MatchedIteratoroperator++ ()
 Advances to the next valid pair.
 
MatchedIterator operator++ (int)
 post-increment
 

Static Public Member Functions

static MatchedIterator end ()
 the end iterator
 

Protected Member Functions

 MatchedIterator (bool)
 
void setToEnd_ ()
 
bool isEnd_ () const
 
void advanceTarget_ ()
 

Protected Attributes

CONST_CONT_IT ref_begin_
 
CONST_CONT_IT ref_end_
 
CONST_CONT_IT tgt_begin_
 
CONST_CONT_IT tgt_end_
 
CONT_IT it_ref_
 
CONT_IT it_tgt_
 
float tol_
 
bool is_end_ = false
 

Detailed Description

template<typename CONT_T, typename TRAIT, bool CONST_T = true>
class OpenMS::MatchedIterator< CONT_T, TRAIT, CONST_T >

For each element in the reference container the closest peak in the target will be searched. If no match is found within the tolerance window, the peak will be skipped over.

This class can be used for example to iterate through the matching peaks in two spectra (e.g. experimental spectrum and reference spectrum) that are within a given tolerance (in m/z, RT, or something user-defined).

The iterator always chooses the closest matching peak in the target container, if more than one candidate is found in the match-window. If two peaks have equal distance, the smaller value is preferred. If no peak is found within the given tolerance (distance), the reference peak does not yield a result and the next reference peak is tested. This means the operator++ can be called at most(!) ref.size()-1 times before it == is.end().

The TRAIT template argument (e.g., ValueTrait, DaTrait or PpmTrait) encodes the distance metric (on the value directly, or a member of the value_type, e.g. ppm or Da for m/z, or RT or any other metric you like). The simplest use case would be a vector<float> or similar. The TRAIT struct just defines two functions which return some distance metrics and accept elements of the container CONT_T as arguments. Both containers must be sorted with respect to the comparator used in TRAIT.

The CONST_T template argument switches between a 'const_iterator' and an 'iterator'.

This iterator is much more efficient than iterating over the reference container and calling findNearest(), i.e. binary search on the target container, i.e. O(n+m) vs. O(n*log(m)). Since this container is much more cache-friendly, the actual speedups are even larger.

Member Typedef Documentation

◆ CONST_CONT_IT

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
using CONST_CONT_IT = typename CONT_T::const_iterator

◆ CONT_IT

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
using CONT_IT = typename std::conditional<CONST_T, typename CONT_T::const_iterator, typename CONT_T::iterator>::type

◆ difference_type

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
using difference_type = std::ptrdiff_t

◆ iterator_category

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
using iterator_category = std::forward_iterator_tag

◆ pointer

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
using pointer = typename std::conditional<CONST_T, typename CONT_T::value_type const*, typename CONT_T::value_type*>::type

◆ reference

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
using reference = typename std::conditional<CONST_T, typename CONT_T::value_type const&, typename CONT_T::value_type&>::type

◆ value_type

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
using value_type = typename CONT_T::value_type

Constructor & Destructor Documentation

◆ MatchedIterator() [1/5]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
MatchedIterator ( const CONT_T &  ref,
const CONT_T &  target,
float  tolerance 
)
inlineexplicit

Constructs a MatchedIterator on two containers. The way a match is found, depends on the TRAIT type (ppm or Da tolerance)

For each element in the reference container the closest peak in the target will be searched. If no match is found within the tolerance window, the peak will be skipped over.

Parameters
[in]refFor each element in this reference container the closest peak in the target will be searched
[in]targetTarget container
[in]toleranceMaximal distance between a valid matching pair in reference and target (unit is according to TRAIT::getDiffAbsolute(), i.e. could be ppm, Da, seconds, ...)

◆ MatchedIterator() [2/5]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
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 
)
inlineexplicit

Constructs a MatchedIterator on two containers. The way a match is found, depends on the TRAIT type (ppm or Da tolerance)

For each element in the reference container the closest peak in the target will be searched. If no match is found within the tolerance window, the peak will be skipped over.

Parameters
[in]ref_beginBegin range of reference container
[in]ref_endEnd range of reference container
[in]tgt_beginBegin range of reference container
[in]tgt_endEnd range of reference container
[in]toleranceMaximal distance between a valid matching pair in reference and target (unit is according to TRAIT::getDiffAbsolute(), i.e. could be ppm, Da, seconds, ...)

References MatchedIterator< CONT_T, TRAIT, CONST_T >::advanceTarget_(), MatchedIterator< CONT_T, TRAIT, CONST_T >::setToEnd_(), MatchedIterator< CONT_T, TRAIT, CONST_T >::tgt_begin_, and MatchedIterator< CONT_T, TRAIT, CONST_T >::tgt_end_.

◆ MatchedIterator() [3/5]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
MatchedIterator ( )
inlineexplicit

Default CTor; do not use this for anything other than assigning to it;.

Referenced by MatchedIterator< CONT_T, TRAIT, CONST_T >::end().

◆ MatchedIterator() [4/5]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
MatchedIterator ( const MatchedIterator< CONT_T, TRAIT, CONST_T > &  rhs)
default

Copy CTor (default)

◆ MatchedIterator() [5/5]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
MatchedIterator ( bool  )
inlineprotected

protected Ctor which creates and end() iterator the bool argument is only used to call the correct target (its value is ignored)

Member Function Documentation

◆ advanceTarget_()

◆ end()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
static MatchedIterator end ( )
inlinestatic

◆ isEnd_()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
bool isEnd_ ( ) const
inlineprotected

◆ operator!=()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
bool operator!= ( const MatchedIterator< CONT_T, TRAIT, CONST_T > &  rhs) const
inline

◆ operator*() [1/2]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
template<bool _CONST = CONST_T>
std::enable_if<!_CONST, reference >::type operator* ( )
inline

◆ operator*() [2/2]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
template<bool _CONST = CONST_T>
std::enable_if< _CONST, reference >::type operator* ( ) const
inline

dereference current target element

References MatchedIterator< CONT_T, TRAIT, CONST_T >::it_tgt_.

◆ operator++() [1/2]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
MatchedIterator & operator++ ( )
inline

◆ operator++() [2/2]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
MatchedIterator operator++ ( int  )
inline

post-increment

◆ operator->() [1/2]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
template<bool _CONST = CONST_T>
std::enable_if<!_CONST, pointer >::type operator-> ( )
inline

◆ operator->() [2/2]

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
template<bool _CONST = CONST_T>
std::enable_if< _CONST, pointer >::type operator-> ( ) const
inline

pointer to current target element

◆ operator=()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
MatchedIterator & operator= ( const MatchedIterator< CONT_T, TRAIT, CONST_T > &  rhs)
default

Assignment operator (default)

◆ operator==()

◆ ref()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
const value_type & ref ( ) const
inline

current element in reference container

References MatchedIterator< CONT_T, TRAIT, CONST_T >::it_ref_.

◆ refIdx()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
size_t refIdx ( ) const
inline

◆ setToEnd_()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
void setToEnd_ ( )
inlineprotected

◆ tgtIdx()

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
size_t tgtIdx ( ) const
inline

Member Data Documentation

◆ is_end_

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
bool is_end_ = false
protected

◆ it_ref_

◆ it_tgt_

◆ ref_begin_

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
CONST_CONT_IT ref_begin_
protected

◆ ref_end_

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
CONST_CONT_IT ref_end_
protected

◆ tgt_begin_

◆ tgt_end_

◆ tol_

template<typename CONT_T , typename TRAIT , bool CONST_T = true>
float tol_
protected