OpenMS  2.7.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
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) More...
 
 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) More...
 
 MatchedIterator ()
 Default CTor; do not use this for anything other than assigning to it;. More...
 
 MatchedIterator (const MatchedIterator &rhs)=default
 Copy CTor (default) More...
 
MatchedIteratoroperator= (const MatchedIterator &rhs)=default
 Assignment operator (default) More...
 
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 More...
 
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 More...
 
template<bool _CONST = CONST_T>
std::enable_if< !_CONST, pointer >::type operator-> ()
 
const value_typeref () const
 current element in reference container More...
 
size_t refIdx () const
 index into reference container More...
 
size_t tgtIdx () const
 index into target container More...
 
MatchedIteratoroperator++ ()
 Advances to the next valid pair. More...
 
MatchedIterator operator++ (int)
 post-increment More...
 

Static Public Member Functions

static MatchedIterator end ()
 the end iterator More...
 

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

using CONST_CONT_IT = typename CONT_T::const_iterator

◆ CONT_IT

using CONT_IT = typename std::conditional<CONST_T, typename CONT_T::const_iterator, typename CONT_T::iterator>::type

◆ difference_type

using difference_type = std::ptrdiff_t

◆ iterator_category

using iterator_category = std::forward_iterator_tag

◆ pointer

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

◆ reference

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

◆ value_type

using value_type = typename CONT_T::value_type

Constructor & Destructor Documentation

◆ MatchedIterator() [1/5]

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
refFor each element in this reference container the closest peak in the target will be searched
targetTarget container
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]

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
ref_beginBegin range of reference container
ref_endEnd range of reference container
tgt_beginBegin range of reference container
tgt_endEnd range of reference container
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]

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]

MatchedIterator ( const MatchedIterator< CONT_T, TRAIT, CONST_T > &  rhs)
default

Copy CTor (default)

◆ MatchedIterator() [5/5]

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_()

void advanceTarget_ ( )
inlineprotected

◆ end()

static MatchedIterator end ( )
inlinestatic

◆ isEnd_()

bool isEnd_ ( ) const
inlineprotected

◆ operator!=()

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

◆ operator*() [1/2]

std::enable_if< !_CONST, reference >::type operator* ( )
inline

◆ operator*() [2/2]

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]

MatchedIterator& operator++ ( )
inline

◆ operator++() [2/2]

MatchedIterator operator++ ( int  )
inline

post-increment

◆ operator->() [1/2]

std::enable_if< !_CONST, pointer >::type operator-> ( )
inline

◆ operator->() [2/2]

std::enable_if< _CONST, pointer >::type operator-> ( ) const
inline

pointer to current target element

◆ operator=()

MatchedIterator& operator= ( const MatchedIterator< CONT_T, TRAIT, CONST_T > &  rhs)
default

Assignment operator (default)

◆ operator==()

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

◆ ref()

const value_type& ref ( ) const
inline

current element in reference container

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

◆ refIdx()

size_t refIdx ( ) const
inline

◆ setToEnd_()

void setToEnd_ ( )
inlineprotected

◆ tgtIdx()

size_t tgtIdx ( ) const
inline

Member Data Documentation

◆ is_end_

bool is_end_ = false
protected

◆ it_ref_

CONT_IT it_ref_
protected

◆ it_tgt_

CONT_IT it_tgt_
protected

◆ ref_begin_

CONST_CONT_IT ref_begin_
protected

◆ ref_end_

CONST_CONT_IT ref_end_
protected

◆ tgt_begin_

CONST_CONT_IT tgt_begin_
protected

◆ tgt_end_

CONST_CONT_IT tgt_end_
protected

◆ tol_

float tol_
protected