Simple dictionary in C++

前端 未结 8 1279

Moving some code from Python to C++.

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

Thinking maps might be overkill? W

8条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 16:56

    This is the fastest, simplest, smallest space solution I can think of. A good optimizing compiler will even remove the cost of accessing the pair and name arrays. This solution works equally well in C.

    #include 
    
    enum Base_enum { A, C, T, G };
    typedef enum Base_enum Base;
    static const Base pair[4] = { T, G, A, C };
    static const char name[4] = { 'A', 'C', 'T', 'G' };
    static const Base base[85] = 
      { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1,  A, -1,  C, -1, -1,
        -1,  G, -1, -1, -1, -1, -1, -1, -1, -1, 
        -1, -1, -1, -1,  T };
    
    const Base
    base2 (const char b)
    {
      switch (b)
        {
        case 'A': return A;
        case 'C': return C;
        case 'T': return T;
        case 'G': return G;
        default: abort ();
        }
    }
    
    int
    main (int argc, char *args) 
    {
      for (Base b = A; b <= G; b++)
        {
          std::cout << name[b] << ":" 
                    << name[pair[b]] << std::endl;
        }
      for (Base b = A; b <= G; b++)
        {
          std::cout << name[base[name[b]]] << ":" 
                    << name[pair[base[name[b]]]] << std::endl;
        }
      for (Base b = A; b <= G; b++)
        {
          std::cout << name[base2(name[b])] << ":" 
                    << name[pair[base2(name[b])]] << std::endl;
        }
    };
    

    base[] is a fast ascii char to Base (i.e. int between 0 and 3 inclusive) lookup that is a bit ugly. A good optimizing compiler should be able to handle base2() but I'm not sure if any do.

提交回复
热议问题