WordDB: use an ordered map for storing words

This way we can use lower_bound to find the first prefix match
in logarithm time and we know all other prefix matches will
follow.
This commit is contained in:
Maxime Coste 2014-01-19 19:43:19 +00:00
parent a2c58d40b8
commit f6eaaf1e78
2 changed files with 5 additions and 4 deletions

View File

@ -103,10 +103,11 @@ void WordDB::on_erase(const Buffer& buffer, BufferCoord begin, BufferCoord end)
std::vector<String> WordDB::find_prefix(const String& prefix) const
{
std::vector<String> res;
for (auto& word : m_word_to_lines)
for (auto it = m_word_to_lines.lower_bound(prefix); it != m_word_to_lines.end(); ++it)
{
if (prefix_match(word.first, prefix))
res.push_back(word.first);
if (not prefix_match(it->first, prefix))
break;
res.push_back(it->first);
}
return res;
}

View File

@ -22,7 +22,7 @@ public:
std::vector<String> find_prefix(const String& prefix) const;
private:
using WordToLines = std::unordered_map<String, std::vector<LineCount>>;
using WordToLines = std::map<String, std::vector<LineCount>>;
using LineToWords = std::map<LineCount, std::vector<String>>;
void add_words(LineCount line, const String& content);