libsequence  1.9.5
CountingOperators.hpp File Reference

Declarations of operators to add associative containers together. More...

#include <algorithm>
#include <functional>
#include <map>
#include <vector>
#include <set>
#include <Sequence/bits/CountingOperators.tcc>

Go to the source code of this file.

Namespaces

 Sequence
 The namespace in which this library resides.
 

Functions

bool operator() (const std::pair< key, value > &l, const std::pair< key, value > &r) const
 
template<typename key , typename value >
std::vector< std::pair< key, value > > Sequence::operator+ (const std::vector< std::pair< key, value > > &lhs, const std::vector< std::pair< key, value > > &rhs)
 
template<typename key , typename value >
std::vector< std::pair< key, value > > Sequence::operator+= (std::vector< std::pair< key, value > > &lhs, const std::vector< std::pair< key, value > > &rhs)
 
template<typename key , typename value , typename comparison >
std::map< key, value, comparison > Sequence::operator+ (const std::map< key, value, comparison > &lhs, const std::map< key, value, comparison > &rhs)
 
template<typename key , typename value , typename comparison >
std::map< key, value, comparison > Sequence::operator+= (std::map< key, value, comparison > &lhs, const std::map< key, value, comparison > &rhs)
 

Detailed Description

Declarations of operators to add associative containers together.

A lot of biological computation involves counting. Associative containers such as vectors of pairs or maps (or hashes if you've got them) are natural data structures to keeps track of counts. However, keeping track of a running total of counts (say codon usage summed accross different sequences) is hampered by the lack of addition operators for associative containers. This file defines template operator+ and += for std::vector<std::pair<key,I> > and std::map<key,I>. The results of these operations are best explained by example:

#include <map>
std::map<char,unsigned> baseCounts,baseCounts2;
baseCounts['A'] = 5;
baseCounts['G'] = 10;
baseCounts2['A'] = 11;
baseCounts2['C'] = 17;
std::map<char,unsigned> baseCounts3 = baseCounts + baseCounts2;
for (std::map<char,unsigned>::const_iterator i = baseCounts3.begin() ;
i != baseCounts3.end() ;
++i)
{
std::cout << i->first << '\t' << i->second << '\n';
}

The output of the above will be (possibly in a different order): A 16
C 17
G 10
Therefore, the result of the addition operation is an associative container containing all the "key" elements of the 2 containers being added. And when those containers contain common keys, the values associated with those keys are summed.

These operators are not restricted in terms of types for the template arguments. The only requirements are that operator== is defined for the key, and operator+ must be valid for the value type.

Definition in file CountingOperators.hpp.

Function Documentation

◆ operator()()

bool operator() ( const std::pair< key, value > &  l,
const std::pair< key, value > &  r 
) const
inline
Returns
true if l.first == r.first, false otherwise
Examples:
codons.cc.

Definition at line 85 of file CountingOperators.hpp.