diff --git a/src/setriq/_C/PairwiseDistanceComputer.cpp b/src/setriq/_C/PairwiseDistanceComputer.cpp index 2dfd396..901cd3d 100644 --- a/src/setriq/_C/PairwiseDistanceComputer.cpp +++ b/src/setriq/_C/PairwiseDistanceComputer.cpp @@ -10,15 +10,15 @@ double_vector_t PairwiseDistanceComputer::compute_distance(const string_vector_t * the method will be multi-threaded. * * @param input_strings: a vector of input strings - * @return a flat (N * (N - 1)) / 2 vector of doubles + * @return a flat (n * (n - 1)) / 2 vector of doubles */ - const auto& N = input_strings.size(); - double_vector_t distance_matrix(N * (N - 1) / 2); + const auto& n = input_strings.size(); + double_vector_t distance_matrix(n * (n - 1) / 2); - #pragma omp parallel for default(none) shared(input_strings, distance_matrix, N) - for (size_t i = 0; i < N - 1; i++) { - for (size_t j = i + 1; j < N; j++) { - size_t index = (N * (N - 1)) / 2 - (N - i) * ((N - i) - 1) / 2 + j - i - 1; +#pragma omp parallel for default(none) firstprivate(n) shared(input_strings, distance_matrix) + for (size_t i = 0; i < n - 1; i++) { + for (size_t j = i + 1; j < n; j++) { + size_t index = (n * (n - 1)) / 2 - (n - i) * ((n - i) - 1) / 2 + j - i - 1; distance_matrix[index] = (*this->distance_metric_)(input_strings[i], input_strings[j]); } } diff --git a/src/setriq/_C/alignment/SmithWaterman.cpp b/src/setriq/_C/alignment/SmithWaterman.cpp index c91efa2..4499d87 100644 --- a/src/setriq/_C/alignment/SmithWaterman.cpp +++ b/src/setriq/_C/alignment/SmithWaterman.cpp @@ -119,11 +119,11 @@ double SmithWaterman::identity_score(const std::string &input_string) { * @param input_string: an input string to be aligned with itself * @return the maximal self-alignment score for an input string */ - const auto& N = input_string.size(); + const auto* end = &input_string.back() + 1; - double score {0}; - for (size_t i = 0; i < N; i++) { - score += this->substitution_matrix_(input_string[i], input_string[i]); + auto&& score = 0.f; + for (auto* ptr = &input_string.front(); ptr != end; ptr++) { + score += this->substitution_matrix_(*ptr, *ptr); } return score; } diff --git a/src/setriq/_C/metrics/TcrDist.cpp b/src/setriq/_C/metrics/TcrDist.cpp index 548c4eb..79b72b1 100644 --- a/src/setriq/_C/metrics/TcrDist.cpp +++ b/src/setriq/_C/metrics/TcrDist.cpp @@ -31,18 +31,24 @@ double metric::TcrDist::forward(const std::string &a, const std::string &b) { * @param b: another string to be compared * @return TcrDist metric between the two strings */ - const auto& max_distance {4.}; + constexpr double max_distance = 4; + const auto& n = a.size(); - double distance {0}, substitution; - for (size_t i = 0; i < a.size(); i++) { - if (a[i] == b[i]) continue; + const auto* ptr_a = &a.front(); + const auto* ptr_b = &b.front(); + auto &&distance = 0.f; + for (size_t i = 0; i < n; i++) { + const auto& _a = *(ptr_a + i); + const auto& _b = *(ptr_b + i); + if (_a == _b) + continue; - if (a[i] == this->gap_symbol_ || b[i] == this->gap_symbol_) { + if (_a == this->gap_symbol_ || _b == this->gap_symbol_) { distance += this->gap_penalty_; continue; } - substitution = max_distance - this->substitution_matrix_(a[i], b[i]); + const auto& substitution = max_distance - this->substitution_matrix_(_a, _b); distance += std::min(max_distance, substitution); } return distance * this->distance_weight_;