This commit is contained in:
kampersanda 2022-07-10 13:39:06 +09:00
parent b5b40e9fdc
commit 5ad5bdd29c
2 changed files with 12 additions and 10 deletions

View file

@ -166,25 +166,28 @@ class tail_vector {
} }
} }
// 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); assert(key.size() != 0);
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 kpos == key.size();
}
tpos += 1; tpos += 1;
} while (kpos < key.size()); } while (kpos < key.size());
return true; return true;
} else { } else {
do { do {
if (!m_chars[tpos] || key[kpos] != m_chars[tpos]) { if (!m_chars[tpos]) {
return true;
}
if (key[kpos] != m_chars[tpos]) {
return false; return false;
} }
kpos += 1; kpos += 1;

View file

@ -166,7 +166,7 @@ class trie {
class prefix_iterator { class prefix_iterator {
private: private:
const trie_type* m_obj = nullptr; const trie_type* m_obj = nullptr;
std::string m_key; std::string_view m_key;
std::uint64_t m_id = 0; std::uint64_t m_id = 0;
std::uint64_t m_kpos = 0; std::uint64_t m_kpos = 0;
std::uint64_t m_npos = 0; std::uint64_t m_npos = 0;
@ -231,7 +231,7 @@ class trie {
private: private:
const trie_type* m_obj = nullptr; const trie_type* m_obj = nullptr;
std::string m_key; std::string_view m_key;
std::uint64_t m_id = 0; std::uint64_t m_id = 0;
std::string m_decoded; std::string m_decoded;
std::vector<cursor_type> m_stack; std::vector<cursor_type> m_stack;
@ -317,8 +317,7 @@ class trie {
: m_num_keys(b.m_keys.size()), m_table(std::move(b.m_table)), m_terms(b.m_terms, true, true), : m_num_keys(b.m_keys.size()), m_table(std::move(b.m_table)), m_terms(b.m_terms, true, true),
m_bcvec(b.m_units, std::move(b.m_leaves)), m_tvec(std::move(b.m_suffixes)) {} m_bcvec(b.m_units, std::move(b.m_leaves)), m_tvec(std::move(b.m_suffixes)) {}
template <class String> static constexpr std::string_view get_suffix(std::string_view s, std::uint64_t i) {
static constexpr String get_suffix(const String& s, std::uint64_t i) {
assert(i <= s.size()); assert(i <= s.size());
return s.substr(i, s.size() - i); return s.substr(i, s.size() - i);
} }
@ -376,7 +375,7 @@ class trie {
itr->is_end = true; itr->is_end = true;
const std::uint64_t tpos = m_bcvec.link(itr->m_npos); const std::uint64_t tpos = m_bcvec.link(itr->m_npos);
if (!m_tvec.match(get_suffix(itr->m_key, itr->m_kpos), tpos)) { if (!m_tvec.prefix_match(get_suffix(itr->m_key, itr->m_kpos), tpos)) {
itr->m_id = num_keys(); itr->m_id = num_keys();
return false; return false;
} }