Skip to content

Commit

Permalink
feat: add LengthDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
poyea authored Jul 25, 2022
1 parent 5d6cab0 commit aae15c0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
cmake_minimum_required(VERSION 3.16..3.23)

project(bk-tree
VERSION 0.1.1
VERSION 0.1.3
LANGUAGES CXX
DESCRIPTION "Header-only Burkhard-Keller tree library"
HOMEPAGE_URL "https://github.com/poyea/bk-tree")
Expand Down
16 changes: 16 additions & 0 deletions bktree/bktree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ class Distance {
}
};

/**
* @brief Length metric
*
* \f$d(x, y) = |m - n|\f$ where
* \f$m\f$ is the length of \f$x\f$, and \f$n\f$ is the length of \f$y\f$, for any \f$x,
* y.\f$
*/
class LengthDistance final : public Distance<LengthDistance> {
public:
explicit LengthDistance(){};
integer_type compute_distance(std::string_view s, std::string_view t) const {
std::int64_t const d = s.length() - t.length();
return d >= 0 ? d : -d;
}
};

/**
* @brief Identity metric
*
Expand Down
34 changes: 34 additions & 0 deletions tests/distance_length.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "gtest/gtest.h"

#include "bktree.hpp"

namespace bk_tree_test {

class Distance_Length_TEST : public ::testing::Test {
protected:
Distance_Length_TEST() {}

virtual ~Distance_Length_TEST() {}

virtual void SetUp() {
// post-construction
}

virtual void TearDown() {
// pre-destruction
}

bk_tree::metrics::LengthDistance dist;
};

TEST_F(Distance_Length_TEST, LengthDistances) {
EXPECT_TRUE(dist("", "") == 0);
EXPECT_TRUE(dist("peter", "piter") == 0);
EXPECT_TRUE(dist("peter", "pitar") == 0);
EXPECT_TRUE(dist("peter", "pitat") == 0);
EXPECT_TRUE(dist("ca", "abc") == 1);
EXPECT_TRUE(dist("peter", "") == 5);
EXPECT_TRUE(dist("", "peter") == 5);
}

} // namespace bk_tree_test

0 comments on commit aae15c0

Please sign in to comment.