Simple dictionary in C++


Moving some code from Python to C++.

BASEPAIRS = { "T": "A", "A": "T", "G": "C", "C": "G" }

Thinking maps might be overkill? What would you use?


If you are into optimization, and assuming the input is always one of the four characters, the function below might be worth a try as a replacement for the map:

char map(const char in)
{ return ((in & 2) ? 'x8a' - in : 'x95' - in); }

It works based on the fact that you are dealing with two symmetric pairs. The conditional works to tell apart the A/T pair from the G/C one (‘G’ and ‘C’ happen to have the second-least-significant bit in common). The remaining arithmetics performs the symmetric mapping. It’s based on the fact that a = (a + b) – b is true for any a,b.


Leave a Reply

Your email address will not be published. Required fields are marked *