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

Refactor `NumericTrie::search_less_than`.

Harpreet Sangarce4fcae76bb8
diff --git a/src/numeric_range_trie.cpp b/src/numeric_range_trie.cpp
index 5ce81287..5b397716 100644
--- a/src/numeric_range_trie.cpp
+++ b/src/numeric_range_trie.cpp
@@ -146,42 +146,68 @@ void NumericTrie::search_greater_than(const int32_t& value, const bool& inclusiv
void NumericTrie::search_less_than(const int32_t& value, const bool& inclusive, uint32_t*& ids, uint32_t& ids_length) {
if ((value == 0 && !inclusive) || (value == -1 && inclusive)) { // (-∞, 0), (-∞, -1]
if (negative_trie != nullptr) {
- negative_trie->get_all_ids(ids, ids_length);
+ uint32_t* negative_ids = nullptr;
+ uint32_t negative_ids_length = 0;
+ negative_trie->get_all_ids(negative_ids, negative_ids_length);
+
+ uint32_t* out = nullptr;
+ ids_length = ArrayUtils::or_scalar(negative_ids, negative_ids_length, ids, ids_length, &out);
+
+ delete [] negative_ids;
+ delete [] ids;
+ ids = out;
}
return;
}
if (value < 0) {
+ if (negative_trie == nullptr) {
+ return;
+ }
+
uint32_t* negative_ids = nullptr;
uint32_t negative_ids_length = 0;
+ auto abs_low = std::abs(value);
+
// Since we store absolute values, search_greater would yield result for <value from negative_trie.
- if (negative_trie != nullptr) {
- auto abs_low = std::abs(value);
- negative_trie->search_greater_than(inclusive ? abs_low : abs_low + 1, negative_ids, negative_ids_length);
- }
+ negative_trie->search_greater_than(inclusive ? abs_low : abs_low + 1, negative_ids, negative_ids_length);
- ids_length = negative_ids_length;
- ids = negative_ids;
+ uint32_t* out = nullptr;
+ ids_length = ArrayUtils::or_scalar(negative_ids, negative_ids_length, ids, ids_length, &out);
+
+ delete [] negative_ids;
+ delete [] ids;
+ ids = out;
} else {
// Have to combine the results of <value from positive_trie and all the ids in negative_trie
- uint32_t* positive_ids = nullptr;
- uint32_t positive_ids_length = 0;
if (positive_trie != nullptr) {
+ uint32_t* positive_ids = nullptr;
+ uint32_t positive_ids_length = 0;
positive_trie->search_less_than(inclusive ? value : value - 1, positive_ids, positive_ids_length);
+
+ uint32_t* out = nullptr;
+ ids_length = ArrayUtils::or_scalar(positive_ids, positive_ids_length, ids, ids_length, &out);
+
+ delete [] positive_ids;
+ delete [] ids;
+ ids = out;
+ }
+
+ if (negative_trie == nullptr) {
+ return;
}
uint32_t* negative_ids = nullptr;
uint32_t negative_ids_length = 0;
- if (negative_trie != nullptr) {
- negative_trie->get_all_ids(negative_ids, negative_ids_length);
- }
+ negative_trie->get_all_ids(negative_ids, negative_ids_length);
- ids_length = ArrayUtils::or_scalar(positive_ids, positive_ids_length, negative_ids, negative_ids_length, &ids);
+ uint32_t* out = nullptr;
+ ids_length = ArrayUtils::or_scalar(negative_ids, negative_ids_length, ids, ids_length, &out);
delete [] negative_ids;
- delete [] positive_ids;
- return;
+ delete [] ids;
+ ids = out;
}
}
diff --git a/test/numeric_range_trie_test.cpp b/test/numeric_range_trie_test.cpp
index 6a45054a..a4e5cb8a 100644
--- a/test/numeric_range_trie_test.cpp
+++ b/test/numeric_range_trie_test.cpp
@@ -295,73 +295,74 @@ TEST_F(NumericRangeTrieTest, SearchLessThan) {
uint32_t ids_length = 0;
trie->search_less_than(0, true, ids, ids_length);
- std::unique_ptr<uint32_t[]> ids_guard(ids);
ASSERT_EQ(4, ids_length);
for (uint32_t i = 4, j = 0; i < ids_length; i++, j++) {
ASSERT_EQ(pairs[i].second, ids[j]);
}
+ reset(ids, ids_length);
trie->search_less_than(0, false, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(4, ids_length);
for (uint32_t i = 0; i < ids_length; i++) {
ASSERT_EQ(pairs[i].second, ids[i]);
}
+ reset(ids, ids_length);
trie->search_less_than(-1, true, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(4, ids_length);
for (uint32_t i = 0; i < ids_length; i++) {
ASSERT_EQ(pairs[i].second, ids[i]);
}
+ reset(ids, ids_length);
trie->search_less_than(-16384, true, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(3, ids_length);
for (uint32_t i = 0; i < ids_length; i++) {
ASSERT_EQ(pairs[i].second, ids[i]);
}
+ reset(ids, ids_length);
trie->search_less_than(-16384, false, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(2, ids_length);
for (uint32_t i = 0; i < ids_length; i++) {
ASSERT_EQ(pairs[i].second, ids[i]);
}
+ reset(ids, ids_length);
trie->search_less_than(8192, true, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(5, ids_length);
for (uint32_t i = 0; i < ids_length; i++) {
ASSERT_EQ(pairs[i].second, ids[i]);
}
+ reset(ids, ids_length);
trie->search_less_than(8192, false, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(4, ids_length);
for (uint32_t i = 0; i < ids_length; i++) {
ASSERT_EQ(pairs[i].second, ids[i]);
}
+ reset(ids, ids_length);
trie->search_less_than(-1000000, false, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(0, ids_length);
+ reset(ids, ids_length);
trie->search_less_than(1000000, true, ids, ids_length);
- ids_guard.reset(ids);
ASSERT_EQ(8, ids_length);
for (uint32_t i = 0; i < ids_length; i++) {
ASSERT_EQ(pairs[i].second, ids[i]);
}
+
+ reset(ids, ids_length);
}
TEST_F(NumericRangeTrieTest, SearchEqualTo) {