summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2014-01-19 19:43:19 +0000
committerMaxime Coste <frrrwww@gmail.com>2014-01-19 19:43:19 +0000
commitf6eaaf1e786f8d5913abf0f22cce48e0be12927c (patch)
tree0f9fad9a1c31fb89370a6547e2e0d782a0927576 /src
parenta2c58d40b82a5bdc6a5a120a5542e3e3b9285ea3 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/word_db.cc7
-rw-r--r--src/word_db.hh2
2 files changed, 5 insertions, 4 deletions
diff --git a/src/word_db.cc b/src/word_db.cc
index c64ae01a..1e2d8a04 100644
--- a/src/word_db.cc
+++ b/src/word_db.cc
@@ -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;
}
diff --git a/src/word_db.hh b/src/word_db.hh
index c7f8a83d..bbbfa04c 100644
--- a/src/word_db.hh
+++ b/src/word_db.hh
@@ -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);