Skip to content

Commit 957dbe8

Browse files
committed
Make a few optimisations
1 parent 806222d commit 957dbe8

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

bktree/bktree.hpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Distance {
7979
class LengthDistance final : public Distance<LengthDistance> {
8080
public:
8181
explicit LengthDistance(){};
82-
integer_type compute_distance(std::string_view s, std::string_view t) const {
82+
integer_type compute_distance(std::string_view s, std::string_view t) const noexcept {
8383
return s.length() > t.length() ? s.length() - t.length() : t.length() - s.length();
8484
}
8585
};
@@ -92,7 +92,7 @@ class LengthDistance final : public Distance<LengthDistance> {
9292
class IdentityDistance final : public Distance<IdentityDistance> {
9393
public:
9494
explicit IdentityDistance(){};
95-
integer_type compute_distance(std::string_view, std::string_view) const {
95+
integer_type compute_distance(std::string_view, std::string_view) const noexcept {
9696
return integer_type{1};
9797
}
9898
};
@@ -110,14 +110,14 @@ class LeeDistance final : public Distance<LeeDistance> {
110110
public:
111111
explicit LeeDistance(integer_type alphabet_size = BK_LEE_ALPHABET_SIZE)
112112
: m_alphabet_size(alphabet_size){};
113-
integer_type compute_distance(std::string_view s, std::string_view t) const {
113+
integer_type compute_distance(std::string_view s, std::string_view t) const noexcept {
114114
const integer_type M = s.length(), N = t.length();
115115
if (M != N) {
116116
return std::numeric_limits<integer_type>::max();
117117
}
118-
const integer_type m_comparsion_size = M;
118+
const integer_type m_comparison_size = M;
119119
integer_type counter = 0, diff;
120-
for (integer_type i = 0; i < m_comparsion_size; ++i) {
120+
for (integer_type i = 0; i < m_comparison_size; ++i) {
121121
diff = std::abs(s[i] - t[i]);
122122
counter += std::min(diff, m_alphabet_size - diff);
123123
}
@@ -146,7 +146,7 @@ class LCSubseqDistance final : public Distance<LCSubseqDistance> {
146146
public:
147147
explicit LCSubseqDistance(size_t initial_size = BK_LCS_MATRIX_INITIAL_SIZE)
148148
: m_current(initial_size), m_previous(initial_size){};
149-
integer_type compute_distance(std::string_view s, std::string_view t) const {
149+
integer_type compute_distance(std::string_view s, std::string_view t) const noexcept {
150150
const integer_type M = s.length(), N = t.length();
151151
if (M == 0 || N == 0) {
152152
return 0;
@@ -180,14 +180,14 @@ class LCSubseqDistance final : public Distance<LCSubseqDistance> {
180180
class HammingDistance final : public Distance<HammingDistance> {
181181
public:
182182
explicit HammingDistance() = default;
183-
integer_type compute_distance(std::string_view s, std::string_view t) const {
183+
integer_type compute_distance(std::string_view s, std::string_view t) const noexcept {
184184
const integer_type M = s.length(), N = t.length();
185185
if (M != N) {
186186
return std::numeric_limits<integer_type>::max();
187187
}
188-
const integer_type m_comparsion_size = M;
188+
const integer_type m_comparison_size = M;
189189
integer_type counter = 0;
190-
for (integer_type i = 0; i < m_comparsion_size; ++i) {
190+
for (integer_type i = 0; i < m_comparison_size; ++i) {
191191
counter += (s[i] != t[i]);
192192
}
193193
return counter;
@@ -224,7 +224,7 @@ class EditDistance final : public Distance<EditDistance> {
224224
public:
225225
explicit EditDistance(size_t initial_size = BK_ED_MATRIX_INITIAL_SIZE)
226226
: m_matrix(initial_size, std::vector<integer_type>(initial_size)){};
227-
integer_type compute_distance(std::string_view s, std::string_view t) const {
227+
integer_type compute_distance(std::string_view s, std::string_view t) const noexcept {
228228
const integer_type M = s.length(), N = t.length();
229229
if (M == 0 || N == 0) {
230230
return N + M;
@@ -262,7 +262,7 @@ class DamerauLevenshteinDistance final : public Distance<DamerauLevenshteinDista
262262
public:
263263
explicit DamerauLevenshteinDistance(size_t initial_size = BK_MATRIX_INITIAL_SIZE)
264264
: m_matrix(initial_size, std::vector<integer_type>(initial_size)){};
265-
integer_type compute_distance(std::string_view s, std::string_view t) const {
265+
integer_type compute_distance(std::string_view s, std::string_view t) const noexcept {
266266
const integer_type M = s.length(), N = t.length();
267267
if (M == 0 || N == 0) {
268268
return N + M;
@@ -322,9 +322,9 @@ class BKTreeNode {
322322
BKTreeNode(std::string_view value) : m_word(value) {}
323323
bool _insert(std::string_view value, const metric_type &distance);
324324
bool _erase(std::string_view value, const metric_type &distance);
325-
void _find(ResultList &output, std::string_view value, const int &limit,
325+
void _find(ResultList &output, std::string_view value, int limit,
326326
const metric_type &metric) const;
327-
ResultList _find_wrapper(std::string_view value, const int &limit,
327+
ResultList _find_wrapper(std::string_view value, int limit,
328328
const metric_type &metric) const;
329329

330330
std::map<int, std::unique_ptr<node_type>> m_children;
@@ -456,7 +456,7 @@ class BKTree {
456456
bool erase(std::string_view value);
457457
size_t size() const noexcept { return m_tree_size; }
458458
bool empty() const noexcept { return m_tree_size == 0; }
459-
[[nodiscard]] ResultList find(std::string_view value, const int &limit) const;
459+
[[nodiscard]] ResultList find(std::string_view value, int limit) const;
460460

461461
Iterator begin() { return Iterator(&m_root); }
462462
Iterator end() { return Iterator(); }
@@ -523,7 +523,7 @@ bool BKTreeNode<Metric>::_erase(std::string_view value,
523523

524524
template <typename Metric>
525525
void BKTreeNode<Metric>::_find(ResultList &output, std::string_view value,
526-
const int &limit, const metric_type &metric) const {
526+
int limit, const metric_type &metric) const {
527527
const int distance = metric(value, m_word);
528528
if (distance <= limit) {
529529
output.push_back({m_word, distance});
@@ -536,7 +536,7 @@ void BKTreeNode<Metric>::_find(ResultList &output, std::string_view value,
536536
}
537537

538538
template <typename Metric>
539-
ResultList BKTreeNode<Metric>::_find_wrapper(std::string_view value, const int &limit,
539+
ResultList BKTreeNode<Metric>::_find_wrapper(std::string_view value, int limit,
540540
const metric_type &metric) const {
541541
ResultList output;
542542
_find(output, value, limit, metric);
@@ -595,7 +595,7 @@ bool BKTree<Metric>::erase(std::string_view value) {
595595
}
596596

597597
template <typename Metric>
598-
ResultList BKTree<Metric>::find(std::string_view value, const int &limit) const {
598+
ResultList BKTree<Metric>::find(std::string_view value, int limit) const {
599599
if (m_root == nullptr) {
600600
return ResultList{};
601601
}

tests/distance_damerau.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ TEST_F(Distance_DamerauLevenshtein_TEST, DamerauLevenshteinDistances) {
3030
EXPECT_TRUE(dist("peter", "") == 5);
3131
EXPECT_TRUE(dist("", "peter") == 5);
3232
EXPECT_TRUE(dist("", "") == 0);
33+
EXPECT_TRUE(dist("abc", "bac") == 1);
34+
EXPECT_TRUE(dist("abc", "acb") == 1);
35+
EXPECT_TRUE(dist("abcd", "abdc") == 1);
3336
}
3437

3538
} // namespace bk_tree_test

0 commit comments

Comments
 (0)