OpenMS
UniqueIdIndexer< T > Class Template Reference

A base class for containers with elements derived from UniqueIdInterface. This adds functionality to convert a unique id into an index into the container. More...

#include <OpenMS/CONCEPT/UniqueIdIndexer.h>

Inheritance diagram for UniqueIdIndexer< T >:
[legend]
Collaboration diagram for UniqueIdIndexer< T >:
[legend]

Public Types

typedef std::unordered_map< UInt64, SizeUniqueIdMap
 

Public Member Functions

Size uniqueIdToIndex (UInt64 unique_id) const
 Returns the index of the feature with the given unique id, or Size(-1) if none exists in this random access container. More...
 
void updateUniqueIdToIndex () const
 Updates the hash map from unique id to index. More...
 
Size resolveUniqueIdConflicts ()
 Assign new UID's to doubly occurring UID's. More...
 
void swap (UniqueIdIndexer &rhs)
 Swap. More...
 

Protected Member Functions

const auto & getBase_ () const
 A little helper to get access to the base (!) class RandomAccessContainer. More...
 
auto & getBase_ ()
 A little helper to get access to the base (!) class RandomAccessContainer. More...
 

Protected Attributes

UniqueIdMap uniqueid_to_index_
 hash map from unique id to index of features More...
 

Detailed Description

template<typename T>
class OpenMS::UniqueIdIndexer< T >

A base class for containers with elements derived from UniqueIdInterface. This adds functionality to convert a unique id into an index into the container.

The derived class needs a data member called data_ which holds the actual elements derived from UniqueIdInterface. This is classical CRTP with the additional requirement that the derived class needs to declare a .getData() member function.

See FeatureMap and ConsensusMap for living examples. The RandomAccessContainer returned by .getData() must support operator[], at(), and size().

Member Typedef Documentation

◆ UniqueIdMap

typedef std::unordered_map<UInt64, Size> UniqueIdMap

Member Function Documentation

◆ getBase_() [1/2]

auto& getBase_ ( )
inlineprotected

A little helper to get access to the base (!) class RandomAccessContainer.

◆ getBase_() [2/2]

const auto& getBase_ ( ) const
inlineprotected

A little helper to get access to the base (!) class RandomAccessContainer.

Referenced by UniqueIdIndexer< T >::resolveUniqueIdConflicts(), UniqueIdIndexer< T >::uniqueIdToIndex(), and UniqueIdIndexer< T >::updateUniqueIdToIndex().

◆ resolveUniqueIdConflicts()

Size resolveUniqueIdConflicts ( )
inline

Assign new UID's to doubly occurring UID's.

Assign new UID's to non-unique UID's. This usually occurs in merging of 'old' feature files, which have sequentially increasing UID's. Conflicting entries receive a new UID, such that all UID's are unique in the container.

Note
Subordinate features are not checked and may remain non-unique. However, they are associated to their parent which makes identification 'unique'.
Returns
The number of invalid (=replaced) elements

References UniqueIdIndexer< T >::getBase_(), UniqueIdInterface::isValid(), and UniqueIdIndexer< T >::uniqueid_to_index_.

◆ swap()

void swap ( UniqueIdIndexer< T > &  rhs)
inline

Swap.

Note
obviously we can swap only with indices for the same type.

References UniqueIdIndexer< T >::uniqueid_to_index_.

◆ uniqueIdToIndex()

Size uniqueIdToIndex ( UInt64  unique_id) const
inline

Returns the index of the feature with the given unique id, or Size(-1) if none exists in this random access container.

The complexity is expected constant upon success, linear upon failure.

The lookup actually performs the following steps:

  • consult the internal hash map (i.e., uniqueid_to_index_)
  • if an index is found and that element indeed has this unique id, then return the index
  • if no index is found or the unique ids do not match, then update the hash map (using updateUniqueIdToIndex()) and lookup the index again
  • if an index is found this time, return it, otherwise return Size(-1)
Note
that subordinate elements are not considered here.

References UniqueIdIndexer< T >::getBase_(), UniqueIdIndexer< T >::uniqueid_to_index_, and UniqueIdIndexer< T >::updateUniqueIdToIndex().

◆ updateUniqueIdToIndex()

void updateUniqueIdToIndex ( ) const
inline

Member Data Documentation

◆ uniqueid_to_index_

UniqueIdMap uniqueid_to_index_
mutableprotected

hash map from unique id to index of features

This is mutable because the hash map is updated on demand, even if the underlying container is const.

Referenced by UniqueIdIndexer< T >::resolveUniqueIdConflicts(), UniqueIdIndexer< T >::swap(), UniqueIdIndexer< T >::uniqueIdToIndex(), and UniqueIdIndexer< T >::updateUniqueIdToIndex().