Martinsos / edlib

Lightweight, super fast C/C++ (& Python) library for sequence alignment using edit (Levenshtein) distance.
http://martinsos.github.io/edlib
MIT License
493 stars 162 forks source link

Add more const #158

Closed SoapZA closed 3 years ago

SoapZA commented 3 years ago

Hi @Martinsos a small PR to add some useful const qualifers. Without these const qualifiers, the following pattern isn't possible:

// we want to use edlib in case-insensitive mode
constexpr std::array<EdlibEqualityPair, 5> EdlibCaseInsensitiveArray{{
    {'A', 'a'}, {'C', 'c'}, {'G', 'g'}, {'T', 't'}, {'N', 'n'},
}};

const EdlibAlignConfig edlibConfig{
    -1,
    EDLIB_MODE_NW,
    EDLIB_TASK_PATH,
    EdlibCaseInsensitiveArray.data(),
    EdlibCaseInsensitiveArray.size(),
};

void foo() {
  [...]
  const auto alnResult =
                edlibAlign(query.c_str(), query.size(), target.c_str(), target.size(), edlibConfig);
  [...]
}

because the std::array would return a const-qualified EdlibEqualityPair*. With this pattern, all parameters get burned into read-only memory on compilation, which improves cache lookup and makes code safer.

Martinsos commented 3 years ago

Great @SoapZA , thanks, this makes perfect sense! Merging.