This commit is contained in:
kampersanda 2022-07-10 14:37:25 +09:00
parent 5ad5bdd29c
commit feae2f1830
2 changed files with 13 additions and 5 deletions

View file

@ -168,17 +168,25 @@ class tail_vector {
// Returns true if TAIL[tpos..epos] is a prefix of key, where epos is the end position of the tail. // Returns true if TAIL[tpos..epos] is a prefix of key, where epos is the end position of the tail.
inline bool prefix_match(std::string_view key, std::uint64_t tpos) const { inline bool prefix_match(std::string_view key, std::uint64_t tpos) const {
assert(key.size() != 0); if (tpos == 0) {
// suffix is empty, returns true always.
return true;
}
if (key.size() == 0) {
// When key is empty, returns true iff suffix is empty.
return false;
}
std::uint64_t kpos = 0; std::uint64_t kpos = 0;
if (bin_mode()) { if (bin_mode()) {
do { do {
if (m_terms[tpos]) {
return true;
}
if (key[kpos] != m_chars[tpos]) { if (key[kpos] != m_chars[tpos]) {
return false; return false;
} }
kpos += 1; kpos += 1;
if (m_terms[tpos]) {
return true;
}
tpos += 1; tpos += 1;
} while (kpos < key.size()); } while (kpos < key.size());
return true; return true;

View file

@ -167,7 +167,7 @@ TEST_CASE("Test " TRIE_NAME " (tiny)") {
test_basic_operations(trie, keys, others); test_basic_operations(trie, keys, others);
{ {
auto itr = trie.make_prefix_iterator("MacBook_Pro"); auto itr = trie.make_prefix_iterator("MacBook_Pro_13inch");
std::vector<std::string> expected = {"Mac", "MacBook", "MacBook_Pro"}; std::vector<std::string> expected = {"Mac", "MacBook", "MacBook_Pro"};
for (const auto& exp : expected) { for (const auto& exp : expected) {
REQUIRE(itr.next()); REQUIRE(itr.next());