Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Add `NumericTrie::search_equal_to`.

Harpreet Sangar8daca328313a
diff --git a/include/numeric_range_trie_test.h b/include/numeric_range_trie_test.h
index cc980e3e..737cf5f9 100644
--- a/include/numeric_range_trie_test.h
+++ b/include/numeric_range_trie_test.h
@@ -41,6 +41,8 @@ class NumericTrie {
void search_less_than(const int32_t& value, uint32_t*& ids, uint32_t& ids_length);
void search_greater_than(const int32_t& value, uint32_t*& ids, uint32_t& ids_length);
+
+ void search_equal_to(const int32_t& value, uint32_t*& ids, uint32_t& ids_length);
};
Node* negative_trie = nullptr;
@@ -64,4 +66,6 @@ public:
void search_greater_than(const int32_t& value, const bool& inclusive,
uint32_t*& ids, uint32_t& ids_length);
+
+ void search_equal_to(const int32_t& value, uint32_t*& ids, uint32_t& ids_length);
};
diff --git a/src/numeric_range_trie.cpp b/src/numeric_range_trie.cpp
index 0c985765..cfa9e7e4 100644
--- a/src/numeric_range_trie.cpp
+++ b/src/numeric_range_trie.cpp
@@ -159,6 +159,19 @@ void NumericTrie::search_less_than(const int32_t& value, const bool& inclusive,
}
}
+void NumericTrie::search_equal_to(const int32_t& value, uint32_t*& ids, uint32_t& ids_length) {
+ uint32_t* match_ids = nullptr;
+ uint32_t match_ids_length = 0;
+ if (value < 0 && negative_trie != nullptr) {
+ negative_trie->search_equal_to(std::abs(value), match_ids, match_ids_length);
+ } else if (value >= 0 && positive_trie != nullptr) {
+ positive_trie->search_equal_to(value, match_ids, match_ids_length);
+ }
+
+ ids = match_ids;
+ ids_length = match_ids_length;
+}
+
void NumericTrie::Node::insert(const int32_t& value, const uint32_t& seq_id) {
char level = 0;
return insert_helper(value, seq_id, level);
@@ -347,3 +360,20 @@ void NumericTrie::Node::search_greater_than_helper(const int32_t& value, char& l
--level;
}
+
+void NumericTrie::Node::search_equal_to(const int32_t& value, uint32_t*& ids, uint32_t& ids_length) {
+ char level = 1;
+ Node* root = this;
+ auto index = get_index(value, level);
+
+ while (level <= MAX_LEVEL) {
+ if (root->children == nullptr || root->children[index] == nullptr) {
+ return;
+ }
+
+ root = root->children[index];
+ index = get_index(value, ++level);
+ }
+
+ root->get_all_ids(ids, ids_length);
+}
diff --git a/test/numeric_range_trie_test.cpp b/test/numeric_range_trie_test.cpp
index 28344882..baad5ef7 100644
--- a/test/numeric_range_trie_test.cpp
+++ b/test/numeric_range_trie_test.cpp
@@ -173,7 +173,7 @@ TEST_F(NumericRangeTrieTest, SearchRange) {
}
}
-TEST_F(NumericRangeTrieTest, SearchGreater) {
+TEST_F(NumericRangeTrieTest, SearchGreaterThan) {
auto trie = new NumericTrie();
std::unique_ptr<NumericTrie> trie_guard(trie);
std::vector<std::pair<int32_t, uint32_t>> pairs = {
@@ -266,7 +266,7 @@ TEST_F(NumericRangeTrieTest, SearchGreater) {
}
}
-TEST_F(NumericRangeTrieTest, SearchLesser) {
+TEST_F(NumericRangeTrieTest, SearchLessThan) {
auto trie = new NumericTrie();
std::unique_ptr<NumericTrie> trie_guard(trie);
std::vector<std::pair<int32_t, uint32_t>> pairs = {
@@ -357,6 +357,52 @@ TEST_F(NumericRangeTrieTest, SearchLesser) {
}
}
+TEST_F(NumericRangeTrieTest, SearchEqualTo) {
+ auto trie = new NumericTrie();
+ std::unique_ptr<NumericTrie> trie_guard(trie);
+ std::vector<std::pair<int32_t, uint32_t>> pairs = {
+ {-8192, 8},
+ {-16384, 32},
+ {-24576, 35},
+ {-32769, 41},
+ {-32768, 43},
+ {-32767, 45},
+ {8192, 49},
+ {16384, 56},
+ {24576, 58},
+ {32768, 91}
+ };
+
+ for (auto const& pair: pairs) {
+ trie->insert(pair.first, pair.second);
+ }
+
+ uint32_t* ids = nullptr;
+ uint32_t ids_length = 0;
+
+ trie->search_equal_to(0, ids, ids_length);
+ std::unique_ptr<uint32_t[]> ids_guard(ids);
+
+ ASSERT_EQ(0, ids_length);
+
+ trie->search_equal_to(-32768, ids, ids_length);
+ ids_guard.reset(ids);
+
+ ASSERT_EQ(1, ids_length);
+ ASSERT_EQ(43, ids[0]);
+
+ trie->search_equal_to(24576, ids, ids_length);
+ ids_guard.reset(ids);
+
+ ASSERT_EQ(1, ids_length);
+ ASSERT_EQ(58, ids[0]);
+
+ trie->search_equal_to(0x202020, ids, ids_length);
+ ids_guard.reset(ids);
+
+ ASSERT_EQ(0, ids_length);
+}
+
TEST_F(NumericRangeTrieTest, MultivalueData) {
auto trie = new NumericTrie();
std::unique_ptr<NumericTrie> trie_guard(trie);
@@ -507,4 +553,9 @@ TEST_F(NumericRangeTrieTest, EmptyTrieOperations) {
ids_guard.reset(ids);
ASSERT_EQ(0, ids_length);
+
+ trie->search_equal_to(15, ids, ids_length);
+ ids_guard.reset(ids);
+
+ ASSERT_EQ(0, ids_length);
}